test.c 1.7 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2023 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 <wolfcrypt/port/Espressif/esp32-crypt.h> /* */
  126. #elif defined(WOLFSSL_ZEPHYR)
  127. #include <stdio.h>
  128. #define printf printk
  129. #elif defined(MICRIUM)
  130. #include <os.h>
  131. #if (OS_VERSION < 50000)
  132. #include <bsp_ser.h>
  133. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  134. #undef printf
  135. #define printf BSP_Ser_Printf
  136. #else
  137. #include <stdio.h>
  138. #endif
  139. #elif defined(WOLFSSL_PB)
  140. #include <stdarg.h>
  141. int wolfssl_pb_print(const char*, ...);
  142. #undef printf
  143. #define printf wolfssl_pb_print
  144. #elif defined(WOLFSSL_TELIT_M2MB)
  145. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  146. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  147. /* remap printf */
  148. #undef printf
  149. #define printf M2M_LOG_INFO
  150. /* OS requires occasional sleep() */
  151. #ifndef TEST_SLEEP_MS
  152. #define TEST_SLEEP_MS 50
  153. #endif
  154. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  155. /* don't use file system for these tests, since ./certs dir isn't loaded */
  156. #undef NO_FILESYSTEM
  157. #define NO_FILESYSTEM
  158. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  159. !defined(THREADX_NO_DC_PRINTF)
  160. #ifndef NETOS
  161. /* since just testing, use THREADX log printf instead (NETOS prototypes
  162. * this elsewhere) */
  163. int dc_log_printf(char*, ...);
  164. #endif
  165. #undef printf
  166. #define printf dc_log_printf
  167. #elif defined(ANDROID)
  168. #ifdef XMALLOC_USER
  169. #include <stdlib.h> /* we're using malloc / free direct here */
  170. #endif
  171. #ifndef STRING_USER
  172. #include <stdio.h>
  173. #endif
  174. #include <android/log.h>
  175. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  176. #ifndef NO_FILESYSTEM
  177. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  178. #endif
  179. #else
  180. #define printf(...) \
  181. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  182. #define fprintf(fp, ...) \
  183. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  184. #endif
  185. #elif defined(WOLFSSL_DEOS)
  186. #include <printx.h>
  187. #undef printf
  188. #define printf printx
  189. #elif defined(WOLFSSL_RENESAS_RSIP)
  190. #ifndef TEST_SLEEP
  191. #define TEST_SLEEP() vTaskDelay(50)
  192. #endif
  193. #undef vprintf
  194. #define vprintf rsip_vprintf
  195. #include <stdarg.h> /* for var args */
  196. int rsip_vprintf(const char* restrict format, va_list args)
  197. {
  198. int ret;
  199. char tmpBuf[80];
  200. ret = XSNPRINTF(tmpBuf, sizeof(tmpBuf), format, args);
  201. printf(tmpBuf);
  202. return ret;
  203. }
  204. #else
  205. #ifdef XMALLOC_USER
  206. #include <stdlib.h> /* we're using malloc / free direct here */
  207. #endif
  208. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  209. #include <stdio.h>
  210. #endif
  211. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  212. #undef printf
  213. #define printf(...) ({})
  214. #endif
  215. /* enable way for customer to override test/bench printf */
  216. #ifdef XPRINTF
  217. #undef printf
  218. #define printf XPRINTF
  219. #elif !defined(printf)
  220. /* arrange for printf() to flush after every message -- this assures
  221. * redirected output (to a log file) records progress right up to the
  222. * moment of a crash/abort(); otherwise anything queued in stdout would
  223. * be lost.
  224. */
  225. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  226. #endif
  227. #endif
  228. #endif /* !WOLFSSL_LOG_PRINTF */
  229. #include <wolfssl/wolfcrypt/memory.h>
  230. #include <wolfssl/wolfcrypt/wc_port.h>
  231. #include <wolfssl/wolfcrypt/logging.h>
  232. #include <wolfssl/wolfcrypt/types.h>
  233. #include <wolfssl/wolfcrypt/asn.h>
  234. #include <wolfssl/wolfcrypt/md2.h>
  235. #include <wolfssl/wolfcrypt/md5.h>
  236. #include <wolfssl/wolfcrypt/md4.h>
  237. #include <wolfssl/wolfcrypt/sha.h>
  238. #include <wolfssl/wolfcrypt/sha256.h>
  239. #include <wolfssl/wolfcrypt/sha512.h>
  240. #include <wolfssl/wolfcrypt/rc2.h>
  241. #include <wolfssl/wolfcrypt/arc4.h>
  242. #if !defined(WC_NO_RNG)
  243. #include <wolfssl/wolfcrypt/random.h>
  244. #endif
  245. #include <wolfssl/wolfcrypt/wolfmath.h>
  246. #include <wolfssl/wolfcrypt/coding.h>
  247. #include <wolfssl/wolfcrypt/signature.h>
  248. #include <wolfssl/wolfcrypt/rsa.h>
  249. #include <wolfssl/wolfcrypt/des3.h>
  250. #include <wolfssl/wolfcrypt/aes.h>
  251. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  252. #include <wolfssl/wolfcrypt/cmac.h>
  253. #include <wolfssl/wolfcrypt/siphash.h>
  254. #include <wolfssl/wolfcrypt/poly1305.h>
  255. #include <wolfssl/wolfcrypt/camellia.h>
  256. #include <wolfssl/wolfcrypt/hmac.h>
  257. #include <wolfssl/wolfcrypt/kdf.h>
  258. #include <wolfssl/wolfcrypt/dh.h>
  259. #include <wolfssl/wolfcrypt/dsa.h>
  260. #include <wolfssl/wolfcrypt/srp.h>
  261. #include <wolfssl/wolfcrypt/chacha.h>
  262. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  263. #include <wolfssl/wolfcrypt/pwdbased.h>
  264. #include <wolfssl/wolfcrypt/ripemd.h>
  265. #include <wolfssl/wolfcrypt/error-crypt.h>
  266. #ifdef HAVE_ECC
  267. #include <wolfssl/wolfcrypt/ecc.h>
  268. #endif
  269. #ifdef WOLFSSL_SM2
  270. #include <wolfssl/wolfcrypt/sm2.h>
  271. #endif
  272. #ifdef HAVE_HPKE
  273. #include <wolfssl/wolfcrypt/hpke.h>
  274. #endif
  275. #ifdef HAVE_CURVE25519
  276. #include <wolfssl/wolfcrypt/curve25519.h>
  277. #endif
  278. #ifdef HAVE_ED25519
  279. #include <wolfssl/wolfcrypt/ed25519.h>
  280. #endif
  281. #ifdef HAVE_CURVE448
  282. #include <wolfssl/wolfcrypt/curve448.h>
  283. #endif
  284. #ifdef HAVE_ED448
  285. #include <wolfssl/wolfcrypt/ed448.h>
  286. #endif
  287. #ifdef WOLFSSL_HAVE_KYBER
  288. #include <wolfssl/wolfcrypt/kyber.h>
  289. #ifdef WOLFSSL_WC_KYBER
  290. #include <wolfssl/wolfcrypt/wc_kyber.h>
  291. #endif
  292. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  293. #include <wolfssl/wolfcrypt/ext_kyber.h>
  294. #endif
  295. #endif
  296. #if defined(WOLFSSL_HAVE_XMSS)
  297. #include <wolfssl/wolfcrypt/xmss.h>
  298. #ifdef HAVE_LIBXMSS
  299. #include <wolfssl/wolfcrypt/ext_xmss.h>
  300. #endif
  301. #endif
  302. #if defined(WOLFSSL_HAVE_LMS)
  303. #include <wolfssl/wolfcrypt/lms.h>
  304. #ifdef HAVE_LIBLMS
  305. #include <wolfssl/wolfcrypt/ext_lms.h>
  306. #endif
  307. #endif
  308. #ifdef WOLFCRYPT_HAVE_ECCSI
  309. #include <wolfssl/wolfcrypt/eccsi.h>
  310. #endif
  311. #ifdef WOLFCRYPT_HAVE_SAKKE
  312. #include <wolfssl/wolfcrypt/sakke.h>
  313. #endif
  314. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  315. #include <wolfssl/wolfcrypt/blake2.h>
  316. #endif
  317. #ifdef WOLFSSL_SHA3
  318. #include <wolfssl/wolfcrypt/sha3.h>
  319. #endif
  320. #ifdef WOLFSSL_SM3
  321. #include <wolfssl/wolfcrypt/sm3.h>
  322. #endif
  323. #ifdef WOLFSSL_SM4
  324. #include <wolfssl/wolfcrypt/sm4.h>
  325. #endif
  326. #ifdef HAVE_LIBZ
  327. #include <wolfssl/wolfcrypt/compress.h>
  328. #endif
  329. #ifdef HAVE_PKCS7
  330. #include <wolfssl/wolfcrypt/pkcs7.h>
  331. #endif
  332. #ifdef HAVE_FIPS
  333. #include <wolfssl/wolfcrypt/fips_test.h>
  334. #endif
  335. #ifdef HAVE_SELFTEST
  336. #include <wolfssl/wolfcrypt/selftest.h>
  337. #endif
  338. #ifdef WOLFSSL_ASYNC_CRYPT
  339. #include <wolfssl/wolfcrypt/async.h>
  340. #endif
  341. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  342. #include <wolfssl/wolfcrypt/logging.h>
  343. #endif
  344. #ifdef WOLFSSL_CAAM
  345. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  346. #endif
  347. #ifdef WOLF_CRYPTO_CB
  348. #include <wolfssl/wolfcrypt/cryptocb.h>
  349. #ifdef HAVE_INTEL_QA_SYNC
  350. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  351. #endif
  352. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  353. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  354. #endif
  355. #ifdef HAVE_RENESAS_SYNC
  356. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  357. #endif
  358. #endif
  359. #ifdef _MSC_VER
  360. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  361. #pragma warning(disable: 4996)
  362. #endif
  363. #ifdef OPENSSL_EXTRA
  364. #ifndef WOLFCRYPT_ONLY
  365. #include <wolfssl/openssl/evp.h>
  366. #include <wolfssl/openssl/hmac.h>
  367. #endif
  368. #include <wolfssl/openssl/rand.h>
  369. #include <wolfssl/openssl/aes.h>
  370. #include <wolfssl/openssl/des.h>
  371. #endif
  372. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  373. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  374. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  375. #define USE_CERT_BUFFERS_2048
  376. #endif
  377. #if !defined(USE_CERT_BUFFERS_256)
  378. #define USE_CERT_BUFFERS_256
  379. #endif
  380. #endif
  381. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  382. #define ENABLE_ECC384_CERT_GEN_TEST
  383. #endif
  384. #include <wolfssl/certs_test.h>
  385. #ifdef DEVKITPRO
  386. #include <wiiuse/wpad.h>
  387. #endif
  388. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  389. /* FIPS build has replaced ecc.h. */
  390. #define wc_ecc_key_get_priv(key) (&((key)->k))
  391. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  392. #endif
  393. #ifdef WOLFSSL_STATIC_MEMORY
  394. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  395. #else
  396. #define HEAP_HINT NULL
  397. #endif /* WOLFSSL_STATIC_MEMORY */
  398. /* these cases do not have intermediate hashing support */
  399. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  400. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  401. #define NO_INTM_HASH_TEST
  402. #endif
  403. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  404. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  405. defined(WOLFSSL_SECO_CAAM)
  406. #define HASH_SIZE_LIMIT
  407. #endif
  408. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  409. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  410. static void initDefaultName(void);
  411. #endif
  412. /* for async devices */
  413. #ifdef WOLFSSL_CAAM_DEVID
  414. static int devId = WOLFSSL_CAAM_DEVID;
  415. #else
  416. #ifdef WC_USE_DEVID
  417. static int devId = WC_USE_DEVID;
  418. #else
  419. static int devId = INVALID_DEVID;
  420. #endif
  421. #endif
  422. #ifdef HAVE_WNR
  423. const char* wnrConfigFile = "wnr-example.conf";
  424. #endif
  425. #define TEST_STRING "Everyone gets Friday off."
  426. #define TEST_STRING_SZ 25
  427. typedef struct testVector {
  428. const char* input;
  429. const char* output;
  430. size_t inLen;
  431. size_t outLen;
  432. } testVector;
  433. #ifndef WOLFSSL_TEST_SUBROUTINE
  434. #define WOLFSSL_TEST_SUBROUTINE
  435. #endif
  436. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  437. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  438. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  439. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  440. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  441. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  442. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  443. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  444. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  445. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  446. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  449. #if !defined(WOLFSSL_NOSHA512_224) && \
  450. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  452. #endif
  453. #if !defined(WOLFSSL_NOSHA512_256) && \
  454. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  455. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  456. #endif
  457. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  458. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  459. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  461. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  462. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  464. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  465. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  470. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  471. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  472. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  473. defined(WOLFSSL_AFALG_XILINX_RSA)
  474. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  475. static wc_test_ret_t hkdf_test(void);
  476. #else
  477. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  478. #endif
  479. #endif
  480. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  481. #ifdef WOLFSSL_TLS13
  482. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  483. #endif
  484. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  485. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  486. #ifdef WC_SRTP_KDF
  487. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void);
  488. #endif
  489. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  490. #ifdef WC_RC2
  491. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  492. #endif
  493. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  494. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  495. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  496. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  497. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  498. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  499. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  500. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  501. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  502. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  503. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  504. #if defined(WOLFSSL_SIPHASH)
  505. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  506. #endif
  507. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  510. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  511. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  512. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  514. #ifdef WOLFSSL_SM4
  515. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  516. #endif
  517. #ifdef WC_RSA_NO_PADDING
  518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  519. #endif
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  521. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  522. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  524. #ifndef WC_NO_RNG
  525. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  526. #endif /* WC_NO_RNG */
  527. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  528. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  529. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  530. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  531. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  535. #endif
  536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  538. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  540. #endif
  541. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  542. #ifdef HAVE_ECC
  543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  544. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  545. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  546. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  547. #endif
  548. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  549. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  550. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  551. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  552. /* skip for ATECC508/608A, cannot import private key buffers */
  553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  554. #endif
  555. #endif
  556. #ifdef HAVE_CURVE25519
  557. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  558. #endif
  559. #ifdef HAVE_ED25519
  560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  561. #endif
  562. #ifdef HAVE_CURVE448
  563. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  564. #endif
  565. #ifdef HAVE_ED448
  566. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  567. #endif
  568. #ifdef WOLFSSL_HAVE_KYBER
  569. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  570. #endif
  571. #if defined(WOLFSSL_HAVE_XMSS)
  572. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  573. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void);
  574. #endif
  575. #if defined(WOLFSSL_XMSS_VERIFY_ONLY) && !defined(WOLFSSL_SMALL_STACK)
  576. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void);
  577. #endif
  578. #endif
  579. #if defined(WOLFSSL_HAVE_LMS)
  580. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  581. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void);
  582. #endif
  583. #if defined(WOLFSSL_LMS_VERIFY_ONLY) && !defined(WOLFSSL_SMALL_STACK)
  584. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void);
  585. #endif
  586. #endif
  587. #ifdef WOLFCRYPT_HAVE_ECCSI
  588. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  589. #endif
  590. #ifdef WOLFCRYPT_HAVE_SAKKE
  591. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  592. #endif
  593. #ifdef HAVE_BLAKE2
  594. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  595. #endif
  596. #ifdef HAVE_BLAKE2S
  597. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  598. #endif
  599. #ifdef HAVE_LIBZ
  600. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  601. #endif
  602. #ifdef HAVE_PKCS7
  603. #ifndef NO_PKCS7_ENCRYPTED_DATA
  604. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  605. #endif
  606. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  607. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  608. #endif
  609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  610. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  611. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  612. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  613. #endif
  614. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  616. word32 keySz);
  617. #endif
  618. #endif
  619. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  620. !defined(NO_FILESYSTEM)
  621. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  622. #endif
  623. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  624. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  625. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  626. #endif
  627. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  628. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  629. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  630. #endif
  631. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  632. #if defined(WOLFSSL_PUBLIC_MP) && \
  633. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  634. defined(USE_FAST_MATH))
  635. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  636. #endif
  637. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  638. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  639. #endif
  640. #if defined(ASN_BER_TO_DER) && \
  641. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  642. defined(OPENSSL_EXTRA_X509_SMALL))
  643. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  644. #endif
  645. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  646. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  647. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  648. #endif
  649. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  651. #else
  652. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  653. #endif
  654. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  655. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  656. #endif
  657. #ifdef WOLFSSL_CAAM_BLOB
  658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  659. #endif
  660. #ifdef HAVE_ARIA
  661. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  662. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  663. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  664. #endif
  665. #ifdef WOLF_CRYPTO_CB
  666. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  667. #endif
  668. #ifdef WOLFSSL_CERT_PIV
  669. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  670. #endif
  671. #ifdef WOLFSSL_AES_SIV
  672. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  673. #endif
  674. #if defined(WOLFSSL_AES_EAX) && \
  675. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  676. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void);
  677. #endif /* WOLFSSL_AES_EAX */
  678. /* General big buffer size for many tests. */
  679. #define FOURK_BUF 4096
  680. #if defined(WOLFSSL_ESPIDF_ERROR_PAUSE)
  681. /* When defined, pause at error condition rather than exit with error. */
  682. #define ERROR_OUT(err, eLabel) \
  683. do { \
  684. ret = (err); \
  685. esp_ShowExtendedSystemInfo(); \
  686. ESP_LOGE("wolfcrypt_test", "ESP Error! ret = %d ", err); \
  687. while (1) { \
  688. vTaskDelay(60000); \
  689. } \
  690. /* Just to appease compiler, don't actually go to eLabel */ \
  691. goto eLabel; \
  692. } while (0)
  693. #else
  694. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  695. #endif
  696. /* Not all unexpected conditions are actually errors .*/
  697. #define WARNING_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  698. static void render_error_message(const char* msg, wc_test_ret_t es)
  699. {
  700. (void)msg;
  701. (void)es;
  702. #ifdef WOLFSSL_LINUXKM
  703. #define err_sys_printf lkm_printf
  704. #else
  705. #define err_sys_printf printf
  706. #endif
  707. switch (WC_TEST_RET_DEC_TAG(es)) {
  708. case WC_TEST_RET_TAG_NC:
  709. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  710. break;
  711. case WC_TEST_RET_TAG_EC:
  712. #ifdef NO_ERROR_STRINGS
  713. err_sys_printf("%s error L=%d code=%d\n", msg,
  714. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  715. #else
  716. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  717. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  718. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  719. #endif
  720. break;
  721. case WC_TEST_RET_TAG_ERRNO:
  722. {
  723. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  724. * flavor that always returns a non-null char pointer that must be used
  725. * directly, and a POSIX flavor that returns an error int, and iff success,
  726. * stores an error string in the supplied buffer. this is all most
  727. * infelicitous...
  728. */
  729. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  730. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  731. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  732. (defined(__USE_XOPEN2K) && \
  733. defined(_POSIX_C_SOURCE) && \
  734. (_POSIX_C_SOURCE >= 200112L)))
  735. char errno_buf[64], *errno_string;
  736. /* precisely mirror the gate used in glibc string.h */
  737. #if defined __USE_XOPEN2K && !defined __USE_GNU
  738. if (strerror_r(WC_TEST_RET_DEC_I(es),
  739. errno_buf, sizeof(errno_buf)) != 0)
  740. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  741. errno_string = errno_buf;
  742. #else
  743. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  744. errno_buf, sizeof(errno_buf));
  745. #endif
  746. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  747. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  748. errno_string);
  749. #else /* can't figure out how to strerror_r(), or don't want error strings */
  750. err_sys_printf("%s error L=%d errno=%d\n", msg,
  751. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  752. #endif
  753. break;
  754. }
  755. case WC_TEST_RET_TAG_I:
  756. err_sys_printf("%s error L=%d i=%d\n", msg,
  757. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  758. break;
  759. }
  760. #undef err_sys_printf
  761. }
  762. static void print_fiducials(void);
  763. #ifdef HAVE_STACK_SIZE
  764. static THREAD_RETURN err_sys(const char* msg, int es)
  765. #else
  766. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  767. #endif
  768. {
  769. render_error_message(msg, es);
  770. print_fiducials();
  771. #ifdef WOLFSSL_LINUXKM
  772. EXIT_TEST(es);
  773. #else
  774. EXIT_TEST(-1);
  775. #endif
  776. }
  777. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  778. /* func_args from test.h, so don't have to pull in other stuff */
  779. typedef struct func_args {
  780. int argc;
  781. char** argv;
  782. wc_test_ret_t return_code;
  783. } func_args;
  784. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  785. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  786. static void myFipsCb(int ok, int err, const char* hash)
  787. {
  788. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  789. printf("message = %s\n", wc_GetErrorString(err));
  790. printf("hash = %s\n", hash);
  791. if (err == IN_CORE_FIPS_E) {
  792. printf("In core integrity hash check failure, copy above hash\n");
  793. printf("into verifyCore[] in fips_test.c and rebuild\n");
  794. }
  795. }
  796. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  797. #ifdef WOLFSSL_STATIC_MEMORY
  798. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  799. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  800. #elif defined(BENCH_EMBEDDED)
  801. static byte gTestMemory[14000];
  802. #elif defined(WOLFSSL_CERT_EXT)
  803. static byte gTestMemory[140000];
  804. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  805. !defined(ALT_ECC_SIZE)
  806. static byte gTestMemory[160000];
  807. #else
  808. static byte gTestMemory[80000];
  809. #endif
  810. #endif
  811. #ifdef WOLFSSL_PB
  812. static int wolfssl_pb_print(const char* msg, ...)
  813. {
  814. int ret;
  815. va_list args;
  816. char tmpBuf[80];
  817. va_start(args, msg);
  818. ret = vsprint(tmpBuf, msg, args);
  819. va_end(args);
  820. fnDumpStringToSystemLog(tmpBuf);
  821. return ret;
  822. }
  823. #endif /* WOLFSSL_PB */
  824. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  825. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  826. /* Enable support for RNG with crypto callback */
  827. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  828. {
  829. int rc = CRYPTOCB_UNAVAILABLE;
  830. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  831. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  832. }
  833. (void)ctx;
  834. (void)thisDevId;
  835. return rc;
  836. }
  837. #endif
  838. /* optional macro to add sleep between tests */
  839. #ifndef TEST_SLEEP
  840. #define TEST_SLEEP() WC_DO_NOTHING
  841. #else
  842. #define TEST_PASS test_pass
  843. #include <stdarg.h> /* for var args */
  844. static WC_INLINE void test_pass(const char* fmt, ...)
  845. {
  846. va_list args;
  847. va_start(args, fmt);
  848. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  849. va_end(args);
  850. PRINT_HEAP_CHECKPOINT();
  851. TEST_SLEEP();
  852. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  853. }
  854. #endif
  855. /* set test pass output to printf if not overridden */
  856. #ifndef TEST_PASS
  857. /* redirect to printf */
  858. #define TEST_PASS(...) { \
  859. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  860. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  861. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  862. } \
  863. PRINT_HEAP_CHECKPOINT(); \
  864. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  865. }
  866. #endif
  867. #ifdef TEST_ALWAYS_RUN_TO_END
  868. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  869. #elif !defined(TEST_FAIL)
  870. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  871. #endif
  872. #ifdef HAVE_STACK_SIZE
  873. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  874. #else
  875. wc_test_ret_t wolfcrypt_test(void* args)
  876. #endif
  877. {
  878. wc_test_ret_t ret;
  879. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  880. long heap_baselineAllocs, heap_baselineBytes;
  881. #endif
  882. #ifdef TEST_ALWAYS_RUN_TO_END
  883. int last_failed_test_ret = 0;
  884. #endif
  885. STACK_SIZE_INIT();
  886. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  887. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  888. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  889. (void)wolfCrypt_heap_peakBytes_checkpoint();
  890. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  891. #endif
  892. printf("------------------------------------------------------------------------------\n");
  893. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  894. #ifdef WOLF_CRYPTO_CB
  895. if (devId != INVALID_DEVID)
  896. printf(" CryptoCB with DevID:%X\n", devId);
  897. #endif
  898. printf("------------------------------------------------------------------------------\n");
  899. if (args) {
  900. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  901. int ch;
  902. #endif
  903. ((func_args*)args)->return_code = -1; /* error state */
  904. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  905. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  906. switch(ch) {
  907. case 's':
  908. #ifdef HAVE_STACK_SIZE_VERBOSE
  909. max_relative_stack = (ssize_t)atoi(myoptarg);
  910. break;
  911. #else
  912. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  913. #endif
  914. case 'm':
  915. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  916. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  917. break;
  918. #else
  919. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  920. #endif
  921. case 'a':
  922. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  923. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  924. break;
  925. #else
  926. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  927. #endif
  928. case 'h':
  929. return err_sys("\
  930. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  931. [-a max_relative_heap_allocs] [-h]\n", 0);
  932. default:
  933. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  934. }
  935. }
  936. #endif
  937. }
  938. #ifdef WOLFSSL_STATIC_MEMORY
  939. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  940. WOLFMEM_GENERAL, 1) != 0) {
  941. printf("unable to load static memory.\n");
  942. return(EXIT_FAILURE);
  943. }
  944. #endif
  945. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  946. wolfSSL_Debugging_ON();
  947. #endif
  948. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  949. wc_SetLoggingHeap(HEAP_HINT);
  950. #endif
  951. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  952. wolfCrypt_SetCb_fips(myFipsCb);
  953. #endif
  954. #if !defined(NO_BIG_INT)
  955. if (CheckCtcSettings() != 1) {
  956. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  957. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  958. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  959. }
  960. #if defined(USE_FAST_MATH) && \
  961. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  962. if (CheckFastMathSettings() != 1)
  963. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  964. WC_TEST_RET_ENC_NC);
  965. #endif /* USE_FAST_MATH */
  966. #endif /* !NO_BIG_INT */
  967. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  968. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  969. initDefaultName();
  970. #endif
  971. #ifdef WOLFSSL_ASYNC_CRYPT
  972. ret = wolfAsync_DevOpen(&devId);
  973. if (ret < 0) {
  974. printf("Async device open failed\nRunning without async\n");
  975. }
  976. #else
  977. (void)devId;
  978. #endif /* WOLFSSL_ASYNC_CRYPT */
  979. #ifdef WOLF_CRYPTO_CB
  980. #ifdef HAVE_INTEL_QA_SYNC
  981. devId = wc_CryptoCb_InitIntelQa();
  982. if (INVALID_DEVID == devId) {
  983. printf("Couldn't init the Intel QA\n");
  984. }
  985. #endif
  986. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  987. devId = wc_CryptoCb_InitOcteon();
  988. if (INVALID_DEVID == devId) {
  989. printf("Couldn't init the Cavium Octeon\n");
  990. }
  991. #endif
  992. #ifdef HAVE_RENESAS_SYNC
  993. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  994. if (devId == INVALID_DEVID) {
  995. printf("Couldn't get the Renesas device ID\n");
  996. }
  997. #endif
  998. #endif
  999. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  1000. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  1001. if (devId == INVALID_DEVID) {
  1002. /* for testing RNG with crypto callback register function */
  1003. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  1004. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  1005. }
  1006. #endif
  1007. #ifdef HAVE_SELFTEST
  1008. if ( (ret = wolfCrypt_SelfTest()) != 0)
  1009. TEST_FAIL("CAVP selftest failed!\n", ret);
  1010. else
  1011. TEST_PASS("CAVP selftest passed!\n");
  1012. #endif
  1013. if ( (ret = error_test()) != 0)
  1014. TEST_FAIL("error test failed!\n", ret);
  1015. else
  1016. TEST_PASS("error test passed!\n");
  1017. if ( (ret = memory_test()) != 0)
  1018. TEST_FAIL("MEMORY test failed!\n", ret);
  1019. else
  1020. TEST_PASS("MEMORY test passed!\n");
  1021. #ifndef NO_CODING
  1022. if ( (ret = base64_test()) != 0)
  1023. TEST_FAIL("base64 test failed!\n", ret);
  1024. else
  1025. TEST_PASS("base64 test passed!\n");
  1026. #ifdef WOLFSSL_BASE16
  1027. if ( (ret = base16_test()) != 0)
  1028. TEST_FAIL("base16 test failed!\n", ret);
  1029. else
  1030. TEST_PASS("base16 test passed!\n");
  1031. #endif
  1032. #endif /* !NO_CODING */
  1033. #ifndef NO_ASN
  1034. if ( (ret = asn_test()) != 0)
  1035. TEST_FAIL("asn test failed!\n", ret);
  1036. else
  1037. TEST_PASS("asn test passed!\n");
  1038. #endif
  1039. #ifndef WC_NO_RNG
  1040. if ( (ret = random_test()) != 0)
  1041. TEST_FAIL("RANDOM test failed!\n", ret);
  1042. else
  1043. TEST_PASS("RANDOM test passed!\n");
  1044. #endif /* WC_NO_RNG */
  1045. #ifndef NO_MD5
  1046. if ( (ret = md5_test()) != 0)
  1047. TEST_FAIL("MD5 test failed!\n", ret);
  1048. else
  1049. TEST_PASS("MD5 test passed!\n");
  1050. #endif
  1051. #ifdef WOLFSSL_MD2
  1052. if ( (ret = md2_test()) != 0)
  1053. TEST_FAIL("MD2 test failed!\n", ret);
  1054. else
  1055. TEST_PASS("MD2 test passed!\n");
  1056. #endif
  1057. #ifndef NO_MD4
  1058. if ( (ret = md4_test()) != 0)
  1059. TEST_FAIL("MD4 test failed!\n", ret);
  1060. else
  1061. TEST_PASS("MD4 test passed!\n");
  1062. #endif
  1063. #ifndef NO_SHA
  1064. if ( (ret = sha_test()) != 0)
  1065. TEST_FAIL("SHA test failed!\n", ret);
  1066. else
  1067. TEST_PASS("SHA test passed!\n");
  1068. #endif
  1069. #ifdef WOLFSSL_SHA224
  1070. if ( (ret = sha224_test()) != 0)
  1071. TEST_FAIL("SHA-224 test failed!\n", ret);
  1072. else
  1073. TEST_PASS("SHA-224 test passed!\n");
  1074. #endif
  1075. #ifndef NO_SHA256
  1076. if ( (ret = sha256_test()) != 0)
  1077. TEST_FAIL("SHA-256 test failed!\n", ret);
  1078. else
  1079. TEST_PASS("SHA-256 test passed!\n");
  1080. #endif
  1081. #ifdef WOLFSSL_SHA384
  1082. if ( (ret = sha384_test()) != 0)
  1083. TEST_FAIL("SHA-384 test failed!\n", ret);
  1084. else
  1085. TEST_PASS("SHA-384 test passed!\n");
  1086. #endif
  1087. #ifdef WOLFSSL_SHA512
  1088. if ((ret = sha512_test()) != 0) {
  1089. TEST_FAIL("SHA-512 test failed!\n", ret);
  1090. }
  1091. else {
  1092. TEST_PASS("SHA-512 test passed!\n");
  1093. }
  1094. #if !defined(WOLFSSL_NOSHA512_224) && \
  1095. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1096. if ((ret = sha512_224_test()) != 0) {
  1097. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1098. }
  1099. else
  1100. TEST_PASS("SHA-512/224 test passed!\n");
  1101. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1102. #if !defined(WOLFSSL_NOSHA512_256) && \
  1103. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1104. if ((ret = sha512_256_test()) != 0) {
  1105. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1106. }
  1107. else
  1108. TEST_PASS("SHA-512/256 test passed!\n");
  1109. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1110. #endif /* WOLFSSL_SHA512 */
  1111. #ifdef WOLFSSL_SHA3
  1112. if ( (ret = sha3_test()) != 0)
  1113. TEST_FAIL("SHA-3 test failed!\n", ret);
  1114. else
  1115. TEST_PASS("SHA-3 test passed!\n");
  1116. #endif
  1117. #ifdef WOLFSSL_SHAKE128
  1118. if ( (ret = shake128_test()) != 0)
  1119. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1120. else
  1121. TEST_PASS("SHAKE128 test passed!\n");
  1122. #endif
  1123. #ifdef WOLFSSL_SHAKE256
  1124. if ( (ret = shake256_test()) != 0)
  1125. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1126. else
  1127. TEST_PASS("SHAKE256 test passed!\n");
  1128. #endif
  1129. #ifdef WOLFSSL_SM3
  1130. if ( (ret = sm3_test()) != 0)
  1131. return err_sys("SM-3 test failed!\n", ret);
  1132. else
  1133. TEST_PASS("SM-3 test passed!\n");
  1134. #endif
  1135. #ifndef NO_HASH_WRAPPER
  1136. if ( (ret = hash_test()) != 0)
  1137. TEST_FAIL("Hash test failed!\n", ret);
  1138. else
  1139. TEST_PASS("Hash test passed!\n");
  1140. #endif
  1141. #ifdef WOLFSSL_RIPEMD
  1142. if ( (ret = ripemd_test()) != 0)
  1143. TEST_FAIL("RIPEMD test failed!\n", ret);
  1144. else
  1145. TEST_PASS("RIPEMD test passed!\n");
  1146. #endif
  1147. #ifdef HAVE_BLAKE2
  1148. if ( (ret = blake2b_test()) != 0)
  1149. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1150. else
  1151. TEST_PASS("BLAKE2b test passed!\n");
  1152. #endif
  1153. #ifdef HAVE_BLAKE2S
  1154. if ( (ret = blake2s_test()) != 0)
  1155. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1156. else
  1157. TEST_PASS("BLAKE2s test passed!\n");
  1158. #endif
  1159. #ifndef NO_HMAC
  1160. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1161. && (HAVE_FIPS_VERSION >= 5))
  1162. if ( (ret = hmac_md5_test()) != 0)
  1163. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1164. else
  1165. TEST_PASS("HMAC-MD5 test passed!\n");
  1166. #endif
  1167. #ifndef NO_SHA
  1168. if ( (ret = hmac_sha_test()) != 0)
  1169. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1170. else
  1171. TEST_PASS("HMAC-SHA test passed!\n");
  1172. #endif
  1173. #ifdef WOLFSSL_SHA224
  1174. if ( (ret = hmac_sha224_test()) != 0)
  1175. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1176. else
  1177. TEST_PASS("HMAC-SHA224 test passed!\n");
  1178. #endif
  1179. #ifndef NO_SHA256
  1180. if ( (ret = hmac_sha256_test()) != 0)
  1181. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1182. else
  1183. TEST_PASS("HMAC-SHA256 test passed!\n");
  1184. #endif
  1185. #ifdef WOLFSSL_SHA384
  1186. if ( (ret = hmac_sha384_test()) != 0)
  1187. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1188. else
  1189. TEST_PASS("HMAC-SHA384 test passed!\n");
  1190. #endif
  1191. #ifdef WOLFSSL_SHA512
  1192. if ( (ret = hmac_sha512_test()) != 0)
  1193. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1194. else
  1195. TEST_PASS("HMAC-SHA512 test passed!\n");
  1196. #endif
  1197. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1198. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1199. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1200. if ( (ret = hmac_sha3_test()) != 0)
  1201. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1202. else
  1203. TEST_PASS("HMAC-SHA3 test passed!\n");
  1204. #endif
  1205. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1206. PRIVATE_KEY_UNLOCK();
  1207. if ( (ret = hkdf_test()) != 0)
  1208. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1209. else
  1210. TEST_PASS("HMAC-KDF test passed!\n");
  1211. PRIVATE_KEY_LOCK();
  1212. #endif
  1213. #endif /* !NO_HMAC */
  1214. #ifdef WOLFSSL_WOLFSSH
  1215. PRIVATE_KEY_UNLOCK();
  1216. if ( (ret = sshkdf_test()) != 0)
  1217. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1218. else
  1219. TEST_PASS("SSH-KDF test passed!\n");
  1220. PRIVATE_KEY_LOCK();
  1221. #endif /* WOLFSSL_WOLFSSH */
  1222. #ifdef WOLFSSL_TLS13
  1223. PRIVATE_KEY_UNLOCK();
  1224. if ( (ret = tls13_kdf_test()) != 0)
  1225. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1226. else
  1227. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1228. PRIVATE_KEY_LOCK();
  1229. #endif /* WOLFSSL_TLS13 */
  1230. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1231. if ( (ret = x963kdf_test()) != 0)
  1232. TEST_FAIL("X963-KDF test failed!\n", ret);
  1233. else
  1234. TEST_PASS("X963-KDF test passed!\n");
  1235. #endif
  1236. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1237. if ( (ret = hpke_test()) != 0)
  1238. TEST_FAIL("HPKE test failed!\n", ret);
  1239. else
  1240. TEST_PASS("HPKE test passed!\n");
  1241. #endif
  1242. #if defined(WC_SRTP_KDF)
  1243. if ( (ret = srtpkdf_test()) != 0)
  1244. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1245. else
  1246. TEST_PASS("SRTP KDF test passed!\n");
  1247. #endif
  1248. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1249. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1250. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1251. if ( (ret = gmac_test()) != 0)
  1252. TEST_FAIL("GMAC test failed!\n", ret);
  1253. else
  1254. TEST_PASS("GMAC test passed!\n");
  1255. #endif
  1256. #ifdef WC_RC2
  1257. if ( (ret = rc2_test()) != 0)
  1258. TEST_FAIL("RC2 test failed!\n", ret);
  1259. else
  1260. TEST_PASS("RC2 test passed!\n");
  1261. #endif
  1262. #ifndef NO_RC4
  1263. if ( (ret = arc4_test()) != 0)
  1264. TEST_FAIL("ARC4 test failed!\n", ret);
  1265. else
  1266. TEST_PASS("ARC4 test passed!\n");
  1267. #endif
  1268. #ifdef HAVE_CHACHA
  1269. if ( (ret = chacha_test()) != 0)
  1270. TEST_FAIL("Chacha test failed!\n", ret);
  1271. else
  1272. TEST_PASS("Chacha test passed!\n");
  1273. #endif
  1274. #ifdef HAVE_XCHACHA
  1275. if ( (ret = XChaCha_test()) != 0)
  1276. TEST_FAIL("XChacha test failed!\n", ret);
  1277. else
  1278. TEST_PASS("XChacha test passed!\n");
  1279. #endif
  1280. #ifdef HAVE_POLY1305
  1281. if ( (ret = poly1305_test()) != 0)
  1282. TEST_FAIL("POLY1305 test failed!\n", ret);
  1283. else
  1284. TEST_PASS("POLY1305 test passed!\n");
  1285. #endif
  1286. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1287. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1288. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1289. else
  1290. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1291. #endif
  1292. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1293. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1294. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1295. else
  1296. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1297. #endif
  1298. #ifndef NO_DES3
  1299. if ( (ret = des_test()) != 0)
  1300. TEST_FAIL("DES test failed!\n", ret);
  1301. else
  1302. TEST_PASS("DES test passed!\n");
  1303. #endif
  1304. #ifndef NO_DES3
  1305. if ( (ret = des3_test()) != 0)
  1306. TEST_FAIL("DES3 test failed!\n", ret);
  1307. else
  1308. TEST_PASS("DES3 test passed!\n");
  1309. #endif
  1310. #ifndef NO_AES
  1311. if ( (ret = aes_test()) != 0)
  1312. TEST_FAIL("AES test failed!\n", ret);
  1313. else
  1314. TEST_PASS("AES test passed!\n");
  1315. #if defined(WOLFSSL_AES_192) && \
  1316. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1317. if ( (ret = aes192_test()) != 0)
  1318. TEST_FAIL("AES192 test failed!\n", ret);
  1319. else
  1320. TEST_PASS("AES192 test passed!\n");
  1321. #endif
  1322. #if defined(WOLFSSL_AES_256)
  1323. if ( (ret = aes256_test()) != 0)
  1324. TEST_FAIL("AES256 test failed!\n", ret);
  1325. else
  1326. TEST_PASS("AES256 test passed!\n");
  1327. #endif
  1328. #ifdef WOLFSSL_AES_OFB
  1329. if ( (ret = aesofb_test()) != 0)
  1330. TEST_FAIL("AES-OFB test failed!\n", ret);
  1331. else
  1332. TEST_PASS("AESOFB test passed!\n");
  1333. #endif
  1334. #ifdef HAVE_AESGCM
  1335. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1336. if ( (ret = aesgcm_test()) != 0)
  1337. TEST_FAIL("AES-GCM test failed!\n", ret);
  1338. #endif
  1339. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1340. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1341. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1342. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1343. if ((ret = aesgcm_default_test()) != 0) {
  1344. TEST_FAIL("AES-GCM test failed!\n", ret);
  1345. }
  1346. #endif
  1347. if (ret == 0) {
  1348. TEST_PASS("AES-GCM test passed!\n");
  1349. }
  1350. #endif
  1351. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1352. if ( (ret = aesccm_test()) != 0)
  1353. TEST_FAIL("AES-CCM test failed!\n", ret);
  1354. else
  1355. TEST_PASS("AES-CCM test passed!\n");
  1356. #endif
  1357. #ifdef HAVE_AES_KEYWRAP
  1358. if ( (ret = aeskeywrap_test()) != 0)
  1359. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1360. else
  1361. TEST_PASS("AES Key Wrap test passed!\n");
  1362. #endif
  1363. #ifdef WOLFSSL_AES_SIV
  1364. if ( (ret = aes_siv_test()) != 0)
  1365. TEST_FAIL("AES-SIV test failed!\n", ret);
  1366. else
  1367. TEST_PASS("AES-SIV test passed!\n");
  1368. #endif
  1369. #endif
  1370. #if defined(WOLFSSL_AES_EAX) && \
  1371. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1372. if ( (ret = aes_eax_test()) != 0)
  1373. TEST_FAIL("AES-EAX test failed!\n", ret);
  1374. else
  1375. TEST_PASS("AES-EAX test passed!\n");
  1376. #endif /* WOLFSSL_AES_EAX */
  1377. #ifdef HAVE_ARIA
  1378. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1379. TEST_FAIL("ARIA128 test failed!\n", ret);
  1380. else
  1381. TEST_PASS("ARIA128 test passed!\n");
  1382. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1383. TEST_FAIL("ARIA192 test failed!\n", ret);
  1384. else
  1385. TEST_PASS("ARIA192 test passed!\n");
  1386. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1387. TEST_FAIL("ARIA256 test failed!\n", ret);
  1388. else
  1389. TEST_PASS("ARIA256 test passed!\n");
  1390. #endif
  1391. #ifdef HAVE_CAMELLIA
  1392. if ( (ret = camellia_test()) != 0)
  1393. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1394. else
  1395. TEST_PASS("CAMELLIA test passed!\n");
  1396. #endif
  1397. #ifdef WOLFSSL_SM4
  1398. if ( (ret = sm4_test()) != 0)
  1399. return err_sys("SM-4 test failed!\n", ret);
  1400. else
  1401. TEST_PASS("SM-4 test passed!\n");
  1402. #endif
  1403. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1404. #ifdef WC_RSA_NO_PADDING
  1405. if ( (ret = rsa_no_pad_test()) != 0)
  1406. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1407. else
  1408. TEST_PASS("RSA NOPAD test passed!\n");
  1409. #endif
  1410. if ( (ret = rsa_test()) != 0)
  1411. TEST_FAIL("RSA test failed!\n", ret);
  1412. else
  1413. TEST_PASS("RSA test passed!\n");
  1414. #endif
  1415. #ifndef NO_DH
  1416. PRIVATE_KEY_UNLOCK();
  1417. if ( (ret = dh_test()) != 0)
  1418. TEST_FAIL("DH test failed!\n", ret);
  1419. else
  1420. TEST_PASS("DH test passed!\n");
  1421. PRIVATE_KEY_LOCK();
  1422. #endif
  1423. #ifndef NO_DSA
  1424. if ( (ret = dsa_test()) != 0)
  1425. TEST_FAIL("DSA test failed!\n", ret);
  1426. else
  1427. TEST_PASS("DSA test passed!\n");
  1428. #endif
  1429. #ifdef WOLFCRYPT_HAVE_SRP
  1430. if ( (ret = srp_test()) != 0)
  1431. TEST_FAIL("SRP test failed!\n", ret);
  1432. else
  1433. TEST_PASS("SRP test passed!\n");
  1434. #endif
  1435. #ifndef NO_PWDBASED
  1436. if ( (ret = pwdbased_test()) != 0)
  1437. TEST_FAIL("PWDBASED test failed!\n", ret);
  1438. else
  1439. TEST_PASS("PWDBASED test passed!\n");
  1440. #endif
  1441. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1442. if ( (ret = openssl_test()) != 0)
  1443. TEST_FAIL("OPENSSL test failed!\n", ret);
  1444. else
  1445. TEST_PASS("OPENSSL test passed!\n");
  1446. if ( (ret = openSSL_evpMD_test()) != 0)
  1447. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1448. else
  1449. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1450. if ( (ret = openssl_pkey0_test()) != 0)
  1451. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1452. else
  1453. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1454. if ( (ret = openssl_pkey1_test()) != 0)
  1455. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1456. else
  1457. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1458. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1459. if ( (ret = openssl_evpSig_test()) != 0)
  1460. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1461. else
  1462. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1463. #endif
  1464. #endif
  1465. #if defined(HAVE_ECC)
  1466. PRIVATE_KEY_UNLOCK();
  1467. if ( (ret = ecc_test()) != 0)
  1468. TEST_FAIL("ECC test failed!\n", ret);
  1469. else
  1470. TEST_PASS("ECC test passed!\n");
  1471. PRIVATE_KEY_LOCK();
  1472. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1473. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1474. if ( (ret = ecc_encrypt_test()) != 0)
  1475. TEST_FAIL("ECC Enc test failed!\n", ret);
  1476. else
  1477. TEST_PASS("ECC Enc test passed!\n");
  1478. #endif
  1479. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1480. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1481. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1482. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1483. /* skip for ATECC508/608A, cannot import private key buffers */
  1484. if ( (ret = ecc_test_buffers()) != 0)
  1485. TEST_FAIL("ECC buffer test failed!\n", ret);
  1486. else
  1487. TEST_PASS("ECC buffer test passed!\n");
  1488. #endif
  1489. #endif
  1490. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1491. !defined(NO_FILESYSTEM)
  1492. if ( (ret = cert_test()) != 0)
  1493. TEST_FAIL("CERT test failed!\n", ret);
  1494. else
  1495. TEST_PASS("CERT test passed!\n");
  1496. #endif
  1497. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1498. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1499. if ( (ret = certext_test()) != 0)
  1500. TEST_FAIL("CERT EXT test failed!\n", ret);
  1501. else
  1502. TEST_PASS("CERT EXT test passed!\n");
  1503. #endif
  1504. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1505. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1506. if ( (ret = decodedCertCache_test()) != 0)
  1507. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1508. else
  1509. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1510. #endif
  1511. #ifdef HAVE_CURVE25519
  1512. if ( (ret = curve25519_test()) != 0)
  1513. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1514. else
  1515. TEST_PASS("CURVE25519 test passed!\n");
  1516. #endif
  1517. #ifdef HAVE_ED25519
  1518. if ( (ret = ed25519_test()) != 0)
  1519. TEST_FAIL("ED25519 test failed!\n", ret);
  1520. else
  1521. TEST_PASS("ED25519 test passed!\n");
  1522. #endif
  1523. #ifdef HAVE_CURVE448
  1524. if ( (ret = curve448_test()) != 0)
  1525. TEST_FAIL("CURVE448 test failed!\n", ret);
  1526. else
  1527. TEST_PASS("CURVE448 test passed!\n");
  1528. #endif
  1529. #ifdef HAVE_ED448
  1530. if ( (ret = ed448_test()) != 0)
  1531. TEST_FAIL("ED448 test failed!\n", ret);
  1532. else
  1533. TEST_PASS("ED448 test passed!\n");
  1534. #endif
  1535. #ifdef WOLFSSL_HAVE_KYBER
  1536. if ( (ret = kyber_test()) != 0)
  1537. TEST_FAIL("KYBER test failed!\n", ret);
  1538. else
  1539. TEST_PASS("KYBER test passed!\n");
  1540. #endif
  1541. #if defined(WOLFSSL_HAVE_XMSS)
  1542. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1543. if ( (ret = xmss_test()) != 0)
  1544. TEST_FAIL("XMSS test failed!\n", ret);
  1545. else
  1546. TEST_PASS("XMSS test passed!\n");
  1547. #endif
  1548. #if defined(WOLFSSL_XMSS_VERIFY_ONLY) && !defined(WOLFSSL_SMALL_STACK)
  1549. if ( (ret = xmss_test_verify_only()) != 0)
  1550. TEST_FAIL("XMSS test failed!\n", ret);
  1551. else
  1552. TEST_PASS("XMSS test passed!\n");
  1553. #endif
  1554. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  1555. #if defined(WOLFSSL_HAVE_LMS)
  1556. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  1557. if ( (ret = lms_test()) != 0)
  1558. TEST_FAIL("LMS test failed!\n", ret);
  1559. else
  1560. TEST_PASS("LMS test passed!\n");
  1561. #endif
  1562. #if defined(WOLFSSL_LMS_VERIFY_ONLY) && !defined(WOLFSSL_SMALL_STACK)
  1563. if ( (ret = lms_test_verify_only()) != 0)
  1564. TEST_FAIL("LMS test failed!\n", ret);
  1565. else
  1566. TEST_PASS("LMS test passed!\n");
  1567. #endif
  1568. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  1569. #ifdef WOLFCRYPT_HAVE_ECCSI
  1570. if ( (ret = eccsi_test()) != 0)
  1571. TEST_FAIL("ECCSI test failed!\n", ret);
  1572. else
  1573. TEST_PASS("ECCSI test passed!\n");
  1574. #endif
  1575. #ifdef WOLFCRYPT_HAVE_SAKKE
  1576. if ( (ret = sakke_test()) != 0)
  1577. TEST_FAIL("SAKKE test failed!\n", ret);
  1578. else
  1579. TEST_PASS("SAKKE test passed!\n");
  1580. #endif
  1581. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1582. if ( (ret = cmac_test()) != 0)
  1583. TEST_FAIL("CMAC test failed!\n", ret);
  1584. else
  1585. TEST_PASS("CMAC test passed!\n");
  1586. #endif
  1587. #if defined(WOLFSSL_SIPHASH)
  1588. if ( (ret = siphash_test()) != 0)
  1589. TEST_FAIL("SipHash test failed!\n", ret);
  1590. else
  1591. TEST_PASS("SipHash test passed!\n");
  1592. #endif
  1593. #ifdef HAVE_LIBZ
  1594. if ( (ret = compress_test()) != 0)
  1595. TEST_FAIL("COMPRESS test failed!\n", ret);
  1596. else
  1597. TEST_PASS("COMPRESS test passed!\n");
  1598. #endif
  1599. #ifdef HAVE_PKCS7
  1600. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1601. if ( (ret = pkcs7encrypted_test()) != 0)
  1602. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  1603. else
  1604. TEST_PASS("PKCS7encrypted test passed!\n");
  1605. #endif
  1606. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1607. if ( (ret = pkcs7compressed_test()) != 0)
  1608. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  1609. else
  1610. TEST_PASS("PKCS7compressed test passed!\n");
  1611. #endif
  1612. if ( (ret = pkcs7signed_test()) != 0)
  1613. TEST_FAIL("PKCS7signed test failed!\n", ret);
  1614. else
  1615. TEST_PASS("PKCS7signed test passed!\n");
  1616. if ( (ret = pkcs7enveloped_test()) != 0)
  1617. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  1618. else
  1619. TEST_PASS("PKCS7enveloped test passed!\n");
  1620. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1621. if ( (ret = pkcs7authenveloped_test()) != 0)
  1622. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  1623. else
  1624. TEST_PASS("PKCS7authenveloped test passed!\n");
  1625. #endif
  1626. #endif
  1627. #if defined(WOLFSSL_PUBLIC_MP) && \
  1628. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  1629. defined(USE_FAST_MATH))
  1630. if ( (ret = mp_test()) != 0)
  1631. TEST_FAIL("mp test failed!\n", ret);
  1632. else
  1633. TEST_PASS("mp test passed!\n");
  1634. #endif
  1635. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1636. if ( (ret = prime_test()) != 0)
  1637. TEST_FAIL("prime test failed!\n", ret);
  1638. else
  1639. TEST_PASS("prime test passed!\n");
  1640. #endif
  1641. #if defined(ASN_BER_TO_DER) && \
  1642. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1643. defined(OPENSSL_EXTRA_X509_SMALL))
  1644. if ( (ret = berder_test()) != 0)
  1645. TEST_FAIL("ber-der test failed!\n", ret);
  1646. else
  1647. TEST_PASS("ber-der test passed!\n");
  1648. #endif
  1649. if ( (ret = logging_test()) != 0)
  1650. TEST_FAIL("logging test failed!\n", ret);
  1651. else
  1652. TEST_PASS("logging test passed!\n");
  1653. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1654. if ( (ret = time_test()) != 0)
  1655. TEST_FAIL("time test failed!\n", ret);
  1656. else
  1657. TEST_PASS("time test passed!\n");
  1658. #endif
  1659. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  1660. if ((ret = wolfcrypt_mutex_test()) != 0)
  1661. #else
  1662. if ((ret = mutex_test()) != 0)
  1663. #endif
  1664. TEST_FAIL("mutex test failed!\n", ret);
  1665. else
  1666. TEST_PASS("mutex test passed!\n");
  1667. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1668. if ( (ret = memcb_test()) != 0)
  1669. TEST_FAIL("memcb test failed!\n", ret);
  1670. else
  1671. TEST_PASS("memcb test passed!\n");
  1672. #endif
  1673. #ifdef WOLFSSL_CAAM_BLOB
  1674. if ( (ret = blob_test()) != 0)
  1675. TEST_FAIL("blob test failed!\n", ret);
  1676. else
  1677. TEST_PASS("blob test passed!\n");
  1678. #endif
  1679. #if defined(WOLF_CRYPTO_CB) && \
  1680. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1681. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  1682. if ( (ret = cryptocb_test()) != 0)
  1683. TEST_FAIL("crypto callback test failed!\n", ret);
  1684. else
  1685. TEST_PASS("crypto callback test passed!\n");
  1686. #endif
  1687. #ifdef WOLFSSL_CERT_PIV
  1688. if ( (ret = certpiv_test()) != 0)
  1689. TEST_FAIL("cert piv test failed!\n", ret);
  1690. else
  1691. TEST_PASS("cert piv test passed!\n");
  1692. #endif
  1693. #ifdef WOLF_CRYPTO_CB
  1694. #ifdef HAVE_INTEL_QA_SYNC
  1695. wc_CryptoCb_CleanupIntelQa(&devId);
  1696. #endif
  1697. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1698. wc_CryptoCb_CleanupOcteon(&devId);
  1699. #endif
  1700. #endif
  1701. #ifdef WOLFSSL_ASYNC_CRYPT
  1702. wolfAsync_DevClose(&devId);
  1703. #endif
  1704. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1705. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1706. wc_ecc_fp_free();
  1707. #endif
  1708. #ifdef TEST_ALWAYS_RUN_TO_END
  1709. if (last_failed_test_ret != 0)
  1710. ret = last_failed_test_ret;
  1711. #endif
  1712. if (args)
  1713. ((func_args*)args)->return_code = ret;
  1714. TEST_PASS("Test complete\n");
  1715. EXIT_TEST(ret);
  1716. }
  1717. #ifndef NO_MAIN_DRIVER
  1718. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1719. int myoptind = 0;
  1720. char* myoptarg = NULL;
  1721. #endif
  1722. /* so overall tests can pull in test function */
  1723. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1724. wc_test_ret_t wolf_test_task(void)
  1725. #else
  1726. #ifndef NO_MAIN_FUNCTION
  1727. int main(int argc, char** argv)
  1728. {
  1729. return (int)wolfcrypt_test_main(argc, argv);
  1730. }
  1731. #endif
  1732. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  1733. #endif
  1734. {
  1735. wc_test_ret_t ret;
  1736. func_args args = { 0, 0, 0 };
  1737. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1738. /* set dummy wallclock time. */
  1739. struct timeval utctime;
  1740. struct timezone tz;
  1741. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1742. utctime.tv_usec = 0;
  1743. tz.tz_minuteswest = 0;
  1744. tz.tz_dsttime = 0;
  1745. settimeofday(&utctime, &tz);
  1746. #endif
  1747. #ifdef WOLFSSL_APACHE_MYNEWT
  1748. #ifdef ARCH_sim
  1749. mcu_sim_parse_args(argc, argv);
  1750. #endif
  1751. sysinit();
  1752. /* set dummy wallclock time. */
  1753. struct os_timeval utctime;
  1754. struct os_timezone tz;
  1755. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1756. utctime.tv_usec = 0;
  1757. tz.tz_minuteswest = 0;
  1758. tz.tz_dsttime = 0;
  1759. os_settimeofday(&utctime, &tz);
  1760. #endif
  1761. #ifdef WOLFSSL_ZEPHYR
  1762. /* set dummy wallclock time. */
  1763. struct timespec utctime;
  1764. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1765. utctime.tv_nsec = 0;
  1766. clock_settime(CLOCK_REALTIME, &utctime);
  1767. #endif
  1768. #ifdef DEVKITPRO
  1769. void *framebuffer;
  1770. GXRModeObj *rmode = NULL;
  1771. VIDEO_Init();
  1772. WPAD_Init();
  1773. rmode = VIDEO_GetPreferredMode(NULL);
  1774. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1775. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1776. #pragma GCC diagnostic pop
  1777. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1778. VIDEO_Configure(rmode);
  1779. VIDEO_SetNextFramebuffer(framebuffer);
  1780. VIDEO_SetBlack(FALSE);
  1781. VIDEO_Flush();
  1782. VIDEO_WaitVSync();
  1783. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1784. #endif
  1785. #ifdef HAVE_WNR
  1786. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  1787. err_sys("Whitewood netRandom global config failed",
  1788. WC_TEST_RET_ENC_EC(ret));
  1789. return WC_TEST_RET_ENC_EC(ret);
  1790. }
  1791. #endif
  1792. #ifndef WOLFSSL_ESPIDF
  1793. args.argc = argc;
  1794. args.argv = argv;
  1795. #endif
  1796. if ((ret = wolfCrypt_Init()) != 0) {
  1797. printf("wolfCrypt_Init failed %d\n", (int)ret);
  1798. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  1799. }
  1800. #ifdef HAVE_WC_INTROSPECTION
  1801. printf("Math: %s\n", wc_GetMathInfo());
  1802. #endif
  1803. #ifdef WC_RNG_SEED_CB
  1804. wc_SetSeed_Cb(wc_GenerateSeed);
  1805. #endif
  1806. #ifdef HAVE_STACK_SIZE
  1807. StackSizeCheck(&args, wolfcrypt_test);
  1808. #else
  1809. wolfcrypt_test(&args);
  1810. #endif
  1811. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1812. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  1813. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  1814. }
  1815. #ifdef HAVE_WNR
  1816. if ((ret = wc_FreeNetRandom()) < 0)
  1817. err_sys("Failed to free netRandom context",
  1818. WC_TEST_RET_ENC_EC(ret));
  1819. #endif /* HAVE_WNR */
  1820. #ifdef DOLPHIN_EMULATOR
  1821. /* Returning from main panics the emulator. Just hang
  1822. * and let the user force quit the emulator window. */
  1823. printf("args.return_code: %d\n", args.return_code);
  1824. printf("Testing complete. You may close the window now\n");
  1825. while (1);
  1826. #endif
  1827. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  1828. return args.return_code;
  1829. } /* wolfcrypt_test_main or wolf_test_task */
  1830. #endif /* NO_MAIN_DRIVER */
  1831. /* helper to save DER, convert to PEM and save PEM */
  1832. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1833. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1834. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1835. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1836. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  1837. #else
  1838. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  1839. #endif
  1840. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  1841. const char* fileDer, const char* filePem, int pemType, int calling_line)
  1842. {
  1843. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1844. wc_test_ret_t ret;
  1845. XFILE derFile;
  1846. derFile = XFOPEN(fileDer, "wb");
  1847. if (!derFile) {
  1848. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  1849. }
  1850. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1851. XFCLOSE(derFile);
  1852. if (ret != derSz) {
  1853. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  1854. }
  1855. #endif
  1856. #ifdef WOLFSSL_DER_TO_PEM
  1857. if (filePem) {
  1858. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1859. XFILE pemFile;
  1860. #endif
  1861. byte* pem;
  1862. int pemSz;
  1863. /* calculate PEM size */
  1864. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1865. if (pemSz < 0) {
  1866. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  1867. }
  1868. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1869. if (pem == NULL) {
  1870. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  1871. }
  1872. /* Convert to PEM */
  1873. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1874. if (pemSz < 0) {
  1875. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1876. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  1877. }
  1878. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1879. pemFile = XFOPEN(filePem, "wb");
  1880. if (!pemFile) {
  1881. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1882. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  1883. }
  1884. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1885. XFCLOSE(pemFile);
  1886. if (ret != pemSz) {
  1887. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1888. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  1889. }
  1890. #endif
  1891. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1892. }
  1893. #endif /* WOLFSSL_DER_TO_PEM */
  1894. /* suppress unused variable warnings */
  1895. (void)der;
  1896. (void)derSz;
  1897. (void)filePem;
  1898. (void)fileDer;
  1899. (void)pemType;
  1900. (void)calling_line;
  1901. return 0;
  1902. }
  1903. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1904. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  1905. {
  1906. const char* errStr;
  1907. char out[WOLFSSL_MAX_ERROR_SZ];
  1908. const char* unknownStr = wc_GetErrorString(0);
  1909. #ifdef NO_ERROR_STRINGS
  1910. /* Ensure a valid error code's string matches an invalid code's.
  1911. * The string is that error strings are not available.
  1912. */
  1913. errStr = wc_GetErrorString(OPEN_RAN_E);
  1914. wc_ErrorString(OPEN_RAN_E, out);
  1915. if (XSTRCMP(errStr, unknownStr) != 0)
  1916. return WC_TEST_RET_ENC_NC;
  1917. if (XSTRCMP(out, unknownStr) != 0)
  1918. return WC_TEST_RET_ENC_NC;
  1919. #else
  1920. int i;
  1921. int j = 0;
  1922. /* Values that are not or no longer error codes. */
  1923. int missing[] = { -123, -124, -128, -129, -159, -163, -164,
  1924. -165, -166, -167, -168, -169, -233, 0 };
  1925. /* Check that all errors have a string and it's the same through the two
  1926. * APIs. Check that the values that are not errors map to the unknown
  1927. * string.
  1928. */
  1929. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1930. errStr = wc_GetErrorString(i);
  1931. wc_ErrorString(i, out);
  1932. if (i != missing[j]) {
  1933. if (XSTRCMP(errStr, unknownStr) == 0)
  1934. return WC_TEST_RET_ENC_NC;
  1935. if (XSTRCMP(out, unknownStr) == 0)
  1936. return WC_TEST_RET_ENC_NC;
  1937. if (XSTRCMP(errStr, out) != 0)
  1938. return WC_TEST_RET_ENC_NC;
  1939. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1940. return WC_TEST_RET_ENC_NC;
  1941. }
  1942. else {
  1943. j++;
  1944. if (XSTRCMP(errStr, unknownStr) != 0)
  1945. return WC_TEST_RET_ENC_NC;
  1946. if (XSTRCMP(out, unknownStr) != 0)
  1947. return WC_TEST_RET_ENC_NC;
  1948. }
  1949. }
  1950. /* Check if the next possible value has been given a string. */
  1951. errStr = wc_GetErrorString(i);
  1952. wc_ErrorString(i, out);
  1953. if (XSTRCMP(errStr, unknownStr) != 0)
  1954. return WC_TEST_RET_ENC_NC;
  1955. if (XSTRCMP(out, unknownStr) != 0)
  1956. return WC_TEST_RET_ENC_NC;
  1957. #endif
  1958. return 0;
  1959. }
  1960. #ifndef NO_CODING
  1961. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  1962. {
  1963. wc_test_ret_t ret;
  1964. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1965. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1966. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1967. byte out[128];
  1968. word32 outLen;
  1969. #ifdef WOLFSSL_BASE64_ENCODE
  1970. byte data[3];
  1971. word32 dataLen;
  1972. byte longData[79] = { 0 };
  1973. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1974. #endif
  1975. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1976. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1977. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1978. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1979. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1980. byte goodChar[] =
  1981. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1982. "abcdefghijklmnopqrstuvwxyz"
  1983. "0123456789+/;";
  1984. byte charTest[] = "A+Gd\0\0\0";
  1985. int i;
  1986. /* Good Base64 encodings. */
  1987. outLen = sizeof(out);
  1988. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1989. if (ret != 0)
  1990. return WC_TEST_RET_ENC_EC(ret);
  1991. outLen = sizeof(out);
  1992. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1993. if (ret != 0)
  1994. return WC_TEST_RET_ENC_EC(ret);
  1995. outLen = sizeof(goodChar);
  1996. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1997. if (ret != 0)
  1998. return WC_TEST_RET_ENC_EC(ret);
  1999. if (outLen != 64 / 4 * 3)
  2000. return WC_TEST_RET_ENC_NC;
  2001. outLen = sizeof(out);
  2002. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2003. if (ret != 0)
  2004. return WC_TEST_RET_ENC_EC(ret);
  2005. /* Bad parameters. */
  2006. outLen = 1;
  2007. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2008. if (ret != BAD_FUNC_ARG)
  2009. return WC_TEST_RET_ENC_EC(ret);
  2010. outLen = sizeof(out);
  2011. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2012. if (ret != ASN_INPUT_E)
  2013. return WC_TEST_RET_ENC_EC(ret);
  2014. outLen = sizeof(out);
  2015. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2016. if (ret != ASN_INPUT_E)
  2017. return WC_TEST_RET_ENC_EC(ret);
  2018. /* Bad character at each offset 0-3. */
  2019. for (i = 0; i < 4; i++) {
  2020. outLen = sizeof(out);
  2021. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2022. if (ret != ASN_INPUT_E)
  2023. return WC_TEST_RET_ENC_I(i);
  2024. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2025. if (ret != ASN_INPUT_E)
  2026. return WC_TEST_RET_ENC_I(i);
  2027. }
  2028. /* Invalid character less than 0x2b */
  2029. for (i = 1; i < 0x2b; i++) {
  2030. outLen = sizeof(out);
  2031. charTest[0] = (byte)i;
  2032. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2033. if (ret != ASN_INPUT_E)
  2034. return WC_TEST_RET_ENC_I(i);
  2035. }
  2036. /* Bad characters in range 0x2b - 0x7a. */
  2037. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2038. outLen = sizeof(out);
  2039. charTest[0] = badChar[i];
  2040. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2041. if (ret != ASN_INPUT_E)
  2042. return WC_TEST_RET_ENC_I(i);
  2043. }
  2044. /* Invalid character greater than 0x7a */
  2045. for (i = 0x7b; i < 0x100; i++) {
  2046. outLen = sizeof(out);
  2047. charTest[0] = (byte)i;
  2048. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2049. if (ret != ASN_INPUT_E)
  2050. return WC_TEST_RET_ENC_I(i);
  2051. }
  2052. #ifdef WOLFSSL_BASE64_ENCODE
  2053. /* Decode and encode all symbols - non-alphanumeric. */
  2054. dataLen = sizeof(data);
  2055. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2056. if (ret != 0)
  2057. return WC_TEST_RET_ENC_EC(ret);
  2058. outLen = sizeof(out);
  2059. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  2060. if (ret != LENGTH_ONLY_E)
  2061. return WC_TEST_RET_ENC_EC(ret);
  2062. outLen = sizeof(out);
  2063. ret = Base64_Encode(data, dataLen, out, &outLen);
  2064. if (ret != 0)
  2065. return WC_TEST_RET_ENC_EC(ret);
  2066. outLen = 7;
  2067. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2068. if (ret != BUFFER_E)
  2069. return WC_TEST_RET_ENC_EC(ret);
  2070. outLen = sizeof(out);
  2071. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2072. if (ret != LENGTH_ONLY_E)
  2073. return WC_TEST_RET_ENC_EC(ret);
  2074. outLen = sizeof(out);
  2075. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2076. if (ret != 0)
  2077. return WC_TEST_RET_ENC_EC(ret);
  2078. outLen = sizeof(out);
  2079. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2080. if (ret != 0)
  2081. return WC_TEST_RET_ENC_EC(ret);
  2082. /* Data that results in an encoding longer than one line. */
  2083. outLen = sizeof(out);
  2084. dataLen = sizeof(longData);
  2085. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2086. if (ret != 0)
  2087. return WC_TEST_RET_ENC_EC(ret);
  2088. outLen = sizeof(out);
  2089. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2090. if (ret != 0)
  2091. return WC_TEST_RET_ENC_EC(ret);
  2092. outLen = sizeof(out);
  2093. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2094. if (ret != 0)
  2095. return WC_TEST_RET_ENC_EC(ret);
  2096. #endif
  2097. return 0;
  2098. }
  2099. #ifdef WOLFSSL_BASE16
  2100. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2101. {
  2102. wc_test_ret_t ret;
  2103. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2104. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2105. byte encoded[40];
  2106. word32 encodedLen;
  2107. byte plain[40];
  2108. word32 len;
  2109. /* length returned includes null termination */
  2110. encodedLen = sizeof(encoded);
  2111. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2112. if (ret != 0)
  2113. return WC_TEST_RET_ENC_EC(ret);
  2114. len = (word32)XSTRLEN((char*)encoded);
  2115. if (len != encodedLen - 1)
  2116. return WC_TEST_RET_ENC_NC;
  2117. len = sizeof(plain);
  2118. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2119. if (ret != 0)
  2120. return WC_TEST_RET_ENC_EC(ret);
  2121. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2122. return WC_TEST_RET_ENC_NC;
  2123. if (encodedLen != sizeof(encodedTestData) ||
  2124. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2125. return WC_TEST_RET_ENC_NC;
  2126. }
  2127. return 0;
  2128. }
  2129. #endif /* WOLFSSL_BASE16 */
  2130. #endif /* !NO_CODING */
  2131. #ifndef NO_ASN
  2132. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2133. {
  2134. wc_test_ret_t ret;
  2135. /* ASN1 encoded date buffer */
  2136. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2137. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  2138. byte format;
  2139. int length;
  2140. const byte* datePart;
  2141. #ifndef NO_ASN_TIME
  2142. struct tm timearg;
  2143. time_t now;
  2144. #endif
  2145. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2146. &length);
  2147. if (ret != 0)
  2148. return WC_TEST_RET_ENC_EC(ret);
  2149. #ifndef NO_ASN_TIME
  2150. /* Parameter Validation tests. */
  2151. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2152. return WC_TEST_RET_ENC_EC(ret);
  2153. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2154. return WC_TEST_RET_ENC_EC(ret);
  2155. now = 0;
  2156. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2157. return WC_TEST_RET_ENC_EC(ret);
  2158. }
  2159. if (now == 0) {
  2160. printf("RTC/Time not set!\n");
  2161. return WC_TEST_RET_ENC_NC;
  2162. }
  2163. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2164. if (ret != 0)
  2165. return WC_TEST_RET_ENC_EC(ret);
  2166. #endif /* !NO_ASN_TIME */
  2167. return 0;
  2168. }
  2169. #endif /* !NO_ASN */
  2170. #ifdef WOLFSSL_MD2
  2171. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2172. {
  2173. wc_test_ret_t ret = 0;
  2174. Md2 md2;
  2175. byte hash[MD2_DIGEST_SIZE];
  2176. testVector a, b, c, d, e, f, g;
  2177. testVector test_md2[7];
  2178. int times = sizeof(test_md2) / sizeof(testVector), i;
  2179. a.input = "";
  2180. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2181. "\x27\x73";
  2182. a.inLen = XSTRLEN(a.input);
  2183. a.outLen = MD2_DIGEST_SIZE;
  2184. b.input = "a";
  2185. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2186. "\xb5\xd1";
  2187. b.inLen = XSTRLEN(b.input);
  2188. b.outLen = MD2_DIGEST_SIZE;
  2189. c.input = "abc";
  2190. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2191. "\xd6\xbb";
  2192. c.inLen = XSTRLEN(c.input);
  2193. c.outLen = MD2_DIGEST_SIZE;
  2194. d.input = "message digest";
  2195. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2196. "\x06\xb0";
  2197. d.inLen = XSTRLEN(d.input);
  2198. d.outLen = MD2_DIGEST_SIZE;
  2199. e.input = "abcdefghijklmnopqrstuvwxyz";
  2200. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2201. "\x94\x0b";
  2202. e.inLen = XSTRLEN(e.input);
  2203. e.outLen = MD2_DIGEST_SIZE;
  2204. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2205. "6789";
  2206. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2207. "\x38\xcd";
  2208. f.inLen = XSTRLEN(f.input);
  2209. f.outLen = MD2_DIGEST_SIZE;
  2210. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2211. "9012345678901234567890";
  2212. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2213. "\xef\xd8";
  2214. g.inLen = XSTRLEN(g.input);
  2215. g.outLen = MD2_DIGEST_SIZE;
  2216. test_md2[0] = a;
  2217. test_md2[1] = b;
  2218. test_md2[2] = c;
  2219. test_md2[3] = d;
  2220. test_md2[4] = e;
  2221. test_md2[5] = f;
  2222. test_md2[6] = g;
  2223. wc_InitMd2(&md2);
  2224. for (i = 0; i < times; ++i) {
  2225. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2226. wc_Md2Final(&md2, hash);
  2227. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2228. return WC_TEST_RET_ENC_I(i);
  2229. }
  2230. for (i = 0; i < times; ++i) {
  2231. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2232. if (ret != 0) {
  2233. return WC_TEST_RET_ENC_I(i);
  2234. }
  2235. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2236. return WC_TEST_RET_ENC_I(i);
  2237. }
  2238. }
  2239. return 0;
  2240. }
  2241. #endif
  2242. #ifndef NO_MD5
  2243. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2244. {
  2245. wc_test_ret_t ret = 0;
  2246. wc_Md5 md5, md5Copy;
  2247. byte hash[WC_MD5_DIGEST_SIZE];
  2248. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2249. testVector a, b, c, d, e, f;
  2250. testVector test_md5[6];
  2251. int times = sizeof(test_md5) / sizeof(testVector), i;
  2252. a.input = "";
  2253. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2254. "\x7e";
  2255. a.inLen = XSTRLEN(a.input);
  2256. a.outLen = WC_MD5_DIGEST_SIZE;
  2257. b.input = "abc";
  2258. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2259. "\x72";
  2260. b.inLen = XSTRLEN(b.input);
  2261. b.outLen = WC_MD5_DIGEST_SIZE;
  2262. c.input = "message digest";
  2263. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2264. "\xd0";
  2265. c.inLen = XSTRLEN(c.input);
  2266. c.outLen = WC_MD5_DIGEST_SIZE;
  2267. d.input = "abcdefghijklmnopqrstuvwxyz";
  2268. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2269. "\x3b";
  2270. d.inLen = XSTRLEN(d.input);
  2271. d.outLen = WC_MD5_DIGEST_SIZE;
  2272. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2273. "6789";
  2274. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2275. "\x9f";
  2276. e.inLen = XSTRLEN(e.input);
  2277. e.outLen = WC_MD5_DIGEST_SIZE;
  2278. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2279. "9012345678901234567890";
  2280. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2281. "\x7a";
  2282. f.inLen = XSTRLEN(f.input);
  2283. f.outLen = WC_MD5_DIGEST_SIZE;
  2284. test_md5[0] = a;
  2285. test_md5[1] = b;
  2286. test_md5[2] = c;
  2287. test_md5[3] = d;
  2288. test_md5[4] = e;
  2289. test_md5[5] = f;
  2290. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2291. if (ret != 0)
  2292. return WC_TEST_RET_ENC_EC(ret);
  2293. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2294. if (ret != 0) {
  2295. wc_Md5Free(&md5);
  2296. return WC_TEST_RET_ENC_EC(ret);
  2297. }
  2298. for (i = 0; i < times; ++i) {
  2299. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2300. (word32)test_md5[i].inLen);
  2301. if (ret != 0)
  2302. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2303. ret = wc_Md5GetHash(&md5, hashcopy);
  2304. if (ret != 0)
  2305. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2306. ret = wc_Md5Copy(&md5, &md5Copy);
  2307. if (ret != 0)
  2308. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2309. ret = wc_Md5Final(&md5, hash);
  2310. if (ret != 0)
  2311. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2312. wc_Md5Free(&md5Copy);
  2313. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2314. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2315. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2316. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2317. }
  2318. #ifndef NO_LARGE_HASH_TEST
  2319. /* BEGIN LARGE HASH TEST */ {
  2320. byte large_input[1024];
  2321. const char* large_digest =
  2322. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2323. for (i = 0; i < (int)sizeof(large_input); i++) {
  2324. large_input[i] = (byte)(i & 0xFF);
  2325. }
  2326. times = 100;
  2327. #ifdef WOLFSSL_PIC32MZ_HASH
  2328. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2329. #endif
  2330. for (i = 0; i < times; ++i) {
  2331. ret = wc_Md5Update(&md5, (byte*)large_input,
  2332. (word32)sizeof(large_input));
  2333. if (ret != 0)
  2334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2335. }
  2336. ret = wc_Md5Final(&md5, hash);
  2337. if (ret != 0)
  2338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2339. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2340. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2341. } /* END LARGE HASH TEST */
  2342. #endif /* NO_LARGE_HASH_TEST */
  2343. exit:
  2344. wc_Md5Free(&md5);
  2345. wc_Md5Free(&md5Copy);
  2346. return ret;
  2347. }
  2348. #endif /* NO_MD5 */
  2349. #ifndef NO_MD4
  2350. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2351. {
  2352. Md4 md4;
  2353. byte hash[MD4_DIGEST_SIZE];
  2354. testVector a, b, c, d, e, f, g;
  2355. testVector test_md4[7];
  2356. int times = sizeof(test_md4) / sizeof(testVector), i;
  2357. a.input = "";
  2358. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2359. "\xc0";
  2360. a.inLen = XSTRLEN(a.input);
  2361. a.outLen = MD4_DIGEST_SIZE;
  2362. b.input = "a";
  2363. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2364. "\x24";
  2365. b.inLen = XSTRLEN(b.input);
  2366. b.outLen = MD4_DIGEST_SIZE;
  2367. c.input = "abc";
  2368. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2369. "\x9d";
  2370. c.inLen = XSTRLEN(c.input);
  2371. c.outLen = MD4_DIGEST_SIZE;
  2372. d.input = "message digest";
  2373. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2374. "\x4b";
  2375. d.inLen = XSTRLEN(d.input);
  2376. d.outLen = MD4_DIGEST_SIZE;
  2377. e.input = "abcdefghijklmnopqrstuvwxyz";
  2378. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2379. "\xa9";
  2380. e.inLen = XSTRLEN(e.input);
  2381. e.outLen = MD4_DIGEST_SIZE;
  2382. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2383. "6789";
  2384. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2385. "\xe4";
  2386. f.inLen = XSTRLEN(f.input);
  2387. f.outLen = MD4_DIGEST_SIZE;
  2388. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2389. "9012345678901234567890";
  2390. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2391. "\x36";
  2392. g.inLen = XSTRLEN(g.input);
  2393. g.outLen = MD4_DIGEST_SIZE;
  2394. test_md4[0] = a;
  2395. test_md4[1] = b;
  2396. test_md4[2] = c;
  2397. test_md4[3] = d;
  2398. test_md4[4] = e;
  2399. test_md4[5] = f;
  2400. test_md4[6] = g;
  2401. wc_InitMd4(&md4);
  2402. for (i = 0; i < times; ++i) {
  2403. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2404. wc_Md4Final(&md4, hash);
  2405. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2406. return WC_TEST_RET_ENC_I(i);
  2407. }
  2408. return 0;
  2409. }
  2410. #endif /* NO_MD4 */
  2411. #ifndef NO_SHA
  2412. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2413. {
  2414. wc_test_ret_t ret = 0;
  2415. wc_Sha sha, shaCopy;
  2416. byte hash[WC_SHA_DIGEST_SIZE];
  2417. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2418. testVector a, b, c, d, e;
  2419. testVector test_sha[5];
  2420. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2421. a.input = "";
  2422. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2423. "\x90\xaf\xd8\x07\x09";
  2424. a.inLen = XSTRLEN(a.input);
  2425. a.outLen = WC_SHA_DIGEST_SIZE;
  2426. b.input = "abc";
  2427. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2428. "\x6C\x9C\xD0\xD8\x9D";
  2429. b.inLen = XSTRLEN(b.input);
  2430. b.outLen = WC_SHA_DIGEST_SIZE;
  2431. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2432. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2433. "\xE5\xE5\x46\x70\xF1";
  2434. c.inLen = XSTRLEN(c.input);
  2435. c.outLen = WC_SHA_DIGEST_SIZE;
  2436. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2437. "aaaaaa";
  2438. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2439. "\x2A\x25\xEC\x64\x4D";
  2440. d.inLen = XSTRLEN(d.input);
  2441. d.outLen = WC_SHA_DIGEST_SIZE;
  2442. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2443. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2444. "aaaaaaaaaa";
  2445. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2446. "\x53\x99\x5E\x26\xA0";
  2447. e.inLen = XSTRLEN(e.input);
  2448. e.outLen = WC_SHA_DIGEST_SIZE;
  2449. test_sha[0] = a;
  2450. test_sha[1] = b;
  2451. test_sha[2] = c;
  2452. test_sha[3] = d;
  2453. test_sha[4] = e;
  2454. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2455. if (ret != 0)
  2456. return WC_TEST_RET_ENC_EC(ret);
  2457. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2458. if (ret != 0) {
  2459. wc_ShaFree(&sha);
  2460. return WC_TEST_RET_ENC_EC(ret);
  2461. }
  2462. for (i = 0; i < times; ++i) {
  2463. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2464. (word32)test_sha[i].inLen);
  2465. if (ret != 0)
  2466. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2467. ret = wc_ShaGetHash(&sha, hashcopy);
  2468. if (ret != 0)
  2469. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2470. ret = wc_ShaCopy(&sha, &shaCopy);
  2471. if (ret != 0)
  2472. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2473. ret = wc_ShaFinal(&sha, hash);
  2474. if (ret != 0)
  2475. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2476. wc_ShaFree(&shaCopy);
  2477. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2478. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2479. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2480. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2481. }
  2482. #ifndef NO_LARGE_HASH_TEST
  2483. /* BEGIN LARGE HASH TEST */ {
  2484. byte large_input[1024];
  2485. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2486. defined(HASH_SIZE_LIMIT)
  2487. const char* large_digest =
  2488. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2489. "\x15\x8c\x6d\xb6";
  2490. #else
  2491. const char* large_digest =
  2492. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2493. "\xc5\xd9\x0a\xac";
  2494. #endif
  2495. for (i = 0; i < (int)sizeof(large_input); i++) {
  2496. large_input[i] = (byte)(i & 0xFF);
  2497. }
  2498. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2499. defined(HASH_SIZE_LIMIT)
  2500. times = 20;
  2501. #else
  2502. times = 100;
  2503. #endif
  2504. #ifdef WOLFSSL_PIC32MZ_HASH
  2505. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2506. #endif
  2507. for (i = 0; i < times; ++i) {
  2508. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2509. (word32)sizeof(large_input));
  2510. if (ret != 0)
  2511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2512. }
  2513. ret = wc_ShaFinal(&sha, hash);
  2514. if (ret != 0)
  2515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2516. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2517. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2518. } /* END LARGE HASH TEST */
  2519. #endif /* NO_LARGE_HASH_TEST */
  2520. exit:
  2521. wc_ShaFree(&sha);
  2522. wc_ShaFree(&shaCopy);
  2523. return ret;
  2524. }
  2525. #endif /* NO_SHA */
  2526. #ifdef WOLFSSL_RIPEMD
  2527. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  2528. {
  2529. RipeMd ripemd;
  2530. wc_test_ret_t ret;
  2531. byte hash[RIPEMD_DIGEST_SIZE];
  2532. testVector a, b, c, d;
  2533. testVector test_ripemd[4];
  2534. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2535. a.input = "abc";
  2536. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2537. "\xb0\x87\xf1\x5a\x0b\xfc";
  2538. a.inLen = XSTRLEN(a.input);
  2539. a.outLen = RIPEMD_DIGEST_SIZE;
  2540. b.input = "message digest";
  2541. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2542. "\x5f\xfa\x21\x59\x5f\x36";
  2543. b.inLen = XSTRLEN(b.input);
  2544. b.outLen = RIPEMD_DIGEST_SIZE;
  2545. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2546. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2547. "\xf4\x9a\xda\x62\xeb\x2b";
  2548. c.inLen = XSTRLEN(c.input);
  2549. c.outLen = RIPEMD_DIGEST_SIZE;
  2550. d.input = "12345678901234567890123456789012345678901234567890123456"
  2551. "789012345678901234567890";
  2552. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2553. "\x82\xbf\x63\x32\x6b\xfb";
  2554. d.inLen = XSTRLEN(d.input);
  2555. d.outLen = RIPEMD_DIGEST_SIZE;
  2556. test_ripemd[0] = a;
  2557. test_ripemd[1] = b;
  2558. test_ripemd[2] = c;
  2559. test_ripemd[3] = d;
  2560. ret = wc_InitRipeMd(&ripemd);
  2561. if (ret != 0) {
  2562. return WC_TEST_RET_ENC_EC(ret);
  2563. }
  2564. for (i = 0; i < times; ++i) {
  2565. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2566. (word32)test_ripemd[i].inLen);
  2567. if (ret != 0) {
  2568. return WC_TEST_RET_ENC_I(i);
  2569. }
  2570. ret = wc_RipeMdFinal(&ripemd, hash);
  2571. if (ret != 0) {
  2572. return WC_TEST_RET_ENC_I(i);
  2573. }
  2574. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2575. return WC_TEST_RET_ENC_I(i);
  2576. }
  2577. return 0;
  2578. }
  2579. #endif /* WOLFSSL_RIPEMD */
  2580. #ifdef HAVE_BLAKE2
  2581. #define BLAKE2B_TESTS 3
  2582. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2583. {
  2584. {
  2585. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2586. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2587. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2588. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2589. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2590. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2591. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2592. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2593. },
  2594. {
  2595. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2596. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2597. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2598. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2599. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2600. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2601. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2602. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2603. },
  2604. {
  2605. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2606. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2607. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2608. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2609. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2610. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2611. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2612. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2613. }
  2614. };
  2615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  2616. {
  2617. Blake2b b2b;
  2618. byte digest[64];
  2619. byte input[64];
  2620. int i, ret;
  2621. for (i = 0; i < (int)sizeof(input); i++)
  2622. input[i] = (byte)i;
  2623. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2624. ret = wc_InitBlake2b(&b2b, 64);
  2625. if (ret != 0)
  2626. return WC_TEST_RET_ENC_I(i);
  2627. ret = wc_Blake2bUpdate(&b2b, input, i);
  2628. if (ret != 0)
  2629. return WC_TEST_RET_ENC_I(i);
  2630. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2631. if (ret != 0)
  2632. return WC_TEST_RET_ENC_I(i);
  2633. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2634. return WC_TEST_RET_ENC_I(i);
  2635. }
  2636. }
  2637. return 0;
  2638. }
  2639. #endif /* HAVE_BLAKE2 */
  2640. #ifdef HAVE_BLAKE2S
  2641. #define BLAKE2S_TESTS 3
  2642. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2643. {
  2644. {
  2645. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2646. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2647. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2648. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2649. },
  2650. {
  2651. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2652. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2653. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2654. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2655. },
  2656. {
  2657. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2658. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2659. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2660. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2661. }
  2662. };
  2663. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  2664. {
  2665. Blake2s b2s;
  2666. byte digest[32];
  2667. byte input[64];
  2668. int i, ret;
  2669. for (i = 0; i < (int)sizeof(input); i++)
  2670. input[i] = (byte)i;
  2671. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2672. ret = wc_InitBlake2s(&b2s, 32);
  2673. if (ret != 0)
  2674. return WC_TEST_RET_ENC_I(i);
  2675. ret = wc_Blake2sUpdate(&b2s, input, i);
  2676. if (ret != 0)
  2677. return WC_TEST_RET_ENC_I(i);
  2678. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2679. if (ret != 0)
  2680. return WC_TEST_RET_ENC_I(i);
  2681. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2682. return WC_TEST_RET_ENC_I(i);
  2683. }
  2684. }
  2685. return 0;
  2686. }
  2687. #endif /* HAVE_BLAKE2S */
  2688. #ifdef WOLFSSL_SHA224
  2689. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  2690. {
  2691. wc_Sha224 sha, shaCopy;
  2692. byte hash[WC_SHA224_DIGEST_SIZE];
  2693. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2694. wc_test_ret_t ret = 0;
  2695. testVector a, b, c;
  2696. testVector test_sha[3];
  2697. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2698. a.input = "";
  2699. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2700. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2701. a.inLen = XSTRLEN(a.input);
  2702. a.outLen = WC_SHA224_DIGEST_SIZE;
  2703. b.input = "abc";
  2704. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2705. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2706. b.inLen = XSTRLEN(b.input);
  2707. b.outLen = WC_SHA224_DIGEST_SIZE;
  2708. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2709. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2710. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2711. c.inLen = XSTRLEN(c.input);
  2712. c.outLen = WC_SHA224_DIGEST_SIZE;
  2713. test_sha[0] = a;
  2714. test_sha[1] = b;
  2715. test_sha[2] = c;
  2716. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2717. if (ret != 0)
  2718. return WC_TEST_RET_ENC_EC(ret);
  2719. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2720. if (ret != 0) {
  2721. wc_Sha224Free(&sha);
  2722. return WC_TEST_RET_ENC_EC(ret);
  2723. }
  2724. for (i = 0; i < times; ++i) {
  2725. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2726. (word32)test_sha[i].inLen);
  2727. if (ret != 0)
  2728. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2729. ret = wc_Sha224GetHash(&sha, hashcopy);
  2730. if (ret != 0)
  2731. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2732. ret = wc_Sha224Copy(&sha, &shaCopy);
  2733. if (ret != 0)
  2734. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2735. ret = wc_Sha224Final(&sha, hash);
  2736. if (ret != 0)
  2737. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2738. wc_Sha224Free(&shaCopy);
  2739. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2740. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2741. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2742. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2743. }
  2744. exit:
  2745. wc_Sha224Free(&sha);
  2746. wc_Sha224Free(&shaCopy);
  2747. return ret;
  2748. }
  2749. #endif
  2750. #ifndef NO_SHA256
  2751. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  2752. {
  2753. wc_Sha256 sha, shaCopy;
  2754. byte hash[WC_SHA256_DIGEST_SIZE];
  2755. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2756. wc_test_ret_t ret = 0;
  2757. testVector a, b, c;
  2758. testVector test_sha[3];
  2759. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2760. a.input = "";
  2761. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2762. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2763. "\xb8\x55";
  2764. a.inLen = XSTRLEN(a.input);
  2765. a.outLen = WC_SHA256_DIGEST_SIZE;
  2766. b.input = "abc";
  2767. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2768. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2769. "\x15\xAD";
  2770. b.inLen = XSTRLEN(b.input);
  2771. b.outLen = WC_SHA256_DIGEST_SIZE;
  2772. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2773. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2774. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2775. "\x06\xC1";
  2776. c.inLen = XSTRLEN(c.input);
  2777. c.outLen = WC_SHA256_DIGEST_SIZE;
  2778. test_sha[0] = a;
  2779. test_sha[1] = b;
  2780. test_sha[2] = c;
  2781. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2782. if (ret != 0)
  2783. return WC_TEST_RET_ENC_EC(ret);
  2784. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2785. if (ret != 0) {
  2786. wc_Sha256Free(&sha);
  2787. return WC_TEST_RET_ENC_EC(ret);
  2788. }
  2789. for (i = 0; i < times; ++i) {
  2790. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2791. (word32)test_sha[i].inLen);
  2792. if (ret != 0) {
  2793. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2794. }
  2795. ret = wc_Sha256GetHash(&sha, hashcopy);
  2796. if (ret != 0)
  2797. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2798. ret = wc_Sha256Copy(&sha, &shaCopy);
  2799. if (ret != 0)
  2800. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2801. ret = wc_Sha256Final(&sha, hash);
  2802. if (ret != 0)
  2803. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2804. wc_Sha256Free(&shaCopy);
  2805. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2806. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2807. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2808. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2809. }
  2810. #ifndef NO_LARGE_HASH_TEST
  2811. /* BEGIN LARGE HASH TEST */ {
  2812. byte large_input[1024];
  2813. #ifdef HASH_SIZE_LIMIT
  2814. const char* large_digest =
  2815. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2816. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2817. #else
  2818. const char* large_digest =
  2819. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2820. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2821. #endif
  2822. for (i = 0; i < (int)sizeof(large_input); i++) {
  2823. large_input[i] = (byte)(i & 0xFF);
  2824. }
  2825. #ifdef HASH_SIZE_LIMIT
  2826. times = 20;
  2827. #else
  2828. times = 100;
  2829. #endif
  2830. #ifdef WOLFSSL_PIC32MZ_HASH
  2831. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2832. #endif
  2833. for (i = 0; i < times; ++i) {
  2834. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2835. (word32)sizeof(large_input));
  2836. if (ret != 0)
  2837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2838. }
  2839. ret = wc_Sha256Final(&sha, hash);
  2840. if (ret != 0)
  2841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2842. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2843. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2844. } /* END LARGE HASH TEST */
  2845. #endif /* NO_LARGE_HASH_TEST */
  2846. exit:
  2847. wc_Sha256Free(&sha);
  2848. wc_Sha256Free(&shaCopy);
  2849. return ret;
  2850. }
  2851. #endif
  2852. #ifdef WOLFSSL_SHA512
  2853. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  2854. {
  2855. /*
  2856. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  2857. */
  2858. wc_Sha512 sha, shaCopy;
  2859. byte hash[WC_SHA512_DIGEST_SIZE];
  2860. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2861. wc_test_ret_t ret = 0;
  2862. testVector a, b, c;
  2863. testVector test_sha[3];
  2864. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2865. a.input = "";
  2866. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2867. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2868. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2869. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2870. "\xf9\x27\xda\x3e";
  2871. a.inLen = XSTRLEN(a.input);
  2872. a.outLen = WC_SHA512_DIGEST_SIZE;
  2873. b.input = "abc";
  2874. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2875. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2876. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2877. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2878. "\xa5\x4c\xa4\x9f";
  2879. b.inLen = XSTRLEN(b.input);
  2880. b.outLen = WC_SHA512_DIGEST_SIZE;
  2881. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2882. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2883. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2884. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2885. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2886. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2887. "\x87\x4b\xe9\x09";
  2888. c.inLen = XSTRLEN(c.input);
  2889. c.outLen = WC_SHA512_DIGEST_SIZE;
  2890. test_sha[0] = a;
  2891. test_sha[1] = b;
  2892. test_sha[2] = c;
  2893. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2894. if (ret != 0)
  2895. return WC_TEST_RET_ENC_EC(ret);
  2896. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2897. if (ret != 0) {
  2898. wc_Sha512Free(&sha);
  2899. return WC_TEST_RET_ENC_EC(ret);
  2900. }
  2901. for (i = 0; i < times; ++i) {
  2902. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2903. (word32)test_sha[i].inLen);
  2904. if (ret != 0)
  2905. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2906. ret = wc_Sha512GetHash(&sha, hashcopy);
  2907. if (ret != 0)
  2908. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2909. ret = wc_Sha512Copy(&sha, &shaCopy);
  2910. if (ret != 0)
  2911. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2912. ret = wc_Sha512Final(&sha, hash);
  2913. if (ret != 0)
  2914. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2915. wc_Sha512Free(&shaCopy);
  2916. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2917. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2918. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2919. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2920. }
  2921. #ifndef NO_LARGE_HASH_TEST
  2922. /* BEGIN LARGE HASH TEST */ {
  2923. byte large_input[1024];
  2924. #ifdef HASH_SIZE_LIMIT
  2925. const char* large_digest =
  2926. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2927. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2928. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2929. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2930. #else
  2931. const char* large_digest =
  2932. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2933. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2934. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2935. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2936. #endif
  2937. for (i = 0; i < (int)sizeof(large_input); i++) {
  2938. large_input[i] = (byte)(i & 0xFF);
  2939. }
  2940. #ifdef HASH_SIZE_LIMIT
  2941. times = 20;
  2942. #else
  2943. times = 100;
  2944. #endif
  2945. for (i = 0; i < times; ++i) {
  2946. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2947. (word32)sizeof(large_input));
  2948. if (ret != 0)
  2949. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2950. }
  2951. ret = wc_Sha512Final(&sha, hash);
  2952. if (ret != 0)
  2953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2954. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2955. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2956. #ifndef NO_UNALIGNED_MEMORY_TEST
  2957. /* Unaligned memory access test */
  2958. for (i = 1; i < 16; i++) {
  2959. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2960. (word32)sizeof(large_input) - i);
  2961. if (ret != 0)
  2962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2963. ret = wc_Sha512Final(&sha, hash);
  2964. }
  2965. #endif
  2966. } /* END LARGE HASH TEST */
  2967. #endif /* NO_LARGE_HASH_TEST */
  2968. exit:
  2969. wc_Sha512Free(&sha);
  2970. wc_Sha512Free(&shaCopy);
  2971. return ret;
  2972. }
  2973. #if !defined(WOLFSSL_NOSHA512_224) && \
  2974. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2975. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  2976. {
  2977. /*
  2978. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  2979. **
  2980. ** NIST SHA512/224 Document Example:
  2981. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  2982. */
  2983. wc_Sha512 sha, shaCopy;
  2984. byte hash[WC_SHA512_224_DIGEST_SIZE];
  2985. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  2986. wc_test_ret_t ret = 0;
  2987. testVector a, b, c;
  2988. testVector test_sha[3];
  2989. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2990. a.input = "";
  2991. a.output = "\x6e\xd0\xdd\x02"
  2992. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  2993. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  2994. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  2995. a.inLen = XSTRLEN(a.input);
  2996. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  2997. /*
  2998. ** See page 1 in above document for the SHA512/224 "abc" test:
  2999. */
  3000. b.input = "abc";
  3001. /*
  3002. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3003. */
  3004. b.output = "\x46\x34\x27\x0f"
  3005. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3006. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3007. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3008. b.inLen = XSTRLEN(b.input);
  3009. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3010. /*
  3011. ** See page 4 in above for the 2-block test:
  3012. */
  3013. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3014. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3015. /*
  3016. ** see page 9 in above document for the 2-block test message digest:
  3017. */
  3018. c.output = "\x23\xfe\xc5\xbb"
  3019. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3020. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3021. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3022. c.inLen = XSTRLEN(c.input);
  3023. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3024. test_sha[0] = a;
  3025. test_sha[1] = b;
  3026. test_sha[2] = c;
  3027. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3028. if (ret != 0)
  3029. return WC_TEST_RET_ENC_EC(ret);
  3030. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3031. if (ret != 0) {
  3032. wc_Sha512_224Free(&sha);
  3033. return WC_TEST_RET_ENC_EC(ret);
  3034. }
  3035. for (i = 0; i < times; ++i) {
  3036. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3037. (word32)test_sha[i].inLen);
  3038. if (ret != 0)
  3039. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3040. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3041. if (ret != 0)
  3042. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3043. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3044. if (ret != 0)
  3045. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3046. ret = wc_Sha512_224Final(&sha, hash);
  3047. if (ret != 0)
  3048. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3049. wc_Sha512_224Free(&shaCopy);
  3050. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3051. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3052. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3053. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3054. }
  3055. #ifndef NO_LARGE_HASH_TEST
  3056. /* BEGIN LARGE HASH TEST */ {
  3057. byte large_input[1024];
  3058. #ifdef HASH_SIZE_LIMIT
  3059. const char* large_digest =
  3060. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3061. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3062. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3063. "\x14\x6c\xcf\x6a";
  3064. #else
  3065. const char* large_digest =
  3066. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3067. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3068. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3069. "\xe2\x4e\x85\x05";
  3070. #endif
  3071. for (i = 0; i < (int)sizeof(large_input); i++) {
  3072. large_input[i] = (byte)(i & 0xFF);
  3073. }
  3074. #ifdef HASH_SIZE_LIMIT
  3075. times = 20;
  3076. #else
  3077. times = 100;
  3078. #endif
  3079. for (i = 0; i < times; ++i) {
  3080. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3081. (word32)sizeof(large_input));
  3082. if (ret != 0)
  3083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3084. }
  3085. ret = wc_Sha512_224Final(&sha, hash);
  3086. if (ret != 0)
  3087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3088. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3089. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3090. #ifndef NO_UNALIGNED_MEMORY_TEST
  3091. /* Unaligned memory access test */
  3092. for (i = 1; i < 16; i++) {
  3093. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3094. (word32)sizeof(large_input) - i);
  3095. if (ret != 0)
  3096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3097. ret = wc_Sha512_224Final(&sha, hash);
  3098. }
  3099. #endif
  3100. } /* END LARGE HASH TEST */
  3101. #endif /* NO_LARGE_HASH_TEST */
  3102. exit:
  3103. wc_Sha512_224Free(&sha);
  3104. wc_Sha512_224Free(&shaCopy);
  3105. return ret;
  3106. } /* sha512_224_test */
  3107. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3108. #if !defined(WOLFSSL_NOSHA512_256) && \
  3109. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3110. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3111. {
  3112. /*
  3113. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3114. ** NIST SHA512/256 Document Example:
  3115. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3116. */
  3117. wc_Sha512 sha, shaCopy;
  3118. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3119. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3120. wc_test_ret_t ret = 0;
  3121. testVector a, b, c;
  3122. testVector test_sha[3];
  3123. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3124. a.input = "";
  3125. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3126. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3127. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3128. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3129. a.inLen = XSTRLEN(a.input);
  3130. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3131. /*
  3132. ** See page 1 of above document for "abc" example:
  3133. */
  3134. b.input = "abc";
  3135. /*
  3136. ** See page 4 of above document for "abc" example digest:
  3137. */
  3138. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3139. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3140. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3141. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3142. b.inLen = XSTRLEN(b.input);
  3143. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3144. /*
  3145. ** See page 4 of above document for Two Block Message Sample:
  3146. */
  3147. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3148. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3149. /*
  3150. ** See page 10 of above document for Two Block Message Sample digest:
  3151. */
  3152. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3153. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3154. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3155. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3156. c.inLen = XSTRLEN(c.input);
  3157. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3158. test_sha[0] = a;
  3159. test_sha[1] = b;
  3160. test_sha[2] = c;
  3161. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3162. if (ret != 0)
  3163. return WC_TEST_RET_ENC_EC(ret);
  3164. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3165. if (ret != 0) {
  3166. wc_Sha512_256Free(&sha);
  3167. return WC_TEST_RET_ENC_EC(ret);
  3168. }
  3169. for (i = 0; i < times; ++i) {
  3170. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3171. (word32)test_sha[i].inLen);
  3172. if (ret != 0)
  3173. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3174. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3175. if (ret != 0)
  3176. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3177. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3178. if (ret != 0)
  3179. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3180. ret = wc_Sha512_256Final(&sha, hash);
  3181. if (ret != 0)
  3182. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3183. wc_Sha512_256Free(&shaCopy);
  3184. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3186. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3187. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3188. }
  3189. #ifndef NO_LARGE_HASH_TEST
  3190. /* BEGIN LARGE HASH TEST */ {
  3191. byte large_input[1024];
  3192. #ifdef HASH_SIZE_LIMIT
  3193. const char* large_digest =
  3194. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3195. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3196. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3197. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3198. #else
  3199. const char* large_digest =
  3200. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3201. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3202. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3203. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3204. #endif
  3205. for (i = 0; i < (int)sizeof(large_input); i++) {
  3206. large_input[i] = (byte)(i & 0xFF);
  3207. }
  3208. #ifdef HASH_SIZE_LIMIT
  3209. times = 20;
  3210. #else
  3211. times = 100;
  3212. #endif
  3213. for (i = 0; i < times; ++i) {
  3214. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3215. (word32)sizeof(large_input));
  3216. if (ret != 0)
  3217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3218. }
  3219. ret = wc_Sha512_256Final(&sha, hash);
  3220. if (ret != 0)
  3221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3222. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3223. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3224. #ifndef NO_UNALIGNED_MEMORY_TEST
  3225. /* Unaligned memory access test */
  3226. for (i = 1; i < 16; i++) {
  3227. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3228. (word32)sizeof(large_input) - i);
  3229. if (ret != 0)
  3230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3231. ret = wc_Sha512_256Final(&sha, hash);
  3232. }
  3233. #endif
  3234. } /* END LARGE HASH TEST */
  3235. #endif /* NO_LARGE_HASH_TEST */
  3236. exit:
  3237. wc_Sha512_256Free(&sha);
  3238. wc_Sha512_256Free(&shaCopy);
  3239. return ret;
  3240. } /* sha512_256_test */
  3241. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3242. #endif /* WOLFSSL_SHA512 */
  3243. #ifdef WOLFSSL_SHA384
  3244. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3245. {
  3246. wc_Sha384 sha, shaCopy;
  3247. byte hash[WC_SHA384_DIGEST_SIZE];
  3248. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3249. wc_test_ret_t ret = 0;
  3250. testVector a, b, c;
  3251. testVector test_sha[3];
  3252. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3253. a.input = "";
  3254. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3255. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3256. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3257. "\x98\xb9\x5b";
  3258. a.inLen = XSTRLEN(a.input);
  3259. a.outLen = WC_SHA384_DIGEST_SIZE;
  3260. b.input = "abc";
  3261. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3262. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3263. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3264. "\xc8\x25\xa7";
  3265. b.inLen = XSTRLEN(b.input);
  3266. b.outLen = WC_SHA384_DIGEST_SIZE;
  3267. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3268. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3269. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3270. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3271. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3272. "\x74\x60\x39";
  3273. c.inLen = XSTRLEN(c.input);
  3274. c.outLen = WC_SHA384_DIGEST_SIZE;
  3275. test_sha[0] = a;
  3276. test_sha[1] = b;
  3277. test_sha[2] = c;
  3278. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3279. if (ret != 0)
  3280. return WC_TEST_RET_ENC_EC(ret);
  3281. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3282. if (ret != 0) {
  3283. wc_Sha384Free(&sha);
  3284. return WC_TEST_RET_ENC_EC(ret);
  3285. }
  3286. for (i = 0; i < times; ++i) {
  3287. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3288. (word32)test_sha[i].inLen);
  3289. if (ret != 0)
  3290. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3291. ret = wc_Sha384GetHash(&sha, hashcopy);
  3292. if (ret != 0)
  3293. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3294. ret = wc_Sha384Copy(&sha, &shaCopy);
  3295. if (ret != 0)
  3296. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3297. ret = wc_Sha384Final(&sha, hash);
  3298. if (ret != 0)
  3299. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3300. wc_Sha384Free(&shaCopy);
  3301. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3302. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3303. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3304. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3305. }
  3306. #ifndef NO_LARGE_HASH_TEST
  3307. /* BEGIN LARGE HASH TEST */ {
  3308. byte large_input[1024];
  3309. #ifdef HASH_SIZE_LIMIT
  3310. const char* large_digest =
  3311. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3312. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3313. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  3314. #else
  3315. const char* large_digest =
  3316. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  3317. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  3318. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  3319. #endif
  3320. for (i = 0; i < (int)sizeof(large_input); i++) {
  3321. large_input[i] = (byte)(i & 0xFF);
  3322. }
  3323. #ifdef HASH_SIZE_LIMIT
  3324. times = 20;
  3325. #else
  3326. times = 100;
  3327. #endif
  3328. for (i = 0; i < times; ++i) {
  3329. ret = wc_Sha384Update(&sha, (byte*)large_input,
  3330. (word32)sizeof(large_input));
  3331. if (ret != 0)
  3332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3333. }
  3334. ret = wc_Sha384Final(&sha, hash);
  3335. if (ret != 0)
  3336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3337. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  3338. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3339. } /* END LARGE HASH TEST */
  3340. #endif /* NO_LARGE_HASH_TEST */
  3341. exit:
  3342. wc_Sha384Free(&sha);
  3343. wc_Sha384Free(&shaCopy);
  3344. return ret;
  3345. }
  3346. #endif /* WOLFSSL_SHA384 */
  3347. #ifdef WOLFSSL_SHA3
  3348. #ifndef WOLFSSL_NOSHA3_224
  3349. static wc_test_ret_t sha3_224_test(void)
  3350. {
  3351. wc_Sha3 sha;
  3352. byte hash[WC_SHA3_224_DIGEST_SIZE];
  3353. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  3354. testVector a, b, c;
  3355. testVector test_sha[3];
  3356. wc_test_ret_t ret = 0;
  3357. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3358. a.input = "";
  3359. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  3360. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  3361. a.inLen = XSTRLEN(a.input);
  3362. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  3363. b.input = "abc";
  3364. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  3365. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  3366. b.inLen = XSTRLEN(b.input);
  3367. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  3368. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3369. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  3370. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  3371. c.inLen = XSTRLEN(c.input);
  3372. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  3373. test_sha[0] = a;
  3374. test_sha[1] = b;
  3375. test_sha[2] = c;
  3376. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  3377. if (ret != 0)
  3378. return WC_TEST_RET_ENC_EC(ret);
  3379. for (i = 0; i < times; ++i) {
  3380. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  3381. (word32)test_sha[i].inLen);
  3382. if (ret != 0)
  3383. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3384. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  3385. if (ret != 0)
  3386. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3387. ret = wc_Sha3_224_Final(&sha, hash);
  3388. if (ret != 0)
  3389. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3390. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  3391. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3392. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  3393. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3394. }
  3395. #ifndef NO_LARGE_HASH_TEST
  3396. /* BEGIN LARGE HASH TEST */ {
  3397. byte large_input[1024];
  3398. const char* large_digest =
  3399. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  3400. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  3401. for (i = 0; i < (int)sizeof(large_input); i++) {
  3402. large_input[i] = (byte)(i & 0xFF);
  3403. }
  3404. times = 100;
  3405. for (i = 0; i < times; ++i) {
  3406. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  3407. (word32)sizeof(large_input));
  3408. if (ret != 0)
  3409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3410. }
  3411. ret = wc_Sha3_224_Final(&sha, hash);
  3412. if (ret != 0)
  3413. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3414. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  3415. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3416. } /* END LARGE HASH TEST */
  3417. #endif /* NO_LARGE_HASH_TEST */
  3418. exit:
  3419. wc_Sha3_224_Free(&sha);
  3420. return ret;
  3421. }
  3422. #endif /* WOLFSSL_NOSHA3_224 */
  3423. #ifndef WOLFSSL_NOSHA3_256
  3424. static wc_test_ret_t sha3_256_test(void)
  3425. {
  3426. wc_Sha3 sha;
  3427. byte hash[WC_SHA3_256_DIGEST_SIZE];
  3428. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  3429. testVector a, b, c;
  3430. testVector test_sha[3];
  3431. wc_test_ret_t ret = 0;
  3432. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3433. byte large_input[1024];
  3434. const char* large_digest =
  3435. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  3436. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  3437. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3438. /* test vector with hash of empty string */
  3439. const char* Keccak256EmptyOut =
  3440. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  3441. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  3442. #endif
  3443. /*
  3444. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  3445. */
  3446. a.input = "";
  3447. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  3448. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  3449. "\x43\x4a";
  3450. a.inLen = XSTRLEN(a.input);
  3451. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  3452. b.input = "abc";
  3453. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  3454. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  3455. "\x15\x32";
  3456. b.inLen = XSTRLEN(b.input);
  3457. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  3458. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3459. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  3460. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  3461. "\x33\x76";
  3462. c.inLen = XSTRLEN(c.input);
  3463. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  3464. test_sha[0] = a;
  3465. test_sha[1] = b;
  3466. test_sha[2] = c;
  3467. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  3468. if (ret != 0)
  3469. return WC_TEST_RET_ENC_EC(ret);
  3470. for (i = 0; i < times; ++i) {
  3471. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  3472. (word32)test_sha[i].inLen);
  3473. if (ret != 0)
  3474. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3475. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  3476. if (ret != 0)
  3477. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3478. ret = wc_Sha3_256_Final(&sha, hash);
  3479. if (ret != 0)
  3480. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3481. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  3482. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3483. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  3484. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3485. }
  3486. #ifndef NO_LARGE_HASH_TEST
  3487. /* BEGIN LARGE HASH TEST */ {
  3488. for (i = 0; i < (int)sizeof(large_input); i++) {
  3489. large_input[i] = (byte)(i & 0xFF);
  3490. }
  3491. times = 100;
  3492. for (i = 0; i < times; ++i) {
  3493. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  3494. (word32)sizeof(large_input));
  3495. if (ret != 0)
  3496. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3497. }
  3498. ret = wc_Sha3_256_Final(&sha, hash);
  3499. if (ret != 0)
  3500. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3501. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  3502. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3503. } /* END LARGE HASH TEST */
  3504. #endif /* NO_LARGE_HASH_TEST */
  3505. /* this is a software only variant of SHA3 not supported by external hardware devices */
  3506. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3507. /* Test for Keccak256 */
  3508. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  3509. if (ret != 0) {
  3510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3511. }
  3512. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  3513. if (ret != 0) {
  3514. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3515. }
  3516. ret = wc_Sha3_256_Final(&sha, hash);
  3517. if (ret != 0) {
  3518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3519. }
  3520. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  3521. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3522. }
  3523. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  3524. exit:
  3525. wc_Sha3_256_Free(&sha);
  3526. return ret;
  3527. }
  3528. #endif /* WOLFSSL_NOSHA3_256 */
  3529. #ifndef WOLFSSL_NOSHA3_384
  3530. static wc_test_ret_t sha3_384_test(void)
  3531. {
  3532. wc_Sha3 sha;
  3533. byte hash[WC_SHA3_384_DIGEST_SIZE];
  3534. byte buf[64];
  3535. #ifndef NO_INTM_HASH_TEST
  3536. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  3537. #endif
  3538. testVector a, b, c;
  3539. testVector test_sha[3];
  3540. wc_test_ret_t ret;
  3541. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3542. /*
  3543. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  3544. */
  3545. a.input = "";
  3546. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  3547. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  3548. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  3549. "\xd5\xf0\x04";
  3550. a.inLen = XSTRLEN(a.input);
  3551. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  3552. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3553. /* NIST test vector with a length that is a multiple of 4 */
  3554. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  3555. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  3556. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  3557. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  3558. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  3559. "\x19\x87\x22";
  3560. b.inLen = XSTRLEN(b.input);
  3561. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3562. #else
  3563. b.input = "abc";
  3564. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  3565. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  3566. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  3567. "\x37\x6d\x25";
  3568. b.inLen = XSTRLEN(b.input);
  3569. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3570. #endif
  3571. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3572. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  3573. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  3574. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  3575. "\x65\x7c\x22";
  3576. c.inLen = XSTRLEN(c.input);
  3577. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  3578. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3579. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  3580. #else
  3581. test_sha[0] = a;
  3582. #endif
  3583. test_sha[1] = b;
  3584. test_sha[2] = c;
  3585. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  3586. if (ret != 0)
  3587. return WC_TEST_RET_ENC_EC(ret);
  3588. for (i = 0; i < times; ++i) {
  3589. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  3590. ret = wc_Sha3_384_Update(&sha, buf,
  3591. (word32)test_sha[i].inLen);
  3592. if (ret != 0)
  3593. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3594. #ifndef NO_INTM_HASH_TEST
  3595. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  3596. if (ret != 0)
  3597. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3598. #endif
  3599. ret = wc_Sha3_384_Final(&sha, hash);
  3600. if (ret != 0)
  3601. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3602. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  3603. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3604. #ifndef NO_INTM_HASH_TEST
  3605. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  3606. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3607. #endif
  3608. }
  3609. #ifndef NO_LARGE_HASH_TEST
  3610. /* BEGIN LARGE HASH TEST */ {
  3611. byte large_input[1024];
  3612. const char* large_digest =
  3613. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3614. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3615. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3616. for (i = 0; i < (int)sizeof(large_input); i++) {
  3617. large_input[i] = (byte)(i & 0xFF);
  3618. }
  3619. times = 100;
  3620. for (i = 0; i < times; ++i) {
  3621. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3622. (word32)sizeof(large_input));
  3623. if (ret != 0)
  3624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3625. }
  3626. ret = wc_Sha3_384_Final(&sha, hash);
  3627. if (ret != 0)
  3628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3629. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3630. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3631. } /* END LARGE HASH TEST */
  3632. #endif /* NO_LARGE_HASH_TEST */
  3633. exit:
  3634. wc_Sha3_384_Free(&sha);
  3635. return ret;
  3636. }
  3637. #endif /* WOLFSSL_NOSHA3_384 */
  3638. #ifndef WOLFSSL_NOSHA3_512
  3639. static wc_test_ret_t sha3_512_test(void)
  3640. {
  3641. wc_Sha3 sha;
  3642. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3643. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3644. testVector a, b, c;
  3645. testVector test_sha[3];
  3646. wc_test_ret_t ret;
  3647. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3648. /*
  3649. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  3650. */
  3651. a.input = "";
  3652. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3653. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3654. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3655. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3656. "\x28\x1d\xcd\x26";
  3657. a.inLen = XSTRLEN(a.input);
  3658. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3659. b.input = "abc";
  3660. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3661. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3662. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3663. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3664. "\x4e\xec\x53\xf0";
  3665. b.inLen = XSTRLEN(b.input);
  3666. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3667. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3668. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3669. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3670. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3671. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3672. "\x39\xd6\xe7\x5e";
  3673. c.inLen = XSTRLEN(c.input);
  3674. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3675. test_sha[0] = a;
  3676. test_sha[1] = b;
  3677. test_sha[2] = c;
  3678. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3679. if (ret != 0)
  3680. return WC_TEST_RET_ENC_EC(ret);
  3681. for (i = 0; i < times; ++i) {
  3682. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3683. (word32)test_sha[i].inLen);
  3684. if (ret != 0)
  3685. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3686. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3687. if (ret != 0)
  3688. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3689. ret = wc_Sha3_512_Final(&sha, hash);
  3690. if (ret != 0)
  3691. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3692. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3693. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3694. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3695. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3696. }
  3697. #ifndef NO_LARGE_HASH_TEST
  3698. /* BEGIN LARGE HASH TEST */ {
  3699. byte large_input[1024];
  3700. const char* large_digest =
  3701. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3702. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3703. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3704. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3705. for (i = 0; i < (int)sizeof(large_input); i++) {
  3706. large_input[i] = (byte)(i & 0xFF);
  3707. }
  3708. times = 100;
  3709. for (i = 0; i < times; ++i) {
  3710. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3711. (word32)sizeof(large_input));
  3712. if (ret != 0)
  3713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3714. }
  3715. ret = wc_Sha3_512_Final(&sha, hash);
  3716. if (ret != 0)
  3717. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3718. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3719. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3720. } /* END LARGE HASH TEST */
  3721. #endif /* NO_LARGE_HASH_TEST */
  3722. exit:
  3723. wc_Sha3_512_Free(&sha);
  3724. return ret;
  3725. }
  3726. #endif /* WOLFSSL_NOSHA3_512 */
  3727. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  3728. {
  3729. wc_test_ret_t ret;
  3730. (void)ret;
  3731. #ifndef WOLFSSL_NOSHA3_224
  3732. if ((ret = sha3_224_test()) != 0)
  3733. return ret;
  3734. #endif
  3735. #ifndef WOLFSSL_NOSHA3_256
  3736. if ((ret = sha3_256_test()) != 0)
  3737. return ret;
  3738. #endif
  3739. #ifndef WOLFSSL_NOSHA3_384
  3740. if ((ret = sha3_384_test()) != 0)
  3741. return ret;
  3742. #endif
  3743. #ifndef WOLFSSL_NOSHA3_512
  3744. if ((ret = sha3_512_test()) != 0)
  3745. return ret;
  3746. #endif
  3747. return 0;
  3748. }
  3749. #endif /* WOLFSSL_SHA3 */
  3750. #ifdef WOLFSSL_SHAKE128
  3751. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3752. size_t large_input_buf_size)
  3753. {
  3754. testVector a, b, c, d, e;
  3755. testVector test_sha[5];
  3756. wc_test_ret_t ret = 0;
  3757. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3758. static const char large_digest[] =
  3759. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3760. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3761. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3762. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3763. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3764. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3765. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3766. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3767. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3768. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3769. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3770. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3771. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3772. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3773. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3774. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3775. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3776. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3777. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3778. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3779. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3780. byte hash[sizeof(large_digest) - 1];
  3781. /*
  3782. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3783. */
  3784. a.input = "";
  3785. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3786. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3787. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3788. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3789. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3790. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3791. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3792. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3793. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3794. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3795. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3796. "\xb0\xfc\xa9";
  3797. a.inLen = XSTRLEN(a.input);
  3798. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3799. b.input = "abc";
  3800. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3801. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3802. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3803. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3804. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3805. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3806. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3807. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3808. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3809. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3810. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3811. "\x47\x58\x4e";
  3812. b.inLen = XSTRLEN(b.input);
  3813. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3814. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3815. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3816. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3817. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3818. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3819. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3820. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3821. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3822. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3823. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3824. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3825. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3826. "\xb4\xc9\x54";
  3827. c.inLen = XSTRLEN(c.input);
  3828. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3829. /* Taken from NIST CAVP test vectors - full rate output. */
  3830. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3831. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3832. "\x85\xe0";
  3833. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3834. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3835. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3836. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3837. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3838. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3839. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3840. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3841. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3842. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3843. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3844. "\x55\x4d\x09";
  3845. d.inLen = 32;
  3846. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3847. /* Taken from NIST CAVP test vectors - more than one output block. */
  3848. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3849. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3850. "\x47\xe4";
  3851. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3852. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3853. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3854. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3855. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3856. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3857. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3858. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3859. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3860. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3861. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3862. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3863. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3864. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3865. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3866. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3867. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3868. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3869. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3870. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3871. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3872. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3873. "\xd9\xe9\xe6\xf9\xff\xa5";
  3874. e.inLen = 32;
  3875. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3876. test_sha[0] = a;
  3877. test_sha[1] = b;
  3878. test_sha[2] = c;
  3879. test_sha[3] = d;
  3880. test_sha[4] = e;
  3881. for (i = 0; i < times; ++i) {
  3882. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3883. if (ret != 0)
  3884. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3885. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3886. (word32)test_sha[i].inLen);
  3887. if (ret != 0)
  3888. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3889. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3890. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3891. if (ret != 0)
  3892. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3893. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3894. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3895. }
  3896. #ifndef NO_LARGE_HASH_TEST
  3897. /* BEGIN LARGE HASH TEST */ {
  3898. for (i = 0; i < (int)large_input_buf_size; i++) {
  3899. large_input_buf[i] = (byte)(i & 0xFF);
  3900. }
  3901. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3902. if (ret != 0)
  3903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3904. /* Absorb is non-incremental. */
  3905. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  3906. (word32)large_input_buf_size);
  3907. if (ret != 0)
  3908. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3909. /* Able to squeeze out blocks incrementally. */
  3910. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3911. if (ret != 0)
  3912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3913. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3914. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3915. if (ret != 0)
  3916. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3917. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3918. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3919. } /* END LARGE HASH TEST */
  3920. #endif /* NO_LARGE_HASH_TEST */
  3921. exit:
  3922. return ret;
  3923. }
  3924. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  3925. {
  3926. wc_Shake sha;
  3927. byte hash[250];
  3928. testVector a, b, c, d, e;
  3929. testVector test_sha[5];
  3930. wc_test_ret_t ret = 0;
  3931. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3932. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  3933. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3934. byte *large_input = NULL;
  3935. #else
  3936. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  3937. #endif
  3938. static const char large_digest[] =
  3939. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3940. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3941. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3942. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3943. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3944. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3945. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3946. "\xfa\x1b";
  3947. /*
  3948. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3949. */
  3950. a.input = "";
  3951. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3952. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3953. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3954. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3955. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3956. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3957. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3958. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3959. a.inLen = XSTRLEN(a.input);
  3960. a.outLen = 114;
  3961. b.input = "abc";
  3962. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3963. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3964. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3965. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3966. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3967. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3968. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3969. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3970. b.inLen = XSTRLEN(b.input);
  3971. b.outLen = 114;
  3972. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3973. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3974. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3975. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3976. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3977. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3978. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3979. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3980. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3981. c.inLen = XSTRLEN(c.input);
  3982. c.outLen = 114;
  3983. /* Taken from NIST CAVP test vectors - full rate output. */
  3984. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3985. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3986. "\x85\xe0";
  3987. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3988. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3989. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3990. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3991. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3992. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3993. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3994. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3995. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3996. "\xa1";
  3997. d.inLen = 32;
  3998. d.outLen = 136;
  3999. /* Taken from NIST CAVP test vectors - more than one output block. */
  4000. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4001. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4002. "\x47\xe4";
  4003. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4004. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4005. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4006. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4007. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4008. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4009. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4010. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4011. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4012. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4013. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4014. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4015. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4016. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4017. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4018. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4019. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4020. e.inLen = 32;
  4021. e.outLen = 250;
  4022. test_sha[0] = a;
  4023. test_sha[1] = b;
  4024. test_sha[2] = c;
  4025. test_sha[3] = d;
  4026. test_sha[4] = e;
  4027. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4028. if (ret != 0)
  4029. return WC_TEST_RET_ENC_EC(ret);
  4030. for (i = 0; i < times; ++i) {
  4031. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4032. (word32)test_sha[i].inLen);
  4033. if (ret != 0)
  4034. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4035. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4036. if (ret != 0)
  4037. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4038. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4039. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4040. }
  4041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4042. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4043. DYNAMIC_TYPE_TMP_BUFFER);
  4044. if (large_input == NULL)
  4045. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4046. #endif
  4047. #ifndef NO_LARGE_HASH_TEST
  4048. /* BEGIN LARGE HASH TEST */ {
  4049. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4050. large_input[i] = (byte)(i & 0xFF);
  4051. }
  4052. times = 100;
  4053. for (i = 0; i < times; ++i) {
  4054. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4055. SHAKE128_LARGE_INPUT_BUFSIZ);
  4056. if (ret != 0)
  4057. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4058. }
  4059. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4060. if (ret != 0)
  4061. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4062. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4063. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4064. } /* END LARGE HASH TEST */
  4065. #endif /* NO_LARGE_HASH_TEST */
  4066. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4067. exit:
  4068. wc_Shake128_Free(&sha);
  4069. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4070. if (large_input != NULL)
  4071. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4072. #endif
  4073. return ret;
  4074. }
  4075. #endif
  4076. #ifdef WOLFSSL_SHAKE256
  4077. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4078. size_t large_input_buf_size)
  4079. {
  4080. testVector a, b, c, d, e;
  4081. testVector test_sha[5];
  4082. wc_test_ret_t ret = 0;
  4083. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4084. static const char large_digest[] =
  4085. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4086. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4087. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4088. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4089. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4090. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4091. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4092. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4093. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4094. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4095. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4096. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4097. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4098. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4099. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4100. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4101. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4102. byte hash[sizeof(large_digest) - 1];
  4103. /*
  4104. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4105. */
  4106. a.input = "";
  4107. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4108. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4109. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4110. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4111. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4112. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4113. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4114. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4115. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4116. "\xdd";
  4117. a.inLen = XSTRLEN(a.input);
  4118. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4119. b.input = "abc";
  4120. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4121. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4122. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4123. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4124. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4125. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4126. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4127. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4128. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4129. "\x29";
  4130. b.inLen = XSTRLEN(b.input);
  4131. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4132. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4133. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4134. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4135. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4136. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4137. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4138. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4139. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4140. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4141. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4142. "\x8d";
  4143. c.inLen = XSTRLEN(c.input);
  4144. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4145. /* Taken from NIST CAVP test vectors - full rate output. */
  4146. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4147. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4148. "\x85\xe0";
  4149. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4150. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4151. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4152. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4153. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4154. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4155. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4156. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4157. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4158. "\xc2";
  4159. d.inLen = 32;
  4160. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4161. /* Taken from NIST CAVP test vectors - more than one output block. */
  4162. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4163. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4164. "\x47\xe4";
  4165. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4166. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4167. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4168. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4169. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4170. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4171. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4172. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4173. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4174. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4175. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4176. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4177. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4178. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4179. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4180. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4181. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4182. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4183. "\x68\x44";
  4184. e.inLen = 32;
  4185. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4186. test_sha[0] = a;
  4187. test_sha[1] = b;
  4188. test_sha[2] = c;
  4189. test_sha[3] = d;
  4190. test_sha[4] = e;
  4191. for (i = 0; i < times; ++i) {
  4192. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4193. if (ret != 0)
  4194. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4195. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4196. (word32)test_sha[i].inLen);
  4197. if (ret != 0)
  4198. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4199. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4200. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4201. if (ret != 0)
  4202. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4203. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4204. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4205. }
  4206. #ifndef NO_LARGE_HASH_TEST
  4207. /* BEGIN LARGE HASH TEST */ {
  4208. for (i = 0; i < (int)large_input_buf_size; i++) {
  4209. large_input_buf[i] = (byte)(i & 0xFF);
  4210. }
  4211. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4212. if (ret != 0)
  4213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4214. /* Absorb is non-incremental. */
  4215. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4216. (word32)large_input_buf_size);
  4217. if (ret != 0)
  4218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4219. /* Able to squeeze out blocks incrementally. */
  4220. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4221. if (ret != 0)
  4222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4223. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4224. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4225. if (ret != 0)
  4226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4227. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4228. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4229. } /* END LARGE HASH TEST */
  4230. #endif /* NO_LARGE_HASH_TEST */
  4231. exit:
  4232. return ret;
  4233. }
  4234. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4235. {
  4236. wc_Shake sha;
  4237. byte hash[250];
  4238. testVector a, b, c, d, e;
  4239. testVector test_sha[5];
  4240. wc_test_ret_t ret = 0;
  4241. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4242. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4243. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4244. byte *large_input = NULL;
  4245. #else
  4246. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4247. #endif
  4248. const char* large_digest =
  4249. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4250. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4251. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4252. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4253. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4254. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4255. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4256. "\xea\x26";
  4257. /*
  4258. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4259. */
  4260. a.input = "";
  4261. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4262. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4263. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4264. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4265. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4266. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4267. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4268. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4269. a.inLen = XSTRLEN(a.input);
  4270. a.outLen = 114;
  4271. b.input = "abc";
  4272. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4273. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4274. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4275. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4276. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4277. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4278. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4279. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4280. b.inLen = XSTRLEN(b.input);
  4281. b.outLen = 114;
  4282. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4283. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4284. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4285. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4286. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4287. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4288. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4289. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4290. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4291. c.inLen = XSTRLEN(c.input);
  4292. c.outLen = 114;
  4293. /* Taken from NIST CAVP test vectors - full rate output. */
  4294. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4295. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4296. "\x85\xe0";
  4297. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4298. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4299. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4300. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4301. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4302. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4303. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4304. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4305. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4306. "\xc2";
  4307. d.inLen = 32;
  4308. d.outLen = 136;
  4309. /* Taken from NIST CAVP test vectors - more than one output block. */
  4310. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4311. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4312. "\x47\xe4";
  4313. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4314. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4315. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4316. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4317. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4318. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4319. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4320. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4321. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4322. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4323. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4324. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4325. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4326. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4327. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4328. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4329. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  4330. e.inLen = 32;
  4331. e.outLen = 250;
  4332. test_sha[0] = a;
  4333. test_sha[1] = b;
  4334. test_sha[2] = c;
  4335. test_sha[3] = d;
  4336. test_sha[4] = e;
  4337. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  4338. if (ret != 0)
  4339. return WC_TEST_RET_ENC_EC(ret);
  4340. for (i = 0; i < times; ++i) {
  4341. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  4342. (word32)test_sha[i].inLen);
  4343. if (ret != 0)
  4344. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4345. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  4346. if (ret != 0)
  4347. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4348. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4349. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4350. }
  4351. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4352. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  4353. DYNAMIC_TYPE_TMP_BUFFER);
  4354. if (large_input == NULL)
  4355. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4356. #endif
  4357. #ifndef NO_LARGE_HASH_TEST
  4358. /* BEGIN LARGE HASH TEST */ {
  4359. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  4360. large_input[i] = (byte)(i & 0xFF);
  4361. }
  4362. times = 100;
  4363. for (i = 0; i < times; ++i) {
  4364. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  4365. SHAKE256_LARGE_INPUT_BUFSIZ);
  4366. if (ret != 0)
  4367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4368. }
  4369. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  4370. if (ret != 0)
  4371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4372. if (XMEMCMP(hash, large_digest, 114) != 0)
  4373. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4374. } /* END LARGE HASH TEST */
  4375. #endif /* NO_LARGE_HASH_TEST */
  4376. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  4377. exit:
  4378. wc_Shake256_Free(&sha);
  4379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4380. if (large_input != NULL)
  4381. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4382. #endif
  4383. return ret;
  4384. }
  4385. #endif
  4386. #ifdef WOLFSSL_SM3
  4387. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  4388. {
  4389. wc_Sm3 sm3, sm3Copy;
  4390. byte hash[WC_SM3_DIGEST_SIZE];
  4391. byte hashGet[WC_SM3_DIGEST_SIZE];
  4392. byte hashCopy[WC_SM3_DIGEST_SIZE];
  4393. wc_test_ret_t ret = 0;
  4394. testVector a, b, c;
  4395. testVector test_sm3[3];
  4396. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  4397. a.input = "";
  4398. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  4399. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  4400. "\xaa\x2b";
  4401. a.inLen = XSTRLEN(a.input);
  4402. a.outLen = WC_SM3_DIGEST_SIZE;
  4403. b.input = "abc";
  4404. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  4405. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  4406. "\xa8\xe0";
  4407. b.inLen = XSTRLEN(b.input);
  4408. b.outLen = WC_SM3_DIGEST_SIZE;
  4409. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4410. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  4411. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  4412. "\x8c\x05";
  4413. c.inLen = XSTRLEN(c.input);
  4414. c.outLen = WC_SM3_DIGEST_SIZE;
  4415. test_sm3[0] = a;
  4416. test_sm3[1] = b;
  4417. test_sm3[2] = c;
  4418. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  4419. if (ret != 0)
  4420. return WC_TEST_RET_ENC_EC(ret);
  4421. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  4422. if (ret != 0) {
  4423. wc_Sm3Free(&sm3);
  4424. return WC_TEST_RET_ENC_EC(ret);
  4425. }
  4426. /* Test all the KATs. */
  4427. for (i = 0; i < times; ++i) {
  4428. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  4429. (word32)test_sm3[i].inLen);
  4430. if (ret != 0) {
  4431. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4432. }
  4433. /* Get the final hash but leave ready for more updates. */
  4434. ret = wc_Sm3GetHash(&sm3, hashGet);
  4435. if (ret != 0)
  4436. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4437. /* Make a copy of the hash. */
  4438. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  4439. if (ret != 0)
  4440. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4441. /* Get the final hash with original. */
  4442. ret = wc_Sm3Final(&sm3, hash);
  4443. if (ret != 0)
  4444. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4445. /* Get the final hash with copy. */
  4446. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  4447. if (ret != 0)
  4448. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4449. /* Dispose of copy. */
  4450. wc_Sm3Free(&sm3Copy);
  4451. /* Check hashes match expected. */
  4452. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  4453. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4454. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  4455. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4456. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  4457. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4458. }
  4459. #ifndef NO_LARGE_HASH_TEST
  4460. {
  4461. word32 sz;
  4462. byte large_input[1024];
  4463. #ifdef HASH_SIZE_LIMIT
  4464. const char* large_digest =
  4465. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  4466. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  4467. times = 20;
  4468. #else
  4469. const char* large_digest =
  4470. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  4471. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  4472. times = 100;
  4473. #endif
  4474. /* Set large input to something. */
  4475. for (i = 0; i < (int)sizeof(large_input); i++) {
  4476. large_input[i] = (byte)(i & 0xFF);
  4477. }
  4478. /* Hash a large number of times. */
  4479. for (i = 0; i < times; ++i) {
  4480. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  4481. (word32)sizeof(large_input));
  4482. if (ret != 0)
  4483. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4484. }
  4485. /* Calculate hash and compare to expected. */
  4486. ret = wc_Sm3Final(&sm3, hash);
  4487. if (ret != 0)
  4488. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4489. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4490. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4491. /* Check updating with various sizes works. */
  4492. for (sz = 1; sz <= 64; sz++) {
  4493. /* Hash a large number of times. */
  4494. for (i = 0; i < times; ++i) {
  4495. word32 o;
  4496. /* Update sz bytes at a time from large input buffer. */
  4497. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  4498. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  4499. if (ret != 0)
  4500. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  4501. }
  4502. /* Check for left-overs. */
  4503. if (o < (word32)sizeof(large_input)) {
  4504. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  4505. (word32)sizeof(large_input) - o);
  4506. if (ret != 0)
  4507. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4508. }
  4509. }
  4510. /* Calculate hash and compare to expected. */
  4511. ret = wc_Sm3Final(&sm3, hash);
  4512. if (ret != 0)
  4513. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4514. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4515. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4516. }
  4517. }
  4518. #endif /* NO_LARGE_HASH_TEST */
  4519. exit:
  4520. wc_Sm3Free(&sm3);
  4521. wc_Sm3Free(&sm3Copy);
  4522. return ret;
  4523. }
  4524. #endif
  4525. #ifndef NO_HASH_WRAPPER
  4526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  4527. {
  4528. wc_HashAlg hash;
  4529. int ret, exp_ret;
  4530. int i, j;
  4531. int digestSz;
  4532. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  4533. byte out[WC_MAX_DIGEST_SIZE];
  4534. byte hashOut[WC_MAX_DIGEST_SIZE];
  4535. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4536. enum wc_HashType hashType;
  4537. #endif
  4538. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  4539. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  4540. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  4541. WC_HASH_TYPE_SHA3_224,
  4542. WC_HASH_TYPE_SHA3_256,
  4543. WC_HASH_TYPE_SHA3_384,
  4544. WC_HASH_TYPE_SHA3_512 };
  4545. enum wc_HashType typesNoImpl[] = {
  4546. #ifdef NO_MD5
  4547. WC_HASH_TYPE_MD5,
  4548. #endif
  4549. #ifdef NO_SHA
  4550. WC_HASH_TYPE_SHA,
  4551. #endif
  4552. #ifndef WOLFSSL_SHA224
  4553. WC_HASH_TYPE_SHA224,
  4554. #endif
  4555. #ifdef NO_SHA256
  4556. WC_HASH_TYPE_SHA256,
  4557. #endif
  4558. #ifndef WOLFSSL_SHA384
  4559. WC_HASH_TYPE_SHA384,
  4560. #endif
  4561. #ifndef WOLFSSL_SHA512
  4562. WC_HASH_TYPE_SHA512,
  4563. #endif
  4564. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  4565. WC_HASH_TYPE_SHA3_224,
  4566. #endif
  4567. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  4568. WC_HASH_TYPE_SHA3_256,
  4569. #endif
  4570. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  4571. WC_HASH_TYPE_SHA3_384,
  4572. #endif
  4573. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  4574. WC_HASH_TYPE_SHA3_512,
  4575. #endif
  4576. WC_HASH_TYPE_NONE
  4577. };
  4578. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  4579. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  4580. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  4581. WC_HASH_TYPE_BLAKE2B,
  4582. WC_HASH_TYPE_NONE };
  4583. /* Parameter Validation testing. */
  4584. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  4585. if (ret != BAD_FUNC_ARG)
  4586. return WC_TEST_RET_ENC_EC(ret);
  4587. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4588. if (ret != BAD_FUNC_ARG)
  4589. return WC_TEST_RET_ENC_EC(ret);
  4590. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4591. if (ret != BAD_FUNC_ARG)
  4592. return WC_TEST_RET_ENC_EC(ret);
  4593. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  4594. if (ret != BAD_FUNC_ARG)
  4595. return WC_TEST_RET_ENC_EC(ret);
  4596. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  4597. if (ret != BAD_FUNC_ARG)
  4598. return WC_TEST_RET_ENC_EC(ret);
  4599. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  4600. if (ret != BAD_FUNC_ARG)
  4601. return WC_TEST_RET_ENC_EC(ret);
  4602. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  4603. if (ret != BAD_FUNC_ARG)
  4604. return WC_TEST_RET_ENC_EC(ret);
  4605. /* Try invalid hash algorithms. */
  4606. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  4607. ret = wc_HashInit(&hash, typesBad[i]);
  4608. if (ret != BAD_FUNC_ARG)
  4609. return WC_TEST_RET_ENC_I(i);
  4610. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  4611. if (ret != BAD_FUNC_ARG)
  4612. return WC_TEST_RET_ENC_I(i);
  4613. ret = wc_HashFinal(&hash, typesBad[i], out);
  4614. if (ret != BAD_FUNC_ARG)
  4615. return WC_TEST_RET_ENC_I(i);
  4616. wc_HashFree(&hash, typesBad[i]);
  4617. }
  4618. /* Try valid hash algorithms. */
  4619. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  4620. exp_ret = 0;
  4621. if (typesGood[i] == typesNoImpl[j]) {
  4622. /* Recognized but no implementation compiled in. */
  4623. exp_ret = HASH_TYPE_E;
  4624. j++;
  4625. }
  4626. ret = wc_HashInit(&hash, typesGood[i]);
  4627. if (ret != exp_ret)
  4628. return WC_TEST_RET_ENC_I(i);
  4629. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  4630. if (ret != exp_ret)
  4631. return WC_TEST_RET_ENC_I(i);
  4632. ret = wc_HashFinal(&hash, typesGood[i], out);
  4633. if (ret != exp_ret)
  4634. return WC_TEST_RET_ENC_I(i);
  4635. wc_HashFree(&hash, typesGood[i]);
  4636. digestSz = wc_HashGetDigestSize(typesGood[i]);
  4637. if (exp_ret < 0 && digestSz != exp_ret)
  4638. return WC_TEST_RET_ENC_I(i);
  4639. if (exp_ret == 0 && digestSz < 0)
  4640. return WC_TEST_RET_ENC_I(i);
  4641. if (exp_ret == 0) {
  4642. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  4643. digestSz - 1);
  4644. if (ret != BUFFER_E)
  4645. return WC_TEST_RET_ENC_I(i);
  4646. }
  4647. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  4648. if (ret != exp_ret)
  4649. return WC_TEST_RET_ENC_I(i);
  4650. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  4651. return WC_TEST_RET_ENC_I(i);
  4652. ret = wc_HashGetBlockSize(typesGood[i]);
  4653. if (exp_ret < 0 && ret != exp_ret)
  4654. return WC_TEST_RET_ENC_I(i);
  4655. if (exp_ret == 0 && ret < 0)
  4656. return WC_TEST_RET_ENC_I(i);
  4657. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4658. ret = wc_HashGetOID(typesGood[i]);
  4659. if (ret == BAD_FUNC_ARG ||
  4660. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  4661. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  4662. return WC_TEST_RET_ENC_I(i);
  4663. }
  4664. hashType = wc_OidGetHash(ret);
  4665. if (exp_ret == 0 && hashType != typesGood[i])
  4666. return WC_TEST_RET_ENC_I(i);
  4667. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4668. }
  4669. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  4670. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  4671. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  4672. return WC_TEST_RET_ENC_I(i);
  4673. }
  4674. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4675. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  4676. #ifdef WOLFSSL_MD2
  4677. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4678. return WC_TEST_RET_ENC_EC(ret);
  4679. #else
  4680. if (ret != HASH_TYPE_E)
  4681. return WC_TEST_RET_ENC_EC(ret);
  4682. #endif
  4683. hashType = wc_OidGetHash(646); /* Md2h */
  4684. #ifdef WOLFSSL_MD2
  4685. if (hashType != WC_HASH_TYPE_MD2)
  4686. return WC_TEST_RET_ENC_NC;
  4687. #else
  4688. if (hashType != WC_HASH_TYPE_NONE)
  4689. return WC_TEST_RET_ENC_NC;
  4690. #endif
  4691. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  4692. #ifndef NO_MD5
  4693. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4694. return WC_TEST_RET_ENC_EC(ret);
  4695. #else
  4696. if (ret != HASH_TYPE_E)
  4697. return WC_TEST_RET_ENC_EC(ret);
  4698. #endif
  4699. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  4700. if (ret != BAD_FUNC_ARG)
  4701. return WC_TEST_RET_ENC_EC(ret);
  4702. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  4703. if (ret != BAD_FUNC_ARG)
  4704. return WC_TEST_RET_ENC_EC(ret);
  4705. hashType = wc_OidGetHash(0);
  4706. if (hashType != WC_HASH_TYPE_NONE)
  4707. return WC_TEST_RET_ENC_NC;
  4708. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4709. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  4710. #ifdef WOLFSSL_MD2
  4711. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4712. return WC_TEST_RET_ENC_EC(ret);
  4713. #else
  4714. if (ret != HASH_TYPE_E)
  4715. return WC_TEST_RET_ENC_EC(ret);
  4716. #endif
  4717. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  4718. #ifdef WOLFSSL_MD2
  4719. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4720. return WC_TEST_RET_ENC_EC(ret);
  4721. #else
  4722. if (ret != HASH_TYPE_E)
  4723. return WC_TEST_RET_ENC_EC(ret);
  4724. #endif
  4725. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  4726. #ifndef NO_MD4
  4727. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4728. return WC_TEST_RET_ENC_EC(ret);
  4729. #else
  4730. if (ret != HASH_TYPE_E)
  4731. return WC_TEST_RET_ENC_EC(ret);
  4732. #endif
  4733. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  4734. #ifndef NO_MD4
  4735. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4736. return WC_TEST_RET_ENC_EC(ret);
  4737. #else
  4738. if (ret != HASH_TYPE_E)
  4739. return WC_TEST_RET_ENC_EC(ret);
  4740. #endif
  4741. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  4742. #if !defined(NO_MD5) && !defined(NO_SHA)
  4743. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4744. return WC_TEST_RET_ENC_EC(ret);
  4745. #else
  4746. if (ret != HASH_TYPE_E)
  4747. return WC_TEST_RET_ENC_EC(ret);
  4748. #endif
  4749. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  4750. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4751. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4752. return WC_TEST_RET_ENC_EC(ret);
  4753. #else
  4754. if (ret != HASH_TYPE_E)
  4755. return WC_TEST_RET_ENC_EC(ret);
  4756. #endif
  4757. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  4758. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4759. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4760. return WC_TEST_RET_ENC_EC(ret);
  4761. #else
  4762. if (ret != HASH_TYPE_E)
  4763. return WC_TEST_RET_ENC_EC(ret);
  4764. #endif
  4765. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  4766. if (ret != BAD_FUNC_ARG)
  4767. return WC_TEST_RET_ENC_EC(ret);
  4768. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  4769. if (ret != BAD_FUNC_ARG)
  4770. return WC_TEST_RET_ENC_EC(ret);
  4771. #if !defined(NO_CERTS) && !defined(NO_ASN)
  4772. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  4773. ret = wc_GetCTC_HashOID(MD2);
  4774. if (ret == 0)
  4775. return WC_TEST_RET_ENC_EC(ret);
  4776. #endif
  4777. #ifndef NO_MD5
  4778. ret = wc_GetCTC_HashOID(WC_MD5);
  4779. if (ret == 0)
  4780. return WC_TEST_RET_ENC_EC(ret);
  4781. #endif
  4782. #ifndef NO_SHA
  4783. ret = wc_GetCTC_HashOID(WC_SHA);
  4784. if (ret == 0)
  4785. return WC_TEST_RET_ENC_EC(ret);
  4786. #endif
  4787. #ifdef WOLFSSL_SHA224
  4788. ret = wc_GetCTC_HashOID(WC_SHA224);
  4789. if (ret == 0)
  4790. return WC_TEST_RET_ENC_EC(ret);
  4791. #endif
  4792. #ifndef NO_SHA256
  4793. ret = wc_GetCTC_HashOID(WC_SHA256);
  4794. if (ret == 0)
  4795. return WC_TEST_RET_ENC_EC(ret);
  4796. #endif
  4797. #ifdef WOLFSSL_SHA384
  4798. ret = wc_GetCTC_HashOID(WC_SHA384);
  4799. if (ret == 0)
  4800. return WC_TEST_RET_ENC_EC(ret);
  4801. #endif
  4802. #ifdef WOLFSSL_SHA512
  4803. ret = wc_GetCTC_HashOID(WC_SHA512);
  4804. if (ret == 0)
  4805. return WC_TEST_RET_ENC_EC(ret);
  4806. #endif
  4807. ret = wc_GetCTC_HashOID(-1);
  4808. if (ret != 0)
  4809. return WC_TEST_RET_ENC_EC(ret);
  4810. #endif
  4811. return 0;
  4812. }
  4813. #endif /* !NO_HASH_WRAPPER */
  4814. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4815. defined(HAVE_FIPS_VERSION) && \
  4816. (HAVE_FIPS_VERSION >= 5))
  4817. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  4818. {
  4819. Hmac hmac;
  4820. byte hash[WC_MD5_DIGEST_SIZE];
  4821. const char* keys[]=
  4822. {
  4823. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4824. "Jefe",
  4825. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4826. };
  4827. testVector a, b, c;
  4828. testVector test_hmac[3];
  4829. wc_test_ret_t ret;
  4830. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4831. a.input = "Hi There";
  4832. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4833. "\x9d";
  4834. a.inLen = XSTRLEN(a.input);
  4835. a.outLen = WC_MD5_DIGEST_SIZE;
  4836. b.input = "what do ya want for nothing?";
  4837. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4838. "\x38";
  4839. b.inLen = XSTRLEN(b.input);
  4840. b.outLen = WC_MD5_DIGEST_SIZE;
  4841. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4842. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4843. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4844. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4845. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4846. "\xf6";
  4847. c.inLen = XSTRLEN(c.input);
  4848. c.outLen = WC_MD5_DIGEST_SIZE;
  4849. test_hmac[0] = a;
  4850. test_hmac[1] = b;
  4851. test_hmac[2] = c;
  4852. for (i = 0; i < times; ++i) {
  4853. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4854. if (i == 1) {
  4855. continue; /* cavium can't handle short keys, fips not allowed */
  4856. }
  4857. #endif
  4858. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  4859. if (ret != 0)
  4860. return WC_TEST_RET_ENC_EC(ret);
  4861. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4862. (word32)XSTRLEN(keys[i]));
  4863. if (ret != 0)
  4864. return WC_TEST_RET_ENC_EC(ret);
  4865. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4866. (word32)test_hmac[i].inLen);
  4867. if (ret != 0)
  4868. return WC_TEST_RET_ENC_EC(ret);
  4869. ret = wc_HmacFinal(&hmac, hash);
  4870. if (ret != 0)
  4871. return WC_TEST_RET_ENC_EC(ret);
  4872. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4873. return WC_TEST_RET_ENC_I(i);
  4874. wc_HmacFree(&hmac);
  4875. }
  4876. #ifndef HAVE_FIPS
  4877. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  4878. return WC_TEST_RET_ENC_EC(ret);
  4879. #endif
  4880. return 0;
  4881. }
  4882. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4883. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4884. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  4885. {
  4886. Hmac hmac;
  4887. byte hash[WC_SHA_DIGEST_SIZE];
  4888. const char* keys[]=
  4889. {
  4890. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4891. "\x0b\x0b\x0b",
  4892. "Jefe",
  4893. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4894. "\xAA\xAA\xAA"
  4895. };
  4896. testVector a, b, c;
  4897. testVector test_hmac[3];
  4898. wc_test_ret_t ret;
  4899. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4900. a.input = "Hi There";
  4901. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4902. "\x8e\xf1\x46\xbe\x00";
  4903. a.inLen = XSTRLEN(a.input);
  4904. a.outLen = WC_SHA_DIGEST_SIZE;
  4905. b.input = "what do ya want for nothing?";
  4906. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4907. "\x9c\x25\x9a\x7c\x79";
  4908. b.inLen = XSTRLEN(b.input);
  4909. b.outLen = WC_SHA_DIGEST_SIZE;
  4910. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4911. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4912. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4913. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4914. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4915. "\x4f\x63\xf1\x75\xd3";
  4916. c.inLen = XSTRLEN(c.input);
  4917. c.outLen = WC_SHA_DIGEST_SIZE;
  4918. test_hmac[0] = a;
  4919. test_hmac[1] = b;
  4920. test_hmac[2] = c;
  4921. for (i = 0; i < times; ++i) {
  4922. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4923. if (i == 1)
  4924. continue; /* cavium can't handle short keys, fips not allowed */
  4925. #endif
  4926. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  4927. return WC_TEST_RET_ENC_EC(ret);
  4928. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4929. (word32)XSTRLEN(keys[i]));
  4930. if (ret != 0)
  4931. return WC_TEST_RET_ENC_EC(ret);
  4932. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4933. (word32)test_hmac[i].inLen);
  4934. if (ret != 0)
  4935. return WC_TEST_RET_ENC_EC(ret);
  4936. ret = wc_HmacFinal(&hmac, hash);
  4937. if (ret != 0)
  4938. return WC_TEST_RET_ENC_EC(ret);
  4939. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4940. return WC_TEST_RET_ENC_I(i);
  4941. wc_HmacFree(&hmac);
  4942. }
  4943. #ifndef HAVE_FIPS
  4944. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  4945. return WC_TEST_RET_ENC_EC(ret);
  4946. #endif
  4947. return 0;
  4948. }
  4949. #endif
  4950. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4951. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  4952. {
  4953. Hmac hmac;
  4954. byte hash[WC_SHA224_DIGEST_SIZE];
  4955. const char* keys[]=
  4956. {
  4957. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4958. "\x0b\x0b\x0b",
  4959. "Jefe",
  4960. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4961. "\xAA\xAA\xAA",
  4962. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4963. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4964. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4965. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4966. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4967. };
  4968. testVector a, b, c, d;
  4969. testVector test_hmac[4];
  4970. wc_test_ret_t ret;
  4971. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4972. a.input = "Hi There";
  4973. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4974. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4975. a.inLen = XSTRLEN(a.input);
  4976. a.outLen = WC_SHA224_DIGEST_SIZE;
  4977. b.input = "what do ya want for nothing?";
  4978. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4979. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4980. b.inLen = XSTRLEN(b.input);
  4981. b.outLen = WC_SHA224_DIGEST_SIZE;
  4982. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4983. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4984. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4985. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4986. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4987. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4988. c.inLen = XSTRLEN(c.input);
  4989. c.outLen = WC_SHA224_DIGEST_SIZE;
  4990. d.input = "Big Key Input";
  4991. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4992. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4993. d.inLen = XSTRLEN(d.input);
  4994. d.outLen = WC_SHA224_DIGEST_SIZE;
  4995. test_hmac[0] = a;
  4996. test_hmac[1] = b;
  4997. test_hmac[2] = c;
  4998. test_hmac[3] = d;
  4999. for (i = 0; i < times; ++i) {
  5000. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5001. if (i == 1)
  5002. continue; /* cavium can't handle short keys, fips not allowed */
  5003. #endif
  5004. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5005. return WC_TEST_RET_ENC_EC(ret);
  5006. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5007. (word32)XSTRLEN(keys[i]));
  5008. if (ret != 0)
  5009. return WC_TEST_RET_ENC_EC(ret);
  5010. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5011. (word32)test_hmac[i].inLen);
  5012. if (ret != 0)
  5013. return WC_TEST_RET_ENC_EC(ret);
  5014. ret = wc_HmacFinal(&hmac, hash);
  5015. if (ret != 0)
  5016. return WC_TEST_RET_ENC_EC(ret);
  5017. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5018. return WC_TEST_RET_ENC_I(i);
  5019. wc_HmacFree(&hmac);
  5020. }
  5021. #ifndef HAVE_FIPS
  5022. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5023. return WC_TEST_RET_ENC_EC(ret);
  5024. #endif
  5025. return 0;
  5026. }
  5027. #endif
  5028. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5029. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5030. {
  5031. Hmac hmac;
  5032. byte hash[WC_SHA256_DIGEST_SIZE];
  5033. const char* keys[]=
  5034. {
  5035. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5036. "\x0b\x0b\x0b",
  5037. "Jefe",
  5038. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5039. "\xAA\xAA\xAA",
  5040. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5041. "\xAA\xAA\xAA",
  5042. };
  5043. testVector a, b, c, d;
  5044. testVector test_hmac[4];
  5045. wc_test_ret_t ret;
  5046. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5047. a.input = "Hi There";
  5048. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5049. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5050. "\xcf\xf7";
  5051. a.inLen = XSTRLEN(a.input);
  5052. a.outLen = WC_SHA256_DIGEST_SIZE;
  5053. b.input = "what do ya want for nothing?";
  5054. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5055. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5056. "\x38\x43";
  5057. b.inLen = XSTRLEN(b.input);
  5058. b.outLen = WC_SHA256_DIGEST_SIZE;
  5059. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5060. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5061. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5062. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5063. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5064. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5065. "\x65\xfe";
  5066. c.inLen = XSTRLEN(c.input);
  5067. c.outLen = WC_SHA256_DIGEST_SIZE;
  5068. d.input = 0;
  5069. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5070. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5071. "\x3e\x46";
  5072. d.inLen = 0;
  5073. d.outLen = WC_SHA256_DIGEST_SIZE;
  5074. test_hmac[0] = a;
  5075. test_hmac[1] = b;
  5076. test_hmac[2] = c;
  5077. test_hmac[3] = d;
  5078. for (i = 0; i < times; ++i) {
  5079. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5080. if (i == 1)
  5081. continue; /* cavium can't handle short keys, fips not allowed */
  5082. #endif
  5083. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5084. if (i == 3)
  5085. continue; /* QuickAssist can't handle empty HMAC */
  5086. #endif
  5087. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5088. return WC_TEST_RET_ENC_I(i);
  5089. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5090. (word32)XSTRLEN(keys[i]));
  5091. if (ret != 0)
  5092. return WC_TEST_RET_ENC_I(i);
  5093. if (test_hmac[i].input != NULL) {
  5094. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5095. (word32)test_hmac[i].inLen);
  5096. if (ret != 0)
  5097. return WC_TEST_RET_ENC_I(i);
  5098. }
  5099. ret = wc_HmacFinal(&hmac, hash);
  5100. if (ret != 0)
  5101. return WC_TEST_RET_ENC_I(i);
  5102. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5103. return WC_TEST_RET_ENC_I(i);
  5104. wc_HmacFree(&hmac);
  5105. }
  5106. #ifndef HAVE_FIPS
  5107. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5108. return WC_TEST_RET_ENC_EC(ret);
  5109. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5110. return WC_TEST_RET_ENC_EC(ret);
  5111. #endif
  5112. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5113. return WC_TEST_RET_ENC_EC(ret);
  5114. return 0;
  5115. }
  5116. #endif
  5117. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5118. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5119. {
  5120. Hmac hmac;
  5121. byte hash[WC_SHA384_DIGEST_SIZE];
  5122. const char* keys[]=
  5123. {
  5124. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5125. "\x0b\x0b\x0b",
  5126. "Jefe",
  5127. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5128. "\xAA\xAA\xAA",
  5129. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5130. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5131. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5132. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5133. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5134. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5135. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5136. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5137. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5138. };
  5139. testVector a, b, c, d;
  5140. testVector test_hmac[4];
  5141. wc_test_ret_t ret;
  5142. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5143. a.input = "Hi There";
  5144. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5145. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5146. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5147. "\xfa\x9c\xb6";
  5148. a.inLen = XSTRLEN(a.input);
  5149. a.outLen = WC_SHA384_DIGEST_SIZE;
  5150. b.input = "what do ya want for nothing?";
  5151. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5152. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5153. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5154. "\xb2\x16\x49";
  5155. b.inLen = XSTRLEN(b.input);
  5156. b.outLen = WC_SHA384_DIGEST_SIZE;
  5157. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5158. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5159. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5160. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5161. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5162. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5163. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5164. "\xa3\x4f\x27";
  5165. c.inLen = XSTRLEN(c.input);
  5166. c.outLen = WC_SHA384_DIGEST_SIZE;
  5167. d.input = "Big Key Input";
  5168. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  5169. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  5170. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  5171. "\x57\x41\x69";
  5172. d.inLen = XSTRLEN(d.input);
  5173. d.outLen = WC_SHA384_DIGEST_SIZE;
  5174. test_hmac[0] = a;
  5175. test_hmac[1] = b;
  5176. test_hmac[2] = c;
  5177. test_hmac[3] = d;
  5178. for (i = 0; i < times; ++i) {
  5179. #if defined(HAVE_FIPS)
  5180. if (i == 1)
  5181. continue; /* fips not allowed */
  5182. #endif
  5183. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5184. return WC_TEST_RET_ENC_EC(ret);
  5185. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5186. (word32)XSTRLEN(keys[i]));
  5187. if (ret != 0)
  5188. return WC_TEST_RET_ENC_EC(ret);
  5189. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5190. (word32)test_hmac[i].inLen);
  5191. if (ret != 0)
  5192. return WC_TEST_RET_ENC_EC(ret);
  5193. ret = wc_HmacFinal(&hmac, hash);
  5194. if (ret != 0)
  5195. return WC_TEST_RET_ENC_EC(ret);
  5196. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5197. return WC_TEST_RET_ENC_I(i);
  5198. wc_HmacFree(&hmac);
  5199. }
  5200. #ifndef HAVE_FIPS
  5201. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5202. return WC_TEST_RET_ENC_EC(ret);
  5203. #endif
  5204. return 0;
  5205. }
  5206. #endif
  5207. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5208. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5209. {
  5210. Hmac hmac;
  5211. byte hash[WC_SHA512_DIGEST_SIZE];
  5212. const char* keys[]=
  5213. {
  5214. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5215. "\x0b\x0b\x0b",
  5216. "Jefe",
  5217. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5218. "\xAA\xAA\xAA",
  5219. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5220. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5221. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5222. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5223. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5224. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5225. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5226. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5227. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5228. };
  5229. testVector a, b, c, d;
  5230. testVector test_hmac[4];
  5231. wc_test_ret_t ret;
  5232. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5233. a.input = "Hi There";
  5234. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5235. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5236. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5237. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  5238. "\x3a\x12\x68\x54";
  5239. a.inLen = XSTRLEN(a.input);
  5240. a.outLen = WC_SHA512_DIGEST_SIZE;
  5241. b.input = "what do ya want for nothing?";
  5242. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  5243. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  5244. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  5245. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  5246. "\x38\xbc\xe7\x37";
  5247. b.inLen = XSTRLEN(b.input);
  5248. b.outLen = WC_SHA512_DIGEST_SIZE;
  5249. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5250. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5251. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5252. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5253. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  5254. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  5255. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  5256. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  5257. "\xe1\x32\x92\xfb";
  5258. c.inLen = XSTRLEN(c.input);
  5259. c.outLen = WC_SHA512_DIGEST_SIZE;
  5260. d.input = "Big Key Input";
  5261. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  5262. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  5263. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  5264. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  5265. "\x1e\x18\xfe\xfa";
  5266. d.inLen = XSTRLEN(d.input);
  5267. d.outLen = WC_SHA512_DIGEST_SIZE;
  5268. test_hmac[0] = a;
  5269. test_hmac[1] = b;
  5270. test_hmac[2] = c;
  5271. test_hmac[3] = d;
  5272. for (i = 0; i < times; ++i) {
  5273. #if defined(HAVE_FIPS)
  5274. if (i == 1)
  5275. continue; /* fips not allowed */
  5276. #endif
  5277. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5278. return WC_TEST_RET_ENC_EC(ret);
  5279. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  5280. (word32)XSTRLEN(keys[i]));
  5281. if (ret != 0)
  5282. return WC_TEST_RET_ENC_EC(ret);
  5283. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5284. (word32)test_hmac[i].inLen);
  5285. if (ret != 0)
  5286. return WC_TEST_RET_ENC_EC(ret);
  5287. ret = wc_HmacFinal(&hmac, hash);
  5288. if (ret != 0)
  5289. return WC_TEST_RET_ENC_EC(ret);
  5290. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  5291. return WC_TEST_RET_ENC_I(i);
  5292. wc_HmacFree(&hmac);
  5293. }
  5294. #ifndef HAVE_FIPS
  5295. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  5296. return WC_TEST_RET_ENC_EC(ret);
  5297. #endif
  5298. return 0;
  5299. }
  5300. #endif
  5301. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  5302. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  5303. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  5304. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  5305. {
  5306. Hmac hmac;
  5307. byte hash[WC_SHA3_512_DIGEST_SIZE];
  5308. const char* key[4] =
  5309. {
  5310. "Jefe",
  5311. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5312. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5313. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  5314. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  5315. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5316. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5317. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5318. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5319. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5320. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5321. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5322. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5323. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5324. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5325. };
  5326. const char* input[4] =
  5327. {
  5328. "what do ya want for nothing?",
  5329. "Hi There",
  5330. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5331. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5332. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5333. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5334. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  5335. "Big Key Input"
  5336. };
  5337. const int hashType[4] =
  5338. {
  5339. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  5340. };
  5341. const int hashSz[4] =
  5342. {
  5343. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  5344. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  5345. };
  5346. const char* output[16] =
  5347. {
  5348. /* key = jefe, input = what do ya want for nothing? */
  5349. /* HMAC-SHA3-224 */
  5350. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  5351. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  5352. /* HMAC-SHA3-256 */
  5353. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  5354. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  5355. /* HMAC-SHA3-384 */
  5356. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  5357. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  5358. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  5359. /* HMAC-SHA3-512 */
  5360. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  5361. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  5362. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  5363. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  5364. /* key = 0b..., input = Hi There */
  5365. /* HMAC-SHA3-224 */
  5366. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  5367. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  5368. /* HMAC-SHA3-256 */
  5369. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  5370. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  5371. /* HMAC-SHA3-384 */
  5372. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  5373. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  5374. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  5375. /* HMAC-SHA3-512 */
  5376. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  5377. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  5378. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  5379. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  5380. /* key = aa..., output = dd... */
  5381. /* HMAC-SHA3-224 */
  5382. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  5383. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  5384. /* HMAC-SHA3-256 */
  5385. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  5386. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  5387. /* HMAC-SHA3-384 */
  5388. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  5389. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  5390. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  5391. /* HMAC-SHA3-512 */
  5392. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  5393. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  5394. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  5395. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  5396. /* key = big key, input = Big Key Input */
  5397. /* HMAC-SHA3-224 */
  5398. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  5399. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  5400. /* HMAC-SHA3-256 */
  5401. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  5402. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  5403. /* HMAC-SHA3-384 */
  5404. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  5405. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  5406. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  5407. /* HMAC-SHA3-512 */
  5408. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  5409. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  5410. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  5411. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  5412. };
  5413. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  5414. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  5415. ret;
  5416. #ifdef HAVE_FIPS
  5417. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  5418. * short. Skip it in FIPS builds. */
  5419. i = 1;
  5420. #endif
  5421. for (; i < iMax; i++) {
  5422. for (j = 0; j < jMax; j++) {
  5423. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5424. return WC_TEST_RET_ENC_EC(ret);
  5425. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  5426. (word32)XSTRLEN(key[i]));
  5427. if (ret != 0)
  5428. return WC_TEST_RET_ENC_EC(ret);
  5429. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  5430. (word32)XSTRLEN(input[i]));
  5431. if (ret != 0)
  5432. return WC_TEST_RET_ENC_EC(ret);
  5433. ret = wc_HmacFinal(&hmac, hash);
  5434. if (ret != 0)
  5435. return WC_TEST_RET_ENC_EC(ret);
  5436. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  5437. return WC_TEST_RET_ENC_NC;
  5438. wc_HmacFree(&hmac);
  5439. if (i > 0)
  5440. continue;
  5441. #ifndef HAVE_FIPS
  5442. ret = wc_HmacSizeByType(hashType[j]);
  5443. if (ret != hashSz[j])
  5444. return WC_TEST_RET_ENC_EC(ret);
  5445. #endif
  5446. }
  5447. }
  5448. return 0;
  5449. }
  5450. #endif
  5451. #ifdef WC_RC2
  5452. typedef struct rc2TestVector {
  5453. const char* input;
  5454. const char* output;
  5455. const char* key; /* Key, variable up to 128 bytes */
  5456. const char* iv; /* IV, 8-bytes */
  5457. int inLen;
  5458. int outLen;
  5459. int keyLen;
  5460. int effectiveKeyBits; /* Up to 1024 bits supported */
  5461. } rc2TestVector;
  5462. static wc_test_ret_t rc2_ecb_test(void)
  5463. {
  5464. wc_test_ret_t ret = 0;
  5465. byte cipher[RC2_BLOCK_SIZE];
  5466. byte plain[RC2_BLOCK_SIZE];
  5467. rc2TestVector a, b, c, d, e, f, g, h;
  5468. rc2TestVector test_rc2[8];
  5469. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  5470. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5471. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  5472. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5473. a.inLen = RC2_BLOCK_SIZE;
  5474. a.outLen = RC2_BLOCK_SIZE;
  5475. a.keyLen = 8;
  5476. a.effectiveKeyBits = 63;
  5477. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5478. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  5479. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5480. b.inLen = RC2_BLOCK_SIZE;
  5481. b.outLen = RC2_BLOCK_SIZE;
  5482. b.keyLen = 8;
  5483. b.effectiveKeyBits = 64;
  5484. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  5485. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  5486. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5487. c.inLen = RC2_BLOCK_SIZE;
  5488. c.outLen = RC2_BLOCK_SIZE;
  5489. c.keyLen = 8;
  5490. c.effectiveKeyBits = 64;
  5491. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5492. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  5493. d.key = "\x88";
  5494. d.inLen = RC2_BLOCK_SIZE;
  5495. d.outLen = RC2_BLOCK_SIZE;
  5496. d.keyLen = 1;
  5497. d.effectiveKeyBits = 64;
  5498. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5499. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  5500. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5501. e.inLen = RC2_BLOCK_SIZE;
  5502. e.outLen = RC2_BLOCK_SIZE;
  5503. e.keyLen = 7;
  5504. e.effectiveKeyBits = 64;
  5505. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5506. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  5507. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5508. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5509. f.inLen = RC2_BLOCK_SIZE;
  5510. f.outLen = RC2_BLOCK_SIZE;
  5511. f.keyLen = 16;
  5512. f.effectiveKeyBits = 64;
  5513. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5514. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  5515. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5516. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5517. g.inLen = RC2_BLOCK_SIZE;
  5518. g.outLen = RC2_BLOCK_SIZE;
  5519. g.keyLen = 16;
  5520. g.effectiveKeyBits = 128;
  5521. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5522. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  5523. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5524. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5525. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5526. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5527. "\x1e";
  5528. h.inLen = RC2_BLOCK_SIZE;
  5529. h.outLen = RC2_BLOCK_SIZE;
  5530. h.keyLen = 33;
  5531. h.effectiveKeyBits = 129;
  5532. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  5533. test_rc2[0] = a;
  5534. test_rc2[1] = b;
  5535. test_rc2[2] = c;
  5536. test_rc2[3] = d;
  5537. test_rc2[4] = e;
  5538. test_rc2[5] = f;
  5539. test_rc2[6] = g;
  5540. test_rc2[7] = h;
  5541. for (i = 0; i < times; ++i) {
  5542. Rc2 enc;
  5543. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  5544. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  5545. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  5546. NULL, test_rc2[i].effectiveKeyBits);
  5547. if (ret != 0) {
  5548. return WC_TEST_RET_ENC_EC(ret);
  5549. }
  5550. /* ECB encrypt */
  5551. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  5552. (word32)test_rc2[i].outLen);
  5553. if (ret != 0) {
  5554. return WC_TEST_RET_ENC_EC(ret);
  5555. }
  5556. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  5557. return WC_TEST_RET_ENC_NC;
  5558. }
  5559. /* ECB decrypt */
  5560. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  5561. if (ret != 0) {
  5562. return WC_TEST_RET_ENC_EC(ret);
  5563. }
  5564. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  5565. return WC_TEST_RET_ENC_NC;
  5566. }
  5567. }
  5568. return 0;
  5569. }
  5570. static wc_test_ret_t rc2_cbc_test(void)
  5571. {
  5572. wc_test_ret_t ret = 0;
  5573. byte cipher[128];
  5574. byte plain[128];
  5575. rc2TestVector a, b, c, d, e, f, g, h, i;
  5576. rc2TestVector test_rc2[9];
  5577. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  5578. /* key length = 7, effective key bits = 63 */
  5579. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5580. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5581. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  5582. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  5583. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5584. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5585. a.inLen = RC2_BLOCK_SIZE*2;
  5586. a.outLen = RC2_BLOCK_SIZE*2;
  5587. a.keyLen = 8;
  5588. a.effectiveKeyBits = 63;
  5589. /* key length = 8, effective key bits = 64, all 0xFF */
  5590. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  5591. "\xff\xff\xff\xff\xff\xff\xff\xff";
  5592. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  5593. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  5594. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5595. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5596. b.inLen = RC2_BLOCK_SIZE*2;
  5597. b.outLen = RC2_BLOCK_SIZE*2;
  5598. b.keyLen = 8;
  5599. b.effectiveKeyBits = 64;
  5600. /* key length = 8, effective key bits = 64 */
  5601. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  5602. "\x10\x00\x00\x00\x00\x00\x00\x01";
  5603. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  5604. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  5605. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5606. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5607. c.inLen = RC2_BLOCK_SIZE*2;
  5608. c.outLen = RC2_BLOCK_SIZE*2;
  5609. c.keyLen = 8;
  5610. c.effectiveKeyBits = 64;
  5611. /* key length = 1, effective key bits = 64 */
  5612. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5613. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5614. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  5615. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  5616. d.key = "\x88";
  5617. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5618. d.inLen = RC2_BLOCK_SIZE*2;
  5619. d.outLen = RC2_BLOCK_SIZE*2;
  5620. d.keyLen = 1;
  5621. d.effectiveKeyBits = 64;
  5622. /* key length = 7, effective key bits = 64 */
  5623. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5624. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5625. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  5626. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  5627. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5628. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5629. e.inLen = RC2_BLOCK_SIZE*2;
  5630. e.outLen = RC2_BLOCK_SIZE*2;
  5631. e.keyLen = 7;
  5632. e.effectiveKeyBits = 64;
  5633. /* key length = 16, effective key bits = 64 */
  5634. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5635. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5636. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  5637. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  5638. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5639. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5640. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5641. f.inLen = RC2_BLOCK_SIZE*2;
  5642. f.outLen = RC2_BLOCK_SIZE*2;
  5643. f.keyLen = 16;
  5644. f.effectiveKeyBits = 64;
  5645. /* key length = 16, effective bits = 128 */
  5646. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5647. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5648. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  5649. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  5650. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5651. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5652. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5653. g.inLen = RC2_BLOCK_SIZE*2;
  5654. g.outLen = RC2_BLOCK_SIZE*2;
  5655. g.keyLen = 16;
  5656. g.effectiveKeyBits = 128;
  5657. /* key length = 33, effective bits = 129 */
  5658. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5659. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5660. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  5661. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  5662. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5663. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5664. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5665. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5666. "\x1e";
  5667. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5668. h.inLen = RC2_BLOCK_SIZE*2;
  5669. h.outLen = RC2_BLOCK_SIZE*2;
  5670. h.keyLen = 33;
  5671. h.effectiveKeyBits = 129;
  5672. /* key length = 10, effective bits = 40 */
  5673. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  5674. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  5675. "\x11\x22\x33\x44\x55\x66\x77\x88"
  5676. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  5677. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  5678. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  5679. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  5680. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  5681. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  5682. "\x3E\x96";
  5683. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  5684. i.inLen = RC2_BLOCK_SIZE*4;
  5685. i.outLen = RC2_BLOCK_SIZE*4;
  5686. i.keyLen = 10;
  5687. i.effectiveKeyBits = 40;
  5688. test_rc2[0] = a;
  5689. test_rc2[1] = b;
  5690. test_rc2[2] = c;
  5691. test_rc2[3] = d;
  5692. test_rc2[4] = e;
  5693. test_rc2[5] = f;
  5694. test_rc2[6] = g;
  5695. test_rc2[7] = h;
  5696. test_rc2[8] = i;
  5697. for (j = 0; j < times; ++j) {
  5698. Rc2 rc2;
  5699. XMEMSET(cipher, 0, sizeof(cipher));
  5700. XMEMSET(plain, 0, sizeof(plain));
  5701. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  5702. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  5703. if (ret != 0) {
  5704. return WC_TEST_RET_ENC_EC(ret);
  5705. }
  5706. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  5707. test_rc2[j].inLen);
  5708. if (ret != 0) {
  5709. return WC_TEST_RET_ENC_EC(ret);
  5710. }
  5711. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  5712. return WC_TEST_RET_ENC_NC;
  5713. }
  5714. /* reset IV for decrypt, since overridden by encrypt operation */
  5715. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  5716. if (ret != 0) {
  5717. return WC_TEST_RET_ENC_EC(ret);
  5718. }
  5719. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  5720. if (ret != 0) {
  5721. return WC_TEST_RET_ENC_EC(ret);
  5722. }
  5723. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  5724. return WC_TEST_RET_ENC_NC;
  5725. }
  5726. }
  5727. return 0;
  5728. }
  5729. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  5730. {
  5731. wc_test_ret_t ret = 0;
  5732. ret = rc2_ecb_test();
  5733. if (ret != 0) {
  5734. return ret;
  5735. }
  5736. return rc2_cbc_test();
  5737. }
  5738. #endif
  5739. #ifndef NO_RC4
  5740. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  5741. {
  5742. byte cipher[16];
  5743. byte plain[16];
  5744. wc_test_ret_t ret;
  5745. const char* keys[] =
  5746. {
  5747. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5748. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5749. "\x00\x00\x00\x00\x00\x00\x00\x00",
  5750. "\xef\x01\x23\x45"
  5751. };
  5752. testVector a, b, c, d;
  5753. testVector test_arc4[4];
  5754. int times = sizeof(test_arc4) / sizeof(testVector), i;
  5755. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  5756. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  5757. a.inLen = 8;
  5758. a.outLen = 8;
  5759. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5760. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  5761. b.inLen = 8;
  5762. b.outLen = 8;
  5763. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5764. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  5765. c.inLen = 8;
  5766. c.outLen = 8;
  5767. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  5768. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  5769. d.inLen = 10;
  5770. d.outLen = 10;
  5771. test_arc4[0] = a;
  5772. test_arc4[1] = b;
  5773. test_arc4[2] = c;
  5774. test_arc4[3] = d;
  5775. for (i = 0; i < times; ++i) {
  5776. Arc4 enc;
  5777. Arc4 dec;
  5778. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  5779. if (i == 3)
  5780. keylen = 4;
  5781. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  5782. if (ret != 0)
  5783. return WC_TEST_RET_ENC_EC(ret);
  5784. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  5785. if (ret != 0)
  5786. return WC_TEST_RET_ENC_EC(ret);
  5787. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  5788. if (ret != 0)
  5789. return WC_TEST_RET_ENC_EC(ret);
  5790. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  5791. if (ret != 0)
  5792. return WC_TEST_RET_ENC_EC(ret);
  5793. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5794. (word32)test_arc4[i].outLen);
  5795. if (ret != 0)
  5796. return WC_TEST_RET_ENC_EC(ret);
  5797. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5798. if (ret != 0)
  5799. return WC_TEST_RET_ENC_EC(ret);
  5800. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5801. return WC_TEST_RET_ENC_I(i);
  5802. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5803. return WC_TEST_RET_ENC_I(i);
  5804. wc_Arc4Free(&enc);
  5805. wc_Arc4Free(&dec);
  5806. }
  5807. return 0;
  5808. }
  5809. #endif
  5810. #ifdef HAVE_CHACHA
  5811. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  5812. {
  5813. ChaCha enc;
  5814. ChaCha dec;
  5815. byte cipher[128];
  5816. byte plain[128];
  5817. byte sliver[64];
  5818. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5819. word32 keySz = 32;
  5820. wc_test_ret_t ret = 0;
  5821. int i;
  5822. int times = 4;
  5823. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5824. {
  5825. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5826. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5827. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5828. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5829. };
  5830. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5831. {
  5832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5833. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5834. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5835. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5836. };
  5837. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5838. {
  5839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5840. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5841. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5842. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5843. };
  5844. /* 128 bit key */
  5845. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5846. {
  5847. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5848. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5849. };
  5850. const byte* keys[] = {key1, key2, key3, key4};
  5851. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5852. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5853. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5854. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5855. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5856. #ifndef BENCH_EMBEDDED
  5857. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5858. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5859. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5860. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5861. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5862. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5863. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5864. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5865. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5866. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5867. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5868. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5869. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5870. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5871. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5872. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5873. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5874. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5875. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5876. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5877. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5878. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5879. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5880. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5881. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5882. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5883. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5884. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5885. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5886. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5887. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5888. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5889. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5890. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5891. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5892. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5893. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5894. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5895. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5896. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5897. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5898. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5899. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5900. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5901. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5902. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5903. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5904. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5905. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5906. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5907. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5908. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5909. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5910. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5911. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5912. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5913. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5914. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5915. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5916. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5917. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5918. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5919. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5920. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5921. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5922. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5923. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5924. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5925. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5926. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5927. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5928. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5929. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5930. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5931. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5932. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5933. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5934. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5935. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5936. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5937. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5938. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5939. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5940. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5941. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5942. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5943. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5944. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5945. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5946. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5947. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5948. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5949. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5950. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5951. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5952. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5953. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5954. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5955. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5956. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5957. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5958. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5959. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5960. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5961. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5962. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5963. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5964. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5965. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5966. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5967. };
  5968. #define CHACHA_BIG_TEST_SIZE 1305
  5969. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5970. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5971. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5972. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5973. #else
  5974. byte* cipher_big;
  5975. byte* plain_big;
  5976. byte* input_big;
  5977. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5978. int block_size;
  5979. #endif /* BENCH_EMBEDDED */
  5980. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5981. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5982. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5983. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5984. byte* test_chacha[4];
  5985. test_chacha[0] = a;
  5986. test_chacha[1] = b;
  5987. test_chacha[2] = c;
  5988. test_chacha[3] = d;
  5989. #ifndef BENCH_EMBEDDED
  5990. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5991. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5992. DYNAMIC_TYPE_TMP_BUFFER);
  5993. if (cipher_big == NULL) {
  5994. return MEMORY_E;
  5995. }
  5996. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5997. DYNAMIC_TYPE_TMP_BUFFER);
  5998. if (plain_big == NULL) {
  5999. return MEMORY_E;
  6000. }
  6001. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6002. DYNAMIC_TYPE_TMP_BUFFER);
  6003. if (input_big == NULL) {
  6004. return MEMORY_E;
  6005. }
  6006. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6007. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6008. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6009. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6010. #endif /* BENCH_EMBEDDED */
  6011. for (i = 0; i < times; ++i) {
  6012. if (i < 3) {
  6013. keySz = 32;
  6014. }
  6015. else {
  6016. keySz = 16;
  6017. }
  6018. XMEMCPY(plain, keys[i], keySz);
  6019. XMEMSET(cipher, 0, 32);
  6020. XMEMCPY(cipher + 4, ivs[i], 8);
  6021. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6022. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6023. if (ret != 0)
  6024. return ret;
  6025. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6026. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6027. if (ret != 0)
  6028. return ret;
  6029. XMEMCPY(plain, input, 8);
  6030. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6031. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6032. if (ret != 0)
  6033. return ret;
  6034. if (XMEMCMP(test_chacha[i], cipher, 8))
  6035. return WC_TEST_RET_ENC_I(i);
  6036. if (XMEMCMP(plain, input, 8))
  6037. return WC_TEST_RET_ENC_I(i);
  6038. }
  6039. /* test of starting at a different counter
  6040. encrypts all of the information and decrypts starting at 2nd chunk */
  6041. XMEMSET(plain, 0, sizeof(plain));
  6042. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6043. XMEMSET(cipher, 0, sizeof(cipher));
  6044. XMEMCPY(cipher + 4, ivs[0], 8);
  6045. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6046. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6047. if (ret != 0)
  6048. return ret;
  6049. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6050. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6051. if (ret != 0)
  6052. return ret;
  6053. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6054. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6055. if (ret != 0)
  6056. return ret;
  6057. if (XMEMCMP(plain + 64, sliver, 64))
  6058. return WC_TEST_RET_ENC_NC;
  6059. #ifndef BENCH_EMBEDDED
  6060. /* test of encrypting more data */
  6061. keySz = 32;
  6062. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6063. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6064. if (ret != 0)
  6065. return ret;
  6066. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6067. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6068. if (ret != 0)
  6069. return ret;
  6070. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6071. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6072. CHACHA_BIG_TEST_SIZE);
  6073. if (ret != 0)
  6074. return ret;
  6075. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6076. return WC_TEST_RET_ENC_NC;
  6077. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6078. return WC_TEST_RET_ENC_NC;
  6079. for (i = 0; i < 18; ++i) {
  6080. /* this will test all paths
  6081. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6082. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6083. */
  6084. block_size = (2 << (i%9)) - (i<9?1:0);
  6085. keySz = 32;
  6086. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6087. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6088. if (ret != 0)
  6089. return ret;
  6090. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6091. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6092. if (ret != 0)
  6093. return ret;
  6094. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  6095. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  6096. if (ret != 0)
  6097. return ret;
  6098. if (XMEMCMP(plain_big, input_big, block_size))
  6099. return WC_TEST_RET_ENC_I(i);
  6100. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6101. return WC_TEST_RET_ENC_I(i);
  6102. }
  6103. /* Streaming test */
  6104. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6105. int j, rem;
  6106. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6107. if (ret != 0)
  6108. return WC_TEST_RET_ENC_EC(ret);
  6109. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6110. if (ret != 0)
  6111. return WC_TEST_RET_ENC_EC(ret);
  6112. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6113. if (ret != 0)
  6114. return WC_TEST_RET_ENC_EC(ret);
  6115. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6116. if (ret != 0)
  6117. return WC_TEST_RET_ENC_EC(ret);
  6118. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6119. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  6120. if (ret != 0)
  6121. return WC_TEST_RET_ENC_EC(ret);
  6122. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  6123. if (ret != 0)
  6124. return WC_TEST_RET_ENC_EC(ret);
  6125. }
  6126. rem = CHACHA_BIG_TEST_SIZE - j;
  6127. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  6128. if (ret != 0)
  6129. return WC_TEST_RET_ENC_EC(ret);
  6130. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  6131. if (ret != 0)
  6132. return WC_TEST_RET_ENC_EC(ret);
  6133. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6134. return WC_TEST_RET_ENC_NC;
  6135. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6136. return WC_TEST_RET_ENC_NC;
  6137. }
  6138. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6139. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6140. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6141. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6142. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6143. #endif /* BENCH_EMBEDDED */
  6144. return 0;
  6145. }
  6146. #endif /* HAVE_CHACHA */
  6147. #ifdef HAVE_POLY1305
  6148. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6149. {
  6150. wc_test_ret_t ret = 0;
  6151. int i;
  6152. byte tag[16];
  6153. Poly1305 enc;
  6154. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6155. {
  6156. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6157. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6158. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6159. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6160. 0x75,0x70
  6161. };
  6162. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6163. {
  6164. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6165. 0x6c,0x64,0x21
  6166. };
  6167. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6168. {
  6169. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6170. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6171. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6172. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6173. };
  6174. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6175. {
  6176. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6177. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6178. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6179. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6180. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6181. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6182. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6183. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6184. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6185. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6186. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6187. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6188. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6189. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6190. 0x61,0x16
  6191. };
  6192. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6193. {
  6194. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6195. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6196. };
  6197. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6198. {
  6199. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6200. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6201. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6202. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6203. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6204. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6205. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6206. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6207. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6208. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6209. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6210. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6211. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6212. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6213. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6214. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6215. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6216. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6217. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6218. 0x61,0x16
  6219. };
  6220. byte additional[] =
  6221. {
  6222. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6223. 0xc4,0xc5,0xc6,0xc7
  6224. };
  6225. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6226. {
  6227. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6228. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6229. };
  6230. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6231. {
  6232. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6233. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6234. };
  6235. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  6236. {
  6237. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  6238. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  6239. };
  6240. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  6241. {
  6242. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  6243. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  6244. };
  6245. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  6246. {
  6247. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  6248. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  6249. };
  6250. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  6251. {
  6252. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6253. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6254. };
  6255. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  6256. {
  6257. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  6258. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  6259. };
  6260. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  6261. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  6262. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  6263. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6264. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6265. };
  6266. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6267. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  6268. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  6269. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  6270. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  6271. };
  6272. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  6273. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  6274. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  6275. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  6276. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  6277. };
  6278. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  6279. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6280. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6281. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6282. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6283. };
  6284. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  6285. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  6286. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  6287. const byte* keys[] = {key, key, key2, key2, key5, key};
  6288. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  6289. correct6};
  6290. for (i = 0; i < 6; i++) {
  6291. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  6292. if (ret != 0)
  6293. return WC_TEST_RET_ENC_I(i);
  6294. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  6295. if (ret != 0)
  6296. return WC_TEST_RET_ENC_I(i);
  6297. ret = wc_Poly1305Final(&enc, tag);
  6298. if (ret != 0)
  6299. return WC_TEST_RET_ENC_I(i);
  6300. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  6301. return WC_TEST_RET_ENC_I(i);
  6302. }
  6303. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  6304. XMEMSET(tag, 0, sizeof(tag));
  6305. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  6306. if (ret != 0)
  6307. return WC_TEST_RET_ENC_EC(ret);
  6308. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6309. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6310. if (ret != 0)
  6311. return WC_TEST_RET_ENC_EC(ret);
  6312. if (XMEMCMP(tag, correct4, sizeof(tag)))
  6313. return WC_TEST_RET_ENC_NC;
  6314. /* Check fail of TLS MAC function if altering additional data */
  6315. XMEMSET(tag, 0, sizeof(tag));
  6316. additional[0]++;
  6317. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6318. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6319. if (ret != 0)
  6320. return WC_TEST_RET_ENC_EC(ret);
  6321. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  6322. return WC_TEST_RET_ENC_NC;
  6323. return 0;
  6324. }
  6325. #endif /* HAVE_POLY1305 */
  6326. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  6327. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  6328. {
  6329. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  6330. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6331. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  6332. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  6333. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  6334. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  6335. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  6336. };
  6337. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  6338. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  6339. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  6340. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  6341. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  6342. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  6343. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  6344. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  6345. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  6346. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  6347. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  6348. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  6349. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  6350. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  6351. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  6352. 0x74, 0x2e
  6353. };
  6354. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  6355. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  6356. 0x44, 0x45, 0x46, 0x47
  6357. };
  6358. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  6359. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  6360. 0xc4, 0xc5, 0xc6, 0xc7
  6361. };
  6362. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  6363. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  6364. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  6365. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  6366. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  6367. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  6368. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  6369. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  6370. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  6371. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  6372. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  6373. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  6374. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  6375. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  6376. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  6377. 0x61, 0x16
  6378. };
  6379. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  6380. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  6381. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  6382. };
  6383. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  6384. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6385. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6386. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  6387. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  6388. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  6389. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  6390. };
  6391. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  6392. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  6393. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  6394. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  6395. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6396. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  6397. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  6398. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  6399. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  6400. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  6401. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  6402. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  6403. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  6404. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  6405. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  6406. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  6407. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6408. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  6409. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  6410. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  6411. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  6412. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  6413. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  6414. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  6415. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  6416. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  6417. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  6418. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  6419. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  6420. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  6421. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  6422. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  6423. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  6424. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  6425. 0x9d
  6426. };
  6427. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6428. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  6429. 0x05, 0x06, 0x07, 0x08
  6430. };
  6431. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  6432. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  6433. 0x00, 0x00, 0x4e, 0x91
  6434. };
  6435. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  6436. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  6437. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  6438. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  6439. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  6440. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  6441. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  6442. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  6443. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  6444. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  6445. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  6446. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  6447. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  6448. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  6449. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  6450. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  6451. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  6452. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  6453. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  6454. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  6455. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  6456. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  6457. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  6458. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  6459. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  6460. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  6461. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  6462. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  6463. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  6464. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  6465. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  6466. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  6467. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  6468. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  6469. 0x9b
  6470. };
  6471. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  6472. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  6473. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  6474. };
  6475. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  6476. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  6477. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  6478. wc_test_ret_t err;
  6479. ChaChaPoly_Aead aead;
  6480. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  6481. #define TEST_SMALL_CHACHA_CHUNKS 32
  6482. #else
  6483. #define TEST_SMALL_CHACHA_CHUNKS 64
  6484. #endif
  6485. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6486. word32 testLen;
  6487. #endif
  6488. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6489. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6490. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6491. /* Parameter Validation testing */
  6492. /* Encrypt */
  6493. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  6494. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6495. if (err != BAD_FUNC_ARG)
  6496. return WC_TEST_RET_ENC_EC(err);
  6497. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  6498. plaintext1, sizeof(plaintext1), generatedCiphertext,
  6499. generatedAuthTag);
  6500. if (err != BAD_FUNC_ARG)
  6501. return WC_TEST_RET_ENC_EC(err);
  6502. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6503. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6504. if (err != BAD_FUNC_ARG)
  6505. return WC_TEST_RET_ENC_EC(err);
  6506. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6507. sizeof(plaintext1), NULL, generatedAuthTag);
  6508. if (err != BAD_FUNC_ARG)
  6509. return WC_TEST_RET_ENC_EC(err);
  6510. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6511. sizeof(plaintext1), generatedCiphertext, NULL);
  6512. if (err != BAD_FUNC_ARG)
  6513. return WC_TEST_RET_ENC_EC(err);
  6514. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6515. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6516. if (err != BAD_FUNC_ARG)
  6517. return WC_TEST_RET_ENC_EC(err);
  6518. /* Decrypt */
  6519. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  6520. sizeof(cipher2), authTag2, generatedPlaintext);
  6521. if (err != BAD_FUNC_ARG)
  6522. return WC_TEST_RET_ENC_EC(err);
  6523. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  6524. sizeof(cipher2), authTag2, generatedPlaintext);
  6525. if (err != BAD_FUNC_ARG)
  6526. return WC_TEST_RET_ENC_EC(err);
  6527. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6528. sizeof(cipher2), authTag2, generatedPlaintext);
  6529. if (err != BAD_FUNC_ARG)
  6530. return WC_TEST_RET_ENC_EC(err);
  6531. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6532. sizeof(cipher2), NULL, generatedPlaintext);
  6533. if (err != BAD_FUNC_ARG)
  6534. return WC_TEST_RET_ENC_EC(err);
  6535. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6536. sizeof(cipher2), authTag2, NULL);
  6537. if (err != BAD_FUNC_ARG)
  6538. return WC_TEST_RET_ENC_EC(err);
  6539. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6540. sizeof(cipher2), authTag2, generatedPlaintext);
  6541. if (err != BAD_FUNC_ARG)
  6542. return WC_TEST_RET_ENC_EC(err);
  6543. /* Test #1 */
  6544. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  6545. aad1, sizeof(aad1),
  6546. plaintext1, sizeof(plaintext1),
  6547. generatedCiphertext, generatedAuthTag);
  6548. if (err) {
  6549. return WC_TEST_RET_ENC_EC(err);
  6550. }
  6551. /* -- Check the ciphertext and authtag */
  6552. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6553. return WC_TEST_RET_ENC_NC;
  6554. }
  6555. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  6556. return WC_TEST_RET_ENC_NC;
  6557. }
  6558. /* -- Verify decryption works */
  6559. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  6560. aad1, sizeof(aad1),
  6561. cipher1, sizeof(cipher1),
  6562. authTag1, generatedPlaintext);
  6563. if (err) {
  6564. return err;
  6565. }
  6566. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6567. return WC_TEST_RET_ENC_NC;
  6568. }
  6569. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6570. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6571. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6572. /* Test #2 */
  6573. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  6574. aad2, sizeof(aad2),
  6575. plaintext2, sizeof(plaintext2),
  6576. generatedCiphertext, generatedAuthTag);
  6577. if (err) {
  6578. return err;
  6579. }
  6580. /* -- Check the ciphertext and authtag */
  6581. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6582. return WC_TEST_RET_ENC_NC;
  6583. }
  6584. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  6585. return WC_TEST_RET_ENC_NC;
  6586. }
  6587. /* -- Verify decryption works */
  6588. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  6589. aad2, sizeof(aad2),
  6590. cipher2, sizeof(cipher2),
  6591. authTag2, generatedPlaintext);
  6592. if (err) {
  6593. return err;
  6594. }
  6595. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6596. return WC_TEST_RET_ENC_NC;
  6597. }
  6598. /* AEAD init/update/final - bad argument tests */
  6599. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  6600. CHACHA20_POLY1305_AEAD_DECRYPT);
  6601. if (err != BAD_FUNC_ARG)
  6602. return WC_TEST_RET_ENC_EC(err);
  6603. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  6604. CHACHA20_POLY1305_AEAD_DECRYPT);
  6605. if (err != BAD_FUNC_ARG)
  6606. return WC_TEST_RET_ENC_EC(err);
  6607. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  6608. CHACHA20_POLY1305_AEAD_DECRYPT);
  6609. if (err != BAD_FUNC_ARG)
  6610. return WC_TEST_RET_ENC_EC(err);
  6611. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  6612. if (err != BAD_FUNC_ARG)
  6613. return WC_TEST_RET_ENC_EC(err);
  6614. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  6615. if (err != BAD_FUNC_ARG)
  6616. return WC_TEST_RET_ENC_EC(err);
  6617. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  6618. generatedPlaintext, sizeof(plaintext1));
  6619. if (err != BAD_FUNC_ARG)
  6620. return WC_TEST_RET_ENC_EC(err);
  6621. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  6622. sizeof(plaintext1));
  6623. if (err != BAD_FUNC_ARG)
  6624. return WC_TEST_RET_ENC_EC(err);
  6625. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  6626. sizeof(plaintext1));
  6627. if (err != BAD_FUNC_ARG)
  6628. return WC_TEST_RET_ENC_EC(err);
  6629. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  6630. if (err != BAD_FUNC_ARG)
  6631. return WC_TEST_RET_ENC_EC(err);
  6632. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  6633. if (err != BAD_FUNC_ARG)
  6634. return WC_TEST_RET_ENC_EC(err);
  6635. /* AEAD init/update/final - bad state tests */
  6636. /* clear struct - make valgrind happy to resolve
  6637. "Conditional jump or move depends on uninitialised value(s)".
  6638. The enum is "int" size and aead.state is "byte" */
  6639. /* The wc_ChaCha20Poly1305_Init function does this normally */
  6640. XMEMSET(&aead, 0, sizeof(aead));
  6641. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6642. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6643. if (err != BAD_STATE_E)
  6644. return WC_TEST_RET_ENC_EC(err);
  6645. aead.state = CHACHA20_POLY1305_STATE_DATA;
  6646. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6647. if (err != BAD_STATE_E)
  6648. return WC_TEST_RET_ENC_EC(err);
  6649. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6650. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  6651. generatedPlaintext, sizeof(plaintext1));
  6652. if (err != BAD_STATE_E)
  6653. return WC_TEST_RET_ENC_EC(err);
  6654. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6655. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6656. if (err != BAD_STATE_E)
  6657. return WC_TEST_RET_ENC_EC(err);
  6658. aead.state = CHACHA20_POLY1305_STATE_READY;
  6659. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6660. if (err != BAD_STATE_E)
  6661. return WC_TEST_RET_ENC_EC(err);
  6662. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6663. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6664. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6665. /* Test 1 - Encrypt */
  6666. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6667. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6668. if (err != 0)
  6669. return WC_TEST_RET_ENC_EC(err);
  6670. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6671. if (err != 0)
  6672. return WC_TEST_RET_ENC_EC(err);
  6673. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6674. /* test doing data in smaller chunks */
  6675. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6676. word32 dataLen = sizeof(plaintext1) - testLen;
  6677. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6678. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6679. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  6680. &generatedCiphertext[testLen], dataLen);
  6681. if (err != 0)
  6682. return WC_TEST_RET_ENC_EC(err);
  6683. testLen += dataLen;
  6684. }
  6685. #else
  6686. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  6687. generatedCiphertext, sizeof(plaintext1));
  6688. #endif
  6689. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6690. if (err != 0)
  6691. return WC_TEST_RET_ENC_EC(err);
  6692. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6693. if (err != 0)
  6694. return WC_TEST_RET_ENC_EC(err);
  6695. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6696. return WC_TEST_RET_ENC_NC;
  6697. }
  6698. /* Test 1 - Decrypt */
  6699. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6700. CHACHA20_POLY1305_AEAD_DECRYPT);
  6701. if (err != 0)
  6702. return WC_TEST_RET_ENC_EC(err);
  6703. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6704. if (err != 0)
  6705. return WC_TEST_RET_ENC_EC(err);
  6706. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6707. /* test doing data in smaller chunks */
  6708. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6709. word32 dataLen = sizeof(plaintext1) - testLen;
  6710. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6711. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6712. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6713. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6714. dataLen);
  6715. if (err != 0)
  6716. return WC_TEST_RET_ENC_EC(err);
  6717. testLen += dataLen;
  6718. }
  6719. #else
  6720. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6721. generatedPlaintext, sizeof(cipher1));
  6722. #endif
  6723. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6724. if (err != 0)
  6725. return WC_TEST_RET_ENC_EC(err);
  6726. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6727. if (err != 0)
  6728. return WC_TEST_RET_ENC_EC(err);
  6729. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6730. return WC_TEST_RET_ENC_NC;
  6731. }
  6732. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6733. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6734. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6735. /* Test 2 - Encrypt */
  6736. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6737. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6738. if (err != 0)
  6739. return WC_TEST_RET_ENC_EC(err);
  6740. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6741. if (err != 0)
  6742. return WC_TEST_RET_ENC_EC(err);
  6743. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6744. /* test doing data in smaller chunks */
  6745. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6746. word32 dataLen = sizeof(plaintext2) - testLen;
  6747. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6748. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6749. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  6750. &generatedCiphertext[testLen], dataLen);
  6751. if (err != 0)
  6752. return WC_TEST_RET_ENC_EC(err);
  6753. testLen += dataLen;
  6754. }
  6755. #else
  6756. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  6757. sizeof(plaintext2));
  6758. #endif
  6759. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6760. if (err != 0)
  6761. return WC_TEST_RET_ENC_EC(err);
  6762. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6763. if (err != 0)
  6764. return WC_TEST_RET_ENC_EC(err);
  6765. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6766. return WC_TEST_RET_ENC_NC;
  6767. }
  6768. /* Test 2 - Decrypt */
  6769. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6770. CHACHA20_POLY1305_AEAD_DECRYPT);
  6771. if (err != 0)
  6772. return WC_TEST_RET_ENC_EC(err);
  6773. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6774. if (err != 0)
  6775. return WC_TEST_RET_ENC_EC(err);
  6776. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6777. /* test doing data in smaller chunks */
  6778. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6779. word32 dataLen = sizeof(plaintext2) - testLen;
  6780. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6781. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6782. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6783. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6784. dataLen);
  6785. if (err != 0)
  6786. return WC_TEST_RET_ENC_EC(err);
  6787. testLen += dataLen;
  6788. }
  6789. #else
  6790. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6791. generatedPlaintext, sizeof(cipher2));
  6792. #endif
  6793. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6794. if (err != 0)
  6795. return WC_TEST_RET_ENC_EC(err);
  6796. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6797. if (err != 0)
  6798. return WC_TEST_RET_ENC_EC(err);
  6799. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6800. return WC_TEST_RET_ENC_NC;
  6801. }
  6802. return err;
  6803. }
  6804. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6805. #ifndef NO_DES3
  6806. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  6807. {
  6808. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6809. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6810. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6811. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6812. };
  6813. byte plain[24];
  6814. byte cipher[24];
  6815. Des enc;
  6816. Des dec;
  6817. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6818. {
  6819. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6820. };
  6821. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6822. {
  6823. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6824. };
  6825. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6826. {
  6827. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6828. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6829. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6830. };
  6831. wc_test_ret_t ret;
  6832. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6833. if (ret != 0)
  6834. return WC_TEST_RET_ENC_EC(ret);
  6835. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6836. if (ret != 0)
  6837. return WC_TEST_RET_ENC_EC(ret);
  6838. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6839. if (ret != 0)
  6840. return WC_TEST_RET_ENC_EC(ret);
  6841. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6842. if (ret != 0)
  6843. return WC_TEST_RET_ENC_EC(ret);
  6844. if (XMEMCMP(plain, vector, sizeof(plain)))
  6845. return WC_TEST_RET_ENC_NC;
  6846. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6847. return WC_TEST_RET_ENC_NC;
  6848. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6849. if (ret != 0)
  6850. return WC_TEST_RET_ENC_EC(ret);
  6851. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6852. {
  6853. EncryptedInfo info;
  6854. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6855. XMEMCPY(info.iv, iv, sizeof(iv));
  6856. info.ivSz = sizeof(iv);
  6857. info.keySz = sizeof(key);
  6858. info.cipherType = WC_CIPHER_DES;
  6859. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6860. sizeof(key), WC_HASH_TYPE_SHA);
  6861. if (ret != 0)
  6862. return WC_TEST_RET_ENC_EC(ret);
  6863. /* Test invalid info ptr */
  6864. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6865. sizeof(key), WC_HASH_TYPE_SHA);
  6866. if (ret != BAD_FUNC_ARG)
  6867. return WC_TEST_RET_ENC_EC(ret);
  6868. #ifndef NO_PWDBASED
  6869. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6870. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6871. sizeof(key), WC_HASH_TYPE_NONE);
  6872. if (ret == 0)
  6873. return WC_TEST_RET_ENC_EC(ret);
  6874. #endif /* !NO_PWDBASED */
  6875. }
  6876. #endif
  6877. return 0;
  6878. }
  6879. #endif /* !NO_DES3 */
  6880. #ifndef NO_DES3
  6881. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  6882. {
  6883. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6884. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6885. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6886. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6887. };
  6888. byte plain[24];
  6889. byte cipher[24];
  6890. Des3 enc;
  6891. Des3 dec;
  6892. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6893. {
  6894. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6895. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6896. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6897. };
  6898. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6899. {
  6900. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6901. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6902. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6903. };
  6904. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6905. {
  6906. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6907. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6908. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6909. };
  6910. wc_test_ret_t ret;
  6911. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6912. size_t i;
  6913. #endif
  6914. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  6915. if (ret != 0)
  6916. return WC_TEST_RET_ENC_EC(ret);
  6917. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  6918. if (ret != 0)
  6919. return WC_TEST_RET_ENC_EC(ret);
  6920. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6921. if (ret != 0)
  6922. return WC_TEST_RET_ENC_EC(ret);
  6923. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6924. if (ret != 0)
  6925. return WC_TEST_RET_ENC_EC(ret);
  6926. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6927. #if defined(WOLFSSL_ASYNC_CRYPT)
  6928. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6929. #endif
  6930. if (ret != 0)
  6931. return WC_TEST_RET_ENC_EC(ret);
  6932. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6933. #if defined(WOLFSSL_ASYNC_CRYPT)
  6934. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6935. #endif
  6936. if (ret != 0)
  6937. return WC_TEST_RET_ENC_EC(ret);
  6938. if (XMEMCMP(plain, vector, sizeof(plain)))
  6939. return WC_TEST_RET_ENC_NC;
  6940. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6941. return WC_TEST_RET_ENC_NC;
  6942. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6943. /* test the same vectors with using compatibility layer */
  6944. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  6945. DES_key_schedule ks1;
  6946. DES_key_schedule ks2;
  6947. DES_key_schedule ks3;
  6948. DES_cblock iv4;
  6949. byte tmp[sizeof(vector)];
  6950. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6951. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6952. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6953. XMEMSET(plain, 0, sizeof(plain));
  6954. XMEMSET(cipher, 0, sizeof(cipher));
  6955. /* Test in-place encrypt/decrypt */
  6956. XMEMCPY(tmp, vector, sizeof(vector));
  6957. /* Use i as the splitter */
  6958. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6959. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6960. &iv4, DES_ENCRYPT);
  6961. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  6962. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  6963. XMEMCPY(cipher, tmp, sizeof(cipher));
  6964. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6965. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6966. &iv4, DES_DECRYPT);
  6967. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  6968. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  6969. XMEMCPY(plain, tmp, sizeof(plain));
  6970. if (XMEMCMP(plain, vector, sizeof(plain)))
  6971. return WC_TEST_RET_ENC_NC;
  6972. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6973. return WC_TEST_RET_ENC_NC;
  6974. }
  6975. #endif /* OPENSSL_EXTRA */
  6976. wc_Des3Free(&enc);
  6977. wc_Des3Free(&dec);
  6978. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6979. {
  6980. EncryptedInfo info;
  6981. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6982. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6983. info.ivSz = sizeof(iv3);
  6984. info.keySz = sizeof(key3);
  6985. info.cipherType = WC_CIPHER_DES3;
  6986. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6987. sizeof(key3), WC_HASH_TYPE_SHA);
  6988. if (ret != 0)
  6989. return WC_TEST_RET_ENC_EC(ret);
  6990. }
  6991. #endif
  6992. return 0;
  6993. }
  6994. #endif /* NO_DES3 */
  6995. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  6996. * see print_fiducials() below.
  6997. */
  6998. #ifndef NO_AES
  6999. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7000. defined(WOLFSSL_AES_XTS)
  7001. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7002. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7003. /* pass in the function, key, iv, plain text and expected and this function
  7004. * tests that the encryption and decryption is successful */
  7005. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7006. const byte* iv, const byte* plain, int plainSz,
  7007. const byte* expected, int expectedSz)
  7008. {
  7009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7010. EVP_CIPHER_CTX *ctx = NULL;
  7011. #else
  7012. EVP_CIPHER_CTX ctx[1];
  7013. #endif
  7014. int ctx_inited = 0;
  7015. int idx, cipherSz;
  7016. wc_test_ret_t ret = 0;
  7017. byte* cipher;
  7018. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7019. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7020. return MEMORY_E;
  7021. #endif
  7022. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7023. if (cipher == NULL) {
  7024. ret = WC_TEST_RET_ENC_ERRNO;
  7025. goto EVP_TEST_END;
  7026. }
  7027. /* test encrypt */
  7028. EVP_CIPHER_CTX_init(ctx);
  7029. ctx_inited = 1;
  7030. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7031. ret = WC_TEST_RET_ENC_NC;
  7032. goto EVP_TEST_END;
  7033. }
  7034. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7035. ret = WC_TEST_RET_ENC_NC;
  7036. goto EVP_TEST_END;
  7037. }
  7038. cipherSz = idx;
  7039. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7040. ret = WC_TEST_RET_ENC_NC;
  7041. goto EVP_TEST_END;
  7042. }
  7043. cipherSz += idx;
  7044. if (XMEMCMP(cipher, expected, plainSz)) {
  7045. ret = WC_TEST_RET_ENC_NC;
  7046. goto EVP_TEST_END;
  7047. }
  7048. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7049. ctx_inited = 0;
  7050. if (ret == WOLFSSL_SUCCESS)
  7051. ret = 0;
  7052. else {
  7053. ret = WC_TEST_RET_ENC_NC;
  7054. goto EVP_TEST_END;
  7055. }
  7056. /* test decrypt */
  7057. EVP_CIPHER_CTX_init(ctx);
  7058. ctx_inited = 1;
  7059. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7060. ret = WC_TEST_RET_ENC_NC;
  7061. goto EVP_TEST_END;
  7062. }
  7063. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7064. ret = WC_TEST_RET_ENC_NC;
  7065. goto EVP_TEST_END;
  7066. }
  7067. cipherSz = idx;
  7068. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7069. ret = WC_TEST_RET_ENC_NC;
  7070. goto EVP_TEST_END;
  7071. }
  7072. cipherSz += idx;
  7073. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7074. ret = WC_TEST_RET_ENC_NC;
  7075. goto EVP_TEST_END;
  7076. }
  7077. EVP_TEST_END:
  7078. if (cipher)
  7079. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7080. (void)cipherSz;
  7081. if (ctx_inited) {
  7082. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7083. if (cleanup_ret != WOLFSSL_SUCCESS)
  7084. ret = WC_TEST_RET_ENC_NC;
  7085. }
  7086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7087. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7088. #endif
  7089. return ret;
  7090. }
  7091. #endif /* OPENSSL_EXTRA */
  7092. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7093. #ifdef WOLFSSL_AES_OFB
  7094. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7095. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7096. {
  7097. #ifdef WOLFSSL_AES_256
  7098. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7099. {
  7100. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7101. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7102. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7103. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7104. };
  7105. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7106. {
  7107. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7108. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7109. };
  7110. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7111. {
  7112. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7113. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7114. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7115. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7116. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7117. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7118. };
  7119. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7120. {
  7121. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7122. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7123. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7124. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7125. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7126. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7127. };
  7128. #endif /* WOLFSSL_AES_256 */
  7129. #ifdef WOLFSSL_AES_128
  7130. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7131. {
  7132. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7133. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7134. };
  7135. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  7136. {
  7137. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7139. };
  7140. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  7141. {
  7142. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7143. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7144. };
  7145. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7146. {
  7147. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7148. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7149. };
  7150. #endif /* WOLFSSL_AES_128 */
  7151. #ifdef WOLFSSL_AES_192
  7152. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7153. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7154. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7155. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7156. };
  7157. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7158. {
  7159. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7160. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7161. };
  7162. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7163. {
  7164. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7165. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7166. };
  7167. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7168. {
  7169. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7170. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7171. };
  7172. #endif /* WOLFSSL_AES_192 */
  7173. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7174. Aes *enc = NULL;
  7175. #else
  7176. Aes enc[1];
  7177. #endif
  7178. byte cipher[AES_BLOCK_SIZE * 4];
  7179. #ifdef HAVE_AES_DECRYPT
  7180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7181. Aes *dec = NULL;
  7182. #else
  7183. Aes dec[1];
  7184. #endif
  7185. byte plain [AES_BLOCK_SIZE * 4];
  7186. #endif
  7187. wc_test_ret_t ret = 0;
  7188. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7189. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7190. ERROR_OUT(-1, out);
  7191. #ifdef HAVE_AES_DECRYPT
  7192. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7193. ERROR_OUT(-1, out);
  7194. #endif
  7195. #endif
  7196. XMEMSET(enc, 0, sizeof *enc);
  7197. #ifdef HAVE_AES_DECRYPT
  7198. XMEMSET(dec, 0, sizeof *dec);
  7199. #endif
  7200. #ifdef WOLFSSL_AES_128
  7201. /* 128 key size test */
  7202. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7203. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7204. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7205. cipher2, sizeof(cipher2));
  7206. if (ret != 0) {
  7207. goto out;
  7208. }
  7209. #endif
  7210. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  7211. if (ret != 0)
  7212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7213. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  7214. if (ret != 0)
  7215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7216. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7217. if (ret != 0)
  7218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7219. #ifdef HAVE_AES_DECRYPT
  7220. /* decrypt uses AES_ENCRYPTION */
  7221. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7222. if (ret != 0)
  7223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7224. #endif
  7225. XMEMSET(cipher, 0, sizeof(cipher));
  7226. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  7227. if (ret != 0)
  7228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7229. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  7230. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7231. #ifdef HAVE_AES_DECRYPT
  7232. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  7233. if (ret != 0)
  7234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7235. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  7236. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7237. #endif /* HAVE_AES_DECRYPT */
  7238. #endif /* WOLFSSL_AES_128 */
  7239. #ifdef WOLFSSL_AES_192
  7240. /* 192 key size test */
  7241. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7242. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7243. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  7244. cipher3, sizeof(cipher3));
  7245. if (ret != 0) {
  7246. goto out;
  7247. }
  7248. #endif
  7249. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7250. if (ret != 0)
  7251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7252. #ifdef HAVE_AES_DECRYPT
  7253. /* decrypt uses AES_ENCRYPTION */
  7254. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7255. if (ret != 0)
  7256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7257. #endif
  7258. XMEMSET(cipher, 0, sizeof(cipher));
  7259. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  7260. if (ret != 0)
  7261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7262. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  7263. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7264. #ifdef HAVE_AES_DECRYPT
  7265. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  7266. if (ret != 0)
  7267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7268. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  7269. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7270. #endif /* HAVE_AES_DECRYPT */
  7271. #endif /* WOLFSSL_AES_192 */
  7272. #ifdef WOLFSSL_AES_256
  7273. /* 256 key size test */
  7274. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7275. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7276. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  7277. cipher1, sizeof(cipher1));
  7278. if (ret != 0) {
  7279. goto out;
  7280. }
  7281. #endif
  7282. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7283. if (ret != 0)
  7284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7285. #ifdef HAVE_AES_DECRYPT
  7286. /* decrypt uses AES_ENCRYPTION */
  7287. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7288. if (ret != 0)
  7289. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7290. #endif
  7291. XMEMSET(cipher, 0, sizeof(cipher));
  7292. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  7293. if (ret != 0)
  7294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7295. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  7296. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7297. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7298. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7299. if (ret != 0)
  7300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7301. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  7302. AES_BLOCK_SIZE))
  7303. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7304. #ifdef HAVE_AES_DECRYPT
  7305. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  7306. if (ret != 0)
  7307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7308. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  7309. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7310. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  7311. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7312. if (ret != 0)
  7313. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7314. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  7315. AES_BLOCK_SIZE))
  7316. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7317. #endif /* HAVE_AES_DECRYPT */
  7318. /* multiple blocks at once */
  7319. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7320. if (ret != 0)
  7321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7322. #ifdef HAVE_AES_DECRYPT
  7323. /* decrypt uses AES_ENCRYPTION */
  7324. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7325. if (ret != 0)
  7326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7327. #endif
  7328. XMEMSET(cipher, 0, sizeof(cipher));
  7329. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  7330. if (ret != 0)
  7331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7332. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  7333. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7334. #ifdef HAVE_AES_DECRYPT
  7335. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  7336. if (ret != 0)
  7337. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7338. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  7339. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7340. #endif /* HAVE_AES_DECRYPT */
  7341. /* inline decrypt/encrypt*/
  7342. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7343. if (ret != 0)
  7344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7345. #ifdef HAVE_AES_DECRYPT
  7346. /* decrypt uses AES_ENCRYPTION */
  7347. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7348. if (ret != 0)
  7349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7350. #endif
  7351. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  7352. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  7353. if (ret != 0)
  7354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7355. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7356. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7357. #ifdef HAVE_AES_DECRYPT
  7358. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  7359. if (ret != 0)
  7360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7361. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  7362. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7363. #endif /* HAVE_AES_DECRYPT */
  7364. /* 256 key size test leftover support */
  7365. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7366. if (ret != 0)
  7367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7368. #ifdef HAVE_AES_DECRYPT
  7369. /* decrypt uses AES_ENCRYPTION */
  7370. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7371. if (ret != 0)
  7372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7373. #endif
  7374. XMEMSET(cipher, 0, sizeof(cipher));
  7375. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  7376. if (ret != 0)
  7377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7378. if (XMEMCMP(cipher, cipher1, 3))
  7379. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7380. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  7381. if (ret != 0)
  7382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7383. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  7384. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7385. #ifdef HAVE_AES_DECRYPT
  7386. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  7387. if (ret != 0)
  7388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7389. if (XMEMCMP(plain, plain1, 6))
  7390. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7391. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  7392. if (ret != 0)
  7393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7394. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  7395. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7396. #endif /* HAVE_AES_DECRYPT */
  7397. out:
  7398. wc_AesFree(enc);
  7399. wc_AesFree(dec);
  7400. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7401. if (enc)
  7402. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7403. #ifdef HAVE_AES_DECRYPT
  7404. if (dec)
  7405. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7406. #endif
  7407. #endif
  7408. #endif /* WOLFSSL_AES_256 */
  7409. return ret;
  7410. }
  7411. #endif /* WOLFSSL_AES_OFB */
  7412. #if defined(WOLFSSL_AES_CFB)
  7413. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  7414. static wc_test_ret_t aescfb_test(void)
  7415. {
  7416. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7417. Aes *enc = NULL;
  7418. #else
  7419. Aes enc[1];
  7420. #endif
  7421. int enc_inited = 0;
  7422. byte cipher[AES_BLOCK_SIZE * 4];
  7423. #ifdef HAVE_AES_DECRYPT
  7424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7425. Aes *dec = NULL;
  7426. #else
  7427. Aes dec[1];
  7428. #endif
  7429. int dec_inited = 0;
  7430. byte plain [AES_BLOCK_SIZE * 4];
  7431. #endif
  7432. wc_test_ret_t ret = 0;
  7433. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7434. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7435. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  7436. };
  7437. #ifdef WOLFSSL_AES_128
  7438. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7439. {
  7440. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7441. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7442. };
  7443. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7444. {
  7445. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  7446. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  7447. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  7448. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  7449. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  7450. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  7451. };
  7452. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7453. {
  7454. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7455. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7456. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7457. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7458. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7459. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  7460. };
  7461. #endif /* WOLFSSL_AES_128 */
  7462. #ifdef WOLFSSL_AES_192
  7463. /* 192 size key test */
  7464. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7465. {
  7466. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7467. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7468. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7469. };
  7470. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7471. {
  7472. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  7473. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  7474. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  7475. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  7476. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  7477. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  7478. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  7479. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  7480. };
  7481. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7482. {
  7483. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7484. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7485. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7486. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7487. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7488. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7489. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7490. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7491. };
  7492. #endif /* WOLFSSL_AES_192 */
  7493. #ifdef WOLFSSL_AES_256
  7494. /* 256 size key simple test */
  7495. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7496. {
  7497. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7498. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7499. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7500. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7501. };
  7502. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7503. {
  7504. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  7505. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  7506. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  7507. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  7508. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  7509. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  7510. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  7511. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  7512. };
  7513. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7514. {
  7515. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7516. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7517. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7518. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7519. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7520. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7521. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7522. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7523. };
  7524. #endif /* WOLFSSL_AES_256 */
  7525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7526. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7527. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7528. #ifdef HAVE_AES_DECRYPT
  7529. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7530. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7531. #endif
  7532. #endif
  7533. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7534. if (ret != 0)
  7535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7536. else
  7537. enc_inited = 1;
  7538. #ifdef HAVE_AES_DECRYPT
  7539. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7540. if (ret != 0)
  7541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7542. else
  7543. dec_inited = 1;
  7544. #endif
  7545. #ifdef WOLFSSL_AES_128
  7546. /* 128 key tests */
  7547. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7548. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7549. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  7550. cipher1, sizeof(cipher1));
  7551. if (ret != 0) {
  7552. return ret;
  7553. }
  7554. #endif
  7555. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7556. if (ret != 0)
  7557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7558. #ifdef HAVE_AES_DECRYPT
  7559. /* decrypt uses AES_ENCRYPTION */
  7560. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7561. if (ret != 0)
  7562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7563. #endif
  7564. XMEMSET(cipher, 0, sizeof(cipher));
  7565. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  7566. if (ret != 0)
  7567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7568. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7569. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7570. /* test restarting encryption process */
  7571. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  7572. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  7573. if (ret != 0)
  7574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7575. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  7576. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  7577. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7578. #ifdef HAVE_AES_DECRYPT
  7579. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  7580. if (ret != 0)
  7581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7582. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  7583. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7584. #endif /* HAVE_AES_DECRYPT */
  7585. #endif /* WOLFSSL_AES_128 */
  7586. #ifdef WOLFSSL_AES_192
  7587. /* 192 key size test */
  7588. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7589. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7590. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  7591. cipher2, sizeof(cipher2));
  7592. if (ret != 0) {
  7593. return ret;
  7594. }
  7595. #endif
  7596. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7597. if (ret != 0)
  7598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7599. #ifdef HAVE_AES_DECRYPT
  7600. /* decrypt uses AES_ENCRYPTION */
  7601. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7602. if (ret != 0)
  7603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7604. #endif
  7605. XMEMSET(cipher, 0, sizeof(cipher));
  7606. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  7607. if (ret != 0)
  7608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7609. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  7610. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7611. #ifdef HAVE_AES_DECRYPT
  7612. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  7613. if (ret != 0)
  7614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7615. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  7616. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7617. #endif /* HAVE_AES_DECRYPT */
  7618. #endif /* WOLFSSL_AES_192 */
  7619. #ifdef WOLFSSL_AES_256
  7620. /* 256 key size test */
  7621. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7622. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7623. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  7624. cipher3, sizeof(cipher3));
  7625. if (ret != 0) {
  7626. return ret;
  7627. }
  7628. #endif
  7629. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7630. if (ret != 0)
  7631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7632. #ifdef HAVE_AES_DECRYPT
  7633. /* decrypt uses AES_ENCRYPTION */
  7634. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7635. if (ret != 0)
  7636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7637. #endif
  7638. /* test with data left overs, magic lengths are checking near edges */
  7639. XMEMSET(cipher, 0, sizeof(cipher));
  7640. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  7641. if (ret != 0)
  7642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7643. if (XMEMCMP(cipher, cipher3, 4))
  7644. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7645. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  7646. if (ret != 0)
  7647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7648. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  7649. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7650. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  7651. (AES_BLOCK_SIZE * 4) - 31);
  7652. if (ret != 0)
  7653. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7654. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  7655. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7656. #ifdef HAVE_AES_DECRYPT
  7657. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  7658. if (ret != 0)
  7659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7660. if (XMEMCMP(plain, msg3, 4))
  7661. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7662. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  7663. if (ret != 0)
  7664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7665. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  7666. if (ret != 0)
  7667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7668. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  7669. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7670. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  7671. (AES_BLOCK_SIZE * 4) - 31);
  7672. if (ret != 0)
  7673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7674. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  7675. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7676. #endif /* HAVE_AES_DECRYPT */
  7677. #endif /* WOLFSSL_AES_256 */
  7678. out:
  7679. if (enc_inited)
  7680. wc_AesFree(enc);
  7681. if (dec_inited)
  7682. wc_AesFree(dec);
  7683. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7684. if (enc)
  7685. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7686. #ifdef HAVE_AES_DECRYPT
  7687. if (dec)
  7688. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7689. #endif
  7690. #endif
  7691. return ret;
  7692. }
  7693. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7694. static wc_test_ret_t aescfb1_test(void)
  7695. {
  7696. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7697. Aes *enc = NULL;
  7698. #else
  7699. Aes enc[1];
  7700. #endif
  7701. int enc_inited = 0;
  7702. byte cipher[AES_BLOCK_SIZE];
  7703. #ifdef HAVE_AES_DECRYPT
  7704. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7705. Aes *dec = NULL;
  7706. #else
  7707. Aes dec[1];
  7708. #endif
  7709. int dec_inited = 0;
  7710. byte plain [AES_BLOCK_SIZE];
  7711. #endif
  7712. wc_test_ret_t ret = 0;
  7713. #ifdef WOLFSSL_AES_128
  7714. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7715. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  7716. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  7717. };
  7718. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7719. {
  7720. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  7721. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  7722. };
  7723. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7724. {
  7725. 0x00
  7726. };
  7727. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7728. {
  7729. 0xC0
  7730. };
  7731. #endif /* WOLFSSL_AES_128 */
  7732. #ifdef WOLFSSL_AES_192
  7733. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7734. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  7735. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  7736. };
  7737. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7738. {
  7739. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  7740. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  7741. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  7742. };
  7743. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7744. {
  7745. 0x30
  7746. };
  7747. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7748. {
  7749. 0x80
  7750. };
  7751. #endif /* WOLFSSL_AES_192 */
  7752. #ifdef WOLFSSL_AES_256
  7753. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7754. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  7755. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  7756. };
  7757. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7758. {
  7759. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  7760. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  7761. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  7762. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  7763. };
  7764. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7765. {
  7766. 0xF7, 0x00
  7767. };
  7768. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7769. {
  7770. 0x41, 0xC0
  7771. };
  7772. #endif /* WOLFSSL_AES_256 */
  7773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7774. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7775. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7776. #ifdef HAVE_AES_DECRYPT
  7777. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7778. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7779. #endif
  7780. #endif
  7781. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7782. if (ret != 0)
  7783. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7784. else
  7785. enc_inited = 1;
  7786. #ifdef HAVE_AES_DECRYPT
  7787. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7788. if (ret != 0)
  7789. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7790. else
  7791. dec_inited = 1;
  7792. #endif
  7793. #ifdef WOLFSSL_AES_128
  7794. /* 128 key tests */
  7795. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7796. if (ret != 0)
  7797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7798. #ifdef HAVE_AES_DECRYPT
  7799. /* decrypt uses AES_ENCRYPTION */
  7800. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7801. if (ret != 0)
  7802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7803. #endif
  7804. XMEMSET(cipher, 0, sizeof(cipher));
  7805. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  7806. if (ret != 0)
  7807. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7808. if (cipher[0] != cipher1[0])
  7809. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7810. #ifdef HAVE_AES_DECRYPT
  7811. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  7812. if (ret != 0)
  7813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7814. if (plain[0] != msg1[0])
  7815. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7816. #endif /* HAVE_AES_DECRYPT */
  7817. #ifdef OPENSSL_EXTRA
  7818. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7819. if (ret != 0)
  7820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7821. XMEMSET(cipher, 0, sizeof(cipher));
  7822. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  7823. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  7824. if (ret != 0)
  7825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7826. #ifndef WOLFCRYPT_ONLY
  7827. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  7828. cipher, sizeof(msg1));
  7829. if (ret != 0) {
  7830. goto out;
  7831. }
  7832. #endif
  7833. #endif
  7834. #endif /* WOLFSSL_AES_128 */
  7835. #ifdef WOLFSSL_AES_192
  7836. /* 192 key tests */
  7837. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7838. if (ret != 0)
  7839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7840. XMEMSET(cipher, 0, sizeof(cipher));
  7841. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  7842. if (ret != 0)
  7843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7844. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7845. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7846. #ifdef OPENSSL_EXTRA
  7847. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7848. if (ret != 0)
  7849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7850. XMEMSET(cipher, 0, sizeof(cipher));
  7851. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7852. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7853. if (ret != 0)
  7854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7855. #ifndef WOLFCRYPT_ONLY
  7856. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7857. cipher, sizeof(msg2));
  7858. if (ret != 0) {
  7859. goto out;
  7860. }
  7861. #endif
  7862. #endif
  7863. #endif /* WOLFSSL_AES_192 */
  7864. #ifdef WOLFSSL_AES_256
  7865. /* 256 key tests */
  7866. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7867. if (ret != 0)
  7868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7869. XMEMSET(cipher, 0, sizeof(cipher));
  7870. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7871. if (ret != 0)
  7872. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7873. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7874. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7875. #ifdef OPENSSL_EXTRA
  7876. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7877. if (ret != 0)
  7878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7879. XMEMSET(cipher, 0, sizeof(cipher));
  7880. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7881. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7882. if (ret != 0)
  7883. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7884. #ifndef WOLFCRYPT_ONLY
  7885. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7886. cipher, sizeof(msg3));
  7887. if (ret != 0) {
  7888. goto out;
  7889. }
  7890. #endif
  7891. #endif
  7892. #endif /* WOLFSSL_AES_256 */
  7893. out:
  7894. if (enc_inited)
  7895. wc_AesFree(enc);
  7896. #ifdef HAVE_AES_DECRYPT
  7897. if (dec_inited)
  7898. wc_AesFree(dec);
  7899. #endif
  7900. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7901. if (enc)
  7902. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7903. #ifdef HAVE_AES_DECRYPT
  7904. if (dec)
  7905. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7906. #endif
  7907. #endif
  7908. return ret;
  7909. }
  7910. static wc_test_ret_t aescfb8_test(void)
  7911. {
  7912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7913. Aes *enc = NULL;
  7914. #else
  7915. Aes enc[1];
  7916. #endif
  7917. int enc_inited = 0;
  7918. byte cipher[AES_BLOCK_SIZE];
  7919. #ifdef HAVE_AES_DECRYPT
  7920. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7921. Aes *dec = NULL;
  7922. #else
  7923. Aes dec[1];
  7924. #endif
  7925. int dec_inited = 0;
  7926. byte plain [AES_BLOCK_SIZE];
  7927. #endif
  7928. wc_test_ret_t ret = 0;
  7929. #ifdef WOLFSSL_AES_128
  7930. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7931. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7932. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7933. };
  7934. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7935. {
  7936. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7937. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7938. };
  7939. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7940. {
  7941. 0xd2,0x76,0x91
  7942. };
  7943. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7944. {
  7945. 0xc9,0x06,0x35
  7946. };
  7947. #endif /* WOLFSSL_AES_128 */
  7948. #ifdef WOLFSSL_AES_192
  7949. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7950. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7951. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7952. };
  7953. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7954. {
  7955. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7956. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7957. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7958. };
  7959. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7960. {
  7961. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7962. 0xb5,0xe8
  7963. };
  7964. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7965. {
  7966. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7967. 0x3a,0x60
  7968. };
  7969. #endif
  7970. #ifdef WOLFSSL_AES_256
  7971. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7972. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7973. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7974. };
  7975. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7976. {
  7977. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7978. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7979. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7980. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7981. };
  7982. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7983. {
  7984. 0x1c,0xff,0x95
  7985. };
  7986. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7987. {
  7988. 0xb9,0x74,0xfa
  7989. };
  7990. #endif
  7991. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7992. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7993. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7994. #ifdef HAVE_AES_DECRYPT
  7995. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7996. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7997. #endif
  7998. #endif
  7999. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8000. if (ret != 0)
  8001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8002. else
  8003. enc_inited = 1;
  8004. #ifdef HAVE_AES_DECRYPT
  8005. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8006. if (ret != 0)
  8007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8008. else
  8009. dec_inited = 1;
  8010. #endif
  8011. #ifdef WOLFSSL_AES_128
  8012. /* 128 key tests */
  8013. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8014. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8015. cipher1, sizeof(cipher1));
  8016. if (ret != 0) {
  8017. return ret;
  8018. }
  8019. #endif
  8020. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8021. if (ret != 0)
  8022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8023. #ifdef HAVE_AES_DECRYPT
  8024. /* decrypt uses AES_ENCRYPTION */
  8025. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8026. if (ret != 0)
  8027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8028. #endif
  8029. XMEMSET(cipher, 0, sizeof(cipher));
  8030. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8031. if (ret != 0)
  8032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8033. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8034. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8035. #ifdef HAVE_AES_DECRYPT
  8036. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8037. if (ret != 0)
  8038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8039. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  8040. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8041. #endif /* HAVE_AES_DECRYPT */
  8042. #endif /* WOLFSSL_AES_128 */
  8043. #ifdef WOLFSSL_AES_192
  8044. /* 192 key tests */
  8045. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8046. if (ret != 0)
  8047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8048. XMEMSET(cipher, 0, sizeof(cipher));
  8049. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8050. if (ret != 0)
  8051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8052. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8053. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8054. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8055. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8056. cipher2, sizeof(msg2));
  8057. if (ret != 0) {
  8058. return ret;
  8059. }
  8060. #endif
  8061. #endif /* WOLFSSL_AES_192 */
  8062. #ifdef WOLFSSL_AES_256
  8063. /* 256 key tests */
  8064. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8065. if (ret != 0)
  8066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8067. XMEMSET(cipher, 0, sizeof(cipher));
  8068. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8069. if (ret != 0)
  8070. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8071. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8072. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8073. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8074. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8075. cipher3, sizeof(msg3));
  8076. if (ret != 0) {
  8077. goto out;
  8078. }
  8079. #endif
  8080. #endif /* WOLFSSL_AES_256 */
  8081. out:
  8082. if (enc_inited)
  8083. wc_AesFree(enc);
  8084. #ifdef HAVE_AES_DECRYPT
  8085. if (dec_inited)
  8086. wc_AesFree(dec);
  8087. #endif
  8088. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8089. if (enc)
  8090. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8091. #ifdef HAVE_AES_DECRYPT
  8092. if (dec)
  8093. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8094. #endif
  8095. #endif
  8096. return ret;
  8097. }
  8098. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8099. #endif /* WOLFSSL_AES_CFB */
  8100. static wc_test_ret_t aes_key_size_test(void)
  8101. {
  8102. wc_test_ret_t ret;
  8103. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8104. Aes *aes;
  8105. #else
  8106. Aes aes[1];
  8107. #endif
  8108. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8109. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8110. #ifndef WOLFSSL_CRYPTOCELL
  8111. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8112. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8113. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8114. #endif
  8115. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8116. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8117. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8118. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8119. byte iv[] = "1234567890abcdef";
  8120. #ifndef HAVE_FIPS
  8121. word32 keySize;
  8122. #endif
  8123. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8124. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8125. return WC_TEST_RET_ENC_ERRNO;
  8126. #endif
  8127. #if !defined(HAVE_FIPS) || \
  8128. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8129. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8130. * supported with that FIPS version */
  8131. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8132. if (ret != BAD_FUNC_ARG)
  8133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8134. #endif
  8135. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8136. /* 0 check OK for FIPSv1 */
  8137. if (ret != 0)
  8138. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8139. #ifndef HAVE_FIPS
  8140. /* Parameter Validation testing. */
  8141. ret = wc_AesGetKeySize(NULL, NULL);
  8142. if (ret != BAD_FUNC_ARG)
  8143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8144. ret = wc_AesGetKeySize(aes, NULL);
  8145. if (ret != BAD_FUNC_ARG)
  8146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8147. ret = wc_AesGetKeySize(NULL, &keySize);
  8148. if (ret != BAD_FUNC_ARG)
  8149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8150. /* Crashes in FIPS */
  8151. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8152. if (ret != BAD_FUNC_ARG)
  8153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8154. #endif
  8155. /* NULL IV indicates to use all zeros IV. */
  8156. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8157. #ifdef WOLFSSL_AES_128
  8158. if (ret != 0)
  8159. #else
  8160. if (ret != BAD_FUNC_ARG)
  8161. #endif
  8162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8163. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8164. if (ret != BAD_FUNC_ARG)
  8165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8166. /* CryptoCell handles rounds internally */
  8167. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8168. /* PSA don't use aes->rounds */
  8169. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8170. /* Force invalid rounds */
  8171. aes->rounds = 16;
  8172. ret = wc_AesGetKeySize(aes, &keySize);
  8173. if (ret != BAD_FUNC_ARG)
  8174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8175. #endif
  8176. #endif
  8177. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8178. #ifdef WOLFSSL_AES_128
  8179. if (ret != 0)
  8180. #else
  8181. if (ret != BAD_FUNC_ARG)
  8182. #endif
  8183. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8184. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8185. ret = wc_AesGetKeySize(aes, &keySize);
  8186. if (ret != 0 || keySize != sizeof(key16))
  8187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8188. #endif
  8189. #ifndef WOLFSSL_CRYPTOCELL
  8190. /* Cryptocell only supports AES-128 key size */
  8191. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8192. #ifdef WOLFSSL_AES_192
  8193. if (ret != 0)
  8194. #else
  8195. if (ret != BAD_FUNC_ARG)
  8196. #endif
  8197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8198. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8199. ret = wc_AesGetKeySize(aes, &keySize);
  8200. if (ret != 0 || keySize != sizeof(key24))
  8201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8202. #endif
  8203. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8204. #ifdef WOLFSSL_AES_256
  8205. if (ret != 0)
  8206. #else
  8207. if (ret != BAD_FUNC_ARG)
  8208. #endif
  8209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8210. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  8211. ret = wc_AesGetKeySize(aes, &keySize);
  8212. if (ret != 0 || keySize != sizeof(key32))
  8213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8214. #endif
  8215. #endif /* !WOLFSSL_CRYPTOCELL */
  8216. ret = 0; /* success */
  8217. out:
  8218. wc_AesFree(aes);
  8219. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8220. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8221. #endif
  8222. return ret;
  8223. }
  8224. #if defined(WOLFSSL_AES_XTS)
  8225. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  8226. #ifdef WOLFSSL_AES_128
  8227. static wc_test_ret_t aes_xts_128_test(void)
  8228. {
  8229. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8230. XtsAes *aes = NULL;
  8231. #else
  8232. XtsAes aes[1];
  8233. #endif
  8234. int aes_inited = 0;
  8235. wc_test_ret_t ret = 0;
  8236. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  8237. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  8238. /* 128 key tests */
  8239. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8240. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  8241. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  8242. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  8243. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  8244. };
  8245. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8246. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  8247. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8248. };
  8249. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8250. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8251. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  8252. };
  8253. /* plain text test of partial block is not from NIST test vector list */
  8254. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8255. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8256. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8257. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8258. };
  8259. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8260. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  8261. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  8262. };
  8263. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8264. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  8265. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  8266. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  8267. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  8268. };
  8269. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8270. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  8271. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  8272. };
  8273. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8274. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  8275. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  8276. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  8277. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  8278. };
  8279. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8280. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  8281. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  8282. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  8283. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  8284. };
  8285. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  8286. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  8287. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  8288. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  8289. };
  8290. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8291. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  8292. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8293. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8294. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8295. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8296. };
  8297. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  8298. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8299. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8300. };
  8301. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  8302. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8303. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8304. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8305. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8306. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  8307. };
  8308. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  8309. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  8310. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  8311. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  8312. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  8313. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  8314. };
  8315. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8316. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8317. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8318. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8319. #endif
  8320. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8321. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8322. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8323. if (ret != 0) {
  8324. printf("EVP_aes_128_xts failed!\n");
  8325. goto out;
  8326. }
  8327. #endif
  8328. XMEMSET(buf, 0, sizeof(buf));
  8329. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  8330. if (ret != 0)
  8331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8332. else
  8333. aes_inited = 1;
  8334. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  8335. if (ret != 0)
  8336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8337. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8338. #if defined(WOLFSSL_ASYNC_CRYPT)
  8339. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8340. #endif
  8341. if (ret != 0)
  8342. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8343. if (XMEMCMP(c2, buf, sizeof(c2)))
  8344. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8345. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8346. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8347. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8348. #if defined(WOLFSSL_ASYNC_CRYPT)
  8349. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8350. #endif
  8351. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8352. if (ret != 0)
  8353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8354. if (XMEMCMP(c2, buf, sizeof(c2)))
  8355. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8356. #endif
  8357. XMEMSET(buf, 0, sizeof(buf));
  8358. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8359. if (ret != 0)
  8360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8361. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8362. #if defined(WOLFSSL_ASYNC_CRYPT)
  8363. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8364. #endif
  8365. if (ret != 0)
  8366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8367. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8368. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8369. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8370. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8371. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8372. #if defined(WOLFSSL_ASYNC_CRYPT)
  8373. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8374. #endif
  8375. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8376. if (ret != 0)
  8377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8378. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8379. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8380. #endif
  8381. /* partial block encryption test */
  8382. XMEMSET(cipher, 0, sizeof(cipher));
  8383. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8384. #if defined(WOLFSSL_ASYNC_CRYPT)
  8385. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8386. #endif
  8387. if (ret != 0)
  8388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8389. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  8390. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8391. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8392. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8393. XMEMSET(cipher, 0, sizeof(cipher));
  8394. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8395. #if defined(WOLFSSL_ASYNC_CRYPT)
  8396. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8397. #endif
  8398. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8399. if (ret != 0)
  8400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8401. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  8402. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8403. #endif
  8404. /* partial block decrypt test */
  8405. XMEMSET(buf, 0, sizeof(buf));
  8406. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8407. if (ret != 0)
  8408. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8409. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8410. #if defined(WOLFSSL_ASYNC_CRYPT)
  8411. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8412. #endif
  8413. if (ret != 0)
  8414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8415. if (XMEMCMP(pp, buf, sizeof(pp)))
  8416. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8417. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8418. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8419. XMEMSET(buf, 0, sizeof(buf));
  8420. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8421. #if defined(WOLFSSL_ASYNC_CRYPT)
  8422. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8423. #endif
  8424. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8425. if (ret != 0)
  8426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8427. if (XMEMCMP(pp, buf, sizeof(pp)))
  8428. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8429. #endif
  8430. /* NIST decrypt test vector */
  8431. XMEMSET(buf, 0, sizeof(buf));
  8432. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8433. #if defined(WOLFSSL_ASYNC_CRYPT)
  8434. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8435. #endif
  8436. if (ret != 0)
  8437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8438. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8439. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8440. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8441. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8442. XMEMSET(buf, 0, sizeof(buf));
  8443. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8444. #if defined(WOLFSSL_ASYNC_CRYPT)
  8445. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8446. #endif
  8447. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8448. if (ret != 0)
  8449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8450. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8451. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8452. #endif
  8453. /* fail case with decrypting using wrong key */
  8454. XMEMSET(buf, 0, sizeof(buf));
  8455. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8456. #if defined(WOLFSSL_ASYNC_CRYPT)
  8457. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8458. #endif
  8459. if (ret != 0)
  8460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8461. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  8462. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8463. /* set correct key and retest */
  8464. XMEMSET(buf, 0, sizeof(buf));
  8465. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  8466. if (ret != 0)
  8467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8468. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8469. #if defined(WOLFSSL_ASYNC_CRYPT)
  8470. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8471. #endif
  8472. if (ret != 0)
  8473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8474. if (XMEMCMP(p2, buf, sizeof(p2)))
  8475. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8476. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8477. /* Test ciphertext stealing in-place. */
  8478. XMEMCPY(buf, p3, sizeof(p3));
  8479. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  8480. if (ret != 0)
  8481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8482. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  8483. #if defined(WOLFSSL_ASYNC_CRYPT)
  8484. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8485. #endif
  8486. if (ret != 0)
  8487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8488. if (XMEMCMP(c3, buf, sizeof(c3)))
  8489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8490. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  8491. if (ret != 0)
  8492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8493. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  8494. #if defined(WOLFSSL_ASYNC_CRYPT)
  8495. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8496. #endif
  8497. if (ret != 0)
  8498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8499. if (XMEMCMP(p3, buf, sizeof(p3)))
  8500. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8501. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8502. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  8503. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) && \
  8504. !defined(WOLFSSL_AFALG)
  8505. {
  8506. #define LARGE_XTS_SZ 1024
  8507. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8508. byte* large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  8509. DYNAMIC_TYPE_TMP_BUFFER);
  8510. #else
  8511. byte large_input[LARGE_XTS_SZ];
  8512. #endif
  8513. int i;
  8514. int j;
  8515. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8516. if (large_input == NULL)
  8517. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  8518. #endif
  8519. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  8520. large_input[i] = (byte)i;
  8521. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  8522. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8523. if (ret != 0)
  8524. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8525. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  8526. sizeof(i1));
  8527. #if defined(WOLFSSL_ASYNC_CRYPT)
  8528. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8529. #endif
  8530. if (ret != 0)
  8531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8532. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8533. if (ret != 0)
  8534. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8535. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  8536. sizeof(i1));
  8537. #if defined(WOLFSSL_ASYNC_CRYPT)
  8538. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8539. #endif
  8540. if (ret != 0)
  8541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8542. for (i = 0; i < j; i++) {
  8543. if (large_input[i] != (byte)i) {
  8544. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8545. }
  8546. }
  8547. }
  8548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8549. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8550. #endif
  8551. }
  8552. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  8553. * (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) &&
  8554. * !WOLFSSL_AFALG
  8555. */
  8556. out:
  8557. if (aes_inited)
  8558. wc_AesXtsFree(aes);
  8559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8560. if (aes)
  8561. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8562. #endif
  8563. return ret;
  8564. }
  8565. #endif /* WOLFSSL_AES_128 */
  8566. #ifdef WOLFSSL_AES_256
  8567. static wc_test_ret_t aes_xts_256_test(void)
  8568. {
  8569. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8570. XtsAes *aes = NULL;
  8571. #else
  8572. XtsAes aes[1];
  8573. #endif
  8574. int aes_inited = 0;
  8575. wc_test_ret_t ret = 0;
  8576. unsigned char buf[AES_BLOCK_SIZE * 3];
  8577. unsigned char cipher[AES_BLOCK_SIZE * 3];
  8578. /* 256 key tests */
  8579. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8580. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  8581. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  8582. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  8583. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  8584. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  8585. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  8586. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  8587. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  8588. };
  8589. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8590. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  8591. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  8592. };
  8593. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8594. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  8595. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  8596. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  8597. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  8598. };
  8599. /* plain text test of partial block is not from NIST test vector list */
  8600. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8601. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8602. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8603. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8604. };
  8605. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8606. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  8607. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  8608. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  8609. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  8610. };
  8611. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8612. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  8613. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  8614. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  8615. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  8616. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  8617. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  8618. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  8619. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  8620. };
  8621. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8622. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  8623. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  8624. };
  8625. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8626. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  8627. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  8628. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  8629. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  8630. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  8631. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  8632. };
  8633. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8634. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  8635. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  8636. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  8637. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  8638. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  8639. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  8640. };
  8641. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8642. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8643. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8644. #endif
  8645. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8646. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8647. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8648. if (ret != 0) {
  8649. printf("EVP_aes_256_xts failed\n");
  8650. goto out;
  8651. }
  8652. #endif
  8653. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  8654. if (ret != 0)
  8655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8656. else
  8657. aes_inited = 1;
  8658. XMEMSET(buf, 0, sizeof(buf));
  8659. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  8660. if (ret != 0)
  8661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8662. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8663. #if defined(WOLFSSL_ASYNC_CRYPT)
  8664. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8665. #endif
  8666. if (ret != 0)
  8667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8668. if (XMEMCMP(c2, buf, sizeof(c2)))
  8669. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8670. XMEMSET(buf, 0, sizeof(buf));
  8671. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8672. if (ret != 0)
  8673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8674. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8675. #if defined(WOLFSSL_ASYNC_CRYPT)
  8676. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8677. #endif
  8678. if (ret != 0)
  8679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8680. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8681. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8682. /* partial block encryption test */
  8683. XMEMSET(cipher, 0, sizeof(cipher));
  8684. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8685. #if defined(WOLFSSL_ASYNC_CRYPT)
  8686. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8687. #endif
  8688. if (ret != 0)
  8689. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8690. /* partial block decrypt test */
  8691. XMEMSET(buf, 0, sizeof(buf));
  8692. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8693. if (ret != 0)
  8694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8695. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8696. #if defined(WOLFSSL_ASYNC_CRYPT)
  8697. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8698. #endif
  8699. if (ret != 0)
  8700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8701. if (XMEMCMP(pp, buf, sizeof(pp)))
  8702. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8703. /* NIST decrypt test vector */
  8704. XMEMSET(buf, 0, sizeof(buf));
  8705. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8706. #if defined(WOLFSSL_ASYNC_CRYPT)
  8707. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8708. #endif
  8709. if (ret != 0)
  8710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8711. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8712. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8713. XMEMSET(buf, 0, sizeof(buf));
  8714. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  8715. if (ret != 0)
  8716. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8717. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8718. #if defined(WOLFSSL_ASYNC_CRYPT)
  8719. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8720. #endif
  8721. if (ret != 0)
  8722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8723. if (XMEMCMP(p2, buf, sizeof(p2)))
  8724. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8725. out:
  8726. if (aes_inited)
  8727. wc_AesXtsFree(aes);
  8728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8729. if (aes)
  8730. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8731. #endif
  8732. return ret;
  8733. }
  8734. #endif /* WOLFSSL_AES_256 */
  8735. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8736. /* both 128 and 256 bit key test */
  8737. static wc_test_ret_t aes_xts_sector_test(void)
  8738. {
  8739. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8740. XtsAes *aes = NULL;
  8741. #else
  8742. XtsAes aes[1];
  8743. #endif
  8744. int aes_inited = 0;
  8745. wc_test_ret_t ret = 0;
  8746. unsigned char buf[AES_BLOCK_SIZE * 2];
  8747. /* 128 key tests */
  8748. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8749. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  8750. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  8751. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  8752. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  8753. };
  8754. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8755. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  8756. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  8757. };
  8758. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8759. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  8760. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  8761. };
  8762. word64 s1 = 141;
  8763. /* 256 key tests */
  8764. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8765. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  8766. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  8767. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  8768. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  8769. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  8770. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  8771. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  8772. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  8773. };
  8774. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8775. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  8776. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  8777. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  8778. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  8779. };
  8780. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8781. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  8782. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  8783. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  8784. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  8785. };
  8786. word64 s2 = 187;
  8787. #if !defined(BENCH_EMBEDDED) && \
  8788. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  8789. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  8790. word32 sectorSz = 512;
  8791. unsigned char data[550];
  8792. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  8793. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  8794. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  8795. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  8796. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  8797. };
  8798. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  8799. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  8800. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8801. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  8802. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  8803. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  8804. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  8805. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  8806. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  8807. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  8808. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  8809. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  8810. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  8811. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  8812. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  8813. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  8814. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  8815. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  8816. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  8817. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  8818. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  8819. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  8820. 0xfc, 0xfd, 0xfe, 0xff,
  8821. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  8822. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8823. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  8824. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  8825. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  8826. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  8827. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  8828. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  8829. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  8830. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  8831. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  8832. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  8833. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  8834. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  8835. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  8836. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  8837. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  8838. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  8839. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  8840. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  8841. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  8842. 0xfc, 0xfd, 0xfe, 0xff,
  8843. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  8844. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8845. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  8846. };
  8847. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  8848. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  8849. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  8850. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  8851. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  8852. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  8853. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  8854. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  8855. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  8856. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  8857. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  8858. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  8859. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  8860. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  8861. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  8862. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  8863. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  8864. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  8865. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  8866. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  8867. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  8868. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  8869. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  8870. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  8871. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  8872. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  8873. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  8874. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  8875. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  8876. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  8877. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  8878. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  8879. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  8880. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  8881. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  8882. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  8883. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  8884. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  8885. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  8886. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  8887. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  8888. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  8889. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  8890. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  8891. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  8892. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  8893. 0x24, 0xe7, 0x3d, 0x6f
  8894. };
  8895. word64 s3 = 0x000000ffffffffff;
  8896. #endif
  8897. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8898. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8899. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8900. #endif
  8901. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  8902. if (ret != 0)
  8903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8904. else
  8905. aes_inited = 1;
  8906. XMEMSET(buf, 0, sizeof(buf));
  8907. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8908. if (ret != 0)
  8909. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8910. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  8911. #if defined(WOLFSSL_ASYNC_CRYPT)
  8912. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8913. #endif
  8914. if (ret != 0)
  8915. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8916. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8917. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8918. /* decrypt test */
  8919. XMEMSET(buf, 0, sizeof(buf));
  8920. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8921. if (ret != 0)
  8922. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8923. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  8924. #if defined(WOLFSSL_ASYNC_CRYPT)
  8925. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8926. #endif
  8927. if (ret != 0)
  8928. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8929. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8930. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8931. /* 256 bit key tests */
  8932. XMEMSET(buf, 0, sizeof(buf));
  8933. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  8934. if (ret != 0)
  8935. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8936. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  8937. #if defined(WOLFSSL_ASYNC_CRYPT)
  8938. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8939. #endif
  8940. if (ret != 0)
  8941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8942. if (XMEMCMP(c2, buf, sizeof(c2)))
  8943. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8944. /* decrypt test */
  8945. XMEMSET(buf, 0, sizeof(buf));
  8946. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  8947. if (ret != 0)
  8948. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8949. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  8950. #if defined(WOLFSSL_ASYNC_CRYPT)
  8951. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8952. #endif
  8953. if (ret != 0)
  8954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8955. if (XMEMCMP(p2, buf, sizeof(p2)))
  8956. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8957. #if !defined(BENCH_EMBEDDED) && \
  8958. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  8959. /* encrypt consecutive sectors test */
  8960. XMEMSET(data, 0, sizeof(buf));
  8961. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  8962. if (ret != 0)
  8963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8964. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  8965. sizeof(p3), s3, sectorSz);
  8966. #if defined(WOLFSSL_ASYNC_CRYPT)
  8967. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8968. #endif
  8969. if (ret != 0)
  8970. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8971. if (XMEMCMP(c3, data, sizeof(c3)))
  8972. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8973. /* decrypt consecutive sectors test */
  8974. XMEMSET(data, 0, sizeof(buf));
  8975. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  8976. if (ret != 0)
  8977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8978. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  8979. sizeof(c3), s3, sectorSz);
  8980. #if defined(WOLFSSL_ASYNC_CRYPT)
  8981. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8982. #endif
  8983. if (ret != 0)
  8984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8985. if (XMEMCMP(p3, data, sizeof(p3)))
  8986. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8987. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  8988. out:
  8989. if (aes_inited)
  8990. wc_AesXtsFree(aes);
  8991. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8992. if (aes)
  8993. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8994. #endif
  8995. return ret;
  8996. }
  8997. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  8998. #ifdef WOLFSSL_AES_128
  8999. /* testing of bad arguments */
  9000. static wc_test_ret_t aes_xts_args_test(void)
  9001. {
  9002. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9003. XtsAes *aes = NULL;
  9004. #else
  9005. XtsAes aes[1];
  9006. #endif
  9007. int aes_inited = 0;
  9008. wc_test_ret_t ret;
  9009. unsigned char buf[AES_BLOCK_SIZE * 2];
  9010. /* 128 key tests */
  9011. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9012. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  9013. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  9014. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  9015. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  9016. };
  9017. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9018. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  9019. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  9020. };
  9021. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9022. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  9023. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  9024. };
  9025. word64 s1 = 141;
  9026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9027. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9028. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9029. #endif
  9030. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9031. if (ret != 0)
  9032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9033. else
  9034. aes_inited = 1;
  9035. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  9036. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9037. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  9038. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9039. /* encryption operations */
  9040. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9041. if (ret != 0)
  9042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9043. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  9044. #if defined(WOLFSSL_ASYNC_CRYPT)
  9045. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9046. #endif
  9047. if (ret == 0)
  9048. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9049. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  9050. #if defined(WOLFSSL_ASYNC_CRYPT)
  9051. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9052. #endif
  9053. if (ret == 0)
  9054. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9055. /* decryption operations */
  9056. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9057. if (ret != 0)
  9058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9059. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  9060. #if defined(WOLFSSL_ASYNC_CRYPT)
  9061. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9062. #endif
  9063. if (ret == 0)
  9064. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9065. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  9066. #if defined(WOLFSSL_ASYNC_CRYPT)
  9067. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9068. #endif
  9069. if (ret == 0)
  9070. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9071. ret = 0;
  9072. out:
  9073. if (aes_inited)
  9074. wc_AesXtsFree(aes);
  9075. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9076. if (aes)
  9077. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9078. #endif
  9079. return ret;
  9080. }
  9081. #endif /* WOLFSSL_AES_128 */
  9082. #endif /* WOLFSSL_AES_XTS */
  9083. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  9084. static wc_test_ret_t aes_cbc_test(void)
  9085. {
  9086. byte cipher[AES_BLOCK_SIZE];
  9087. byte plain[AES_BLOCK_SIZE];
  9088. wc_test_ret_t ret;
  9089. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  9090. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9091. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9092. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9093. };
  9094. byte key[] = "0123456789abcdef "; /* align */
  9095. byte iv[] = "1234567890abcdef "; /* align */
  9096. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9097. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9098. /* Parameter Validation testing. */
  9099. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  9100. if (ret != BAD_FUNC_ARG)
  9101. return WC_TEST_RET_ENC_EC(ret);
  9102. #ifdef HAVE_AES_DECRYPT
  9103. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  9104. if (ret != BAD_FUNC_ARG)
  9105. return WC_TEST_RET_ENC_EC(ret);
  9106. #endif
  9107. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  9108. AES_BLOCK_SIZE, iv);
  9109. if (ret != 0)
  9110. return WC_TEST_RET_ENC_EC(ret);
  9111. #ifdef HAVE_AES_DECRYPT
  9112. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  9113. AES_BLOCK_SIZE, iv);
  9114. if (ret != 0)
  9115. return WC_TEST_RET_ENC_EC(ret);
  9116. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  9117. return WC_TEST_RET_ENC_NC;
  9118. #endif /* HAVE_AES_DECRYPT */
  9119. (void)plain;
  9120. return 0;
  9121. }
  9122. #endif
  9123. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9124. static wc_test_ret_t aesecb_test(void)
  9125. {
  9126. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9127. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9128. #else
  9129. Aes enc[1];
  9130. #endif
  9131. int enc_inited = 0;
  9132. byte cipher[AES_BLOCK_SIZE * 4];
  9133. #ifdef HAVE_AES_DECRYPT
  9134. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9135. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9136. #else
  9137. Aes dec[1];
  9138. #endif
  9139. int dec_inited = 0;
  9140. byte plain [AES_BLOCK_SIZE * 4];
  9141. #endif /* HAVE_AES_DECRYPT */
  9142. wc_test_ret_t ret = 0;
  9143. #if defined(WOLFSSL_AES_256)
  9144. {
  9145. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  9146. {
  9147. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9148. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9149. };
  9150. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  9151. {
  9152. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  9153. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  9154. };
  9155. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  9156. {
  9157. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9158. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9159. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9160. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9161. };
  9162. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9163. if (ret != 0)
  9164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9165. enc_inited = 1;
  9166. #if defined(HAVE_AES_DECRYPT)
  9167. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9168. if (ret != 0)
  9169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9170. dec_inited = 1;
  9171. #endif
  9172. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9173. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9174. if (ret != 0)
  9175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9176. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  9177. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9178. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9179. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9180. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  9181. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9182. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9183. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9184. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9185. if (ret != 0)
  9186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9187. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9188. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9189. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9190. if (ret != 0)
  9191. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9192. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9193. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9194. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9195. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9196. if (ret != 0)
  9197. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9198. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9199. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9200. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9201. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9202. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9203. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9204. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9205. if (ret != 0)
  9206. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9207. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9208. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9209. #endif
  9210. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9211. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9212. if (ret != 0)
  9213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9214. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  9215. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9216. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9217. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9218. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  9219. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9220. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9221. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9222. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9223. if (ret != 0)
  9224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9225. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9226. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9227. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9228. if (ret != 0)
  9229. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9230. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9231. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9232. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9233. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9234. if (ret != 0)
  9235. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9236. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9237. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9238. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9239. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9240. if (ret != 0)
  9241. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9242. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9243. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9244. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9245. if (ret != 0)
  9246. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9247. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9248. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9249. #endif
  9250. }
  9251. out:
  9252. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9253. if (enc) {
  9254. if (enc_inited)
  9255. wc_AesFree(enc);
  9256. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9257. }
  9258. #ifdef HAVE_AES_DECRYPT
  9259. if (dec) {
  9260. if (dec_inited)
  9261. wc_AesFree(dec);
  9262. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9263. }
  9264. #endif
  9265. #else
  9266. if (enc_inited)
  9267. wc_AesFree(enc);
  9268. #ifdef HAVE_AES_DECRYPT
  9269. if (dec_inited)
  9270. wc_AesFree(dec);
  9271. #endif
  9272. #endif
  9273. #endif /* WOLFSSL_AES_256 */
  9274. return ret;
  9275. }
  9276. #endif /* HAVE_AES_ECB */
  9277. #ifdef WOLFSSL_AES_COUNTER
  9278. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  9279. {
  9280. wc_test_ret_t ret = 0;
  9281. /* test vectors from "Recommendation for Block Cipher Modes of
  9282. * Operation" NIST Special Publication 800-38A */
  9283. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  9284. {
  9285. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  9286. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  9287. };
  9288. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  9289. {
  9290. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9291. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  9292. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  9293. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  9294. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  9295. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  9296. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  9297. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  9298. };
  9299. #ifdef WOLFSSL_ARMASM
  9300. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  9301. {
  9302. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  9303. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9304. };
  9305. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  9306. {
  9307. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9308. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  9309. };
  9310. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  9311. {
  9312. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9313. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9314. };
  9315. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  9316. {
  9317. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  9318. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9319. };
  9320. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  9321. {
  9322. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9323. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  9324. };
  9325. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  9326. {
  9327. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  9328. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9329. };
  9330. #endif
  9331. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  9332. {
  9333. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9334. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9335. };
  9336. #ifdef WOLFSSL_ARMASM
  9337. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  9338. {
  9339. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9340. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9341. };
  9342. #endif
  9343. #ifdef WOLFSSL_AES_128
  9344. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  9345. {
  9346. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  9347. 0xc2
  9348. };
  9349. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  9350. {
  9351. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  9352. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  9353. };
  9354. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  9355. {
  9356. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  9357. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  9358. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  9359. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  9360. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  9361. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  9362. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  9363. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  9364. };
  9365. #ifdef WOLFSSL_ARMASM
  9366. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  9367. {
  9368. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  9369. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  9370. 0x7e,0xad,0xea,0x5c,0xd1
  9371. };
  9372. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  9373. {
  9374. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  9375. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  9376. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  9377. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  9378. };
  9379. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  9380. {
  9381. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  9382. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  9383. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  9384. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  9385. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  9386. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  9387. };
  9388. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  9389. {
  9390. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  9391. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  9392. 0xae,0x74,0x94,0x72,0x8e
  9393. };
  9394. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  9395. {
  9396. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  9397. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  9398. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  9399. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  9400. };
  9401. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  9402. {
  9403. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  9404. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  9405. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  9406. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  9407. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  9408. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  9409. };
  9410. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  9411. {
  9412. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  9413. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  9414. 0x9d,0x77,0xc9,0x5f,0x38
  9415. };
  9416. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  9417. {
  9418. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  9419. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  9420. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  9421. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  9422. };
  9423. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  9424. {
  9425. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  9426. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  9427. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  9428. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  9429. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  9430. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  9431. };
  9432. #endif
  9433. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  9434. {
  9435. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  9436. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  9437. 0xd3,0xda,0xe1,0x5b,0x04
  9438. };
  9439. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  9440. {
  9441. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  9442. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  9443. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  9444. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  9445. };
  9446. #ifdef WOLFSSL_ARMASM
  9447. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  9448. {
  9449. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  9450. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  9451. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  9452. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  9453. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  9454. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  9455. };
  9456. #endif
  9457. #endif /* WOLFSSL_AES_128 */
  9458. #ifdef WOLFSSL_AES_192
  9459. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  9460. {
  9461. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9462. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9463. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9464. };
  9465. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  9466. {
  9467. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  9468. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  9469. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  9470. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  9471. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  9472. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  9473. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  9474. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  9475. };
  9476. #ifdef WOLFSSL_ARMASM
  9477. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  9478. {
  9479. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9480. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9481. 0x0c,0x33,0x97,0x06,0xc0
  9482. };
  9483. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  9484. {
  9485. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9486. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9487. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  9488. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  9489. };
  9490. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  9491. {
  9492. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  9493. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  9494. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  9495. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  9496. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  9497. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  9498. };
  9499. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  9500. {
  9501. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9502. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9503. 0x3d,0xf7,0xb4,0xfd,0x8c
  9504. };
  9505. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  9506. {
  9507. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9508. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9509. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  9510. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  9511. };
  9512. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  9513. {
  9514. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  9515. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  9516. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  9517. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  9518. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  9519. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  9520. };
  9521. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  9522. {
  9523. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9524. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9525. 0x47,0xb5,0xf3,0x30,0x3b
  9526. };
  9527. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  9528. {
  9529. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9530. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9531. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  9532. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  9533. };
  9534. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  9535. {
  9536. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  9537. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  9538. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  9539. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  9540. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  9541. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  9542. };
  9543. #endif
  9544. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  9545. {
  9546. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9547. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9548. 0x8c,0x68,0xa7,0xd9,0x57
  9549. };
  9550. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  9551. {
  9552. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9553. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9554. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  9555. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  9556. };
  9557. #ifdef WOLFSSL_ARMASM
  9558. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  9559. {
  9560. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  9561. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  9562. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  9563. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  9564. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  9565. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  9566. };
  9567. #endif
  9568. #endif
  9569. #ifdef WOLFSSL_AES_256
  9570. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  9571. {
  9572. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9573. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9574. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9575. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9576. };
  9577. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  9578. {
  9579. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  9580. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  9581. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  9582. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  9583. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  9584. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  9585. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  9586. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  9587. };
  9588. #ifdef WOLFSSL_ARMASM
  9589. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  9590. {
  9591. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9592. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9593. 0xa2,0x13,0x47,0x74,0xda
  9594. };
  9595. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  9596. {
  9597. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9598. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9599. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  9600. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  9601. };
  9602. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  9603. {
  9604. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  9605. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  9606. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  9607. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  9608. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  9609. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  9610. };
  9611. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  9612. {
  9613. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9614. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9615. 0xd4,0x94,0x4c,0xcd,0x4d
  9616. };
  9617. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  9618. {
  9619. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9620. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9621. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  9622. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  9623. };
  9624. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  9625. {
  9626. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  9627. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  9628. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  9629. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  9630. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  9631. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  9632. };
  9633. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  9634. {
  9635. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9636. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9637. 0xb8,0x69,0xa1,0x48,0x45
  9638. };
  9639. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  9640. {
  9641. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9642. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9643. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  9644. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  9645. };
  9646. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  9647. {
  9648. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  9649. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  9650. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  9651. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  9652. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  9653. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  9654. };
  9655. #endif
  9656. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  9657. {
  9658. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9659. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9660. 0x4b,0x45,0x7c,0xd6,0x8a
  9661. };
  9662. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  9663. {
  9664. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9665. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9666. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  9667. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  9668. };
  9669. #ifdef WOLFSSL_ARMASM
  9670. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  9671. {
  9672. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  9673. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  9674. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  9675. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  9676. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  9677. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  9678. };
  9679. #endif
  9680. #endif
  9681. int i;
  9682. struct {
  9683. const byte* key;
  9684. int keySz;
  9685. const byte* iv;
  9686. const byte* plain;
  9687. int len;
  9688. const byte* cipher;
  9689. } testVec[] = {
  9690. #ifdef WOLFSSL_AES_128
  9691. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9692. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  9693. /* let's try with just 9 bytes, non block size test */
  9694. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9695. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  9696. /* and an additional 9 bytes to reuse tmp left buffer */
  9697. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  9698. /* Counter wrapping */
  9699. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9700. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  9701. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9702. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  9703. ctr128Wrap128CipherLong },
  9704. #ifdef WOLFSSL_ARMASM
  9705. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  9706. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  9707. ctr128Wrap128_2CipherLong },
  9708. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9709. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  9710. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9711. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  9712. ctr128Wrap96CipherLong },
  9713. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  9714. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  9715. ctr128Wrap96_2CipherLong },
  9716. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9717. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  9718. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9719. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  9720. ctr128Wrap64CipherLong },
  9721. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  9722. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  9723. ctr128Wrap64_2CipherLong },
  9724. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9725. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  9726. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9727. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  9728. ctr128Wrap32CipherLong },
  9729. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  9730. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  9731. ctr128Wrap32_2CipherLong },
  9732. #endif
  9733. #endif
  9734. #ifdef WOLFSSL_AES_192
  9735. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9736. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  9737. /* let's try with just 9 bytes, non block size test */
  9738. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9739. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  9740. /* Counter wrapping */
  9741. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9742. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  9743. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9744. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  9745. ctr192Wrap128CipherLong },
  9746. #ifdef WOLFSSL_ARMASM
  9747. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  9748. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  9749. ctr192Wrap128_2CipherLong },
  9750. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9751. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  9752. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9753. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  9754. ctr192Wrap96CipherLong },
  9755. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  9756. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  9757. ctr192Wrap96_2CipherLong },
  9758. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9759. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  9760. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9761. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  9762. ctr192Wrap64CipherLong },
  9763. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  9764. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  9765. ctr192Wrap64_2CipherLong },
  9766. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9767. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  9768. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9769. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  9770. ctr192Wrap32CipherLong },
  9771. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  9772. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  9773. ctr192Wrap32_2CipherLong },
  9774. #endif
  9775. #endif
  9776. #ifdef WOLFSSL_AES_256
  9777. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9778. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  9779. /* let's try with just 9 bytes, non block size test */
  9780. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9781. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  9782. /* Counter wrapping */
  9783. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9784. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  9785. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9786. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  9787. ctr256Wrap128CipherLong },
  9788. #ifdef WOLFSSL_ARMASM
  9789. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  9790. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  9791. ctr256Wrap128_2CipherLong },
  9792. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9793. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  9794. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9795. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  9796. ctr256Wrap96CipherLong },
  9797. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  9798. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  9799. ctr256Wrap96_2CipherLong },
  9800. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9801. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  9802. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9803. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  9804. ctr256Wrap64CipherLong },
  9805. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  9806. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  9807. ctr256Wrap64_2CipherLong },
  9808. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9809. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  9810. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9811. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  9812. ctr256Wrap32CipherLong },
  9813. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  9814. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  9815. ctr256Wrap32_2CipherLong },
  9816. #endif
  9817. #endif
  9818. };
  9819. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  9820. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  9821. if (testVec[i].key != NULL) {
  9822. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  9823. testVec[i].iv, AES_ENCRYPTION);
  9824. if (ret != 0) {
  9825. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9826. }
  9827. /* Ctr only uses encrypt, even on key setup */
  9828. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  9829. testVec[i].iv, AES_ENCRYPTION);
  9830. if (ret != 0) {
  9831. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9832. }
  9833. }
  9834. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  9835. if (ret != 0) {
  9836. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9837. }
  9838. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  9839. if (ret != 0) {
  9840. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9841. }
  9842. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  9843. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9844. }
  9845. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  9846. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  9847. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9848. }
  9849. #endif
  9850. }
  9851. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  9852. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  9853. if (testVec[i].key != NULL) {
  9854. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  9855. testVec[i].iv, AES_ENCRYPTION);
  9856. if (ret != 0) {
  9857. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9858. }
  9859. /* Ctr only uses encrypt, even on key setup */
  9860. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  9861. testVec[i].iv, AES_ENCRYPTION);
  9862. if (ret != 0) {
  9863. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9864. }
  9865. }
  9866. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9867. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  9868. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9869. if (ret != 0) {
  9870. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9871. }
  9872. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9873. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  9874. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9875. if (ret != 0) {
  9876. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9877. }
  9878. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  9879. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9880. }
  9881. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  9882. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  9883. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9884. }
  9885. #endif
  9886. }
  9887. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  9888. if (testVec[i].key != NULL) {
  9889. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9890. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  9891. testVec[i].iv, AES_ENCRYPTION);
  9892. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9893. if (ret != 0) {
  9894. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9895. }
  9896. /* Ctr only uses encrypt, even on key setup */
  9897. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9898. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  9899. testVec[i].iv, AES_ENCRYPTION);
  9900. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9901. if (ret != 0) {
  9902. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9903. }
  9904. }
  9905. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  9906. if (ret != 0) {
  9907. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9908. }
  9909. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  9910. if (ret != 0) {
  9911. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9912. }
  9913. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  9914. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9915. }
  9916. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  9917. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  9918. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9919. }
  9920. #endif
  9921. }
  9922. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_AES_C_DYNAMIC_FALLBACK */
  9923. out:
  9924. return ret;
  9925. }
  9926. #endif /* WOLFSSL_AES_COUNTER */
  9927. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  9928. {
  9929. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9931. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9932. #else
  9933. Aes enc[1];
  9934. #endif
  9935. int enc_inited = 0;
  9936. byte cipher[AES_BLOCK_SIZE * 4];
  9937. #ifdef HAVE_AES_DECRYPT
  9938. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9939. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9940. #else
  9941. Aes dec[1];
  9942. #endif
  9943. int dec_inited = 0;
  9944. byte plain [AES_BLOCK_SIZE * 4];
  9945. #endif /* HAVE_AES_DECRYPT */
  9946. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9947. wc_test_ret_t ret = 0;
  9948. #ifdef HAVE_AES_CBC
  9949. #ifdef WOLFSSL_AES_128
  9950. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  9951. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9952. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9953. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9954. };
  9955. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9956. {
  9957. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  9958. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  9959. };
  9960. #ifdef HAVE_RENESAS_SYNC
  9961. const byte *key =
  9962. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  9963. #else
  9964. WOLFSSL_SMALL_STACK_STATIC const
  9965. byte key[] = "0123456789abcdef "; /* align */
  9966. #endif
  9967. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  9968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9969. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9970. if (enc == NULL)
  9971. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9972. #endif
  9973. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9974. if (dec == NULL)
  9975. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9976. #endif
  9977. #endif
  9978. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9979. if (ret != 0)
  9980. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9981. enc_inited = 1;
  9982. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9983. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9984. if (ret != 0)
  9985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9986. dec_inited = 1;
  9987. #endif
  9988. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  9989. if (ret != 0)
  9990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9991. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9992. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  9993. if (ret != 0)
  9994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9995. #endif
  9996. #ifdef HAVE_AES_ECB
  9997. {
  9998. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb[AES_BLOCK_SIZE] = {
  9999. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  10000. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  10001. };
  10002. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  10003. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  10004. #if defined(WOLFSSL_ASYNC_CRYPT)
  10005. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10006. #endif
  10007. if (ret != 0)
  10008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10009. if (XMEMCMP(cipher, verify_ecb, AES_BLOCK_SIZE))
  10010. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10011. #ifdef HAVE_AES_DECRYPT
  10012. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  10013. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  10014. #if defined(WOLFSSL_ASYNC_CRYPT)
  10015. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10016. #endif
  10017. if (ret != 0)
  10018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10019. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10020. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10021. #endif /* HAVE_AES_DECRYPT */
  10022. }
  10023. #endif
  10024. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  10025. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  10026. #if defined(WOLFSSL_ASYNC_CRYPT)
  10027. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10028. #endif
  10029. if (ret != 0)
  10030. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10031. #ifdef HAVE_AES_DECRYPT
  10032. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  10033. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  10034. #if defined(WOLFSSL_ASYNC_CRYPT)
  10035. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10036. #endif
  10037. if (ret != 0)
  10038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10039. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10040. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10041. #endif /* HAVE_AES_DECRYPT */
  10042. /* skipped because wrapped key use in case of renesas sm */
  10043. #ifndef HAVE_RENESAS_SYNC
  10044. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  10045. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10046. #endif
  10047. #endif /* WOLFSSL_AES_128 */
  10048. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  10049. {
  10050. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  10051. /* "All work and no play makes Jack a dull boy. " */
  10052. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10053. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10054. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10055. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10056. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10057. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10058. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10059. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10060. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10061. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10062. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10063. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10064. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10065. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10066. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10067. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10068. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10069. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10070. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10071. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10072. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10073. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10074. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10075. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10076. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10077. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10078. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10079. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10080. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10081. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10082. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10083. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10084. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10085. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10086. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10087. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10088. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10089. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10090. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10091. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10092. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10093. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10094. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10095. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10096. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10097. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10098. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10099. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  10100. };
  10101. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  10102. word32 keySz, msgSz;
  10103. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10104. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10105. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10106. if ((bigCipher == NULL) ||
  10107. (bigPlain == NULL)) {
  10108. if (bigCipher != NULL)
  10109. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10110. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10111. }
  10112. #else
  10113. byte bigCipher[sizeof(bigMsg)];
  10114. byte bigPlain[sizeof(bigMsg)];
  10115. #endif
  10116. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  10117. * message by AES_BLOCK_SIZE for each size of AES key. */
  10118. for (keySz = 16; keySz <= 32; keySz += 8) {
  10119. for (msgSz = AES_BLOCK_SIZE;
  10120. msgSz <= sizeof(bigMsg);
  10121. msgSz += AES_BLOCK_SIZE) {
  10122. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  10123. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  10124. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  10125. if (ret != 0) {
  10126. ret = WC_TEST_RET_ENC_EC(ret);
  10127. break;
  10128. }
  10129. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  10130. if (ret != 0) {
  10131. ret = WC_TEST_RET_ENC_EC(ret);
  10132. break;
  10133. }
  10134. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  10135. #if defined(WOLFSSL_ASYNC_CRYPT)
  10136. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10137. #endif
  10138. if (ret != 0) {
  10139. ret = WC_TEST_RET_ENC_EC(ret);
  10140. break;
  10141. }
  10142. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  10143. #if defined(WOLFSSL_ASYNC_CRYPT)
  10144. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10145. #endif
  10146. if (ret != 0) {
  10147. ret = WC_TEST_RET_ENC_EC(ret);
  10148. break;
  10149. }
  10150. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  10151. ret = WC_TEST_RET_ENC_NC;
  10152. break;
  10153. }
  10154. }
  10155. if (ret != 0)
  10156. break;
  10157. }
  10158. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10159. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  10160. * message by AES_BLOCK_SIZE for each size of AES key. */
  10161. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10162. for (keySz = 16; keySz <= 32; keySz += 8) {
  10163. for (msgSz = AES_BLOCK_SIZE;
  10164. msgSz <= sizeof(bigMsg);
  10165. msgSz += AES_BLOCK_SIZE) {
  10166. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  10167. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  10168. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  10169. if (ret != 0) {
  10170. ret = WC_TEST_RET_ENC_EC(ret);
  10171. break;
  10172. }
  10173. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  10174. if (ret != 0) {
  10175. ret = WC_TEST_RET_ENC_EC(ret);
  10176. break;
  10177. }
  10178. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  10179. #if defined(WOLFSSL_ASYNC_CRYPT)
  10180. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10181. #endif
  10182. if (ret != 0) {
  10183. ret = WC_TEST_RET_ENC_EC(ret);
  10184. break;
  10185. }
  10186. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  10187. #if defined(WOLFSSL_ASYNC_CRYPT)
  10188. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10189. #endif
  10190. if (ret != 0) {
  10191. ret = WC_TEST_RET_ENC_EC(ret);
  10192. break;
  10193. }
  10194. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  10195. ret = WC_TEST_RET_ENC_NC;
  10196. break;
  10197. }
  10198. }
  10199. if (ret != 0)
  10200. break;
  10201. }
  10202. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10203. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_AES_C_DYNAMIC_FALLBACK */
  10204. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10205. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10206. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10207. #endif
  10208. if (ret != 0)
  10209. goto out;
  10210. }
  10211. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  10212. /* Test of AES IV state with encrypt/decrypt */
  10213. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  10214. {
  10215. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  10216. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  10217. */
  10218. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  10219. {
  10220. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  10221. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  10222. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  10223. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  10224. };
  10225. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  10226. {
  10227. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  10228. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  10229. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  10230. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  10231. };
  10232. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  10233. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  10234. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  10235. };
  10236. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  10237. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10238. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  10239. };
  10240. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  10241. if (ret != 0)
  10242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10243. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  10244. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  10245. #if defined(WOLFSSL_ASYNC_CRYPT)
  10246. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10247. #endif
  10248. if (ret != 0)
  10249. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10250. #ifndef HAVE_RENESAS_SYNC
  10251. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  10252. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10253. #endif
  10254. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  10255. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  10256. #if defined(WOLFSSL_ASYNC_CRYPT)
  10257. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10258. #endif
  10259. if (ret != 0)
  10260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10261. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  10262. AES_BLOCK_SIZE))
  10263. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10264. #if defined(HAVE_AES_DECRYPT)
  10265. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  10266. if (ret != 0)
  10267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10268. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  10269. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  10270. #if defined(WOLFSSL_ASYNC_CRYPT)
  10271. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10272. #endif
  10273. if (ret != 0)
  10274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10275. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  10276. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10277. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  10278. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  10279. #if defined(WOLFSSL_ASYNC_CRYPT)
  10280. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10281. #endif
  10282. if (ret != 0)
  10283. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10284. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  10285. AES_BLOCK_SIZE))
  10286. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10287. #endif /* HAVE_AES_DECRYPT */
  10288. }
  10289. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  10290. #endif /* HAVE_AES_CBC */
  10291. #ifdef WOLFSSL_AES_COUNTER
  10292. ret = aesctr_test(enc, dec, cipher, plain);
  10293. if (ret != 0)
  10294. return ret;
  10295. #endif
  10296. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  10297. {
  10298. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  10299. {
  10300. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10301. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10302. };
  10303. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  10304. {
  10305. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  10306. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  10307. };
  10308. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  10309. {
  10310. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10311. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10312. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10313. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10314. };
  10315. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10316. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  10317. if (ret != 0)
  10318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10319. #if !defined(HAVE_SELFTEST) && \
  10320. (defined(WOLFSSL_LINUXKM) || \
  10321. !defined(HAVE_FIPS) || \
  10322. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  10323. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  10324. if (ret != 0)
  10325. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10326. #else
  10327. wc_AesEncryptDirect(enc, cipher, niPlain);
  10328. #endif
  10329. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  10330. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10331. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10332. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  10333. if (ret != 0)
  10334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10335. #if !defined(HAVE_SELFTEST) && \
  10336. (defined(WOLFSSL_LINUXKM) || \
  10337. !defined(HAVE_FIPS) || \
  10338. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  10339. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  10340. if (ret != 0)
  10341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10342. #else
  10343. wc_AesDecryptDirect(dec, plain, niCipher);
  10344. #endif
  10345. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  10346. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10347. }
  10348. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  10349. #ifndef HAVE_RENESAS_SYNC
  10350. ret = aes_key_size_test();
  10351. if (ret != 0)
  10352. goto out;
  10353. #endif
  10354. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  10355. !defined(HAVE_RENESAS_SYNC)
  10356. ret = aes_cbc_test();
  10357. if (ret != 0)
  10358. goto out;
  10359. #endif
  10360. #if defined(WOLFSSL_AES_XTS)
  10361. #ifdef WOLFSSL_AES_128
  10362. ret = aes_xts_128_test();
  10363. if (ret != 0)
  10364. goto out;
  10365. #endif
  10366. #ifdef WOLFSSL_AES_256
  10367. ret = aes_xts_256_test();
  10368. if (ret != 0)
  10369. goto out;
  10370. #endif
  10371. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10372. ret = aes_xts_sector_test();
  10373. if (ret != 0)
  10374. goto out;
  10375. #endif
  10376. #ifdef WOLFSSL_AES_128
  10377. ret = aes_xts_args_test();
  10378. if (ret != 0)
  10379. goto out;
  10380. #endif
  10381. #endif
  10382. #if defined(WOLFSSL_AES_CFB)
  10383. ret = aescfb_test();
  10384. if (ret != 0)
  10385. goto out;
  10386. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10387. ret = aescfb1_test();
  10388. if (ret != 0)
  10389. goto out;
  10390. ret = aescfb8_test();
  10391. if (ret != 0)
  10392. goto out;
  10393. #endif
  10394. #endif
  10395. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10396. ret = aesecb_test();
  10397. if (ret != 0)
  10398. goto out;
  10399. #endif
  10400. out:
  10401. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  10402. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10403. if (enc) {
  10404. if (enc_inited)
  10405. wc_AesFree(enc);
  10406. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10407. }
  10408. #else
  10409. if (enc_inited)
  10410. wc_AesFree(enc);
  10411. #endif
  10412. (void)cipher;
  10413. #ifdef HAVE_AES_DECRYPT
  10414. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10415. if (dec) {
  10416. if (dec_inited)
  10417. wc_AesFree(dec);
  10418. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10419. }
  10420. #else
  10421. if (dec_inited)
  10422. wc_AesFree(dec);
  10423. #endif
  10424. (void)plain;
  10425. #endif /* HAVE_AES_DECRYPT */
  10426. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  10427. return ret;
  10428. }
  10429. #ifdef WOLFSSL_AES_192
  10430. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  10431. {
  10432. #ifdef HAVE_AES_CBC
  10433. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10434. Aes *enc = NULL;
  10435. #else
  10436. Aes enc[1];
  10437. #endif
  10438. byte cipher[AES_BLOCK_SIZE];
  10439. #ifdef HAVE_AES_DECRYPT
  10440. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10441. Aes *dec = NULL;
  10442. #else
  10443. Aes dec[1];
  10444. #endif
  10445. byte plain[AES_BLOCK_SIZE];
  10446. #endif
  10447. #endif /* HAVE_AES_CBC */
  10448. wc_test_ret_t ret = 0;
  10449. #ifdef HAVE_AES_CBC
  10450. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  10451. * Appendix F.2.3 */
  10452. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  10453. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10454. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10455. };
  10456. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  10457. {
  10458. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  10459. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  10460. };
  10461. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  10462. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  10463. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  10464. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  10465. };
  10466. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  10467. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  10468. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  10469. };
  10470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10471. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10472. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10473. #ifdef HAVE_AES_DECRYPT
  10474. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10475. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10476. #endif
  10477. #endif
  10478. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10479. if (ret != 0)
  10480. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10481. #ifdef HAVE_AES_DECRYPT
  10482. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10483. if (ret != 0)
  10484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10485. #endif
  10486. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  10487. if (ret != 0)
  10488. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10489. #ifdef HAVE_AES_DECRYPT
  10490. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  10491. if (ret != 0)
  10492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10493. #endif
  10494. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10495. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  10496. #if defined(WOLFSSL_ASYNC_CRYPT)
  10497. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10498. #endif
  10499. if (ret != 0)
  10500. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10501. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  10502. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10503. #ifdef HAVE_AES_DECRYPT
  10504. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10505. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  10506. #if defined(WOLFSSL_ASYNC_CRYPT)
  10507. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10508. #endif
  10509. if (ret != 0)
  10510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10511. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  10512. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10513. }
  10514. #endif
  10515. wc_AesFree(enc);
  10516. #ifdef HAVE_AES_DECRYPT
  10517. wc_AesFree(dec);
  10518. #endif
  10519. out:
  10520. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10521. if (enc)
  10522. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10523. #ifdef HAVE_AES_DECRYPT
  10524. if (dec)
  10525. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10526. #endif
  10527. #endif
  10528. #endif /* HAVE_AES_CBC */
  10529. return ret;
  10530. }
  10531. #endif /* WOLFSSL_AES_192 */
  10532. #ifdef WOLFSSL_AES_256
  10533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  10534. {
  10535. #ifdef HAVE_AES_CBC
  10536. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10537. Aes *enc = NULL;
  10538. #else
  10539. Aes enc[1];
  10540. #endif
  10541. byte cipher[AES_BLOCK_SIZE];
  10542. #ifdef HAVE_AES_DECRYPT
  10543. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10544. Aes *dec = NULL;
  10545. #else
  10546. Aes dec[1];
  10547. #endif
  10548. byte plain[AES_BLOCK_SIZE];
  10549. #endif
  10550. #endif /* HAVE_AES_CBC */
  10551. wc_test_ret_t ret = 0;
  10552. #ifdef HAVE_AES_CBC
  10553. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  10554. * Appendix F.2.5 */
  10555. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  10556. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10557. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10558. };
  10559. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  10560. {
  10561. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  10562. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  10563. };
  10564. #ifdef HAVE_RENESAS_SYNC
  10565. byte *key =
  10566. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  10567. int keySz = (256/8);
  10568. #else
  10569. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  10570. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10571. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10572. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10573. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10574. };
  10575. int keySz = (int)sizeof(key);
  10576. #endif
  10577. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  10578. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  10579. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  10580. };
  10581. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10582. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10583. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10584. #ifdef HAVE_AES_DECRYPT
  10585. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10586. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10587. #endif
  10588. #endif
  10589. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10590. if (ret != 0)
  10591. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10592. #ifdef HAVE_AES_DECRYPT
  10593. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10594. if (ret != 0)
  10595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10596. #endif
  10597. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  10598. if (ret != 0)
  10599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10600. #ifdef HAVE_AES_DECRYPT
  10601. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  10602. if (ret != 0)
  10603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10604. #endif
  10605. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10606. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  10607. #if defined(WOLFSSL_ASYNC_CRYPT)
  10608. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10609. #endif
  10610. if (ret != 0)
  10611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10612. #ifdef HAVE_AES_DECRYPT
  10613. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10614. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  10615. #if defined(WOLFSSL_ASYNC_CRYPT)
  10616. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10617. #endif
  10618. if (ret != 0)
  10619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10620. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  10621. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10622. }
  10623. #endif
  10624. #ifndef HAVE_RENESAS_SYNC
  10625. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  10626. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10627. #endif
  10628. wc_AesFree(enc);
  10629. #ifdef HAVE_AES_DECRYPT
  10630. wc_AesFree(dec);
  10631. #endif
  10632. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10633. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  10634. if (ret != 0)
  10635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10636. #ifdef HAVE_AES_DECRYPT
  10637. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  10638. if (ret != 0)
  10639. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10640. #endif
  10641. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10642. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10643. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  10644. #if defined(WOLFSSL_ASYNC_CRYPT)
  10645. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10646. #endif
  10647. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10648. if (ret != 0)
  10649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10650. #ifdef HAVE_AES_DECRYPT
  10651. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10652. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10653. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  10654. #if defined(WOLFSSL_ASYNC_CRYPT)
  10655. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10656. #endif
  10657. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10658. if (ret != 0)
  10659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10660. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  10661. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10662. }
  10663. #endif
  10664. #ifndef HAVE_RENESAS_SYNC
  10665. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  10666. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10667. #endif
  10668. wc_AesFree(enc);
  10669. #ifdef HAVE_AES_DECRYPT
  10670. wc_AesFree(dec);
  10671. #endif
  10672. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10673. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  10674. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10675. if (ret != 0)
  10676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10677. #ifdef HAVE_AES_DECRYPT
  10678. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10679. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  10680. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10681. if (ret != 0)
  10682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10683. #endif
  10684. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10685. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  10686. #if defined(WOLFSSL_ASYNC_CRYPT)
  10687. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10688. #endif
  10689. if (ret != 0)
  10690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10691. #ifdef HAVE_AES_DECRYPT
  10692. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10693. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  10694. #if defined(WOLFSSL_ASYNC_CRYPT)
  10695. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10696. #endif
  10697. if (ret != 0)
  10698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10699. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  10700. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10701. }
  10702. #endif
  10703. #ifndef HAVE_RENESAS_SYNC
  10704. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  10705. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10706. #endif
  10707. wc_AesFree(enc);
  10708. #ifdef HAVE_AES_DECRYPT
  10709. wc_AesFree(dec);
  10710. #endif
  10711. #endif
  10712. out:
  10713. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10714. if (enc)
  10715. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10716. #ifdef HAVE_AES_DECRYPT
  10717. if (dec)
  10718. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10719. #endif
  10720. #endif
  10721. #endif /* HAVE_AES_CBC */
  10722. return ret;
  10723. }
  10724. #endif /* WOLFSSL_AES_256 */
  10725. #ifdef HAVE_AESGCM
  10726. #ifdef WOLFSSL_AES_128
  10727. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  10728. byte* plain, int plainSz, byte* cipher, int cipherSz,
  10729. byte* aad, int aadSz, byte* tag, int tagSz)
  10730. {
  10731. wc_test_ret_t ret;
  10732. int enc_inited = 0, dec_inited = 0;
  10733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10734. Aes *enc = NULL;
  10735. Aes *dec = NULL;
  10736. #else
  10737. Aes enc[1];
  10738. Aes dec[1];
  10739. #endif
  10740. byte resultT[AES_BLOCK_SIZE];
  10741. byte resultP[AES_BLOCK_SIZE * 3];
  10742. byte resultC[AES_BLOCK_SIZE * 3];
  10743. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10744. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10745. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10746. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10747. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10748. #endif
  10749. XMEMSET(resultT, 0, sizeof(resultT));
  10750. XMEMSET(resultC, 0, sizeof(resultC));
  10751. XMEMSET(resultP, 0, sizeof(resultP));
  10752. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10753. if (ret != 0)
  10754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10755. else
  10756. enc_inited = 1;
  10757. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10758. if (ret != 0)
  10759. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10760. else
  10761. dec_inited = 1;
  10762. ret = wc_AesGcmSetKey(enc, key, keySz);
  10763. if (ret != 0)
  10764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10765. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10766. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  10767. resultT, tagSz, aad, aadSz);
  10768. #if defined(WOLFSSL_ASYNC_CRYPT)
  10769. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10770. #endif
  10771. if (ret != 0)
  10772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10773. if (cipher != NULL) {
  10774. if (XMEMCMP(cipher, resultC, cipherSz))
  10775. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10776. }
  10777. if (XMEMCMP(tag, resultT, tagSz))
  10778. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10779. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10780. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10781. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  10782. resultT, tagSz, aad, aadSz);
  10783. #if defined(WOLFSSL_ASYNC_CRYPT)
  10784. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10785. #endif
  10786. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10787. if (ret != 0)
  10788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10789. if (cipher != NULL) {
  10790. if (XMEMCMP(cipher, resultC, cipherSz))
  10791. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10792. }
  10793. if (XMEMCMP(tag, resultT, tagSz))
  10794. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10795. #endif
  10796. #ifdef HAVE_AES_DECRYPT
  10797. ret = wc_AesGcmSetKey(dec, key, keySz);
  10798. if (ret != 0)
  10799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10800. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  10801. iv, ivSz, resultT, tagSz, aad, aadSz);
  10802. #if defined(WOLFSSL_ASYNC_CRYPT)
  10803. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10804. #endif
  10805. if (ret != 0)
  10806. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10807. if (plain != NULL) {
  10808. if (XMEMCMP(plain, resultP, plainSz))
  10809. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10810. }
  10811. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10812. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10813. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  10814. iv, ivSz, resultT, tagSz, aad, aadSz);
  10815. #if defined(WOLFSSL_ASYNC_CRYPT)
  10816. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10817. #endif
  10818. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10819. if (ret != 0)
  10820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10821. if (plain != NULL) {
  10822. if (XMEMCMP(plain, resultP, plainSz))
  10823. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10824. }
  10825. #endif
  10826. #endif /* HAVE_AES_DECRYPT */
  10827. ret = 0;
  10828. out:
  10829. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10830. if (enc) {
  10831. if (enc_inited)
  10832. wc_AesFree(enc);
  10833. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10834. }
  10835. if (dec) {
  10836. if (dec_inited)
  10837. wc_AesFree(dec);
  10838. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10839. }
  10840. #else
  10841. if (enc_inited)
  10842. wc_AesFree(enc);
  10843. if (dec_inited)
  10844. wc_AesFree(dec);
  10845. #endif
  10846. return ret;
  10847. }
  10848. #endif
  10849. /* tests that only use 12 byte IV and 16 or less byte AAD
  10850. * test vectors are from NIST SP 800-38D
  10851. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  10852. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  10853. {
  10854. #ifdef WOLFSSL_AES_128
  10855. byte key1[] = {
  10856. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  10857. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  10858. };
  10859. byte iv1[] = {
  10860. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  10861. 0xe4, 0xed, 0x2f, 0x6d
  10862. };
  10863. ALIGN64 byte plain1[] = {
  10864. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  10865. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  10866. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  10867. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  10868. };
  10869. byte aad1[] = {
  10870. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  10871. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  10872. };
  10873. ALIGN64 byte cipher1[] = {
  10874. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  10875. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  10876. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  10877. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  10878. };
  10879. byte tag1[] = {
  10880. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  10881. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  10882. };
  10883. byte key2[] = {
  10884. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  10885. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  10886. };
  10887. byte iv2[] = {
  10888. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  10889. 0xc9, 0x8a, 0xff, 0xe3
  10890. };
  10891. ALIGN64 byte plain2[] = {
  10892. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  10893. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  10894. };
  10895. ALIGN64 byte cipher2[] = {
  10896. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  10897. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  10898. };
  10899. byte tag2[] = {
  10900. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  10901. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  10902. };
  10903. byte key3[] = {
  10904. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  10905. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  10906. };
  10907. byte iv3[] = {
  10908. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  10909. 0x6a, 0x4b, 0xd5, 0xe1
  10910. };
  10911. byte tag3[] = {
  10912. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  10913. 0x11, 0x64, 0xb2, 0xff
  10914. };
  10915. wc_test_ret_t ret;
  10916. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  10917. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  10918. aad1, sizeof(aad1), tag1, sizeof(tag1));
  10919. if (ret != 0) {
  10920. return ret;
  10921. }
  10922. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  10923. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  10924. NULL, 0, tag2, sizeof(tag2));
  10925. if (ret != 0) {
  10926. return ret;
  10927. }
  10928. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  10929. NULL, 0, NULL, 0,
  10930. NULL, 0, tag3, sizeof(tag3));
  10931. if (ret != 0) {
  10932. return ret;
  10933. }
  10934. #endif
  10935. return 0;
  10936. }
  10937. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  10938. {
  10939. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10940. Aes *enc = NULL;
  10941. Aes *dec = NULL;
  10942. #else
  10943. Aes enc[1];
  10944. Aes dec[1];
  10945. #endif
  10946. /*
  10947. * This is Test Case 16 from the document Galois/
  10948. * Counter Mode of Operation (GCM) by McGrew and
  10949. * Viega.
  10950. */
  10951. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10952. {
  10953. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  10954. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  10955. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  10956. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  10957. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  10958. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  10959. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  10960. 0xba, 0x63, 0x7b, 0x39
  10961. };
  10962. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10963. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10964. {
  10965. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10966. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10967. 0xab, 0xad, 0xda, 0xd2
  10968. };
  10969. #endif
  10970. #ifdef WOLFSSL_AES_256
  10971. #ifdef HAVE_RENESAS_SYNC
  10972. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  10973. int k1Sz = (int)(256/8);
  10974. #else
  10975. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10976. {
  10977. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10978. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  10979. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10980. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  10981. };
  10982. int k1Sz = (int)sizeof(k1);
  10983. #endif
  10984. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10985. {
  10986. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  10987. 0xde, 0xca, 0xf8, 0x88
  10988. };
  10989. #endif /* WOLFSSL_AES_256 */
  10990. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10991. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10992. {
  10993. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  10994. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  10995. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  10996. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  10997. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  10998. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  10999. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  11000. 0xbc, 0xc9, 0xf6, 0x62
  11001. };
  11002. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  11003. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  11004. {
  11005. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  11006. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  11007. };
  11008. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  11009. #if !defined(HAVE_FIPS) && \
  11010. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  11011. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  11012. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  11013. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  11014. !(defined(WOLF_CRYPTO_CB) && \
  11015. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  11016. #define ENABLE_NON_12BYTE_IV_TEST
  11017. #ifdef WOLFSSL_AES_192
  11018. /* Test Case 12, uses same plaintext and AAD data. */
  11019. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  11020. {
  11021. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  11022. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  11023. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  11024. };
  11025. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  11026. {
  11027. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  11028. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  11029. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  11030. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  11031. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  11032. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  11033. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  11034. 0xa6, 0x37, 0xb3, 0x9b
  11035. };
  11036. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  11037. {
  11038. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  11039. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  11040. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  11041. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  11042. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  11043. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  11044. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  11045. 0xe9, 0xb7, 0x37, 0x3b
  11046. };
  11047. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  11048. {
  11049. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  11050. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  11051. };
  11052. #endif /* WOLFSSL_AES_192 */
  11053. #ifdef WOLFSSL_AES_128
  11054. /* The following is an interesting test case from the example
  11055. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  11056. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  11057. {
  11058. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  11059. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  11060. };
  11061. #ifdef HAVE_RENESAS_SYNC
  11062. const byte *k3 =
  11063. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  11064. int k3Sz = (int)(128/8);
  11065. #else
  11066. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  11067. {
  11068. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  11069. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  11070. };
  11071. int k3Sz = (int)sizeof(k3);
  11072. #endif
  11073. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  11074. {
  11075. 0xca
  11076. };
  11077. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  11078. {
  11079. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  11080. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  11081. };
  11082. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  11083. {
  11084. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  11085. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  11086. };
  11087. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  11088. {
  11089. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  11090. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  11091. };
  11092. #endif /* WOLFSSL_AES_128 */
  11093. #ifdef WOLFSSL_AES_256
  11094. int ivlen;
  11095. #endif
  11096. #endif
  11097. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  11098. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  11099. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  11100. wc_test_ret_t ret = 0;
  11101. #ifdef WOLFSSL_AES_256
  11102. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  11103. int alen;
  11104. #endif
  11105. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11106. int plen;
  11107. #endif
  11108. #endif
  11109. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  11110. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  11111. byte bufA[sizeof(a) + 1];
  11112. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11113. #endif
  11114. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  11115. #if !defined(BENCH_AESGCM_LARGE)
  11116. #define BENCH_AESGCM_LARGE 1024
  11117. #endif
  11118. #ifndef WOLFSSL_NO_MALLOC
  11119. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11120. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11121. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11122. if ((! large_input) || (! large_output) || (! large_outdec))
  11123. ERROR_OUT(MEMORY_E, out);
  11124. #else
  11125. byte large_input[BENCH_AESGCM_LARGE];
  11126. byte large_output[BENCH_AESGCM_LARGE];
  11127. byte large_outdec[BENCH_AESGCM_LARGE];
  11128. #endif
  11129. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  11130. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  11131. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  11132. #endif
  11133. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11134. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11135. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11136. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11137. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11138. #endif
  11139. XMEMSET(resultT, 0, sizeof(resultT));
  11140. XMEMSET(resultC, 0, sizeof(resultC));
  11141. XMEMSET(resultP, 0, sizeof(resultP));
  11142. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11143. if (ret != 0)
  11144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11145. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11146. if (ret != 0)
  11147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11148. #ifdef WOLFSSL_AES_256
  11149. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  11150. if (ret != 0)
  11151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11152. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11153. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  11154. resultT, sizeof(t1), a, sizeof(a));
  11155. #if defined(WOLFSSL_ASYNC_CRYPT)
  11156. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11157. #endif
  11158. if (ret != 0)
  11159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11160. #ifndef HAVE_RENESAS_SYNC
  11161. if (XMEMCMP(c1, resultC, sizeof(c1)))
  11162. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11163. if (XMEMCMP(t1, resultT, sizeof(t1)))
  11164. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11165. #endif
  11166. #ifdef HAVE_AES_DECRYPT
  11167. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  11168. if (ret != 0)
  11169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11170. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  11171. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11172. #if defined(WOLFSSL_ASYNC_CRYPT)
  11173. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11174. #endif
  11175. if (ret != 0)
  11176. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11177. if (XMEMCMP(p, resultP, sizeof(p)))
  11178. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11179. #endif /* HAVE_AES_DECRYPT */
  11180. /* Large buffer test */
  11181. #ifdef BENCH_AESGCM_LARGE
  11182. /* setup test buffer */
  11183. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11184. large_input[alen] = (byte)alen;
  11185. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11186. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11187. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11188. resultT, sizeof(t1), a, sizeof(a));
  11189. #if defined(WOLFSSL_ASYNC_CRYPT)
  11190. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11191. #endif
  11192. if (ret != 0)
  11193. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11194. #ifdef HAVE_AES_DECRYPT
  11195. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11196. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11197. sizeof(t1), a, sizeof(a));
  11198. #if defined(WOLFSSL_ASYNC_CRYPT)
  11199. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11200. #endif
  11201. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11202. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11203. if (ret != 0)
  11204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11205. #endif /* HAVE_AES_DECRYPT */
  11206. #endif /* BENCH_AESGCM_LARGE */
  11207. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  11208. /* Variable IV length test */
  11209. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  11210. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11211. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  11212. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  11213. #if defined(WOLFSSL_ASYNC_CRYPT)
  11214. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11215. #endif
  11216. if (ret != 0)
  11217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11218. #ifdef HAVE_AES_DECRYPT
  11219. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  11220. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  11221. #if defined(WOLFSSL_ASYNC_CRYPT)
  11222. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11223. #endif
  11224. if (ret != 0)
  11225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11226. #endif /* HAVE_AES_DECRYPT */
  11227. }
  11228. #endif
  11229. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  11230. /* Variable authenticated data length test */
  11231. for (alen=0; alen<(int)sizeof(p); alen++) {
  11232. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11233. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  11234. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  11235. #if defined(WOLFSSL_ASYNC_CRYPT)
  11236. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11237. #endif
  11238. if (ret != 0)
  11239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11240. #ifdef HAVE_AES_DECRYPT
  11241. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  11242. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  11243. #if defined(WOLFSSL_ASYNC_CRYPT)
  11244. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11245. #endif
  11246. if (ret != 0)
  11247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11248. #endif /* HAVE_AES_DECRYPT */
  11249. }
  11250. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  11251. if (! large_aad)
  11252. ERROR_OUT(MEMORY_E, out);
  11253. XMEMSET(large_aad, 0, 1024+16);
  11254. /* Variable authenticated data length test */
  11255. for (alen=0; alen<=1024; alen+=16) {
  11256. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11257. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  11258. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  11259. if (ret != 0)
  11260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11261. #ifdef HAVE_AES_DECRYPT
  11262. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  11263. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  11264. if (ret != 0)
  11265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11266. #endif /* HAVE_AES_DECRYPT */
  11267. }
  11268. /* Test unaligned memory of all potential arguments */
  11269. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  11270. if (ret != 0)
  11271. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11272. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11273. XMEMCPY(&buf[1], p, sizeof(p));
  11274. XMEMCPY(&bufA[1], a, sizeof(a));
  11275. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  11276. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  11277. if (ret != 0)
  11278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11279. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  11280. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11281. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  11282. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11283. #ifdef HAVE_AES_DECRYPT
  11284. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  11285. if (ret != 0)
  11286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11287. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  11288. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  11289. if (ret != 0)
  11290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11291. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  11292. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11293. #endif /* HAVE_AES_DECRYPT */
  11294. #endif /* Xilinx Versal */
  11295. #endif
  11296. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11297. #ifdef BENCH_AESGCM_LARGE
  11298. /* Variable plain text length test */
  11299. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  11300. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11301. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11302. plen, iv1, sizeof(iv1), resultT,
  11303. sizeof(t1), a, sizeof(a));
  11304. #if defined(WOLFSSL_ASYNC_CRYPT)
  11305. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11306. #endif
  11307. if (ret != 0)
  11308. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11309. #ifdef HAVE_AES_DECRYPT
  11310. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11311. plen, iv1, sizeof(iv1), resultT,
  11312. sizeof(t1), a, sizeof(a));
  11313. #if defined(WOLFSSL_ASYNC_CRYPT)
  11314. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11315. #endif
  11316. if (ret != 0)
  11317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11318. #endif /* HAVE_AES_DECRYPT */
  11319. }
  11320. #else /* BENCH_AESGCM_LARGE */
  11321. /* Variable plain text length test */
  11322. for (plen=1; plen<(int)sizeof(p); plen++) {
  11323. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11324. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  11325. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11326. #if defined(WOLFSSL_ASYNC_CRYPT)
  11327. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11328. #endif
  11329. if (ret != 0)
  11330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11331. #ifdef HAVE_AES_DECRYPT
  11332. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  11333. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11334. #if defined(WOLFSSL_ASYNC_CRYPT)
  11335. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11336. #endif
  11337. if (ret != 0)
  11338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11339. #endif /* HAVE_AES_DECRYPT */
  11340. }
  11341. #endif /* BENCH_AESGCM_LARGE */
  11342. #endif
  11343. #endif /* WOLFSSL_AES_256 */
  11344. /* test with IV != 12 bytes */
  11345. #ifdef ENABLE_NON_12BYTE_IV_TEST
  11346. XMEMSET(resultT, 0, sizeof(resultT));
  11347. XMEMSET(resultC, 0, sizeof(resultC));
  11348. XMEMSET(resultP, 0, sizeof(resultP));
  11349. #ifdef WOLFSSL_AES_192
  11350. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  11351. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11352. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  11353. resultT, sizeof(t1), a, sizeof(a));
  11354. #if defined(WOLFSSL_ASYNC_CRYPT)
  11355. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11356. #endif
  11357. if (ret != 0)
  11358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11359. if (XMEMCMP(c2, resultC, sizeof(c2)))
  11360. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11361. if (XMEMCMP(t2, resultT, sizeof(t1)))
  11362. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11363. #ifdef HAVE_AES_DECRYPT
  11364. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  11365. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  11366. #if defined(WOLFSSL_ASYNC_CRYPT)
  11367. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11368. #endif
  11369. if (ret != 0)
  11370. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11371. if (XMEMCMP(p, resultP, sizeof(p)))
  11372. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11373. #endif /* HAVE_AES_DECRYPT */
  11374. /* Large buffer test */
  11375. #ifdef BENCH_AESGCM_LARGE
  11376. wc_AesGcmSetKey(enc, k2, k3Sz);
  11377. wc_AesGcmSetKey(dec, k2, k3Sz);
  11378. /* setup test buffer */
  11379. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11380. large_input[alen] = (byte)alen;
  11381. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11382. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11383. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11384. resultT, sizeof(t1), a, sizeof(a));
  11385. #if defined(WOLFSSL_ASYNC_CRYPT)
  11386. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11387. #endif
  11388. if (ret != 0)
  11389. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11390. #ifdef HAVE_AES_DECRYPT
  11391. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11392. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11393. sizeof(t1), a, sizeof(a));
  11394. #if defined(WOLFSSL_ASYNC_CRYPT)
  11395. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11396. #endif
  11397. if (ret != 0)
  11398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11399. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11401. #endif /* HAVE_AES_DECRYPT */
  11402. #endif /* BENCH_AESGCM_LARGE */
  11403. XMEMSET(resultT, 0, sizeof(resultT));
  11404. XMEMSET(resultC, 0, sizeof(resultC));
  11405. XMEMSET(resultP, 0, sizeof(resultP));
  11406. #endif /* WOLFSSL_AES_192 */
  11407. #ifdef WOLFSSL_AES_128
  11408. wc_AesGcmSetKey(enc, k3, k3Sz);
  11409. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11410. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  11411. resultT, sizeof(t3), a3, sizeof(a3));
  11412. #if defined(WOLFSSL_ASYNC_CRYPT)
  11413. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11414. #endif
  11415. if (ret != 0)
  11416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11417. #ifndef HAVE_RENESAS_SYNC
  11418. if (XMEMCMP(c3, resultC, sizeof(c3)))
  11419. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11420. if (XMEMCMP(t3, resultT, sizeof(t3)))
  11421. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11422. #endif
  11423. #ifdef HAVE_AES_DECRYPT
  11424. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  11425. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  11426. #if defined(WOLFSSL_ASYNC_CRYPT)
  11427. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11428. #endif
  11429. if (ret != 0)
  11430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11431. if (XMEMCMP(p3, resultP, sizeof(p3)))
  11432. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11433. #endif /* HAVE_AES_DECRYPT */
  11434. /* Large buffer test */
  11435. #ifdef BENCH_AESGCM_LARGE
  11436. wc_AesGcmSetKey(enc, k3, k3Sz);
  11437. wc_AesGcmSetKey(dec, k3, k3Sz);
  11438. /* setup test buffer */
  11439. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11440. large_input[alen] = (byte)alen;
  11441. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11442. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11443. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11444. resultT, sizeof(t1), a, sizeof(a));
  11445. #if defined(WOLFSSL_ASYNC_CRYPT)
  11446. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11447. #endif
  11448. if (ret != 0)
  11449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11450. #ifdef HAVE_AES_DECRYPT
  11451. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11452. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11453. sizeof(t1), a, sizeof(a));
  11454. #if defined(WOLFSSL_ASYNC_CRYPT)
  11455. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11456. #endif
  11457. if (ret != 0)
  11458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11459. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11460. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11461. #endif /* HAVE_AES_DECRYPT */
  11462. #endif /* BENCH_AESGCM_LARGE */
  11463. #endif /* WOLFSSL_AES_128 */
  11464. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  11465. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  11466. !defined(WOLFSSL_XILINX_CRYPT) && \
  11467. !(defined(WOLF_CRYPTO_CB) && \
  11468. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  11469. XMEMSET(resultT, 0, sizeof(resultT));
  11470. XMEMSET(resultC, 0, sizeof(resultC));
  11471. XMEMSET(resultP, 0, sizeof(resultP));
  11472. wc_AesGcmSetKey(enc, k1, k1Sz);
  11473. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11474. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  11475. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  11476. #if defined(WOLFSSL_ASYNC_CRYPT)
  11477. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11478. #endif
  11479. if (ret != 0)
  11480. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11481. #ifndef HAVE_RENESAS_SYNC
  11482. if (XMEMCMP(c1, resultC, sizeof(c1)))
  11483. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11484. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  11485. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11486. #endif
  11487. #ifdef HAVE_AES_DECRYPT
  11488. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  11489. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  11490. #if defined(WOLFSSL_ASYNC_CRYPT)
  11491. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11492. #endif
  11493. if (ret != 0)
  11494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11495. if (XMEMCMP(p, resultP, sizeof(p)))
  11496. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11497. #endif /* HAVE_AES_DECRYPT */
  11498. #endif /* WOLFSSL_AES_256 */
  11499. #if !defined(HAVE_FIPS) || \
  11500. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11501. /* Test encrypt with internally generated IV */
  11502. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  11503. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  11504. {
  11505. WC_RNG rng;
  11506. byte randIV[12];
  11507. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11508. if (ret != 0)
  11509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11510. XMEMSET(randIV, 0, sizeof(randIV));
  11511. XMEMSET(resultT, 0, sizeof(resultT));
  11512. XMEMSET(resultC, 0, sizeof(resultC));
  11513. XMEMSET(resultP, 0, sizeof(resultP));
  11514. wc_AesGcmSetKey(enc, k1, k1Sz);
  11515. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  11516. if (ret != 0)
  11517. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11518. ret = wc_AesGcmEncrypt_ex(enc,
  11519. resultC, p, sizeof(p),
  11520. randIV, sizeof(randIV),
  11521. resultT, sizeof(t1),
  11522. a, sizeof(a));
  11523. #if defined(WOLFSSL_ASYNC_CRYPT)
  11524. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11525. #endif
  11526. if (ret != 0)
  11527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11528. /* Check the IV has been set. */
  11529. {
  11530. word32 i, ivSum = 0;
  11531. for (i = 0; i < sizeof(randIV); i++)
  11532. ivSum += randIV[i];
  11533. if (ivSum == 0)
  11534. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11535. }
  11536. #ifdef HAVE_AES_DECRYPT
  11537. wc_AesGcmSetKey(dec, k1, k1Sz);
  11538. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  11539. if (ret != 0)
  11540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11541. ret = wc_AesGcmDecrypt(dec,
  11542. resultP, resultC, sizeof(c1),
  11543. randIV, sizeof(randIV),
  11544. resultT, sizeof(t1),
  11545. a, sizeof(a));
  11546. #if defined(WOLFSSL_ASYNC_CRYPT)
  11547. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11548. #endif
  11549. if (ret != 0)
  11550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11551. if (XMEMCMP(p, resultP, sizeof(p)))
  11552. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11553. #endif /* HAVE_AES_DECRYPT */
  11554. wc_FreeRng(&rng);
  11555. }
  11556. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  11557. #endif /* HAVE_FIPS_VERSION >= 2 */
  11558. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11559. #ifdef WOLFSSL_AES_256
  11560. #ifdef WOLFSSL_AESGCM_STREAM
  11561. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11562. if (ret != 0)
  11563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11564. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  11565. if (ret != 0)
  11566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11567. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  11568. if (ret != 0)
  11569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11570. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  11571. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11572. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  11573. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11574. #ifdef HAVE_AES_DECRYPT
  11575. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11576. if (ret != 0)
  11577. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11578. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  11579. if (ret != 0)
  11580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11581. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  11582. if (ret != 0)
  11583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11584. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  11585. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11586. #endif
  11587. /* alen is the size to pass in with each update. */
  11588. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  11589. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11590. if (ret != 0)
  11591. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11592. /* plen is the offset into AAD to update with. */
  11593. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  11594. int len = sizeof(a) - plen;
  11595. if (len > alen) len = alen;
  11596. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  11597. if (ret != 0)
  11598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11599. }
  11600. /* plen is the offset into plaintext to update with. */
  11601. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  11602. int len = sizeof(p) - plen;
  11603. if (len > alen) len = alen;
  11604. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  11605. NULL, 0);
  11606. if (ret != 0)
  11607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11608. }
  11609. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  11610. if (ret != 0)
  11611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11612. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  11613. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11614. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  11615. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11616. }
  11617. #ifdef HAVE_AES_DECRYPT
  11618. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  11619. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11620. if (ret != 0)
  11621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11622. /* plen is the offset into AAD to update with. */
  11623. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  11624. int len = sizeof(a) - plen;
  11625. if (len > alen) len = alen;
  11626. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  11627. if (ret != 0)
  11628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11629. }
  11630. /* plen is the offset into cipher text to update with. */
  11631. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  11632. int len = sizeof(c1) - plen;
  11633. if (len > alen) len = alen;
  11634. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  11635. NULL, 0);
  11636. if (ret != 0)
  11637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11638. }
  11639. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  11640. if (ret != 0)
  11641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11642. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  11643. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11644. }
  11645. #endif /* HAVE_AES_DECRYPT */
  11646. #ifdef BENCH_AESGCM_LARGE
  11647. /* setup test buffer */
  11648. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11649. if (ret != 0)
  11650. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11651. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  11652. BENCH_AESGCM_LARGE, a, sizeof(a));
  11653. if (ret != 0)
  11654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11655. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  11656. if (ret != 0)
  11657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11658. #ifdef HAVE_AES_DECRYPT
  11659. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11660. if (ret != 0)
  11661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11662. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  11663. BENCH_AESGCM_LARGE, a, sizeof(a));
  11664. if (ret != 0)
  11665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11666. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  11667. if (ret != 0)
  11668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11669. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11670. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11671. #endif /* HAVE_AES_DECRYPT */
  11672. #endif /* BENCH_AESGCM_LARGE */
  11673. #endif /* WOLFSSL_AESGCM_STREAM */
  11674. #endif /* WOLFSSL_AES_256 */
  11675. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  11676. wc_AesFree(enc);
  11677. wc_AesFree(dec);
  11678. ret = 0;
  11679. out:
  11680. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  11681. !defined(WOLFSSL_NO_MALLOC)
  11682. if (large_input)
  11683. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11684. if (large_output)
  11685. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11686. if (large_outdec)
  11687. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11688. #endif
  11689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11690. if (enc)
  11691. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11692. if (dec)
  11693. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11694. #endif
  11695. return ret;
  11696. }
  11697. #ifdef WOLFSSL_AES_128
  11698. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  11699. {
  11700. wc_test_ret_t ret;
  11701. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11702. Gmac *gmac;
  11703. #else
  11704. Gmac gmac[1];
  11705. #endif
  11706. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  11707. {
  11708. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  11709. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  11710. };
  11711. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  11712. {
  11713. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  11714. 0xe2, 0x8c, 0x8f, 0x16
  11715. };
  11716. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  11717. {
  11718. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  11719. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  11720. };
  11721. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  11722. {
  11723. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  11724. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  11725. };
  11726. #if (!defined(HAVE_FIPS) || \
  11727. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  11728. /* FIPS builds only allow 16-byte auth tags. */
  11729. /* This sample uses a 15-byte auth tag. */
  11730. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  11731. {
  11732. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  11733. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  11734. };
  11735. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  11736. {
  11737. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  11738. 0x1a, 0x60, 0x24, 0xa7
  11739. };
  11740. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  11741. {
  11742. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  11743. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  11744. };
  11745. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  11746. {
  11747. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  11748. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  11749. };
  11750. #endif
  11751. byte tag[16];
  11752. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11753. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11754. return WC_TEST_RET_ENC_ERRNO;
  11755. #endif
  11756. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  11757. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  11758. XMEMSET(tag, 0, sizeof(tag));
  11759. wc_GmacSetKey(gmac, k1, sizeof(k1));
  11760. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  11761. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  11762. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11763. #if (!defined(HAVE_FIPS) || \
  11764. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  11765. XMEMSET(tag, 0, sizeof(tag));
  11766. wc_GmacSetKey(gmac, k2, sizeof(k2));
  11767. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  11768. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  11769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11770. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  11771. {
  11772. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  11773. {
  11774. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  11775. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  11776. };
  11777. WC_RNG rng;
  11778. byte iv[12];
  11779. #ifndef HAVE_FIPS
  11780. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11781. if (ret != 0)
  11782. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11783. #else
  11784. ret = wc_InitRng(&rng);
  11785. if (ret != 0)
  11786. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11787. #endif
  11788. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  11789. t1, sizeof(t1));
  11790. if (ret != 0)
  11791. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11792. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  11793. badT, sizeof(badT));
  11794. if (ret != AES_GCM_AUTH_E)
  11795. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11796. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  11797. t2, sizeof(t2));
  11798. if (ret != 0)
  11799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11800. XMEMSET(tag, 0, sizeof(tag));
  11801. XMEMSET(iv, 0, sizeof(iv));
  11802. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  11803. tag, sizeof(tag), &rng);
  11804. if (ret != 0)
  11805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11806. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  11807. tag, sizeof(tag));
  11808. if (ret != 0)
  11809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11810. wc_FreeRng(&rng);
  11811. }
  11812. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  11813. #endif /* HAVE_FIPS */
  11814. ret = 0;
  11815. out:
  11816. wc_AesFree(&gmac->aes);
  11817. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11818. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  11819. #endif
  11820. return ret;
  11821. }
  11822. #endif /* WOLFSSL_AES_128 */
  11823. #endif /* HAVE_AESGCM */
  11824. #if defined(HAVE_AESCCM)
  11825. #if defined(WOLFSSL_AES_256)
  11826. static wc_test_ret_t aesccm_256_test(void)
  11827. {
  11828. wc_test_ret_t ret;
  11829. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  11830. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  11831. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  11832. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  11833. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  11834. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  11835. };
  11836. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  11837. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  11838. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  11839. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  11840. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  11841. 0x20, 0x21, 0x22, 0x23};
  11842. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  11843. 0x8A, 0xB1, 0xA8, 0x74};
  11844. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  11845. 0x95, 0xFC, 0x08, 0x20};
  11846. byte output[sizeof(in_plaintext)];
  11847. byte atag[sizeof(exp_tag)];
  11848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11849. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  11850. if (aes == NULL) {
  11851. return MEMORY_E;
  11852. }
  11853. #else
  11854. Aes aes[1];
  11855. #endif
  11856. ret = wc_AesInit(aes, HEAP_HINT, devId);
  11857. if (ret == 0) {
  11858. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  11859. }
  11860. if (ret == 0) {
  11861. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  11862. in_nonce, sizeof(in_nonce),
  11863. atag, sizeof(atag),
  11864. in_auth, sizeof(in_auth));
  11865. }
  11866. /* Verify we produce the proper ciphertext and tag */
  11867. if (ret == 0 &&
  11868. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  11869. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  11870. ret = WC_TEST_RET_ENC_NC;
  11871. }
  11872. if (ret == 0) {
  11873. /* decrypt inline */
  11874. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  11875. in_nonce, sizeof(in_nonce),
  11876. atag, sizeof(atag),
  11877. in_auth, sizeof(in_auth));
  11878. }
  11879. /* Verify decryption was successful */
  11880. if (ret == 0 &&
  11881. XMEMCMP(output, in_plaintext, sizeof(output))) {
  11882. ret = WC_TEST_RET_ENC_NC;
  11883. }
  11884. wc_AesFree(aes);
  11885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11886. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11887. #endif
  11888. return ret;
  11889. }
  11890. #endif /* WOLFSSL_AES_256 */
  11891. #if defined(WOLFSSL_AES_128)
  11892. static wc_test_ret_t aesccm_128_test(void)
  11893. {
  11894. wc_test_ret_t ret;
  11895. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11896. Aes *enc;
  11897. #else
  11898. Aes enc[1];
  11899. #endif
  11900. /* key */
  11901. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  11902. {
  11903. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  11904. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  11905. };
  11906. /* nonce */
  11907. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  11908. {
  11909. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  11910. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  11911. };
  11912. /* plaintext */
  11913. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  11914. {
  11915. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11916. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11917. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  11918. };
  11919. /* plaintext - long */
  11920. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  11921. {
  11922. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11923. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11924. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  11925. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  11926. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  11927. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  11928. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  11929. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  11930. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  11931. 0x50
  11932. };
  11933. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  11934. {
  11935. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11936. };
  11937. /* ciphertext */
  11938. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  11939. {
  11940. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11941. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11942. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  11943. };
  11944. /* tag - authentication */
  11945. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  11946. {
  11947. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  11948. };
  11949. /* ciphertext - long */
  11950. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  11951. {
  11952. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11953. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11954. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  11955. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  11956. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  11957. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  11958. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  11959. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  11960. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  11961. 0x0b
  11962. };
  11963. /* tag - authentication - long */
  11964. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  11965. {
  11966. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  11967. };
  11968. /* tag - authentication - empty plaintext */
  11969. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  11970. {
  11971. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  11972. };
  11973. byte t2[sizeof(t)];
  11974. byte p2[sizeof(p)];
  11975. byte c2[sizeof(c)];
  11976. byte iv2[sizeof(iv)];
  11977. byte pl2[sizeof(pl)];
  11978. byte cl2[sizeof(cl)];
  11979. byte tl2[sizeof(tl)];
  11980. byte t_empty2[sizeof(t_empty)];
  11981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11982. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11983. return WC_TEST_RET_ENC_ERRNO;
  11984. #endif
  11985. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  11986. XMEMSET(t2, 0, sizeof(t2));
  11987. XMEMSET(c2, 0, sizeof(c2));
  11988. XMEMSET(p2, 0, sizeof(p2));
  11989. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11990. if (ret != 0)
  11991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11992. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11993. if (ret != 0)
  11994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11995. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11996. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  11997. t2, sizeof(t2), a, sizeof(a));
  11998. if (ret != 0)
  11999. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12000. if (XMEMCMP(c, c2, sizeof(c2)))
  12001. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12002. if (XMEMCMP(t, t2, sizeof(t2)))
  12003. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12004. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  12005. t2, sizeof(t2), a, sizeof(a));
  12006. if (ret != 0)
  12007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12008. if (XMEMCMP(p, p2, sizeof(p2)))
  12009. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12010. /* Test the authentication failure */
  12011. t2[0]++; /* Corrupt the authentication tag. */
  12012. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  12013. t2, sizeof(t2), a, sizeof(a));
  12014. if (ret == 0)
  12015. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12016. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  12017. * authentication fail. */
  12018. XMEMSET(c2, 0, sizeof(c2));
  12019. if (XMEMCMP(p2, c2, sizeof(p2)))
  12020. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12021. wc_AesFree(enc);
  12022. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  12023. XMEMSET(t2, 0, sizeof(t2));
  12024. XMEMSET(c2, 0, sizeof(c2));
  12025. XMEMSET(p2, 0, sizeof(p2));
  12026. XMEMSET(iv2, 0, sizeof(iv2));
  12027. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12028. if (ret != 0)
  12029. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12030. #ifndef HAVE_SELFTEST
  12031. /* selftest build does not have wc_AesCcmSetNonce() or
  12032. * wc_AesCcmEncrypt_ex() */
  12033. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  12034. if (ret != 0)
  12035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12036. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  12037. if (ret != 0)
  12038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12039. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  12040. t2, sizeof(t2), a, sizeof(a));
  12041. if (ret != 0)
  12042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12043. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  12044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12045. if (XMEMCMP(c, c2, sizeof(c2)))
  12046. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12047. if (XMEMCMP(t, t2, sizeof(t2)))
  12048. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12049. #endif
  12050. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12051. /* test fail on invalid IV sizes */
  12052. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  12053. if (ret != 0)
  12054. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12055. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  12056. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  12057. t2, 1, a, sizeof(a));
  12058. if (ret == 0) {
  12059. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12060. }
  12061. #endif
  12062. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  12063. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  12064. tl2, sizeof(tl2), a, sizeof(a));
  12065. if (ret != 0)
  12066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12067. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  12068. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12069. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  12070. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12071. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  12072. tl2, sizeof(tl2), a, sizeof(a));
  12073. if (ret != 0)
  12074. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12075. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  12076. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12077. /* test empty message as null input or output with nonzero inSz. */
  12078. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  12079. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12080. a, sizeof(a));
  12081. if (ret != BAD_FUNC_ARG)
  12082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12083. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  12084. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12085. a, sizeof(a));
  12086. if (ret != BAD_FUNC_ARG)
  12087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12088. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  12089. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12090. sizeof(a));
  12091. if (ret != BAD_FUNC_ARG)
  12092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12093. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  12094. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12095. sizeof(a));
  12096. if (ret != BAD_FUNC_ARG)
  12097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12098. /* test empty message as null input and output with zero inSz --
  12099. * must either succeed, or fail early with BAD_FUNC_ARG.
  12100. */
  12101. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  12102. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12103. a, sizeof(a));
  12104. if (ret != BAD_FUNC_ARG) {
  12105. if (ret != 0)
  12106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12107. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  12108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12109. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  12110. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  12111. sizeof(t_empty2), a, sizeof(a));
  12112. if (ret != 0)
  12113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12114. }
  12115. /* test empty message as zero-length string -- must work. */
  12116. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  12117. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12118. sizeof(a));
  12119. if (ret != 0)
  12120. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12121. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  12122. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12123. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  12124. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12125. sizeof(a));
  12126. if (ret != 0)
  12127. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12128. wc_AesFree(enc);
  12129. ret = 0;
  12130. out:
  12131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12132. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12133. #endif
  12134. return ret;
  12135. }
  12136. #endif /* WOLFSSL_AES_128 */
  12137. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  12138. {
  12139. wc_test_ret_t ret = 0;
  12140. #ifdef WOLFSSL_AES_128
  12141. if (ret == 0)
  12142. ret = aesccm_128_test();
  12143. #endif
  12144. #ifdef WOLFSSL_AES_256
  12145. if (ret == 0)
  12146. ret = aesccm_256_test();
  12147. #endif
  12148. return ret;
  12149. }
  12150. #endif /* HAVE_AESCCM */
  12151. #if defined(WOLFSSL_AES_EAX) && \
  12152. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  12153. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  12154. {
  12155. typedef struct {
  12156. byte key[AES_256_KEY_SIZE];
  12157. int key_length;
  12158. byte iv[AES_BLOCK_SIZE];
  12159. int iv_length;
  12160. byte aad[AES_BLOCK_SIZE * 2];
  12161. int aad_length;
  12162. byte msg[AES_BLOCK_SIZE * 2];
  12163. int msg_length;
  12164. byte ct[AES_BLOCK_SIZE * 2];
  12165. int ct_length;
  12166. byte tag[AES_BLOCK_SIZE];
  12167. int tag_length;
  12168. int valid;
  12169. } AadVector;
  12170. /* A small selection of Google wycheproof vectors that use vectors
  12171. * from the original paper: eprint.iacr.org/2003/069
  12172. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  12173. */
  12174. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  12175. /* Vector from paper - empty message with auth data */
  12176. {
  12177. /* key, key length */
  12178. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  12179. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  12180. /* iv, iv length */
  12181. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  12182. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  12183. /* aad, aad length */
  12184. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  12185. /* msg, msg length */
  12186. {0}, 0,
  12187. /* ct, ct length */
  12188. {0}, 0,
  12189. /* tag, tag length */
  12190. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  12191. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  12192. /* valid */
  12193. 1,
  12194. },
  12195. /* Vector from paper - no auth data, valid auth tag */
  12196. {
  12197. /* key, key length */
  12198. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12199. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  12200. /* iv , iv length */
  12201. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  12202. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  12203. /* aad, aad length */
  12204. {0}, 0,
  12205. /* msg, msg length */
  12206. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12208. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  12209. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  12210. /* ct, ct length */
  12211. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  12212. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  12213. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  12214. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  12215. /* tag, tag length */
  12216. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  12217. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  12218. /* valid */
  12219. 1,
  12220. },
  12221. /* Vector from paper - no auth data with invalid auth tag */
  12222. {
  12223. /* key, key length */
  12224. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12225. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  12226. /* iv, iv length */
  12227. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  12228. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  12229. /* aad, aad length */
  12230. {0}, 0,
  12231. /* msg, msg length */
  12232. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  12233. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  12234. /* ct , ct length */
  12235. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  12236. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  12237. /* tag, tag length */
  12238. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  12239. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  12240. /* valid */
  12241. 0,
  12242. },
  12243. };
  12244. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  12245. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  12246. wc_test_ret_t ret;
  12247. int i;
  12248. int len;
  12249. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  12250. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  12251. len = sizeof(authtag);
  12252. ret = wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  12253. ciphertext,
  12254. vectors[i].msg, vectors[i].msg_length,
  12255. vectors[i].iv, vectors[i].iv_length,
  12256. authtag, len,
  12257. vectors[i].aad, vectors[i].aad_length);
  12258. if (ret != 0) {
  12259. return WC_TEST_RET_ENC_EC(ret);
  12260. }
  12261. /* check ciphertext matches vector */
  12262. if (XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length)) {
  12263. return WC_TEST_RET_ENC_NC;
  12264. }
  12265. /* check that tag matches vector only for vectors marked as valid */
  12266. ret = XMEMCMP(authtag, vectors[i].tag, len);
  12267. if (vectors[i].valid == 1 && ret != 0 ) {
  12268. return WC_TEST_RET_ENC_NC;
  12269. }
  12270. else if (vectors[i].valid == 0 && ret == 0) {
  12271. return WC_TEST_RET_ENC_NC;
  12272. }
  12273. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  12274. ret = wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  12275. ciphertext,
  12276. vectors[i].ct, vectors[i].ct_length,
  12277. vectors[i].iv, vectors[i].iv_length,
  12278. authtag, len,
  12279. vectors[i].aad, vectors[i].aad_length);
  12280. if (ret != 0) {
  12281. return WC_TEST_RET_ENC_EC(ret);
  12282. }
  12283. /* check decrypted ciphertext matches vector plaintext */
  12284. if (XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length)) {
  12285. return WC_TEST_RET_ENC_NC;
  12286. }
  12287. }
  12288. return 0;
  12289. }
  12290. #endif /* WOLFSSL_AES_EAX */
  12291. #ifdef HAVE_AES_KEYWRAP
  12292. #define MAX_KEYWRAP_TEST_OUTLEN 40
  12293. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  12294. typedef struct keywrapVector {
  12295. const byte* kek;
  12296. const byte* data;
  12297. const byte* verify;
  12298. word32 kekLen;
  12299. word32 dataLen;
  12300. word32 verifyLen;
  12301. } keywrapVector;
  12302. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  12303. {
  12304. int wrapSz, plainSz, testSz, i;
  12305. /* test vectors from RFC 3394 (kek, data, verify) */
  12306. #ifdef WOLFSSL_AES_128
  12307. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  12308. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12309. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12310. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12311. };
  12312. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  12313. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12314. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12315. };
  12316. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  12317. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  12318. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  12319. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  12320. };
  12321. #endif /* WOLFSSL_AES_128 */
  12322. #ifdef WOLFSSL_AES_192
  12323. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  12324. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  12325. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12326. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12327. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  12328. };
  12329. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  12330. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12331. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12332. };
  12333. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  12334. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  12335. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  12336. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  12337. };
  12338. #endif
  12339. #ifdef WOLFSSL_AES_256
  12340. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  12341. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  12342. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12343. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12344. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12345. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12346. };
  12347. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  12348. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12349. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12350. };
  12351. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  12352. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  12353. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  12354. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  12355. };
  12356. #endif
  12357. #ifdef WOLFSSL_AES_192
  12358. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  12359. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  12360. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12361. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12362. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  12363. };
  12364. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  12365. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12366. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12367. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12368. };
  12369. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  12370. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  12371. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  12372. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  12373. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  12374. };
  12375. #endif
  12376. #ifdef WOLFSSL_AES_256
  12377. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  12378. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  12379. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12380. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12381. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12382. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12383. };
  12384. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  12385. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12386. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12387. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12388. };
  12389. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  12390. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  12391. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  12392. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  12393. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  12394. };
  12395. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  12396. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  12397. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12398. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12399. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12400. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12401. };
  12402. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  12403. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12404. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12405. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12406. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12407. };
  12408. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  12409. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  12410. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  12411. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  12412. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  12413. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  12414. };
  12415. #endif /* WOLFSSL_AES_256 */
  12416. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  12417. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  12418. const keywrapVector test_wrap[] =
  12419. {
  12420. #ifdef WOLFSSL_AES_128
  12421. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  12422. #endif
  12423. #ifdef WOLFSSL_AES_192
  12424. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  12425. #endif
  12426. #ifdef WOLFSSL_AES_256
  12427. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  12428. #endif
  12429. #ifdef WOLFSSL_AES_192
  12430. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  12431. #endif
  12432. #ifdef WOLFSSL_AES_256
  12433. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  12434. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  12435. #endif
  12436. };
  12437. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  12438. XMEMSET(output, 0, sizeof(output));
  12439. XMEMSET(plain, 0, sizeof(plain));
  12440. for (i = 0; i < testSz; i++) {
  12441. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  12442. test_wrap[i].data, test_wrap[i].dataLen,
  12443. output, sizeof(output), NULL);
  12444. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  12445. return WC_TEST_RET_ENC_NC;
  12446. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  12447. return WC_TEST_RET_ENC_NC;
  12448. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  12449. output, wrapSz,
  12450. plain, sizeof(plain), NULL);
  12451. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  12452. return WC_TEST_RET_ENC_NC;
  12453. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  12454. return WC_TEST_RET_ENC_I(i);
  12455. }
  12456. return 0;
  12457. }
  12458. #endif /* HAVE_AES_KEYWRAP */
  12459. #endif /* NO_AES */
  12460. #ifdef HAVE_ARIA
  12461. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  12462. {
  12463. #ifndef DEBUG_WOLFSSL
  12464. (void)strName;
  12465. (void)data;
  12466. (void)dataSz;
  12467. #else
  12468. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  12469. WOLFSSL_BUFFER(data,dataSz);
  12470. #endif
  12471. }
  12472. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  12473. {
  12474. int ret = 0;
  12475. byte data[] = TEST_STRING;
  12476. word32 dataSz = TEST_STRING_SZ;
  12477. /* Arbitrarily random long key that we will truncate to the right size */
  12478. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  12479. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  12480. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  12481. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  12482. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  12483. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  12484. wc_Aria aria;
  12485. XMEMSET((void *)&aria, 0, sizeof(aria));
  12486. ret = wc_AriaInitCrypt(&aria, algo);
  12487. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12488. ret = wc_AriaSetKey(&aria, key);
  12489. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12490. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  12491. printOutput("Key", key, keySz);
  12492. WC_RNG rng;
  12493. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12494. if (ret != 0)
  12495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12496. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  12497. if (ret != 0)
  12498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12499. wc_FreeRng(&rng);
  12500. printOutput("Plaintext", data, sizeof(data));
  12501. XMEMSET(cipher, 0, sizeof(cipher));
  12502. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  12503. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  12504. authTag, authTagSz);
  12505. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12506. printOutput("Ciphertext", cipher, sizeof(cipher));
  12507. printOutput("AuthTag", authTag, sizeof(authTag));
  12508. XMEMSET(plain, 0, sizeof(plain));
  12509. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  12510. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  12511. authTag, authTagSz);
  12512. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12513. printOutput("Plaintext", plain, sizeof(plain));
  12514. if (XMEMCMP(plain, data, dataSz) != 0)
  12515. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  12516. out:
  12517. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  12518. else { ret = wc_AriaFreeCrypt(&aria); }
  12519. return ret;
  12520. }
  12521. #endif /* HAVE_ARIA */
  12522. #ifdef HAVE_CAMELLIA
  12523. enum {
  12524. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  12525. };
  12526. typedef struct {
  12527. int type;
  12528. const byte* plaintext;
  12529. const byte* iv;
  12530. const byte* ciphertext;
  12531. const byte* key;
  12532. word32 keySz;
  12533. int errorCode;
  12534. } test_vector_t;
  12535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  12536. {
  12537. /* Camellia ECB Test Plaintext */
  12538. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  12539. {
  12540. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12541. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  12542. };
  12543. /* Camellia ECB Test Initialization Vector */
  12544. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  12545. /* Test 1: Camellia ECB 128-bit key */
  12546. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  12547. {
  12548. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12549. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  12550. };
  12551. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  12552. {
  12553. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  12554. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  12555. };
  12556. /* Test 2: Camellia ECB 192-bit key */
  12557. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  12558. {
  12559. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12560. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  12561. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  12562. };
  12563. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  12564. {
  12565. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  12566. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  12567. };
  12568. /* Test 3: Camellia ECB 256-bit key */
  12569. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  12570. {
  12571. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12572. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  12573. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12574. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  12575. };
  12576. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  12577. {
  12578. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  12579. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  12580. };
  12581. /* Camellia CBC Test Plaintext */
  12582. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  12583. {
  12584. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  12585. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  12586. };
  12587. /* Camellia CBC Test Initialization Vector */
  12588. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  12589. {
  12590. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12591. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12592. };
  12593. /* Test 4: Camellia-CBC 128-bit key */
  12594. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  12595. {
  12596. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  12597. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  12598. };
  12599. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  12600. {
  12601. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  12602. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  12603. };
  12604. /* Test 5: Camellia-CBC 192-bit key */
  12605. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  12606. {
  12607. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  12608. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  12609. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  12610. };
  12611. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  12612. {
  12613. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  12614. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  12615. };
  12616. /* Test 6: CBC 256-bit key */
  12617. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  12618. {
  12619. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  12620. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  12621. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  12622. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  12623. };
  12624. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  12625. {
  12626. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  12627. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  12628. };
  12629. byte out[CAMELLIA_BLOCK_SIZE];
  12630. Camellia cam;
  12631. int i, testsSz, ret;
  12632. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  12633. {
  12634. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  12635. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  12636. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  12637. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  12638. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  12639. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  12640. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  12641. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  12642. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  12643. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  12644. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  12645. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  12646. };
  12647. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  12648. for (i = 0; i < testsSz; i++) {
  12649. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  12650. testVectors[i].iv) != 0)
  12651. return testVectors[i].errorCode;
  12652. switch (testVectors[i].type) {
  12653. case CAM_ECB_ENC:
  12654. ret = wc_CamelliaEncryptDirect(&cam, out,
  12655. testVectors[i].plaintext);
  12656. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  12657. CAMELLIA_BLOCK_SIZE))
  12658. return testVectors[i].errorCode;
  12659. break;
  12660. case CAM_ECB_DEC:
  12661. ret = wc_CamelliaDecryptDirect(&cam, out,
  12662. testVectors[i].ciphertext);
  12663. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  12664. CAMELLIA_BLOCK_SIZE))
  12665. return testVectors[i].errorCode;
  12666. break;
  12667. case CAM_CBC_ENC:
  12668. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  12669. CAMELLIA_BLOCK_SIZE);
  12670. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  12671. CAMELLIA_BLOCK_SIZE))
  12672. return testVectors[i].errorCode;
  12673. break;
  12674. case CAM_CBC_DEC:
  12675. ret = wc_CamelliaCbcDecrypt(&cam, out,
  12676. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  12677. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  12678. CAMELLIA_BLOCK_SIZE))
  12679. return testVectors[i].errorCode;
  12680. break;
  12681. default:
  12682. break;
  12683. }
  12684. }
  12685. /* Setting the IV and checking it was actually set. */
  12686. ret = wc_CamelliaSetIV(&cam, ivc);
  12687. if (ret != 0)
  12688. return WC_TEST_RET_ENC_EC(ret);
  12689. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  12690. return WC_TEST_RET_ENC_NC;
  12691. /* Setting the IV to NULL should be same as all zeros IV */
  12692. ret = wc_CamelliaSetIV(&cam, NULL);
  12693. if (ret != 0)
  12694. return WC_TEST_RET_ENC_EC(ret);
  12695. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  12696. return WC_TEST_RET_ENC_NC;
  12697. /* First parameter should never be null */
  12698. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  12699. return WC_TEST_RET_ENC_NC;
  12700. /* First parameter should never be null, check it fails */
  12701. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  12702. return WC_TEST_RET_ENC_NC;
  12703. /* Key should have a size of 16, 24, or 32 */
  12704. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  12705. return WC_TEST_RET_ENC_NC;
  12706. return 0;
  12707. }
  12708. #endif /* HAVE_CAMELLIA */
  12709. #ifdef WOLFSSL_SM4
  12710. #ifdef WOLFSSL_SM4_ECB
  12711. static int sm4_ecb_test(void)
  12712. {
  12713. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  12714. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12715. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12716. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12717. };
  12718. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  12719. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  12720. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  12721. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  12722. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  12723. };
  12724. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  12725. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  12726. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  12727. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  12728. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  12729. };
  12730. wc_Sm4 sm4;
  12731. byte enc[SM4_BLOCK_SIZE * 4];
  12732. byte dec[SM4_BLOCK_SIZE * 4];
  12733. int ret;
  12734. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12735. if (ret != 0)
  12736. return WC_TEST_RET_ENC_EC(ret);
  12737. /* Encrypt and decrypt with ECB. */
  12738. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12739. if (ret != 0)
  12740. return WC_TEST_RET_ENC_EC(ret);
  12741. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  12742. if (ret != 0)
  12743. return WC_TEST_RET_ENC_EC(ret);
  12744. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  12745. return WC_TEST_RET_ENC_NC;
  12746. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  12747. if (ret != 0)
  12748. return WC_TEST_RET_ENC_EC(ret);
  12749. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  12750. return WC_TEST_RET_ENC_NC;
  12751. wc_Sm4Free(&sm4);
  12752. return 0;
  12753. }
  12754. #endif
  12755. #ifdef WOLFSSL_SM4_CBC
  12756. static int sm4_cbc_test(void)
  12757. {
  12758. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  12759. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12760. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12761. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12762. };
  12763. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  12764. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  12765. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  12766. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  12767. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  12768. };
  12769. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  12770. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12771. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12772. };
  12773. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  12774. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  12775. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  12776. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  12777. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  12778. };
  12779. wc_Sm4 sm4;
  12780. byte enc[SM4_BLOCK_SIZE * 4];
  12781. byte dec[SM4_BLOCK_SIZE * 4];
  12782. int ret;
  12783. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12784. if (ret != 0)
  12785. return WC_TEST_RET_ENC_EC(ret);
  12786. /* Encrypt and decrypt with CBC. */
  12787. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12788. if (ret != 0)
  12789. return WC_TEST_RET_ENC_EC(ret);
  12790. ret = wc_Sm4SetIV(&sm4, i1);
  12791. if (ret != 0)
  12792. return WC_TEST_RET_ENC_EC(ret);
  12793. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  12794. if (ret != 0)
  12795. return WC_TEST_RET_ENC_EC(ret);
  12796. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  12797. return WC_TEST_RET_ENC_NC;
  12798. ret = wc_Sm4SetIV(&sm4, i1);
  12799. if (ret != 0)
  12800. return WC_TEST_RET_ENC_EC(ret);
  12801. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  12802. if (ret != 0)
  12803. return WC_TEST_RET_ENC_EC(ret);
  12804. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  12805. return WC_TEST_RET_ENC_NC;
  12806. /* Encrypt and decrypt in-place with CBC. */
  12807. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12808. if (ret != 0)
  12809. return WC_TEST_RET_ENC_EC(ret);
  12810. ret = wc_Sm4SetIV(&sm4, i1);
  12811. if (ret != 0)
  12812. return WC_TEST_RET_ENC_EC(ret);
  12813. XMEMCPY(enc, p1, sizeof(p1));
  12814. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  12815. if (ret != 0)
  12816. return WC_TEST_RET_ENC_EC(ret);
  12817. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  12818. return WC_TEST_RET_ENC_NC;
  12819. ret = wc_Sm4SetIV(&sm4, i1);
  12820. if (ret != 0)
  12821. return WC_TEST_RET_ENC_EC(ret);
  12822. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  12823. if (ret != 0)
  12824. return WC_TEST_RET_ENC_EC(ret);
  12825. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  12826. return WC_TEST_RET_ENC_NC;
  12827. wc_Sm4Free(&sm4);
  12828. return 0;
  12829. }
  12830. #endif
  12831. #ifdef WOLFSSL_SM4_CTR
  12832. static int sm4_ctr_test(void)
  12833. {
  12834. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  12835. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12836. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12837. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12838. };
  12839. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  12840. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12841. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12842. };
  12843. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  12844. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  12845. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  12846. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  12847. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  12848. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12849. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  12850. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  12851. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  12852. };
  12853. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  12854. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  12855. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  12856. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  12857. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  12858. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  12859. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  12860. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  12861. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  12862. };
  12863. wc_Sm4 sm4;
  12864. byte enc[SM4_BLOCK_SIZE * 4];
  12865. byte dec[SM4_BLOCK_SIZE * 4];
  12866. int chunk;
  12867. int i;
  12868. int ret;
  12869. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12870. if (ret != 0)
  12871. return WC_TEST_RET_ENC_EC(ret);
  12872. /* Encrypt and decrypt using encrypt with CTR. */
  12873. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12874. if (ret != 0)
  12875. return WC_TEST_RET_ENC_EC(ret);
  12876. ret = wc_Sm4SetIV(&sm4, i1);
  12877. if (ret != 0)
  12878. return WC_TEST_RET_ENC_EC(ret);
  12879. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  12880. if (ret != 0)
  12881. return WC_TEST_RET_ENC_EC(ret);
  12882. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  12883. return WC_TEST_RET_ENC_NC;
  12884. ret = wc_Sm4SetIV(&sm4, i1);
  12885. if (ret != 0)
  12886. return WC_TEST_RET_ENC_EC(ret);
  12887. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  12888. if (ret != 0)
  12889. return WC_TEST_RET_ENC_EC(ret);
  12890. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  12891. return WC_TEST_RET_ENC_NC;
  12892. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  12893. ret = wc_Sm4SetIV(&sm4, i1);
  12894. if (ret != 0)
  12895. return WC_TEST_RET_ENC_I(chunk);
  12896. XMEMSET(enc, 0, sizeof(enc));
  12897. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  12898. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  12899. if (ret != 0)
  12900. return WC_TEST_RET_ENC_I(i);
  12901. }
  12902. if (i < (int)sizeof(p2)) {
  12903. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  12904. if (ret != 0)
  12905. return WC_TEST_RET_ENC_I(chunk);
  12906. }
  12907. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  12908. return WC_TEST_RET_ENC_I(chunk);
  12909. }
  12910. wc_Sm4Free(&sm4);
  12911. return 0;
  12912. }
  12913. #endif
  12914. #ifdef WOLFSSL_SM4_GCM
  12915. static int sm4_gcm_test(void)
  12916. {
  12917. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12918. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12919. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12920. };
  12921. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  12922. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  12923. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  12924. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  12925. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  12926. };
  12927. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  12928. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12929. 0x08, 0x09, 0x0A, 0x0B
  12930. };
  12931. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  12932. 0xFF, 0xEE, 0xDD
  12933. };
  12934. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  12935. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  12936. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  12937. };
  12938. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  12939. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  12940. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  12941. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  12942. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  12943. };
  12944. /* RFC8998 A.1. */
  12945. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  12946. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  12947. 0x00, 0x00, 0xAB, 0xCD
  12948. };
  12949. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  12950. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12951. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12952. };
  12953. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  12954. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  12955. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  12956. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  12957. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  12958. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12959. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  12960. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12961. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  12962. };
  12963. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  12964. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12965. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12966. 0xAB, 0xAD, 0xDA, 0xD2
  12967. };
  12968. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  12969. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  12970. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  12971. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  12972. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  12973. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  12974. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  12975. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  12976. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  12977. };
  12978. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  12979. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  12980. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  12981. };
  12982. wc_Sm4 sm4;
  12983. byte enc[SM4_BLOCK_SIZE * 4];
  12984. byte dec[SM4_BLOCK_SIZE * 4];
  12985. byte tag[SM4_BLOCK_SIZE];
  12986. int ret;
  12987. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12988. if (ret != 0)
  12989. return WC_TEST_RET_ENC_EC(ret);
  12990. /* Encrypt and decrypt using encrypt with GCM. */
  12991. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  12992. if (ret != 0)
  12993. return WC_TEST_RET_ENC_EC(ret);
  12994. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  12995. sizeof(tag), a1, sizeof(a1));
  12996. if (ret != 0)
  12997. return WC_TEST_RET_ENC_EC(ret);
  12998. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  12999. return WC_TEST_RET_ENC_NC;
  13000. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  13001. return WC_TEST_RET_ENC_NC;
  13002. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  13003. sizeof(tag), a1, sizeof(a1));
  13004. if (ret != 0)
  13005. return WC_TEST_RET_ENC_EC(ret);
  13006. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13007. return WC_TEST_RET_ENC_NC;
  13008. /* RFC8998 test vector. */
  13009. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  13010. if (ret != 0)
  13011. return WC_TEST_RET_ENC_EC(ret);
  13012. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  13013. sizeof(tag), a2, sizeof(a2));
  13014. if (ret != 0)
  13015. return WC_TEST_RET_ENC_EC(ret);
  13016. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  13017. return WC_TEST_RET_ENC_NC;
  13018. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  13019. return WC_TEST_RET_ENC_NC;
  13020. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  13021. sizeof(tag), a2, sizeof(a2));
  13022. if (ret != 0)
  13023. return WC_TEST_RET_ENC_EC(ret);
  13024. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  13025. return WC_TEST_RET_ENC_NC;
  13026. wc_Sm4Free(&sm4);
  13027. return 0;
  13028. }
  13029. #endif
  13030. #ifdef WOLFSSL_SM4_CCM
  13031. static int sm4_ccm_test(void)
  13032. {
  13033. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13034. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13035. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13036. };
  13037. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  13038. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  13039. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  13040. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  13041. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  13042. };
  13043. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  13044. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13045. 0x08, 0x09, 0x0A, 0x0B
  13046. };
  13047. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  13048. 0xFF, 0xEE, 0xDD
  13049. };
  13050. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  13051. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  13052. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  13053. };
  13054. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  13055. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  13056. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  13057. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  13058. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  13059. };
  13060. /* RFC8998 A.1. */
  13061. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  13062. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  13063. 0x00, 0x00, 0xAB, 0xCD
  13064. };
  13065. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  13066. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13067. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13068. };
  13069. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  13070. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  13071. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  13072. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  13073. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  13074. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13075. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  13076. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13077. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  13078. };
  13079. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  13080. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13081. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13082. 0xAB, 0xAD, 0xDA, 0xD2
  13083. };
  13084. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  13085. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  13086. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  13087. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  13088. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  13089. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  13090. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  13091. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  13092. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  13093. };
  13094. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  13095. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  13096. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  13097. };
  13098. wc_Sm4 sm4;
  13099. byte enc[SM4_BLOCK_SIZE * 4];
  13100. byte dec[SM4_BLOCK_SIZE * 4];
  13101. byte tag[SM4_BLOCK_SIZE];
  13102. int ret;
  13103. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13104. if (ret != 0)
  13105. return -6720;
  13106. /* Encrypt and decrypt using encrypt with CCM. */
  13107. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13108. if (ret != 0)
  13109. return WC_TEST_RET_ENC_EC(ret);
  13110. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  13111. sizeof(tag), a1, sizeof(a1));
  13112. if (ret != 0)
  13113. return WC_TEST_RET_ENC_EC(ret);
  13114. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  13115. return WC_TEST_RET_ENC_NC;
  13116. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  13117. return WC_TEST_RET_ENC_NC;
  13118. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  13119. sizeof(tag), a1, sizeof(a1));
  13120. if (ret != 0)
  13121. return WC_TEST_RET_ENC_EC(ret);
  13122. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13123. return WC_TEST_RET_ENC_NC;
  13124. /* RFC8998 test vector. */
  13125. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  13126. if (ret != 0)
  13127. return WC_TEST_RET_ENC_EC(ret);
  13128. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  13129. sizeof(tag), a2, sizeof(a2));
  13130. if (ret != 0)
  13131. return WC_TEST_RET_ENC_EC(ret);
  13132. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  13133. return WC_TEST_RET_ENC_NC;
  13134. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  13135. return WC_TEST_RET_ENC_NC;
  13136. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  13137. sizeof(tag), a2, sizeof(a2));
  13138. if (ret != 0)
  13139. return WC_TEST_RET_ENC_EC(ret);
  13140. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  13141. return WC_TEST_RET_ENC_NC;
  13142. wc_Sm4Free(&sm4);
  13143. return 0;
  13144. }
  13145. #endif
  13146. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  13147. {
  13148. wc_test_ret_t ret;
  13149. #ifdef WOLFSSL_SM4_ECB
  13150. ret = sm4_ecb_test();
  13151. if (ret != 0)
  13152. return ret;
  13153. #endif
  13154. #ifdef WOLFSSL_SM4_CBC
  13155. ret = sm4_cbc_test();
  13156. if (ret != 0)
  13157. return ret;
  13158. #endif
  13159. #ifdef WOLFSSL_SM4_CTR
  13160. ret = sm4_ctr_test();
  13161. if (ret != 0)
  13162. return ret;
  13163. #endif
  13164. #ifdef WOLFSSL_SM4_GCM
  13165. ret = sm4_gcm_test();
  13166. if (ret != 0)
  13167. return ret;
  13168. #endif
  13169. #ifdef WOLFSSL_SM4_CCM
  13170. ret = sm4_ccm_test();
  13171. if (ret != 0)
  13172. return ret;
  13173. #endif
  13174. return 0;
  13175. }
  13176. #endif
  13177. #ifdef HAVE_XCHACHA
  13178. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  13179. wc_test_ret_t ret;
  13180. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  13181. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  13182. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  13183. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  13184. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  13185. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  13186. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  13187. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  13188. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  13189. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  13190. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  13191. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  13192. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  13193. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  13194. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  13195. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  13196. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  13197. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  13198. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  13199. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  13200. };
  13201. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  13202. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13203. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13204. };
  13205. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  13206. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  13207. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  13208. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  13209. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  13210. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  13211. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  13212. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  13213. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  13214. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  13215. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  13216. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  13217. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  13218. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  13219. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  13220. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  13221. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  13222. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  13223. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  13224. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  13225. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  13226. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  13227. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  13228. };
  13229. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13230. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  13231. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13232. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13233. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  13234. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13235. #else
  13236. struct ChaCha chacha[1];
  13237. byte buf1[sizeof Plaintext];
  13238. byte buf2[sizeof Plaintext];
  13239. #endif
  13240. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  13241. if (ret < 0)
  13242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13243. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  13244. if (ret < 0)
  13245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13246. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  13247. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13248. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  13249. if (ret < 0)
  13250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13251. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  13252. if (ret < 0)
  13253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13254. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  13255. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13256. out:
  13257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13258. if (chacha)
  13259. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  13260. if (buf1)
  13261. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13262. if (buf2)
  13263. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13264. #endif
  13265. return ret;
  13266. }
  13267. #endif /* HAVE_XCHACHA */
  13268. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  13269. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  13270. wc_test_ret_t ret;
  13271. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  13272. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  13273. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  13274. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  13275. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  13276. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  13277. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  13278. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  13279. 0x74, 0x2e }; /* t. */
  13280. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  13281. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  13282. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13283. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13284. };
  13285. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  13286. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  13287. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  13288. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  13289. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  13290. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  13291. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  13292. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  13293. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  13294. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  13295. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  13296. 0xb5, 0x2e };
  13297. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  13298. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  13299. };
  13300. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13301. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13302. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13303. if ((buf1 == NULL) || (buf2 == NULL))
  13304. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13305. #else
  13306. byte buf1[sizeof Ciphertext + sizeof Tag];
  13307. byte buf2[sizeof Plaintext];
  13308. #endif
  13309. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  13310. Plaintext, sizeof Plaintext,
  13311. AAD, sizeof AAD,
  13312. IV, sizeof IV,
  13313. Key, sizeof Key);
  13314. if (ret < 0)
  13315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13316. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  13317. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13318. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  13319. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13320. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  13321. buf1, sizeof Ciphertext + sizeof Tag,
  13322. AAD, sizeof AAD,
  13323. IV, sizeof IV,
  13324. Key, sizeof Key);
  13325. if (ret < 0)
  13326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13327. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  13328. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13329. out:
  13330. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13331. if (buf1 != NULL)
  13332. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13333. if (buf2 != NULL)
  13334. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13335. #endif
  13336. return ret;
  13337. }
  13338. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  13339. #ifndef WC_NO_RNG
  13340. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  13341. {
  13342. byte block[32];
  13343. wc_test_ret_t ret;
  13344. int i;
  13345. XMEMSET(block, 0, sizeof(block));
  13346. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  13347. if (ret != 0) {
  13348. ret = 1;
  13349. goto exit;
  13350. }
  13351. /* Check for 0's */
  13352. for (i=0; i<(int)sizeof(block); i++) {
  13353. if (block[i] == 0) {
  13354. ret++;
  13355. }
  13356. }
  13357. /* All zeros count check */
  13358. if (ret >= (int)sizeof(block)) {
  13359. ret = 2;
  13360. goto exit;
  13361. }
  13362. ret = wc_RNG_GenerateByte(rng, block);
  13363. if (ret != 0) {
  13364. ret = 3;
  13365. goto exit;
  13366. }
  13367. /* Parameter validation testing. */
  13368. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  13369. if (ret != BAD_FUNC_ARG) {
  13370. ret = 4;
  13371. goto exit;
  13372. }
  13373. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  13374. if (ret != BAD_FUNC_ARG) {
  13375. ret = 5;
  13376. goto exit;
  13377. }
  13378. ret = wc_RNG_GenerateByte(NULL, block);
  13379. if (ret != BAD_FUNC_ARG) {
  13380. ret = 6;
  13381. goto exit;
  13382. }
  13383. ret = wc_RNG_GenerateByte(rng, NULL);
  13384. if (ret != BAD_FUNC_ARG) {
  13385. ret = 7;
  13386. goto exit;
  13387. }
  13388. ret = 0;
  13389. exit:
  13390. if (ret != 0)
  13391. ret = errorOffset - (ret * 1000000);
  13392. return ret;
  13393. }
  13394. static wc_test_ret_t random_rng_test(void)
  13395. {
  13396. WC_RNG localRng;
  13397. WC_RNG* rng;
  13398. wc_test_ret_t ret;
  13399. rng = &localRng;
  13400. /* Test stack based RNG. */
  13401. #ifndef HAVE_FIPS
  13402. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  13403. #else
  13404. ret = wc_InitRng(rng);
  13405. #endif
  13406. if (ret != 0)
  13407. return WC_TEST_RET_ENC_EC(ret);
  13408. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  13409. /* Make sure and free RNG */
  13410. wc_FreeRng(rng);
  13411. if (ret != 0)
  13412. return ret;
  13413. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  13414. {
  13415. byte nonce[8] = { 0 };
  13416. /* Test dynamic RNG. */
  13417. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  13418. if (rng == NULL)
  13419. return WC_TEST_RET_ENC_ERRNO;
  13420. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  13421. wc_rng_free(rng);
  13422. }
  13423. #endif
  13424. return ret;
  13425. }
  13426. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  13427. #ifdef WC_RNG_SEED_CB
  13428. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  13429. {
  13430. word32 i;
  13431. (void)os;
  13432. /* Known answer test. Set the seed to the same value every time. */
  13433. for (i = 0; i < sz; i++)
  13434. output[i] = (byte)i;
  13435. return 0;
  13436. }
  13437. static wc_test_ret_t rng_seed_test(void)
  13438. {
  13439. #ifndef HAVE_FIPS
  13440. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  13441. {
  13442. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  13443. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  13444. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  13445. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  13446. };
  13447. #else
  13448. /* FIPS uses a longer seed, so different check value. */
  13449. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  13450. {
  13451. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  13452. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  13453. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  13454. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  13455. };
  13456. #endif
  13457. byte output[WC_SHA256_DIGEST_SIZE];
  13458. WC_RNG rng;
  13459. wc_test_ret_t ret;
  13460. ret = wc_SetSeed_Cb(seed_cb);
  13461. if (ret != 0) {
  13462. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13463. }
  13464. ret = wc_InitRng(&rng);
  13465. if (ret != 0) {
  13466. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13467. }
  13468. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  13469. if (ret != 0) {
  13470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13471. }
  13472. ret = XMEMCMP(output, check, sizeof(output));
  13473. if (ret != 0) {
  13474. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13475. }
  13476. ret = wc_FreeRng(&rng);
  13477. if (ret != 0) {
  13478. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13479. }
  13480. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  13481. if (ret != 0) {
  13482. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13483. }
  13484. out:
  13485. return ret;
  13486. }
  13487. #endif
  13488. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  13489. {
  13490. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  13491. {
  13492. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  13493. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  13494. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  13495. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  13496. };
  13497. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  13498. {
  13499. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  13500. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  13501. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  13502. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  13503. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  13504. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  13505. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  13506. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  13507. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  13508. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  13509. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  13510. };
  13511. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  13512. {
  13513. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  13514. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  13515. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  13516. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  13517. };
  13518. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  13519. {
  13520. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  13521. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  13522. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  13523. };
  13524. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  13525. {
  13526. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  13527. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  13528. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  13529. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  13530. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  13531. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  13532. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  13533. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  13534. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  13535. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  13536. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  13537. };
  13538. byte output[WC_SHA256_DIGEST_SIZE * 4];
  13539. wc_test_ret_t ret;
  13540. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  13541. output, sizeof(output));
  13542. if (ret != 0)
  13543. return WC_TEST_RET_ENC_EC(ret);
  13544. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  13545. return WC_TEST_RET_ENC_NC;
  13546. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  13547. test2EntropyB, sizeof(test2EntropyB),
  13548. output, sizeof(output));
  13549. if (ret != 0)
  13550. return WC_TEST_RET_ENC_EC(ret);
  13551. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  13552. return WC_TEST_RET_ENC_NC;
  13553. /* Basic RNG generate block test */
  13554. if ((ret = random_rng_test()) != 0)
  13555. return ret;
  13556. /* Test the seed check function. */
  13557. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  13558. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13559. {
  13560. word32 i, outputSz;
  13561. /* Repeat the same byte over and over. Should fail. */
  13562. outputSz = sizeof(output);
  13563. XMEMSET(output, 1, outputSz);
  13564. ret = wc_RNG_TestSeed(output, outputSz);
  13565. if (ret == 0)
  13566. return WC_TEST_RET_ENC_NC;
  13567. /* Every byte of the entropy scratch is different,
  13568. * entropy is a single byte that shouldn't match. */
  13569. outputSz = (sizeof(output) / 2) + 1;
  13570. for (i = 0; i < outputSz; i++)
  13571. output[i] = (byte)i;
  13572. ret = wc_RNG_TestSeed(output, outputSz);
  13573. if (ret != 0)
  13574. return WC_TEST_RET_ENC_EC(ret);
  13575. outputSz = sizeof(output);
  13576. for (i = 0; i < outputSz; i++)
  13577. output[i] = (byte)i;
  13578. ret = wc_RNG_TestSeed(output, outputSz);
  13579. if (ret != 0)
  13580. return WC_TEST_RET_ENC_EC(ret);
  13581. }
  13582. #endif
  13583. /* Test the seed callback. */
  13584. #ifdef WC_RNG_SEED_CB
  13585. if ((ret = rng_seed_test()) != 0)
  13586. return ret;
  13587. #endif
  13588. return 0;
  13589. }
  13590. #else
  13591. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  13592. {
  13593. /* Basic RNG generate block test */
  13594. return random_rng_test();
  13595. }
  13596. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  13597. #endif /* WC_NO_RNG */
  13598. #ifndef MEM_TEST_SZ
  13599. #define MEM_TEST_SZ 1024
  13600. #endif
  13601. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  13602. static int simple_mem_test(int sz)
  13603. {
  13604. int ret = 0;
  13605. byte* b;
  13606. int i;
  13607. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13608. if (b == NULL) {
  13609. return WC_TEST_RET_ENC_NC;
  13610. }
  13611. /* utilize memory */
  13612. for (i = 0; i < sz; i++) {
  13613. b[i] = (byte)i;
  13614. }
  13615. /* read back and verify */
  13616. for (i = 0; i < sz; i++) {
  13617. if (b[i] != (byte)i) {
  13618. ret = WC_TEST_RET_ENC_NC;
  13619. break;
  13620. }
  13621. }
  13622. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13623. return ret;
  13624. }
  13625. #endif
  13626. /* If successful, returns the first letter of the byte array `in`.
  13627. **
  13628. ** This is a deceptively simple test of a read-only embedded Linux file system.
  13629. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  13630. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  13631. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  13632. **
  13633. ** Edit with caution. See PR #6523. */
  13634. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  13635. {
  13636. wc_test_ret_t ret = 0;
  13637. volatile word32 j = -1; /* must be volatile to properly detect error */
  13638. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  13639. (void)ret;
  13640. j = *outJ; /* Found index to use in const array. */
  13641. if (j == 0) {
  13642. #ifdef WOLFSSL_DEBUG
  13643. printf("Testing const byte ptr reference...\n");
  13644. #endif
  13645. /* although j is zero, in[0] does not detect the Illegal instruction */
  13646. ret = in[j]; /* The big test: can we actually access the `in` data? */
  13647. }
  13648. else {
  13649. ret = -1;
  13650. }
  13651. return ret;
  13652. }
  13653. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  13654. {
  13655. wc_test_ret_t ret = 0;
  13656. word32 j = 0; /* used in embedded const pointer test */
  13657. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  13658. int i;
  13659. #endif
  13660. #ifdef WOLFSSL_STATIC_MEMORY
  13661. word32 size[] = { WOLFMEM_BUCKETS };
  13662. word32 dist[] = { WOLFMEM_DIST };
  13663. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  13664. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  13665. /* pad to account for if head of buffer is not at set memory
  13666. * alignment when tests are ran */
  13667. #endif
  13668. #ifdef WOLFSSL_STATIC_MEMORY
  13669. /* check macro settings */
  13670. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  13671. return WC_TEST_RET_ENC_NC;
  13672. }
  13673. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  13674. return WC_TEST_RET_ENC_NC;
  13675. }
  13676. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  13677. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  13678. /* each element in array should be divisible by alignment size */
  13679. return WC_TEST_RET_ENC_NC;
  13680. }
  13681. }
  13682. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  13683. if (size[i - 1] >= size[i]) {
  13684. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  13685. }
  13686. }
  13687. /* check that padding size returned is possible */
  13688. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  13689. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  13690. }
  13691. ret = wolfSSL_MemoryPaddingSz();
  13692. if (ret < 0) {
  13693. return WC_TEST_RET_ENC_EC(ret);
  13694. }
  13695. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  13696. return WC_TEST_RET_ENC_NC; /* not aligned! */
  13697. }
  13698. /* check function to return optimum buffer size (rounded down) */
  13699. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  13700. if (ret < 0) {
  13701. return WC_TEST_RET_ENC_EC(ret);
  13702. }
  13703. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  13704. return WC_TEST_RET_ENC_NC; /* not aligned! */
  13705. }
  13706. if ((unsigned int)ret > sizeof(buffer)) {
  13707. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  13708. }
  13709. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  13710. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  13711. * value
  13712. */
  13713. }
  13714. ret = wolfSSL_MemoryPaddingSz();
  13715. ret += pad; /* add space that is going to be needed if buffer not aligned */
  13716. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  13717. (ret + (int)size[0])) {
  13718. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  13719. * value
  13720. */
  13721. }
  13722. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  13723. if ((ret - pad) < 0) {
  13724. return WC_TEST_RET_ENC_NC;
  13725. }
  13726. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  13727. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  13728. }
  13729. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  13730. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  13731. }
  13732. /* check for passing bad or unknown arguments to functions */
  13733. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  13734. return WC_TEST_RET_ENC_NC;
  13735. }
  13736. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  13737. return WC_TEST_RET_ENC_NC; /* should round to 0
  13738. since struct + bucket will not fit */
  13739. }
  13740. (void)dist; /* avoid static analysis warning of variable not used */
  13741. #endif
  13742. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  13743. /* simple test */
  13744. ret = simple_mem_test(MEM_TEST_SZ);
  13745. if (ret != 0)
  13746. return ret;
  13747. #endif
  13748. #ifdef COMPLEX_MEM_TEST
  13749. /* test various size blocks */
  13750. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  13751. ret = simple_mem_test(i);
  13752. if (ret != 0)
  13753. return ret;
  13754. }
  13755. #endif
  13756. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  13757. /* realloc test */
  13758. {
  13759. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  13760. DYNAMIC_TYPE_TMP_BUFFER);
  13761. #ifndef WOLFSSL_NO_REALLOC
  13762. byte *c = NULL;
  13763. if (b) {
  13764. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  13765. DYNAMIC_TYPE_TMP_BUFFER);
  13766. if (c)
  13767. b = c;
  13768. }
  13769. #endif
  13770. if (b)
  13771. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13772. if ((b == NULL)
  13773. #ifndef WOLFSSL_NO_REALLOC
  13774. || (c == NULL)
  13775. #endif
  13776. ) {
  13777. return WC_TEST_RET_ENC_ERRNO;
  13778. }
  13779. }
  13780. #endif
  13781. if (ret == 0) {
  13782. /* This test is only interesting on embedded R/O Flash systems */
  13783. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  13784. ret = 1;
  13785. }
  13786. }
  13787. return ret;
  13788. }
  13789. #ifndef NO_FILESYSTEM
  13790. /* Cert Paths */
  13791. #ifdef FREESCALE_MQX
  13792. #define CERT_PREFIX "a:\\"
  13793. #define CERT_PATH_SEP "\\"
  13794. #elif defined(WOLFSSL_uTKERNEL2)
  13795. #define CERT_PREFIX "/uda/"
  13796. #define CERT_PATH_SEP "/"
  13797. #elif defined(_WIN32_WCE)
  13798. #define CERT_PREFIX "\\windows\\"
  13799. #define CERT_PATH_SEP "\\"
  13800. #endif
  13801. #ifndef CERT_PREFIX
  13802. #define CERT_PREFIX "./"
  13803. #endif
  13804. #ifndef CERT_PATH_SEP
  13805. #define CERT_PATH_SEP "/"
  13806. #endif
  13807. #ifndef CERT_WRITE_TEMP_DIR
  13808. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  13809. #endif
  13810. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  13811. /* Generated Test Certs */
  13812. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13813. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  13814. #if !defined(NO_RSA) && !defined(NO_ASN)
  13815. static const char* clientKey = CERT_ROOT "client-key.der";
  13816. static const char* clientCert = CERT_ROOT "client-cert.der";
  13817. #ifdef WOLFSSL_CERT_EXT
  13818. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  13819. #endif
  13820. #endif /* !NO_RSA && !NO_ASN */
  13821. #endif
  13822. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13823. #if !defined(NO_RSA) && !defined(NO_ASN)
  13824. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  13825. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  13826. #ifdef WOLFSSL_CERT_GEN
  13827. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  13828. #endif
  13829. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  13830. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  13831. #endif
  13832. #ifdef HAVE_PKCS7
  13833. static const char* rsaServerCertDerFile =
  13834. CERT_ROOT "server-cert.der";
  13835. static const char* rsaServerKeyDerFile =
  13836. CERT_ROOT "server-key.der";
  13837. #endif
  13838. #endif
  13839. #endif /* !NO_RSA && !NO_ASN */
  13840. #endif /* !USE_CERT_BUFFER_* */
  13841. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13842. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  13843. #if !defined(NO_ASN) && !defined(NO_DH)
  13844. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  13845. #endif
  13846. #endif
  13847. #if !defined(NO_ASN) && !defined(NO_DH)
  13848. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  13849. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  13850. #if !defined(USE_CERT_BUFFERS_2048)
  13851. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  13852. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  13853. #endif
  13854. #endif
  13855. #endif
  13856. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13857. #ifndef NO_DSA
  13858. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  13859. #endif
  13860. #endif /* !USE_CERT_BUFFER_* */
  13861. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  13862. #ifdef HAVE_ECC
  13863. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  13864. #ifdef HAVE_ECC_KEY_IMPORT
  13865. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  13866. #endif
  13867. #endif
  13868. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  13869. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  13870. !defined(NO_ECC_SECP)
  13871. #ifndef NO_RSA
  13872. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  13873. #endif
  13874. #ifndef NO_ASN_TIME
  13875. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  13876. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  13877. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  13878. static const char* eccCaKey384File =
  13879. CERT_ROOT "ca-ecc384-key.der";
  13880. static const char* eccCaCert384File =
  13881. CERT_ROOT "ca-ecc384-cert.pem";
  13882. #endif
  13883. #endif
  13884. #endif
  13885. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  13886. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  13887. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  13888. #endif
  13889. #endif /* HAVE_ECC */
  13890. #ifdef HAVE_ED25519
  13891. #ifdef WOLFSSL_TEST_CERT
  13892. static const char* serverEd25519Cert =
  13893. CERT_ROOT "ed25519/server-ed25519.der";
  13894. static const char* caEd25519Cert =
  13895. CERT_ROOT "ed25519/ca-ed25519.der";
  13896. #endif
  13897. #endif
  13898. #ifdef HAVE_ED448
  13899. #ifdef WOLFSSL_TEST_CERT
  13900. static const char* serverEd448Cert =
  13901. CERT_ROOT "ed448/server-ed448.der";
  13902. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  13903. #endif
  13904. #endif
  13905. #endif /* !USE_CERT_BUFFER_* */
  13906. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  13907. !defined(NO_FILESYSTEM)
  13908. static const char* certExtNc =
  13909. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  13910. static const char* certExtIa =
  13911. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  13912. static const char* certExtNct =
  13913. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  13914. #endif
  13915. #ifndef NO_WRITE_TEMP_FILES
  13916. #ifdef HAVE_ECC
  13917. #ifndef NO_ECC_SECP
  13918. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  13919. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  13920. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  13921. #endif
  13922. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  13923. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  13924. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  13925. #endif
  13926. #endif
  13927. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  13928. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  13929. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  13930. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  13931. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  13932. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  13933. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  13934. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  13935. #endif
  13936. #endif /* HAVE_ECC_KEY_EXPORT */
  13937. #endif /* HAVE_ECC */
  13938. #ifndef NO_RSA
  13939. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  13940. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  13941. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  13942. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  13943. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  13944. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  13945. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  13946. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  13947. #endif
  13948. #endif
  13949. #endif /* !NO_RSA */
  13950. #if !defined(NO_RSA) || !defined(NO_DSA)
  13951. #ifdef WOLFSSL_KEY_GEN
  13952. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  13953. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  13954. #endif
  13955. #endif
  13956. #endif /* !NO_WRITE_TEMP_FILES */
  13957. #endif /* !NO_FILESYSTEM */
  13958. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  13959. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  13960. static CertName certDefaultName;
  13961. static void initDefaultName(void)
  13962. {
  13963. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  13964. NameAttrib* n;
  13965. #endif
  13966. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  13967. certDefaultName.countryEnc = CTC_PRINTABLE;
  13968. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  13969. certDefaultName.stateEnc = CTC_UTF8;
  13970. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  13971. certDefaultName.streetEnc = CTC_UTF8;
  13972. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  13973. certDefaultName.localityEnc = CTC_UTF8;
  13974. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  13975. certDefaultName.surEnc = CTC_UTF8;
  13976. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  13977. certDefaultName.orgEnc = CTC_UTF8;
  13978. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  13979. certDefaultName.unitEnc = CTC_UTF8;
  13980. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  13981. certDefaultName.commonNameEnc = CTC_UTF8;
  13982. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  13983. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  13984. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  13985. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  13986. #ifdef WOLFSSL_CERT_EXT
  13987. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  13988. certDefaultName.busCatEnc = CTC_UTF8;
  13989. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  13990. certDefaultName.joiStEnc = CTC_PRINTABLE;
  13991. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  13992. certDefaultName.joiCEnc = CTC_PRINTABLE;
  13993. #endif
  13994. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  13995. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  13996. certDefaultName.userIdEnc = CTC_PRINTABLE;
  13997. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  13998. /* test having additional OUs and setting DC */
  13999. n = &certDefaultName.name[0];
  14000. n->id = ASN_ORGUNIT_NAME;
  14001. n->type = CTC_UTF8;
  14002. n->sz = XSTRLEN("Development-2");
  14003. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  14004. #if CTC_MAX_ATTRIB > 3
  14005. n = &certDefaultName.name[1];
  14006. n->id = ASN_DOMAIN_COMPONENT;
  14007. n->type = CTC_UTF8;
  14008. n->sz = XSTRLEN("com");
  14009. XMEMCPY(n->value, "com", sizeof("com"));
  14010. n = &certDefaultName.name[2];
  14011. n->id = ASN_DOMAIN_COMPONENT;
  14012. n->type = CTC_UTF8;
  14013. n->sz = XSTRLEN("wolfssl");
  14014. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  14015. #endif
  14016. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  14017. #ifdef WOLFSSL_CUSTOM_OID
  14018. /* TODO: Add test case for custom OID's */
  14019. #endif
  14020. }
  14021. #ifdef WOLFSSL_CERT_EXT
  14022. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  14023. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  14024. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  14025. "digitalSignature,nonRepudiation";
  14026. #endif
  14027. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  14028. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  14029. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  14030. #endif
  14031. #endif /* WOLFSSL_CERT_EXT */
  14032. #endif /* WOLFSSL_CERT_GEN */
  14033. #ifndef NO_RSA
  14034. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  14035. !defined(NO_FILESYSTEM)
  14036. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  14037. static byte minName[] = { 0x30, 0x00 };
  14038. static byte nameBad[] = {
  14039. 0x30, 0x08,
  14040. 0x31, 0x06,
  14041. 0x30, 0x04,
  14042. 0x06, 0x02,
  14043. 0x55, 0x04,
  14044. };
  14045. static byte minDates[] = {
  14046. 0x30, 0x1e,
  14047. 0x17, 0x0d,
  14048. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  14049. 0x32, 0x33, 0x31, 0x30, 0x5a,
  14050. 0x17, 0x0d,
  14051. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  14052. 0x32, 0x33, 0x31, 0x30, 0x5a
  14053. };
  14054. static byte minPubKey[] = {
  14055. 0x30, 0x1c,
  14056. 0x30, 0x0d,
  14057. 0x06, 0x09,
  14058. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  14059. 0x01,
  14060. 0x05, 0x00,
  14061. 0x03, 0x0b,
  14062. 0x00, 0x30, 0x08,
  14063. 0x02, 0x01,
  14064. 0x03,
  14065. 0x02, 0x03,
  14066. 0x01, 0x00, 0x01
  14067. };
  14068. static byte minSigAlg[] = {
  14069. 0x30, 0x0d,
  14070. 0x06, 0x09,
  14071. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  14072. 0x0b,
  14073. 0x05, 0x00
  14074. };
  14075. static byte minSig[] = {
  14076. 0x03, 0x01,
  14077. 0x00
  14078. };
  14079. static int add_seq(byte* certData, int offset, byte* data, byte length)
  14080. {
  14081. XMEMMOVE(certData + offset + 2, data, length);
  14082. certData[offset++] = 0x30;
  14083. certData[offset++] = length;
  14084. return offset + length;
  14085. }
  14086. static int add_data(byte* certData, int offset, byte* data, byte length)
  14087. {
  14088. XMEMCPY(certData + offset, data, length);
  14089. return offset + length;
  14090. }
  14091. static wc_test_ret_t cert_asn1_test(void)
  14092. {
  14093. wc_test_ret_t ret;
  14094. int len[3];
  14095. DecodedCert cert;
  14096. byte certData[114];
  14097. byte* badCert = NULL;
  14098. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  14099. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  14100. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14101. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  14102. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14103. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  14104. len[1] = add_seq(certData, 0, certData, len[2]);
  14105. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  14106. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  14107. len[0] = add_seq(certData, 0, certData, len[1]);
  14108. /* Minimal good certificate */
  14109. InitDecodedCert(&cert, certData, len[0], 0);
  14110. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14111. FreeDecodedCert(&cert);
  14112. if (ret != 0)
  14113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14114. /* Bad issuer name */
  14115. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  14116. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  14117. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  14118. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  14119. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14120. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  14121. len[1] = add_seq(certData, 0, certData, len[2]);
  14122. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  14123. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  14124. len[0] = add_seq(certData, 0, certData, len[1]);
  14125. /* Put data into allocated buffer to allow access error checking. */
  14126. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14127. XMEMCPY(badCert, certData, len[0]);
  14128. InitDecodedCert(&cert, badCert, len[0], 0);
  14129. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14130. FreeDecodedCert(&cert);
  14131. if (ret != ASN_PARSE_E) {
  14132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14133. }
  14134. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14135. badCert = NULL;
  14136. ret = 0;
  14137. done:
  14138. if (badCert != NULL)
  14139. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14140. return ret;
  14141. }
  14142. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  14143. {
  14144. #if !defined(NO_FILESYSTEM)
  14145. DecodedCert cert;
  14146. byte* tmp;
  14147. size_t bytes;
  14148. XFILE file;
  14149. wc_test_ret_t ret;
  14150. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14151. if (tmp == NULL)
  14152. return WC_TEST_RET_ENC_ERRNO;
  14153. /* Certificate with Name Constraints extension. */
  14154. file = XFOPEN(certExtNc, "rb");
  14155. if (!file) {
  14156. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14157. }
  14158. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14159. XFCLOSE(file);
  14160. if (bytes == 0)
  14161. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14162. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14163. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14164. if (ret != 0)
  14165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14166. FreeDecodedCert(&cert);
  14167. /* Certificate with Inhibit Any Policy extension. */
  14168. file = XFOPEN(certExtIa, "rb");
  14169. if (!file) {
  14170. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14171. }
  14172. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14173. XFCLOSE(file);
  14174. if (bytes == 0)
  14175. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14176. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14177. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14178. if (ret != 0)
  14179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14180. FreeDecodedCert(&cert);
  14181. /* Certificate with Netscape Certificate Type extension. */
  14182. file = XFOPEN(certExtNct, "rb");
  14183. if (!file) {
  14184. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14185. }
  14186. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14187. XFCLOSE(file);
  14188. if (bytes == 0)
  14189. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14190. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14191. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14192. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  14193. if (ret != 0)
  14194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14195. #else
  14196. if (ret != ASN_CRIT_EXT_E) {
  14197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14198. }
  14199. ret = 0;
  14200. #endif
  14201. done:
  14202. FreeDecodedCert(&cert);
  14203. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14204. #endif /* !NO_FILESYSTEM */
  14205. if (ret == 0)
  14206. ret = cert_asn1_test();
  14207. return ret;
  14208. }
  14209. #endif /* WOLFSSL_TEST_CERT */
  14210. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  14211. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  14212. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  14213. {
  14214. DecodedCert cert;
  14215. byte* tmp;
  14216. size_t bytes;
  14217. XFILE file;
  14218. wc_test_ret_t ret;
  14219. /* created from rsa_test : othercert.der */
  14220. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  14221. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  14222. /* created from rsa_test : othercert.der */
  14223. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  14224. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  14225. #ifdef HAVE_ECC
  14226. /* created from ecc_test_cert_gen : certecc.der */
  14227. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14228. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  14229. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  14230. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  14231. #else
  14232. /* Authority key id from ./certs/ca-ecc-cert.pem */
  14233. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  14234. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  14235. #endif
  14236. #endif /* HAVE_ECC */
  14237. /* created from rsa_test : cert.der */
  14238. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  14239. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  14240. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14241. if (tmp == NULL)
  14242. return WC_TEST_RET_ENC_ERRNO;
  14243. /* load othercert.der (Cert signed by an authority) */
  14244. file = XFOPEN(otherCertDerFile, "rb");
  14245. if (!file) {
  14246. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14247. return WC_TEST_RET_ENC_ERRNO;
  14248. }
  14249. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14250. XFCLOSE(file);
  14251. if (bytes == 0)
  14252. return WC_TEST_RET_ENC_ERRNO;
  14253. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14254. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14255. if (ret != 0)
  14256. return WC_TEST_RET_ENC_EC(ret);
  14257. /* check the SKID from a RSA certificate */
  14258. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  14259. return WC_TEST_RET_ENC_NC;
  14260. /* check the AKID from an RSA certificate */
  14261. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14262. return WC_TEST_RET_ENC_NC;
  14263. /* check the Key Usage from an RSA certificate */
  14264. if (!cert.extKeyUsageSet)
  14265. return WC_TEST_RET_ENC_NC;
  14266. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  14267. return WC_TEST_RET_ENC_NC;
  14268. /* check the CA Basic Constraints from an RSA certificate */
  14269. if (cert.isCA)
  14270. return WC_TEST_RET_ENC_NC;
  14271. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14272. /* check the Certificate Policies Id */
  14273. if (cert.extCertPoliciesNb != 1)
  14274. return WC_TEST_RET_ENC_NC;
  14275. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  14276. return WC_TEST_RET_ENC_NC;
  14277. #endif
  14278. FreeDecodedCert(&cert);
  14279. #ifdef HAVE_ECC
  14280. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  14281. file = XFOPEN(certEccDerFile, "rb");
  14282. if (!file) {
  14283. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14284. return WC_TEST_RET_ENC_ERRNO;
  14285. }
  14286. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14287. XFCLOSE(file);
  14288. if (bytes == 0)
  14289. return WC_TEST_RET_ENC_ERRNO;
  14290. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14291. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14292. if (ret != 0)
  14293. return WC_TEST_RET_ENC_EC(ret);
  14294. /* check the SKID from a ECC certificate - generated dynamically */
  14295. /* check the AKID from an ECC certificate */
  14296. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14297. return WC_TEST_RET_ENC_NC;
  14298. /* check the Key Usage from an ECC certificate */
  14299. if (!cert.extKeyUsageSet)
  14300. return WC_TEST_RET_ENC_NC;
  14301. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  14302. return WC_TEST_RET_ENC_NC;
  14303. /* check the CA Basic Constraints from an ECC certificate */
  14304. if (cert.isCA)
  14305. return WC_TEST_RET_ENC_NC;
  14306. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14307. /* check the Certificate Policies Id */
  14308. if (cert.extCertPoliciesNb != 2)
  14309. return WC_TEST_RET_ENC_NC;
  14310. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  14311. return WC_TEST_RET_ENC_NC;
  14312. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  14313. return WC_TEST_RET_ENC_NC;
  14314. #endif
  14315. FreeDecodedCert(&cert);
  14316. #endif /* HAVE_ECC */
  14317. /* load cert.der (self signed certificate) */
  14318. file = XFOPEN(certDerFile, "rb");
  14319. if (!file) {
  14320. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14321. return WC_TEST_RET_ENC_ERRNO;
  14322. }
  14323. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14324. XFCLOSE(file);
  14325. if (bytes == 0)
  14326. return WC_TEST_RET_ENC_ERRNO;
  14327. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14328. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14329. if (ret != 0)
  14330. return WC_TEST_RET_ENC_EC(ret);
  14331. /* check the SKID from a CA certificate */
  14332. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  14333. return WC_TEST_RET_ENC_NC;
  14334. /* check the AKID from an CA certificate */
  14335. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14336. return WC_TEST_RET_ENC_NC;
  14337. /* check the Key Usage from CA certificate */
  14338. if (!cert.extKeyUsageSet)
  14339. return WC_TEST_RET_ENC_NC;
  14340. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  14341. return WC_TEST_RET_ENC_NC;
  14342. /* check the CA Basic Constraints CA certificate */
  14343. if (!cert.isCA)
  14344. return WC_TEST_RET_ENC_NC;
  14345. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14346. /* check the Certificate Policies Id */
  14347. if (cert.extCertPoliciesNb != 2)
  14348. return WC_TEST_RET_ENC_NC;
  14349. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  14350. return WC_TEST_RET_ENC_NC;
  14351. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  14352. return WC_TEST_RET_ENC_NC;
  14353. #endif
  14354. FreeDecodedCert(&cert);
  14355. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14356. return 0;
  14357. }
  14358. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  14359. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  14360. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  14361. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  14362. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  14363. {
  14364. wc_test_ret_t ret = 0;
  14365. Cert cert;
  14366. FILE* file;
  14367. byte* der;
  14368. word32 derSz;
  14369. derSz = FOURK_BUF;
  14370. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14371. if (der == NULL)
  14372. ret = WC_TEST_RET_ENC_NC;
  14373. if (ret == 0) {
  14374. /* load cert.der */
  14375. file = XFOPEN(certDerFile, "rb");
  14376. if (file != NULL) {
  14377. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  14378. XFCLOSE(file);
  14379. if (derSz == 0)
  14380. ret = WC_TEST_RET_ENC_ERRNO;
  14381. }
  14382. else
  14383. ret = WC_TEST_RET_ENC_ERRNO;
  14384. }
  14385. if (ret == 0) {
  14386. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  14387. if (ret != 0)
  14388. ret = WC_TEST_RET_ENC_EC(ret);
  14389. }
  14390. if (ret == 0) {
  14391. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  14392. if (ret != 0)
  14393. ret = WC_TEST_RET_ENC_EC(ret);
  14394. }
  14395. if (ret == 0) {
  14396. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  14397. if (ret == BAD_FUNC_ARG)
  14398. ret = 0;
  14399. else
  14400. ret = WC_TEST_RET_ENC_EC(ret);
  14401. }
  14402. if (ret == 0) {
  14403. ret = wc_SetSubjectRaw(&cert, der, derSz);
  14404. if (ret != 0)
  14405. ret = WC_TEST_RET_ENC_EC(ret);
  14406. }
  14407. if (ret == 0) {
  14408. ret = wc_SetSubjectRaw(NULL, der, derSz);
  14409. if (ret == BAD_FUNC_ARG)
  14410. ret = 0;
  14411. else
  14412. ret = WC_TEST_RET_ENC_EC(ret);
  14413. }
  14414. if (ret == 0) {
  14415. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  14416. if (ret != 0)
  14417. ret = WC_TEST_RET_ENC_EC(ret);
  14418. }
  14419. if (ret == 0) {
  14420. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  14421. if (ret == BAD_FUNC_ARG)
  14422. ret = 0;
  14423. else
  14424. ret = WC_TEST_RET_ENC_EC(ret);
  14425. }
  14426. if (ret == 0) {
  14427. ret = wc_SetIssuerRaw(&cert, der, derSz);
  14428. if (ret != 0)
  14429. ret = WC_TEST_RET_ENC_EC(ret);
  14430. }
  14431. if (ret == 0) {
  14432. ret = wc_SetIssuerRaw(NULL, der, derSz);
  14433. if (ret == BAD_FUNC_ARG)
  14434. ret = 0;
  14435. else
  14436. ret = WC_TEST_RET_ENC_EC(ret);
  14437. }
  14438. #ifdef WOLFSSL_ALT_NAMES
  14439. if (ret == 0) {
  14440. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  14441. if (ret != 0)
  14442. ret = WC_TEST_RET_ENC_EC(ret);
  14443. }
  14444. if (ret == 0) {
  14445. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  14446. if (ret == BAD_FUNC_ARG)
  14447. ret = 0;
  14448. else
  14449. ret = WC_TEST_RET_ENC_EC(ret);
  14450. }
  14451. if (ret == 0) {
  14452. ret = wc_SetDatesBuffer(&cert, der, derSz);
  14453. if (ret != 0)
  14454. ret = WC_TEST_RET_ENC_EC(ret);
  14455. }
  14456. if (ret == 0) {
  14457. ret = wc_SetDatesBuffer(NULL, der, derSz);
  14458. if (ret == BAD_FUNC_ARG)
  14459. ret = 0;
  14460. else
  14461. ret = WC_TEST_RET_ENC_EC(ret);
  14462. }
  14463. #endif
  14464. if (ret == 0) {
  14465. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  14466. if (ret != 0)
  14467. ret = WC_TEST_RET_ENC_EC(ret);
  14468. }
  14469. if (ret == 0) {
  14470. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  14471. if (ret == BAD_FUNC_ARG)
  14472. ret = 0;
  14473. else
  14474. ret = WC_TEST_RET_ENC_NC;
  14475. }
  14476. wc_SetCert_Free(&cert);
  14477. if (ret == 0) {
  14478. if(cert.decodedCert != NULL)
  14479. ret = WC_TEST_RET_ENC_NC;
  14480. }
  14481. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14482. return ret;
  14483. }
  14484. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  14485. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  14486. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  14487. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14488. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14489. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  14490. {
  14491. wc_test_ret_t ret;
  14492. byte e[RSA_TEST_BYTES];
  14493. byte n[RSA_TEST_BYTES];
  14494. word32 eSz = sizeof(e);
  14495. word32 nSz = sizeof(n);
  14496. /* Parameter Validation testing. */
  14497. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  14498. #ifdef HAVE_USER_RSA
  14499. /* Implementation using IPP Libraries returns:
  14500. * -101 = USER_CRYPTO_ERROR
  14501. */
  14502. if (ret == 0)
  14503. #else
  14504. if (ret != BAD_FUNC_ARG)
  14505. #endif
  14506. return WC_TEST_RET_ENC_EC(ret);
  14507. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  14508. #ifdef HAVE_USER_RSA
  14509. /* Implementation using IPP Libraries returns:
  14510. * -101 = USER_CRYPTO_ERROR
  14511. */
  14512. if (ret == 0)
  14513. #else
  14514. if (ret != BAD_FUNC_ARG)
  14515. #endif
  14516. return WC_TEST_RET_ENC_EC(ret);
  14517. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  14518. #ifdef HAVE_USER_RSA
  14519. /* Implementation using IPP Libraries returns:
  14520. * -101 = USER_CRYPTO_ERROR
  14521. */
  14522. if (ret == 0)
  14523. #else
  14524. if (ret != BAD_FUNC_ARG)
  14525. #endif
  14526. return WC_TEST_RET_ENC_EC(ret);
  14527. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  14528. #ifdef HAVE_USER_RSA
  14529. /* Implementation using IPP Libraries returns:
  14530. * -101 = USER_CRYPTO_ERROR
  14531. */
  14532. if (ret == 0)
  14533. #else
  14534. if (ret != BAD_FUNC_ARG)
  14535. #endif
  14536. return WC_TEST_RET_ENC_EC(ret);
  14537. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  14538. #ifdef HAVE_USER_RSA
  14539. /* Implementation using IPP Libraries returns:
  14540. * -101 = USER_CRYPTO_ERROR
  14541. */
  14542. if (ret == 0)
  14543. #else
  14544. if (ret != BAD_FUNC_ARG)
  14545. #endif
  14546. return WC_TEST_RET_ENC_EC(ret);
  14547. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  14548. if (ret != 0)
  14549. return WC_TEST_RET_ENC_EC(ret);
  14550. eSz = 0;
  14551. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  14552. #ifdef HAVE_USER_RSA
  14553. /* Implementation using IPP Libraries returns:
  14554. * -101 = USER_CRYPTO_ERROR
  14555. */
  14556. if (ret == 0)
  14557. #else
  14558. if (ret != RSA_BUFFER_E)
  14559. #endif
  14560. return WC_TEST_RET_ENC_EC(ret);
  14561. eSz = sizeof(e);
  14562. nSz = 0;
  14563. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  14564. #ifdef HAVE_USER_RSA
  14565. /* Implementation using IPP Libraries returns:
  14566. * -101 = USER_CRYPTO_ERROR
  14567. */
  14568. if (ret == 0)
  14569. #else
  14570. if (ret != RSA_BUFFER_E)
  14571. #endif
  14572. return WC_TEST_RET_ENC_EC(ret);
  14573. return 0;
  14574. }
  14575. #endif /* NO_ASN */
  14576. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14577. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14578. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  14579. {
  14580. wc_test_ret_t ret;
  14581. byte e[3];
  14582. word32 eSz = sizeof(e);
  14583. byte n[RSA_TEST_BYTES];
  14584. word32 nSz = sizeof(n);
  14585. byte d[RSA_TEST_BYTES];
  14586. word32 dSz = sizeof(d);
  14587. byte p[RSA_TEST_BYTES/2];
  14588. word32 pSz = sizeof(p);
  14589. byte q[RSA_TEST_BYTES/2];
  14590. word32 qSz = sizeof(q);
  14591. word32 zero = 0;
  14592. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14593. if (ret != BAD_FUNC_ARG)
  14594. return WC_TEST_RET_ENC_EC(ret);
  14595. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14596. if (ret != BAD_FUNC_ARG)
  14597. return WC_TEST_RET_ENC_EC(ret);
  14598. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14599. if (ret != BAD_FUNC_ARG)
  14600. return WC_TEST_RET_ENC_EC(ret);
  14601. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14602. if (ret != BAD_FUNC_ARG)
  14603. return WC_TEST_RET_ENC_EC(ret);
  14604. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  14605. if (ret != BAD_FUNC_ARG)
  14606. return WC_TEST_RET_ENC_EC(ret);
  14607. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  14608. if (ret != BAD_FUNC_ARG)
  14609. return WC_TEST_RET_ENC_EC(ret);
  14610. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  14611. if (ret != BAD_FUNC_ARG)
  14612. return WC_TEST_RET_ENC_EC(ret);
  14613. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  14614. if (ret != BAD_FUNC_ARG)
  14615. return WC_TEST_RET_ENC_EC(ret);
  14616. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  14617. if (ret != BAD_FUNC_ARG)
  14618. return WC_TEST_RET_ENC_EC(ret);
  14619. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  14620. if (ret != BAD_FUNC_ARG)
  14621. return WC_TEST_RET_ENC_EC(ret);
  14622. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  14623. if (ret != BAD_FUNC_ARG)
  14624. return WC_TEST_RET_ENC_EC(ret);
  14625. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14626. if (ret != RSA_BUFFER_E)
  14627. return WC_TEST_RET_ENC_EC(ret);
  14628. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  14629. if (ret != RSA_BUFFER_E)
  14630. return WC_TEST_RET_ENC_EC(ret);
  14631. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14632. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  14633. if (ret != RSA_BUFFER_E)
  14634. return WC_TEST_RET_ENC_EC(ret);
  14635. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  14636. if (ret != RSA_BUFFER_E)
  14637. return WC_TEST_RET_ENC_EC(ret);
  14638. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  14639. if (ret != RSA_BUFFER_E)
  14640. return WC_TEST_RET_ENC_EC(ret);
  14641. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14642. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  14643. if (ret != 0)
  14644. return WC_TEST_RET_ENC_EC(ret);
  14645. return 0;
  14646. }
  14647. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  14648. #ifndef NO_SIG_WRAPPER
  14649. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  14650. {
  14651. wc_test_ret_t ret;
  14652. word32 sigSz;
  14653. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  14654. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  14655. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  14656. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  14657. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  14658. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  14659. };
  14660. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  14661. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  14662. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  14663. 0x00, 0x04, 0x20,
  14664. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  14665. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  14666. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  14667. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  14668. };
  14669. word32 inLen = (word32)XSTRLEN((char*)in);
  14670. byte out[RSA_TEST_BYTES];
  14671. /* Parameter Validation testing. */
  14672. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  14673. if (ret != BAD_FUNC_ARG)
  14674. return WC_TEST_RET_ENC_EC(ret);
  14675. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  14676. if (ret != BAD_FUNC_ARG)
  14677. return WC_TEST_RET_ENC_EC(ret);
  14678. sigSz = (word32)modLen;
  14679. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  14680. inLen, out, &sigSz, key, keyLen, rng);
  14681. if (ret != BAD_FUNC_ARG)
  14682. return WC_TEST_RET_ENC_EC(ret);
  14683. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14684. 0, out, &sigSz, key, keyLen, rng);
  14685. if (ret != BAD_FUNC_ARG)
  14686. return WC_TEST_RET_ENC_EC(ret);
  14687. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14688. inLen, NULL, &sigSz, key, keyLen, rng);
  14689. if (ret != BAD_FUNC_ARG)
  14690. return WC_TEST_RET_ENC_EC(ret);
  14691. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14692. inLen, out, NULL, key, keyLen, rng);
  14693. if (ret != BAD_FUNC_ARG)
  14694. return WC_TEST_RET_ENC_EC(ret);
  14695. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14696. inLen, out, &sigSz, NULL, keyLen, rng);
  14697. if (ret != BAD_FUNC_ARG)
  14698. return WC_TEST_RET_ENC_EC(ret);
  14699. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14700. inLen, out, &sigSz, key, 0, rng);
  14701. if (ret != BAD_FUNC_ARG)
  14702. return WC_TEST_RET_ENC_EC(ret);
  14703. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14704. inLen, out, &sigSz, key, keyLen, NULL);
  14705. #ifdef HAVE_USER_RSA
  14706. /* Implementation using IPP Libraries returns:
  14707. * -101 = USER_CRYPTO_ERROR
  14708. */
  14709. if (ret == 0)
  14710. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  14711. /* blinding / rng handled with hardware acceleration */
  14712. if (ret != 0)
  14713. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  14714. /* async may not require RNG */
  14715. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14716. if (ret != NO_VALID_DEVID)
  14717. #else
  14718. if (ret != 0 && ret != MISSING_RNG_E)
  14719. #endif
  14720. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  14721. /* FIPS140 implementation does not do blinding */
  14722. if (ret != 0)
  14723. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  14724. if (ret != SIG_TYPE_E)
  14725. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  14726. /* RNG is handled by hardware */
  14727. if (ret != 0)
  14728. #else
  14729. if (ret != MISSING_RNG_E)
  14730. #endif
  14731. return WC_TEST_RET_ENC_EC(ret);
  14732. sigSz = 0;
  14733. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14734. inLen, out, &sigSz, key, keyLen, rng);
  14735. if (ret != BAD_FUNC_ARG)
  14736. return WC_TEST_RET_ENC_EC(ret);
  14737. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  14738. inLen, out, (word32)modLen, key, keyLen);
  14739. if (ret != BAD_FUNC_ARG)
  14740. return WC_TEST_RET_ENC_EC(ret);
  14741. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14742. 0, out, (word32)modLen, key, keyLen);
  14743. if (ret != BAD_FUNC_ARG)
  14744. return WC_TEST_RET_ENC_EC(ret);
  14745. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14746. inLen, NULL, (word32)modLen, key, keyLen);
  14747. if (ret != BAD_FUNC_ARG)
  14748. return WC_TEST_RET_ENC_EC(ret);
  14749. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14750. inLen, out, 0, key, keyLen);
  14751. if (ret != BAD_FUNC_ARG)
  14752. return WC_TEST_RET_ENC_EC(ret);
  14753. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14754. inLen, out, (word32)modLen, NULL, keyLen);
  14755. if (ret != BAD_FUNC_ARG)
  14756. return WC_TEST_RET_ENC_EC(ret);
  14757. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14758. inLen, out, (word32)modLen, key, 0);
  14759. if (ret != BAD_FUNC_ARG)
  14760. return WC_TEST_RET_ENC_EC(ret);
  14761. #ifndef HAVE_ECC
  14762. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  14763. if (ret != SIG_TYPE_E)
  14764. return WC_TEST_RET_ENC_EC(ret);
  14765. #endif
  14766. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14767. return 0;
  14768. #endif
  14769. /* Use APIs. */
  14770. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  14771. if (ret != modLen)
  14772. return WC_TEST_RET_ENC_EC(ret);
  14773. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  14774. if (ret != modLen)
  14775. return WC_TEST_RET_ENC_EC(ret);
  14776. sigSz = (word32)ret;
  14777. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14778. XMEMSET(out, 0, sizeof(out));
  14779. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14780. inLen, out, &sigSz, key, keyLen, rng);
  14781. if (ret != 0)
  14782. return WC_TEST_RET_ENC_EC(ret);
  14783. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14784. inLen, out, (word32)modLen, key, keyLen);
  14785. if (ret != 0)
  14786. return WC_TEST_RET_ENC_EC(ret);
  14787. sigSz = (word32)sizeof(out);
  14788. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  14789. in, inLen, out, &sigSz, key, keyLen, rng);
  14790. if (ret != 0)
  14791. return WC_TEST_RET_ENC_EC(ret);
  14792. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  14793. in, inLen, out, (word32)modLen, key, keyLen);
  14794. if (ret != 0)
  14795. return WC_TEST_RET_ENC_EC(ret);
  14796. /* Wrong signature type. */
  14797. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  14798. inLen, out, (word32)modLen, key, keyLen);
  14799. if (ret == 0)
  14800. return WC_TEST_RET_ENC_EC(ret);
  14801. /* check hash functions */
  14802. sigSz = (word32)sizeof(out);
  14803. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  14804. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  14805. if (ret != 0)
  14806. return WC_TEST_RET_ENC_EC(ret);
  14807. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  14808. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  14809. if (ret != 0)
  14810. return WC_TEST_RET_ENC_EC(ret);
  14811. sigSz = (word32)sizeof(out);
  14812. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  14813. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  14814. if (ret != 0)
  14815. return WC_TEST_RET_ENC_EC(ret);
  14816. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  14817. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  14818. if (ret != 0)
  14819. return WC_TEST_RET_ENC_EC(ret);
  14820. #else
  14821. (void)hash;
  14822. (void)hashEnc;
  14823. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  14824. return 0;
  14825. }
  14826. #endif /* !NO_SIG_WRAPPER */
  14827. #ifdef WC_RSA_NONBLOCK
  14828. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  14829. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  14830. {
  14831. wc_test_ret_t ret = 0;
  14832. int count;
  14833. int signSz = 0;
  14834. RsaNb nb;
  14835. byte* inlinePlain = NULL;
  14836. /* Enable non-blocking RSA mode - provide context */
  14837. ret = wc_RsaSetNonBlock(key, &nb);
  14838. if (ret != 0)
  14839. return ret;
  14840. #ifdef WC_RSA_NONBLOCK_TIME
  14841. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  14842. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  14843. if (ret != 0)
  14844. return ret;
  14845. #endif
  14846. count = 0;
  14847. do {
  14848. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  14849. count++; /* track number of would blocks */
  14850. if (ret == FP_WOULDBLOCK) {
  14851. /* do "other" work here */
  14852. }
  14853. } while (ret == FP_WOULDBLOCK);
  14854. if (ret < 0) {
  14855. return ret;
  14856. }
  14857. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  14858. printf("RSA non-block sign: %d times\n", count);
  14859. #endif
  14860. signSz = ret;
  14861. /* Test non-blocking verify */
  14862. XMEMSET(plain, 0, plainSz);
  14863. count = 0;
  14864. do {
  14865. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  14866. count++; /* track number of would blocks */
  14867. if (ret == FP_WOULDBLOCK) {
  14868. /* do "other" work here */
  14869. }
  14870. } while (ret == FP_WOULDBLOCK);
  14871. if (ret < 0) {
  14872. return ret;
  14873. }
  14874. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  14875. printf("RSA non-block verify: %d times\n", count);
  14876. #endif
  14877. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  14878. return SIG_VERIFY_E;
  14879. }
  14880. /* Test inline non-blocking verify */
  14881. count = 0;
  14882. do {
  14883. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  14884. count++; /* track number of would blocks */
  14885. if (ret == FP_WOULDBLOCK) {
  14886. /* do "other" work here */
  14887. }
  14888. } while (ret == FP_WOULDBLOCK);
  14889. if (ret < 0) {
  14890. return ret;
  14891. }
  14892. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  14893. printf("RSA non-block inline verify: %d times\n", count);
  14894. #endif
  14895. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  14896. return SIG_VERIFY_E;
  14897. }
  14898. /* Disabling non-block RSA mode */
  14899. ret = wc_RsaSetNonBlock(key, NULL);
  14900. (void)count;
  14901. return 0;
  14902. }
  14903. #endif
  14904. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14905. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  14906. {
  14907. wc_test_ret_t ret;
  14908. word32 inSz;
  14909. word32 inOutIdx;
  14910. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  14911. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  14912. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  14913. 0x03 };
  14914. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  14915. 0x30, 0x18, 0x30, 0x16,
  14916. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14917. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14918. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  14919. 0x30, 0x1a, 0x30, 0x18,
  14920. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14921. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  14922. 0x02, 0x1, 0x03 };
  14923. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  14924. 0x30, 0x1b, 0x30, 0x19,
  14925. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14926. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  14927. 0x02, 0x1, 0x03 };
  14928. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  14929. 0x30, 0x18, 0x30, 0x16,
  14930. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14931. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14932. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  14933. 0x30, 0x18, 0x30, 0x16,
  14934. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14935. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14936. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  14937. 0x30, 0x16, 0x30, 0x14,
  14938. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14939. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14940. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  14941. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  14942. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14943. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  14944. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  14945. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  14946. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  14947. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  14948. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14949. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  14950. 0x30, 0x17, 0x30, 0x15,
  14951. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  14952. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  14953. ret = wc_InitRsaKey(keyPub, NULL);
  14954. if (ret != 0)
  14955. return WC_TEST_RET_ENC_EC(ret);
  14956. /* Parameter Validation testing. */
  14957. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  14958. if (ret != BAD_FUNC_ARG) {
  14959. ret = WC_TEST_RET_ENC_EC(ret);
  14960. goto done;
  14961. }
  14962. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  14963. if (ret != BAD_FUNC_ARG) {
  14964. ret = WC_TEST_RET_ENC_EC(ret);
  14965. goto done;
  14966. }
  14967. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  14968. if (ret != BAD_FUNC_ARG) {
  14969. ret = WC_TEST_RET_ENC_EC(ret);
  14970. goto done;
  14971. }
  14972. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  14973. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  14974. if (ret != 0)
  14975. #else
  14976. if (ret != ASN_GETINT_E)
  14977. #endif
  14978. {
  14979. ret = WC_TEST_RET_ENC_EC(ret);
  14980. goto done;
  14981. }
  14982. wc_FreeRsaKey(keyPub);
  14983. ret = wc_InitRsaKey(keyPub, NULL);
  14984. if (ret != 0)
  14985. return WC_TEST_RET_ENC_EC(ret);
  14986. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  14987. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  14988. if (ret != 0) {
  14989. #else
  14990. if (ret != ASN_GETINT_E) {
  14991. #endif
  14992. ret = WC_TEST_RET_ENC_EC(ret);
  14993. goto done;
  14994. }
  14995. wc_FreeRsaKey(keyPub);
  14996. ret = wc_InitRsaKey(keyPub, NULL);
  14997. if (ret != 0)
  14998. return WC_TEST_RET_ENC_EC(ret);
  14999. /* Use API. */
  15000. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  15001. if (ret != 0) {
  15002. ret = WC_TEST_RET_ENC_EC(ret);
  15003. goto done;
  15004. }
  15005. wc_FreeRsaKey(keyPub);
  15006. ret = wc_InitRsaKey(keyPub, NULL);
  15007. if (ret != 0)
  15008. return WC_TEST_RET_ENC_EC(ret);
  15009. /* Parameter Validation testing. */
  15010. inSz = sizeof(good);
  15011. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  15012. if (ret != BAD_FUNC_ARG) {
  15013. ret = WC_TEST_RET_ENC_EC(ret);
  15014. goto done;
  15015. }
  15016. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  15017. if (ret != BAD_FUNC_ARG) {
  15018. ret = WC_TEST_RET_ENC_EC(ret);
  15019. goto done;
  15020. }
  15021. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  15022. if (ret != BAD_FUNC_ARG) {
  15023. ret = WC_TEST_RET_ENC_EC(ret);
  15024. goto done;
  15025. }
  15026. /* Use good data and offset to bad data. */
  15027. inOutIdx = 2;
  15028. inSz = sizeof(good) - inOutIdx;
  15029. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  15030. if (ret != ASN_PARSE_E) {
  15031. ret = WC_TEST_RET_ENC_EC(ret);
  15032. goto done;
  15033. }
  15034. inOutIdx = 2;
  15035. inSz = sizeof(goodAlgId) - inOutIdx;
  15036. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15037. if (ret != ASN_PARSE_E) {
  15038. ret = WC_TEST_RET_ENC_EC(ret);
  15039. goto done;
  15040. }
  15041. inOutIdx = 2;
  15042. inSz = sizeof(goodAlgId);
  15043. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15044. #ifndef WOLFSSL_NO_DECODE_EXTRA
  15045. if (ret != ASN_PARSE_E)
  15046. #else
  15047. if (ret != ASN_RSA_KEY_E)
  15048. #endif
  15049. {
  15050. ret = WC_TEST_RET_ENC_EC(ret);
  15051. goto done;
  15052. }
  15053. /* Try different bad data. */
  15054. inSz = sizeof(badAlgIdNull);
  15055. inOutIdx = 0;
  15056. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  15057. if (ret != ASN_EXPECT_0_E) {
  15058. ret = WC_TEST_RET_ENC_EC(ret);
  15059. goto done;
  15060. }
  15061. inSz = sizeof(badNotBitString);
  15062. inOutIdx = 0;
  15063. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  15064. if (ret != ASN_BITSTR_E) {
  15065. ret = WC_TEST_RET_ENC_EC(ret);
  15066. goto done;
  15067. }
  15068. inSz = sizeof(badBitStringLen);
  15069. inOutIdx = 0;
  15070. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  15071. if (ret != ASN_PARSE_E) {
  15072. ret = WC_TEST_RET_ENC_EC(ret);
  15073. goto done;
  15074. }
  15075. inSz = sizeof(badNoSeq);
  15076. inOutIdx = 0;
  15077. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  15078. if (ret != ASN_PARSE_E) {
  15079. ret = WC_TEST_RET_ENC_EC(ret);
  15080. goto done;
  15081. }
  15082. inSz = sizeof(badNoObj);
  15083. inOutIdx = 0;
  15084. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  15085. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  15086. ret = WC_TEST_RET_ENC_EC(ret);
  15087. goto done;
  15088. }
  15089. inSz = sizeof(badIntN);
  15090. inOutIdx = 0;
  15091. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  15092. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  15093. ret = WC_TEST_RET_ENC_EC(ret);
  15094. goto done;
  15095. }
  15096. inSz = sizeof(badNotIntE);
  15097. inOutIdx = 0;
  15098. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  15099. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  15100. ret = WC_TEST_RET_ENC_EC(ret);
  15101. goto done;
  15102. }
  15103. /* TODO: Shouldn't pass as the sequence length is too small. */
  15104. inSz = sizeof(badLength);
  15105. inOutIdx = 0;
  15106. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  15107. #ifndef WOLFSSL_ASN_TEMPLATE
  15108. if (ret != 0)
  15109. #else
  15110. if (ret != ASN_PARSE_E)
  15111. #endif
  15112. {
  15113. ret = WC_TEST_RET_ENC_EC(ret);
  15114. goto done;
  15115. }
  15116. /* TODO: Shouldn't ignore object id's data. */
  15117. wc_FreeRsaKey(keyPub);
  15118. ret = wc_InitRsaKey(keyPub, NULL);
  15119. if (ret != 0)
  15120. return WC_TEST_RET_ENC_EC(ret);
  15121. inSz = sizeof(badBitStrNoZero);
  15122. inOutIdx = 0;
  15123. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  15124. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  15125. ret = WC_TEST_RET_ENC_EC(ret);
  15126. goto done;
  15127. }
  15128. wc_FreeRsaKey(keyPub);
  15129. ret = wc_InitRsaKey(keyPub, NULL);
  15130. if (ret != 0)
  15131. return WC_TEST_RET_ENC_EC(ret);
  15132. /* Valid data cases. */
  15133. inSz = sizeof(good);
  15134. inOutIdx = 0;
  15135. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  15136. if (ret != 0) {
  15137. ret = WC_TEST_RET_ENC_EC(ret);
  15138. goto done;
  15139. }
  15140. if (inOutIdx != inSz) {
  15141. ret = WC_TEST_RET_ENC_NC;
  15142. goto done;
  15143. }
  15144. wc_FreeRsaKey(keyPub);
  15145. ret = wc_InitRsaKey(keyPub, NULL);
  15146. if (ret != 0)
  15147. return WC_TEST_RET_ENC_EC(ret);
  15148. inSz = sizeof(goodAlgId);
  15149. inOutIdx = 0;
  15150. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15151. if (ret != 0) {
  15152. ret = WC_TEST_RET_ENC_EC(ret);
  15153. goto done;
  15154. }
  15155. if (inOutIdx != inSz) {
  15156. ret = WC_TEST_RET_ENC_NC;
  15157. goto done;
  15158. }
  15159. wc_FreeRsaKey(keyPub);
  15160. ret = wc_InitRsaKey(keyPub, NULL);
  15161. if (ret != 0)
  15162. return WC_TEST_RET_ENC_EC(ret);
  15163. inSz = sizeof(goodAlgIdNull);
  15164. inOutIdx = 0;
  15165. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  15166. if (ret != 0) {
  15167. ret = WC_TEST_RET_ENC_EC(ret);
  15168. goto done;
  15169. }
  15170. if (inOutIdx != inSz) {
  15171. ret = WC_TEST_RET_ENC_NC;
  15172. goto done;
  15173. }
  15174. done:
  15175. wc_FreeRsaKey(keyPub);
  15176. return ret;
  15177. }
  15178. #endif
  15179. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  15180. /* Need to create known good signatures to test with this. */
  15181. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15182. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15183. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  15184. {
  15185. byte digest[WC_MAX_DIGEST_SIZE];
  15186. wc_test_ret_t ret = 0;
  15187. const char inStr[] = TEST_STRING;
  15188. word32 inLen = (word32)TEST_STRING_SZ;
  15189. word32 outSz;
  15190. word32 plainSz;
  15191. word32 digestSz;
  15192. int i, j;
  15193. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  15194. int k, l;
  15195. #endif
  15196. #ifndef WOLFSSL_SE050
  15197. int len;
  15198. #endif
  15199. byte* plain;
  15200. int mgf[] = {
  15201. #ifndef NO_SHA
  15202. WC_MGF1SHA1,
  15203. #endif
  15204. #ifdef WOLFSSL_SHA224
  15205. WC_MGF1SHA224,
  15206. #endif
  15207. WC_MGF1SHA256,
  15208. #ifdef WOLFSSL_SHA384
  15209. WC_MGF1SHA384,
  15210. #endif
  15211. #ifdef WOLFSSL_SHA512
  15212. WC_MGF1SHA512
  15213. #endif
  15214. };
  15215. enum wc_HashType hash[] = {
  15216. #ifndef NO_SHA
  15217. WC_HASH_TYPE_SHA,
  15218. #endif
  15219. #ifdef WOLFSSL_SHA224
  15220. WC_HASH_TYPE_SHA224,
  15221. #endif
  15222. WC_HASH_TYPE_SHA256,
  15223. #ifdef WOLFSSL_SHA384
  15224. WC_HASH_TYPE_SHA384,
  15225. #endif
  15226. #ifdef WOLFSSL_SHA512
  15227. WC_HASH_TYPE_SHA512,
  15228. #endif
  15229. };
  15230. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  15231. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15232. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  15233. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15234. if (in == NULL || out == NULL || sig == NULL)
  15235. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  15236. #endif
  15237. XMEMCPY(in, inStr, inLen);
  15238. /* Test all combinations of hash and MGF. */
  15239. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  15240. /* Calculate hash of message. */
  15241. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  15242. if (ret != 0)
  15243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15244. digestSz = wc_HashGetDigestSize(hash[j]);
  15245. #ifdef WOLFSSL_SE050
  15246. /* SE050 only supports MGF matched to same hash type */
  15247. i = j;
  15248. #else
  15249. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  15250. #endif
  15251. outSz = RSA_TEST_BYTES;
  15252. do {
  15253. #if defined(WOLFSSL_ASYNC_CRYPT)
  15254. ret = wc_AsyncWait(ret, &key->asyncDev,
  15255. WC_ASYNC_FLAG_CALL_AGAIN);
  15256. #endif
  15257. if (ret >= 0) {
  15258. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  15259. hash[j], mgf[i], -1, key, rng);
  15260. }
  15261. } while (ret == WC_PENDING_E);
  15262. if (ret <= 0)
  15263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15264. outSz = (word32)ret;
  15265. XMEMCPY(sig, out, outSz);
  15266. plain = NULL;
  15267. TEST_SLEEP();
  15268. do {
  15269. #if defined(WOLFSSL_ASYNC_CRYPT)
  15270. ret = wc_AsyncWait(ret, &key->asyncDev,
  15271. WC_ASYNC_FLAG_CALL_AGAIN);
  15272. #endif
  15273. if (ret >= 0) {
  15274. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  15275. mgf[i], -1, key);
  15276. }
  15277. } while (ret == WC_PENDING_E);
  15278. if (ret <= 0)
  15279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15280. plainSz = (word32)ret;
  15281. TEST_SLEEP();
  15282. #if defined(HAVE_SELFTEST) && \
  15283. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15284. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15285. hash[j], -1);
  15286. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15287. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15288. hash[j], -1, 0);
  15289. #else
  15290. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  15291. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  15292. #endif
  15293. if (ret != 0)
  15294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15295. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  15296. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  15297. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  15298. if (i == k && j == l)
  15299. continue;
  15300. XMEMCPY(sig, out, outSz);
  15301. do {
  15302. #if defined(WOLFSSL_ASYNC_CRYPT)
  15303. ret = wc_AsyncWait(ret, &key->asyncDev,
  15304. WC_ASYNC_FLAG_CALL_AGAIN);
  15305. #endif
  15306. if (ret >= 0) {
  15307. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  15308. (byte**)&plain, hash[l], mgf[k], -1, key);
  15309. }
  15310. } while (ret == WC_PENDING_E);
  15311. if (ret >= 0)
  15312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15313. }
  15314. }
  15315. #endif
  15316. #ifndef WOLFSSL_SE050
  15317. } /* end mgf for loop */
  15318. #endif
  15319. }
  15320. /* SE050 generates salts internally only of hash length */
  15321. #ifndef WOLFSSL_SE050
  15322. /* Test that a salt length of zero works. */
  15323. digestSz = wc_HashGetDigestSize(hash[0]);
  15324. outSz = RSA_TEST_BYTES;
  15325. do {
  15326. #if defined(WOLFSSL_ASYNC_CRYPT)
  15327. ret = wc_AsyncWait(ret, &key->asyncDev,
  15328. WC_ASYNC_FLAG_CALL_AGAIN);
  15329. #endif
  15330. if (ret >= 0) {
  15331. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15332. mgf[0], 0, key, rng);
  15333. }
  15334. } while (ret == WC_PENDING_E);
  15335. if (ret <= 0)
  15336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15337. outSz = (word32)ret;
  15338. TEST_SLEEP();
  15339. do {
  15340. #if defined(WOLFSSL_ASYNC_CRYPT)
  15341. ret = wc_AsyncWait(ret, &key->asyncDev,
  15342. WC_ASYNC_FLAG_CALL_AGAIN);
  15343. #endif
  15344. if (ret >= 0) {
  15345. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  15346. 0, key);
  15347. }
  15348. } while (ret == WC_PENDING_E);
  15349. if (ret <= 0)
  15350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15351. plainSz = (word32)ret;
  15352. TEST_SLEEP();
  15353. do {
  15354. #if defined(WOLFSSL_ASYNC_CRYPT)
  15355. ret = wc_AsyncWait(ret, &key->asyncDev,
  15356. WC_ASYNC_FLAG_CALL_AGAIN);
  15357. #endif
  15358. if (ret >= 0) {
  15359. #if defined(HAVE_SELFTEST) && \
  15360. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15361. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  15362. hash[0], 0);
  15363. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15364. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  15365. hash[0], 0, 0);
  15366. #else
  15367. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  15368. hash[0], 0, 0, HEAP_HINT);
  15369. #endif
  15370. }
  15371. } while (ret == WC_PENDING_E);
  15372. if (ret != 0)
  15373. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15374. XMEMCPY(sig, out, outSz);
  15375. plain = NULL;
  15376. do {
  15377. #if defined(WOLFSSL_ASYNC_CRYPT)
  15378. ret = wc_AsyncWait(ret, &key->asyncDev,
  15379. WC_ASYNC_FLAG_CALL_AGAIN);
  15380. #endif
  15381. if (ret >= 0) {
  15382. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  15383. 0, key);
  15384. }
  15385. } while (ret == WC_PENDING_E);
  15386. if (ret <= 0)
  15387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15388. plainSz = (word32)ret;
  15389. TEST_SLEEP();
  15390. #if defined(HAVE_SELFTEST) && \
  15391. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15392. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15393. hash[0], 0);
  15394. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15395. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15396. hash[0], 0, 0);
  15397. #else
  15398. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15399. 0, 0, HEAP_HINT);
  15400. #endif
  15401. if (ret != 0)
  15402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15403. /* Test bad salt lengths in various APIs. */
  15404. digestSz = wc_HashGetDigestSize(hash[0]);
  15405. outSz = RSA_TEST_BYTES;
  15406. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  15407. len = -2;
  15408. #else
  15409. len = -3;
  15410. #endif
  15411. do {
  15412. #if defined(WOLFSSL_ASYNC_CRYPT)
  15413. ret = wc_AsyncWait(ret, &key->asyncDev,
  15414. WC_ASYNC_FLAG_CALL_AGAIN);
  15415. #endif
  15416. if (ret >= 0) {
  15417. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15418. mgf[0], len, key, rng);
  15419. }
  15420. } while (ret == WC_PENDING_E);
  15421. if (ret != PSS_SALTLEN_E)
  15422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15423. do {
  15424. #if defined(WOLFSSL_ASYNC_CRYPT)
  15425. ret = wc_AsyncWait(ret, &key->asyncDev,
  15426. WC_ASYNC_FLAG_CALL_AGAIN);
  15427. #endif
  15428. if (ret >= 0) {
  15429. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15430. mgf[0], digestSz + 1, key, rng);
  15431. }
  15432. } while (ret == WC_PENDING_E);
  15433. if (ret != PSS_SALTLEN_E)
  15434. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15435. TEST_SLEEP();
  15436. do {
  15437. #if defined(WOLFSSL_ASYNC_CRYPT)
  15438. ret = wc_AsyncWait(ret, &key->asyncDev,
  15439. WC_ASYNC_FLAG_CALL_AGAIN);
  15440. #endif
  15441. if (ret >= 0) {
  15442. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  15443. mgf[0], -2, key);
  15444. }
  15445. } while (ret == WC_PENDING_E);
  15446. if (ret != PSS_SALTLEN_E)
  15447. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15448. TEST_SLEEP();
  15449. do {
  15450. #if defined(WOLFSSL_ASYNC_CRYPT)
  15451. ret = wc_AsyncWait(ret, &key->asyncDev,
  15452. WC_ASYNC_FLAG_CALL_AGAIN);
  15453. #endif
  15454. if (ret >= 0) {
  15455. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  15456. digestSz + 1, key);
  15457. }
  15458. } while (ret == WC_PENDING_E);
  15459. if (ret != PSS_SALTLEN_E)
  15460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15461. TEST_SLEEP();
  15462. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  15463. len = -2;
  15464. #else
  15465. len = -3;
  15466. #endif
  15467. #if defined(HAVE_SELFTEST) && \
  15468. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15469. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15470. hash[0], len);
  15471. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15472. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15473. hash[0], len, 0);
  15474. #else
  15475. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15476. len, 0, HEAP_HINT);
  15477. #endif
  15478. if (ret != PSS_SALTLEN_E)
  15479. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15480. #ifndef WOLFSSL_PSS_LONG_SALT
  15481. len = digestSz + 1;
  15482. #else
  15483. len = plainSz - digestSz - 1;
  15484. #endif
  15485. #if defined(HAVE_SELFTEST) && \
  15486. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15487. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15488. hash[0], len);
  15489. if (ret != PSS_SALTLEN_E)
  15490. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15491. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15492. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15493. hash[0], len, 0);
  15494. if (ret != BAD_PADDING_E)
  15495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15496. #else
  15497. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15498. len, 0, HEAP_HINT);
  15499. if (ret != PSS_SALTLEN_E)
  15500. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15501. #endif
  15502. ret = 0;
  15503. #endif /* WOLFSSL_SE050 */
  15504. exit_rsa_pss:
  15505. WC_FREE_VAR(sig, HEAP_HINT);
  15506. WC_FREE_VAR(in, HEAP_HINT);
  15507. WC_FREE_VAR(out, HEAP_HINT);
  15508. return ret;
  15509. }
  15510. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  15511. #endif
  15512. #ifdef WC_RSA_NO_PADDING
  15513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  15514. {
  15515. WC_RNG rng;
  15516. byte* tmp = NULL;
  15517. size_t bytes;
  15518. wc_test_ret_t ret;
  15519. word32 inLen = 0;
  15520. word32 idx = 0;
  15521. word32 outSz = RSA_TEST_BYTES;
  15522. word32 plainSz = RSA_TEST_BYTES;
  15523. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15524. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  15525. !defined(NO_FILESYSTEM)
  15526. XFILE file;
  15527. #endif
  15528. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  15529. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15530. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15531. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15532. if (key == NULL || out == NULL || plain == NULL)
  15533. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  15534. #endif
  15535. /* initialize stack structures */
  15536. XMEMSET(&rng, 0, sizeof(rng));
  15537. XMEMSET(key, 0, sizeof(RsaKey));
  15538. #ifdef USE_CERT_BUFFERS_1024
  15539. bytes = (size_t)sizeof_client_key_der_1024;
  15540. if (bytes < (size_t)sizeof_client_cert_der_1024)
  15541. bytes = (size_t)sizeof_client_cert_der_1024;
  15542. #elif defined(USE_CERT_BUFFERS_2048)
  15543. bytes = (size_t)sizeof_client_key_der_2048;
  15544. if (bytes < (size_t)sizeof_client_cert_der_2048)
  15545. bytes = (size_t)sizeof_client_cert_der_2048;
  15546. #else
  15547. bytes = FOURK_BUF;
  15548. #endif
  15549. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15550. if (tmp == NULL
  15551. #ifdef WOLFSSL_ASYNC_CRYPT
  15552. || out == NULL || plain == NULL
  15553. #endif
  15554. ) {
  15555. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  15556. }
  15557. #ifdef USE_CERT_BUFFERS_1024
  15558. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  15559. #elif defined(USE_CERT_BUFFERS_2048)
  15560. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  15561. #elif defined(USE_CERT_BUFFERS_3072)
  15562. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  15563. #elif defined(USE_CERT_BUFFERS_4096)
  15564. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  15565. #elif !defined(NO_FILESYSTEM)
  15566. file = XFOPEN(clientKey, "rb");
  15567. if (!file) {
  15568. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  15569. WC_TEST_RET_ENC_ERRNO);
  15570. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  15571. }
  15572. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  15573. XFCLOSE(file);
  15574. if (bytes == 0)
  15575. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  15576. #else
  15577. /* No key to use. */
  15578. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  15579. #endif /* USE_CERT_BUFFERS */
  15580. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  15581. if (ret != 0) {
  15582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15583. }
  15584. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  15585. if (ret != 0) {
  15586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15587. }
  15588. /* after loading in key use tmp as the test buffer */
  15589. #ifndef HAVE_FIPS
  15590. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15591. #else
  15592. ret = wc_InitRng(&rng);
  15593. #endif
  15594. if (ret != 0) {
  15595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15596. }
  15597. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  15598. inLen = wc_RsaEncryptSize(key);
  15599. outSz = inLen;
  15600. plainSz = inLen;
  15601. XMEMSET(tmp, 7, inLen);
  15602. do {
  15603. #if defined(WOLFSSL_ASYNC_CRYPT)
  15604. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15605. #endif
  15606. if (ret >= 0) {
  15607. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  15608. RSA_PRIVATE_ENCRYPT, &rng);
  15609. }
  15610. } while (ret == WC_PENDING_E);
  15611. if (ret <= 0) {
  15612. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15613. }
  15614. /* encrypted result should not be the same as input */
  15615. if (XMEMCMP(out, tmp, inLen) == 0) {
  15616. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  15617. }
  15618. TEST_SLEEP();
  15619. /* decrypt with public key and compare result */
  15620. do {
  15621. #if defined(WOLFSSL_ASYNC_CRYPT)
  15622. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15623. #endif
  15624. if (ret >= 0) {
  15625. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  15626. RSA_PUBLIC_DECRYPT, &rng);
  15627. }
  15628. } while (ret == WC_PENDING_E);
  15629. if (ret <= 0) {
  15630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15631. }
  15632. if (XMEMCMP(plain, tmp, inLen) != 0) {
  15633. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  15634. }
  15635. TEST_SLEEP();
  15636. #endif
  15637. #ifdef WC_RSA_BLINDING
  15638. ret = wc_RsaSetRNG(NULL, &rng);
  15639. if (ret != BAD_FUNC_ARG) {
  15640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15641. }
  15642. ret = wc_RsaSetRNG(key, &rng);
  15643. if (ret < 0) {
  15644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15645. }
  15646. #endif
  15647. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  15648. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  15649. do {
  15650. #if defined(WOLFSSL_ASYNC_CRYPT)
  15651. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15652. #endif
  15653. if (ret >= 0) {
  15654. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  15655. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  15656. }
  15657. } while (ret == WC_PENDING_E);
  15658. if (ret < 0) {
  15659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15660. }
  15661. TEST_SLEEP();
  15662. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  15663. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15664. do {
  15665. #if defined(WOLFSSL_ASYNC_CRYPT)
  15666. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15667. #endif
  15668. if (ret >= 0) {
  15669. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  15670. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  15671. }
  15672. } while (ret == WC_PENDING_E);
  15673. if (ret < 0) {
  15674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15675. }
  15676. if (XMEMCMP(plain, tmp, inLen) != 0) {
  15677. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  15678. }
  15679. TEST_SLEEP();
  15680. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15681. /* test some bad arguments */
  15682. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  15683. &rng);
  15684. if (ret != BAD_FUNC_ARG) {
  15685. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15686. }
  15687. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  15688. &rng);
  15689. if (ret != BAD_FUNC_ARG) {
  15690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15691. }
  15692. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  15693. &rng);
  15694. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  15695. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15696. }
  15697. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  15698. RSA_PUBLIC_DECRYPT, &rng);
  15699. if (ret != BAD_FUNC_ARG) {
  15700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  15701. }
  15702. /* if making it to this point of code without hitting an ERROR_OUT then
  15703. * all tests have passed */
  15704. ret = 0;
  15705. exit_rsa_nopadding:
  15706. wc_FreeRsaKey(key);
  15707. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15708. WC_FREE_VAR(key, HEAP_HINT);
  15709. WC_FREE_VAR(out, HEAP_HINT);
  15710. WC_FREE_VAR(plain, HEAP_HINT);
  15711. wc_FreeRng(&rng);
  15712. return ret;
  15713. }
  15714. #endif /* WC_RSA_NO_PADDING */
  15715. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  15716. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  15717. {
  15718. byte* tmp = NULL;
  15719. size_t bytes;
  15720. wc_test_ret_t ret;
  15721. word32 inLen = 0;
  15722. #ifndef NO_ASN
  15723. word32 idx = 0;
  15724. #endif
  15725. word32 outSz = RSA_TEST_BYTES;
  15726. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15727. word32 plainSz = RSA_TEST_BYTES;
  15728. #endif
  15729. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  15730. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  15731. XFILE file;
  15732. #endif
  15733. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15734. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15735. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15736. #endif
  15737. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15738. if (out == NULL
  15739. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15740. || plain == NULL
  15741. #endif
  15742. ) {
  15743. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  15744. }
  15745. #endif
  15746. #if defined(USE_CERT_BUFFERS_2048)
  15747. bytes = (size_t)sizeof_client_key_der_2048;
  15748. if (bytes < (size_t)sizeof_client_cert_der_2048)
  15749. bytes = (size_t)sizeof_client_cert_der_2048;
  15750. #else
  15751. bytes = FOURK_BUF;
  15752. #endif
  15753. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15754. if (tmp == NULL
  15755. #ifdef WOLFSSL_ASYNC_CRYPT
  15756. || out == NULL || plain == NULL
  15757. #endif
  15758. ) {
  15759. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  15760. }
  15761. #if defined(USE_CERT_BUFFERS_2048)
  15762. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  15763. #elif defined(USE_CERT_BUFFERS_3072)
  15764. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  15765. #elif defined(USE_CERT_BUFFERS_4096)
  15766. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  15767. #elif !defined(NO_FILESYSTEM)
  15768. file = XFOPEN(clientKey, "rb");
  15769. if (!file) {
  15770. err_sys("can't open ./certs/client-key.der, "
  15771. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  15772. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  15773. }
  15774. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  15775. XFCLOSE(file);
  15776. if (bytes == 0)
  15777. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  15778. #else
  15779. /* No key to use. */
  15780. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  15781. #endif /* USE_CERT_BUFFERS */
  15782. #ifndef NO_ASN
  15783. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  15784. if (ret != 0) {
  15785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15786. }
  15787. #else
  15788. #ifdef USE_CERT_BUFFERS_2048
  15789. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  15790. if (ret != 0) {
  15791. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15792. }
  15793. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  15794. if (ret != 0) {
  15795. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15796. }
  15797. #ifndef NO_SIG_WRAPPER
  15798. modLen = 2048;
  15799. #endif
  15800. #else
  15801. #error Not supported yet!
  15802. #endif
  15803. #endif
  15804. key->n.dp[0] &= (mp_digit)-2;
  15805. if (ret != 0) {
  15806. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15807. }
  15808. /* after loading in key use tmp as the test buffer */
  15809. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15810. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  15811. !defined(WOLFSSL_XILINX_CRYPT)
  15812. /* The ARM64_ASM code that was FIPS validated did not return these expected
  15813. * failure codes. These tests cases were added after the assembly was
  15814. * in-lined in the module and validated, these tests will be available in
  15815. * the 140-3 module */
  15816. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  15817. inLen = 32;
  15818. outSz = wc_RsaEncryptSize(key);
  15819. XMEMSET(tmp, 7, plainSz);
  15820. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  15821. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  15822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15823. }
  15824. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  15825. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15827. }
  15828. #endif
  15829. #ifdef WC_RSA_BLINDING
  15830. ret = wc_RsaSetRNG(key, rng);
  15831. if (ret < 0) {
  15832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15833. }
  15834. #endif
  15835. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  15836. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  15837. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  15838. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15840. }
  15841. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  15842. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15843. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  15844. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  15845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  15846. }
  15847. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15848. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15849. /* if making it to this point of code without hitting an ERROR_OUT then
  15850. * all tests have passed */
  15851. ret = 0;
  15852. exit_rsa_even_mod:
  15853. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15854. WC_FREE_VAR(out, HEAP_HINT);
  15855. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15856. WC_FREE_VAR(plain, HEAP_HINT);
  15857. #endif
  15858. (void)out;
  15859. (void)outSz;
  15860. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15861. (void)plain;
  15862. (void)plainSz;
  15863. #endif
  15864. (void)inLen;
  15865. (void)rng;
  15866. return ret;
  15867. }
  15868. #endif /* WOLFSSL_HAVE_SP_RSA */
  15869. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  15870. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  15871. {
  15872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15873. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15874. #ifdef WOLFSSL_TEST_CERT
  15875. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15876. #endif
  15877. #else
  15878. RsaKey caKey[1];
  15879. #ifdef WOLFSSL_TEST_CERT
  15880. DecodedCert decode[1];
  15881. #endif
  15882. #endif
  15883. byte* der = NULL;
  15884. wc_test_ret_t ret;
  15885. Cert* myCert = NULL;
  15886. int certSz;
  15887. size_t bytes3;
  15888. word32 idx3 = 0;
  15889. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15890. XFILE file3;
  15891. #endif
  15892. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  15893. struct tm beforeTime;
  15894. struct tm afterTime;
  15895. #endif
  15896. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  15897. (void)keypub;
  15898. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15899. if (caKey == NULL)
  15900. ERROR_OUT(MEMORY_E, exit_rsa);
  15901. #ifdef WOLFSSL_TEST_CERT
  15902. if (decode == NULL)
  15903. ERROR_OUT(MEMORY_E, exit_rsa);
  15904. #endif
  15905. #endif
  15906. XMEMSET(caKey, 0, sizeof *caKey);
  15907. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15908. if (der == NULL) {
  15909. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15910. }
  15911. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15912. if (myCert == NULL) {
  15913. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15914. }
  15915. /* self signed */
  15916. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  15917. if (ret != 0) {
  15918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15919. }
  15920. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  15921. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  15922. myCert->serialSz = (int)sizeof(mySerial);
  15923. myCert->isCA = 1;
  15924. #ifndef NO_SHA256
  15925. myCert->sigType = CTC_SHA256wRSA;
  15926. #else
  15927. myCert->sigType = CTC_SHAwRSA;
  15928. #endif
  15929. #ifdef WOLFSSL_CERT_EXT
  15930. /* add Policies */
  15931. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  15932. CTC_MAX_CERTPOL_SZ);
  15933. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  15934. CTC_MAX_CERTPOL_SZ);
  15935. myCert->certPoliciesNb = 2;
  15936. /* add SKID from the Public Key */
  15937. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  15938. if (ret != 0) {
  15939. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15940. }
  15941. /* add AKID from the Public Key */
  15942. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  15943. if (ret != 0) {
  15944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15945. }
  15946. /* add Key Usage */
  15947. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  15948. if (ret != 0) {
  15949. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15950. }
  15951. #ifdef WOLFSSL_EKU_OID
  15952. {
  15953. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  15954. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  15955. HEAP_HINT);
  15956. if (ret != 0) {
  15957. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15958. }
  15959. }
  15960. #endif /* WOLFSSL_EKU_OID */
  15961. #endif /* WOLFSSL_CERT_EXT */
  15962. do {
  15963. #if defined(WOLFSSL_ASYNC_CRYPT)
  15964. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15965. #endif
  15966. if (ret >= 0) {
  15967. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  15968. }
  15969. } while (ret == WC_PENDING_E);
  15970. if (ret < 0) {
  15971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15972. }
  15973. certSz = (word32)ret;
  15974. #ifdef WOLFSSL_TEST_CERT
  15975. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  15976. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15977. if (ret != 0) {
  15978. FreeDecodedCert(decode);
  15979. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15980. }
  15981. FreeDecodedCert(decode);
  15982. #endif
  15983. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  15984. CERT_TYPE);
  15985. if (ret != 0) {
  15986. goto exit_rsa;
  15987. }
  15988. /* Setup Certificate */
  15989. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  15990. if (ret < 0) {
  15991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15992. }
  15993. #ifdef WOLFSSL_ALT_NAMES
  15994. /* Get CA Cert for testing */
  15995. #ifdef USE_CERT_BUFFERS_1024
  15996. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  15997. bytes3 = sizeof_ca_cert_der_1024;
  15998. #elif defined(USE_CERT_BUFFERS_2048)
  15999. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  16000. bytes3 = sizeof_ca_cert_der_2048;
  16001. #else
  16002. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  16003. if (!file3) {
  16004. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16005. }
  16006. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16007. XFCLOSE(file3);
  16008. if (bytes3 == 0)
  16009. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16010. #endif /* USE_CERT_BUFFERS */
  16011. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  16012. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  16013. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  16014. if (ret != 0)
  16015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16016. #endif
  16017. /* get alt names from der */
  16018. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  16019. if (ret != 0)
  16020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16021. /* get dates from der */
  16022. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  16023. if (ret != 0)
  16024. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16025. #ifndef NO_ASN_TIME
  16026. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  16027. if (ret < 0)
  16028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16029. #endif
  16030. #endif /* WOLFSSL_ALT_NAMES */
  16031. /* Get CA Key */
  16032. #ifdef USE_CERT_BUFFERS_1024
  16033. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  16034. bytes3 = sizeof_ca_key_der_1024;
  16035. #elif defined(USE_CERT_BUFFERS_2048)
  16036. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  16037. bytes3 = sizeof_ca_key_der_2048;
  16038. #else
  16039. file3 = XFOPEN(rsaCaKeyFile, "rb");
  16040. if (!file3) {
  16041. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16042. }
  16043. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16044. XFCLOSE(file3);
  16045. if (bytes3 == 0)
  16046. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16047. #endif /* USE_CERT_BUFFERS */
  16048. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  16049. if (ret != 0)
  16050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16051. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  16052. if (ret != 0)
  16053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16054. #ifndef NO_SHA256
  16055. myCert->sigType = CTC_SHA256wRSA;
  16056. #else
  16057. myCert->sigType = CTC_SHAwRSA;
  16058. #endif
  16059. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  16060. #ifdef WOLFSSL_CERT_EXT
  16061. /* add Policies */
  16062. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  16063. CTC_MAX_CERTPOL_SZ);
  16064. myCert->certPoliciesNb =1;
  16065. /* add SKID from the Public Key */
  16066. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  16067. if (ret != 0)
  16068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16069. /* add AKID from the CA certificate */
  16070. #if defined(USE_CERT_BUFFERS_2048)
  16071. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  16072. sizeof_ca_cert_der_2048);
  16073. #elif defined(USE_CERT_BUFFERS_1024)
  16074. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  16075. sizeof_ca_cert_der_1024);
  16076. #else
  16077. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  16078. #endif
  16079. if (ret != 0)
  16080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16081. /* add Key Usage */
  16082. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  16083. if (ret != 0)
  16084. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16085. #endif /* WOLFSSL_CERT_EXT */
  16086. #if defined(USE_CERT_BUFFERS_2048)
  16087. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  16088. sizeof_ca_cert_der_2048);
  16089. #elif defined(USE_CERT_BUFFERS_1024)
  16090. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  16091. sizeof_ca_cert_der_1024);
  16092. #else
  16093. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  16094. #endif
  16095. if (ret < 0)
  16096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16097. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  16098. if (certSz < 0) {
  16099. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  16100. }
  16101. ret = 0;
  16102. do {
  16103. #if defined(WOLFSSL_ASYNC_CRYPT)
  16104. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16105. #endif
  16106. if (ret >= 0) {
  16107. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  16108. caKey, NULL, rng);
  16109. }
  16110. } while (ret == WC_PENDING_E);
  16111. if (ret < 0)
  16112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16113. certSz = (word32)ret;
  16114. #ifdef WOLFSSL_TEST_CERT
  16115. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  16116. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  16117. if (ret != 0) {
  16118. FreeDecodedCert(decode);
  16119. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16120. }
  16121. FreeDecodedCert(decode);
  16122. #endif
  16123. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  16124. CERT_TYPE);
  16125. if (ret != 0) {
  16126. goto exit_rsa;
  16127. }
  16128. exit_rsa:
  16129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16130. if (caKey != NULL) {
  16131. wc_FreeRsaKey(caKey);
  16132. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16133. }
  16134. #ifdef WOLFSSL_TEST_CERT
  16135. if (decode != NULL)
  16136. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16137. #endif
  16138. #else
  16139. wc_FreeRsaKey(caKey);
  16140. #endif
  16141. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16142. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16143. return ret;
  16144. }
  16145. #endif
  16146. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  16147. defined(WOLFSSL_CERT_GEN)
  16148. /* Make Cert / Sign example for ECC cert and RSA CA */
  16149. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  16150. {
  16151. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16152. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16153. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16154. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16155. #ifdef WOLFSSL_TEST_CERT
  16156. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16157. #endif
  16158. #else
  16159. RsaKey caKey[1];
  16160. ecc_key caEccKey[1];
  16161. ecc_key caEccKeyPub[1];
  16162. #ifdef WOLFSSL_TEST_CERT
  16163. DecodedCert decode[1];
  16164. #endif
  16165. #endif
  16166. byte* der = NULL;
  16167. Cert* myCert = NULL;
  16168. int certSz;
  16169. size_t bytes3;
  16170. word32 idx3 = 0;
  16171. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  16172. || !defined(USE_CERT_BUFFERS_256)
  16173. XFILE file3;
  16174. #endif
  16175. wc_test_ret_t ret;
  16176. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16177. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  16178. #ifdef WOLFSSL_TEST_CERT
  16179. || (decode == NULL)
  16180. #endif
  16181. )
  16182. ERROR_OUT(MEMORY_E, exit_rsa);
  16183. #endif
  16184. XMEMSET(caKey, 0, sizeof *caKey);
  16185. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  16186. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  16187. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16188. if (der == NULL) {
  16189. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16190. }
  16191. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16192. if (myCert == NULL) {
  16193. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16194. }
  16195. /* Get CA Key */
  16196. #ifdef USE_CERT_BUFFERS_1024
  16197. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  16198. bytes3 = sizeof_ca_key_der_1024;
  16199. #elif defined(USE_CERT_BUFFERS_2048)
  16200. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  16201. bytes3 = sizeof_ca_key_der_2048;
  16202. #else
  16203. file3 = XFOPEN(rsaCaKeyFile, "rb");
  16204. if (!file3) {
  16205. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16206. }
  16207. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16208. XFCLOSE(file3);
  16209. if (bytes3 == 0)
  16210. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16211. #endif /* USE_CERT_BUFFERS */
  16212. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  16213. if (ret != 0)
  16214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16215. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  16216. if (ret != 0)
  16217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16218. /* Get Cert Key */
  16219. #ifdef USE_CERT_BUFFERS_256
  16220. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  16221. bytes3 = sizeof_ecc_key_pub_der_256;
  16222. #else
  16223. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  16224. if (!file3) {
  16225. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16226. }
  16227. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16228. XFCLOSE(file3);
  16229. if (bytes3 == 0)
  16230. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16231. #endif
  16232. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  16233. if (ret != 0)
  16234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16235. idx3 = 0;
  16236. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  16237. if (ret != 0)
  16238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16239. /* Setup Certificate */
  16240. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  16241. if (ret != 0)
  16242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16243. #ifndef NO_SHA256
  16244. myCert->sigType = CTC_SHA256wRSA;
  16245. #else
  16246. myCert->sigType = CTC_SHAwRSA;
  16247. #endif
  16248. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  16249. #ifdef WOLFSSL_CERT_EXT
  16250. /* add Policies */
  16251. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  16252. CTC_MAX_CERTPOL_SZ);
  16253. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  16254. CTC_MAX_CERTPOL_SZ);
  16255. myCert->certPoliciesNb = 2;
  16256. /* add SKID from the Public Key */
  16257. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  16258. if (ret != 0)
  16259. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16260. /* add AKID from the CA certificate */
  16261. #if defined(USE_CERT_BUFFERS_2048)
  16262. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  16263. sizeof_ca_cert_der_2048);
  16264. #elif defined(USE_CERT_BUFFERS_1024)
  16265. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  16266. sizeof_ca_cert_der_1024);
  16267. #else
  16268. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  16269. #endif
  16270. if (ret != 0)
  16271. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16272. /* add Key Usage */
  16273. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  16274. if (ret != 0)
  16275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16276. #endif /* WOLFSSL_CERT_EXT */
  16277. #if defined(USE_CERT_BUFFERS_2048)
  16278. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  16279. sizeof_ca_cert_der_2048);
  16280. #elif defined(USE_CERT_BUFFERS_1024)
  16281. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  16282. sizeof_ca_cert_der_1024);
  16283. #else
  16284. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  16285. #endif
  16286. if (ret < 0)
  16287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16288. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  16289. if (certSz < 0) {
  16290. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  16291. }
  16292. ret = 0;
  16293. do {
  16294. #if defined(WOLFSSL_ASYNC_CRYPT)
  16295. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16296. #endif
  16297. if (ret >= 0) {
  16298. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  16299. FOURK_BUF, caKey, NULL, rng);
  16300. }
  16301. } while (ret == WC_PENDING_E);
  16302. if (ret < 0)
  16303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16304. certSz = (word32)ret;
  16305. #ifdef WOLFSSL_TEST_CERT
  16306. InitDecodedCert(decode, der, certSz, 0);
  16307. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  16308. if (ret != 0) {
  16309. FreeDecodedCert(decode);
  16310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16311. }
  16312. FreeDecodedCert(decode);
  16313. #endif
  16314. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  16315. CERT_TYPE);
  16316. if (ret != 0) {
  16317. goto exit_rsa;
  16318. }
  16319. exit_rsa:
  16320. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16321. if (caKey != NULL) {
  16322. wc_FreeRsaKey(caKey);
  16323. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16324. }
  16325. if (caEccKey != NULL) {
  16326. wc_ecc_free(caEccKey);
  16327. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16328. }
  16329. if (caEccKeyPub != NULL) {
  16330. wc_ecc_free(caEccKeyPub);
  16331. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16332. }
  16333. #ifdef WOLFSSL_TEST_CERT
  16334. if (decode != NULL)
  16335. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16336. #endif
  16337. #else
  16338. wc_FreeRsaKey(caKey);
  16339. wc_ecc_free(caEccKey);
  16340. wc_ecc_free(caEccKeyPub);
  16341. #endif
  16342. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16343. myCert = NULL;
  16344. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16345. der = NULL;
  16346. if (ret >= 0)
  16347. ret = 0;
  16348. return ret;
  16349. }
  16350. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  16351. #ifdef WOLFSSL_KEY_GEN
  16352. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  16353. {
  16354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16355. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16356. #else
  16357. RsaKey genKey[1];
  16358. #endif
  16359. wc_test_ret_t ret;
  16360. byte* der = NULL;
  16361. #ifndef WOLFSSL_CRYPTOCELL
  16362. word32 idx = 0;
  16363. #endif
  16364. int derSz = 0;
  16365. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  16366. int keySz = 1024;
  16367. #else
  16368. int keySz = 2048;
  16369. #endif
  16370. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16371. if (! genKey)
  16372. ERROR_OUT(MEMORY_E, exit_rsa);
  16373. #endif
  16374. XMEMSET(genKey, 0, sizeof *genKey);
  16375. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  16376. if (ret != 0)
  16377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16378. #ifdef HAVE_FIPS
  16379. for (;;) {
  16380. #endif
  16381. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  16382. #if defined(WOLFSSL_ASYNC_CRYPT)
  16383. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16384. #endif
  16385. #ifdef HAVE_FIPS
  16386. if (ret == PRIME_GEN_E)
  16387. continue;
  16388. break;
  16389. }
  16390. #endif
  16391. if (ret != 0)
  16392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16393. TEST_SLEEP();
  16394. #ifdef WOLFSSL_RSA_KEY_CHECK
  16395. ret = wc_CheckRsaKey(genKey);
  16396. if (ret != 0)
  16397. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16398. #endif
  16399. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16400. if (der == NULL) {
  16401. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16402. }
  16403. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  16404. if (derSz < 0) {
  16405. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16406. }
  16407. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16408. PRIVATEKEY_TYPE);
  16409. if (ret != 0) {
  16410. goto exit_rsa;
  16411. }
  16412. wc_FreeRsaKey(genKey);
  16413. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  16414. if (ret != 0)
  16415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16416. #ifndef WOLFSSL_CRYPTOCELL
  16417. idx = 0;
  16418. /* The private key part of the key gen pairs from cryptocell can't be exported */
  16419. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  16420. if (ret != 0)
  16421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16422. #endif /* WOLFSSL_CRYPTOCELL */
  16423. exit_rsa:
  16424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16425. if (genKey) {
  16426. wc_FreeRsaKey(genKey);
  16427. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16428. }
  16429. #else
  16430. wc_FreeRsaKey(genKey);
  16431. #endif
  16432. if (der != NULL) {
  16433. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16434. der = NULL;
  16435. }
  16436. return ret;
  16437. }
  16438. #endif
  16439. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16440. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  16441. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  16442. (!defined(HAVE_FIPS) || \
  16443. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  16444. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16445. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  16446. {
  16447. wc_test_ret_t ret = 0;
  16448. word32 idx = 0;
  16449. const char inStr[] = TEST_STRING;
  16450. const word32 inLen = (word32)TEST_STRING_SZ;
  16451. const word32 outSz = RSA_TEST_BYTES;
  16452. const word32 plainSz = RSA_TEST_BYTES;
  16453. byte* res = NULL;
  16454. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  16455. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16456. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16457. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16458. if (in == NULL || out == NULL || plain == NULL)
  16459. ERROR_OUT(MEMORY_E, exit_rsa);
  16460. #endif
  16461. XMEMCPY(in, inStr, inLen);
  16462. #ifndef NO_SHA
  16463. do {
  16464. #if defined(WOLFSSL_ASYNC_CRYPT)
  16465. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16466. #endif
  16467. if (ret >= 0) {
  16468. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16469. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16470. }
  16471. } while (ret == WC_PENDING_E);
  16472. if (ret < 0)
  16473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16474. TEST_SLEEP();
  16475. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16476. idx = (word32)ret;
  16477. do {
  16478. #if defined(WOLFSSL_ASYNC_CRYPT)
  16479. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16480. #endif
  16481. if (ret >= 0) {
  16482. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16483. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16484. }
  16485. } while (ret == WC_PENDING_E);
  16486. if (ret < 0)
  16487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16488. if (XMEMCMP(plain, in, inLen)) {
  16489. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16490. }
  16491. TEST_SLEEP();
  16492. #endif /* NO_SHA */
  16493. #endif
  16494. #ifndef NO_SHA256
  16495. XMEMSET(plain, 0, plainSz);
  16496. do {
  16497. #if defined(WOLFSSL_ASYNC_CRYPT)
  16498. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16499. #endif
  16500. if (ret >= 0) {
  16501. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16502. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  16503. }
  16504. } while (ret == WC_PENDING_E);
  16505. if (ret < 0)
  16506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16507. TEST_SLEEP();
  16508. idx = (word32)ret;
  16509. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16510. do {
  16511. #if defined(WOLFSSL_ASYNC_CRYPT)
  16512. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16513. #endif
  16514. if (ret >= 0) {
  16515. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16516. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  16517. }
  16518. } while (ret == WC_PENDING_E);
  16519. if (ret < 0)
  16520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16521. if (XMEMCMP(plain, in, inLen)) {
  16522. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16523. }
  16524. TEST_SLEEP();
  16525. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16526. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16527. do {
  16528. #if defined(WOLFSSL_ASYNC_CRYPT)
  16529. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16530. #endif
  16531. if (ret >= 0) {
  16532. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  16533. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  16534. }
  16535. } while (ret == WC_PENDING_E);
  16536. if (ret < 0)
  16537. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16538. if (ret != (int)inLen) {
  16539. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16540. }
  16541. if (XMEMCMP(res, in, inLen)) {
  16542. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16543. }
  16544. TEST_SLEEP();
  16545. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16546. /* check fails if not using the same optional label */
  16547. XMEMSET(plain, 0, plainSz);
  16548. do {
  16549. #if defined(WOLFSSL_ASYNC_CRYPT)
  16550. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16551. #endif
  16552. if (ret >= 0) {
  16553. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16554. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  16555. }
  16556. } while (ret == WC_PENDING_E);
  16557. if (ret < 0)
  16558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16559. TEST_SLEEP();
  16560. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  16561. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16562. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  16563. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  16564. * due to label */
  16565. idx = (word32)ret;
  16566. do {
  16567. #if defined(WOLFSSL_ASYNC_CRYPT)
  16568. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16569. #endif
  16570. if (ret >= 0) {
  16571. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16572. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  16573. }
  16574. } while (ret == WC_PENDING_E);
  16575. if (ret > 0) { /* in this case decrypt should fail */
  16576. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16577. }
  16578. ret = 0;
  16579. TEST_SLEEP();
  16580. #endif /* !HAVE_CAVIUM */
  16581. /* check using optional label with encrypt/decrypt */
  16582. XMEMSET(plain, 0, plainSz);
  16583. do {
  16584. #if defined(WOLFSSL_ASYNC_CRYPT)
  16585. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16586. #endif
  16587. if (ret >= 0) {
  16588. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16589. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  16590. }
  16591. } while (ret == WC_PENDING_E);
  16592. if (ret < 0)
  16593. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16594. TEST_SLEEP();
  16595. idx = (word32)ret;
  16596. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16597. do {
  16598. #if defined(WOLFSSL_ASYNC_CRYPT)
  16599. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16600. #endif
  16601. if (ret >= 0) {
  16602. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16603. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  16604. }
  16605. } while (ret == WC_PENDING_E);
  16606. if (ret < 0)
  16607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16608. if (XMEMCMP(plain, in, inLen)) {
  16609. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16610. }
  16611. TEST_SLEEP();
  16612. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16613. #ifndef NO_SHA
  16614. /* check fail using mismatch hash algorithms */
  16615. XMEMSET(plain, 0, plainSz);
  16616. do {
  16617. #if defined(WOLFSSL_ASYNC_CRYPT)
  16618. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16619. #endif
  16620. if (ret >= 0) {
  16621. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16622. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  16623. }
  16624. } while (ret == WC_PENDING_E);
  16625. if (ret < 0)
  16626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16627. TEST_SLEEP();
  16628. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  16629. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16630. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  16631. idx = (word32)ret;
  16632. do {
  16633. #if defined(WOLFSSL_ASYNC_CRYPT)
  16634. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16635. #endif
  16636. if (ret >= 0) {
  16637. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16638. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  16639. in, inLen);
  16640. }
  16641. } while (ret == WC_PENDING_E);
  16642. if (ret > 0) { /* should fail */
  16643. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16644. }
  16645. ret = 0;
  16646. TEST_SLEEP();
  16647. #endif /* !HAVE_CAVIUM */
  16648. #endif /* NO_SHA */
  16649. #endif /* NO_SHA256 */
  16650. #ifdef WOLFSSL_SHA512
  16651. /* Check valid RSA key size is used while using hash length of SHA512
  16652. If key size is less than (hash length * 2) + 2 then is invalid use
  16653. and test, since OAEP padding requires this.
  16654. BAD_FUNC_ARG is returned when this case is not met */
  16655. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  16656. XMEMSET(plain, 0, plainSz);
  16657. do {
  16658. #if defined(WOLFSSL_ASYNC_CRYPT)
  16659. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16660. #endif
  16661. if (ret >= 0) {
  16662. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16663. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  16664. }
  16665. } while (ret == WC_PENDING_E);
  16666. if (ret < 0)
  16667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16668. TEST_SLEEP();
  16669. idx = (word32)ret;
  16670. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16671. do {
  16672. #if defined(WOLFSSL_ASYNC_CRYPT)
  16673. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16674. #endif
  16675. if (ret >= 0) {
  16676. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16677. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  16678. }
  16679. } while (ret == WC_PENDING_E);
  16680. if (ret < 0)
  16681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16682. if (XMEMCMP(plain, in, inLen)) {
  16683. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16684. }
  16685. TEST_SLEEP();
  16686. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16687. }
  16688. #endif /* WOLFSSL_SHA512 */
  16689. /* check using pkcsv15 padding with _ex API */
  16690. XMEMSET(plain, 0, plainSz);
  16691. do {
  16692. #if defined(WOLFSSL_ASYNC_CRYPT)
  16693. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16694. #endif
  16695. if (ret >= 0) {
  16696. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16697. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  16698. }
  16699. } while (ret == WC_PENDING_E);
  16700. if (ret < 0)
  16701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16702. TEST_SLEEP();
  16703. idx = (word32)ret;
  16704. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16705. do {
  16706. #if defined(WOLFSSL_ASYNC_CRYPT)
  16707. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16708. #endif
  16709. if (ret >= 0) {
  16710. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16711. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  16712. }
  16713. } while (ret == WC_PENDING_E);
  16714. if (ret < 0)
  16715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16716. if (XMEMCMP(plain, in, inLen)) {
  16717. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16718. }
  16719. TEST_SLEEP();
  16720. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16721. exit_rsa:
  16722. WC_FREE_VAR(in, HEAP_HINT);
  16723. WC_FREE_VAR(out, HEAP_HINT);
  16724. WC_FREE_VAR(plain, HEAP_HINT);
  16725. (void)idx;
  16726. (void)inStr;
  16727. (void)res;
  16728. if (ret >= 0)
  16729. ret = 0;
  16730. return ret;
  16731. }
  16732. #endif
  16733. #endif
  16734. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  16735. {
  16736. wc_test_ret_t ret;
  16737. size_t bytes;
  16738. WC_RNG rng;
  16739. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16740. byte* tmp = NULL;
  16741. byte* der = NULL;
  16742. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16743. #else
  16744. RsaKey key[1];
  16745. byte tmp[FOURK_BUF];
  16746. #endif
  16747. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16749. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16750. #else
  16751. RsaKey keypub[1];
  16752. #endif
  16753. #endif
  16754. word32 idx = 0;
  16755. const char inStr[] = TEST_STRING;
  16756. const word32 inLen = (word32)TEST_STRING_SZ;
  16757. const word32 outSz = RSA_TEST_BYTES;
  16758. const word32 plainSz = RSA_TEST_BYTES;
  16759. byte* res = NULL;
  16760. #ifndef NO_SIG_WRAPPER
  16761. int modLen;
  16762. #endif
  16763. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  16764. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  16765. !defined(NO_FILESYSTEM)
  16766. XFILE file;
  16767. #ifdef WOLFSSL_TEST_CERT
  16768. XFILE file2;
  16769. #endif
  16770. #endif
  16771. #ifdef WOLFSSL_TEST_CERT
  16772. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16773. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16774. #else
  16775. DecodedCert cert[1];
  16776. #endif
  16777. #ifndef NO_ASN_TIME
  16778. struct tm timearg;
  16779. const byte* date;
  16780. byte dateFormat;
  16781. int dateLength;
  16782. #endif
  16783. #endif
  16784. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  16785. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16786. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16787. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16788. if (in == NULL || out == NULL || plain == NULL)
  16789. ERROR_OUT(MEMORY_E, exit_rsa);
  16790. #endif
  16791. XMEMCPY(in, inStr, inLen);
  16792. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16793. if (key == NULL)
  16794. ERROR_OUT(MEMORY_E, exit_rsa);
  16795. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16796. if (keypub == NULL)
  16797. ERROR_OUT(MEMORY_E, exit_rsa);
  16798. #endif
  16799. #ifdef WOLFSSL_TEST_CERT
  16800. if (cert == NULL)
  16801. ERROR_OUT(MEMORY_E, exit_rsa);
  16802. #endif
  16803. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16804. /* initialize stack structures */
  16805. XMEMSET(&rng, 0, sizeof(rng));
  16806. XMEMSET(key, 0, sizeof *key);
  16807. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16808. XMEMSET(keypub, 0, sizeof *keypub);
  16809. #endif
  16810. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  16811. ret = rsa_decode_test(key);
  16812. if (ret != 0)
  16813. ERROR_OUT(ret, exit_rsa);
  16814. #endif
  16815. #ifdef USE_CERT_BUFFERS_1024
  16816. bytes = (size_t)sizeof_client_key_der_1024;
  16817. if (bytes < (size_t)sizeof_client_cert_der_1024)
  16818. bytes = (size_t)sizeof_client_cert_der_1024;
  16819. #elif defined(USE_CERT_BUFFERS_2048)
  16820. bytes = (size_t)sizeof_client_key_der_2048;
  16821. if (bytes < (size_t)sizeof_client_cert_der_2048)
  16822. bytes = (size_t)sizeof_client_cert_der_2048;
  16823. #elif defined(USE_CERT_BUFFERS_3072)
  16824. bytes = (size_t)sizeof_client_key_der_3072;
  16825. if (bytes < (size_t)sizeof_client_cert_der_3072)
  16826. bytes = (size_t)sizeof_client_cert_der_3072;
  16827. #elif defined(USE_CERT_BUFFERS_4096)
  16828. bytes = (size_t)sizeof_client_key_der_4096;
  16829. if (bytes < (size_t)sizeof_client_cert_der_4096)
  16830. bytes = (size_t)sizeof_client_cert_der_4096;
  16831. #else
  16832. bytes = FOURK_BUF;
  16833. #endif
  16834. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16835. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16836. if (tmp == NULL)
  16837. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16838. #endif
  16839. #ifdef USE_CERT_BUFFERS_1024
  16840. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  16841. #elif defined(USE_CERT_BUFFERS_2048)
  16842. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  16843. #elif defined(USE_CERT_BUFFERS_3072)
  16844. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  16845. #elif defined(USE_CERT_BUFFERS_4096)
  16846. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  16847. #elif !defined(NO_FILESYSTEM)
  16848. file = XFOPEN(clientKey, "rb");
  16849. if (!file) {
  16850. err_sys("can't open ./certs/client-key.der, "
  16851. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  16852. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16853. }
  16854. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16855. XFCLOSE(file);
  16856. if (bytes == 0)
  16857. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16858. #else
  16859. /* No key to use. */
  16860. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16861. #endif /* USE_CERT_BUFFERS */
  16862. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  16863. if (ret != 0)
  16864. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16865. #ifndef NO_ASN
  16866. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  16867. if (ret != 0)
  16868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16869. #ifndef NO_SIG_WRAPPER
  16870. modLen = wc_RsaEncryptSize(key);
  16871. #endif
  16872. #else
  16873. #ifdef USE_CERT_BUFFERS_2048
  16874. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  16875. if (ret != 0)
  16876. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16877. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  16878. if (ret != 0)
  16879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16880. #ifndef NO_SIG_WRAPPER
  16881. modLen = 2048;
  16882. #endif
  16883. #else
  16884. #error Not supported yet!
  16885. #endif
  16886. #endif
  16887. #ifndef WC_NO_RNG
  16888. #ifndef HAVE_FIPS
  16889. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16890. #else
  16891. ret = wc_InitRng(&rng);
  16892. #endif
  16893. if (ret != 0)
  16894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16895. #endif
  16896. #ifndef NO_SIG_WRAPPER
  16897. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  16898. if (ret != 0)
  16899. goto exit_rsa;
  16900. #endif
  16901. #ifdef WC_RSA_NONBLOCK
  16902. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  16903. if (ret != 0)
  16904. goto exit_rsa;
  16905. #endif
  16906. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16907. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16908. do {
  16909. #if defined(WOLFSSL_ASYNC_CRYPT)
  16910. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16911. #endif
  16912. if (ret >= 0) {
  16913. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  16914. }
  16915. } while (ret == WC_PENDING_E);
  16916. if (ret < 0)
  16917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16918. TEST_SLEEP();
  16919. #ifdef WC_RSA_BLINDING
  16920. {
  16921. wc_test_ret_t tmpret = ret;
  16922. ret = wc_RsaSetRNG(key, &rng);
  16923. if (ret < 0)
  16924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16925. ret = tmpret;
  16926. }
  16927. #endif
  16928. idx = (word32)ret; /* save off encrypted length */
  16929. do {
  16930. #if defined(WOLFSSL_ASYNC_CRYPT)
  16931. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16932. #endif
  16933. if (ret >= 0) {
  16934. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  16935. }
  16936. } while (ret == WC_PENDING_E);
  16937. if (ret < 0)
  16938. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16939. if (XMEMCMP(plain, in, inLen)) {
  16940. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16941. }
  16942. TEST_SLEEP();
  16943. do {
  16944. #if defined(WOLFSSL_ASYNC_CRYPT)
  16945. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16946. #endif
  16947. if (ret >= 0) {
  16948. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  16949. }
  16950. } while (ret == WC_PENDING_E);
  16951. if (ret < 0)
  16952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16953. if (ret != (int)inLen) {
  16954. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16955. }
  16956. if (XMEMCMP(res, in, inLen)) {
  16957. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16958. }
  16959. TEST_SLEEP();
  16960. do {
  16961. #if defined(WOLFSSL_ASYNC_CRYPT)
  16962. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16963. #endif
  16964. if (ret >= 0) {
  16965. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  16966. }
  16967. } while (ret == WC_PENDING_E);
  16968. if (ret < 0)
  16969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16970. TEST_SLEEP();
  16971. #elif defined(WOLFSSL_PUBLIC_MP)
  16972. {
  16973. static byte signature_2048[] = {
  16974. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  16975. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  16976. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  16977. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  16978. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  16979. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  16980. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  16981. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  16982. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  16983. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  16984. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  16985. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  16986. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  16987. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  16988. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  16989. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  16990. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  16991. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  16992. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  16993. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  16994. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  16995. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  16996. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  16997. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  16998. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  16999. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  17000. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  17001. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  17002. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  17003. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  17004. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  17005. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  17006. };
  17007. ret = sizeof(signature_2048);
  17008. XMEMCPY(out, signature_2048, ret);
  17009. }
  17010. #endif
  17011. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  17012. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  17013. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17014. idx = (word32)ret;
  17015. XMEMSET(plain, 0, plainSz);
  17016. do {
  17017. #if defined(WOLFSSL_ASYNC_CRYPT)
  17018. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17019. #endif
  17020. if (ret >= 0) {
  17021. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  17022. #if defined(WOLFSSL_CRYPTOCELL)
  17023. /*
  17024. Cryptocell requires the input data and signature byte array to verify.
  17025. first argument must be the input data
  17026. second argument must be the length of input data
  17027. third argument must be the signature byte array or the output from
  17028. wc_RsaSSL_Sign()
  17029. fourth argument must be the length of the signature byte array
  17030. */
  17031. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  17032. #else
  17033. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  17034. #endif /* WOLFSSL_CRYPTOCELL */
  17035. #else
  17036. byte* dec = NULL;
  17037. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  17038. if (ret > 0) {
  17039. XMEMCPY(plain, dec, ret);
  17040. }
  17041. #endif
  17042. }
  17043. } while (ret == WC_PENDING_E);
  17044. if (ret < 0)
  17045. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17046. if (XMEMCMP(plain, in, (size_t)ret)) {
  17047. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17048. }
  17049. TEST_SLEEP();
  17050. #endif
  17051. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17052. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  17053. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  17054. (!defined(HAVE_FIPS) || \
  17055. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  17056. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17057. ret = rsa_oaep_padding_test(key, &rng);
  17058. if (ret != 0)
  17059. return ret;
  17060. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  17061. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  17062. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17063. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  17064. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  17065. ret = rsa_export_key_test(key);
  17066. if (ret != 0)
  17067. return ret;
  17068. #endif
  17069. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17070. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  17071. ret = rsa_flatten_test(key);
  17072. if (ret != 0)
  17073. return ret;
  17074. #endif
  17075. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  17076. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17077. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  17078. (void)clientCert;
  17079. #endif
  17080. #ifdef WOLFSSL_TEST_CERT
  17081. #if defined(WOLFSSL_MDK_ARM)
  17082. #define sizeof(s) XSTRLEN((char *)(s))
  17083. #endif
  17084. #ifdef USE_CERT_BUFFERS_1024
  17085. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  17086. bytes = (size_t)sizeof_client_cert_der_1024;
  17087. #elif defined(USE_CERT_BUFFERS_2048)
  17088. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  17089. bytes = (size_t)sizeof_client_cert_der_2048;
  17090. #elif defined(USE_CERT_BUFFERS_3072)
  17091. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  17092. bytes = (size_t)sizeof_client_cert_der_3072;
  17093. #elif defined(USE_CERT_BUFFERS_4096)
  17094. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  17095. bytes = (size_t)sizeof_client_cert_der_4096;
  17096. #elif !defined(NO_FILESYSTEM)
  17097. file2 = XFOPEN(clientCert, "rb");
  17098. if (!file2) {
  17099. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17100. }
  17101. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  17102. XFCLOSE(file2);
  17103. if (bytes == 0)
  17104. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17105. #else
  17106. /* No certificate to use. */
  17107. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17108. #endif
  17109. #ifdef sizeof
  17110. #undef sizeof
  17111. #endif
  17112. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  17113. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  17114. if (ret != 0) {
  17115. FreeDecodedCert(cert);
  17116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17117. }
  17118. #ifndef NO_ASN_TIME
  17119. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  17120. &dateFormat, &dateLength);
  17121. if (ret != 0) {
  17122. FreeDecodedCert(cert);
  17123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17124. }
  17125. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  17126. if (ret != 0) {
  17127. FreeDecodedCert(cert);
  17128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17129. }
  17130. #endif
  17131. FreeDecodedCert(cert);
  17132. #endif /* WOLFSSL_TEST_CERT */
  17133. #ifdef WOLFSSL_CERT_EXT
  17134. #ifdef USE_CERT_BUFFERS_1024
  17135. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17136. bytes = sizeof_client_keypub_der_1024;
  17137. #elif defined(USE_CERT_BUFFERS_2048)
  17138. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17139. bytes = sizeof_client_keypub_der_2048;
  17140. #elif defined(USE_CERT_BUFFERS_3072)
  17141. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  17142. bytes = sizeof_client_keypub_der_3072;
  17143. #elif defined(USE_CERT_BUFFERS_4096)
  17144. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  17145. bytes = sizeof_client_keypub_der_4096;
  17146. #else
  17147. file = XFOPEN(clientKeyPub, "rb");
  17148. if (!file) {
  17149. err_sys("can't open ./certs/client-keyPub.der, "
  17150. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17151. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17152. }
  17153. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17154. XFCLOSE(file);
  17155. if (bytes == 0)
  17156. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17157. #endif /* USE_CERT_BUFFERS */
  17158. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  17159. if (ret != 0)
  17160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17161. idx = 0;
  17162. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  17163. if (ret != 0)
  17164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17165. #endif /* WOLFSSL_CERT_EXT */
  17166. #ifdef WOLFSSL_KEY_GEN
  17167. ret = rsa_keygen_test(&rng);
  17168. if (ret != 0)
  17169. goto exit_rsa;
  17170. #endif
  17171. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  17172. /* Make Cert / Sign example for RSA cert and RSA CA */
  17173. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  17174. if (ret != 0)
  17175. goto exit_rsa;
  17176. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  17177. ret = rsa_ecc_certgen_test(&rng, tmp);
  17178. if (ret != 0)
  17179. goto exit_rsa;
  17180. #endif
  17181. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  17182. {
  17183. Cert *req;
  17184. int derSz;
  17185. #ifndef WOLFSSL_SMALL_STACK
  17186. byte* der = NULL;
  17187. #endif
  17188. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17189. if (! req)
  17190. ERROR_OUT(MEMORY_E, exit_rsa);
  17191. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  17192. if (der == NULL) {
  17193. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17194. }
  17195. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  17196. if (ret != 0)
  17197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17198. req->version = 0;
  17199. req->isCA = 1;
  17200. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  17201. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  17202. #ifndef NO_SHA256
  17203. req->sigType = CTC_SHA256wRSA;
  17204. #else
  17205. req->sigType = CTC_SHAwRSA;
  17206. #endif
  17207. #ifdef WOLFSSL_CERT_EXT
  17208. /* add SKID from the Public Key */
  17209. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  17210. if (ret != 0)
  17211. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17212. /* add Key Usage */
  17213. ret = wc_SetKeyUsage(req, certKeyUsage2);
  17214. if (ret != 0)
  17215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17216. /* add Extended Key Usage */
  17217. ret = wc_SetExtKeyUsage(req,
  17218. "serverAuth,clientAuth,codeSigning,"
  17219. "emailProtection,timeStamping,OCSPSigning");
  17220. if (ret != 0)
  17221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17222. #ifdef WOLFSSL_EKU_OID
  17223. {
  17224. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  17225. "2.16.840.1.111111.100.1.10.1";
  17226. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  17227. HEAP_HINT);
  17228. if (ret != 0)
  17229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17230. }
  17231. #endif /* WOLFSSL_EKU_OID */
  17232. #endif /* WOLFSSL_CERT_EXT */
  17233. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  17234. if (derSz < 0) {
  17235. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17236. }
  17237. #ifdef WOLFSSL_CERT_EXT
  17238. /* Try again with "any" flag set, will override all others */
  17239. ret = wc_SetExtKeyUsage(req, "any");
  17240. if (ret != 0)
  17241. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17242. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  17243. if (derSz < 0) {
  17244. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17245. }
  17246. #endif /* WOLFSSL_CERT_EXT */
  17247. ret = 0;
  17248. do {
  17249. #if defined(WOLFSSL_ASYNC_CRYPT)
  17250. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17251. #endif
  17252. if (ret >= 0) {
  17253. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  17254. key, NULL, &rng);
  17255. }
  17256. } while (ret == WC_PENDING_E);
  17257. if (ret < 0)
  17258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17259. derSz = (word32)ret;
  17260. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  17261. CERTREQ_TYPE);
  17262. if (ret != 0) {
  17263. goto exit_rsa;
  17264. }
  17265. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  17266. if (derSz < 0) {
  17267. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17268. }
  17269. /* Test getting the size of the buffer without providing the buffer.
  17270. * derSz is set to the "largest buffer" we are willing to allocate. */
  17271. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  17272. if (derSz < 0) {
  17273. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17274. }
  17275. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17276. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17277. der = NULL;
  17278. }
  17279. #endif /* WOLFSSL_CERT_REQ */
  17280. #endif /* WOLFSSL_CERT_GEN */
  17281. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17282. /* Need to create known good signatures to test with this. */
  17283. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17284. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17285. ret = rsa_pss_test(&rng, key);
  17286. if (ret != 0)
  17287. goto exit_rsa;
  17288. #endif
  17289. #endif
  17290. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17291. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17292. /* New key to be loaded in rsa_even_mod_test(). */
  17293. if (key != NULL)
  17294. #endif
  17295. wc_FreeRsaKey(key);
  17296. /* New key to be loaded in rsa_even_mod_test(). */
  17297. ret = rsa_even_mod_test(&rng, key);
  17298. #endif
  17299. exit_rsa:
  17300. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17301. if (key != NULL) {
  17302. wc_FreeRsaKey(key);
  17303. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17304. }
  17305. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17306. if (keypub != NULL) {
  17307. wc_FreeRsaKey(keypub);
  17308. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17309. }
  17310. #endif
  17311. #ifdef WOLFSSL_TEST_CERT
  17312. if (cert != NULL)
  17313. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17314. #endif
  17315. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17316. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17317. #else
  17318. wc_FreeRsaKey(key);
  17319. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17320. wc_FreeRsaKey(keypub);
  17321. #endif
  17322. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17323. wc_FreeRng(&rng);
  17324. WC_FREE_VAR(in, HEAP_HINT);
  17325. WC_FREE_VAR(out, HEAP_HINT);
  17326. WC_FREE_VAR(plain, HEAP_HINT);
  17327. (void)res;
  17328. (void)bytes;
  17329. (void)idx;
  17330. (void)in;
  17331. (void)out;
  17332. (void)plain;
  17333. (void)idx;
  17334. (void)inStr;
  17335. (void)inLen;
  17336. (void)outSz;
  17337. (void)plainSz;
  17338. /* ret can be greater then 0 with certgen but all negative values should
  17339. * be returned and treated as an error */
  17340. if (ret >= 0) {
  17341. return 0;
  17342. }
  17343. else {
  17344. return ret;
  17345. }
  17346. }
  17347. #endif /* !NO_RSA */
  17348. #ifndef NO_DH
  17349. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  17350. {
  17351. wc_test_ret_t ret = 0;
  17352. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17353. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17354. #else
  17355. DhKey key[1];
  17356. #endif
  17357. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  17358. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  17359. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  17360. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  17361. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  17362. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  17363. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  17364. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  17365. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  17366. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  17367. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  17368. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  17369. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  17370. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  17371. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  17372. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  17373. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  17374. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  17375. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  17376. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  17377. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  17378. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  17379. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  17380. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  17381. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  17382. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  17383. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  17384. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  17385. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  17386. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  17387. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  17388. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  17389. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  17390. };
  17391. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  17392. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  17393. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  17394. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  17395. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  17396. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  17397. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  17398. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  17399. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  17400. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  17401. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  17402. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  17403. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  17404. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  17405. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  17406. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  17407. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  17408. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  17409. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  17410. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  17411. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  17412. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  17413. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  17414. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  17415. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  17416. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  17417. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  17418. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  17419. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  17420. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  17421. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  17422. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  17423. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  17424. };
  17425. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  17426. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  17427. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  17428. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  17429. 0x40, 0x52, 0xed, 0x41
  17430. };
  17431. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  17432. 0x00,
  17433. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  17434. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  17435. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  17436. 0x40, 0x52, 0xed, 0x41
  17437. };
  17438. byte priv[256];
  17439. byte pub[256];
  17440. word32 privSz = sizeof(priv);
  17441. word32 pubSz = sizeof(pub);
  17442. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17443. if (key == NULL)
  17444. ERROR_OUT(MEMORY_E, exit_gen_test);
  17445. #endif
  17446. /* Parameter Validation testing. */
  17447. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  17448. if (ret != BAD_FUNC_ARG)
  17449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17450. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  17451. if (ret != BAD_FUNC_ARG)
  17452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17453. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  17454. if (ret != BAD_FUNC_ARG)
  17455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17456. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  17457. if (ret != BAD_FUNC_ARG)
  17458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17459. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  17460. if (ret != BAD_FUNC_ARG)
  17461. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17462. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  17463. if (ret != BAD_FUNC_ARG)
  17464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17465. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17466. if (ret != 0)
  17467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17468. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  17469. if (ret != 0)
  17470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17471. wc_FreeDhKey(key);
  17472. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17473. if (ret != 0)
  17474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17475. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  17476. if (ret != 0)
  17477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17478. /* Use API. */
  17479. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  17480. #if defined(WOLFSSL_ASYNC_CRYPT)
  17481. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17482. #endif
  17483. if (ret != 0)
  17484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17485. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  17486. if (ret != 0)
  17487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17488. wc_FreeDhKey(key);
  17489. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17490. if (ret != 0)
  17491. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17492. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  17493. if (ret != 0)
  17494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17495. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  17496. if (ret != 0)
  17497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17498. #ifndef HAVE_SELFTEST
  17499. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  17500. if (ret != 0)
  17501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17502. /* Taint the public key so the check fails. */
  17503. pub[0]++;
  17504. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  17505. if (ret != MP_CMP_E) {
  17506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17507. }
  17508. #ifdef WOLFSSL_KEY_GEN
  17509. wc_FreeDhKey(key);
  17510. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17511. if (ret != 0)
  17512. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17513. ret = wc_DhGenerateParams(rng, 2048, key);
  17514. if (ret != 0)
  17515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17516. privSz = sizeof(priv);
  17517. pubSz = sizeof(pub);
  17518. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  17519. #if defined(WOLFSSL_ASYNC_CRYPT)
  17520. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17521. #endif
  17522. if (ret != 0)
  17523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17524. #endif /* WOLFSSL_KEY_GEN */
  17525. #endif /* HAVE_SELFTEST */
  17526. ret = 0;
  17527. exit_gen_test:
  17528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17529. if (key) {
  17530. wc_FreeDhKey(key);
  17531. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17532. }
  17533. #else
  17534. wc_FreeDhKey(key);
  17535. #endif
  17536. return ret;
  17537. }
  17538. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  17539. {
  17540. wc_test_ret_t ret = 0;
  17541. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17542. DhKey *smallKey = NULL;
  17543. #else
  17544. DhKey smallKey[1];
  17545. #endif
  17546. byte p[2] = { 1, 7 }; /* 263 in decimal */
  17547. byte g[2] = { 0, 2 };
  17548. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  17549. #ifdef WOLFSSL_DH_CONST
  17550. /* the table for constant DH lookup will round to the lowest byte size 21 */
  17551. byte priv[21];
  17552. byte pub[21];
  17553. #else
  17554. byte priv[2];
  17555. byte pub[2];
  17556. #endif
  17557. word32 privSz = sizeof(priv);
  17558. word32 pubSz = sizeof(pub);
  17559. #endif
  17560. int smallKey_inited = 0;
  17561. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17562. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  17563. return WC_TEST_RET_ENC_ERRNO;
  17564. #endif
  17565. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  17566. if (ret != 0)
  17567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17568. smallKey_inited = 1;
  17569. /* Parameter Validation testing. */
  17570. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  17571. if (ret != BAD_FUNC_ARG)
  17572. return WC_TEST_RET_ENC_EC(ret);
  17573. wc_FreeDhKey(NULL);
  17574. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  17575. if (ret != BAD_FUNC_ARG) {
  17576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17577. }
  17578. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  17579. if (ret != BAD_FUNC_ARG) {
  17580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17581. }
  17582. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  17583. if (ret != BAD_FUNC_ARG) {
  17584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17585. }
  17586. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  17587. if (ret != BAD_FUNC_ARG) {
  17588. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17589. }
  17590. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  17591. if (ret != BAD_FUNC_ARG) {
  17592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17593. }
  17594. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  17595. if (ret != 0)
  17596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17597. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  17598. /* Use API. */
  17599. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  17600. #if defined(WOLFSSL_ASYNC_CRYPT)
  17601. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  17602. #endif
  17603. if (ret != 0) {
  17604. ret = WC_TEST_RET_ENC_EC(ret);
  17605. }
  17606. #else
  17607. (void)rng;
  17608. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  17609. ret = 0;
  17610. #endif
  17611. #endif
  17612. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  17613. {
  17614. byte priv[260];
  17615. byte pub[260];
  17616. word32 privSz = sizeof(priv);
  17617. word32 pubSz = sizeof(pub);
  17618. /* test odd ball param generation with DH */
  17619. wc_FreeDhKey(smallKey);
  17620. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  17621. if (ret != 0)
  17622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17623. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  17624. if (ret != 0)
  17625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17626. privSz = sizeof(priv);
  17627. pubSz = sizeof(pub);
  17628. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  17629. #if defined(WOLFSSL_ASYNC_CRYPT)
  17630. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  17631. #endif
  17632. if (ret != 0)
  17633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17634. }
  17635. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  17636. exit_gen_test:
  17637. if (smallKey_inited)
  17638. wc_FreeDhKey(smallKey);
  17639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17640. if (smallKey != NULL)
  17641. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17642. #endif
  17643. return ret;
  17644. }
  17645. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17646. typedef struct dh_pubvalue_test {
  17647. const byte* data;
  17648. word32 len;
  17649. } dh_pubvalue_test;
  17650. static wc_test_ret_t dh_test_check_pubvalue(void)
  17651. {
  17652. wc_test_ret_t ret;
  17653. word32 i;
  17654. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  17655. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  17656. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  17657. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  17658. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  17659. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  17660. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  17661. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  17662. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  17663. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  17664. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  17665. const dh_pubvalue_test dh_pubval_fail[] = {
  17666. { prime, sizeof(prime) },
  17667. { pubValZero, sizeof(pubValZero) },
  17668. { pubValZeroLong, sizeof(pubValZeroLong) },
  17669. { pubValOne, sizeof(pubValOne) },
  17670. { pubValOneLong, sizeof(pubValOneLong) },
  17671. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  17672. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  17673. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  17674. { pubValTooBig0, sizeof(pubValTooBig0) },
  17675. { pubValTooBig1, sizeof(pubValTooBig1) },
  17676. { pubValTooLong, sizeof(pubValTooLong) },
  17677. };
  17678. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  17679. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  17680. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  17681. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  17682. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  17683. const dh_pubvalue_test dh_pubval_pass[] = {
  17684. { pubValTwo, sizeof(pubValTwo) },
  17685. { pubValTwoLong, sizeof(pubValTwoLong) },
  17686. { pubValGood, sizeof(pubValGood) },
  17687. { pubValGoodLen, sizeof(pubValGoodLen) },
  17688. { pubValGoodLong, sizeof(pubValGoodLong) },
  17689. };
  17690. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  17691. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  17692. dh_pubval_fail[i].len);
  17693. if (ret != MP_VAL)
  17694. return WC_TEST_RET_ENC_I(i);
  17695. }
  17696. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  17697. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  17698. dh_pubval_pass[i].len);
  17699. if (ret != 0)
  17700. return WC_TEST_RET_ENC_I(i);
  17701. }
  17702. return 0;
  17703. }
  17704. #endif
  17705. #if defined(HAVE_FFDHE)
  17706. #if defined(HAVE_FFDHE_4096)
  17707. #define MAX_DH_PRIV_SZ 39
  17708. #define MAX_DH_KEY_SZ 512
  17709. #elif defined(HAVE_FFDHE_3072)
  17710. #define MAX_DH_PRIV_SZ 34
  17711. #define MAX_DH_KEY_SZ 384
  17712. #else
  17713. #define MAX_DH_PRIV_SZ 29
  17714. #define MAX_DH_KEY_SZ 256
  17715. #endif
  17716. #ifndef WC_NO_RNG
  17717. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17718. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  17719. #ifdef HAVE_PUBLIC_FFDHE
  17720. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  17721. #else
  17722. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  17723. #endif
  17724. {
  17725. wc_test_ret_t ret;
  17726. word32 privSz, pubSz, privSz2, pubSz2;
  17727. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17728. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17729. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17730. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17731. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17732. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17733. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17734. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17735. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17736. #else
  17737. byte priv[MAX_DH_PRIV_SZ];
  17738. byte pub[MAX_DH_KEY_SZ];
  17739. byte priv2[MAX_DH_PRIV_SZ];
  17740. byte pub2[MAX_DH_KEY_SZ];
  17741. byte agree[MAX_DH_KEY_SZ];
  17742. byte agree2[MAX_DH_KEY_SZ];
  17743. DhKey key[1];
  17744. DhKey key2[1];
  17745. #endif
  17746. word32 agreeSz = MAX_DH_KEY_SZ;
  17747. word32 agreeSz2 = MAX_DH_KEY_SZ;
  17748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17749. if ((priv == NULL) ||
  17750. (pub == NULL) ||
  17751. (priv2 == NULL) ||
  17752. (pub2 == NULL) ||
  17753. (agree == NULL) ||
  17754. (agree2 == NULL) ||
  17755. (key == NULL) ||
  17756. (key2 == NULL))
  17757. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17758. #endif
  17759. pubSz = MAX_DH_KEY_SZ;
  17760. pubSz2 = MAX_DH_KEY_SZ;
  17761. #ifdef HAVE_PUBLIC_FFDHE
  17762. privSz = MAX_DH_PRIV_SZ;
  17763. privSz2 = MAX_DH_PRIV_SZ;
  17764. #else
  17765. privSz = wc_DhGetNamedKeyMinSize(name);
  17766. privSz2 = privSz;
  17767. #endif
  17768. XMEMSET(key, 0, sizeof(*key));
  17769. XMEMSET(key2, 0, sizeof(*key2));
  17770. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17771. if (ret != 0)
  17772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17773. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  17774. if (ret != 0)
  17775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17776. #ifdef HAVE_PUBLIC_FFDHE
  17777. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  17778. #else
  17779. ret = wc_DhSetNamedKey(key, name);
  17780. #endif
  17781. if (ret != 0)
  17782. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17783. #ifdef HAVE_PUBLIC_FFDHE
  17784. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  17785. params->g_len);
  17786. #else
  17787. ret = wc_DhSetNamedKey(key2, name);
  17788. #endif
  17789. if (ret != 0)
  17790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17791. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  17792. #if defined(WOLFSSL_ASYNC_CRYPT)
  17793. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17794. #endif
  17795. if (ret != 0)
  17796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17797. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  17798. #if defined(WOLFSSL_ASYNC_CRYPT)
  17799. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  17800. #endif
  17801. if (ret != 0)
  17802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17803. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  17804. #if defined(WOLFSSL_ASYNC_CRYPT)
  17805. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17806. #endif
  17807. if (ret != 0)
  17808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17809. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  17810. #if defined(WOLFSSL_ASYNC_CRYPT)
  17811. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  17812. #endif
  17813. if (ret != 0)
  17814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17815. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  17816. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17817. }
  17818. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  17819. /* Make p even */
  17820. key->p.dp[0] &= (mp_digit)-2;
  17821. if (ret != 0)
  17822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17823. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  17824. #if defined(WOLFSSL_ASYNC_CRYPT)
  17825. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17826. #endif
  17827. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17829. }
  17830. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  17831. #if defined(WOLFSSL_ASYNC_CRYPT)
  17832. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17833. #endif
  17834. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  17835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17836. }
  17837. #ifndef HAVE_SELFTEST
  17838. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  17839. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  17840. ret != ASYNC_OP_E) {
  17841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17842. }
  17843. #endif
  17844. /* Getting here means success - set ret to 0. */
  17845. ret = 0;
  17846. #endif
  17847. done:
  17848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  17849. !defined(WC_NO_RNG)
  17850. if (priv)
  17851. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17852. if (pub)
  17853. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17854. if (priv2)
  17855. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17856. if (pub2)
  17857. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17858. if (agree)
  17859. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17860. if (agree2)
  17861. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17862. if (key) {
  17863. wc_FreeDhKey(key);
  17864. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17865. }
  17866. if (key2) {
  17867. wc_FreeDhKey(key2);
  17868. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17869. }
  17870. #else
  17871. wc_FreeDhKey(key);
  17872. wc_FreeDhKey(key2);
  17873. #endif
  17874. return ret;
  17875. }
  17876. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  17877. #endif /* !WC_NO_RNG */
  17878. #endif /* HAVE_FFDHE */
  17879. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  17880. {
  17881. wc_test_ret_t ret;
  17882. word32 bytes;
  17883. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  17884. #ifndef WC_NO_RNG
  17885. WC_RNG rng;
  17886. int rngInit = 0;
  17887. #endif
  17888. int keyInit = 0, key2Init = 0;
  17889. #define DH_TEST_TMP_SIZE 1024
  17890. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  17891. #define DH_TEST_BUF_SIZE 256
  17892. #else
  17893. #define DH_TEST_BUF_SIZE 512
  17894. #endif
  17895. #ifndef WC_NO_RNG
  17896. word32 agreeSz = DH_TEST_BUF_SIZE;
  17897. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  17898. #endif
  17899. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17900. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17901. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17902. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17903. #else
  17904. DhKey key[1];
  17905. DhKey key2[1];
  17906. byte tmp[DH_TEST_TMP_SIZE];
  17907. #endif
  17908. #ifndef WC_NO_RNG
  17909. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17910. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17911. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17912. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17913. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17914. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17915. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17916. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  17917. agree == NULL || agree2 == NULL) {
  17918. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17919. }
  17920. #else
  17921. byte priv[DH_TEST_BUF_SIZE];
  17922. byte pub[DH_TEST_BUF_SIZE];
  17923. byte priv2[DH_TEST_BUF_SIZE];
  17924. byte pub2[DH_TEST_BUF_SIZE];
  17925. byte agree[DH_TEST_BUF_SIZE];
  17926. byte agree2[DH_TEST_BUF_SIZE];
  17927. #endif
  17928. #endif /* !WC_NO_RNG */
  17929. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17930. if (key == NULL || key2 == NULL || tmp == NULL) {
  17931. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17932. }
  17933. #endif
  17934. #ifdef USE_CERT_BUFFERS_1024
  17935. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  17936. bytes = (size_t)sizeof_dh_key_der_1024;
  17937. #elif defined(USE_CERT_BUFFERS_2048)
  17938. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  17939. bytes = (size_t)sizeof_dh_key_der_2048;
  17940. #elif defined(USE_CERT_BUFFERS_3072)
  17941. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  17942. bytes = (size_t)sizeof_dh_key_der_3072;
  17943. #elif defined(USE_CERT_BUFFERS_4096)
  17944. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  17945. bytes = (size_t)sizeof_dh_key_der_4096;
  17946. #elif defined(NO_ASN)
  17947. /* don't use file, no DER parsing */
  17948. #elif !defined(NO_FILESYSTEM)
  17949. {
  17950. XFILE file = XFOPEN(dhParamsFile, "rb");
  17951. if (! file)
  17952. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17953. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  17954. XFCLOSE(file);
  17955. if (bytes == 0)
  17956. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17957. }
  17958. #else
  17959. /* No DH key to use. */
  17960. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17961. #endif /* USE_CERT_BUFFERS */
  17962. (void)idx;
  17963. (void)tmp;
  17964. (void)bytes;
  17965. pubSz = DH_TEST_BUF_SIZE;
  17966. pubSz2 = DH_TEST_BUF_SIZE;
  17967. privSz = DH_TEST_BUF_SIZE;
  17968. privSz2 = DH_TEST_BUF_SIZE;
  17969. #ifndef WC_NO_RNG
  17970. XMEMSET(&rng, 0, sizeof(rng));
  17971. #endif
  17972. /* Use API for coverage. */
  17973. ret = wc_InitDhKey(key);
  17974. if (ret != 0)
  17975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17976. wc_FreeDhKey(key);
  17977. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17978. if (ret != 0)
  17979. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17980. keyInit = 1;
  17981. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  17982. if (ret != 0)
  17983. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17984. key2Init = 1;
  17985. #ifdef NO_ASN
  17986. #ifndef WOLFSSL_SP_MATH
  17987. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17988. if (ret != 0)
  17989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17990. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17991. if (ret != 0)
  17992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17993. #else
  17994. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  17995. sizeof(dh2048_g));
  17996. if (ret != 0)
  17997. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17998. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  17999. sizeof(dh2048_g));
  18000. if (ret != 0)
  18001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18002. #endif
  18003. #else
  18004. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18005. if (ret != 0)
  18006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18007. idx = 0;
  18008. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  18009. if (ret != 0)
  18010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18011. #endif
  18012. #ifndef WC_NO_RNG
  18013. #ifndef HAVE_FIPS
  18014. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18015. #else
  18016. ret = wc_InitRng(&rng);
  18017. #endif
  18018. if (ret != 0)
  18019. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18020. rngInit = 1;
  18021. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  18022. #if defined(WOLFSSL_ASYNC_CRYPT)
  18023. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18024. #endif
  18025. if (ret != 0)
  18026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18027. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  18028. #if defined(WOLFSSL_ASYNC_CRYPT)
  18029. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18030. #endif
  18031. if (ret != 0)
  18032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18033. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  18034. #if defined(WOLFSSL_ASYNC_CRYPT)
  18035. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18036. #endif
  18037. if (ret != 0)
  18038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18039. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  18040. #if defined(WOLFSSL_ASYNC_CRYPT)
  18041. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18042. #endif
  18043. if (ret != 0)
  18044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18045. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  18046. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18047. }
  18048. #endif /* !WC_NO_RNG */
  18049. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18050. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  18051. if (ret != BAD_FUNC_ARG)
  18052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18053. ret = wc_DhCheckPrivKey(key, priv, privSz);
  18054. if (ret != 0)
  18055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18056. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  18057. if (ret != BAD_FUNC_ARG)
  18058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18059. {
  18060. word32 pSz, qSz, gSz;
  18061. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  18062. if (ret != LENGTH_ONLY_E)
  18063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18064. }
  18065. #endif
  18066. /* Test DH key import / export */
  18067. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  18068. (!defined(HAVE_FIPS) || \
  18069. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  18070. wc_FreeDhKey(key);
  18071. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18072. if (ret != 0)
  18073. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18074. #ifndef NO_ASN
  18075. {
  18076. /* DH Private - Key Export / Import */
  18077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18078. byte *tmp2;
  18079. #else
  18080. byte tmp2[DH_TEST_TMP_SIZE];
  18081. #endif
  18082. #if defined(USE_CERT_BUFFERS_2048)
  18083. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  18084. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  18085. #else
  18086. XFILE file = XFOPEN(dhKeyFile, "rb");
  18087. if (!file)
  18088. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18089. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  18090. XFCLOSE(file);
  18091. if (bytes == 0)
  18092. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18093. #endif
  18094. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18095. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18096. if (tmp2 == NULL)
  18097. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18098. #endif
  18099. idx = 0;
  18100. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  18101. /* Import DH Private key as DER */
  18102. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18103. if (ret == 0) {
  18104. /* Export as DER */
  18105. idx = DH_TEST_TMP_SIZE;
  18106. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  18107. }
  18108. /* Verify export matches original */
  18109. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  18110. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18111. }
  18112. /* DH Public Key - Export / Import */
  18113. #if defined(USE_CERT_BUFFERS_2048)
  18114. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  18115. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  18116. #else
  18117. file = XFOPEN(dhKeyPubFile, "rb");
  18118. if (!file)
  18119. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18120. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  18121. XFCLOSE(file);
  18122. if (bytes == 0)
  18123. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18124. #endif
  18125. /* for HAVE_WOLF_BIGINT prevent leak */
  18126. wc_FreeDhKey(key);
  18127. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18128. idx = 0;
  18129. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  18130. /* Import DH Public key as DER */
  18131. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18132. if (ret == 0) {
  18133. /* Export as DER */
  18134. idx = DH_TEST_TMP_SIZE;
  18135. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  18136. }
  18137. /* Verify export matches original */
  18138. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  18139. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18140. }
  18141. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18142. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18143. #endif
  18144. }
  18145. #else
  18146. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  18147. if (ret != 0)
  18148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18149. #endif /* !NO_ASN */
  18150. privSz = DH_TEST_BUF_SIZE;
  18151. pubSz = DH_TEST_BUF_SIZE;
  18152. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  18153. if (ret != 0)
  18154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18155. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  18156. if (ret != 0)
  18157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18158. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  18159. #ifndef WC_NO_RNG
  18160. ret = dh_generate_test(&rng);
  18161. if (ret != 0)
  18162. ERROR_OUT(ret, done);
  18163. ret = dh_fips_generate_test(&rng);
  18164. if (ret != 0)
  18165. ERROR_OUT(ret, done);
  18166. #endif /* !WC_NO_RNG */
  18167. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18168. ret = dh_test_check_pubvalue();
  18169. if (ret != 0)
  18170. ERROR_OUT(ret, done);
  18171. #endif
  18172. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  18173. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  18174. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  18175. * this will be available for testing in the 140-3 module */
  18176. #ifndef WC_NO_RNG
  18177. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  18178. #ifdef HAVE_FFDHE_2048
  18179. #ifdef HAVE_PUBLIC_FFDHE
  18180. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  18181. #else
  18182. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  18183. #endif
  18184. if (ret != 0)
  18185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18186. #endif
  18187. #ifdef HAVE_FFDHE_3072
  18188. #ifdef HAVE_PUBLIC_FFDHE
  18189. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  18190. #else
  18191. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  18192. #endif
  18193. if (ret != 0)
  18194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18195. #endif
  18196. #ifdef HAVE_FFDHE_4096
  18197. #ifdef HAVE_PUBLIC_FFDHE
  18198. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  18199. #else
  18200. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  18201. #endif
  18202. if (ret != 0)
  18203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18204. #endif
  18205. #endif /* !WC_NO_RNG */
  18206. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  18207. wc_FreeDhKey(key);
  18208. keyInit = 0;
  18209. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  18210. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  18211. /* Test Check Key */
  18212. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  18213. NULL, 0, 0, &rng);
  18214. if (ret != 0)
  18215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18216. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  18217. #endif
  18218. done:
  18219. #ifndef WC_NO_RNG
  18220. if (rngInit)
  18221. wc_FreeRng(&rng);
  18222. #endif
  18223. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18224. if (key) {
  18225. if (keyInit)
  18226. wc_FreeDhKey(key);
  18227. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18228. }
  18229. if (key2) {
  18230. if (key2Init)
  18231. wc_FreeDhKey(key2);
  18232. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18233. }
  18234. if (tmp)
  18235. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18236. if (priv)
  18237. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18238. if (pub)
  18239. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18240. if (priv2)
  18241. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18242. if (pub2)
  18243. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18244. if (agree)
  18245. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18246. if (agree2)
  18247. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18248. #else
  18249. if (keyInit)
  18250. wc_FreeDhKey(key);
  18251. if (key2Init)
  18252. wc_FreeDhKey(key2);
  18253. #endif
  18254. (void)privSz;
  18255. (void)pubSz;
  18256. (void)pubSz2;
  18257. (void)privSz2;
  18258. return ret;
  18259. #undef DH_TEST_BUF_SIZE
  18260. #undef DH_TEST_TMP_SIZE
  18261. }
  18262. #endif /* NO_DH */
  18263. #ifndef NO_DSA
  18264. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  18265. {
  18266. wc_test_ret_t ret = 0;
  18267. int answer;
  18268. word32 bytes;
  18269. word32 idx = 0;
  18270. WC_RNG rng;
  18271. int rng_inited = 0;
  18272. wc_Sha sha;
  18273. byte hash[WC_SHA_DIGEST_SIZE];
  18274. byte signature[40];
  18275. int key_inited = 0;
  18276. #ifdef WOLFSSL_KEY_GEN
  18277. byte* der = 0;
  18278. int derIn_inited = 0;
  18279. int genKey_inited = 0;
  18280. #endif
  18281. #define DSA_TEST_TMP_SIZE 1024
  18282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18283. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18284. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18285. #ifdef WOLFSSL_KEY_GEN
  18286. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18287. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18288. #endif
  18289. if ((tmp == NULL) ||
  18290. (key == NULL)
  18291. #ifdef WOLFSSL_KEY_GEN
  18292. || (derIn == NULL)
  18293. || (genKey == NULL)
  18294. #endif
  18295. ) {
  18296. ret = WC_TEST_RET_ENC_NC;
  18297. goto out;
  18298. }
  18299. #else
  18300. byte tmp[1024];
  18301. DsaKey key[1];
  18302. #ifdef WOLFSSL_KEY_GEN
  18303. DsaKey derIn[1];
  18304. DsaKey genKey[1];
  18305. #endif
  18306. #endif
  18307. #ifdef USE_CERT_BUFFERS_1024
  18308. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18309. bytes = sizeof_dsa_key_der_1024;
  18310. #elif defined(USE_CERT_BUFFERS_2048)
  18311. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18312. bytes = sizeof_dsa_key_der_2048;
  18313. #else
  18314. {
  18315. XFILE file = XFOPEN(dsaKey, "rb");
  18316. if (!file)
  18317. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  18318. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  18319. XFCLOSE(file);
  18320. if (bytes == 0)
  18321. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  18322. }
  18323. #endif /* USE_CERT_BUFFERS */
  18324. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  18325. if (ret != 0)
  18326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18327. wc_ShaUpdate(&sha, tmp, bytes);
  18328. wc_ShaFinal(&sha, hash);
  18329. wc_ShaFree(&sha);
  18330. ret = wc_InitDsaKey(key);
  18331. if (ret != 0)
  18332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18333. key_inited = 1;
  18334. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  18335. if (ret != 0)
  18336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18337. #ifndef HAVE_FIPS
  18338. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18339. #else
  18340. ret = wc_InitRng(&rng);
  18341. #endif
  18342. if (ret != 0)
  18343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18344. rng_inited = 1;
  18345. ret = wc_DsaSign(hash, signature, key, &rng);
  18346. if (ret != 0)
  18347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18348. ret = wc_DsaVerify(hash, signature, key, &answer);
  18349. if (ret != 0)
  18350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18351. if (answer != 1)
  18352. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  18353. wc_FreeDsaKey(key);
  18354. key_inited = 0;
  18355. ret = wc_InitDsaKey_h(key, NULL);
  18356. if (ret != 0)
  18357. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18358. key_inited = 1;
  18359. #ifdef WOLFSSL_KEY_GEN
  18360. {
  18361. int derSz = 0;
  18362. ret = wc_InitDsaKey(genKey);
  18363. if (ret != 0)
  18364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18365. genKey_inited = 1;
  18366. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  18367. if (ret != 0)
  18368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18369. ret = wc_MakeDsaKey(&rng, genKey);
  18370. if (ret != 0)
  18371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18372. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18373. if (der == NULL)
  18374. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  18375. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  18376. if (derSz < 0)
  18377. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  18378. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18379. DSA_PRIVATEKEY_TYPE);
  18380. if (ret != 0)
  18381. goto out;
  18382. ret = wc_InitDsaKey(derIn);
  18383. if (ret != 0)
  18384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18385. derIn_inited = 1;
  18386. idx = 0;
  18387. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  18388. if (ret != 0)
  18389. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18390. }
  18391. #endif /* WOLFSSL_KEY_GEN */
  18392. out:
  18393. #ifdef WOLFSSL_KEY_GEN
  18394. if (der)
  18395. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18396. #endif
  18397. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18398. if (tmp)
  18399. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18400. if (key) {
  18401. if (key_inited)
  18402. wc_FreeDsaKey(key);
  18403. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18404. }
  18405. #ifdef WOLFSSL_KEY_GEN
  18406. if (derIn) {
  18407. if (derIn_inited)
  18408. wc_FreeDsaKey(derIn);
  18409. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18410. }
  18411. if (genKey) {
  18412. if (genKey_inited)
  18413. wc_FreeDsaKey(genKey);
  18414. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18415. }
  18416. #endif
  18417. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  18418. if (key_inited)
  18419. wc_FreeDsaKey(key);
  18420. #ifdef WOLFSSL_KEY_GEN
  18421. if (derIn_inited)
  18422. wc_FreeDsaKey(derIn);
  18423. if (genKey_inited)
  18424. wc_FreeDsaKey(genKey);
  18425. #endif
  18426. #endif
  18427. if (rng_inited)
  18428. wc_FreeRng(&rng);
  18429. return ret;
  18430. }
  18431. #endif /* NO_DSA */
  18432. #ifdef WOLFCRYPT_HAVE_SRP
  18433. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  18434. {
  18435. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  18436. WC_RNG rng;
  18437. if(NULL == buf || !size)
  18438. return WC_TEST_RET_ENC_NC;
  18439. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  18440. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  18441. wc_FreeRng(&rng);
  18442. }
  18443. return ret;
  18444. }
  18445. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  18446. {
  18447. wc_test_ret_t r;
  18448. byte clientPubKey[192]; /* A */
  18449. byte serverPubKey[192]; /* B */
  18450. word32 clientPubKeySz = 192;
  18451. word32 serverPubKeySz = 192;
  18452. byte username[] = "user";
  18453. word32 usernameSz = 4;
  18454. byte password[] = "password";
  18455. word32 passwordSz = 8;
  18456. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  18457. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  18458. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  18459. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  18460. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  18461. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  18462. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  18463. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  18464. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  18465. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  18466. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  18467. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  18468. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  18469. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  18470. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  18471. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  18472. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  18473. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  18474. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  18475. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  18476. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  18477. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  18478. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  18479. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  18480. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  18481. };
  18482. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  18483. 0x02
  18484. };
  18485. byte salt[10];
  18486. byte verifier[192];
  18487. word32 v_size = sizeof(verifier);
  18488. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  18489. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  18490. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18491. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18492. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18493. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  18494. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  18495. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  18496. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  18497. if ((cli == NULL) ||
  18498. (srv == NULL) ||
  18499. (clientProof == NULL) ||
  18500. (serverProof == NULL)) {
  18501. r = WC_TEST_RET_ENC_NC;
  18502. goto out;
  18503. }
  18504. #else
  18505. Srp cli[1], srv[1];
  18506. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  18507. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  18508. #endif
  18509. /* set as 0's so if second init on srv not called SrpTerm is not on
  18510. * garbage values */
  18511. XMEMSET(srv, 0, sizeof *srv);
  18512. XMEMSET(cli, 0, sizeof *cli);
  18513. /* generating random salt */
  18514. r = generate_random_salt(salt, sizeof(salt));
  18515. /* client knows username and password. */
  18516. /* server knows N, g, salt and verifier. */
  18517. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  18518. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  18519. /* loading N, g and salt in advance to generate the verifier. */
  18520. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  18521. g, sizeof(g),
  18522. salt, sizeof(salt));
  18523. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  18524. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  18525. /* client sends username to server */
  18526. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  18527. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  18528. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  18529. g, sizeof(g),
  18530. salt, sizeof(salt));
  18531. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  18532. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  18533. /* server sends N, g, salt and B to client */
  18534. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  18535. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  18536. serverPubKey, serverPubKeySz);
  18537. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  18538. /* client sends A and M1 to server */
  18539. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  18540. serverPubKey, serverPubKeySz);
  18541. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  18542. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  18543. /* server sends M2 to client */
  18544. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  18545. wc_SrpTerm(cli);
  18546. wc_SrpTerm(srv);
  18547. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18548. out:
  18549. if (cli)
  18550. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18551. if (srv)
  18552. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18553. if (clientProof)
  18554. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18555. if (serverProof)
  18556. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18557. #endif
  18558. return r;
  18559. }
  18560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  18561. {
  18562. wc_test_ret_t ret;
  18563. #ifndef NO_SHA
  18564. ret = srp_test_digest(SRP_TYPE_SHA);
  18565. if (ret != 0)
  18566. return ret;
  18567. #endif
  18568. #ifndef NO_SHA256
  18569. ret = srp_test_digest(SRP_TYPE_SHA256);
  18570. if (ret != 0)
  18571. return ret;
  18572. #endif
  18573. #ifdef WOLFSSL_SHA384
  18574. ret = srp_test_digest(SRP_TYPE_SHA384);
  18575. if (ret != 0)
  18576. return ret;
  18577. #endif
  18578. #ifdef WOLFSSL_SHA512
  18579. ret = srp_test_digest(SRP_TYPE_SHA512);
  18580. if (ret != 0)
  18581. return ret;
  18582. #endif
  18583. return ret;
  18584. }
  18585. #endif /* WOLFCRYPT_HAVE_SRP */
  18586. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  18587. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  18588. static wc_test_ret_t openssl_aes_test(void)
  18589. {
  18590. #ifdef HAVE_AES_CBC
  18591. #ifdef WOLFSSL_AES_128
  18592. {
  18593. /* EVP_CipherUpdate test */
  18594. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  18595. {
  18596. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18597. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18598. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18599. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18600. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18601. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18602. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18603. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18604. };
  18605. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18606. "0123456789abcdef "; /* align */
  18607. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  18608. "1234567890abcdef "; /* align */
  18609. byte cipher[AES_BLOCK_SIZE * 4];
  18610. byte plain [AES_BLOCK_SIZE * 4];
  18611. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18612. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  18613. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  18614. #else
  18615. EVP_CIPHER_CTX en[1];
  18616. EVP_CIPHER_CTX de[1];
  18617. #endif
  18618. int outlen ;
  18619. int total = 0;
  18620. int i;
  18621. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18622. if ((en == NULL) || (de == NULL))
  18623. return MEMORY_E;
  18624. #endif
  18625. EVP_CIPHER_CTX_init(en);
  18626. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  18627. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  18628. return WC_TEST_RET_ENC_NC;
  18629. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  18630. (byte*)cbcPlain, 9) == 0)
  18631. return WC_TEST_RET_ENC_NC;
  18632. if (outlen != 0)
  18633. return WC_TEST_RET_ENC_NC;
  18634. total += outlen;
  18635. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  18636. (byte*)&cbcPlain[9] , 9) == 0)
  18637. return WC_TEST_RET_ENC_NC;
  18638. if (outlen != 16)
  18639. return WC_TEST_RET_ENC_NC;
  18640. total += outlen;
  18641. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  18642. return WC_TEST_RET_ENC_NC;
  18643. if (outlen != 16)
  18644. return WC_TEST_RET_ENC_NC;
  18645. total += outlen;
  18646. if (total != 32)
  18647. return 3408;
  18648. EVP_CIPHER_CTX_cleanup(en);
  18649. total = 0;
  18650. EVP_CIPHER_CTX_init(de);
  18651. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  18652. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18653. return WC_TEST_RET_ENC_NC;
  18654. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  18655. return WC_TEST_RET_ENC_NC;
  18656. if (outlen != 0)
  18657. return WC_TEST_RET_ENC_NC;
  18658. total += outlen;
  18659. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  18660. (byte*)&cipher[6], 12) == 0)
  18661. return WC_TEST_RET_ENC_NC;
  18662. if (outlen != 0)
  18663. total += outlen;
  18664. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  18665. (byte*)&cipher[6+12], 14) == 0)
  18666. return WC_TEST_RET_ENC_NC;
  18667. if (outlen != 16)
  18668. return WC_TEST_RET_ENC_NC;
  18669. total += outlen;
  18670. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  18671. return WC_TEST_RET_ENC_NC;
  18672. if (outlen != 2)
  18673. return WC_TEST_RET_ENC_NC;
  18674. total += outlen;
  18675. if (total != 18)
  18676. return 3427;
  18677. if (XMEMCMP(plain, cbcPlain, 18))
  18678. return WC_TEST_RET_ENC_NC;
  18679. EVP_CIPHER_CTX_cleanup(de);
  18680. /* test with encrypting/decrypting more than 16 bytes at once */
  18681. total = 0;
  18682. EVP_CIPHER_CTX_init(en);
  18683. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  18684. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  18685. return WC_TEST_RET_ENC_NC;
  18686. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  18687. (byte*)cbcPlain, 17) == 0)
  18688. return WC_TEST_RET_ENC_NC;
  18689. if (outlen != 16)
  18690. return WC_TEST_RET_ENC_NC;
  18691. total += outlen;
  18692. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  18693. (byte*)&cbcPlain[17] , 1) == 0)
  18694. return WC_TEST_RET_ENC_NC;
  18695. if (outlen != 0)
  18696. return WC_TEST_RET_ENC_NC;
  18697. total += outlen;
  18698. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  18699. return WC_TEST_RET_ENC_NC;
  18700. if (outlen != 16)
  18701. return WC_TEST_RET_ENC_NC;
  18702. total += outlen;
  18703. if (total != 32)
  18704. return WC_TEST_RET_ENC_NC;
  18705. EVP_CIPHER_CTX_cleanup(en);
  18706. total = 0;
  18707. EVP_CIPHER_CTX_init(de);
  18708. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  18709. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18710. return WC_TEST_RET_ENC_NC;
  18711. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  18712. return WC_TEST_RET_ENC_NC;
  18713. if (outlen != 16)
  18714. return WC_TEST_RET_ENC_NC;
  18715. total += outlen;
  18716. /* final call on non block size should fail */
  18717. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  18718. return WC_TEST_RET_ENC_NC;
  18719. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  18720. (byte*)&cipher[17], 1) == 0)
  18721. return WC_TEST_RET_ENC_NC;
  18722. if (outlen != 0)
  18723. total += outlen;
  18724. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  18725. (byte*)&cipher[17+1], 14) == 0)
  18726. return WC_TEST_RET_ENC_NC;
  18727. if (outlen != 0)
  18728. return WC_TEST_RET_ENC_NC;
  18729. total += outlen;
  18730. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  18731. return WC_TEST_RET_ENC_NC;
  18732. if (outlen != 2)
  18733. return WC_TEST_RET_ENC_NC;
  18734. total += outlen;
  18735. if (total != 18)
  18736. return WC_TEST_RET_ENC_NC;
  18737. if (XMEMCMP(plain, cbcPlain, 18))
  18738. return WC_TEST_RET_ENC_NC;
  18739. /* test byte by byte decrypt */
  18740. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  18741. plain[i] = i;
  18742. }
  18743. EVP_CIPHER_CTX_cleanup(de);
  18744. total = 0;
  18745. EVP_CIPHER_CTX_init(en);
  18746. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  18747. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  18748. return WC_TEST_RET_ENC_NC;
  18749. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  18750. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  18751. return WC_TEST_RET_ENC_NC;
  18752. if (outlen != AES_BLOCK_SIZE * 3)
  18753. return WC_TEST_RET_ENC_NC;
  18754. total += outlen;
  18755. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  18756. return WC_TEST_RET_ENC_NC;
  18757. if (outlen != AES_BLOCK_SIZE)
  18758. return WC_TEST_RET_ENC_NC;
  18759. total += outlen;
  18760. if (total != sizeof(plain))
  18761. return WC_TEST_RET_ENC_NC;
  18762. EVP_CIPHER_CTX_cleanup(en);
  18763. total = 0;
  18764. EVP_CIPHER_CTX_init(de);
  18765. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  18766. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18767. return WC_TEST_RET_ENC_NC;
  18768. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  18769. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  18770. (byte*)cipher + i, 1) == 0)
  18771. return WC_TEST_RET_ENC_NC;
  18772. if (outlen > 0) {
  18773. int j;
  18774. total += outlen;
  18775. for (j = 0; j < total; j++) {
  18776. if (plain[j] != j) {
  18777. return WC_TEST_RET_ENC_NC;
  18778. }
  18779. }
  18780. }
  18781. }
  18782. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  18783. return WC_TEST_RET_ENC_NC;
  18784. total += outlen;
  18785. if (total != AES_BLOCK_SIZE * 3) {
  18786. return WC_TEST_RET_ENC_NC;
  18787. }
  18788. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  18789. if (plain[i] != i) {
  18790. return WC_TEST_RET_ENC_NC;
  18791. }
  18792. }
  18793. EVP_CIPHER_CTX_cleanup(de);
  18794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18795. wolfSSL_EVP_CIPHER_CTX_free(en);
  18796. wolfSSL_EVP_CIPHER_CTX_free(de);
  18797. #endif
  18798. }
  18799. /* set buffers to be exact size to catch potential over read/write */
  18800. {
  18801. /* EVP_CipherUpdate test */
  18802. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  18803. {
  18804. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18805. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18806. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18807. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18808. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18809. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18810. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18811. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18812. };
  18813. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18814. "0123456789abcdef "; /* align */
  18815. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  18816. "1234567890abcdef "; /* align */
  18817. #define EVP_TEST_BUF_SZ 18
  18818. #define EVP_TEST_BUF_PAD 32
  18819. byte cipher[EVP_TEST_BUF_SZ];
  18820. byte plain [EVP_TEST_BUF_SZ];
  18821. byte padded[EVP_TEST_BUF_PAD];
  18822. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18823. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  18824. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  18825. #else
  18826. EVP_CIPHER_CTX en[1];
  18827. EVP_CIPHER_CTX de[1];
  18828. #endif
  18829. int outlen ;
  18830. int total = 0;
  18831. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18832. if ((en == NULL) || (de == NULL))
  18833. return MEMORY_E;
  18834. #endif
  18835. EVP_CIPHER_CTX_init(en);
  18836. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  18837. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  18838. return WC_TEST_RET_ENC_NC;
  18839. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  18840. return WC_TEST_RET_ENC_NC;
  18841. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  18842. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  18843. return WC_TEST_RET_ENC_NC;
  18844. if (outlen != 16)
  18845. return WC_TEST_RET_ENC_NC;
  18846. total += outlen;
  18847. /* should fail here */
  18848. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  18849. return WC_TEST_RET_ENC_NC;
  18850. EVP_CIPHER_CTX_cleanup(en);
  18851. /* turn padding back on and do successful encrypt */
  18852. total = 0;
  18853. EVP_CIPHER_CTX_init(en);
  18854. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  18855. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  18856. return WC_TEST_RET_ENC_NC;
  18857. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  18858. return WC_TEST_RET_ENC_NC;
  18859. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  18860. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  18861. return WC_TEST_RET_ENC_NC;
  18862. if (outlen != 16)
  18863. return WC_TEST_RET_ENC_NC;
  18864. total += outlen;
  18865. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  18866. return WC_TEST_RET_ENC_NC;
  18867. total += outlen;
  18868. if (total != 32)
  18869. return WC_TEST_RET_ENC_NC;
  18870. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  18871. EVP_CIPHER_CTX_cleanup(en);
  18872. /* test out of bounds read on buffers w/o padding during decryption */
  18873. total = 0;
  18874. EVP_CIPHER_CTX_init(de);
  18875. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  18876. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18877. return WC_TEST_RET_ENC_NC;
  18878. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  18879. return WC_TEST_RET_ENC_NC;
  18880. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  18881. EVP_TEST_BUF_SZ) == 0)
  18882. return WC_TEST_RET_ENC_NC;
  18883. if (outlen != 16)
  18884. return WC_TEST_RET_ENC_NC;
  18885. total += outlen;
  18886. /* should fail since not using padding */
  18887. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  18888. return WC_TEST_RET_ENC_NC;
  18889. EVP_CIPHER_CTX_cleanup(de);
  18890. total = 0;
  18891. EVP_CIPHER_CTX_init(de);
  18892. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  18893. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18894. return WC_TEST_RET_ENC_NC;
  18895. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  18896. return WC_TEST_RET_ENC_NC;
  18897. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  18898. EVP_TEST_BUF_PAD) == 0)
  18899. return WC_TEST_RET_ENC_NC;
  18900. if (outlen != 16)
  18901. return WC_TEST_RET_ENC_NC;
  18902. total += outlen;
  18903. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  18904. return WC_TEST_RET_ENC_NC;
  18905. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  18906. return WC_TEST_RET_ENC_NC;
  18907. EVP_CIPHER_CTX_cleanup(de);
  18908. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18909. wolfSSL_EVP_CIPHER_CTX_free(en);
  18910. wolfSSL_EVP_CIPHER_CTX_free(de);
  18911. #endif
  18912. }
  18913. { /* evp_cipher test: EVP_aes_128_cbc */
  18914. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18915. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18916. #else
  18917. EVP_CIPHER_CTX ctx[1];
  18918. #endif
  18919. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  18920. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  18921. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  18922. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  18923. };
  18924. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18925. {
  18926. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  18927. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  18928. };
  18929. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18930. "0123456789abcdef "; /* align */
  18931. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  18932. "1234567890abcdef "; /* align */
  18933. byte cipher[AES_BLOCK_SIZE * 4];
  18934. byte plain [AES_BLOCK_SIZE * 4];
  18935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18936. if (ctx == NULL)
  18937. return MEMORY_E;
  18938. #endif
  18939. EVP_CIPHER_CTX_init(ctx);
  18940. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  18941. return WC_TEST_RET_ENC_NC;
  18942. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  18943. return WC_TEST_RET_ENC_NC;
  18944. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18945. return WC_TEST_RET_ENC_NC;
  18946. EVP_CIPHER_CTX_cleanup(ctx);
  18947. EVP_CIPHER_CTX_init(ctx);
  18948. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  18949. return WC_TEST_RET_ENC_NC;
  18950. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  18951. return WC_TEST_RET_ENC_NC;
  18952. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18953. return WC_TEST_RET_ENC_NC;
  18954. EVP_CIPHER_CTX_cleanup(ctx);
  18955. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18956. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18957. #endif
  18958. } /* end evp_cipher test: EVP_aes_128_cbc*/
  18959. #endif /* WOLFSSL_AES_128 */
  18960. #endif /* HAVE_AES_CBC */
  18961. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  18962. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  18963. { /* evp_cipher test: EVP_aes_256_ecb*/
  18964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18965. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18966. #else
  18967. EVP_CIPHER_CTX ctx[1];
  18968. #endif
  18969. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18970. {
  18971. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18972. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18973. };
  18974. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18975. {
  18976. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18977. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18978. };
  18979. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18980. {
  18981. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18982. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18983. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18984. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18985. };
  18986. byte cipher[AES_BLOCK_SIZE * 4];
  18987. byte plain [AES_BLOCK_SIZE * 4];
  18988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18989. if (ctx == NULL)
  18990. return MEMORY_E;
  18991. #endif
  18992. EVP_CIPHER_CTX_init(ctx);
  18993. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  18994. return WC_TEST_RET_ENC_NC;
  18995. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  18996. return WC_TEST_RET_ENC_NC;
  18997. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18998. return WC_TEST_RET_ENC_NC;
  18999. EVP_CIPHER_CTX_init(ctx);
  19000. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  19001. return WC_TEST_RET_ENC_NC;
  19002. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  19003. return WC_TEST_RET_ENC_NC;
  19004. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19005. return WC_TEST_RET_ENC_NC;
  19006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19007. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  19008. #endif
  19009. } /* end evp_cipher test */
  19010. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  19011. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  19012. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  19013. {
  19014. /* Test: AES_encrypt/decrypt/set Key */
  19015. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19016. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19017. #ifdef HAVE_AES_DECRYPT
  19018. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19019. #endif
  19020. #else
  19021. AES_KEY enc[1];
  19022. #ifdef HAVE_AES_DECRYPT
  19023. AES_KEY dec[1];
  19024. #endif
  19025. #endif
  19026. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19027. {
  19028. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19029. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19030. };
  19031. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  19032. {
  19033. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  19034. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  19035. };
  19036. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19037. {
  19038. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19039. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19040. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19041. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19042. };
  19043. byte plain[sizeof(msg)];
  19044. byte cipher[sizeof(msg)];
  19045. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19046. if (enc == NULL)
  19047. return MEMORY_E;
  19048. #ifdef HAVE_AES_DECRYPT
  19049. if (dec == NULL)
  19050. return MEMORY_E;
  19051. #endif
  19052. #endif
  19053. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  19054. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  19055. AES_encrypt(msg, cipher, enc);
  19056. #ifdef HAVE_AES_DECRYPT
  19057. AES_decrypt(cipher, plain, dec);
  19058. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19059. return WC_TEST_RET_ENC_NC;
  19060. #endif /* HAVE_AES_DECRYPT */
  19061. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19062. return WC_TEST_RET_ENC_NC;
  19063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19064. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19065. #ifdef HAVE_AES_DECRYPT
  19066. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19067. #endif
  19068. #endif
  19069. }
  19070. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  19071. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  19072. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  19073. #ifdef WOLFSSL_AES_COUNTER
  19074. {
  19075. byte plainBuff [64];
  19076. byte cipherBuff[64];
  19077. #ifdef WOLFSSL_AES_128
  19078. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  19079. {
  19080. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  19081. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  19082. };
  19083. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  19084. {
  19085. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19086. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19087. };
  19088. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  19089. {
  19090. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19091. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19092. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19093. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19094. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19095. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19096. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19097. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19098. };
  19099. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  19100. {
  19101. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  19102. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  19103. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  19104. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  19105. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  19106. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  19107. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  19108. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  19109. };
  19110. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  19111. {
  19112. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  19113. 0xc2
  19114. };
  19115. #endif
  19116. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19117. * NIST Special Publication 800-38A */
  19118. #ifdef WOLFSSL_AES_192
  19119. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  19120. {
  19121. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  19122. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  19123. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  19124. };
  19125. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  19126. {
  19127. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19128. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19129. };
  19130. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  19131. {
  19132. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19133. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19134. };
  19135. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  19136. {
  19137. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  19138. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  19139. };
  19140. #endif /* WOLFSSL_AES_192 */
  19141. #ifdef WOLFSSL_AES_256
  19142. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19143. * NIST Special Publication 800-38A */
  19144. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  19145. {
  19146. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19147. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19148. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19149. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19150. };
  19151. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  19152. {
  19153. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19154. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19155. };
  19156. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  19157. {
  19158. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19159. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19160. };
  19161. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  19162. {
  19163. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  19164. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  19165. };
  19166. #endif /* WOLFSSL_AES_256 */
  19167. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19168. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19169. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19170. #else
  19171. EVP_CIPHER_CTX en[1];
  19172. EVP_CIPHER_CTX de[1];
  19173. #endif
  19174. #ifdef WOLFSSL_AES_128
  19175. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19176. EVP_CIPHER_CTX *p_en;
  19177. EVP_CIPHER_CTX *p_de;
  19178. #endif
  19179. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19180. if ((en == NULL) || (de == NULL))
  19181. return MEMORY_E;
  19182. #endif
  19183. EVP_CIPHER_CTX_init(en);
  19184. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19185. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19186. return WC_TEST_RET_ENC_NC;
  19187. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  19188. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19189. return WC_TEST_RET_ENC_NC;
  19190. EVP_CIPHER_CTX_init(de);
  19191. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19192. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19193. return WC_TEST_RET_ENC_NC;
  19194. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19195. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19196. return WC_TEST_RET_ENC_NC;
  19197. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19198. return WC_TEST_RET_ENC_NC;
  19199. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19200. return WC_TEST_RET_ENC_NC;
  19201. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19202. return WC_TEST_RET_ENC_NC;
  19203. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19204. return WC_TEST_RET_ENC_NC;
  19205. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19206. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  19207. if (p_en == NULL)
  19208. return WC_TEST_RET_ENC_ERRNO;
  19209. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  19210. if (p_de == NULL)
  19211. return WC_TEST_RET_ENC_ERRNO;
  19212. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  19213. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19214. return WC_TEST_RET_ENC_NC;
  19215. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  19216. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19217. return WC_TEST_RET_ENC_NC;
  19218. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  19219. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19220. return WC_TEST_RET_ENC_NC;
  19221. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  19222. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19223. return WC_TEST_RET_ENC_NC;
  19224. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19225. return WC_TEST_RET_ENC_NC;
  19226. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19227. return WC_TEST_RET_ENC_NC;
  19228. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  19229. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  19230. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19231. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19232. return WC_TEST_RET_ENC_NC;
  19233. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19234. return WC_TEST_RET_ENC_NC;
  19235. EVP_CIPHER_CTX_init(en);
  19236. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19237. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19238. return WC_TEST_RET_ENC_NC;
  19239. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19240. return WC_TEST_RET_ENC_NC;
  19241. EVP_CIPHER_CTX_init(de);
  19242. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19243. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19244. return WC_TEST_RET_ENC_NC;
  19245. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19246. return WC_TEST_RET_ENC_NC;
  19247. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19248. return WC_TEST_RET_ENC_NC;
  19249. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  19250. return WC_TEST_RET_ENC_NC;
  19251. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19252. return WC_TEST_RET_ENC_NC;
  19253. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19254. return WC_TEST_RET_ENC_NC;
  19255. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19256. return WC_TEST_RET_ENC_NC;
  19257. if (XMEMCMP(cipherBuff, oddCipher, 9))
  19258. return WC_TEST_RET_ENC_NC;
  19259. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19260. return WC_TEST_RET_ENC_NC;
  19261. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19262. return WC_TEST_RET_ENC_NC;
  19263. #endif /* WOLFSSL_AES_128 */
  19264. #ifdef WOLFSSL_AES_192
  19265. EVP_CIPHER_CTX_init(en);
  19266. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  19267. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19268. return WC_TEST_RET_ENC_NC;
  19269. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  19270. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19271. return WC_TEST_RET_ENC_NC;
  19272. EVP_CIPHER_CTX_init(de);
  19273. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  19274. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19275. return WC_TEST_RET_ENC_NC;
  19276. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19277. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19278. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19279. return WC_TEST_RET_ENC_NC;
  19280. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  19281. return WC_TEST_RET_ENC_NC;
  19282. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  19283. return WC_TEST_RET_ENC_NC;
  19284. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19285. return WC_TEST_RET_ENC_NC;
  19286. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19287. return WC_TEST_RET_ENC_NC;
  19288. #endif /* WOLFSSL_AES_192 */
  19289. #ifdef WOLFSSL_AES_256
  19290. EVP_CIPHER_CTX_init(en);
  19291. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  19292. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19293. return WC_TEST_RET_ENC_NC;
  19294. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  19295. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19296. return WC_TEST_RET_ENC_NC;
  19297. EVP_CIPHER_CTX_init(de);
  19298. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  19299. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19300. return WC_TEST_RET_ENC_NC;
  19301. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19302. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19303. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19304. return WC_TEST_RET_ENC_NC;
  19305. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  19306. return WC_TEST_RET_ENC_NC;
  19307. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  19308. return WC_TEST_RET_ENC_NC;
  19309. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19310. return WC_TEST_RET_ENC_NC;
  19311. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19312. return WC_TEST_RET_ENC_NC;
  19313. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19314. wolfSSL_EVP_CIPHER_CTX_free(en);
  19315. wolfSSL_EVP_CIPHER_CTX_free(de);
  19316. #endif
  19317. #endif /* WOLFSSL_AES_256 */
  19318. }
  19319. #endif /* HAVE_AES_COUNTER */
  19320. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  19321. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  19322. {
  19323. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19324. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19325. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19326. #else
  19327. AES_KEY enc[1];
  19328. AES_KEY dec[1];
  19329. #endif
  19330. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  19331. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  19332. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  19333. };
  19334. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19335. {
  19336. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  19337. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  19338. };
  19339. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  19340. {
  19341. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  19342. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  19343. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  19344. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  19345. };
  19346. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19347. {
  19348. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19349. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19350. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19351. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  19352. };
  19353. byte cipher[AES_BLOCK_SIZE * 2];
  19354. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  19355. int num = 0;
  19356. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19357. if ((enc == NULL) || (dec == NULL))
  19358. return MEMORY_E;
  19359. #endif
  19360. XMEMCPY(iv, setIv, sizeof(setIv));
  19361. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  19362. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  19363. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  19364. &num, AES_ENCRYPT);
  19365. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  19366. return WC_TEST_RET_ENC_NC;
  19367. if (num != 15) /* should have used 15 of the 16 bytes */
  19368. return WC_TEST_RET_ENC_NC;
  19369. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  19370. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  19371. &num, AES_ENCRYPT);
  19372. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  19373. return WC_TEST_RET_ENC_NC;
  19374. if (num != 0)
  19375. return WC_TEST_RET_ENC_NC;
  19376. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19377. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19378. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19379. #endif
  19380. }
  19381. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  19382. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  19383. return 0;
  19384. }
  19385. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  19386. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  19387. {
  19388. wc_test_ret_t ret;
  19389. EVP_MD_CTX md_ctx;
  19390. testVector a, b, c, d, e, f;
  19391. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  19392. a.inLen = 0;
  19393. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  19394. (void)a;
  19395. (void)b;
  19396. (void)c;
  19397. (void)d;
  19398. (void)e;
  19399. (void)f;
  19400. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  19401. {
  19402. byte* p;
  19403. p = (byte*)CRYPTO_malloc(10, "", 0);
  19404. if (p == NULL) {
  19405. return WC_TEST_RET_ENC_NC;
  19406. }
  19407. XMEMSET(p, 0, 10);
  19408. CRYPTO_free(p, "", 0);
  19409. }
  19410. #ifndef NO_MD5
  19411. a.input = "1234567890123456789012345678901234567890123456789012345678"
  19412. "9012345678901234567890";
  19413. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  19414. "\x7a";
  19415. a.inLen = XSTRLEN(a.input);
  19416. a.outLen = WC_MD5_DIGEST_SIZE;
  19417. EVP_MD_CTX_init(&md_ctx);
  19418. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  19419. if (ret == WOLFSSL_SUCCESS) {
  19420. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  19421. }
  19422. if (ret == WOLFSSL_SUCCESS) {
  19423. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19424. }
  19425. EVP_MD_CTX_cleanup(&md_ctx);
  19426. if (ret != WOLFSSL_SUCCESS)
  19427. return WC_TEST_RET_ENC_NC;
  19428. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  19429. return WC_TEST_RET_ENC_NC;
  19430. #endif /* NO_MD5 */
  19431. #ifndef NO_SHA
  19432. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  19433. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  19434. "aaaaaaaaaa";
  19435. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  19436. "\x53\x99\x5E\x26\xA0";
  19437. b.inLen = XSTRLEN(b.input);
  19438. b.outLen = WC_SHA_DIGEST_SIZE;
  19439. EVP_MD_CTX_init(&md_ctx);
  19440. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  19441. if (ret == WOLFSSL_SUCCESS) {
  19442. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  19443. if (ret == WOLFSSL_SUCCESS)
  19444. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19445. }
  19446. EVP_MD_CTX_cleanup(&md_ctx);
  19447. if (ret != WOLFSSL_SUCCESS)
  19448. return WC_TEST_RET_ENC_NC;
  19449. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  19450. return WC_TEST_RET_ENC_NC;
  19451. #endif /* NO_SHA */
  19452. #ifdef WOLFSSL_SHA224
  19453. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19454. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19455. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  19456. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  19457. e.inLen = XSTRLEN(e.input);
  19458. e.outLen = WC_SHA224_DIGEST_SIZE;
  19459. EVP_MD_CTX_init(&md_ctx);
  19460. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  19461. if (ret == WOLFSSL_SUCCESS) {
  19462. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  19463. if (ret == WOLFSSL_SUCCESS)
  19464. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19465. }
  19466. EVP_MD_CTX_cleanup(&md_ctx);
  19467. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  19468. return WC_TEST_RET_ENC_NC;
  19469. }
  19470. #endif /* WOLFSSL_SHA224 */
  19471. #ifndef NO_SHA256
  19472. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  19473. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  19474. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  19475. "\x06\xC1";
  19476. d.inLen = XSTRLEN(d.input);
  19477. d.outLen = WC_SHA256_DIGEST_SIZE;
  19478. EVP_MD_CTX_init(&md_ctx);
  19479. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  19480. if (ret == WOLFSSL_SUCCESS) {
  19481. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  19482. if (ret == WOLFSSL_SUCCESS)
  19483. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19484. }
  19485. EVP_MD_CTX_cleanup(&md_ctx);
  19486. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  19487. return WC_TEST_RET_ENC_NC;
  19488. }
  19489. #endif /* !NO_SHA256 */
  19490. #ifdef WOLFSSL_SHA384
  19491. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19492. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19493. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  19494. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  19495. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  19496. "\x74\x60\x39";
  19497. e.inLen = XSTRLEN(e.input);
  19498. e.outLen = WC_SHA384_DIGEST_SIZE;
  19499. EVP_MD_CTX_init(&md_ctx);
  19500. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  19501. if (ret == WOLFSSL_SUCCESS) {
  19502. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  19503. if (ret == WOLFSSL_SUCCESS)
  19504. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19505. }
  19506. EVP_MD_CTX_cleanup(&md_ctx);
  19507. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  19508. return WC_TEST_RET_ENC_NC;
  19509. }
  19510. #endif /* WOLFSSL_SHA384 */
  19511. #ifdef WOLFSSL_SHA512
  19512. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19513. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19514. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  19515. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  19516. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  19517. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  19518. "\x87\x4b\xe9\x09";
  19519. f.inLen = XSTRLEN(f.input);
  19520. f.outLen = WC_SHA512_DIGEST_SIZE;
  19521. EVP_MD_CTX_init(&md_ctx);
  19522. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  19523. if (ret == WOLFSSL_SUCCESS) {
  19524. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  19525. if (ret == WOLFSSL_SUCCESS)
  19526. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19527. }
  19528. EVP_MD_CTX_cleanup(&md_ctx);
  19529. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  19530. return WC_TEST_RET_ENC_NC;
  19531. }
  19532. #if !defined(WOLFSSL_NOSHA512_224) && \
  19533. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  19534. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19535. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19536. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  19537. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  19538. f.inLen = XSTRLEN(f.input);
  19539. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  19540. EVP_MD_CTX_init(&md_ctx);
  19541. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  19542. if (ret == WOLFSSL_SUCCESS) {
  19543. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  19544. if (ret == WOLFSSL_SUCCESS)
  19545. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19546. }
  19547. EVP_MD_CTX_cleanup(&md_ctx);
  19548. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  19549. return WC_TEST_RET_ENC_NC;
  19550. }
  19551. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  19552. #if !defined(WOLFSSL_NOSHA512_256) && \
  19553. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  19554. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19555. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19556. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  19557. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  19558. "\x56\x3a";
  19559. f.inLen = XSTRLEN(f.input);
  19560. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  19561. EVP_MD_CTX_init(&md_ctx);
  19562. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  19563. if (ret == WOLFSSL_SUCCESS) {
  19564. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  19565. if (ret == WOLFSSL_SUCCESS)
  19566. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19567. }
  19568. EVP_MD_CTX_cleanup(&md_ctx);
  19569. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  19570. return WC_TEST_RET_ENC_NC;
  19571. }
  19572. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  19573. #endif /* WOLFSSL_SHA512 */
  19574. #ifdef WOLFSSL_SHA3
  19575. #ifndef WOLFSSL_NOSHA3_224
  19576. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19577. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19578. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  19579. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  19580. e.inLen = XSTRLEN(e.input);
  19581. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  19582. EVP_MD_CTX_init(&md_ctx);
  19583. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  19584. if (ret == WOLFSSL_SUCCESS) {
  19585. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  19586. if (ret == WOLFSSL_SUCCESS)
  19587. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19588. }
  19589. EVP_MD_CTX_cleanup(&md_ctx);
  19590. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  19591. return WC_TEST_RET_ENC_NC;
  19592. }
  19593. #endif /* WOLFSSL_NOSHA3_224 */
  19594. #ifndef WOLFSSL_NOSHA3_256
  19595. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19596. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19597. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  19598. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  19599. "\x1d\x18";
  19600. d.inLen = XSTRLEN(d.input);
  19601. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  19602. EVP_MD_CTX_init(&md_ctx);
  19603. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  19604. if (ret == WOLFSSL_SUCCESS) {
  19605. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  19606. if (ret == WOLFSSL_SUCCESS)
  19607. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19608. }
  19609. EVP_MD_CTX_cleanup(&md_ctx);
  19610. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  19611. return WC_TEST_RET_ENC_NC;
  19612. }
  19613. #endif /* WOLFSSL_NOSHA3_256 */
  19614. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19615. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19616. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  19617. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  19618. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  19619. "\xaa\x7f\xc7";
  19620. e.inLen = XSTRLEN(e.input);
  19621. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  19622. EVP_MD_CTX_init(&md_ctx);
  19623. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  19624. if (ret == WOLFSSL_SUCCESS) {
  19625. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  19626. if (ret == WOLFSSL_SUCCESS)
  19627. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19628. }
  19629. EVP_MD_CTX_cleanup(&md_ctx);
  19630. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  19631. return WC_TEST_RET_ENC_NC;
  19632. }
  19633. #ifndef WOLFSSL_NOSHA3_512
  19634. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19635. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19636. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  19637. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  19638. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  19639. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  19640. "\xf2\x1d\xd1\x85";
  19641. f.inLen = XSTRLEN(f.input);
  19642. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  19643. EVP_MD_CTX_init(&md_ctx);
  19644. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  19645. if (ret == WOLFSSL_SUCCESS) {
  19646. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  19647. if (ret == WOLFSSL_SUCCESS)
  19648. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19649. }
  19650. EVP_MD_CTX_cleanup(&md_ctx);
  19651. if (ret != WOLFSSL_SUCCESS ||
  19652. XMEMCMP(hash, f.output, f.outLen) != 0) {
  19653. return WC_TEST_RET_ENC_NC;
  19654. }
  19655. #endif /* WOLFSSL_NOSHA3_512 */
  19656. #endif /* WOLFSSL_SHA3 */
  19657. #ifndef WC_NO_RNG
  19658. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  19659. return WC_TEST_RET_ENC_NC;
  19660. #endif
  19661. #ifndef NO_MD5
  19662. c.input = "what do ya want for nothing?";
  19663. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  19664. "\x76";
  19665. c.inLen = XSTRLEN(c.input);
  19666. c.outLen = WC_MD5_DIGEST_SIZE;
  19667. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  19668. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  19669. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  19670. hash, 0) != NULL)
  19671. #else
  19672. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  19673. hash, 0) == NULL ||
  19674. XMEMCMP(hash, c.output, c.outLen) != 0)
  19675. #endif
  19676. {
  19677. return WC_TEST_RET_ENC_NC;
  19678. }
  19679. #endif /* NO_MD5 */
  19680. #ifndef NO_DES3
  19681. { /* des test */
  19682. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  19683. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  19684. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  19685. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  19686. };
  19687. byte plain[24];
  19688. byte cipher[24];
  19689. const_DES_cblock key = {
  19690. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  19691. };
  19692. DES_cblock iv = {
  19693. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  19694. };
  19695. DES_key_schedule sched;
  19696. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19697. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  19698. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  19699. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  19700. };
  19701. DES_key_sched(&key, &sched);
  19702. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  19703. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  19704. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  19705. return WC_TEST_RET_ENC_NC;
  19706. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  19707. return WC_TEST_RET_ENC_NC;
  19708. /* test changing iv */
  19709. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  19710. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  19711. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  19712. return WC_TEST_RET_ENC_NC;
  19713. } /* end des test */
  19714. #endif /* NO_DES3 */
  19715. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  19716. if ((ret = openssl_aes_test()) != 0) {
  19717. return ret;
  19718. }
  19719. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  19720. { /* evp_cipher test: EVP_aes_128_cbc */
  19721. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19722. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  19723. #else
  19724. EVP_CIPHER_CTX ctx[1];
  19725. #endif
  19726. int idx, cipherSz, plainSz;
  19727. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  19728. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  19729. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  19730. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  19731. };
  19732. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19733. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  19734. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  19735. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  19736. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  19737. };
  19738. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19739. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  19740. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  19741. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  19742. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  19743. };
  19744. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19745. "0123456789abcdef "; /* align */
  19746. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  19747. "1234567890abcdef "; /* align */
  19748. byte cipher[AES_BLOCK_SIZE * 4];
  19749. byte plain [AES_BLOCK_SIZE * 4];
  19750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19751. if (ctx == NULL)
  19752. return MEMORY_E;
  19753. #endif
  19754. cipherSz = 0;
  19755. EVP_CIPHER_CTX_init(ctx);
  19756. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  19757. if (ret == WOLFSSL_SUCCESS) {
  19758. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  19759. if (ret == WOLFSSL_SUCCESS)
  19760. cipherSz += idx;
  19761. }
  19762. if (ret == WOLFSSL_SUCCESS) {
  19763. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  19764. if (ret == WOLFSSL_SUCCESS)
  19765. cipherSz += idx;
  19766. }
  19767. EVP_CIPHER_CTX_cleanup(ctx);
  19768. if (ret != WOLFSSL_SUCCESS)
  19769. return WC_TEST_RET_ENC_NC;
  19770. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  19771. return WC_TEST_RET_ENC_NC;
  19772. /* check partial decrypt (not enough padding for full block) */
  19773. plainSz = 0;
  19774. EVP_CIPHER_CTX_init(ctx);
  19775. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  19776. if (ret == WOLFSSL_SUCCESS) {
  19777. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  19778. if (ret == WOLFSSL_SUCCESS)
  19779. plainSz += idx;
  19780. }
  19781. if (ret == WOLFSSL_SUCCESS) {
  19782. /* this test should fail... not enough padding for full block */
  19783. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  19784. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  19785. ret = WOLFSSL_SUCCESS;
  19786. else
  19787. ret = WC_TEST_RET_ENC_NC;
  19788. }
  19789. else
  19790. ret = WC_TEST_RET_ENC_NC;
  19791. EVP_CIPHER_CTX_cleanup(ctx);
  19792. if (ret != WOLFSSL_SUCCESS)
  19793. return ret;
  19794. plainSz = 0;
  19795. EVP_CIPHER_CTX_init(ctx);
  19796. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  19797. if (ret == WOLFSSL_SUCCESS) {
  19798. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  19799. if (ret == WOLFSSL_SUCCESS)
  19800. plainSz += idx;
  19801. }
  19802. if (ret == WOLFSSL_SUCCESS) {
  19803. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  19804. if (ret == WOLFSSL_SUCCESS)
  19805. plainSz += idx;
  19806. }
  19807. EVP_CIPHER_CTX_cleanup(ctx);
  19808. if (ret != WOLFSSL_SUCCESS)
  19809. return WC_TEST_RET_ENC_NC;
  19810. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  19811. return WC_TEST_RET_ENC_NC;
  19812. cipherSz = 0;
  19813. EVP_CIPHER_CTX_init(ctx);
  19814. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  19815. if (ret == WOLFSSL_SUCCESS) {
  19816. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  19817. if (ret == WOLFSSL_SUCCESS)
  19818. cipherSz += idx;
  19819. }
  19820. if (ret == WOLFSSL_SUCCESS) {
  19821. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  19822. if (ret == WOLFSSL_SUCCESS)
  19823. cipherSz += idx;
  19824. }
  19825. EVP_CIPHER_CTX_cleanup(ctx);
  19826. if (ret != WOLFSSL_SUCCESS)
  19827. return WC_TEST_RET_ENC_NC;
  19828. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  19829. return WC_TEST_RET_ENC_NC;
  19830. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19831. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  19832. #endif
  19833. } /* end evp_cipher test: EVP_aes_128_cbc*/
  19834. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  19835. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  19836. { /* evp_cipher test: EVP_aes_256_ecb*/
  19837. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19838. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  19839. #else
  19840. EVP_CIPHER_CTX ctx[1];
  19841. #endif
  19842. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  19843. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19844. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19845. };
  19846. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19847. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  19848. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  19849. };
  19850. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  19851. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19852. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19853. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19854. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19855. };
  19856. byte cipher[AES_BLOCK_SIZE * 4];
  19857. byte plain [AES_BLOCK_SIZE * 4];
  19858. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19859. if (ctx == NULL)
  19860. return MEMORY_E;
  19861. #endif
  19862. EVP_CIPHER_CTX_init(ctx);
  19863. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  19864. if (ret == WOLFSSL_SUCCESS)
  19865. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  19866. EVP_CIPHER_CTX_cleanup(ctx);
  19867. if (ret != 16)
  19868. return WC_TEST_RET_ENC_NC;
  19869. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19870. return WC_TEST_RET_ENC_NC;
  19871. EVP_CIPHER_CTX_init(ctx);
  19872. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  19873. if (ret == WOLFSSL_SUCCESS)
  19874. ret = EVP_Cipher(ctx, plain, cipher, 16);
  19875. EVP_CIPHER_CTX_cleanup(ctx);
  19876. if (ret != 16)
  19877. return WC_TEST_RET_ENC_NC;
  19878. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19879. return WC_TEST_RET_ENC_NC;
  19880. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19881. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  19882. #endif
  19883. } /* end evp_cipher test */
  19884. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  19885. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  19886. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  19887. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  19888. {
  19889. /* Test: AES_encrypt/decrypt/set Key */
  19890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19891. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19892. #ifdef HAVE_AES_DECRYPT
  19893. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19894. #endif
  19895. #else
  19896. AES_KEY enc[1];
  19897. #ifdef HAVE_AES_DECRYPT
  19898. AES_KEY dec[1];
  19899. #endif
  19900. #endif
  19901. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19902. {
  19903. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19904. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19905. };
  19906. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  19907. {
  19908. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  19909. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  19910. };
  19911. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19912. {
  19913. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19914. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19915. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19916. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19917. };
  19918. byte plain[sizeof(msg)];
  19919. byte cipher[sizeof(msg)];
  19920. printf("openSSL extra test\n") ;
  19921. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19922. if (enc == NULL)
  19923. return MEMORY_E;
  19924. #ifdef HAVE_AES_DECRYPT
  19925. if (dec == NULL)
  19926. return MEMORY_E;
  19927. #endif
  19928. #endif
  19929. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  19930. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  19931. AES_encrypt(msg, cipher, enc);
  19932. #ifdef HAVE_AES_DECRYPT
  19933. AES_decrypt(cipher, plain, dec);
  19934. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19935. return WC_TEST_RET_ENC_NC;
  19936. #endif /* HAVE_AES_DECRYPT */
  19937. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19938. return WC_TEST_RET_ENC_NC;
  19939. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19940. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19941. #ifdef HAVE_AES_DECRYPT
  19942. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19943. #endif
  19944. #endif
  19945. }
  19946. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  19947. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  19948. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  19949. #ifdef WOLFSSL_AES_COUNTER
  19950. {
  19951. byte plainBuff [64];
  19952. byte cipherBuff[64];
  19953. #ifdef WOLFSSL_AES_128
  19954. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  19955. {
  19956. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  19957. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  19958. };
  19959. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  19960. {
  19961. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19962. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19963. };
  19964. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  19965. {
  19966. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19967. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19968. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19969. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19970. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19971. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19972. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19973. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19974. };
  19975. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  19976. {
  19977. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  19978. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  19979. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  19980. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  19981. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  19982. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  19983. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  19984. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  19985. };
  19986. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  19987. {
  19988. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  19989. 0xc2
  19990. };
  19991. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19992. EVP_CIPHER_CTX *p_en;
  19993. EVP_CIPHER_CTX *p_de;
  19994. #endif
  19995. #endif /* WOLFSSL_AES_128 */
  19996. #ifdef WOLFSSL_AES_192
  19997. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19998. * NIST Special Publication 800-38A */
  19999. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  20000. {
  20001. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  20002. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  20003. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  20004. };
  20005. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  20006. {
  20007. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  20008. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  20009. };
  20010. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  20011. {
  20012. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20013. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20014. };
  20015. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  20016. {
  20017. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  20018. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  20019. };
  20020. #endif /* WOLFSSL_AES_192 */
  20021. #ifdef WOLFSSL_AES_256
  20022. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  20023. * NIST Special Publication 800-38A */
  20024. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  20025. {
  20026. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  20027. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  20028. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  20029. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  20030. };
  20031. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  20032. {
  20033. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  20034. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  20035. };
  20036. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  20037. {
  20038. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20039. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20040. };
  20041. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  20042. {
  20043. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  20044. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  20045. };
  20046. #endif /* WOLFSSL_AES_256 */
  20047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20048. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20049. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20050. if ((en == NULL) || (de == NULL))
  20051. return MEMORY_E;
  20052. #else
  20053. EVP_CIPHER_CTX en[1];
  20054. EVP_CIPHER_CTX de[1];
  20055. #endif
  20056. #ifdef WOLFSSL_AES_128
  20057. EVP_CIPHER_CTX_init(en);
  20058. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  20059. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20060. return WC_TEST_RET_ENC_NC;
  20061. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  20062. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20063. return WC_TEST_RET_ENC_NC;
  20064. EVP_CIPHER_CTX_init(de);
  20065. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  20066. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20067. return WC_TEST_RET_ENC_NC;
  20068. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20069. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20070. return WC_TEST_RET_ENC_NC;
  20071. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  20072. return WC_TEST_RET_ENC_NC;
  20073. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  20074. return WC_TEST_RET_ENC_NC;
  20075. EVP_CIPHER_CTX_cleanup(en);
  20076. EVP_CIPHER_CTX_cleanup(de);
  20077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20078. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  20079. if (p_en == NULL)
  20080. return WC_TEST_RET_ENC_ERRNO;
  20081. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  20082. if (p_de == NULL)
  20083. return WC_TEST_RET_ENC_ERRNO;
  20084. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  20085. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20086. return WC_TEST_RET_ENC_NC;
  20087. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  20088. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20089. return WC_TEST_RET_ENC_NC;
  20090. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  20091. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20092. return WC_TEST_RET_ENC_NC;
  20093. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  20094. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20095. return WC_TEST_RET_ENC_NC;
  20096. EVP_CIPHER_CTX_cleanup(p_en);
  20097. EVP_CIPHER_CTX_cleanup(p_de);
  20098. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  20099. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  20100. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  20101. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  20102. return WC_TEST_RET_ENC_NC;
  20103. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  20104. return WC_TEST_RET_ENC_NC;
  20105. EVP_CIPHER_CTX_init(en);
  20106. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  20107. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20108. return WC_TEST_RET_ENC_NC;
  20109. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  20110. return WC_TEST_RET_ENC_NC;
  20111. EVP_CIPHER_CTX_init(de);
  20112. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  20113. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20114. return WC_TEST_RET_ENC_NC;
  20115. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  20116. return WC_TEST_RET_ENC_NC;
  20117. if (XMEMCMP(plainBuff, ctrPlain, 9))
  20118. return WC_TEST_RET_ENC_NC;
  20119. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  20120. return WC_TEST_RET_ENC_NC;
  20121. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  20122. return WC_TEST_RET_ENC_NC;
  20123. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  20124. return WC_TEST_RET_ENC_NC;
  20125. if (XMEMCMP(plainBuff, ctrPlain, 9))
  20126. return WC_TEST_RET_ENC_NC;
  20127. if (XMEMCMP(cipherBuff, oddCipher, 9))
  20128. return WC_TEST_RET_ENC_NC;
  20129. EVP_CIPHER_CTX_cleanup(en);
  20130. EVP_CIPHER_CTX_cleanup(de);
  20131. #endif /* WOLFSSL_AES_128 */
  20132. #ifdef WOLFSSL_AES_192
  20133. EVP_CIPHER_CTX_init(en);
  20134. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  20135. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  20136. return WC_TEST_RET_ENC_NC;
  20137. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  20138. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20139. return WC_TEST_RET_ENC_NC;
  20140. EVP_CIPHER_CTX_init(de);
  20141. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  20142. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  20143. return WC_TEST_RET_ENC_NC;
  20144. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  20145. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20146. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20147. return WC_TEST_RET_ENC_NC;
  20148. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  20149. return WC_TEST_RET_ENC_NC;
  20150. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  20151. return WC_TEST_RET_ENC_NC;
  20152. EVP_CIPHER_CTX_cleanup(en);
  20153. EVP_CIPHER_CTX_cleanup(de);
  20154. #endif /* WOLFSSL_AES_192 */
  20155. #ifdef WOLFSSL_AES_256
  20156. EVP_CIPHER_CTX_init(en);
  20157. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  20158. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  20159. return WC_TEST_RET_ENC_NC;
  20160. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  20161. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20162. return WC_TEST_RET_ENC_NC;
  20163. EVP_CIPHER_CTX_init(de);
  20164. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  20165. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  20166. return WC_TEST_RET_ENC_NC;
  20167. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  20168. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20169. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20170. return WC_TEST_RET_ENC_NC;
  20171. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  20172. return WC_TEST_RET_ENC_NC;
  20173. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  20174. return WC_TEST_RET_ENC_NC;
  20175. EVP_CIPHER_CTX_cleanup(en);
  20176. EVP_CIPHER_CTX_cleanup(de);
  20177. #endif /* WOLFSSL_AES_256 */
  20178. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20179. wolfSSL_EVP_CIPHER_CTX_free(en);
  20180. wolfSSL_EVP_CIPHER_CTX_free(de);
  20181. #endif
  20182. }
  20183. #endif /* HAVE_AES_COUNTER */
  20184. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  20185. {
  20186. /* EVP_CipherUpdate test */
  20187. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20188. {
  20189. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20190. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20191. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20192. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20193. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20194. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20195. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20196. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20197. };
  20198. byte key[] = "0123456789abcdef "; /* align */
  20199. byte iv[] = "1234567890abcdef "; /* align */
  20200. byte cipher[AES_BLOCK_SIZE * 4];
  20201. byte plain [AES_BLOCK_SIZE * 4];
  20202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20203. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20204. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20205. #else
  20206. EVP_CIPHER_CTX en[1];
  20207. EVP_CIPHER_CTX de[1];
  20208. #endif
  20209. int outlen ;
  20210. int total = 0;
  20211. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20212. if ((en == NULL) || (de == NULL))
  20213. return MEMORY_E;
  20214. #endif
  20215. EVP_CIPHER_CTX_init(en);
  20216. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20217. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20218. return WC_TEST_RET_ENC_NC;
  20219. /* openSSL compatibility, if(inlen == 0)return 1; */
  20220. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20221. (byte*)cbcPlain, 0) != 1)
  20222. return WC_TEST_RET_ENC_NC;
  20223. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20224. return WC_TEST_RET_ENC_NC;
  20225. EVP_CIPHER_CTX_init(en);
  20226. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20227. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20228. return WC_TEST_RET_ENC_NC;
  20229. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20230. (byte*)cbcPlain, 9) == 0)
  20231. return WC_TEST_RET_ENC_NC;
  20232. if(outlen != 0)
  20233. return WC_TEST_RET_ENC_NC;
  20234. total += outlen;
  20235. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20236. (byte*)&cbcPlain[9] , 9) == 0)
  20237. return WC_TEST_RET_ENC_NC;
  20238. if(outlen != 16)
  20239. return WC_TEST_RET_ENC_NC;
  20240. total += outlen;
  20241. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20242. return WC_TEST_RET_ENC_NC;
  20243. if(outlen != 16)
  20244. return WC_TEST_RET_ENC_NC;
  20245. total += outlen;
  20246. if(total != 32)
  20247. return WC_TEST_RET_ENC_NC;
  20248. total = 0;
  20249. EVP_CIPHER_CTX_init(de);
  20250. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20251. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20252. return WC_TEST_RET_ENC_NC;
  20253. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20254. return WC_TEST_RET_ENC_NC;
  20255. if(outlen != 0)
  20256. return WC_TEST_RET_ENC_NC;
  20257. total += outlen;
  20258. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20259. (byte*)&cipher[6], 12) == 0)
  20260. return WC_TEST_RET_ENC_NC;
  20261. if(outlen != 0)
  20262. total += outlen;
  20263. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20264. (byte*)&cipher[6+12], 14) == 0)
  20265. return WC_TEST_RET_ENC_NC;
  20266. if(outlen != 16)
  20267. return WC_TEST_RET_ENC_NC;
  20268. total += outlen;
  20269. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20270. return WC_TEST_RET_ENC_NC;
  20271. if(outlen != 2)
  20272. return WC_TEST_RET_ENC_NC;
  20273. total += outlen;
  20274. if(total != 18)
  20275. return WC_TEST_RET_ENC_NC;
  20276. if (XMEMCMP(plain, cbcPlain, 18))
  20277. return WC_TEST_RET_ENC_NC;
  20278. total = 0;
  20279. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20280. return WC_TEST_RET_ENC_NC;
  20281. EVP_CIPHER_CTX_init(en);
  20282. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  20283. (unsigned char*)key, (unsigned char*)iv) == 0)
  20284. return WC_TEST_RET_ENC_NC;
  20285. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  20286. return WC_TEST_RET_ENC_NC;
  20287. if(outlen != 0)
  20288. return WC_TEST_RET_ENC_NC;
  20289. total += outlen;
  20290. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  20291. return WC_TEST_RET_ENC_NC;
  20292. if(outlen != 16)
  20293. return WC_TEST_RET_ENC_NC;
  20294. total += outlen;
  20295. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20296. return WC_TEST_RET_ENC_NC;
  20297. if(outlen != 16)
  20298. return WC_TEST_RET_ENC_NC;
  20299. total += outlen;
  20300. if(total != 32)
  20301. return 3438;
  20302. total = 0;
  20303. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20304. return WC_TEST_RET_ENC_NC;
  20305. EVP_CIPHER_CTX_init(de);
  20306. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  20307. (unsigned char*)key, (unsigned char*)iv) == 0)
  20308. return WC_TEST_RET_ENC_NC;
  20309. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20310. return WC_TEST_RET_ENC_NC;
  20311. if(outlen != 0)
  20312. return WC_TEST_RET_ENC_NC;
  20313. total += outlen;
  20314. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  20315. return WC_TEST_RET_ENC_NC;
  20316. if(outlen != 0)
  20317. total += outlen;
  20318. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  20319. return WC_TEST_RET_ENC_NC;
  20320. if(outlen != 16)
  20321. return WC_TEST_RET_ENC_NC;
  20322. total += outlen;
  20323. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  20324. return WC_TEST_RET_ENC_NC;
  20325. if(outlen != 2)
  20326. return WC_TEST_RET_ENC_NC;
  20327. total += outlen;
  20328. if(total != 18)
  20329. return 3447;
  20330. if (XMEMCMP(plain, cbcPlain, 18))
  20331. return WC_TEST_RET_ENC_NC;
  20332. if (EVP_CIPHER_key_length(NULL) != 0)
  20333. return WC_TEST_RET_ENC_NC;
  20334. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  20335. return WC_TEST_RET_ENC_NC;
  20336. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  20337. return WC_TEST_RET_ENC_NC;
  20338. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  20339. return WC_TEST_RET_ENC_NC;
  20340. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20341. return WC_TEST_RET_ENC_NC;
  20342. EVP_CIPHER_CTX_init(en);
  20343. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  20344. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20345. return WC_TEST_RET_ENC_NC;
  20346. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20347. return WC_TEST_RET_ENC_NC;
  20348. EVP_CIPHER_CTX_init(en);
  20349. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  20350. (unsigned char*)key, (unsigned char*)iv) == 0)
  20351. return WC_TEST_RET_ENC_NC;
  20352. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20353. return WC_TEST_RET_ENC_NC;
  20354. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20355. return WC_TEST_RET_ENC_NC;
  20356. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20357. return WC_TEST_RET_ENC_NC;
  20358. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20359. return WC_TEST_RET_ENC_NC;
  20360. EVP_CIPHER_CTX_init(de);
  20361. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  20362. (unsigned char*)key, (unsigned char*)iv) == 0)
  20363. return WC_TEST_RET_ENC_NC;
  20364. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20365. return WC_TEST_RET_ENC_NC;
  20366. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20367. return WC_TEST_RET_ENC_NC;
  20368. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  20369. return WC_TEST_RET_ENC_NC;
  20370. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20371. return WC_TEST_RET_ENC_NC;
  20372. EVP_CIPHER_CTX_init(en);
  20373. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  20374. (unsigned char*)key, (unsigned char*)iv);
  20375. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  20376. return WC_TEST_RET_ENC_NC;
  20377. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  20378. return WC_TEST_RET_ENC_NC;
  20379. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  20380. return WC_TEST_RET_ENC_NC;
  20381. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  20382. return WC_TEST_RET_ENC_NC;
  20383. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  20384. return WC_TEST_RET_ENC_NC;
  20385. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  20386. EVP_CIPHER_CTX_set_flags(en, 42);
  20387. if (en->flags != 42)
  20388. return WC_TEST_RET_ENC_NC;
  20389. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  20390. return WC_TEST_RET_ENC_NC;
  20391. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  20392. return WC_TEST_RET_ENC_NC;
  20393. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  20394. return WC_TEST_RET_ENC_NC;
  20395. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20396. return WC_TEST_RET_ENC_NC;
  20397. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20398. return WC_TEST_RET_ENC_NC;
  20399. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20400. wolfSSL_EVP_CIPHER_CTX_free(en);
  20401. wolfSSL_EVP_CIPHER_CTX_free(de);
  20402. #endif
  20403. }
  20404. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  20405. #endif /* ifndef NO_AES */
  20406. return 0;
  20407. }
  20408. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  20409. {
  20410. wc_test_ret_t ret = 0;
  20411. #if !defined(NO_SHA256) && !defined(NO_SHA)
  20412. WOLFSSL_EVP_MD_CTX* ctx;
  20413. WOLFSSL_EVP_MD_CTX* ctx2;
  20414. ctx = EVP_MD_CTX_create();
  20415. ctx2 = EVP_MD_CTX_create();
  20416. ret = EVP_DigestInit(ctx, EVP_sha256());
  20417. if (ret != SSL_SUCCESS) {
  20418. ret = WC_TEST_RET_ENC_NC;
  20419. goto openSSL_evpMD_test_done;
  20420. }
  20421. ret = EVP_MD_CTX_copy(ctx2, ctx);
  20422. if (ret != SSL_SUCCESS) {
  20423. ret = WC_TEST_RET_ENC_NC;
  20424. goto openSSL_evpMD_test_done;
  20425. }
  20426. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  20427. ret = WC_TEST_RET_ENC_NC;
  20428. goto openSSL_evpMD_test_done;
  20429. }
  20430. ret = EVP_DigestInit(ctx, EVP_sha1());
  20431. if (ret != SSL_SUCCESS) {
  20432. ret = WC_TEST_RET_ENC_NC;
  20433. goto openSSL_evpMD_test_done;
  20434. }
  20435. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  20436. ret = WC_TEST_RET_ENC_NC;
  20437. goto openSSL_evpMD_test_done;
  20438. }
  20439. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  20440. if (ret != SSL_SUCCESS) {
  20441. ret = WC_TEST_RET_ENC_NC;
  20442. goto openSSL_evpMD_test_done;
  20443. }
  20444. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  20445. ret = WC_TEST_RET_ENC_NC;
  20446. goto openSSL_evpMD_test_done;
  20447. }
  20448. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  20449. ret = WC_TEST_RET_ENC_NC;
  20450. goto openSSL_evpMD_test_done;
  20451. }
  20452. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  20453. ret = WC_TEST_RET_ENC_NC;
  20454. goto openSSL_evpMD_test_done;
  20455. }
  20456. if (EVP_add_digest(NULL) != 0) {
  20457. ret = WC_TEST_RET_ENC_NC;
  20458. goto openSSL_evpMD_test_done;
  20459. }
  20460. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  20461. ret = WC_TEST_RET_ENC_NC;
  20462. goto openSSL_evpMD_test_done;
  20463. }
  20464. ret = 0; /* got to success state without jumping to end with a fail */
  20465. openSSL_evpMD_test_done:
  20466. EVP_MD_CTX_destroy(ctx);
  20467. EVP_MD_CTX_destroy(ctx2);
  20468. #endif /* NO_SHA256 */
  20469. return ret;
  20470. }
  20471. #ifdef DEBUG_SIGN
  20472. static void show(const char *title, const char *p, unsigned int s) {
  20473. char* i;
  20474. printf("%s: ", title);
  20475. for (i = p;
  20476. i < p + s;
  20477. printf("%c", *i), i++);
  20478. printf("\n");
  20479. }
  20480. #else
  20481. #define show(a,b,c) WC_DO_NOTHING
  20482. #endif
  20483. #define FOURK_BUFF 4096
  20484. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  20485. {
  20486. wc_test_ret_t ret = 0;
  20487. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  20488. byte* prvTmp;
  20489. byte* pubTmp;
  20490. int prvBytes;
  20491. int pubBytes;
  20492. RSA *prvRsa = NULL;
  20493. RSA *pubRsa = NULL;
  20494. EVP_PKEY *prvPkey = NULL;
  20495. EVP_PKEY *pubPkey = NULL;
  20496. EVP_PKEY_CTX *enc = NULL;
  20497. EVP_PKEY_CTX *dec = NULL;
  20498. byte in[] = TEST_STRING;
  20499. byte out[256];
  20500. size_t outlen;
  20501. size_t keySz;
  20502. byte plain[256];
  20503. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  20504. XFILE keyFile;
  20505. XFILE keypubFile;
  20506. char cliKey[] = "./certs/client-key.der";
  20507. char cliKeypub[] = "./certs/client-keyPub.der";
  20508. #endif
  20509. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20510. if (prvTmp == NULL)
  20511. return WC_TEST_RET_ENC_ERRNO;
  20512. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20513. if (pubTmp == NULL) {
  20514. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20515. return WC_TEST_RET_ENC_NC;
  20516. }
  20517. #ifdef USE_CERT_BUFFERS_1024
  20518. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  20519. prvBytes = sizeof_client_key_der_1024;
  20520. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  20521. pubBytes = sizeof_client_keypub_der_1024;
  20522. #elif defined(USE_CERT_BUFFERS_2048)
  20523. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  20524. prvBytes = sizeof_client_key_der_2048;
  20525. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  20526. pubBytes = sizeof_client_keypub_der_2048;
  20527. #else
  20528. keyFile = XFOPEN(cliKey, "rb");
  20529. if (!keyFile) {
  20530. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20531. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20532. err_sys("can't open ./certs/client-key.der, "
  20533. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  20534. return WC_TEST_RET_ENC_ERRNO;
  20535. }
  20536. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  20537. XFCLOSE(keyFile);
  20538. if (prvBytes == 0) {
  20539. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20540. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20541. return WC_TEST_RET_ENC_ERRNO;
  20542. }
  20543. keypubFile = XFOPEN(cliKeypub, "rb");
  20544. if (!keypubFile) {
  20545. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20546. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20547. err_sys("can't open ./certs/client-cert.der, "
  20548. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  20549. return WC_TEST_RET_ENC_ERRNO;
  20550. }
  20551. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  20552. XFCLOSE(keypubFile);
  20553. if (pubBytes == 0) {
  20554. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20555. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20556. return WC_TEST_RET_ENC_ERRNO;
  20557. }
  20558. #endif /* USE_CERT_BUFFERS */
  20559. prvRsa = wolfSSL_RSA_new();
  20560. pubRsa = wolfSSL_RSA_new();
  20561. if((prvRsa == NULL) || (pubRsa == NULL)){
  20562. printf("error with RSA_new\n");
  20563. ret = WC_TEST_RET_ENC_ERRNO;
  20564. goto openssl_pkey0_test_done;
  20565. }
  20566. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  20567. if(ret != SSL_SUCCESS){
  20568. printf("error with RSA_LoadDer_ex\n");
  20569. ret = WC_TEST_RET_ENC_EC(ret);
  20570. goto openssl_pkey0_test_done;
  20571. }
  20572. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  20573. if(ret != SSL_SUCCESS){
  20574. printf("error with RSA_LoadDer_ex\n");
  20575. ret = WC_TEST_RET_ENC_EC(ret);
  20576. goto openssl_pkey0_test_done;
  20577. }
  20578. keySz = (size_t)RSA_size(pubRsa);
  20579. prvPkey = wolfSSL_EVP_PKEY_new();
  20580. pubPkey = wolfSSL_EVP_PKEY_new();
  20581. if((prvPkey == NULL) || (pubPkey == NULL)){
  20582. printf("error with PKEY_new\n");
  20583. ret = WC_TEST_RET_ENC_NC;
  20584. goto openssl_pkey0_test_done;
  20585. }
  20586. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  20587. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  20588. if(ret != 2){
  20589. printf("error with PKEY_set1_RSA\n");
  20590. ret = WC_TEST_RET_ENC_I(ret);
  20591. goto openssl_pkey0_test_done;
  20592. }
  20593. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  20594. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  20595. if((dec == NULL)||(enc==NULL)){
  20596. printf("error with EVP_PKEY_CTX_new\n");
  20597. ret = WC_TEST_RET_ENC_NC;
  20598. goto openssl_pkey0_test_done;
  20599. }
  20600. ret = EVP_PKEY_decrypt_init(dec);
  20601. if (ret != 1) {
  20602. printf("error with decrypt init\n");
  20603. ret = WC_TEST_RET_ENC_NC;
  20604. goto openssl_pkey0_test_done;
  20605. }
  20606. ret = EVP_PKEY_encrypt_init(enc);
  20607. if (ret != 1) {
  20608. printf("error with encrypt init\n");
  20609. ret = WC_TEST_RET_ENC_NC;
  20610. goto openssl_pkey0_test_done;
  20611. }
  20612. XMEMSET(out, 0, sizeof(out));
  20613. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  20614. if (ret != 1) {
  20615. printf("error encrypting msg\n");
  20616. ret = WC_TEST_RET_ENC_NC;
  20617. goto openssl_pkey0_test_done;
  20618. }
  20619. show("encrypted msg", out, outlen);
  20620. XMEMSET(plain, 0, sizeof(plain));
  20621. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  20622. if (ret != 1) {
  20623. printf("error decrypting msg\n");
  20624. ret = WC_TEST_RET_ENC_NC;
  20625. goto openssl_pkey0_test_done;
  20626. }
  20627. show("decrypted msg", plain, outlen);
  20628. /* RSA_PKCS1_OAEP_PADDING test */
  20629. ret = EVP_PKEY_decrypt_init(dec);
  20630. if (ret != 1) {
  20631. printf("error with decrypt init\n");
  20632. ret = WC_TEST_RET_ENC_NC;
  20633. goto openssl_pkey0_test_done;
  20634. }
  20635. ret = EVP_PKEY_encrypt_init(enc);
  20636. if (ret != 1) {
  20637. printf("error with encrypt init\n");
  20638. ret = WC_TEST_RET_ENC_NC;
  20639. goto openssl_pkey0_test_done;
  20640. }
  20641. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  20642. printf("first set rsa padding error\n");
  20643. ret = WC_TEST_RET_ENC_EC(ret);
  20644. goto openssl_pkey0_test_done;
  20645. }
  20646. #ifndef HAVE_FIPS
  20647. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  20648. printf("second set rsa padding error\n");
  20649. ret = WC_TEST_RET_ENC_EC(ret);
  20650. goto openssl_pkey0_test_done;
  20651. }
  20652. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  20653. printf("third set rsa padding error\n");
  20654. ret = WC_TEST_RET_ENC_EC(ret);
  20655. goto openssl_pkey0_test_done;
  20656. }
  20657. #endif
  20658. XMEMSET(out, 0, sizeof(out));
  20659. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  20660. if (ret != 1) {
  20661. printf("error encrypting msg\n");
  20662. ret = WC_TEST_RET_ENC_NC;
  20663. goto openssl_pkey0_test_done;
  20664. }
  20665. show("encrypted msg", out, outlen);
  20666. XMEMSET(plain, 0, sizeof(plain));
  20667. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  20668. if (ret != 1) {
  20669. printf("error decrypting msg\n");
  20670. ret = WC_TEST_RET_ENC_NC;
  20671. goto openssl_pkey0_test_done;
  20672. }
  20673. show("decrypted msg", plain, outlen);
  20674. ret = 0; /* made it to this point without error then set success */
  20675. openssl_pkey0_test_done:
  20676. wolfSSL_RSA_free(prvRsa);
  20677. wolfSSL_RSA_free(pubRsa);
  20678. EVP_PKEY_free(pubPkey);
  20679. EVP_PKEY_free(prvPkey);
  20680. EVP_PKEY_CTX_free(dec);
  20681. EVP_PKEY_CTX_free(enc);
  20682. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20683. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20684. #endif /* NO_RSA */
  20685. return ret;
  20686. }
  20687. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  20688. {
  20689. wc_test_ret_t ret = 0;
  20690. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  20691. !defined(NO_SHA)
  20692. EVP_PKEY_CTX* dec = NULL;
  20693. EVP_PKEY_CTX* enc = NULL;
  20694. EVP_PKEY* pubKey = NULL;
  20695. EVP_PKEY* prvKey = NULL;
  20696. X509* x509 = NULL;
  20697. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  20698. const unsigned char* clikey;
  20699. long cliKeySz;
  20700. size_t outlen;
  20701. int keyLenBits = 2048;
  20702. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20703. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20704. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20705. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20706. if ((tmp == NULL) ||
  20707. (cipher == NULL) ||
  20708. (plain == NULL)) {
  20709. ret = WC_TEST_RET_ENC_NC;
  20710. goto openssl_pkey1_test_done;
  20711. }
  20712. #else
  20713. unsigned char tmp[FOURK_BUF];
  20714. unsigned char cipher[RSA_TEST_BYTES];
  20715. unsigned char plain[RSA_TEST_BYTES];
  20716. #endif
  20717. #if defined(USE_CERT_BUFFERS_1024)
  20718. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  20719. cliKeySz = (long)sizeof_client_key_der_1024;
  20720. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  20721. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  20722. keyLenBits = 1024;
  20723. #elif defined(USE_CERT_BUFFERS_2048)
  20724. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  20725. cliKeySz = (long)sizeof_client_key_der_2048;
  20726. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  20727. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  20728. #elif defined(USE_CERT_BUFFERS_3072)
  20729. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  20730. cliKeySz = (long)sizeof_client_key_der_3072;
  20731. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  20732. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  20733. keyLenBits = 3072;
  20734. #elif defined(USE_CERT_BUFFERS_4096)
  20735. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  20736. cliKeySz = (long)sizeof_client_key_der_4096;
  20737. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  20738. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  20739. keyLenBits = 4096;
  20740. #else
  20741. {
  20742. XFILE f;
  20743. f = XFOPEN(clientKey, "rb");
  20744. if (!f) {
  20745. err_sys("can't open ./certs/client-key.der, "
  20746. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  20747. ret = WC_TEST_RET_ENC_ERRNO;
  20748. goto openssl_pkey1_test_done;
  20749. }
  20750. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  20751. XFCLOSE(f);
  20752. if (cliKeySz == 0)
  20753. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  20754. }
  20755. /* using existing wolfSSL api to get public and private key */
  20756. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  20757. #endif /* USE_CERT_BUFFERS */
  20758. clikey = tmp;
  20759. if ((prvKey = EVP_PKEY_new()) == NULL) {
  20760. ret = WC_TEST_RET_ENC_ERRNO;
  20761. goto openssl_pkey1_test_done;
  20762. }
  20763. EVP_PKEY_free(prvKey);
  20764. prvKey = NULL;
  20765. if (x509 == NULL) {
  20766. ret = WC_TEST_RET_ENC_NC;
  20767. goto openssl_pkey1_test_done;
  20768. }
  20769. pubKey = X509_get_pubkey(x509);
  20770. if (pubKey == NULL) {
  20771. ret = WC_TEST_RET_ENC_NC;
  20772. goto openssl_pkey1_test_done;
  20773. }
  20774. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  20775. if (prvKey == NULL) {
  20776. ret = WC_TEST_RET_ENC_NC;
  20777. goto openssl_pkey1_test_done;
  20778. }
  20779. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  20780. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  20781. ret = WC_TEST_RET_ENC_NC;
  20782. goto openssl_pkey1_test_done;
  20783. }
  20784. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  20785. ret = WC_TEST_RET_ENC_NC;
  20786. goto openssl_pkey1_test_done;
  20787. }
  20788. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  20789. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  20790. if (dec == NULL || enc == NULL) {
  20791. ret = WC_TEST_RET_ENC_ERRNO;
  20792. goto openssl_pkey1_test_done;
  20793. }
  20794. if (EVP_PKEY_decrypt_init(dec) != 1) {
  20795. ret = WC_TEST_RET_ENC_NC;
  20796. goto openssl_pkey1_test_done;
  20797. }
  20798. if (EVP_PKEY_encrypt_init(enc) != 1) {
  20799. ret = WC_TEST_RET_ENC_NC;
  20800. goto openssl_pkey1_test_done;
  20801. }
  20802. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  20803. ret = WC_TEST_RET_ENC_NC;
  20804. goto openssl_pkey1_test_done;
  20805. }
  20806. #ifndef HAVE_FIPS
  20807. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  20808. ret = WC_TEST_RET_ENC_EC(ret);
  20809. goto openssl_pkey1_test_done;
  20810. }
  20811. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  20812. ret = WC_TEST_RET_ENC_EC(ret);
  20813. goto openssl_pkey1_test_done;
  20814. }
  20815. #endif
  20816. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  20817. outlen = keyLenBits/8;
  20818. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  20819. ret = WC_TEST_RET_ENC_EC(ret);
  20820. goto openssl_pkey1_test_done;
  20821. }
  20822. XMEMSET(plain, 0, RSA_TEST_BYTES);
  20823. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  20824. ret = WC_TEST_RET_ENC_NC;
  20825. goto openssl_pkey1_test_done;
  20826. }
  20827. openssl_pkey1_test_done:
  20828. if (pubKey != NULL) {
  20829. EVP_PKEY_free(pubKey);
  20830. }
  20831. if (prvKey != NULL) {
  20832. EVP_PKEY_free(prvKey);
  20833. }
  20834. if (dec != NULL) {
  20835. EVP_PKEY_CTX_free(dec);
  20836. }
  20837. if (enc != NULL) {
  20838. EVP_PKEY_CTX_free(enc);
  20839. }
  20840. if (x509 != NULL) {
  20841. X509_free(x509);
  20842. }
  20843. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20844. if (tmp != NULL)
  20845. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20846. if (cipher != NULL)
  20847. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20848. if (plain != NULL)
  20849. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20850. #endif
  20851. #endif
  20852. return ret;
  20853. }
  20854. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  20855. {
  20856. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  20857. byte* prvTmp;
  20858. byte* pubTmp;
  20859. int prvBytes;
  20860. int pubBytes;
  20861. RSA *prvRsa;
  20862. RSA *pubRsa;
  20863. EVP_PKEY *prvPkey;
  20864. EVP_PKEY *pubPkey;
  20865. EVP_MD_CTX* sign;
  20866. EVP_MD_CTX* verf;
  20867. char msg[] = "see spot run";
  20868. unsigned char sig[256];
  20869. unsigned int sigSz;
  20870. const void* pt;
  20871. unsigned int count;
  20872. wc_test_ret_t ret, ret1, ret2;
  20873. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  20874. XFILE keyFile;
  20875. XFILE keypubFile;
  20876. char cliKey[] = "./certs/client-key.der";
  20877. char cliKeypub[] = "./certs/client-keyPub.der";
  20878. #endif
  20879. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20880. if (prvTmp == NULL)
  20881. return WC_TEST_RET_ENC_ERRNO;
  20882. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20883. if (pubTmp == NULL) {
  20884. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20885. return WC_TEST_RET_ENC_NC;
  20886. }
  20887. #ifdef USE_CERT_BUFFERS_1024
  20888. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  20889. prvBytes = sizeof_client_key_der_1024;
  20890. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  20891. pubBytes = sizeof_client_keypub_der_1024;
  20892. #elif defined(USE_CERT_BUFFERS_2048)
  20893. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  20894. prvBytes = sizeof_client_key_der_2048;
  20895. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  20896. pubBytes = sizeof_client_keypub_der_2048;
  20897. #else
  20898. keyFile = XFOPEN(cliKey, "rb");
  20899. if (!keyFile) {
  20900. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20901. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20902. err_sys("can't open ./certs/client-key.der, "
  20903. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  20904. return WC_TEST_RET_ENC_ERRNO;
  20905. }
  20906. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  20907. XFCLOSE(keyFile);
  20908. if (prvBytes == 0) {
  20909. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20910. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20911. return WC_TEST_RET_ENC_ERRNO;
  20912. }
  20913. keypubFile = XFOPEN(cliKeypub, "rb");
  20914. if (!keypubFile) {
  20915. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20916. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20917. err_sys("can't open ./certs/client-cert.der, "
  20918. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  20919. return WC_TEST_RET_ENC_ERRNO;
  20920. }
  20921. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  20922. XFCLOSE(keypubFile);
  20923. if (pubBytes == 0) {
  20924. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20925. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20926. return WC_TEST_RET_ENC_ERRNO;
  20927. }
  20928. #endif /* USE_CERT_BUFFERS */
  20929. prvRsa = wolfSSL_RSA_new();
  20930. pubRsa = wolfSSL_RSA_new();
  20931. if((prvRsa == NULL) || (pubRsa == NULL)){
  20932. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20933. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20934. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  20935. return WC_TEST_RET_ENC_NC;
  20936. }
  20937. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  20938. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  20939. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  20940. printf("error with RSA_LoadDer_ex\n");
  20941. return WC_TEST_RET_ENC_NC;
  20942. }
  20943. prvPkey = wolfSSL_EVP_PKEY_new();
  20944. pubPkey = wolfSSL_EVP_PKEY_new();
  20945. if((prvPkey == NULL) || (pubPkey == NULL)){
  20946. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20947. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20948. printf("error with KEY_new\n");
  20949. return WC_TEST_RET_ENC_NC;
  20950. }
  20951. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  20952. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  20953. if((ret1 != 1) || (ret2 != 1)){
  20954. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20955. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20956. printf("error with EVP_PKEY_set1_RSA\n");
  20957. return WC_TEST_RET_ENC_NC;
  20958. }
  20959. /****************** sign and verify *******************/
  20960. sign = EVP_MD_CTX_create();
  20961. verf = EVP_MD_CTX_create();
  20962. if((sign == NULL)||(verf == NULL)){
  20963. printf("error with EVP_MD_CTX_create\n");
  20964. EVP_MD_CTX_destroy(sign);
  20965. EVP_MD_CTX_destroy(verf);
  20966. return WC_TEST_RET_ENC_NC;
  20967. }
  20968. ret = EVP_SignInit(sign, EVP_sha1());
  20969. if (ret != SSL_SUCCESS){
  20970. printf("error with EVP_SignInit\n");
  20971. EVP_MD_CTX_destroy(sign);
  20972. EVP_MD_CTX_destroy(verf);
  20973. return WC_TEST_RET_ENC_NC;
  20974. }
  20975. count = sizeof(msg);
  20976. show("message = ", (char *)msg, count);
  20977. /* sign */
  20978. XMEMSET(sig, 0, sizeof(sig));
  20979. pt = (const void*)msg;
  20980. ret1 = EVP_SignUpdate(sign, pt, count);
  20981. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  20982. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  20983. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20984. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20985. EVP_MD_CTX_destroy(sign);
  20986. EVP_MD_CTX_destroy(verf);
  20987. printf("error with EVP_MD_CTX_create\n");
  20988. return WC_TEST_RET_ENC_NC;
  20989. }
  20990. show("signature = ", (char *)sig, sigSz);
  20991. /* verify */
  20992. pt = (const void*)msg;
  20993. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  20994. ret2 = EVP_VerifyUpdate(verf, pt, count);
  20995. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  20996. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20997. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20998. EVP_MD_CTX_destroy(sign);
  20999. EVP_MD_CTX_destroy(verf);
  21000. printf("error with EVP_Verify\n");
  21001. return WC_TEST_RET_ENC_NC;
  21002. }
  21003. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  21004. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21005. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21006. EVP_MD_CTX_destroy(sign);
  21007. EVP_MD_CTX_destroy(verf);
  21008. printf("error with EVP_VerifyFinal\n");
  21009. return WC_TEST_RET_ENC_NC;
  21010. }
  21011. /* expect fail without update */
  21012. EVP_VerifyInit(verf, EVP_sha1());
  21013. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  21014. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21015. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21016. EVP_MD_CTX_destroy(sign);
  21017. EVP_MD_CTX_destroy(verf);
  21018. printf("EVP_VerifyInit without update not detected\n");
  21019. return WC_TEST_RET_ENC_NC;
  21020. }
  21021. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21022. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21023. EVP_MD_CTX_destroy(sign);
  21024. EVP_MD_CTX_destroy(verf);
  21025. wolfSSL_RSA_free(prvRsa);
  21026. wolfSSL_RSA_free(pubRsa);
  21027. EVP_PKEY_free(pubPkey);
  21028. EVP_PKEY_free(prvPkey);
  21029. #endif /* NO_RSA */
  21030. return 0;
  21031. }
  21032. #endif /* OPENSSL_EXTRA */
  21033. #ifndef NO_PWDBASED
  21034. #ifdef HAVE_SCRYPT
  21035. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  21036. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  21037. {
  21038. #ifdef HAVE_FIPS
  21039. /* RFC 7914 test vector keys are too short for FIPS. */
  21040. #else
  21041. wc_test_ret_t ret;
  21042. byte derived[64];
  21043. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  21044. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  21045. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  21046. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  21047. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  21048. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  21049. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  21050. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  21051. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  21052. };
  21053. #if !defined(BENCH_EMBEDDED)
  21054. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21055. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  21056. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  21057. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  21058. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  21059. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  21060. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  21061. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  21062. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  21063. };
  21064. #endif
  21065. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  21066. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  21067. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  21068. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  21069. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  21070. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  21071. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  21072. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  21073. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  21074. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  21075. };
  21076. #endif
  21077. #ifdef SCRYPT_TEST_ALL
  21078. /* Test case is very slow.
  21079. * Use for confirmation after code change or new platform.
  21080. */
  21081. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  21082. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  21083. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  21084. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  21085. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  21086. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  21087. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  21088. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  21089. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  21090. };
  21091. #endif
  21092. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  21093. if (ret != 0)
  21094. return WC_TEST_RET_ENC_EC(ret);
  21095. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  21096. return WC_TEST_RET_ENC_NC;
  21097. #if !defined(BENCH_EMBEDDED)
  21098. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  21099. sizeof(verify2));
  21100. if (ret != 0)
  21101. return WC_TEST_RET_ENC_EC(ret);
  21102. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  21103. return WC_TEST_RET_ENC_NC;
  21104. #endif
  21105. /* Test case with parallel overflowing */
  21106. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  21107. sizeof(verify1));
  21108. if (ret != BAD_FUNC_ARG)
  21109. return WC_TEST_RET_ENC_EC(ret);
  21110. /* Don't run these test on embedded, since they use large mallocs */
  21111. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  21112. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  21113. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  21114. if (ret != 0)
  21115. return WC_TEST_RET_ENC_EC(ret);
  21116. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  21117. return WC_TEST_RET_ENC_NC;
  21118. #ifdef SCRYPT_TEST_ALL
  21119. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  21120. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  21121. if (ret != 0)
  21122. return WC_TEST_RET_ENC_EC(ret);
  21123. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  21124. return WC_TEST_RET_ENC_NC;
  21125. #endif
  21126. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  21127. #if !defined(BENCH_EMBEDDED)
  21128. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  21129. 8, 16, sizeof(verify2));
  21130. if (ret != 0)
  21131. return WC_TEST_RET_ENC_EC(ret);
  21132. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  21133. return WC_TEST_RET_ENC_NC;
  21134. #endif
  21135. #endif /* !HAVE_FIPS */
  21136. return 0;
  21137. }
  21138. #endif
  21139. #ifdef HAVE_PKCS12
  21140. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  21141. {
  21142. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  21143. 0x00, 0x00 };
  21144. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  21145. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  21146. 0x00, 0x67, 0x00, 0x00 };
  21147. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  21148. byte derived[64];
  21149. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21150. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  21151. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  21152. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  21153. };
  21154. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21155. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  21156. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  21157. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  21158. };
  21159. int id = 1;
  21160. int kLen = 24;
  21161. int iterations = 1;
  21162. wc_test_ret_t ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  21163. iterations, kLen, WC_SHA256, id);
  21164. if (ret < 0)
  21165. return WC_TEST_RET_ENC_EC(ret);
  21166. if (XMEMCMP(derived, verify, kLen) != 0)
  21167. return WC_TEST_RET_ENC_NC;
  21168. iterations = 1000;
  21169. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  21170. iterations, kLen, WC_SHA256, id);
  21171. if (ret < 0)
  21172. return WC_TEST_RET_ENC_EC(ret);
  21173. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  21174. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  21175. if (ret < 0)
  21176. return WC_TEST_RET_ENC_EC(ret);
  21177. if (XMEMCMP(derived, verify2, 24) != 0)
  21178. return WC_TEST_RET_ENC_NC;
  21179. return 0;
  21180. }
  21181. #endif /* HAVE_PKCS12 */
  21182. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  21183. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  21184. {
  21185. char passwd[] = "passwordpassword";
  21186. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  21187. int iterations = 2048;
  21188. int kLen = 24;
  21189. byte derived[64];
  21190. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21191. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  21192. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  21193. };
  21194. wc_test_ret_t ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  21195. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  21196. if (ret != 0)
  21197. return ret;
  21198. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  21199. return WC_TEST_RET_ENC_NC;
  21200. return 0;
  21201. }
  21202. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  21203. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  21204. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  21205. {
  21206. char passwd[] = "password";
  21207. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  21208. int iterations = 1000;
  21209. int kLen = 16;
  21210. byte derived[16];
  21211. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21212. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  21213. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  21214. };
  21215. wc_test_ret_t ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  21216. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  21217. HEAP_HINT);
  21218. if (ret != 0)
  21219. return ret;
  21220. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  21221. return WC_TEST_RET_ENC_NC;
  21222. return 0;
  21223. }
  21224. #endif /* HAVE_PBKDF2 && !NO_SHA */
  21225. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  21226. {
  21227. wc_test_ret_t ret = 0;
  21228. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  21229. ret = pbkdf1_test();
  21230. if (ret != 0)
  21231. return ret;
  21232. #endif
  21233. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  21234. ret = pbkdf2_test();
  21235. if (ret != 0)
  21236. return ret;
  21237. #endif
  21238. #ifdef HAVE_PKCS12
  21239. ret = pkcs12_test();
  21240. if (ret != 0)
  21241. return ret;
  21242. #endif
  21243. #ifdef HAVE_SCRYPT
  21244. ret = scrypt_test();
  21245. if (ret != 0)
  21246. return ret;
  21247. #endif
  21248. return ret;
  21249. }
  21250. #endif /* NO_PWDBASED */
  21251. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  21252. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  21253. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  21254. defined(WOLFSSL_AFALG_XILINX_RSA)
  21255. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  21256. static wc_test_ret_t hkdf_test(void)
  21257. #else
  21258. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  21259. #endif
  21260. {
  21261. wc_test_ret_t ret = 0;
  21262. #if !defined(NO_SHA) || !defined(NO_SHA256)
  21263. int L;
  21264. byte okm1[42];
  21265. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21266. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21267. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  21268. #ifndef HAVE_FIPS
  21269. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21270. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  21271. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  21272. 0xf8, 0xf9 };
  21273. #endif
  21274. #ifndef NO_SHA
  21275. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  21276. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  21277. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  21278. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  21279. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  21280. 0x49, 0x18 };
  21281. #ifndef HAVE_FIPS
  21282. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  21283. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  21284. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  21285. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  21286. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  21287. 0xf8, 0x96 };
  21288. #endif
  21289. #endif /* !NO_SHA */
  21290. #ifndef NO_SHA256
  21291. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  21292. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  21293. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  21294. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  21295. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  21296. 0x96, 0xc8 };
  21297. #ifndef HAVE_FIPS
  21298. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  21299. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  21300. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  21301. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  21302. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  21303. 0x58, 0x65 };
  21304. #endif
  21305. #endif /* !NO_SHA256 */
  21306. XMEMSET(okm1, 0, sizeof(okm1));
  21307. L = (int)sizeof(okm1);
  21308. #ifndef NO_SHA
  21309. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  21310. okm1, L);
  21311. if (ret != 0)
  21312. return WC_TEST_RET_ENC_EC(ret);
  21313. if (XMEMCMP(okm1, res1, L) != 0)
  21314. return WC_TEST_RET_ENC_NC;
  21315. #ifndef HAVE_FIPS
  21316. /* fips can't have key size under 14 bytes, salt is key too */
  21317. L = (int)sizeof(okm1);
  21318. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  21319. info1, (word32)sizeof(info1), okm1, L);
  21320. if (ret != 0)
  21321. return WC_TEST_RET_ENC_EC(ret);
  21322. if (XMEMCMP(okm1, res2, L) != 0)
  21323. return WC_TEST_RET_ENC_NC;
  21324. #endif /* HAVE_FIPS */
  21325. #endif /* !NO_SHA */
  21326. #ifndef NO_SHA256
  21327. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  21328. okm1, L);
  21329. if (ret != 0)
  21330. return WC_TEST_RET_ENC_EC(ret);
  21331. if (XMEMCMP(okm1, res3, L) != 0)
  21332. return WC_TEST_RET_ENC_NC;
  21333. #ifndef HAVE_FIPS
  21334. /* fips can't have key size under 14 bytes, salt is key too */
  21335. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  21336. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  21337. if (ret != 0)
  21338. return WC_TEST_RET_ENC_EC(ret);
  21339. if (XMEMCMP(okm1, res4, L) != 0)
  21340. return WC_TEST_RET_ENC_NC;
  21341. #endif /* HAVE_FIPS */
  21342. #endif /* !NO_SHA256 */
  21343. #endif /* !NO_SHA || !NO_SHA256 */
  21344. return ret;
  21345. }
  21346. #endif /* HAVE_HKDF */
  21347. #ifdef WOLFSSL_WOLFSSH
  21348. typedef struct {
  21349. byte hashId;
  21350. byte keyId;
  21351. const byte* k;
  21352. word32 kSz;
  21353. const byte* h;
  21354. word32 hSz;
  21355. const byte* sessionId;
  21356. word32 sessionIdSz;
  21357. const byte* expectedKey;
  21358. word32 expectedKeySz;
  21359. } SshKdfTestVector;
  21360. /** Test Vector Set #3: SHA-256 **/
  21361. static const byte sshKdfTvSet3k[] = {
  21362. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  21363. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  21364. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  21365. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  21366. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  21367. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  21368. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  21369. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  21370. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  21371. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  21372. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  21373. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  21374. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  21375. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  21376. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  21377. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  21378. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  21379. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  21380. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  21381. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  21382. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  21383. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  21384. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  21385. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  21386. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  21387. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  21388. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  21389. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  21390. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  21391. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  21392. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  21393. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  21394. };
  21395. static const byte sshKdfTvSet3h[] = {
  21396. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  21397. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  21398. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  21399. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  21400. };
  21401. static const byte sshKdfTvSet3sid[] = {
  21402. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  21403. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  21404. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  21405. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  21406. };
  21407. static const byte sshKdfTvSet3a[] = {
  21408. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  21409. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  21410. };
  21411. static const byte sshKdfTvSet3b[] = {
  21412. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  21413. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  21414. };
  21415. static const byte sshKdfTvSet3c[] = {
  21416. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  21417. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  21418. };
  21419. static const byte sshKdfTvSet3d[] = {
  21420. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  21421. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  21422. };
  21423. static const byte sshKdfTvSet3e[] = {
  21424. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  21425. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  21426. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  21427. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  21428. };
  21429. static const byte sshKdfTvSet3f[] = {
  21430. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  21431. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  21432. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  21433. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  21434. };
  21435. static const SshKdfTestVector sshKdfTestVectors[] = {
  21436. {WC_HASH_TYPE_SHA256, 'A',
  21437. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21438. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21439. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21440. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  21441. {WC_HASH_TYPE_SHA256, 'B',
  21442. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21443. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21444. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21445. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  21446. {WC_HASH_TYPE_SHA256, 'C',
  21447. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21448. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21449. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21450. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  21451. {WC_HASH_TYPE_SHA256, 'D',
  21452. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21453. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21454. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21455. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  21456. {WC_HASH_TYPE_SHA256, 'E',
  21457. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21458. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21459. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21460. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  21461. {WC_HASH_TYPE_SHA256, 'F',
  21462. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21463. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21464. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21465. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  21466. };
  21467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  21468. {
  21469. wc_test_ret_t result = 0;
  21470. word32 i;
  21471. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  21472. const SshKdfTestVector* tv = NULL;
  21473. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  21474. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  21475. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  21476. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  21477. cKey, tv->expectedKeySz,
  21478. tv->k, tv->kSz, tv->h, tv->hSz,
  21479. tv->sessionId, tv->sessionIdSz);
  21480. if (result != 0) {
  21481. printf("KDF: Could not derive key.\n");
  21482. result = WC_TEST_RET_ENC_EC(result);
  21483. }
  21484. else {
  21485. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  21486. printf("KDF: Calculated Key does not match Expected Key.\n");
  21487. result = WC_TEST_RET_ENC_EC(result);
  21488. }
  21489. }
  21490. if (result != 0) break;
  21491. }
  21492. return result;
  21493. }
  21494. #endif /* WOLFSSL_WOLFSSH */
  21495. #ifdef WOLFSSL_TLS13
  21496. #define TLSV13_PSK_DHE_SZ 40
  21497. typedef struct {
  21498. enum wc_HashType hashAlg;
  21499. word32 pskSz;
  21500. word32 dheSz;
  21501. byte psk[TLSV13_PSK_DHE_SZ];
  21502. byte dhe[TLSV13_PSK_DHE_SZ];
  21503. byte hashHello1[WC_MAX_DIGEST_SIZE];
  21504. byte hashHello2[WC_MAX_DIGEST_SIZE];
  21505. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  21506. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  21507. /* Expected */
  21508. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  21509. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  21510. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  21511. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  21512. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  21513. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  21514. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  21515. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  21516. } Tls13KdfTestVector;
  21517. /* The following tests come from the CAVP test vectors we used for
  21518. * our FIPS validation. The hash values used are the components from
  21519. * the test hashed together. hashHello1 is the hash of the
  21520. * clientHelloRandom value of the test vector. hashHello2 is the hash
  21521. * of the clientHelloRandom and serverHelloRandom values from the test
  21522. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  21523. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  21524. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  21525. * hashed together. */
  21526. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  21527. { /* 1 */
  21528. WC_HASH_TYPE_SHA256, 35, 35,
  21529. { /* PSK */
  21530. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  21531. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  21532. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  21533. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  21534. 0x7f, 0x6d, 0xbe
  21535. },
  21536. { /* DHE */
  21537. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  21538. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  21539. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  21540. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  21541. 0x81, 0x26, 0x40
  21542. },
  21543. { /* Hello 1 */
  21544. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  21545. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  21546. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  21547. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  21548. },
  21549. { /* Hello 2 */
  21550. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  21551. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  21552. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  21553. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  21554. },
  21555. { /* Finished 1 */
  21556. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  21557. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  21558. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  21559. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  21560. },
  21561. { /* Finished 2 */
  21562. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  21563. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  21564. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  21565. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  21566. },
  21567. { /* Client Early Traffic Secret */
  21568. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  21569. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  21570. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  21571. },
  21572. { /* Early Exporter Master Secret */
  21573. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  21574. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  21575. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  21576. },
  21577. { /* Client Handshake Traffic Secret */
  21578. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  21579. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  21580. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  21581. },
  21582. { /* Server Handshake Traffic Secret */
  21583. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  21584. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  21585. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  21586. },
  21587. { /* Client Application Traffic Secret */
  21588. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  21589. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  21590. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  21591. },
  21592. { /* Server Application Traffic Secret */
  21593. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  21594. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  21595. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  21596. },
  21597. { /* Exporter Master Secret */
  21598. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  21599. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  21600. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  21601. },
  21602. { /* Resumption Master Secret */
  21603. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  21604. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  21605. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  21606. }
  21607. },
  21608. { /* 6 */
  21609. WC_HASH_TYPE_SHA256, 0, 33,
  21610. { 0 }, /* PSK */
  21611. { /* DHE */
  21612. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  21613. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  21614. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  21615. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  21616. 0x18
  21617. },
  21618. { /* Hello 1 */
  21619. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  21620. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  21621. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  21622. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  21623. },
  21624. { /* Hello 2 */
  21625. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  21626. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  21627. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  21628. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  21629. },
  21630. { /* Finished 1 */
  21631. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  21632. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  21633. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  21634. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  21635. },
  21636. { /* Finished 2 */
  21637. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  21638. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  21639. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  21640. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  21641. },
  21642. {
  21643. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  21644. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  21645. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  21646. },
  21647. {
  21648. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  21649. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  21650. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  21651. },
  21652. {
  21653. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  21654. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  21655. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  21656. },
  21657. {
  21658. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  21659. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  21660. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  21661. },
  21662. {
  21663. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  21664. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  21665. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  21666. },
  21667. {
  21668. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  21669. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  21670. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  21671. },
  21672. {
  21673. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  21674. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  21675. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  21676. },
  21677. {
  21678. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  21679. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  21680. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  21681. }
  21682. },
  21683. { /* 11 */
  21684. WC_HASH_TYPE_SHA256, 33, 0,
  21685. { /* PSK */
  21686. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  21687. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  21688. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  21689. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  21690. 0x12
  21691. },
  21692. { 0 }, /* DHE */
  21693. { /* Hello 1 */
  21694. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  21695. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  21696. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  21697. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  21698. },
  21699. { /* Hello 2 */
  21700. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  21701. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  21702. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  21703. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  21704. },
  21705. { /* Finished 1 */
  21706. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  21707. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  21708. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  21709. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  21710. },
  21711. { /* Finished 2 */
  21712. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  21713. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  21714. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  21715. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  21716. },
  21717. {
  21718. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  21719. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  21720. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  21721. {
  21722. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  21723. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  21724. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  21725. {
  21726. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  21727. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  21728. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  21729. {
  21730. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  21731. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  21732. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  21733. {
  21734. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  21735. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  21736. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  21737. {
  21738. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  21739. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  21740. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  21741. {
  21742. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  21743. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  21744. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  21745. {
  21746. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  21747. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  21748. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  21749. },
  21750. #ifdef WOLFSSL_SHA384
  21751. { /* 26 */
  21752. WC_HASH_TYPE_SHA384, 35, 35,
  21753. { /* PSK */
  21754. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  21755. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  21756. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  21757. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  21758. 0x68, 0xba, 0xf9
  21759. },
  21760. { /* DHE */
  21761. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  21762. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  21763. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  21764. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  21765. 0xd8, 0x66, 0xac
  21766. },
  21767. { /* Hello 1 */
  21768. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  21769. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  21770. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  21771. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  21772. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  21773. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  21774. },
  21775. { /* Hello 2 */
  21776. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  21777. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  21778. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  21779. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  21780. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  21781. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  21782. },
  21783. { /* Finished 1 */
  21784. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  21785. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  21786. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  21787. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  21788. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  21789. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  21790. },
  21791. { /* Finished 2 */
  21792. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  21793. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  21794. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  21795. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  21796. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  21797. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  21798. },
  21799. {
  21800. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  21801. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  21802. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  21803. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  21804. },
  21805. {
  21806. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  21807. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  21808. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  21809. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  21810. },
  21811. {
  21812. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  21813. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  21814. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  21815. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  21816. },
  21817. {
  21818. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  21819. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  21820. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  21821. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  21822. },
  21823. {
  21824. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  21825. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  21826. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  21827. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  21828. },
  21829. {
  21830. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  21831. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  21832. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  21833. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  21834. },
  21835. {
  21836. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  21837. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  21838. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  21839. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  21840. },
  21841. {
  21842. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  21843. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  21844. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  21845. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  21846. }
  21847. },
  21848. { /* 36 */
  21849. WC_HASH_TYPE_SHA384, 0, 33,
  21850. { 0 }, /* PSK */
  21851. { /* DHE */
  21852. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  21853. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  21854. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  21855. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  21856. 0xfd
  21857. },
  21858. { /* Hello 1 */
  21859. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  21860. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  21861. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  21862. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  21863. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  21864. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  21865. },
  21866. { /* Hello 2 */
  21867. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  21868. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  21869. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  21870. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  21871. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  21872. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  21873. },
  21874. { /* Finished 1 */
  21875. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  21876. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  21877. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  21878. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  21879. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  21880. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  21881. },
  21882. { /* Finished 2 */
  21883. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  21884. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  21885. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  21886. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  21887. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  21888. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  21889. },
  21890. {
  21891. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  21892. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  21893. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  21894. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  21895. },
  21896. {
  21897. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  21898. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  21899. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  21900. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  21901. },
  21902. {
  21903. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  21904. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  21905. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  21906. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  21907. },
  21908. {
  21909. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  21910. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  21911. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  21912. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  21913. },
  21914. {
  21915. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  21916. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  21917. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  21918. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  21919. },
  21920. {
  21921. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  21922. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  21923. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  21924. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  21925. },
  21926. {
  21927. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  21928. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  21929. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  21930. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  21931. },
  21932. {
  21933. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  21934. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  21935. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  21936. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  21937. }
  21938. },
  21939. { /* 41 */
  21940. WC_HASH_TYPE_SHA384, 33, 0,
  21941. { /* PSK */
  21942. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  21943. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  21944. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  21945. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  21946. 0x12
  21947. },
  21948. { 0 }, /* DHE */
  21949. { /* Hello 1 */
  21950. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  21951. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  21952. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  21953. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  21954. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  21955. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  21956. },
  21957. { /* Hello 2 */
  21958. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  21959. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  21960. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  21961. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  21962. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  21963. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  21964. },
  21965. { /* Finished 1 */
  21966. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  21967. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  21968. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  21969. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  21970. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  21971. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  21972. },
  21973. { /* Finished 2 */
  21974. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  21975. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  21976. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  21977. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  21978. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  21979. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  21980. },
  21981. {
  21982. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  21983. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  21984. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  21985. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  21986. },
  21987. {
  21988. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  21989. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  21990. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  21991. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  21992. },
  21993. {
  21994. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  21995. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  21996. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  21997. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  21998. },
  21999. {
  22000. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  22001. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  22002. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  22003. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  22004. },
  22005. {
  22006. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  22007. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  22008. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  22009. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  22010. },
  22011. {
  22012. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  22013. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  22014. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  22015. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  22016. },
  22017. {
  22018. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  22019. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  22020. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  22021. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  22022. },
  22023. {
  22024. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  22025. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  22026. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  22027. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  22028. }
  22029. },
  22030. #endif /* WOLFSSL_SHA384 */
  22031. };
  22032. static const char protocolLabel[] = "tls13 ";
  22033. static const char ceTrafficLabel[] = "c e traffic";
  22034. static const char eExpMasterLabel[] = "e exp master";
  22035. static const char cHsTrafficLabel[] = "c hs traffic";
  22036. static const char sHsTrafficLabel[] = "s hs traffic";
  22037. static const char cAppTrafficLabel[] = "c ap traffic";
  22038. static const char sAppTrafficLabel[] = "s ap traffic";
  22039. static const char expMasterLabel[] = "exp master";
  22040. static const char resMasterLabel[] = "res master";
  22041. static const char derivedLabel[] = "derived";
  22042. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  22043. {
  22044. wc_test_ret_t ret = 0;
  22045. word32 i;
  22046. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  22047. const Tls13KdfTestVector* tv = NULL;
  22048. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  22049. byte output[WC_MAX_DIGEST_SIZE];
  22050. byte secret[WC_MAX_DIGEST_SIZE];
  22051. byte salt[WC_MAX_DIGEST_SIZE];
  22052. byte zeroes[WC_MAX_DIGEST_SIZE];
  22053. byte hashZero[WC_MAX_DIGEST_SIZE];
  22054. int hashAlgSz;
  22055. XMEMSET(zeroes, 0, sizeof zeroes);
  22056. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  22057. if (hashAlgSz == BAD_FUNC_ARG) break;
  22058. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  22059. if (ret != 0) break;
  22060. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  22061. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  22062. tv->pskSz, tv->hashAlg);
  22063. if (ret != 0) break;
  22064. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22065. secret, hashAlgSz,
  22066. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22067. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  22068. tv->hashHello1, hashAlgSz, tv->hashAlg);
  22069. if (ret != 0) break;
  22070. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  22071. if (ret != 0) break;
  22072. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22073. secret, hashAlgSz,
  22074. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22075. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  22076. tv->hashHello1, hashAlgSz, tv->hashAlg);
  22077. if (ret != 0) break;
  22078. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  22079. if (ret != 0) break;
  22080. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  22081. secret, hashAlgSz,
  22082. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22083. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  22084. hashZero, hashAlgSz, tv->hashAlg);
  22085. if (ret != 0) break;
  22086. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  22087. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  22088. tv->dheSz, tv->hashAlg);
  22089. if (ret != 0) break;
  22090. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22091. secret, hashAlgSz,
  22092. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22093. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  22094. tv->hashHello2, hashAlgSz, tv->hashAlg);
  22095. if (ret != 0) break;
  22096. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  22097. output, hashAlgSz);
  22098. if (ret != 0) break;
  22099. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22100. secret, hashAlgSz,
  22101. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22102. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  22103. tv->hashHello2, hashAlgSz, tv->hashAlg);
  22104. if (ret != 0) break;
  22105. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  22106. if (ret != 0) break;
  22107. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  22108. secret, hashAlgSz,
  22109. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22110. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  22111. hashZero, hashAlgSz, tv->hashAlg);
  22112. if (ret != 0) break;
  22113. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  22114. zeroes, hashAlgSz, tv->hashAlg);
  22115. if (ret != 0) break;
  22116. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22117. secret, hashAlgSz,
  22118. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22119. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  22120. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22121. if (ret != 0) break;
  22122. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  22123. if (ret != 0) break;
  22124. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22125. secret, hashAlgSz,
  22126. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22127. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  22128. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22129. if (ret != 0) break;
  22130. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  22131. if (ret != 0) break;
  22132. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22133. secret, hashAlgSz,
  22134. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22135. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  22136. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22137. if (ret != 0) break;
  22138. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  22139. if (ret != 0) break;
  22140. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22141. secret, hashAlgSz,
  22142. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22143. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  22144. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  22145. if (ret != 0) break;
  22146. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  22147. if (ret != 0) break;
  22148. }
  22149. return ret;
  22150. }
  22151. #endif /* WOLFSSL_TLS13 */
  22152. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  22153. * see print_fiducials() below.
  22154. */
  22155. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  22156. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  22157. {
  22158. wc_test_ret_t ret;
  22159. byte kek[128];
  22160. #ifndef NO_SHA
  22161. /* SHA-1, COUNT = 0
  22162. * shared secret length: 192
  22163. * SharedInfo length: 0
  22164. * key data length: 128
  22165. */
  22166. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  22167. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  22168. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  22169. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  22170. };
  22171. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  22172. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  22173. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  22174. };
  22175. #endif
  22176. #ifndef NO_SHA256
  22177. /* SHA-256, COUNT = 3
  22178. * shared secret length: 192
  22179. * SharedInfo length: 0
  22180. * key data length: 128
  22181. */
  22182. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  22183. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  22184. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  22185. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  22186. };
  22187. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  22188. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  22189. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  22190. };
  22191. #endif
  22192. #ifdef WOLFSSL_SHA512
  22193. /* SHA-512, COUNT = 0
  22194. * shared secret length: 192
  22195. * SharedInfo length: 0
  22196. * key data length: 128
  22197. */
  22198. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  22199. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  22200. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  22201. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  22202. };
  22203. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  22204. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  22205. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  22206. };
  22207. /* SHA-512, COUNT = 0
  22208. * shared secret length: 521
  22209. * SharedInfo length: 128
  22210. * key data length: 1024
  22211. */
  22212. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  22213. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  22214. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  22215. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  22216. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  22217. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  22218. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  22219. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  22220. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  22221. 0x5a, 0x2d
  22222. };
  22223. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  22224. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  22225. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  22226. };
  22227. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  22228. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  22229. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  22230. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  22231. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  22232. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  22233. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  22234. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  22235. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  22236. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  22237. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  22238. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  22239. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  22240. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  22241. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  22242. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  22243. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  22244. };
  22245. #endif
  22246. #ifndef NO_SHA
  22247. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  22248. kek, sizeof(verify));
  22249. if (ret != 0)
  22250. return WC_TEST_RET_ENC_EC(ret);
  22251. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  22252. return WC_TEST_RET_ENC_NC;
  22253. #endif
  22254. #ifndef NO_SHA256
  22255. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  22256. kek, sizeof(verify2));
  22257. if (ret != 0)
  22258. return WC_TEST_RET_ENC_EC(ret);
  22259. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  22260. return WC_TEST_RET_ENC_NC;
  22261. #endif
  22262. #ifdef WOLFSSL_SHA512
  22263. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  22264. kek, sizeof(verify3));
  22265. if (ret != 0)
  22266. return WC_TEST_RET_ENC_EC(ret);
  22267. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  22268. return WC_TEST_RET_ENC_NC;
  22269. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  22270. sizeof(info4), kek, sizeof(verify4));
  22271. if (ret != 0)
  22272. return WC_TEST_RET_ENC_EC(ret);
  22273. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  22274. return WC_TEST_RET_ENC_NC;
  22275. #endif
  22276. return 0;
  22277. }
  22278. #endif /* HAVE_X963_KDF */
  22279. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  22280. defined(HAVE_AESGCM)
  22281. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  22282. {
  22283. wc_test_ret_t ret = 0;
  22284. int rngRet = 0;
  22285. WC_RNG rng[1];
  22286. const char* start_text = "this is a test";
  22287. const char* info_text = "info";
  22288. const char* aad_text = "aad";
  22289. byte ciphertext[MAX_HPKE_LABEL_SZ];
  22290. byte plaintext[MAX_HPKE_LABEL_SZ];
  22291. void* receiverKey = NULL;
  22292. void* ephemeralKey = NULL;
  22293. #ifdef WOLFSSL_SMALL_STACK
  22294. byte *pubKey = NULL; /* public key */
  22295. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  22296. #else
  22297. byte pubKey[HPKE_Npk_MAX]; /* public key */
  22298. word16 pubKeySz = (word16)sizeof(pubKey);
  22299. #endif
  22300. rngRet = ret = wc_InitRng(rng);
  22301. if (ret != 0)
  22302. return WC_TEST_RET_ENC_EC(ret);
  22303. #ifdef WOLFSSL_SMALL_STACK
  22304. if (ret == 0) {
  22305. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  22306. DYNAMIC_TYPE_TMP_BUFFER);
  22307. if (pubKey == NULL)
  22308. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  22309. }
  22310. #endif
  22311. /* generate the keys */
  22312. if (ret == 0) {
  22313. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  22314. if (ret != 0)
  22315. ret = WC_TEST_RET_ENC_EC(ret);
  22316. }
  22317. if (ret == 0) {
  22318. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  22319. if (ret != 0)
  22320. ret = WC_TEST_RET_ENC_EC(ret);
  22321. }
  22322. /* seal */
  22323. if (ret == 0) {
  22324. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  22325. (byte*)info_text, (word32)XSTRLEN(info_text),
  22326. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  22327. (byte*)start_text, (word32)XSTRLEN(start_text),
  22328. ciphertext);
  22329. if (ret != 0)
  22330. ret = WC_TEST_RET_ENC_EC(ret);
  22331. }
  22332. /* export ephemeral key */
  22333. if (ret == 0) {
  22334. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  22335. if (ret != 0)
  22336. ret = WC_TEST_RET_ENC_EC(ret);
  22337. }
  22338. /* open with exported ephemeral key */
  22339. if (ret == 0) {
  22340. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  22341. (byte*)info_text, (word32)XSTRLEN(info_text),
  22342. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  22343. ciphertext, (word32)XSTRLEN(start_text),
  22344. plaintext);
  22345. if (ret != 0)
  22346. ret = WC_TEST_RET_ENC_EC(ret);
  22347. }
  22348. if (ret == 0) {
  22349. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  22350. if (ret != 0)
  22351. ret = WC_TEST_RET_ENC_NC;
  22352. }
  22353. if (ephemeralKey != NULL)
  22354. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  22355. if (receiverKey != NULL)
  22356. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  22357. #ifdef WOLFSSL_SMALL_STACK
  22358. if (pubKey != NULL)
  22359. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22360. #endif
  22361. if (rngRet == 0)
  22362. wc_FreeRng(rng);
  22363. return ret;
  22364. }
  22365. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  22366. {
  22367. wc_test_ret_t ret = 0;
  22368. Hpke hpke[1];
  22369. #if defined(HAVE_ECC)
  22370. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  22371. /* p256 */
  22372. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  22373. HPKE_AES_128_GCM, NULL);
  22374. if (ret != 0)
  22375. return WC_TEST_RET_ENC_EC(ret);
  22376. ret = hpke_test_single(hpke);
  22377. if (ret != 0)
  22378. return ret;
  22379. #endif
  22380. #if defined(WOLFSSL_SHA384) && \
  22381. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  22382. /* p384 */
  22383. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  22384. HPKE_AES_128_GCM, NULL);
  22385. if (ret != 0)
  22386. return WC_TEST_RET_ENC_EC(ret);
  22387. ret = hpke_test_single(hpke);
  22388. if (ret != 0)
  22389. return ret;
  22390. #endif
  22391. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  22392. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  22393. /* p521 */
  22394. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  22395. HPKE_AES_128_GCM, NULL);
  22396. if (ret != 0)
  22397. return WC_TEST_RET_ENC_EC(ret);
  22398. ret = hpke_test_single(hpke);
  22399. if (ret != 0)
  22400. return ret;
  22401. #endif
  22402. #endif
  22403. #if defined(HAVE_CURVE25519)
  22404. /* test with curve25519 and aes256 */
  22405. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  22406. HPKE_AES_256_GCM, NULL);
  22407. if (ret != 0)
  22408. return WC_TEST_RET_ENC_EC(ret);
  22409. ret = hpke_test_single(hpke);
  22410. if (ret != 0)
  22411. return ret;
  22412. #endif
  22413. return ret;
  22414. /* x448 and chacha20 are unimplemented */
  22415. }
  22416. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  22417. #if defined(WC_SRTP_KDF)
  22418. typedef struct Srtp_Kdf_Tv {
  22419. const unsigned char* key;
  22420. word32 keySz;
  22421. const unsigned char* salt;
  22422. word32 saltSz;
  22423. int kdfIdx;
  22424. const unsigned char* index;
  22425. const unsigned char* ke;
  22426. const unsigned char* ka;
  22427. const unsigned char* ks;
  22428. const unsigned char* index_c;
  22429. const unsigned char* ke_c;
  22430. const unsigned char* ka_c;
  22431. const unsigned char* ks_c;
  22432. word32 keSz;
  22433. word32 kaSz;
  22434. word32 ksSz;
  22435. } Srtp_Kdf_Tv;
  22436. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  22437. {
  22438. wc_test_ret_t ret = 0;
  22439. /* 128-bit key, kdrIdx = -1 */
  22440. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  22441. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  22442. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  22443. };
  22444. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  22445. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  22446. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  22447. };
  22448. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  22449. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  22450. };
  22451. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  22452. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  22453. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  22454. };
  22455. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  22456. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  22457. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  22458. 0xbd, 0x32, 0x6e, 0xe4
  22459. };
  22460. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  22461. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  22462. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  22463. };
  22464. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  22465. 0x56, 0xf3, 0xf1, 0x97
  22466. };
  22467. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  22468. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  22469. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  22470. };
  22471. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  22472. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  22473. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  22474. 0xc3, 0xd8, 0x77, 0x0e
  22475. };
  22476. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  22477. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  22478. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  22479. };
  22480. /* 192-bit key, kdrIdx = 0 */
  22481. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  22482. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  22483. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  22484. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  22485. };
  22486. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  22487. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  22488. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  22489. };
  22490. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  22491. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  22492. };
  22493. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  22494. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  22495. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  22496. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  22497. };
  22498. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  22499. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  22500. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  22501. 0xd1, 0xd1, 0x47, 0x08
  22502. };
  22503. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  22504. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  22505. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  22506. };
  22507. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  22508. 0x40, 0xbf, 0xd4, 0xa9
  22509. };
  22510. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  22511. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  22512. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  22513. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  22514. };
  22515. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  22516. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  22517. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  22518. 0xf3, 0x3b, 0x8e, 0x29
  22519. };
  22520. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  22521. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  22522. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  22523. };
  22524. /* 256-bit key, kdrIdx = 1 */
  22525. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  22526. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  22527. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  22528. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  22529. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  22530. };
  22531. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  22532. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  22533. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  22534. };
  22535. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  22536. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  22537. };
  22538. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  22539. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  22540. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  22541. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  22542. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  22543. };
  22544. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  22545. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  22546. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  22547. 0x65, 0xc7, 0x50, 0xc1
  22548. };
  22549. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  22550. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  22551. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  22552. };
  22553. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  22554. 0x31, 0x2d, 0x58, 0x15
  22555. };
  22556. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  22557. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  22558. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  22559. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  22560. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  22561. };
  22562. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  22563. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  22564. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  22565. 0x35, 0x78, 0xdf, 0xa4
  22566. };
  22567. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  22568. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  22569. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  22570. };
  22571. /* 128-bit key, kdrIdx = 8 */
  22572. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  22573. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  22574. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  22575. };
  22576. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  22577. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  22578. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  22579. };
  22580. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  22581. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  22582. };
  22583. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  22584. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  22585. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  22586. };
  22587. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  22588. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  22589. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  22590. 0xea, 0x31, 0x24, 0xe6
  22591. };
  22592. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  22593. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  22594. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  22595. };
  22596. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  22597. 0x4a, 0x7d, 0xaa, 0x85
  22598. };
  22599. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  22600. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  22601. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  22602. };
  22603. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  22604. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  22605. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  22606. 0x05, 0x18, 0xe3, 0xed
  22607. };
  22608. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  22609. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  22610. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  22611. };
  22612. #define SRTP_TV_CNT 4
  22613. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  22614. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  22615. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  22616. 16, 20, 14 },
  22617. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  22618. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  22619. 24, 20, 14 },
  22620. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  22621. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  22622. 32, 20, 14 },
  22623. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  22624. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  22625. 16, 20, 14 },
  22626. };
  22627. int i;
  22628. int idx;
  22629. unsigned char keyE[32];
  22630. unsigned char keyA[20];
  22631. unsigned char keyS[14];
  22632. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  22633. #ifndef WOLFSSL_AES_128
  22634. if (tv[i].keySz == AES_128_KEY_SIZE) {
  22635. continue;
  22636. }
  22637. #endif
  22638. #ifndef WOLFSSL_AES_192
  22639. if (tv[i].keySz == AES_192_KEY_SIZE) {
  22640. continue;
  22641. }
  22642. #endif
  22643. #ifndef WOLFSSL_AES_256
  22644. if (tv[i].keySz == AES_256_KEY_SIZE) {
  22645. continue;
  22646. }
  22647. #endif
  22648. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22649. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22650. keyS, tv[i].ksSz);
  22651. if (ret != 0)
  22652. return WC_TEST_RET_ENC_EC(ret);
  22653. if (XMEMCMP(keyE, tv[i].ke, 16) != 0)
  22654. return WC_TEST_RET_ENC_NC;
  22655. if (XMEMCMP(keyA, tv[i].ka, 20) != 0)
  22656. return WC_TEST_RET_ENC_NC;
  22657. if (XMEMCMP(keyS, tv[i].ks, 14) != 0)
  22658. return WC_TEST_RET_ENC_NC;
  22659. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22660. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22661. keyS, tv[i].ksSz);
  22662. if (ret != 0)
  22663. return WC_TEST_RET_ENC_EC(ret);
  22664. if (XMEMCMP(keyE, tv[i].ke_c, 16) != 0)
  22665. return WC_TEST_RET_ENC_NC;
  22666. if (XMEMCMP(keyA, tv[i].ka_c, 20) != 0)
  22667. return WC_TEST_RET_ENC_NC;
  22668. if (XMEMCMP(keyS, tv[i].ks_c, 14) != 0)
  22669. return WC_TEST_RET_ENC_NC;
  22670. }
  22671. #ifdef WOLFSSL_AES_128
  22672. i = 0;
  22673. #elif defined(WOLFSSL_AES_192)
  22674. i = 1;
  22675. #else
  22676. i = 2;
  22677. #endif
  22678. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  22679. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22680. keyS, tv[i].ksSz);
  22681. if (ret != BAD_FUNC_ARG)
  22682. return WC_TEST_RET_ENC_EC(ret);
  22683. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  22684. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22685. keyS, tv[i].ksSz);
  22686. if (ret != BAD_FUNC_ARG)
  22687. return WC_TEST_RET_ENC_EC(ret);
  22688. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  22689. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22690. keyS, tv[i].ksSz);
  22691. if (ret != BAD_FUNC_ARG)
  22692. return WC_TEST_RET_ENC_EC(ret);
  22693. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  22694. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22695. keyS, tv[i].ksSz);
  22696. if (ret != BAD_FUNC_ARG)
  22697. return WC_TEST_RET_ENC_EC(ret);
  22698. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  22699. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22700. keyS, tv[i].ksSz);
  22701. if (ret != BAD_FUNC_ARG)
  22702. return WC_TEST_RET_ENC_EC(ret);
  22703. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  22704. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22705. keyS, tv[i].ksSz);
  22706. if (ret != BAD_FUNC_ARG)
  22707. return WC_TEST_RET_ENC_EC(ret);
  22708. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22709. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22710. keyS, tv[i].ksSz);
  22711. if (ret != BAD_FUNC_ARG)
  22712. return WC_TEST_RET_ENC_EC(ret);
  22713. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22714. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22715. keyS, tv[i].ksSz);
  22716. if (ret != BAD_FUNC_ARG)
  22717. return WC_TEST_RET_ENC_EC(ret);
  22718. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  22719. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22720. keyS, tv[i].ksSz);
  22721. if (ret != BAD_FUNC_ARG)
  22722. return WC_TEST_RET_ENC_EC(ret);
  22723. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  22724. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22725. keyS, tv[i].ksSz);
  22726. if (ret != BAD_FUNC_ARG)
  22727. return WC_TEST_RET_ENC_EC(ret);
  22728. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22729. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22730. keyS, tv[i].ksSz);
  22731. if (ret != BAD_FUNC_ARG)
  22732. return WC_TEST_RET_ENC_EC(ret);
  22733. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22734. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22735. keyS, tv[i].ksSz);
  22736. if (ret != BAD_FUNC_ARG)
  22737. return WC_TEST_RET_ENC_EC(ret);
  22738. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22739. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22740. keyS, tv[i].ksSz);
  22741. if (ret != BAD_FUNC_ARG)
  22742. return WC_TEST_RET_ENC_EC(ret);
  22743. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22744. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22745. keyS, tv[i].ksSz);
  22746. if (ret != BAD_FUNC_ARG)
  22747. return WC_TEST_RET_ENC_EC(ret);
  22748. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22749. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  22750. keyS, tv[i].ksSz);
  22751. if (ret != 0)
  22752. return WC_TEST_RET_ENC_EC(ret);
  22753. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22754. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  22755. keyS, tv[i].ksSz);
  22756. if (ret != 0)
  22757. return WC_TEST_RET_ENC_EC(ret);
  22758. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22759. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  22760. keyS, tv[i].ksSz);
  22761. if (ret != 0)
  22762. return WC_TEST_RET_ENC_EC(ret);
  22763. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22764. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  22765. keyS, tv[i].ksSz);
  22766. if (ret != 0)
  22767. return WC_TEST_RET_ENC_EC(ret);
  22768. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22769. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22770. NULL, tv[i].ksSz);
  22771. if (ret != 0)
  22772. return WC_TEST_RET_ENC_EC(ret);
  22773. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  22774. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  22775. NULL, tv[i].ksSz);
  22776. if (ret != 0)
  22777. return WC_TEST_RET_ENC_EC(ret);
  22778. idx = wc_SRTP_KDF_kdr_to_idx(0);
  22779. if (idx != -1)
  22780. return WC_TEST_RET_ENC_NC;
  22781. for (i = 0; i < 32; i++) {
  22782. word32 kdr = 1 << i;
  22783. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  22784. if (idx != i)
  22785. return WC_TEST_RET_ENC_NC;
  22786. }
  22787. return 0;
  22788. }
  22789. #endif
  22790. #ifdef HAVE_ECC
  22791. /* size to use for ECC key gen tests */
  22792. #ifndef ECC_KEYGEN_SIZE
  22793. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  22794. #define ECC_KEYGEN_SIZE 32
  22795. #elif defined(HAVE_ECC384)
  22796. #define ECC_KEYGEN_SIZE 48
  22797. #elif defined(HAVE_ECC224)
  22798. #define ECC_KEYGEN_SIZE 28
  22799. #elif defined(HAVE_ECC521)
  22800. #define ECC_KEYGEN_SIZE 66
  22801. #else
  22802. #error No ECC keygen size defined for test
  22803. #endif
  22804. #endif
  22805. #ifdef BENCH_EMBEDDED
  22806. #define ECC_SHARED_SIZE 128
  22807. #else
  22808. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  22809. #endif
  22810. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  22811. #define HAVE_ECC_DETERMINISTIC_K
  22812. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  22813. #else
  22814. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  22815. #endif
  22816. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  22817. #ifdef NO_ECC_SECP
  22818. #define NO_ECC_VECTOR_TEST
  22819. #endif
  22820. #ifndef NO_ECC_VECTOR_TEST
  22821. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  22822. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  22823. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  22824. #define HAVE_ECC_VECTOR_TEST
  22825. #endif
  22826. #endif
  22827. #ifdef HAVE_ECC_VECTOR_TEST
  22828. typedef struct eccVector {
  22829. const char* msg; /* SHA-1 Encoded Message */
  22830. const char* Qx;
  22831. const char* Qy;
  22832. const char* d; /* Private Key */
  22833. const char* R;
  22834. const char* S;
  22835. const char* curveName;
  22836. word32 msgLen;
  22837. word32 keySize;
  22838. #ifndef NO_ASN
  22839. const byte* r;
  22840. word32 rSz;
  22841. const byte* s;
  22842. word32 sSz;
  22843. #endif
  22844. } eccVector;
  22845. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22846. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  22847. {
  22848. wc_test_ret_t ret = 0;
  22849. int verify = 0;
  22850. word32 sigSz;
  22851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22852. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22853. #else
  22854. ecc_key userA[1];
  22855. #endif
  22856. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  22857. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  22858. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  22859. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  22860. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  22861. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  22862. #endif
  22863. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  22864. if (sig == NULL)
  22865. ERROR_OUT(MEMORY_E, done);
  22866. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  22867. if (sigRaw == NULL || r == NULL || s == NULL)
  22868. ERROR_OUT(MEMORY_E, done);
  22869. #endif
  22870. #endif
  22871. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22872. if (userA == NULL)
  22873. ERROR_OUT(MEMORY_E, done);
  22874. #endif
  22875. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  22876. if (ret != 0)
  22877. goto done;
  22878. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  22879. vector->d, vector->curveName);
  22880. if (ret != 0)
  22881. goto done;
  22882. XMEMSET(sig, 0, ECC_SIG_SIZE);
  22883. sigSz = ECC_SIG_SIZE;
  22884. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  22885. if (ret != 0)
  22886. goto done;
  22887. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  22888. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  22889. sigRawSz = ECC_SIG_SIZE;
  22890. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  22891. sigRaw, &sigRawSz);
  22892. if (ret != 0)
  22893. goto done;
  22894. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  22895. ret = WC_TEST_RET_ENC_NC;
  22896. goto done;
  22897. }
  22898. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  22899. if (ret != 0)
  22900. goto done;
  22901. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  22902. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  22903. ret = WC_TEST_RET_ENC_NC;
  22904. goto done;
  22905. }
  22906. #endif
  22907. #ifdef HAVE_ECC_VERIFY
  22908. do {
  22909. #if defined(WOLFSSL_ASYNC_CRYPT)
  22910. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22911. #endif
  22912. if (ret == 0)
  22913. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  22914. vector->msgLen, &verify, userA);
  22915. } while (ret == WC_PENDING_E);
  22916. if (ret != 0)
  22917. goto done;
  22918. TEST_SLEEP();
  22919. if (verify != 1)
  22920. ret = WC_TEST_RET_ENC_NC;
  22921. #endif
  22922. done:
  22923. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22924. if (userA != NULL) {
  22925. wc_ecc_free(userA);
  22926. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22927. }
  22928. #else
  22929. wc_ecc_free(userA);
  22930. #endif
  22931. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  22932. WC_FREE_VAR(sigRaw, HEAP_HINT);
  22933. WC_FREE_VAR(r, HEAP_HINT);
  22934. WC_FREE_VAR(s, HEAP_HINT);
  22935. #endif
  22936. WC_FREE_VAR(sig, HEAP_HINT);
  22937. return ret;
  22938. }
  22939. static wc_test_ret_t ecc_test_vector(int keySize)
  22940. {
  22941. wc_test_ret_t ret;
  22942. eccVector vec;
  22943. XMEMSET(&vec, 0, sizeof(vec));
  22944. vec.keySize = (word32)keySize;
  22945. switch(keySize) {
  22946. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  22947. case 14:
  22948. return 0;
  22949. #endif /* HAVE_ECC112 */
  22950. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  22951. case 16:
  22952. return 0;
  22953. #endif /* HAVE_ECC128 */
  22954. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  22955. case 20:
  22956. return 0;
  22957. #endif /* HAVE_ECC160 */
  22958. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  22959. case 24:
  22960. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22961. #if 1
  22962. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  22963. vec.msgLen = 20;
  22964. #else
  22965. /* This is the raw message prior to SHA-1 */
  22966. vec.msg =
  22967. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  22968. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  22969. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  22970. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  22971. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  22972. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  22973. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  22974. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  22975. vec.msgLen = 128;
  22976. #endif
  22977. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  22978. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  22979. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  22980. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22981. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22982. vec.curveName = "SECP192R1";
  22983. #ifndef NO_ASN
  22984. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  22985. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  22986. vec.rSz = 24;
  22987. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  22988. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  22989. vec.sSz = 24;
  22990. #endif
  22991. break;
  22992. #endif /* HAVE_ECC192 */
  22993. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  22994. case 28:
  22995. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  22996. #if 1
  22997. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  22998. vec.msgLen = 20;
  22999. #else
  23000. /* This is the raw message prior to SHA-1 */
  23001. vec.msg =
  23002. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  23003. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  23004. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  23005. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  23006. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  23007. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  23008. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  23009. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  23010. vec.msgLen = 128;
  23011. #endif
  23012. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  23013. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  23014. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  23015. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  23016. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  23017. vec.curveName = "SECP224R1";
  23018. #ifndef NO_ASN
  23019. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  23020. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  23021. "\xbc\x16\x71\xa7";
  23022. vec.rSz = 28;
  23023. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  23024. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  23025. "\x6a\xf3\xad\x5b";
  23026. vec.sSz = 28;
  23027. #endif
  23028. break;
  23029. #endif /* HAVE_ECC224 */
  23030. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  23031. case 30:
  23032. return 0;
  23033. #endif /* HAVE_ECC239 */
  23034. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  23035. case 32:
  23036. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  23037. #if 1
  23038. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  23039. vec.msgLen = 20;
  23040. #else
  23041. /* This is the raw message prior to SHA-1 */
  23042. vec.msg =
  23043. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  23044. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  23045. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  23046. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  23047. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  23048. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  23049. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  23050. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  23051. vec.msgLen = 128;
  23052. #endif
  23053. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  23054. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  23055. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  23056. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  23057. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  23058. #ifndef NO_ASN
  23059. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  23060. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  23061. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  23062. vec.rSz = 32;
  23063. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  23064. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  23065. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  23066. vec.sSz = 32;
  23067. #endif
  23068. vec.curveName = "SECP256R1";
  23069. break;
  23070. #endif /* !NO_ECC256 */
  23071. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  23072. case 40:
  23073. return 0;
  23074. #endif /* HAVE_ECC320 */
  23075. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  23076. case 48:
  23077. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  23078. #if 1
  23079. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  23080. vec.msgLen = 20;
  23081. #else
  23082. /* This is the raw message prior to SHA-1 */
  23083. vec.msg =
  23084. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  23085. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  23086. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  23087. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  23088. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  23089. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  23090. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  23091. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  23092. vec.msgLen = 128;
  23093. #endif
  23094. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  23095. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  23096. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  23097. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  23098. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  23099. vec.curveName = "SECP384R1";
  23100. #ifndef NO_ASN
  23101. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  23102. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  23103. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  23104. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  23105. vec.rSz = 48;
  23106. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  23107. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  23108. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  23109. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  23110. vec.sSz = 48;
  23111. #endif
  23112. break;
  23113. #endif /* HAVE_ECC384 */
  23114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  23115. case 64:
  23116. return 0;
  23117. #endif /* HAVE_ECC512 */
  23118. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  23119. case 66:
  23120. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  23121. #if 1
  23122. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  23123. vec.msgLen = 20;
  23124. #else
  23125. /* This is the raw message prior to SHA-1 */
  23126. vec.msg =
  23127. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  23128. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  23129. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  23130. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  23131. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  23132. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  23133. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  23134. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  23135. vec.msgLen = 128;
  23136. #endif
  23137. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  23138. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  23139. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  23140. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  23141. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  23142. vec.curveName = "SECP521R1";
  23143. #ifndef NO_ASN
  23144. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  23145. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  23146. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  23147. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  23148. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  23149. "\xa3\xbb\x15\x21\xbe";
  23150. vec.rSz = 65;
  23151. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  23152. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  23153. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  23154. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  23155. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  23156. "\x3d\x22\xf2\x48\x0c";
  23157. vec.sSz = 65;
  23158. #endif
  23159. break;
  23160. #endif /* HAVE_ECC521 */
  23161. default:
  23162. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  23163. }; /* Switch */
  23164. ret = ecc_test_vector_item(&vec);
  23165. if (ret < 0) {
  23166. return ret;
  23167. }
  23168. return 0;
  23169. }
  23170. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23171. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23172. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23173. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23174. #if defined(HAVE_ECC256)
  23175. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  23176. {
  23177. wc_test_ret_t ret;
  23178. #ifdef WOLFSSL_SMALL_STACK
  23179. ecc_key *key = NULL;
  23180. #else
  23181. ecc_key key[1];
  23182. #endif
  23183. int key_inited = 0;
  23184. byte sig[72];
  23185. word32 sigSz;
  23186. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23187. unsigned char hash[32];
  23188. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23189. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  23190. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23191. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  23192. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23193. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  23194. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  23195. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  23196. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  23197. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  23198. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  23199. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  23200. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  23201. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  23202. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  23203. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  23204. };
  23205. #ifdef WOLFSSL_SMALL_STACK
  23206. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23207. if (key == NULL)
  23208. return MEMORY_E;
  23209. #endif
  23210. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23211. if (ret != 0) {
  23212. goto done;
  23213. }
  23214. key_inited = 1;
  23215. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  23216. if (ret != 0) {
  23217. goto done;
  23218. }
  23219. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  23220. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  23221. if (ret != 0) {
  23222. goto done;
  23223. }
  23224. ret = wc_ecc_set_deterministic(key, 1);
  23225. if (ret != 0) {
  23226. goto done;
  23227. }
  23228. sigSz = sizeof(sig);
  23229. do {
  23230. #if defined(WOLFSSL_ASYNC_CRYPT)
  23231. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23232. #endif
  23233. if (ret == 0)
  23234. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23235. } while (ret == WC_PENDING_E);
  23236. if (ret != 0) {
  23237. goto done;
  23238. }
  23239. TEST_SLEEP();
  23240. if (sigSz != sizeof(expSig)) {
  23241. ret = WC_TEST_RET_ENC_NC;
  23242. goto done;
  23243. }
  23244. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  23245. ret = WC_TEST_RET_ENC_NC;
  23246. goto done;
  23247. }
  23248. sigSz = sizeof(sig);
  23249. do {
  23250. #if defined(WOLFSSL_ASYNC_CRYPT)
  23251. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23252. #endif
  23253. if (ret == 0)
  23254. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23255. } while (ret == WC_PENDING_E);
  23256. if (ret != 0) {
  23257. goto done;
  23258. }
  23259. TEST_SLEEP();
  23260. done:
  23261. if (key_inited)
  23262. wc_ecc_free(key);
  23263. #ifdef WOLFSSL_SMALL_STACK
  23264. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23265. #endif
  23266. return ret;
  23267. }
  23268. #endif
  23269. #ifdef WOLFSSL_PUBLIC_MP
  23270. #if defined(HAVE_ECC384)
  23271. /* KAT from RFC6979 */
  23272. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  23273. {
  23274. wc_test_ret_t ret;
  23275. #ifdef WOLFSSL_SMALL_STACK
  23276. ecc_key *key;
  23277. mp_int *r, *s, *expR, *expS;
  23278. #else
  23279. ecc_key key[1];
  23280. mp_int r[1], s[1], expR[1], expS[1];
  23281. #endif
  23282. int key_inited = 0;
  23283. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23284. unsigned char hash[32];
  23285. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23286. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  23287. "96D5724E4C70A825F872C9EA60D2EDF5";
  23288. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23289. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  23290. "DEF8F0EA9055866064A254515480BC13";
  23291. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23292. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  23293. "288B231C3AE0D4FE7344FD2533264720";
  23294. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  23295. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  23296. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  23297. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  23298. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  23299. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  23300. #ifdef WOLFSSL_SMALL_STACK
  23301. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23302. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23303. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23304. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23305. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23306. if ((key == NULL) ||
  23307. (r == NULL) ||
  23308. (s == NULL) ||
  23309. (expR == NULL) ||
  23310. (expS == NULL))
  23311. {
  23312. ret = MEMORY_E;
  23313. goto done;
  23314. }
  23315. #endif
  23316. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  23317. if (ret != MP_OKAY) {
  23318. goto done;
  23319. }
  23320. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23321. if (ret != 0) {
  23322. goto done;
  23323. }
  23324. key_inited = 1;
  23325. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  23326. if (ret != 0) {
  23327. goto done;
  23328. }
  23329. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  23330. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  23331. if (ret != 0) {
  23332. goto done;
  23333. }
  23334. ret = wc_ecc_set_deterministic(key, 1);
  23335. if (ret != 0) {
  23336. goto done;
  23337. }
  23338. do {
  23339. #if defined(WOLFSSL_ASYNC_CRYPT)
  23340. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23341. #endif
  23342. if (ret == 0)
  23343. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  23344. } while (ret == WC_PENDING_E);
  23345. if (ret != 0) {
  23346. goto done;
  23347. }
  23348. TEST_SLEEP();
  23349. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  23350. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  23351. if (mp_cmp(r, expR) != MP_EQ) {
  23352. ret = WC_TEST_RET_ENC_NC;
  23353. }
  23354. done:
  23355. if (key_inited)
  23356. wc_ecc_free(key);
  23357. #ifdef WOLFSSL_SMALL_STACK
  23358. if (key != NULL)
  23359. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23360. if (r != NULL)
  23361. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23362. if (s != NULL)
  23363. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23364. if (expR != NULL)
  23365. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23366. if (expS != NULL)
  23367. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23368. #endif
  23369. return ret;
  23370. }
  23371. #endif /* HAVE_ECC384 */
  23372. #if defined(HAVE_ECC521)
  23373. /* KAT from RFC6979 */
  23374. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  23375. {
  23376. wc_test_ret_t ret;
  23377. #ifdef WOLFSSL_SMALL_STACK
  23378. ecc_key *key;
  23379. mp_int *r, *s, *expR, *expS;
  23380. #else
  23381. ecc_key key[1];
  23382. mp_int r[1], s[1], expR[1], expS[1];
  23383. #endif
  23384. int key_inited = 0;
  23385. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23386. unsigned char hash[32];
  23387. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23388. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  23389. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  23390. "538";
  23391. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23392. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  23393. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  23394. "3A4";
  23395. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23396. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  23397. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  23398. "CF5";
  23399. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  23400. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  23401. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  23402. "1A7";
  23403. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  23404. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  23405. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  23406. "CFC";
  23407. #ifdef WOLFSSL_SMALL_STACK
  23408. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23409. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23410. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23411. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23412. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23413. if ((key == NULL) ||
  23414. (r == NULL) ||
  23415. (s == NULL) ||
  23416. (expR == NULL) ||
  23417. (expS == NULL))
  23418. {
  23419. ret = MEMORY_E;
  23420. goto done;
  23421. }
  23422. #endif
  23423. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  23424. if (ret != MP_OKAY) {
  23425. goto done;
  23426. }
  23427. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23428. if (ret != 0) {
  23429. return WC_TEST_RET_ENC_EC(ret);
  23430. }
  23431. key_inited = 1;
  23432. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  23433. if (ret != 0) {
  23434. goto done;
  23435. }
  23436. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  23437. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  23438. if (ret != 0) {
  23439. goto done;
  23440. }
  23441. ret = wc_ecc_set_deterministic(key, 1);
  23442. if (ret != 0) {
  23443. goto done;
  23444. }
  23445. do {
  23446. #if defined(WOLFSSL_ASYNC_CRYPT)
  23447. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23448. #endif
  23449. if (ret == 0)
  23450. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  23451. } while (ret == WC_PENDING_E);
  23452. if (ret != 0) {
  23453. goto done;
  23454. }
  23455. TEST_SLEEP();
  23456. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  23457. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  23458. if (mp_cmp(r, expR) != MP_EQ) {
  23459. ret = WC_TEST_RET_ENC_NC;
  23460. }
  23461. done:
  23462. if (key_inited)
  23463. wc_ecc_free(key);
  23464. #ifdef WOLFSSL_SMALL_STACK
  23465. if (key != NULL)
  23466. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23467. if (r != NULL)
  23468. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23469. if (s != NULL)
  23470. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23471. if (expR != NULL)
  23472. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23473. if (expS != NULL)
  23474. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23475. #endif
  23476. return ret;
  23477. }
  23478. #endif /* HAVE_ECC521 */
  23479. #endif /* WOLFSSL_PUBLIC_MP */
  23480. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  23481. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  23482. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  23483. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23484. !defined(WOLFSSL_KCAPI_ECC)
  23485. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  23486. {
  23487. wc_test_ret_t ret;
  23488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23489. ecc_key *key = NULL;
  23490. #else
  23491. ecc_key key[1];
  23492. #endif
  23493. int key_inited = 0;
  23494. byte sig[72];
  23495. word32 sigSz;
  23496. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  23497. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  23498. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  23499. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  23500. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  23501. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  23502. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  23503. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  23504. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  23505. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  23506. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  23507. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  23508. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  23509. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  23510. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  23511. };
  23512. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23513. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  23514. return MEMORY_E;
  23515. #endif
  23516. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23517. if (ret != 0) {
  23518. goto done;
  23519. }
  23520. key_inited = 1;
  23521. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  23522. if (ret != 0) {
  23523. goto done;
  23524. }
  23525. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  23526. && (HAVE_FIPS_VERSION > 2)))
  23527. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  23528. #endif
  23529. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  23530. if (ret != 0) {
  23531. goto done;
  23532. }
  23533. sigSz = sizeof(sig);
  23534. do {
  23535. #if defined(WOLFSSL_ASYNC_CRYPT)
  23536. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23537. #endif
  23538. if (ret == 0)
  23539. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23540. } while (ret == WC_PENDING_E);
  23541. if (ret != 0) {
  23542. goto done;
  23543. }
  23544. TEST_SLEEP();
  23545. if (sigSz != sizeof(expSig)) {
  23546. ret = WC_TEST_RET_ENC_NC;
  23547. goto done;
  23548. }
  23549. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  23550. ret = WC_TEST_RET_ENC_NC;
  23551. goto done;
  23552. }
  23553. sigSz = sizeof(sig);
  23554. do {
  23555. #if defined(WOLFSSL_ASYNC_CRYPT)
  23556. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23557. #endif
  23558. if (ret == 0)
  23559. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23560. } while (ret == WC_PENDING_E);
  23561. if (ret != 0) {
  23562. goto done;
  23563. }
  23564. TEST_SLEEP();
  23565. done:
  23566. if (key_inited)
  23567. wc_ecc_free(key);
  23568. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23569. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23570. #endif
  23571. return ret;
  23572. }
  23573. #endif
  23574. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  23575. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  23576. {
  23577. wc_test_ret_t ret;
  23578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23579. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23580. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23581. #else
  23582. ecc_key pub_key[1], priv_key[1];
  23583. #endif
  23584. byte sharedA[32] = {0}, sharedB[32] = {0};
  23585. word32 x, z;
  23586. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  23587. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  23588. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  23589. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  23590. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  23591. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  23592. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23593. if ((pub_key == NULL) ||
  23594. (priv_key == NULL)) {
  23595. ret = MEMORY_E;
  23596. goto done;
  23597. }
  23598. #endif
  23599. XMEMSET(pub_key, 0, sizeof *pub_key);
  23600. XMEMSET(priv_key, 0, sizeof *priv_key);
  23601. /* setup private and public keys */
  23602. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  23603. if (ret != 0)
  23604. goto done;
  23605. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  23606. if (ret != 0)
  23607. goto done;
  23608. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  23609. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  23610. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  23611. if (ret != 0)
  23612. goto done;
  23613. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  23614. if (ret != 0)
  23615. goto done;
  23616. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23617. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23618. !defined(HAVE_SELFTEST)
  23619. ret = wc_ecc_set_rng(priv_key, rng);
  23620. if (ret != 0)
  23621. goto done;
  23622. #else
  23623. (void)rng;
  23624. #endif
  23625. /* compute ECC Cofactor shared secret */
  23626. x = sizeof(sharedA);
  23627. do {
  23628. #if defined(WOLFSSL_ASYNC_CRYPT)
  23629. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23630. #endif
  23631. if (ret == 0)
  23632. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  23633. } while (ret == WC_PENDING_E);
  23634. if (ret != 0) {
  23635. goto done;
  23636. }
  23637. TEST_SLEEP();
  23638. /* read in expected Z */
  23639. z = sizeof(sharedB);
  23640. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  23641. if (ret != 0)
  23642. goto done;
  23643. /* compare results */
  23644. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  23645. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23646. }
  23647. done:
  23648. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23649. if (priv_key) {
  23650. wc_ecc_free(priv_key);
  23651. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23652. }
  23653. if (pub_key) {
  23654. wc_ecc_free(pub_key);
  23655. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23656. }
  23657. #else
  23658. wc_ecc_free(priv_key);
  23659. wc_ecc_free(pub_key);
  23660. #endif
  23661. return ret;
  23662. }
  23663. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  23664. #endif /* HAVE_ECC_VECTOR_TEST */
  23665. #ifdef HAVE_ECC_KEY_IMPORT
  23666. /* returns 0 on success */
  23667. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  23668. {
  23669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23670. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23671. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  23672. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23673. #endif
  23674. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23675. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23676. #else
  23677. ecc_key key[1];
  23678. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  23679. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23680. ecc_key pub[1];
  23681. #endif
  23682. byte exportBuf[ECC_BUFSIZE];
  23683. byte tmp[ECC_BUFSIZE];
  23684. #endif
  23685. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  23686. word32 x;
  23687. word32 tmpSz;
  23688. wc_test_ret_t ret = 0;
  23689. ecc_point* pubPoint = NULL;
  23690. #ifdef HAVE_ECC_VERIFY
  23691. int verify = 0;
  23692. #endif
  23693. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23694. if ((key == NULL) ||
  23695. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  23696. (pub == NULL) ||
  23697. #endif
  23698. (exportBuf == NULL) ||
  23699. (tmp == NULL))
  23700. ERROR_OUT(MEMORY_E, done);
  23701. #endif
  23702. (void)msg;
  23703. (void)verify;
  23704. (void)exportBuf;
  23705. (void)rng;
  23706. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23707. #ifndef NO_ECC256
  23708. #ifdef USE_CERT_BUFFERS_256
  23709. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  23710. tmpSz = (size_t)sizeof_ecc_key_der_256;
  23711. #else
  23712. {
  23713. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  23714. if (!file) {
  23715. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23716. }
  23717. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  23718. XFCLOSE(file);
  23719. if (tmpSz == 0)
  23720. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23721. }
  23722. #endif /* USE_CERT_BUFFERS_256 */
  23723. /* import private only then test with */
  23724. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  23725. if (ret == 0) {
  23726. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23727. }
  23728. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  23729. if (ret == 0) {
  23730. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23731. }
  23732. x = 0;
  23733. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  23734. if (ret != 0)
  23735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23736. #ifdef HAVE_ECC_KEY_EXPORT
  23737. x = ECC_BUFSIZE;
  23738. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  23739. if (ret != 0)
  23740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23741. /* make private only key */
  23742. wc_ecc_free(key);
  23743. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23744. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  23745. if (ret != 0)
  23746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23747. x = ECC_BUFSIZE;
  23748. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  23749. if (ret == 0) {
  23750. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23751. }
  23752. #endif /* HAVE_ECC_KEY_EXPORT */
  23753. ret = wc_ecc_make_pub(NULL, NULL);
  23754. if (ret == 0) {
  23755. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23756. }
  23757. TEST_SLEEP();
  23758. #ifndef WOLFSSL_NO_MALLOC
  23759. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  23760. if (pubPoint == NULL) {
  23761. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23762. }
  23763. #if !defined(WOLFSSL_CRYPTOCELL)
  23764. ret = wc_ecc_make_pub(key, pubPoint);
  23765. #if defined(WOLFSSL_ASYNC_CRYPT)
  23766. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  23767. #endif
  23768. if (ret != 0)
  23769. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23770. #endif
  23771. TEST_SLEEP();
  23772. #ifdef HAVE_ECC_KEY_EXPORT
  23773. /* export should still fail, is private only key */
  23774. x = ECC_BUFSIZE;
  23775. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  23776. if (ret == 0) {
  23777. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23778. }
  23779. #endif /* HAVE_ECC_KEY_EXPORT */
  23780. #endif /* !WOLFSSL_NO_MALLOC */
  23781. #endif /* !NO_ECC256 */
  23782. /* create a new key since above test for loading key is not supported */
  23783. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  23784. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  23785. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  23786. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  23787. if (ret != 0)
  23788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23789. #endif
  23790. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  23791. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  23792. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  23793. tmpSz = ECC_BUFSIZE;
  23794. ret = 0;
  23795. do {
  23796. #if defined(WOLFSSL_ASYNC_CRYPT)
  23797. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23798. #endif
  23799. if (ret == 0) {
  23800. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  23801. &tmpSz, rng, key);
  23802. }
  23803. } while (ret == WC_PENDING_E);
  23804. if (ret != 0)
  23805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23806. TEST_SLEEP();
  23807. #ifdef HAVE_ECC_VERIFY
  23808. /* try verify with private only key */
  23809. ret = 0;
  23810. do {
  23811. #if defined(WOLFSSL_ASYNC_CRYPT)
  23812. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23813. #endif
  23814. if (ret == 0) {
  23815. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  23816. (word32)XSTRLEN((const char*)msg), &verify, key);
  23817. }
  23818. } while (ret == WC_PENDING_E);
  23819. if (ret != 0)
  23820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23821. if (verify != 1) {
  23822. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23823. }
  23824. TEST_SLEEP();
  23825. #ifdef HAVE_ECC_KEY_EXPORT
  23826. /* exporting the public part should now work */
  23827. x = ECC_BUFSIZE;
  23828. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  23829. if (ret != 0)
  23830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23831. #endif /* HAVE_ECC_KEY_EXPORT */
  23832. #endif /* HAVE_ECC_VERIFY */
  23833. #endif /* HAVE_ECC_SIGN */
  23834. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23835. /* now test private only key with creating a shared secret */
  23836. x = ECC_BUFSIZE;
  23837. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  23838. if (ret != 0)
  23839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23840. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  23841. /* make private only key */
  23842. wc_ecc_free(key);
  23843. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23844. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  23845. if (ret != 0)
  23846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23847. /* check that public export fails with private only key */
  23848. x = ECC_BUFSIZE;
  23849. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  23850. if (ret == 0) {
  23851. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23852. }
  23853. #endif /* WOLFSSL_QNX_CAAM */
  23854. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  23855. /* make public key for shared secret */
  23856. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  23857. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  23858. #ifdef HAVE_ECC_CDH
  23859. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  23860. #endif
  23861. #if defined(WOLFSSL_ASYNC_CRYPT)
  23862. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  23863. #endif
  23864. if (ret != 0)
  23865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23866. TEST_SLEEP();
  23867. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23868. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23869. !defined(HAVE_SELFTEST)
  23870. ret = wc_ecc_set_rng(key, rng);
  23871. if (ret != 0)
  23872. goto done;
  23873. #endif
  23874. x = ECC_BUFSIZE;
  23875. do {
  23876. #if defined(WOLFSSL_ASYNC_CRYPT)
  23877. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23878. #endif
  23879. if (ret == 0) {
  23880. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  23881. }
  23882. } while (ret == WC_PENDING_E);
  23883. wc_ecc_free(pub);
  23884. if (ret != 0)
  23885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23886. TEST_SLEEP();
  23887. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  23888. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23889. ret = 0;
  23890. done:
  23891. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  23892. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23893. if (key != NULL) {
  23894. wc_ecc_free(key);
  23895. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23896. }
  23897. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  23898. if (pub != NULL)
  23899. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23900. #endif
  23901. if (exportBuf != NULL)
  23902. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23903. if (tmp != NULL)
  23904. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23905. #else
  23906. wc_ecc_free(key);
  23907. #endif
  23908. return ret;
  23909. }
  23910. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  23911. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23912. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  23913. {
  23914. wc_test_ret_t ret;
  23915. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23916. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23917. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23918. #else
  23919. ecc_key eccKey[1];
  23920. byte tmpBuf[ECC_BUFSIZE];
  23921. #endif
  23922. word32 tmpSz;
  23923. word32 idx;
  23924. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23925. if ((eccKey == NULL) || (tmpBuf == NULL))
  23926. ERROR_OUT(MEMORY_E, done);
  23927. #endif
  23928. ret = wc_ecc_init(eccKey);
  23929. if (ret != 0) {
  23930. goto done;
  23931. }
  23932. ret = wc_ecc_make_key(rng, keySize, eccKey);
  23933. #if defined(WOLFSSL_ASYNC_CRYPT)
  23934. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  23935. #endif
  23936. if (ret != 0) {
  23937. goto done;
  23938. }
  23939. tmpSz = ECC_BUFSIZE;
  23940. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  23941. wc_ecc_free(eccKey);
  23942. if (ret < 0) {
  23943. goto done;
  23944. }
  23945. tmpSz = (word32)ret;
  23946. ret = wc_ecc_init(eccKey);
  23947. if (ret != 0) {
  23948. goto done;
  23949. }
  23950. idx = 0;
  23951. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  23952. if (ret != 0) {
  23953. goto done;
  23954. }
  23955. wc_ecc_free(eccKey);
  23956. ret = wc_ecc_init(eccKey);
  23957. if (ret != 0) {
  23958. goto done;
  23959. }
  23960. idx = 0;
  23961. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  23962. if (ret != 0) {
  23963. goto done;
  23964. }
  23965. ret = 0;
  23966. done:
  23967. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23968. if (eccKey != NULL) {
  23969. wc_ecc_free(eccKey);
  23970. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23971. }
  23972. if (tmpBuf != NULL)
  23973. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23974. #else
  23975. wc_ecc_free(eccKey);
  23976. #endif
  23977. return ret;
  23978. }
  23979. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  23980. #endif /* HAVE_ECC_KEY_IMPORT */
  23981. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  23982. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23983. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  23984. {
  23985. wc_test_ret_t ret = 0;
  23986. int derSz;
  23987. #ifdef HAVE_PKCS8
  23988. word32 pkcs8Sz;
  23989. #endif
  23990. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23991. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23992. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23993. #else
  23994. byte der[ECC_BUFSIZE];
  23995. ecc_key userA[1];
  23996. #endif
  23997. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23998. if ((der == NULL) || (userA == NULL))
  23999. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  24000. #endif
  24001. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24002. if (ret != 0)
  24003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24004. ret = wc_ecc_make_key(rng, keySize, userA);
  24005. #if defined(WOLFSSL_ASYNC_CRYPT)
  24006. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  24007. #endif
  24008. if (ret != 0)
  24009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24010. TEST_SLEEP();
  24011. ret = wc_ecc_check_key(userA);
  24012. if (ret != 0)
  24013. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24014. TEST_SLEEP();
  24015. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  24016. if (derSz < 0) {
  24017. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24018. }
  24019. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  24020. ECC_PRIVATEKEY_TYPE);
  24021. if (ret != 0)
  24022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24023. /* test export of public key */
  24024. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  24025. if (derSz < 0) {
  24026. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24027. }
  24028. if (derSz == 0) {
  24029. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24030. }
  24031. #ifdef HAVE_COMP_KEY
  24032. /* test export of compressed public key */
  24033. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  24034. if (derSz < 0) {
  24035. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24036. }
  24037. if (derSz == 0) {
  24038. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24039. }
  24040. #endif
  24041. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  24042. if (ret != 0)
  24043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24044. #ifdef HAVE_PKCS8
  24045. /* test export of PKCS#8 unencrypted private key */
  24046. pkcs8Sz = FOURK_BUF;
  24047. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  24048. if (derSz < 0) {
  24049. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24050. }
  24051. if (derSz == 0) {
  24052. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24053. }
  24054. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  24055. if (ret != 0) {
  24056. goto done;
  24057. }
  24058. #endif /* HAVE_PKCS8 */
  24059. done:
  24060. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24061. if (der != NULL)
  24062. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24063. if (userA != NULL) {
  24064. wc_ecc_free(userA);
  24065. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24066. }
  24067. #else
  24068. wc_ecc_free(userA);
  24069. #endif
  24070. return ret;
  24071. }
  24072. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  24073. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  24074. int curve_id, const ecc_set_type* dp)
  24075. {
  24076. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  24077. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24078. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24079. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24080. word32 y;
  24081. #endif
  24082. #ifdef HAVE_ECC_KEY_EXPORT
  24083. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  24084. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  24085. #endif
  24086. word32 x = 0;
  24087. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24088. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  24089. defined(HAVE_ECC_SIGN)
  24090. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  24091. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  24092. int i;
  24093. #ifdef HAVE_ECC_VERIFY
  24094. int verify;
  24095. #endif /* HAVE_ECC_VERIFY */
  24096. #endif /* HAVE_ECC_SIGN */
  24097. wc_test_ret_t ret;
  24098. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24099. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24100. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24101. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24102. #else
  24103. ecc_key userA[1];
  24104. ecc_key userB[1];
  24105. ecc_key pubKey[1];
  24106. #endif
  24107. #ifndef WC_NO_RNG
  24108. int curveSize;
  24109. #endif
  24110. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  24111. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  24112. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24113. if (sharedA == NULL || sharedB == NULL)
  24114. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24115. #endif
  24116. #ifdef HAVE_ECC_KEY_EXPORT
  24117. if (exportBuf == NULL)
  24118. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24119. #endif
  24120. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24121. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  24122. defined(HAVE_ECC_SIGN)
  24123. if (sig == NULL || digest == NULL)
  24124. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24125. #endif
  24126. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  24127. (void)testVerifyCount;
  24128. (void)dp;
  24129. (void)x;
  24130. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24131. if ((userA == NULL) ||
  24132. (userB == NULL) ||
  24133. (pubKey == NULL))
  24134. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24135. #endif
  24136. XMEMSET(userA, 0, sizeof *userA);
  24137. XMEMSET(userB, 0, sizeof *userB);
  24138. XMEMSET(pubKey, 0, sizeof *pubKey);
  24139. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24140. if (ret != 0)
  24141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24142. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  24143. if (ret != 0)
  24144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24145. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24146. if (ret != 0)
  24147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24148. #ifdef WOLFSSL_CUSTOM_CURVES
  24149. if (dp != NULL) {
  24150. ret = wc_ecc_set_custom_curve(userA, dp);
  24151. if (ret != 0)
  24152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24153. ret = wc_ecc_set_custom_curve(userB, dp);
  24154. if (ret != 0)
  24155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24156. }
  24157. #endif
  24158. #ifndef WC_NO_RNG
  24159. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  24160. #if defined(WOLFSSL_ASYNC_CRYPT)
  24161. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  24162. #endif
  24163. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  24164. if (ret == NO_VALID_DEVID) {
  24165. ret = 0;
  24166. goto done; /* no software case */
  24167. }
  24168. #endif
  24169. if (ret == ECC_CURVE_OID_E)
  24170. goto done; /* catch case, where curve is not supported */
  24171. if (ret != 0)
  24172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24173. TEST_SLEEP();
  24174. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  24175. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  24176. if (curveSize != userA->dp->size)
  24177. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24178. }
  24179. ret = wc_ecc_check_key(userA);
  24180. if (ret != 0)
  24181. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24182. TEST_SLEEP();
  24183. /* ATECC508/608 configuration may not support more than one ECDH key */
  24184. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24185. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  24186. #if defined(WOLFSSL_ASYNC_CRYPT)
  24187. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  24188. #endif
  24189. if (ret != 0)
  24190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24191. TEST_SLEEP();
  24192. /* only perform the below tests if the key size matches */
  24193. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  24194. /* Not an error, just not a key size match */
  24195. WARNING_OUT(ECC_CURVE_OID_E, done);
  24196. #ifdef HAVE_ECC_DHE
  24197. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24198. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24199. !defined(HAVE_SELFTEST)
  24200. ret = wc_ecc_set_rng(userA, rng);
  24201. if (ret != 0)
  24202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24203. ret = wc_ecc_set_rng(userB, rng);
  24204. if (ret != 0)
  24205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24206. #endif
  24207. x = ECC_SHARED_SIZE;
  24208. do {
  24209. #if defined(WOLFSSL_ASYNC_CRYPT)
  24210. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24211. #endif
  24212. if (ret == 0)
  24213. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  24214. } while (ret == WC_PENDING_E);
  24215. if (ret != 0)
  24216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24217. TEST_SLEEP();
  24218. y = ECC_SHARED_SIZE;
  24219. do {
  24220. #if defined(WOLFSSL_ASYNC_CRYPT)
  24221. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24222. #endif
  24223. if (ret == 0)
  24224. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  24225. } while (ret == WC_PENDING_E);
  24226. if (ret != 0)
  24227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24228. if (y != x)
  24229. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24230. if (XMEMCMP(sharedA, sharedB, x))
  24231. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24232. TEST_SLEEP();
  24233. #ifdef HAVE_ECC_CDH
  24234. /* add cofactor flag */
  24235. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  24236. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  24237. x = ECC_SHARED_SIZE;
  24238. do {
  24239. #if defined(WOLFSSL_ASYNC_CRYPT)
  24240. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24241. #endif
  24242. if (ret == 0)
  24243. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  24244. } while (ret == WC_PENDING_E);
  24245. if (ret != 0)
  24246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24247. TEST_SLEEP();
  24248. y = ECC_SHARED_SIZE;
  24249. do {
  24250. #if defined(WOLFSSL_ASYNC_CRYPT)
  24251. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24252. #endif
  24253. if (ret == 0)
  24254. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  24255. } while (ret == WC_PENDING_E);
  24256. if (ret != 0)
  24257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24258. if (y != x)
  24259. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24260. if (XMEMCMP(sharedA, sharedB, x))
  24261. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24262. TEST_SLEEP();
  24263. /* remove cofactor flag */
  24264. wc_ecc_set_flags(userA, 0);
  24265. wc_ecc_set_flags(userB, 0);
  24266. #endif /* HAVE_ECC_CDH */
  24267. #endif /* HAVE_ECC_DHE */
  24268. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  24269. #ifdef HAVE_ECC_KEY_EXPORT
  24270. x = ECC_KEY_EXPORT_BUF_SIZE;
  24271. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  24272. if (ret != 0)
  24273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24274. #ifdef HAVE_ECC_KEY_IMPORT
  24275. #ifdef WOLFSSL_CUSTOM_CURVES
  24276. if (dp != NULL) {
  24277. ret = wc_ecc_set_custom_curve(pubKey, dp);
  24278. if (ret != 0)
  24279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24280. }
  24281. #endif
  24282. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24283. if (ret != 0)
  24284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24285. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24286. #ifdef HAVE_ECC_DHE
  24287. y = ECC_SHARED_SIZE;
  24288. do {
  24289. #if defined(WOLFSSL_ASYNC_CRYPT)
  24290. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24291. #endif
  24292. if (ret == 0)
  24293. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  24294. } while (ret == WC_PENDING_E);
  24295. if (ret != 0)
  24296. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24297. if (XMEMCMP(sharedA, sharedB, y))
  24298. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24299. TEST_SLEEP();
  24300. #endif /* HAVE_ECC_DHE */
  24301. #ifdef HAVE_COMP_KEY
  24302. /* try compressed export / import too */
  24303. x = ECC_KEY_EXPORT_BUF_SIZE;
  24304. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  24305. if (ret != 0)
  24306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24307. wc_ecc_free(pubKey);
  24308. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24309. if (ret != 0)
  24310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24311. #ifdef WOLFSSL_CUSTOM_CURVES
  24312. if (dp != NULL) {
  24313. ret = wc_ecc_set_custom_curve(pubKey, dp);
  24314. if (ret != 0)
  24315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24316. }
  24317. #endif
  24318. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24319. if (ret != 0)
  24320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24321. #ifdef HAVE_ECC_DHE
  24322. y = ECC_SHARED_SIZE;
  24323. do {
  24324. #if defined(WOLFSSL_ASYNC_CRYPT)
  24325. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24326. #endif
  24327. if (ret == 0)
  24328. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  24329. } while (ret == WC_PENDING_E);
  24330. if (ret != 0)
  24331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24332. if (XMEMCMP(sharedA, sharedB, y))
  24333. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24334. TEST_SLEEP();
  24335. #endif /* HAVE_ECC_DHE */
  24336. #endif /* HAVE_COMP_KEY */
  24337. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  24338. #endif /* !WC_NO_RNG */
  24339. #endif /* HAVE_ECC_KEY_IMPORT */
  24340. #endif /* HAVE_ECC_KEY_EXPORT */
  24341. /* For KCAPI cannot sign using generated ECDH key */
  24342. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24343. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  24344. #ifdef HAVE_ECC_SIGN
  24345. /* ECC w/out Shamir has issue with all 0 digest */
  24346. /* WC_BIGINT doesn't have 0 len well on hardware */
  24347. /* Cryptocell has issues with all 0 digest */
  24348. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  24349. !defined(WOLFSSL_CRYPTOCELL)
  24350. /* test DSA sign hash with zeros */
  24351. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24352. digest[i] = 0;
  24353. }
  24354. x = ECC_SIG_SIZE;
  24355. do {
  24356. #if defined(WOLFSSL_ASYNC_CRYPT)
  24357. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24358. #endif
  24359. if (ret == 0)
  24360. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  24361. userA);
  24362. } while (ret == WC_PENDING_E);
  24363. if (ret != 0)
  24364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24365. TEST_SLEEP();
  24366. #ifdef HAVE_ECC_VERIFY
  24367. for (i=0; i<testVerifyCount; i++) {
  24368. verify = 0;
  24369. do {
  24370. #if defined(WOLFSSL_ASYNC_CRYPT)
  24371. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24372. #endif
  24373. if (ret == 0)
  24374. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  24375. &verify, userA);
  24376. } while (ret == WC_PENDING_E);
  24377. if (ret != 0)
  24378. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24379. if (verify != 1)
  24380. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24381. TEST_SLEEP();
  24382. }
  24383. #endif /* HAVE_ECC_VERIFY */
  24384. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  24385. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  24386. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24387. digest[i] = (byte)i;
  24388. }
  24389. x = ECC_SIG_SIZE;
  24390. do {
  24391. #if defined(WOLFSSL_ASYNC_CRYPT)
  24392. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24393. #endif
  24394. if (ret == 0)
  24395. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  24396. } while (ret == WC_PENDING_E);
  24397. if (ret != 0)
  24398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24399. TEST_SLEEP();
  24400. #ifdef HAVE_ECC_VERIFY
  24401. for (i=0; i<testVerifyCount; i++) {
  24402. verify = 0;
  24403. do {
  24404. #if defined(WOLFSSL_ASYNC_CRYPT)
  24405. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24406. #endif
  24407. if (ret == 0)
  24408. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  24409. } while (ret == WC_PENDING_E);
  24410. if (ret != 0)
  24411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24412. if (verify != 1)
  24413. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24414. TEST_SLEEP();
  24415. }
  24416. #endif /* HAVE_ECC_VERIFY */
  24417. #endif /* HAVE_ECC_SIGN */
  24418. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  24419. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  24420. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  24421. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  24422. !defined(WOLFSSL_KCAPI_ECC)
  24423. x = ECC_KEY_EXPORT_BUF_SIZE;
  24424. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  24425. if (ret != 0)
  24426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24427. #elif defined(HAVE_ECC_KEY_EXPORT)
  24428. (void)exportBuf;
  24429. #endif /* HAVE_ECC_KEY_EXPORT */
  24430. done:
  24431. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24432. if (userA != NULL) {
  24433. wc_ecc_free(userA);
  24434. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24435. }
  24436. if (userB != NULL) {
  24437. wc_ecc_free(userB);
  24438. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24439. }
  24440. if (pubKey != NULL) {
  24441. wc_ecc_free(pubKey);
  24442. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24443. }
  24444. #else
  24445. wc_ecc_free(pubKey);
  24446. wc_ecc_free(userB);
  24447. wc_ecc_free(userA);
  24448. #endif
  24449. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  24450. WC_FREE_VAR(sharedA, HEAP_HINT);
  24451. WC_FREE_VAR(sharedB, HEAP_HINT);
  24452. #endif
  24453. #ifdef HAVE_ECC_KEY_EXPORT
  24454. WC_FREE_VAR(exportBuf, HEAP_HINT);
  24455. #endif
  24456. #ifdef HAVE_ECC_SIGN
  24457. WC_FREE_VAR(sig, HEAP_HINT);
  24458. WC_FREE_VAR(digest, HEAP_HINT);
  24459. #endif
  24460. (void)keySize;
  24461. (void)curve_id;
  24462. (void)rng;
  24463. return ret;
  24464. }
  24465. #undef ECC_TEST_VERIFY_COUNT
  24466. #define ECC_TEST_VERIFY_COUNT 2
  24467. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  24468. {
  24469. wc_test_ret_t ret;
  24470. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  24471. NULL);
  24472. if (ret < 0) {
  24473. if (ret == ECC_CURVE_OID_E) {
  24474. /* ignore error for curves not found */
  24475. /* some curve sizes are only available with:
  24476. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  24477. and HAVE_ECC_KOBLITZ */
  24478. }
  24479. else {
  24480. printf("ecc_test_curve_size %d failed!\n", keySize);
  24481. return ret;
  24482. }
  24483. }
  24484. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  24485. #ifdef HAVE_ECC_VECTOR_TEST
  24486. ret = ecc_test_vector(keySize);
  24487. if (ret < 0) {
  24488. printf("ecc_test_vector %d failed!\n", keySize);
  24489. return ret;
  24490. }
  24491. #endif
  24492. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  24493. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  24494. ret = ecc_test_key_decode(rng, keySize);
  24495. if (ret < 0) {
  24496. if (ret == ECC_CURVE_OID_E) {
  24497. /* ignore error for curves not found */
  24498. }
  24499. else {
  24500. printf("ecc_test_key_decode %d failed!\n", keySize);
  24501. return ret;
  24502. }
  24503. }
  24504. #endif
  24505. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  24506. ret = ecc_test_key_gen(rng, keySize);
  24507. if (ret < 0) {
  24508. if (ret == ECC_CURVE_OID_E) {
  24509. /* ignore error for curves not found */
  24510. }
  24511. else {
  24512. printf("ecc_test_key_gen %d failed!\n", keySize);
  24513. return ret;
  24514. }
  24515. }
  24516. #endif
  24517. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  24518. return 0;
  24519. }
  24520. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  24521. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  24522. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  24523. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24524. static wc_test_ret_t ecc_point_test(void)
  24525. {
  24526. wc_test_ret_t ret;
  24527. ecc_point* point;
  24528. ecc_point* point2;
  24529. #ifdef HAVE_COMP_KEY
  24530. ecc_point* point3;
  24531. ecc_point* point4;
  24532. #endif
  24533. word32 outLen;
  24534. byte out[65];
  24535. byte der[] = { 0x04, /* = Uncompressed */
  24536. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24537. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24538. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24539. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24540. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24541. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24542. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24543. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  24544. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  24545. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  24546. byte derComp0[] = { 0x02, /* = Compressed, y even */
  24547. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24548. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24549. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24550. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  24551. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  24552. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24553. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24554. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24555. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  24556. #endif
  24557. byte altDer[] = { 0x04, /* = Uncompressed */
  24558. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24559. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24560. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24561. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24562. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24563. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24564. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  24565. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  24566. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  24567. /* if curve P256 is not enabled then test should not fail */
  24568. if (curve_idx == ECC_CURVE_INVALID)
  24569. return 0;
  24570. outLen = sizeof(out);
  24571. point = wc_ecc_new_point();
  24572. if (point == NULL)
  24573. return WC_TEST_RET_ENC_ERRNO;
  24574. point2 = wc_ecc_new_point();
  24575. if (point2 == NULL) {
  24576. wc_ecc_del_point(point);
  24577. return WC_TEST_RET_ENC_NC;
  24578. }
  24579. #ifdef HAVE_COMP_KEY
  24580. point3 = wc_ecc_new_point();
  24581. if (point3 == NULL) {
  24582. wc_ecc_del_point(point2);
  24583. wc_ecc_del_point(point);
  24584. return WC_TEST_RET_ENC_NC;
  24585. }
  24586. point4 = wc_ecc_new_point();
  24587. if (point4 == NULL) {
  24588. wc_ecc_del_point(point3);
  24589. wc_ecc_del_point(point2);
  24590. wc_ecc_del_point(point);
  24591. return WC_TEST_RET_ENC_NC;
  24592. }
  24593. #endif
  24594. /* Parameter Validation testing. */
  24595. wc_ecc_del_point(NULL);
  24596. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  24597. if (ret != ECC_BAD_ARG_E) {
  24598. ret = WC_TEST_RET_ENC_EC(ret);
  24599. goto done;
  24600. }
  24601. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  24602. if (ret != ECC_BAD_ARG_E) {
  24603. ret = WC_TEST_RET_ENC_EC(ret);
  24604. goto done;
  24605. }
  24606. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  24607. if (ret != ECC_BAD_ARG_E) {
  24608. ret = WC_TEST_RET_ENC_EC(ret);
  24609. goto done;
  24610. }
  24611. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  24612. if (ret != ECC_BAD_ARG_E) {
  24613. ret = WC_TEST_RET_ENC_EC(ret);
  24614. goto done;
  24615. }
  24616. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  24617. if (ret != ECC_BAD_ARG_E) {
  24618. ret = WC_TEST_RET_ENC_EC(ret);
  24619. goto done;
  24620. }
  24621. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  24622. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  24623. ret = WC_TEST_RET_ENC_EC(ret);
  24624. goto done;
  24625. }
  24626. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  24627. if (ret != ECC_BAD_ARG_E) {
  24628. ret = WC_TEST_RET_ENC_EC(ret);
  24629. goto done;
  24630. }
  24631. outLen = 0;
  24632. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  24633. if (ret != BUFFER_E) {
  24634. ret = WC_TEST_RET_ENC_EC(ret);
  24635. goto done;
  24636. }
  24637. ret = wc_ecc_copy_point(NULL, NULL);
  24638. if (ret != ECC_BAD_ARG_E) {
  24639. ret = WC_TEST_RET_ENC_EC(ret);
  24640. goto done;
  24641. }
  24642. ret = wc_ecc_copy_point(NULL, point2);
  24643. if (ret != ECC_BAD_ARG_E) {
  24644. ret = WC_TEST_RET_ENC_EC(ret);
  24645. goto done;
  24646. }
  24647. ret = wc_ecc_copy_point(point, NULL);
  24648. if (ret != ECC_BAD_ARG_E) {
  24649. ret = WC_TEST_RET_ENC_EC(ret);
  24650. goto done;
  24651. }
  24652. ret = wc_ecc_cmp_point(NULL, NULL);
  24653. if (ret != BAD_FUNC_ARG) {
  24654. ret = WC_TEST_RET_ENC_EC(ret);
  24655. goto done;
  24656. }
  24657. ret = wc_ecc_cmp_point(NULL, point2);
  24658. if (ret != BAD_FUNC_ARG) {
  24659. ret = WC_TEST_RET_ENC_EC(ret);
  24660. goto done;
  24661. }
  24662. ret = wc_ecc_cmp_point(point, NULL);
  24663. if (ret != BAD_FUNC_ARG) {
  24664. ret = WC_TEST_RET_ENC_EC(ret);
  24665. goto done;
  24666. }
  24667. /* Use API. */
  24668. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  24669. if (ret != 0) {
  24670. ret = WC_TEST_RET_ENC_EC(ret);
  24671. goto done;
  24672. }
  24673. outLen = sizeof(out);
  24674. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  24675. if (ret != 0) {
  24676. ret = WC_TEST_RET_ENC_EC(ret);
  24677. goto done;
  24678. }
  24679. if (outLen != sizeof(der)) {
  24680. ret = WC_TEST_RET_ENC_NC;
  24681. goto done;
  24682. }
  24683. if (XMEMCMP(out, der, outLen) != 0) {
  24684. ret = WC_TEST_RET_ENC_NC;
  24685. goto done;
  24686. }
  24687. ret = wc_ecc_copy_point(point2, point);
  24688. if (ret != MP_OKAY) {
  24689. ret = WC_TEST_RET_ENC_EC(ret);
  24690. goto done;
  24691. }
  24692. ret = wc_ecc_cmp_point(point2, point);
  24693. if (ret != MP_EQ) {
  24694. ret = WC_TEST_RET_ENC_EC(ret);
  24695. goto done;
  24696. }
  24697. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  24698. if (ret != 0) {
  24699. ret = WC_TEST_RET_ENC_EC(ret);
  24700. goto done;
  24701. }
  24702. ret = wc_ecc_cmp_point(point2, point);
  24703. if (ret != MP_GT) {
  24704. ret = WC_TEST_RET_ENC_EC(ret);
  24705. goto done;
  24706. }
  24707. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  24708. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  24709. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  24710. if (ret != 0) {
  24711. ret = WC_TEST_RET_ENC_EC(ret);
  24712. goto done;
  24713. }
  24714. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  24715. if (ret != 0) {
  24716. ret = WC_TEST_RET_ENC_EC(ret);
  24717. goto done;
  24718. }
  24719. ret = wc_ecc_cmp_point(point3, point4);
  24720. if (ret != MP_EQ) {
  24721. ret = WC_TEST_RET_ENC_EC(ret);
  24722. goto done;
  24723. }
  24724. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  24725. if (ret != 0) {
  24726. ret = WC_TEST_RET_ENC_EC(ret);
  24727. goto done;
  24728. }
  24729. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  24730. if (ret != 0) {
  24731. ret = WC_TEST_RET_ENC_EC(ret);
  24732. goto done;
  24733. }
  24734. ret = wc_ecc_cmp_point(point3, point4);
  24735. if (ret != MP_EQ) {
  24736. ret = WC_TEST_RET_ENC_EC(ret);
  24737. goto done;
  24738. }
  24739. #endif
  24740. done:
  24741. #ifdef HAVE_COMP_KEY
  24742. wc_ecc_del_point(point4);
  24743. wc_ecc_del_point(point3);
  24744. #endif
  24745. wc_ecc_del_point(point2);
  24746. wc_ecc_del_point(point);
  24747. return ret;
  24748. }
  24749. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  24750. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24751. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  24752. {
  24753. wc_test_ret_t ret;
  24754. word32 sigSz;
  24755. int size;
  24756. byte out[ECC_MAX_SIG_SIZE];
  24757. byte in[] = TEST_STRING;
  24758. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  24759. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  24760. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  24761. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  24762. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  24763. };
  24764. word32 inLen = (word32)XSTRLEN((char*)in);
  24765. size = wc_ecc_sig_size(key);
  24766. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  24767. if (ret != size)
  24768. return WC_TEST_RET_ENC_NC;
  24769. sigSz = (word32)ret;
  24770. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  24771. inLen, out, &sigSz, key, sizeof(*key), rng);
  24772. if (ret != 0)
  24773. return WC_TEST_RET_ENC_EC(ret);
  24774. TEST_SLEEP();
  24775. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  24776. inLen, out, sigSz, key, sizeof(*key));
  24777. if (ret != 0)
  24778. return WC_TEST_RET_ENC_EC(ret);
  24779. TEST_SLEEP();
  24780. sigSz = (word32)sizeof(out);
  24781. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  24782. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  24783. if (ret != 0)
  24784. return WC_TEST_RET_ENC_EC(ret);
  24785. TEST_SLEEP();
  24786. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  24787. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  24788. if (ret != 0)
  24789. return WC_TEST_RET_ENC_EC(ret);
  24790. TEST_SLEEP();
  24791. return 0;
  24792. }
  24793. #endif
  24794. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  24795. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24796. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  24797. {
  24798. wc_test_ret_t ret;
  24799. int curve_id;
  24800. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24801. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24802. #else
  24803. ecc_key keyImp[1];
  24804. #endif
  24805. byte priv[32];
  24806. word32 privLen;
  24807. byte pub[65*2];
  24808. word32 pubLen, pubLenX, pubLenY;
  24809. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  24810. "ce46cbc947616d0cbaa82323818a793d";
  24811. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  24812. "922f8b30ea6e8811742ac7238fe87308";
  24813. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  24814. "2e7a493ec1153b3a95feb8a4873f8d08";
  24815. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24816. if (keyImp == NULL)
  24817. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24818. #endif
  24819. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  24820. privLen = sizeof(priv);
  24821. ret = wc_ecc_export_private_only(key, priv, &privLen);
  24822. if (ret != 0) {
  24823. ret = WC_TEST_RET_ENC_EC(ret);
  24824. goto done;
  24825. }
  24826. pubLen = sizeof(pub);
  24827. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  24828. if (ret != 0) {
  24829. ret = WC_TEST_RET_ENC_EC(ret);
  24830. goto done;
  24831. }
  24832. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  24833. if (ret != 0) {
  24834. ret = WC_TEST_RET_ENC_EC(ret);
  24835. goto done;
  24836. }
  24837. wc_ecc_free(keyImp);
  24838. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  24839. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  24840. if (ret != 0) {
  24841. ret = WC_TEST_RET_ENC_EC(ret);
  24842. goto done;
  24843. }
  24844. wc_ecc_free(keyImp);
  24845. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  24846. curve_id = wc_ecc_get_curve_id(key->idx);
  24847. if (curve_id < 0) {
  24848. ret = WC_TEST_RET_ENC_EC(curve_id);
  24849. goto done;
  24850. }
  24851. /* test import private only */
  24852. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  24853. curve_id);
  24854. if (ret != 0) {
  24855. ret = WC_TEST_RET_ENC_EC(ret);
  24856. goto done;
  24857. }
  24858. wc_ecc_free(keyImp);
  24859. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  24860. /* test export public raw */
  24861. pubLenX = pubLenY = 32;
  24862. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  24863. if (ret != 0) {
  24864. ret = WC_TEST_RET_ENC_EC(ret);
  24865. goto done;
  24866. }
  24867. #ifndef HAVE_SELFTEST
  24868. /* test import of public */
  24869. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  24870. if (ret != 0) {
  24871. ret = WC_TEST_RET_ENC_EC(ret);
  24872. goto done;
  24873. }
  24874. #endif
  24875. wc_ecc_free(keyImp);
  24876. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  24877. /* test export private and public raw */
  24878. pubLenX = pubLenY = privLen = 32;
  24879. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  24880. priv, &privLen);
  24881. if (ret != 0) {
  24882. ret = WC_TEST_RET_ENC_EC(ret);
  24883. goto done;
  24884. }
  24885. #ifndef HAVE_SELFTEST
  24886. /* test import of private and public */
  24887. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  24888. if (ret != 0) {
  24889. ret = WC_TEST_RET_ENC_EC(ret);
  24890. goto done;
  24891. }
  24892. #endif
  24893. done:
  24894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24895. if (keyImp != NULL) {
  24896. wc_ecc_free(keyImp);
  24897. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24898. }
  24899. #else
  24900. wc_ecc_free(keyImp);
  24901. #endif
  24902. return ret;
  24903. }
  24904. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  24905. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  24906. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24907. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  24908. {
  24909. wc_test_ret_t ret;
  24910. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24911. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24912. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24913. #else
  24914. ecc_key key2[1];
  24915. ecc_key key3[1];
  24916. #endif
  24917. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24918. if ((key2 == NULL) || (key3 == NULL))
  24919. ERROR_OUT(MEMORY_E, done);
  24920. #endif
  24921. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  24922. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  24923. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  24924. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  24925. * private key in key2.
  24926. */
  24927. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  24928. ECC_SECP256R1);
  24929. if (ret != 0)
  24930. goto done;
  24931. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  24932. * in key3.
  24933. */
  24934. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  24935. key1->dp->prime, ECC_SECP256R1);
  24936. if (ret != 0)
  24937. goto done;
  24938. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  24939. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  24940. 1);
  24941. if (ret != 0) {
  24942. ret = WC_TEST_RET_ENC_EC(ret);
  24943. goto done;
  24944. }
  24945. done:
  24946. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24947. if (key2 != NULL) {
  24948. wc_ecc_free(key2);
  24949. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24950. }
  24951. if (key3 != NULL) {
  24952. wc_ecc_free(key3);
  24953. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24954. }
  24955. #else
  24956. wc_ecc_free(key3);
  24957. wc_ecc_free(key2);
  24958. #endif
  24959. return ret;
  24960. }
  24961. #endif
  24962. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  24963. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24964. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  24965. {
  24966. wc_test_ret_t ret;
  24967. byte out[128];
  24968. word32 outLen = sizeof(out);
  24969. /* Parameter Validation testing. */
  24970. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  24971. if (ret != BAD_FUNC_ARG)
  24972. return WC_TEST_RET_ENC_EC(ret);
  24973. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  24974. if (ret != BAD_FUNC_ARG)
  24975. return WC_TEST_RET_ENC_EC(ret);
  24976. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  24977. if (ret != BAD_FUNC_ARG)
  24978. return WC_TEST_RET_ENC_EC(ret);
  24979. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  24980. if (ret != BAD_FUNC_ARG)
  24981. return WC_TEST_RET_ENC_EC(ret);
  24982. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24983. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24984. !defined(HAVE_SELFTEST)
  24985. ret = wc_ecc_set_rng(key, rng);
  24986. if (ret != 0)
  24987. return WC_TEST_RET_ENC_EC(ret);
  24988. #else
  24989. (void)rng;
  24990. #endif
  24991. /* Use API. */
  24992. ret = 0;
  24993. do {
  24994. #if defined(WOLFSSL_ASYNC_CRYPT)
  24995. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24996. #endif
  24997. if (ret == 0)
  24998. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  24999. } while (ret == WC_PENDING_E);
  25000. if (ret != 0)
  25001. return WC_TEST_RET_ENC_EC(ret);
  25002. TEST_SLEEP();
  25003. return 0;
  25004. }
  25005. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  25006. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  25007. {
  25008. wc_test_ret_t ret;
  25009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25010. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25011. #else
  25012. ecc_key key[1];
  25013. #endif
  25014. #if !defined(NO_ECC_SECP) && \
  25015. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  25016. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  25017. word32 idx = 0;
  25018. #endif
  25019. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25020. if (key == NULL)
  25021. ERROR_OUT(MEMORY_E, done);
  25022. #endif
  25023. wc_ecc_init_ex(key, HEAP_HINT, devId);
  25024. /* Use API */
  25025. ret = wc_ecc_set_flags(NULL, 0);
  25026. if (ret != BAD_FUNC_ARG) {
  25027. ret = WC_TEST_RET_ENC_EC(ret);
  25028. goto done;
  25029. }
  25030. ret = wc_ecc_set_flags(key, 0);
  25031. if (ret != 0) {
  25032. ret = WC_TEST_RET_ENC_EC(ret);
  25033. goto done;
  25034. }
  25035. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  25036. #ifndef WC_NO_RNG
  25037. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  25038. #if defined(WOLFSSL_ASYNC_CRYPT)
  25039. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  25040. #endif
  25041. if (ret != 0) {
  25042. goto done;
  25043. }
  25044. #ifndef NO_SIG_WRAPPER
  25045. ret = ecc_sig_test(rng, key);
  25046. if (ret < 0)
  25047. goto done;
  25048. #endif
  25049. TEST_SLEEP();
  25050. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  25051. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25052. ret = ecc_ssh_test(key, rng);
  25053. if (ret < 0)
  25054. goto done;
  25055. #endif
  25056. wc_ecc_free(key);
  25057. #else
  25058. (void)rng;
  25059. #endif /* !WC_NO_RNG */
  25060. #if !defined(NO_ECC_SECP) && \
  25061. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  25062. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  25063. /* Use test ECC key - ensure real private "d" exists */
  25064. #ifdef USE_CERT_BUFFERS_256
  25065. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  25066. sizeof_ecc_key_der_256);
  25067. #else
  25068. {
  25069. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  25070. byte der[128];
  25071. word32 derSz;
  25072. if (!file) {
  25073. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  25074. }
  25075. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  25076. XFCLOSE(file);
  25077. if (derSz == 0)
  25078. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  25079. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  25080. }
  25081. #endif
  25082. if (ret != 0) {
  25083. goto done;
  25084. }
  25085. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  25086. ret = ecc_exp_imp_test(key);
  25087. if (ret < 0)
  25088. goto done;
  25089. #endif
  25090. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  25091. !defined(WOLFSSL_CRYPTOCELL)
  25092. ret = ecc_mulmod_test(key);
  25093. if (ret < 0)
  25094. goto done;
  25095. #endif
  25096. #endif
  25097. #else
  25098. (void)rng;
  25099. (void)idx;
  25100. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25101. done:
  25102. wc_ecc_free(key);
  25103. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25104. if (key != NULL) {
  25105. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25106. }
  25107. #endif
  25108. return ret;
  25109. }
  25110. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  25111. #if defined(WOLFSSL_CERT_EXT) && \
  25112. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25113. static wc_test_ret_t ecc_decode_test(void)
  25114. {
  25115. wc_test_ret_t ret;
  25116. word32 inSz;
  25117. word32 inOutIdx;
  25118. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25119. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25120. #else
  25121. ecc_key key[1];
  25122. #endif
  25123. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  25124. /* This is ecc_clikeypub_der_256. */
  25125. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  25126. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  25127. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  25128. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  25129. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  25130. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  25131. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  25132. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  25133. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  25134. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  25135. 0xb4 };
  25136. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  25137. 0x00, 0x04, 0x01, 0x01 };
  25138. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  25139. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25140. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  25141. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25142. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  25143. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25144. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  25145. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25146. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25147. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  25148. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25149. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  25150. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  25151. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25152. 0x03, 0x03, 0x04, 0x01, 0x01 };
  25153. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  25154. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25155. 0x03, 0x03, 0x00, 0x04, 0x01 };
  25156. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25157. if (key == NULL)
  25158. ERROR_OUT(MEMORY_E, done);
  25159. #endif
  25160. XMEMSET(key, 0, sizeof *key);
  25161. wc_ecc_init_ex(key, HEAP_HINT, devId);
  25162. inSz = sizeof(good);
  25163. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  25164. if (ret != BAD_FUNC_ARG) {
  25165. ret = WC_TEST_RET_ENC_EC(ret);
  25166. goto done;
  25167. }
  25168. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  25169. if (ret != BAD_FUNC_ARG) {
  25170. ret = WC_TEST_RET_ENC_EC(ret);
  25171. goto done;
  25172. }
  25173. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  25174. if (ret != BAD_FUNC_ARG) {
  25175. ret = WC_TEST_RET_ENC_EC(ret);
  25176. goto done;
  25177. }
  25178. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  25179. if (ret != BAD_FUNC_ARG) {
  25180. ret = WC_TEST_RET_ENC_EC(ret);
  25181. goto done;
  25182. }
  25183. /* Change offset to produce bad input data. */
  25184. inOutIdx = 2;
  25185. inSz = sizeof(good) - inOutIdx;
  25186. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25187. if (ret != ASN_PARSE_E) {
  25188. ret = WC_TEST_RET_ENC_EC(ret);
  25189. goto done;
  25190. }
  25191. inOutIdx = 4;
  25192. inSz = sizeof(good) - inOutIdx;
  25193. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25194. if (ret != ASN_PARSE_E) {
  25195. ret = WC_TEST_RET_ENC_EC(ret);
  25196. goto done;
  25197. }
  25198. /* Bad data. */
  25199. inSz = sizeof(badNoObjId);
  25200. inOutIdx = 0;
  25201. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  25202. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  25203. ret = WC_TEST_RET_ENC_EC(ret);
  25204. goto done;
  25205. }
  25206. inSz = sizeof(badOneObjId);
  25207. inOutIdx = 0;
  25208. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  25209. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  25210. ret = WC_TEST_RET_ENC_EC(ret);
  25211. goto done;
  25212. }
  25213. inSz = sizeof(badObjId1Len);
  25214. inOutIdx = 0;
  25215. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  25216. if (ret != ASN_PARSE_E) {
  25217. ret = WC_TEST_RET_ENC_EC(ret);
  25218. goto done;
  25219. }
  25220. inSz = sizeof(badObj2d1Len);
  25221. inOutIdx = 0;
  25222. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  25223. if (ret != ASN_PARSE_E) {
  25224. ret = WC_TEST_RET_ENC_EC(ret);
  25225. goto done;
  25226. }
  25227. inSz = sizeof(badNotBitStr);
  25228. inOutIdx = 0;
  25229. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  25230. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  25231. ret = WC_TEST_RET_ENC_EC(ret);
  25232. goto done;
  25233. }
  25234. inSz = sizeof(badBitStrLen);
  25235. inOutIdx = 0;
  25236. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  25237. if (ret != ASN_PARSE_E) {
  25238. ret = WC_TEST_RET_ENC_EC(ret);
  25239. goto done;
  25240. }
  25241. inSz = sizeof(badNoBitStrZero);
  25242. inOutIdx = 0;
  25243. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  25244. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  25245. ret = WC_TEST_RET_ENC_EC(ret);
  25246. goto done;
  25247. }
  25248. inSz = sizeof(badPoint);
  25249. inOutIdx = 0;
  25250. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  25251. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  25252. ret = WC_TEST_RET_ENC_EC(ret);
  25253. goto done;
  25254. }
  25255. inSz = sizeof(good);
  25256. inOutIdx = 0;
  25257. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25258. if (ret != 0) {
  25259. ret = WC_TEST_RET_ENC_EC(ret);
  25260. goto done;
  25261. }
  25262. done:
  25263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25264. if (key != NULL) {
  25265. wc_ecc_free(key);
  25266. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25267. }
  25268. #else
  25269. wc_ecc_free(key);
  25270. #endif
  25271. return ret;
  25272. }
  25273. #endif /* WOLFSSL_CERT_EXT */
  25274. #ifdef WOLFSSL_CUSTOM_CURVES
  25275. static const byte eccKeyExplicitCurve[] = {
  25276. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  25277. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  25278. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  25279. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  25280. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  25281. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25282. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25283. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  25284. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  25285. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  25286. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  25287. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  25288. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  25289. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  25290. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  25291. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  25292. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  25293. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  25294. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25295. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25296. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  25297. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  25298. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  25299. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  25300. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  25301. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  25302. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  25303. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  25304. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  25305. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  25306. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  25307. };
  25308. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  25309. {
  25310. wc_test_ret_t ret;
  25311. word32 inOutIdx;
  25312. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25313. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25314. #else
  25315. ecc_key key[1];
  25316. #endif
  25317. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  25318. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  25319. #ifndef WOLFSSL_ECC_CURVE_STATIC
  25320. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  25321. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  25322. };
  25323. #define ecc_oid_brainpoolp256r1_sz \
  25324. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  25325. #else
  25326. #define ecc_oid_brainpoolp256r1 { \
  25327. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  25328. }
  25329. #define ecc_oid_brainpoolp256r1_sz 9
  25330. #endif
  25331. #define ecc_oid_brainpoolp256r1_sum 104
  25332. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  25333. 32, /* size/bytes */
  25334. ECC_CURVE_CUSTOM, /* ID */
  25335. "BRAINPOOLP256R1", /* curve name */
  25336. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  25337. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  25338. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  25339. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  25340. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  25341. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  25342. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  25343. ecc_oid_brainpoolp256r1_sz,
  25344. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  25345. 1, /* cofactor */
  25346. };
  25347. #endif /* HAVE_ECC_BRAINPOOL */
  25348. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25349. if (! key) {
  25350. ret = MEMORY_E;
  25351. goto done;
  25352. }
  25353. #endif
  25354. XMEMSET(key, 0, sizeof *key);
  25355. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  25356. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  25357. &ecc_dp_brainpool256r1);
  25358. if (ret != 0) {
  25359. printf("ECC test for custom curve failed!\n");
  25360. goto done;
  25361. }
  25362. #endif
  25363. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  25364. {
  25365. int curve_id;
  25366. #ifdef HAVE_ECC_BRAINPOOL
  25367. curve_id = ECC_BRAINPOOLP256R1;
  25368. #else
  25369. curve_id = ECC_SECP256K1;
  25370. #endif
  25371. /* Test and demonstrate use of non-SECP curve */
  25372. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  25373. if (ret < 0) {
  25374. printf("ECC test for curve_id %d failed!\n", curve_id);
  25375. goto done;
  25376. }
  25377. }
  25378. #endif
  25379. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25380. if (ret != 0) {
  25381. ret = WC_TEST_RET_ENC_EC(ret);
  25382. goto done;
  25383. }
  25384. inOutIdx = 0;
  25385. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  25386. sizeof(eccKeyExplicitCurve));
  25387. if (ret != 0)
  25388. ret = WC_TEST_RET_ENC_EC(ret);
  25389. done:
  25390. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25391. if (key) {
  25392. wc_ecc_free(key);
  25393. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25394. }
  25395. #else
  25396. wc_ecc_free(key);
  25397. #endif
  25398. (void)rng;
  25399. return ret;
  25400. }
  25401. #endif /* WOLFSSL_CUSTOM_CURVES */
  25402. #ifdef WOLFSSL_SM2
  25403. #ifdef HAVE_ECC_VERIFY
  25404. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  25405. #ifdef WOLFSSL_SM2
  25406. #ifdef HAVE_OID_ENCODING
  25407. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  25408. #define CODED_SM2P256V1_SZ 6
  25409. #else
  25410. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  25411. #define CODED_SM2P256V1_SZ 10
  25412. #endif
  25413. #ifndef WOLFSSL_ECC_CURVE_STATIC
  25414. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  25415. #else
  25416. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  25417. #endif
  25418. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  25419. #endif /* WOLFSSL_SM2 */
  25420. #define ECC_SM2P256V1_TEST 102
  25421. static int test_sm2_verify_caseA2(void)
  25422. {
  25423. ecc_key key;
  25424. int ret, res;
  25425. mp_int r,s;
  25426. /* test key values */
  25427. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  25428. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  25429. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  25430. const ecc_set_type ecc_sm2_A2 = {
  25431. 32, /* size/bytes */
  25432. ECC_SM2P256V1_TEST, /* ID */
  25433. "SM2P256V1_TEST", /* curve name */
  25434. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  25435. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  25436. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  25437. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  25438. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  25439. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  25440. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  25441. ecc_oid_sm2p256v1, /* oid/oidSz */
  25442. ecc_oid_sm2p256v1_sz,
  25443. ECC_SM2P256V1_OID, /* oid sum */
  25444. 1, /* cofactor */
  25445. };
  25446. /* use canned hash value hash = H(ZA||M) */
  25447. const byte hash[] = {
  25448. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  25449. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  25450. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  25451. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  25452. };
  25453. /* canned r and s */
  25454. const byte rCan[] = {
  25455. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  25456. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  25457. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  25458. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  25459. };
  25460. const byte sCan[] = {
  25461. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  25462. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  25463. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  25464. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  25465. };
  25466. mp_init(&r);
  25467. mp_init(&s);
  25468. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  25469. if (ret != 0)
  25470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25471. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  25472. if (ret != 0)
  25473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25474. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  25475. if (ret != 0)
  25476. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25477. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  25478. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  25479. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  25480. if (ret != 0)
  25481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25482. if (res != 1)
  25483. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25484. done:
  25485. mp_free(&r);
  25486. mp_free(&s);
  25487. wc_ecc_free(&key);
  25488. return ret;
  25489. }
  25490. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  25491. static int test_sm2_verify_case(void)
  25492. {
  25493. ecc_key key;
  25494. int ret, res;
  25495. /* test key values */
  25496. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  25497. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  25498. /* use canned hash value hash = H(ZA||M) */
  25499. const byte hash[] = {
  25500. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  25501. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  25502. 0x50,0x69,0x5B,0x20
  25503. };
  25504. const byte sig[] = {
  25505. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  25506. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  25507. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  25508. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  25509. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  25510. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  25511. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  25512. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  25513. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  25514. };
  25515. const byte badSig[] = {
  25516. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  25517. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  25518. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  25519. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  25520. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  25521. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  25522. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  25523. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  25524. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  25525. };
  25526. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  25527. if (ret != 0)
  25528. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25529. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  25530. if (ret != 0)
  25531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25532. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  25533. &key);
  25534. if (ret != 0)
  25535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25536. if (res != 1)
  25537. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25538. /* now test a case that should fail */
  25539. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  25540. &res, &key);
  25541. if (ret != 0)
  25542. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25543. if (res == 1)
  25544. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25545. done:
  25546. wc_ecc_free(&key);
  25547. return ret;
  25548. }
  25549. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  25550. {
  25551. const ecc_set_type* dp = wc_ecc_get_curve_params(
  25552. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  25553. int keySize = 32;
  25554. int curve_id = ECC_SM2P256V1;
  25555. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  25556. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  25557. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  25558. #endif
  25559. #ifdef HAVE_ECC_KEY_EXPORT
  25560. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  25561. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  25562. #endif
  25563. word32 x = 0;
  25564. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  25565. word32 y;
  25566. #endif
  25567. #ifdef HAVE_ECC_SIGN
  25568. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25569. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  25570. int i;
  25571. #ifdef HAVE_ECC_VERIFY
  25572. int verify;
  25573. #endif /* HAVE_ECC_VERIFY */
  25574. #endif /* HAVE_ECC_SIGN */
  25575. int ret;
  25576. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25577. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25578. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25579. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25580. #else
  25581. ecc_key userA[1];
  25582. ecc_key userB[1];
  25583. ecc_key pubKey[1];
  25584. #endif
  25585. #ifndef WC_NO_RNG
  25586. int curveSize;
  25587. #endif
  25588. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  25589. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  25590. if (sharedA == NULL || sharedB == NULL)
  25591. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25592. #endif
  25593. #ifdef HAVE_ECC_KEY_EXPORT
  25594. if (exportBuf == NULL)
  25595. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25596. #endif
  25597. #ifdef HAVE_ECC_SIGN
  25598. if (sig == NULL || digest == NULL)
  25599. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25600. #endif
  25601. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  25602. (void)testVerifyCount;
  25603. (void)dp;
  25604. (void)x;
  25605. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25606. if ((userA == NULL) ||
  25607. (userB == NULL) ||
  25608. (pubKey == NULL))
  25609. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25610. #endif
  25611. XMEMSET(userA, 0, sizeof *userA);
  25612. XMEMSET(userB, 0, sizeof *userB);
  25613. XMEMSET(pubKey, 0, sizeof *pubKey);
  25614. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25615. if (ret != 0)
  25616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25617. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  25618. if (ret != 0)
  25619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25620. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  25621. if (ret != 0)
  25622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25623. #ifndef WC_NO_RNG
  25624. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  25625. if (ret == ECC_CURVE_OID_E)
  25626. goto done; /* catch case, where curve is not supported */
  25627. if (ret != 0)
  25628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25629. TEST_SLEEP();
  25630. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  25631. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  25632. if (curveSize != userA->dp->size) {
  25633. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25634. }
  25635. }
  25636. ret = wc_ecc_check_key(userA);
  25637. if (ret != 0)
  25638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25639. TEST_SLEEP();
  25640. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  25641. if (ret != 0)
  25642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25643. /* only perform the below tests if the key size matches */
  25644. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  25645. if (ret != 0) {
  25646. ret = ECC_CURVE_OID_E;
  25647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25648. }
  25649. #ifdef HAVE_ECC_DHE
  25650. #if defined(ECC_TIMING_RESISTANT)
  25651. ret = wc_ecc_set_rng(userA, rng);
  25652. if (ret != 0)
  25653. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25654. ret = wc_ecc_set_rng(userB, rng);
  25655. if (ret != 0)
  25656. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25657. #endif
  25658. x = ECC_SHARED_SIZE;
  25659. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  25660. if (ret != 0)
  25661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25662. y = ECC_SHARED_SIZE;
  25663. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  25664. if (ret != 0)
  25665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25666. if (y != x)
  25667. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25668. if (XMEMCMP(sharedA, sharedB, x))
  25669. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25670. #endif /* HAVE_ECC_DHE */
  25671. #ifdef HAVE_ECC_KEY_EXPORT
  25672. x = ECC_KEY_EXPORT_BUF_SIZE;
  25673. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  25674. if (ret != 0)
  25675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25676. #ifdef HAVE_ECC_KEY_IMPORT
  25677. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  25678. if (ret != 0)
  25679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25680. #ifdef HAVE_ECC_DHE
  25681. y = ECC_SHARED_SIZE;
  25682. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  25683. if (ret != 0)
  25684. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25685. if (XMEMCMP(sharedA, sharedB, y))
  25686. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25687. #endif /* HAVE_ECC_DHE */
  25688. #ifdef HAVE_COMP_KEY
  25689. /* try compressed export / import too */
  25690. x = ECC_KEY_EXPORT_BUF_SIZE;
  25691. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  25692. if (ret != 0)
  25693. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25694. wc_ecc_free(pubKey);
  25695. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  25696. if (ret != 0)
  25697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25698. #endif
  25699. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  25700. if (ret != 0)
  25701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25702. #ifdef HAVE_ECC_DHE
  25703. y = ECC_SHARED_SIZE;
  25704. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  25705. if (ret != 0)
  25706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25707. if (XMEMCMP(sharedA, sharedB, y))
  25708. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25709. #endif /* HAVE_ECC_DHE */
  25710. #endif /* HAVE_ECC_KEY_IMPORT */
  25711. #endif /* HAVE_ECC_KEY_EXPORT */
  25712. #endif /* !WC_NO_RNG */
  25713. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  25714. !defined(WC_NO_RNG))
  25715. #ifdef HAVE_ECC_SIGN
  25716. /* ECC w/out Shamir has issue with all 0 digest */
  25717. /* WC_BIGINT doesn't have 0 len well on hardware */
  25718. /* Cryptocell has issues with all 0 digest */
  25719. #if defined(ECC_SHAMIR)
  25720. /* test DSA sign hash with zeros */
  25721. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  25722. digest[i] = 0;
  25723. }
  25724. x = ECC_SIG_SIZE;
  25725. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  25726. if (ret != 0)
  25727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25728. #ifdef HAVE_ECC_VERIFY
  25729. for (i = 0; i < testVerifyCount; i++) {
  25730. verify = 0;
  25731. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  25732. userA);
  25733. if (ret != 0)
  25734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25735. if (verify != 1)
  25736. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25737. }
  25738. #endif /* HAVE_ECC_VERIFY */
  25739. #endif /* ECC_SHAMIR */
  25740. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  25741. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  25742. digest[i] = (byte)i;
  25743. }
  25744. x = ECC_SIG_SIZE;
  25745. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  25746. if (ret != 0)
  25747. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25748. #ifdef HAVE_ECC_VERIFY
  25749. for (i = 0; i < testVerifyCount; i++) {
  25750. verify = 0;
  25751. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  25752. userA);
  25753. if (ret != 0)
  25754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25755. if (verify != 1)
  25756. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25757. }
  25758. #endif /* HAVE_ECC_VERIFY */
  25759. #endif /* HAVE_ECC_SIGN */
  25760. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  25761. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  25762. x = ECC_KEY_EXPORT_BUF_SIZE;
  25763. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  25764. if (ret != 0)
  25765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25766. #elif defined(HAVE_ECC_KEY_EXPORT)
  25767. (void)exportBuf;
  25768. #endif /* HAVE_ECC_KEY_EXPORT */
  25769. done:
  25770. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25771. if (userA != NULL) {
  25772. wc_ecc_free(userA);
  25773. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25774. }
  25775. if (userB != NULL) {
  25776. wc_ecc_free(userB);
  25777. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25778. }
  25779. if (pubKey != NULL) {
  25780. wc_ecc_free(pubKey);
  25781. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25782. }
  25783. #else
  25784. wc_ecc_free(pubKey);
  25785. wc_ecc_free(userB);
  25786. wc_ecc_free(userA);
  25787. #endif
  25788. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  25789. WC_FREE_VAR(sharedA, HEAP_HINT);
  25790. WC_FREE_VAR(sharedB, HEAP_HINT);
  25791. #endif
  25792. #ifdef HAVE_ECC_KEY_EXPORT
  25793. WC_FREE_VAR(exportBuf, HEAP_HINT);
  25794. #endif
  25795. #ifdef HAVE_ECC_SIGN
  25796. WC_FREE_VAR(sig, HEAP_HINT);
  25797. WC_FREE_VAR(digest, HEAP_HINT);
  25798. #endif
  25799. (void)keySize;
  25800. (void)curve_id;
  25801. (void)rng;
  25802. return ret;
  25803. }
  25804. #endif /* HAVE_ECC_VERIFY */
  25805. static int test_sm2_create_digest(void)
  25806. {
  25807. const byte msg[] = "message to sign";
  25808. const byte id[] = "0123456789";
  25809. const byte badId[] = "0123556789";
  25810. byte expected[] = {
  25811. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  25812. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  25813. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  25814. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  25815. };
  25816. ecc_key key;
  25817. int ret;
  25818. /* test key values */
  25819. const char qx[] =
  25820. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  25821. const char qy[] =
  25822. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  25823. const char d[] =
  25824. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  25825. byte digest[WC_SHA256_DIGEST_SIZE];
  25826. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  25827. if (ret != 0)
  25828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25829. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  25830. if (ret != 0)
  25831. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25832. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  25833. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  25834. WC_SHA256_DIGEST_SIZE, &key);
  25835. if (ret != 0)
  25836. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25837. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  25838. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25839. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  25840. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  25841. WC_SHA256_DIGEST_SIZE, &key);
  25842. if (ret != 0)
  25843. goto done;
  25844. /* should be different than the previous ID used */
  25845. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  25846. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25847. done:
  25848. wc_ecc_free(&key);
  25849. return ret;
  25850. }
  25851. static int test_sm2_verify(void)
  25852. {
  25853. int ret = 0;
  25854. #ifdef HAVE_ECC_VERIFY
  25855. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  25856. ret = test_sm2_verify_caseA2();
  25857. if (ret != 0)
  25858. return ret;
  25859. #endif
  25860. ret = test_sm2_verify_case();
  25861. if (ret != 0)
  25862. return ret;
  25863. #endif /* HAVE_ECC_VERIFY */
  25864. ret = test_sm2_create_digest();
  25865. return ret;
  25866. }
  25867. #endif /* WOLFSSL_SM2 */
  25868. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  25869. /* Make Cert / Sign example for ECC cert and ECC CA */
  25870. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  25871. {
  25872. wc_test_ret_t ret;
  25873. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25874. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25875. #ifdef WOLFSSL_TEST_CERT
  25876. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25877. #endif
  25878. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25879. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25880. #else
  25881. Cert myCert[1];
  25882. #ifdef WOLFSSL_TEST_CERT
  25883. DecodedCert decode[1];
  25884. #endif
  25885. ecc_key caEccKey[1];
  25886. ecc_key certPubKey[1];
  25887. #endif
  25888. int certSz;
  25889. size_t bytes;
  25890. word32 idx = 0;
  25891. #ifndef USE_CERT_BUFFERS_256
  25892. XFILE file;
  25893. #endif
  25894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25895. byte* der = NULL;
  25896. #else
  25897. byte der[FOURK_BUF];
  25898. #endif
  25899. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25900. if ((myCert == NULL)
  25901. #ifdef WOLFSSL_TEST_CERT
  25902. || (decode == NULL)
  25903. #endif
  25904. || (caEccKey == NULL) || (certPubKey == NULL))
  25905. ERROR_OUT(MEMORY_E, exit);
  25906. #endif
  25907. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  25908. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  25909. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25910. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25911. if (der == NULL) {
  25912. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  25913. }
  25914. #endif
  25915. /* Get cert private key */
  25916. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  25917. /* Get Cert Key 384 */
  25918. #ifdef USE_CERT_BUFFERS_256
  25919. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  25920. bytes = sizeof_ca_ecc_key_der_384;
  25921. #else
  25922. file = XFOPEN(eccCaKey384File, "rb");
  25923. if (!file) {
  25924. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  25925. }
  25926. bytes = XFREAD(der, 1, FOURK_BUF, file);
  25927. XFCLOSE(file);
  25928. if (bytes == 0)
  25929. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  25930. (void)eccCaKeyFile;
  25931. #endif /* USE_CERT_BUFFERS_256 */
  25932. #else
  25933. #ifdef USE_CERT_BUFFERS_256
  25934. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  25935. bytes = sizeof_ca_ecc_key_der_256;
  25936. #else
  25937. file = XFOPEN(eccCaKeyFile, "rb");
  25938. if (!file) {
  25939. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  25940. }
  25941. bytes = XFREAD(der, 1, FOURK_BUF, file);
  25942. XFCLOSE(file);
  25943. if (bytes == 0)
  25944. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  25945. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  25946. (void)eccCaKey384File;
  25947. #endif
  25948. #endif /* USE_CERT_BUFFERS_256 */
  25949. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  25950. /* Get CA Key */
  25951. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  25952. if (ret != 0)
  25953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25954. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  25955. if (ret != 0)
  25956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25957. /* Make a public key */
  25958. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  25959. if (ret != 0)
  25960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25961. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  25962. #if defined(WOLFSSL_ASYNC_CRYPT)
  25963. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  25964. #endif
  25965. if (ret != 0)
  25966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25967. TEST_SLEEP();
  25968. /* Setup Certificate */
  25969. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  25970. if (ret != 0)
  25971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25972. #ifndef NO_SHA256
  25973. myCert->sigType = CTC_SHA256wECDSA;
  25974. #else
  25975. myCert->sigType = CTC_SHAwECDSA;
  25976. #endif
  25977. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  25978. #ifdef WOLFSSL_CERT_EXT
  25979. /* add Policies */
  25980. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  25981. CTC_MAX_CERTPOL_SZ);
  25982. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  25983. CTC_MAX_CERTPOL_SZ);
  25984. myCert->certPoliciesNb = 2;
  25985. /* add SKID from the Public Key */
  25986. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  25987. if (ret != 0)
  25988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25989. /* add AKID from the Public Key */
  25990. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  25991. if (ret != 0)
  25992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25993. /* add Key Usage */
  25994. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  25995. if (ret != 0)
  25996. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  25997. #endif /* WOLFSSL_CERT_EXT */
  25998. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  25999. #if defined(USE_CERT_BUFFERS_256)
  26000. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  26001. sizeof_ca_ecc_cert_der_384);
  26002. #else
  26003. ret = wc_SetIssuer(myCert, eccCaCert384File);
  26004. (void)eccCaCertFile;
  26005. #endif
  26006. #else
  26007. #if defined(USE_CERT_BUFFERS_256)
  26008. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  26009. sizeof_ca_ecc_cert_der_256);
  26010. #else
  26011. ret = wc_SetIssuer(myCert, eccCaCertFile);
  26012. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  26013. (void)eccCaCert384File;
  26014. #endif
  26015. #endif
  26016. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  26017. if (ret < 0)
  26018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26019. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  26020. if (certSz < 0) {
  26021. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  26022. }
  26023. ret = 0;
  26024. do {
  26025. #if defined(WOLFSSL_ASYNC_CRYPT)
  26026. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26027. #endif
  26028. if (ret >= 0) {
  26029. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  26030. FOURK_BUF, NULL, caEccKey, rng);
  26031. }
  26032. } while (ret == WC_PENDING_E);
  26033. if (ret < 0)
  26034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26035. certSz = (word32)ret;
  26036. TEST_SLEEP();
  26037. #ifdef WOLFSSL_TEST_CERT
  26038. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  26039. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  26040. if (ret != 0) {
  26041. FreeDecodedCert(decode);
  26042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26043. }
  26044. FreeDecodedCert(decode);
  26045. #endif
  26046. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  26047. CERT_TYPE);
  26048. if (ret != 0) {
  26049. goto exit;
  26050. }
  26051. exit:
  26052. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26053. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26054. #endif
  26055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26056. if (myCert != NULL)
  26057. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26058. #ifdef WOLFSSL_TEST_CERT
  26059. if (decode != NULL)
  26060. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26061. #endif
  26062. if (caEccKey != NULL) {
  26063. wc_ecc_free(caEccKey);
  26064. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26065. }
  26066. if (certPubKey != NULL) {
  26067. wc_ecc_free(certPubKey);
  26068. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26069. }
  26070. #else
  26071. wc_ecc_free(certPubKey);
  26072. wc_ecc_free(caEccKey);
  26073. #endif
  26074. return ret;
  26075. }
  26076. #endif /* WOLFSSL_CERT_GEN */
  26077. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  26078. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  26079. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  26080. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  26081. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  26082. {
  26083. wc_test_ret_t ret = 0;
  26084. ecc_key* key;
  26085. #ifdef WC_NO_RNG
  26086. word32 idx = 0;
  26087. #endif
  26088. key = wc_ecc_key_new(HEAP_HINT);
  26089. if (key == NULL) {
  26090. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26091. }
  26092. #ifndef WC_NO_RNG
  26093. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26094. #if defined(WOLFSSL_ASYNC_CRYPT)
  26095. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26096. #endif
  26097. if (ret != 0)
  26098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26099. #else
  26100. /* use test ECC key */
  26101. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  26102. (word32)sizeof_ecc_key_der_256);
  26103. (void)rng;
  26104. #endif
  26105. exit:
  26106. wc_ecc_key_free(key);
  26107. return ret;
  26108. }
  26109. #endif
  26110. /* ECC Non-blocking tests for Sign and Verify */
  26111. /* Requires SP math and supports P384 or P256 */
  26112. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  26113. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  26114. defined(WOLFSSL_PUBLIC_MP)
  26115. /* ECC Private Key "d" */
  26116. static const byte p256PrivKey[] = {
  26117. /* SECP256R1 */
  26118. /* d */
  26119. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  26120. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  26121. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  26122. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  26123. };
  26124. #ifdef HAVE_ECC384
  26125. static const byte p384PrivKey[] = {
  26126. /* SECP384R1 */
  26127. /* d */
  26128. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  26129. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  26130. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  26131. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  26132. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  26133. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  26134. };
  26135. #endif /* HAVE_ECC384 */
  26136. #ifdef HAVE_ECC521
  26137. static const byte p521PrivKey[] = {
  26138. /* SECP521R1 */
  26139. /* d */
  26140. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  26141. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  26142. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  26143. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  26144. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  26145. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  26146. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  26147. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  26148. 0xa4, 0x36
  26149. };
  26150. #endif /* HAVE_ECC521 */
  26151. /* ECC public key Qx/Qy */
  26152. static const byte p256PubKey[] = {
  26153. /* SECP256R1 */
  26154. /* Qx */
  26155. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  26156. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  26157. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  26158. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  26159. /* Qy */
  26160. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  26161. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  26162. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  26163. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  26164. };
  26165. #ifdef HAVE_ECC384
  26166. static const byte p384PubKey[] = {
  26167. /* SECP384R1 */
  26168. /* Qx */
  26169. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  26170. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  26171. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  26172. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  26173. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  26174. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  26175. /* Qy */
  26176. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  26177. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  26178. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  26179. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  26180. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  26181. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  26182. };
  26183. #endif
  26184. #ifdef HAVE_ECC521
  26185. static const byte p521PubKey[] = {
  26186. /* SECP521R1 */
  26187. /* Qx */
  26188. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  26189. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  26190. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  26191. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  26192. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  26193. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  26194. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  26195. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  26196. 0x8b, 0xbc,
  26197. /* Qy */
  26198. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  26199. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  26200. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  26201. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  26202. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  26203. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  26204. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  26205. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  26206. 0x91, 0xd9
  26207. };
  26208. /* perform verify of signature and hash using public key */
  26209. /* key is public Qx + public Qy */
  26210. /* sig is r + s */
  26211. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  26212. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  26213. uint32_t curveSz, int curveId)
  26214. {
  26215. wc_test_ret_t ret;
  26216. int verify_res = 0, count = 0;
  26217. mp_int r, s;
  26218. ecc_key ecc;
  26219. ecc_nb_ctx_t nb_ctx;
  26220. /* validate arguments */
  26221. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  26222. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  26223. {
  26224. return WC_TEST_RET_ENC_NC;
  26225. }
  26226. /* Setup the ECC key */
  26227. ret = wc_ecc_init(&ecc);
  26228. if (ret < 0) {
  26229. return WC_TEST_RET_ENC_EC(ret);
  26230. }
  26231. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  26232. if (ret != MP_OKAY) {
  26233. wc_ecc_free(&ecc);
  26234. return WC_TEST_RET_ENC_EC(ret);
  26235. }
  26236. /* Setup the signature r/s variables */
  26237. ret = mp_init(&r);
  26238. if (ret != MP_OKAY) {
  26239. wc_ecc_free(&ecc);
  26240. return WC_TEST_RET_ENC_EC(ret);
  26241. }
  26242. ret = mp_init(&s);
  26243. if (ret != MP_OKAY) {
  26244. mp_clear(&r);
  26245. wc_ecc_free(&ecc);
  26246. return WC_TEST_RET_ENC_EC(ret);
  26247. }
  26248. /* Import public key x/y */
  26249. ret = wc_ecc_import_unsigned(
  26250. &ecc,
  26251. (byte*)key, /* Public "x" Coordinate */
  26252. (byte*)(key + curveSz), /* Public "y" Coordinate */
  26253. NULL, /* Private "d" (optional) */
  26254. curveId /* ECC Curve Id */
  26255. );
  26256. /* Make sure it was a public key imported */
  26257. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  26258. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  26259. }
  26260. /* Import signature r/s */
  26261. if (ret == 0) {
  26262. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  26263. if (ret < 0)
  26264. ret = WC_TEST_RET_ENC_EC(ret);
  26265. }
  26266. if (ret == 0) {
  26267. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  26268. if (ret < 0)
  26269. ret = WC_TEST_RET_ENC_EC(ret);
  26270. }
  26271. /* Verify ECC Signature */
  26272. if (ret == 0) {
  26273. do {
  26274. ret = wc_ecc_verify_hash_ex(
  26275. &r, &s, /* r/s as mp_int */
  26276. hash, hashSz, /* computed hash digest */
  26277. &verify_res, /* verification result 1=success */
  26278. &ecc
  26279. );
  26280. count++;
  26281. /* This is where real-time work could be called */
  26282. } while (ret == FP_WOULDBLOCK);
  26283. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26284. printf("ECC non-block verify: %d times\n", count);
  26285. #endif
  26286. if (ret < 0)
  26287. ret = WC_TEST_RET_ENC_EC(ret);
  26288. }
  26289. /* check verify result */
  26290. if (ret == 0 && verify_res == 0) {
  26291. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  26292. }
  26293. mp_clear(&r);
  26294. mp_clear(&s);
  26295. wc_ecc_free(&ecc);
  26296. (void)count;
  26297. return ret;
  26298. }
  26299. /* perform signature operation against hash using private key */
  26300. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  26301. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  26302. uint32_t curveSz, int curveId, WC_RNG* rng)
  26303. {
  26304. wc_test_ret_t ret;
  26305. int count = 0;
  26306. mp_int r, s;
  26307. ecc_key ecc;
  26308. ecc_nb_ctx_t nb_ctx;
  26309. /* validate arguments */
  26310. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  26311. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  26312. {
  26313. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  26314. }
  26315. /* Initialize signature result */
  26316. memset(sig, 0, curveSz*2);
  26317. /* Setup the ECC key */
  26318. ret = wc_ecc_init(&ecc);
  26319. if (ret < 0) {
  26320. return WC_TEST_RET_ENC_EC(ret);
  26321. }
  26322. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  26323. if (ret != MP_OKAY) {
  26324. wc_ecc_free(&ecc);
  26325. return WC_TEST_RET_ENC_EC(ret);
  26326. }
  26327. /* Setup the signature r/s variables */
  26328. ret = mp_init(&r);
  26329. if (ret != MP_OKAY) {
  26330. wc_ecc_free(&ecc);
  26331. return WC_TEST_RET_ENC_EC(ret);
  26332. }
  26333. ret = mp_init(&s);
  26334. if (ret != MP_OKAY) {
  26335. mp_clear(&r);
  26336. wc_ecc_free(&ecc);
  26337. return WC_TEST_RET_ENC_EC(ret);
  26338. }
  26339. /* Import private key "k" */
  26340. ret = wc_ecc_import_private_key_ex(
  26341. key, keySz, /* private key "d" */
  26342. NULL, 0, /* public (optional) */
  26343. &ecc,
  26344. curveId /* ECC Curve Id */
  26345. );
  26346. if (ret < 0)
  26347. ret = WC_TEST_RET_ENC_EC(ret);
  26348. if (ret == 0) {
  26349. do {
  26350. /* Verify ECC Signature */
  26351. ret = wc_ecc_sign_hash_ex(
  26352. hash, hashSz, /* computed hash digest */
  26353. rng, &ecc, /* random and key context */
  26354. &r, &s /* r/s as mp_int */
  26355. );
  26356. count++;
  26357. /* This is where real-time work could be called */
  26358. } while (ret == FP_WOULDBLOCK);
  26359. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26360. printf("ECC non-block sign: %d times\n", count);
  26361. #endif
  26362. if (ret < 0)
  26363. ret = WC_TEST_RET_ENC_EC(ret);
  26364. }
  26365. if (ret == 0) {
  26366. /* export r/s */
  26367. mp_to_unsigned_bin_len(&r, sig, curveSz);
  26368. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  26369. }
  26370. mp_clear(&r);
  26371. mp_clear(&s);
  26372. wc_ecc_free(&ecc);
  26373. (void)count;
  26374. return ret;
  26375. }
  26376. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  26377. /*
  26378. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  26379. * don't have non-blocking versions of the key checking functions, yet.
  26380. */
  26381. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  26382. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  26383. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  26384. {
  26385. wc_test_ret_t ret;
  26386. ecc_key keyA;
  26387. ecc_key keyB;
  26388. ecc_nb_ctx_t nbCtxA;
  26389. ecc_nb_ctx_t nbCtxB;
  26390. byte secretA[ECC_SHARED_SIZE];
  26391. byte secretB[ECC_SHARED_SIZE];
  26392. word32 secretSzA = ECC_SHARED_SIZE;
  26393. word32 secretSzB = ECC_SHARED_SIZE;
  26394. int count = 0;
  26395. ret = wc_ecc_init(&keyA);
  26396. if (ret == 0) {
  26397. ret = wc_ecc_init(&keyB);
  26398. if (ret < 0)
  26399. ret = WC_TEST_RET_ENC_EC(ret);
  26400. }
  26401. if (ret == 0) {
  26402. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  26403. if (ret < 0)
  26404. ret = WC_TEST_RET_ENC_EC(ret);
  26405. }
  26406. if (ret == 0) {
  26407. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  26408. if (ret < 0)
  26409. ret = WC_TEST_RET_ENC_EC(ret);
  26410. }
  26411. if (ret == 0) {
  26412. do {
  26413. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  26414. count++;
  26415. } while (ret == FP_WOULDBLOCK);
  26416. if (ret < 0)
  26417. ret = WC_TEST_RET_ENC_EC(ret);
  26418. }
  26419. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26420. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  26421. #endif
  26422. if (ret == 0) {
  26423. ret = wc_ecc_check_key(&keyA);
  26424. if (ret < 0)
  26425. ret = WC_TEST_RET_ENC_EC(ret);
  26426. }
  26427. if (ret == 0) {
  26428. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  26429. privKey, curveId);
  26430. if (ret < 0)
  26431. ret = WC_TEST_RET_ENC_EC(ret);
  26432. }
  26433. count = 0;
  26434. if (ret == 0) {
  26435. do {
  26436. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  26437. count++;
  26438. } while (ret == FP_WOULDBLOCK);
  26439. if (ret < 0)
  26440. ret = WC_TEST_RET_ENC_EC(ret);
  26441. }
  26442. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26443. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  26444. #endif
  26445. if (ret == 0) {
  26446. do {
  26447. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  26448. } while (ret == FP_WOULDBLOCK);
  26449. if (ret < 0)
  26450. ret = WC_TEST_RET_ENC_EC(ret);
  26451. }
  26452. if (ret == 0) {
  26453. if (secretSzA != secretSzB ||
  26454. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  26455. ret = WC_TEST_RET_ENC_NC;
  26456. }
  26457. }
  26458. wc_ecc_free(&keyA);
  26459. wc_ecc_free(&keyB);
  26460. return ret;
  26461. }
  26462. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  26463. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  26464. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  26465. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  26466. WC_RNG* rng)
  26467. {
  26468. wc_test_ret_t ret = 0;
  26469. byte* sig = NULL;
  26470. word32 sigSz = curveSz * 2;
  26471. static const byte hash[] = {
  26472. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  26473. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  26474. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  26475. };
  26476. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  26477. if (sig == NULL) {
  26478. ret = WC_TEST_RET_ENC_ERRNO;
  26479. }
  26480. if (ret == 0) {
  26481. /* Sign hash using private key */
  26482. /* Note: result of an ECC sign varies for each call even with same
  26483. private key and hash. This is because a new random public key is
  26484. used for each operation. */
  26485. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  26486. &sigSz, curveSz, curveId, rng);
  26487. }
  26488. if (ret == 0) {
  26489. /* Verify generated signature is valid */
  26490. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  26491. sigSz, curveSz, curveId);
  26492. }
  26493. if (sig != NULL) {
  26494. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  26495. }
  26496. return ret;
  26497. }
  26498. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  26499. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  26500. {
  26501. wc_test_ret_t ret = 0;
  26502. word32 i;
  26503. int curveIds[3] = {0, 0, 0};
  26504. word32 curveSzs[3] = {0, 0, 0};
  26505. const byte* privKeys[3] = {NULL, NULL, NULL};
  26506. word32 privKeySzs[3] = {0, 0, 0};
  26507. const byte* pubKeys[3] = {NULL, NULL, NULL};
  26508. word32 pubKeySzs[3] = {0, 0, 0};
  26509. curveIds[0] = ECC_SECP256R1;
  26510. curveSzs[0] = 32;
  26511. privKeys[0] = p256PrivKey;
  26512. privKeySzs[0] = sizeof(p256PrivKey);
  26513. pubKeys[0] = p256PubKey;
  26514. pubKeySzs[0] = sizeof(p256PubKey);
  26515. #ifdef HAVE_ECC384
  26516. curveIds[1] = ECC_SECP384R1;
  26517. curveSzs[1] = 48;
  26518. privKeys[1] = p384PrivKey;
  26519. privKeySzs[1] = sizeof(p384PrivKey);
  26520. pubKeys[1] = p384PubKey;
  26521. pubKeySzs[1] = sizeof(p384PubKey);
  26522. #endif
  26523. #ifdef HAVE_ECC521
  26524. curveIds[2] = ECC_SECP521R1;
  26525. curveSzs[2] = 66;
  26526. privKeys[2] = p521PrivKey;
  26527. privKeySzs[2] = sizeof(p521PrivKey);
  26528. pubKeys[2] = p521PubKey;
  26529. pubKeySzs[2] = sizeof(p521PubKey);
  26530. #endif
  26531. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  26532. if (curveIds[i] == 0) {
  26533. continue;
  26534. }
  26535. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  26536. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  26537. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  26538. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  26539. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  26540. if (ret == 0) {
  26541. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  26542. pubKeys[i], rng);
  26543. }
  26544. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  26545. }
  26546. return ret;
  26547. }
  26548. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  26549. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  26550. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  26551. (HAVE_FIPS_VERSION > 2)))
  26552. static int ecc_test_raw_enc_dec(void)
  26553. {
  26554. int ret;
  26555. unsigned char r[1];
  26556. word32 rSz;
  26557. unsigned char s[1];
  26558. word32 sSz;
  26559. unsigned char rZero[] = { 0, 0, 0, 0 };
  26560. unsigned char sOne[] = { 0, 0, 1 };
  26561. unsigned char sigRaw[32];
  26562. word32 sigRawSz;
  26563. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  26564. sigRawSz = sizeof(sigRaw);
  26565. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  26566. sigRaw, &sigRawSz);
  26567. if (ret != 0) {
  26568. return WC_TEST_RET_ENC_EC(ret);
  26569. }
  26570. if (sigRawSz != sizeof(expSig)) {
  26571. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  26572. }
  26573. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  26574. return WC_TEST_RET_ENC_NC;
  26575. }
  26576. rSz = sizeof(r);
  26577. sSz = sizeof(s);
  26578. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  26579. if (ret != 0) {
  26580. return WC_TEST_RET_ENC_EC(ret);
  26581. }
  26582. if (rSz != 1) {
  26583. return WC_TEST_RET_ENC_EC((int)rSz);
  26584. }
  26585. if (sSz != 1) {
  26586. return WC_TEST_RET_ENC_EC((int)sSz);
  26587. }
  26588. if (r[0] != 0) {
  26589. return WC_TEST_RET_ENC_EC(r[0]);
  26590. }
  26591. if (s[0] != 1) {
  26592. return WC_TEST_RET_ENC_EC(s[0]);
  26593. }
  26594. return ret;
  26595. }
  26596. #endif
  26597. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  26598. {
  26599. wc_test_ret_t ret;
  26600. WC_RNG rng;
  26601. #if defined(WOLFSSL_CERT_EXT) && \
  26602. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  26603. ret = ecc_decode_test();
  26604. if (ret < 0)
  26605. return ret;
  26606. #endif
  26607. #ifndef HAVE_FIPS
  26608. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26609. #else
  26610. ret = wc_InitRng(&rng);
  26611. #endif
  26612. #ifndef WC_NO_RNG
  26613. if (ret != 0)
  26614. return WC_TEST_RET_ENC_EC(ret);
  26615. #else
  26616. (void)ret;
  26617. #endif
  26618. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  26619. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  26620. if (ret < 0) {
  26621. printf("keySize=14, Default\n");
  26622. goto done;
  26623. }
  26624. #endif /* HAVE_ECC112 */
  26625. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  26626. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  26627. if (ret < 0) {
  26628. printf("keySize=16, Default\n");
  26629. goto done;
  26630. }
  26631. #endif /* HAVE_ECC128 */
  26632. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  26633. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  26634. if (ret < 0) {
  26635. printf("keySize=20, Default\n");
  26636. goto done;
  26637. }
  26638. #endif /* HAVE_ECC160 */
  26639. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  26640. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  26641. printf("keySize=24, Default\n");
  26642. if (ret < 0) {
  26643. goto done;
  26644. }
  26645. #endif /* HAVE_ECC192 */
  26646. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  26647. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  26648. if (ret < 0) {
  26649. printf("keySize=28, Default\n");
  26650. goto done;
  26651. }
  26652. #endif /* HAVE_ECC224 */
  26653. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  26654. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  26655. if (ret < 0) {
  26656. printf("keySize=30, Default\n");
  26657. goto done;
  26658. }
  26659. #endif /* HAVE_ECC239 */
  26660. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  26661. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  26662. if (ret < 0) {
  26663. printf("keySize=32, Default\n");
  26664. goto done;
  26665. }
  26666. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  26667. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  26668. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26669. ret = ecc_point_test();
  26670. if (ret < 0) {
  26671. goto done;
  26672. }
  26673. #endif
  26674. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  26675. ret = ecc_def_curve_test(&rng);
  26676. if (ret < 0) {
  26677. printf("Default\n");
  26678. goto done;
  26679. }
  26680. #endif
  26681. #endif /* !NO_ECC256 */
  26682. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  26683. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  26684. if (ret < 0) {
  26685. printf("keySize=40, Default\n");
  26686. goto done;
  26687. }
  26688. #endif /* HAVE_ECC320 */
  26689. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  26690. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  26691. if (ret < 0) {
  26692. printf("keySize=48, Default\n");
  26693. goto done;
  26694. }
  26695. #endif /* HAVE_ECC384 */
  26696. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  26697. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  26698. if (ret < 0) {
  26699. printf("keySize=64, Default\n");
  26700. goto done;
  26701. }
  26702. #endif /* HAVE_ECC512 */
  26703. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  26704. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  26705. if (ret < 0) {
  26706. printf("keySize=66, Default\n");
  26707. goto done;
  26708. }
  26709. #endif /* HAVE_ECC521 */
  26710. #ifdef WOLFSSL_SM2
  26711. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  26712. if (ret < 0) {
  26713. printf("SM2\n");
  26714. goto done;
  26715. }
  26716. #endif
  26717. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  26718. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  26719. (HAVE_FIPS_VERSION > 2)))
  26720. ret = ecc_test_raw_enc_dec();
  26721. if (ret != 0) {
  26722. printf("raw sig encode/decode\n");
  26723. goto done;
  26724. }
  26725. #endif
  26726. #if defined(WOLFSSL_CUSTOM_CURVES)
  26727. ret = ecc_test_custom_curves(&rng);
  26728. if (ret != 0) {
  26729. printf("Custom\n");
  26730. goto done;
  26731. }
  26732. #endif
  26733. #if defined(WOLFSSL_SM2)
  26734. ret = test_sm2_verify();
  26735. if (ret != 0) {
  26736. printf("SM2 Verify\n");
  26737. goto done;
  26738. }
  26739. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  26740. if (ret != 0) {
  26741. printf("SM2 test\n");
  26742. goto done;
  26743. }
  26744. #endif
  26745. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  26746. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  26747. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  26748. #ifdef HAVE_ECC256
  26749. ret = ecc_test_deterministic_k(&rng);
  26750. if (ret != 0) {
  26751. printf("ecc_test_deterministic_k failed!\n");
  26752. goto done;
  26753. }
  26754. #endif
  26755. #ifdef WOLFSSL_PUBLIC_MP
  26756. #if defined(HAVE_ECC384)
  26757. ret = ecc384_test_deterministic_k(&rng);
  26758. if (ret != 0) {
  26759. printf("ecc384_test_deterministic_k failed!\n");
  26760. goto done;
  26761. }
  26762. #endif
  26763. #if defined(HAVE_ECC521)
  26764. ret = ecc521_test_deterministic_k(&rng);
  26765. if (ret != 0) {
  26766. printf("ecc512_test_deterministic_k failed!\n");
  26767. goto done;
  26768. }
  26769. #endif
  26770. #endif
  26771. #endif
  26772. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  26773. !defined(WOLFSSL_KCAPI_ECC)
  26774. ret = ecc_test_sign_vectors(&rng);
  26775. if (ret != 0) {
  26776. printf("ecc_test_sign_vectors failed!\n");
  26777. goto done;
  26778. }
  26779. #endif
  26780. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  26781. defined(HAVE_ECC_DHE)
  26782. ret = ecc_test_cdh_vectors(&rng);
  26783. if (ret != 0) {
  26784. printf("ecc_test_cdh_vectors failed!\n");
  26785. goto done;
  26786. }
  26787. #endif
  26788. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  26789. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  26790. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  26791. ret = ecc_test_make_pub(&rng);
  26792. if (ret != 0) {
  26793. printf("ecc_test_make_pub failed!\n");
  26794. goto done;
  26795. }
  26796. #elif defined(HAVE_ECC_KEY_IMPORT)
  26797. (void)ecc_test_make_pub; /* for compiler warning */
  26798. #endif
  26799. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  26800. ret = ecc_test_cert_gen(&rng);
  26801. if (ret != 0) {
  26802. printf("ecc_test_cert_gen failed!\n");
  26803. goto done;
  26804. }
  26805. #endif
  26806. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  26807. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  26808. defined(WOLFSSL_CUSTOM_CURVES))
  26809. ret = ecc_test_allocator(&rng);
  26810. if (ret != 0) {
  26811. printf("ecc_test_allocator failed!\n");
  26812. goto done;
  26813. }
  26814. #endif
  26815. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  26816. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  26817. ret = ecc_test_nonblock(&rng);
  26818. if (ret != 0) {
  26819. printf("ecc_test_nonblock failed!\n");
  26820. goto done;
  26821. }
  26822. #endif
  26823. done:
  26824. wc_FreeRng(&rng);
  26825. return ret;
  26826. }
  26827. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  26828. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  26829. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  26830. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  26831. {
  26832. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26833. byte* plaintext;
  26834. byte* encrypted;
  26835. byte* decrypted;
  26836. #else
  26837. byte plaintext[128];
  26838. byte encrypted[128];
  26839. byte decrypted[128];
  26840. #endif
  26841. ecEncCtx* aCtx = NULL;
  26842. ecEncCtx* bCtx = NULL;
  26843. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  26844. 14, 15};
  26845. wc_test_ret_t ret = 0;
  26846. static const char message[] = "Hello wolfSSL!";
  26847. word32 plaintextLen;
  26848. word32 encryptLen = 128;
  26849. word32 decryptLen = 128;
  26850. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26851. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26852. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26853. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26854. #endif
  26855. wc_ecc_free(a);
  26856. wc_ecc_free(b);
  26857. ret = wc_ecc_init(a);
  26858. if (ret != 0)
  26859. ret = WC_TEST_RET_ENC_EC(ret);
  26860. if (ret == 0) {
  26861. ret = wc_ecc_init(b);
  26862. if (ret != 0)
  26863. ret = WC_TEST_RET_ENC_EC(ret);
  26864. }
  26865. if (ret == 0)
  26866. ret = wc_ecc_make_key(rng, 32, a);
  26867. if (ret == 0)
  26868. ret = wc_ecc_make_key(rng, 32, b);
  26869. /* create context */
  26870. if (ret == 0) {
  26871. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  26872. if (aCtx == NULL)
  26873. ret = WC_TEST_RET_ENC_NC;
  26874. }
  26875. if (ret == 0) {
  26876. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  26877. if (bCtx == NULL)
  26878. ret = WC_TEST_RET_ENC_NC;
  26879. }
  26880. /* set salt */
  26881. if (ret == 0) {
  26882. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  26883. if (ret != 0)
  26884. ret = 10472;
  26885. }
  26886. if (ret == 0) {
  26887. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  26888. if (ret != 0)
  26889. ret = 10473;
  26890. }
  26891. XMEMSET(plaintext, 0, 128);
  26892. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  26893. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  26894. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  26895. /* encrypt */
  26896. if (ret == 0) {
  26897. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  26898. &encryptLen, aCtx);
  26899. if (ret != 0)
  26900. ret = WC_TEST_RET_ENC_EC(ret);
  26901. }
  26902. /* decrypt */
  26903. if (ret == 0) {
  26904. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  26905. &decryptLen, bCtx);
  26906. if (ret != 0)
  26907. ret = WC_TEST_RET_ENC_EC(ret);
  26908. }
  26909. /* compare */
  26910. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  26911. ret = WC_TEST_RET_ENC_NC;
  26912. wc_ecc_free(a);
  26913. wc_ecc_free(b);
  26914. wc_ecc_ctx_free(aCtx);
  26915. wc_ecc_ctx_free(bCtx);
  26916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26917. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26918. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26919. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26920. #endif
  26921. return ret;
  26922. }
  26923. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  26924. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  26925. * wolfFIPS 5.3.
  26926. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  26927. */
  26928. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  26929. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  26930. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  26931. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  26932. {
  26933. wc_test_ret_t ret = 0;
  26934. #ifdef WOLFSSL_ECIES_OLD
  26935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26936. ecc_key* userA = NULL;
  26937. #else
  26938. ecc_key userA[1];
  26939. #endif
  26940. int userAInit = 0;
  26941. #endif
  26942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26943. ecc_key* userB = NULL;
  26944. #else
  26945. ecc_key userB[1];
  26946. #endif
  26947. int userBInit = 0;
  26948. ecc_key* tmpKey;
  26949. byte plain[48];
  26950. word32 plainSz = sizeof(plain);
  26951. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  26952. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  26953. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  26954. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  26955. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  26956. };
  26957. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  26958. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  26959. 0x04,
  26960. /* X */
  26961. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  26962. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  26963. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  26964. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  26965. /* X */
  26966. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  26967. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  26968. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  26969. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  26970. };
  26971. #endif
  26972. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  26973. #ifdef WOLFSSL_ECIES_OLD
  26974. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  26975. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  26976. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  26977. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  26978. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  26979. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  26980. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  26981. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  26982. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  26983. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  26984. #elif defined(WOLFSSL_ECIES_ISO18033)
  26985. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  26986. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  26987. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  26988. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  26989. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  26990. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  26991. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  26992. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  26993. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  26994. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  26995. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  26996. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  26997. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  26998. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  26999. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  27000. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  27001. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  27002. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  27003. 0x7f
  27004. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27005. /* EC P-256 point */
  27006. 0x04,
  27007. /* X */
  27008. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  27009. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  27010. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  27011. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  27012. /* Y */
  27013. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  27014. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  27015. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  27016. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  27017. /* IV */
  27018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27020. /* Encrypted Msg */
  27021. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  27022. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  27023. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  27024. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  27025. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  27026. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  27027. /* HMAC */
  27028. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  27029. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  27030. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  27031. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  27032. #else
  27033. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  27034. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  27035. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  27036. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  27037. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  27038. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  27039. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  27040. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  27041. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  27042. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  27043. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  27044. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  27045. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  27046. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  27047. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  27048. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  27049. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  27050. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  27051. 0x55
  27052. #endif
  27053. };
  27054. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  27055. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27056. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  27057. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  27058. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  27059. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  27060. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  27061. };
  27062. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27063. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  27064. DYNAMIC_TYPE_TMP_BUFFER);
  27065. if (userB == NULL) {
  27066. ret = WC_TEST_RET_ENC_ERRNO;
  27067. }
  27068. #ifdef WOLFSSL_ECIES_OLD
  27069. if (ret == 0) {
  27070. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  27071. DYNAMIC_TYPE_TMP_BUFFER);
  27072. if (userA == NULL) {
  27073. ret = WC_TEST_RET_ENC_ERRNO;
  27074. }
  27075. }
  27076. #endif
  27077. #endif
  27078. if (ret == 0) {
  27079. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  27080. if (ret != 0)
  27081. ret = WC_TEST_RET_ENC_EC(ret);
  27082. }
  27083. if (ret == 0) {
  27084. userBInit = 1;
  27085. #ifdef WOLFSSL_ECIES_OLD
  27086. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  27087. if (ret != 0)
  27088. ret = WC_TEST_RET_ENC_EC(ret);
  27089. }
  27090. if (ret == 0) {
  27091. userAInit = 1;
  27092. tmpKey = userA;
  27093. #else
  27094. tmpKey = NULL;
  27095. #endif
  27096. }
  27097. if (ret == 0) {
  27098. #ifdef WOLFSSL_QNX_CAAM
  27099. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  27100. sizeof(pubKey), userB, ECC_SECP256R1);
  27101. #else
  27102. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  27103. userB, ECC_SECP256R1);
  27104. #endif
  27105. if (ret != 0)
  27106. ret = WC_TEST_RET_ENC_EC(ret);
  27107. }
  27108. #ifdef WOLFSSL_ECIES_OLD
  27109. if (ret == 0) {
  27110. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  27111. ECC_SECP256R1);
  27112. if (ret != 0)
  27113. ret = WC_TEST_RET_ENC_EC(ret);
  27114. }
  27115. #endif
  27116. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27117. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27118. !defined(HAVE_SELFTEST)
  27119. if (ret == 0) {
  27120. ret = wc_ecc_set_rng(userB, rng);
  27121. if (ret != 0) {
  27122. ret = WC_TEST_RET_ENC_EC(ret);
  27123. }
  27124. }
  27125. #else
  27126. (void)rng;
  27127. #endif
  27128. if (ret == 0) {
  27129. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  27130. &plainSz, NULL);
  27131. if (ret != 0)
  27132. ret = WC_TEST_RET_ENC_EC(ret);
  27133. }
  27134. if (ret == 0) {
  27135. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27136. ret = WC_TEST_RET_ENC_NC;
  27137. }
  27138. }
  27139. if (userBInit)
  27140. wc_ecc_free(userB);
  27141. #ifdef WOLFSSL_ECIES_OLD
  27142. if (userAInit)
  27143. wc_ecc_free(userA);
  27144. #endif
  27145. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27146. if (userB != NULL) {
  27147. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27148. }
  27149. #ifdef WOLFSSL_ECIES_OLD
  27150. if (userA != NULL) {
  27151. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27152. }
  27153. #endif
  27154. #endif
  27155. return ret;
  27156. }
  27157. #endif
  27158. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  27159. byte encAlgo, byte kdfAlgo, byte macAlgo)
  27160. {
  27161. wc_test_ret_t ret = 0;
  27162. byte msg[48];
  27163. byte plain[48];
  27164. #ifdef WOLFSSL_ECIES_OLD
  27165. byte out[80];
  27166. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27167. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  27168. #else
  27169. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  27170. #endif
  27171. word32 outSz = sizeof(out);
  27172. word32 plainSz = sizeof(plain);
  27173. int i;
  27174. ecEncCtx* cliCtx = NULL;
  27175. ecEncCtx* srvCtx = NULL;
  27176. byte cliSalt[EXCHANGE_SALT_SZ];
  27177. byte srvSalt[EXCHANGE_SALT_SZ];
  27178. const byte* tmpSalt;
  27179. byte msg2[48];
  27180. byte plain2[48];
  27181. #ifdef WOLFSSL_ECIES_OLD
  27182. byte out2[80];
  27183. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27184. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  27185. #else
  27186. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  27187. #endif
  27188. word32 outSz2 = sizeof(out2);
  27189. word32 plainSz2 = sizeof(plain2);
  27190. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27191. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  27192. DYNAMIC_TYPE_TMP_BUFFER);
  27193. #else
  27194. ecc_key tmpKey[1];
  27195. #endif
  27196. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27197. if (tmpKey == NULL) {
  27198. ERROR_OUT(MEMORY_E, done);
  27199. }
  27200. #endif
  27201. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  27202. if (ret != 0)
  27203. goto done;
  27204. /* set message to incrementing 0,1,2,etc... */
  27205. for (i = 0; i < (int)sizeof(msg); i++)
  27206. msg[i] = i;
  27207. /* encrypt msg to B */
  27208. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  27209. if (ret != 0) {
  27210. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27211. }
  27212. #ifdef WOLFSSL_ECIES_OLD
  27213. tmpKey->dp = userA->dp;
  27214. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  27215. if (ret != 0) {
  27216. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27217. }
  27218. #endif
  27219. /* decrypt msg from A */
  27220. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  27221. if (ret != 0) {
  27222. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27223. }
  27224. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27225. ret = WC_TEST_RET_ENC_NC; goto done;
  27226. }
  27227. #ifndef WOLFSSL_ECIES_OLD
  27228. /* A decrypts msg (response) from B */
  27229. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  27230. if (ret != 0)
  27231. goto done;
  27232. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27233. ret = WC_TEST_RET_ENC_NC; goto done;
  27234. }
  27235. #endif
  27236. /* let's verify message exchange works, A is client, B is server */
  27237. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  27238. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  27239. if (cliCtx == NULL || srvCtx == NULL) {
  27240. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  27241. }
  27242. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  27243. if (ret != 0)
  27244. goto done;
  27245. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  27246. if (ret != 0)
  27247. goto done;
  27248. /* get salt to send to peer */
  27249. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  27250. if (tmpSalt == NULL) {
  27251. ret = WC_TEST_RET_ENC_NC; goto done;
  27252. }
  27253. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27254. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  27255. if (tmpSalt == NULL) {
  27256. ret = WC_TEST_RET_ENC_NC; goto done;
  27257. }
  27258. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27259. /* in actual use, we'd get the peer's salt over the transport */
  27260. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  27261. if (ret != 0)
  27262. goto done;
  27263. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  27264. if (ret != 0)
  27265. goto done;
  27266. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  27267. if (ret != 0)
  27268. goto done;
  27269. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  27270. if (ret != 0)
  27271. goto done;
  27272. /* get encrypted msg (request) to send to B */
  27273. outSz = sizeof(out);
  27274. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  27275. if (ret != 0)
  27276. goto done;
  27277. #ifndef WOLFSSL_ECIES_OLD
  27278. wc_ecc_free(tmpKey);
  27279. #endif
  27280. /* B decrypts msg (request) from A */
  27281. plainSz = sizeof(plain);
  27282. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  27283. if (ret != 0)
  27284. goto done;
  27285. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27286. ret = WC_TEST_RET_ENC_NC; goto done;
  27287. }
  27288. /* msg2 (response) from B to A */
  27289. for (i = 0; i < (int)sizeof(msg2); i++)
  27290. msg2[i] = i + sizeof(msg2);
  27291. /* get encrypted msg (response) to send to B */
  27292. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  27293. &outSz2, srvCtx);
  27294. if (ret != 0)
  27295. goto done;
  27296. #ifdef WOLFSSL_ECIES_OLD
  27297. tmpKey->dp = userB->dp;
  27298. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  27299. if (ret != 0) {
  27300. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27301. }
  27302. #else
  27303. wc_ecc_free(tmpKey);
  27304. #endif
  27305. /* A decrypts msg (response) from B */
  27306. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  27307. cliCtx);
  27308. if (ret != 0)
  27309. goto done;
  27310. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  27311. ret = WC_TEST_RET_ENC_NC; goto done;
  27312. }
  27313. #if defined(HAVE_COMP_KEY) && \
  27314. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  27315. /* Create new client and server contexts. */
  27316. wc_ecc_ctx_free(srvCtx);
  27317. wc_ecc_ctx_free(cliCtx);
  27318. /* let's verify message exchange works, A is client, B is server */
  27319. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  27320. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  27321. if (cliCtx == NULL || srvCtx == NULL) {
  27322. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  27323. }
  27324. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  27325. if (ret != 0)
  27326. goto done;
  27327. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  27328. if (ret != 0)
  27329. goto done;
  27330. /* get salt to send to peer */
  27331. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  27332. if (tmpSalt == NULL) {
  27333. ret = WC_TEST_RET_ENC_NC; goto done;
  27334. }
  27335. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27336. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  27337. if (tmpSalt == NULL) {
  27338. ret = WC_TEST_RET_ENC_NC; goto done;
  27339. }
  27340. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27341. /* in actual use, we'd get the peer's salt over the transport */
  27342. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  27343. if (ret != 0)
  27344. goto done;
  27345. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  27346. if (ret != 0)
  27347. goto done;
  27348. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  27349. if (ret != 0)
  27350. goto done;
  27351. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  27352. if (ret != 0)
  27353. goto done;
  27354. /* get encrypted msg (request) to send to B - compressed public key */
  27355. outSz = sizeof(out);
  27356. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  27357. 1);
  27358. if (ret != 0)
  27359. goto done;
  27360. #ifndef WOLFSSL_ECIES_OLD
  27361. wc_ecc_free(tmpKey);
  27362. #endif
  27363. /* B decrypts msg (request) from A - out has a compressed public key */
  27364. plainSz = sizeof(plain);
  27365. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  27366. if (ret != 0)
  27367. goto done;
  27368. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27369. ret = WC_TEST_RET_ENC_NC; goto done;
  27370. }
  27371. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  27372. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  27373. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  27374. ret = ecc_encrypt_kat(rng);
  27375. #endif
  27376. done:
  27377. /* cleanup */
  27378. wc_ecc_ctx_free(srvCtx);
  27379. wc_ecc_ctx_free(cliCtx);
  27380. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27381. if (tmpKey != NULL) {
  27382. wc_ecc_free(tmpKey);
  27383. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27384. }
  27385. #else
  27386. wc_ecc_free(tmpKey);
  27387. #endif
  27388. return ret;
  27389. }
  27390. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  27391. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  27392. {
  27393. WC_RNG rng;
  27394. wc_test_ret_t ret;
  27395. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27396. ecc_key *userA;
  27397. ecc_key *userB;
  27398. #else
  27399. ecc_key userA[1];
  27400. ecc_key userB[1];
  27401. #endif
  27402. #ifndef HAVE_FIPS
  27403. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27404. #else
  27405. ret = wc_InitRng(&rng);
  27406. #endif
  27407. if (ret != 0)
  27408. return WC_TEST_RET_ENC_EC(ret);
  27409. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27410. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  27411. DYNAMIC_TYPE_TMP_BUFFER);
  27412. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  27413. DYNAMIC_TYPE_TMP_BUFFER);
  27414. if ((userA == NULL) || (userB == NULL)) {
  27415. ERROR_OUT(MEMORY_E, done);
  27416. }
  27417. #endif
  27418. XMEMSET(userA, 0, sizeof *userA);
  27419. XMEMSET(userB, 0, sizeof *userB);
  27420. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  27421. if (ret != 0)
  27422. goto done;
  27423. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  27424. if (ret != 0)
  27425. goto done;
  27426. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  27427. #if defined(WOLFSSL_ASYNC_CRYPT)
  27428. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  27429. #endif
  27430. if (ret != 0){
  27431. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27432. }
  27433. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  27434. #if defined(WOLFSSL_ASYNC_CRYPT)
  27435. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  27436. #endif
  27437. if (ret != 0){
  27438. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27439. }
  27440. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27441. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27442. !defined(HAVE_SELFTEST)
  27443. ret = wc_ecc_set_rng(userA, &rng);
  27444. if (ret != 0) {
  27445. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27446. }
  27447. ret = wc_ecc_set_rng(userB, &rng);
  27448. if (ret != 0) {
  27449. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27450. }
  27451. #endif
  27452. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  27453. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27454. #ifdef WOLFSSL_AES_128
  27455. if (ret == 0) {
  27456. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  27457. ecHKDF_SHA256, ecHMAC_SHA256);
  27458. if (ret != 0) {
  27459. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  27460. }
  27461. }
  27462. #ifdef HAVE_X963_KDF
  27463. if (ret == 0) {
  27464. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  27465. ecKDF_X963_SHA256, ecHMAC_SHA256);
  27466. if (ret != 0) {
  27467. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  27468. }
  27469. }
  27470. if (ret == 0) {
  27471. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  27472. ecKDF_SHA256, ecHMAC_SHA256);
  27473. if (ret != 0) {
  27474. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  27475. }
  27476. }
  27477. #endif
  27478. #endif
  27479. #ifdef WOLFSSL_AES_256
  27480. if (ret == 0) {
  27481. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  27482. ecHKDF_SHA256, ecHMAC_SHA256);
  27483. if (ret != 0) {
  27484. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  27485. }
  27486. }
  27487. #endif
  27488. #endif
  27489. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  27490. #ifdef WOLFSSL_AES_128
  27491. if (ret == 0) {
  27492. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  27493. ecHKDF_SHA256, ecHMAC_SHA256);
  27494. if (ret != 0) {
  27495. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  27496. }
  27497. }
  27498. #endif
  27499. #ifdef WOLFSSL_AES_256
  27500. if (ret == 0) {
  27501. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  27502. ecHKDF_SHA256, ecHMAC_SHA256);
  27503. if (ret != 0) {
  27504. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  27505. }
  27506. }
  27507. #endif
  27508. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  27509. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27510. if (ret == 0) {
  27511. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  27512. }
  27513. #endif
  27514. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  27515. done:
  27516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27517. if (userA != NULL) {
  27518. wc_ecc_free(userA);
  27519. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27520. }
  27521. if (userB != NULL) {
  27522. wc_ecc_free(userB);
  27523. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27524. }
  27525. #else
  27526. wc_ecc_free(userB);
  27527. wc_ecc_free(userA);
  27528. #endif
  27529. wc_FreeRng(&rng);
  27530. return ret;
  27531. }
  27532. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  27533. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  27534. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  27535. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  27536. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  27537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  27538. {
  27539. size_t bytes;
  27540. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27541. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27542. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27543. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27544. #else
  27545. ecc_key cliKey[1];
  27546. ecc_key servKey[1];
  27547. ecc_key tmpKey[1];
  27548. #endif
  27549. WC_RNG rng;
  27550. word32 idx = 0;
  27551. wc_test_ret_t ret;
  27552. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  27553. byte in[] = "Everyone gets Friday off. ecc p";
  27554. word32 inLen = (word32)XSTRLEN((char*)in);
  27555. byte out[256];
  27556. byte plain[256];
  27557. int verify = 0;
  27558. word32 x;
  27559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27560. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  27561. ERROR_OUT(MEMORY_E, done);
  27562. #endif
  27563. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  27564. if (ret != 0)
  27565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27566. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  27567. if (ret != 0)
  27568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27569. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  27570. if (ret != 0)
  27571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27572. bytes = (size_t)sizeof_ecc_clikey_der_256;
  27573. /* place client key into ecc_key struct cliKey */
  27574. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  27575. (word32)bytes);
  27576. if (ret != 0)
  27577. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27578. idx = 0;
  27579. bytes = (size_t)sizeof_ecc_key_der_256;
  27580. /* place server key into ecc_key struct servKey */
  27581. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  27582. (word32)bytes);
  27583. if (ret != 0)
  27584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27585. #ifndef WC_NO_RNG
  27586. #ifndef HAVE_FIPS
  27587. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27588. #else
  27589. ret = wc_InitRng(&rng);
  27590. #endif
  27591. if (ret != 0)
  27592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27593. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27594. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27595. !defined(HAVE_SELFTEST)
  27596. ret = wc_ecc_set_rng(cliKey, &rng);
  27597. if (ret != 0)
  27598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27599. ret = wc_ecc_set_rng(servKey, &rng);
  27600. if (ret != 0)
  27601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27602. #endif
  27603. #endif /* !WC_NO_RNG */
  27604. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  27605. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  27606. {
  27607. word32 y;
  27608. /* test encrypt and decrypt if they're available */
  27609. x = sizeof(out);
  27610. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  27611. if (ret < 0)
  27612. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27613. #ifdef WOLFSSL_ECIES_OLD
  27614. tmpKey->dp = cliKey->dp;
  27615. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  27616. if (ret != 0) {
  27617. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27618. }
  27619. #endif
  27620. y = sizeof(plain);
  27621. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  27622. if (ret < 0)
  27623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27624. if (XMEMCMP(plain, in, inLen))
  27625. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27626. }
  27627. #endif
  27628. x = sizeof(out);
  27629. do {
  27630. #if defined(WOLFSSL_ASYNC_CRYPT)
  27631. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27632. #endif
  27633. if (ret == 0)
  27634. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  27635. } while (ret == WC_PENDING_E);
  27636. if (ret < 0)
  27637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27638. TEST_SLEEP();
  27639. XMEMSET(plain, 0, sizeof(plain));
  27640. do {
  27641. #if defined(WOLFSSL_ASYNC_CRYPT)
  27642. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27643. #endif
  27644. if (ret == 0)
  27645. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  27646. cliKey);
  27647. } while (ret == WC_PENDING_E);
  27648. if (ret < 0)
  27649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27650. if (verify != 1)
  27651. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27652. TEST_SLEEP();
  27653. #ifdef WOLFSSL_CERT_EXT
  27654. idx = 0;
  27655. bytes = sizeof_ecc_clikeypub_der_256;
  27656. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  27657. (word32) bytes);
  27658. if (ret != 0)
  27659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27660. #endif
  27661. ret = 0;
  27662. done:
  27663. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27664. if (cliKey != NULL) {
  27665. wc_ecc_free(cliKey);
  27666. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27667. }
  27668. if (servKey != NULL) {
  27669. wc_ecc_free(servKey);
  27670. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27671. }
  27672. if (tmpKey != NULL) {
  27673. wc_ecc_free(tmpKey);
  27674. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27675. }
  27676. #else
  27677. wc_ecc_free(cliKey);
  27678. wc_ecc_free(servKey);
  27679. wc_ecc_free(tmpKey);
  27680. #endif
  27681. wc_FreeRng(&rng);
  27682. return ret;
  27683. }
  27684. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  27685. #endif /* HAVE_ECC */
  27686. #ifdef HAVE_CURVE25519
  27687. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  27688. defined(HAVE_CURVE25519_KEY_IMPORT)
  27689. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  27690. #define X25519_TEST_CNT 5
  27691. #else
  27692. #define X25519_TEST_CNT 1
  27693. #endif
  27694. static wc_test_ret_t curve25519_overflow_test(void)
  27695. {
  27696. /* secret key for party a */
  27697. byte sa[X25519_TEST_CNT][32] = {
  27698. {
  27699. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  27700. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  27701. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  27702. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  27703. },
  27704. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  27705. {
  27706. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  27707. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  27708. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  27709. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  27710. },
  27711. {
  27712. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  27713. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  27714. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  27715. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  27716. },
  27717. {
  27718. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  27719. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  27720. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  27721. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  27722. },
  27723. {
  27724. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  27725. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  27726. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  27727. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  27728. }
  27729. #endif
  27730. };
  27731. /* public key for party b */
  27732. byte pb[X25519_TEST_CNT][32] = {
  27733. {
  27734. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27735. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27736. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27737. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  27738. },
  27739. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  27740. {
  27741. /* 0xff first byte in original - invalid! */
  27742. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27743. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27744. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  27745. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  27746. },
  27747. {
  27748. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  27749. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  27750. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  27751. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  27752. },
  27753. {
  27754. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  27755. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  27756. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  27757. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  27758. },
  27759. {
  27760. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  27761. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  27762. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  27763. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  27764. }
  27765. #endif
  27766. };
  27767. /* expected shared key */
  27768. byte ss[X25519_TEST_CNT][32] = {
  27769. {
  27770. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  27771. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  27772. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  27773. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  27774. },
  27775. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  27776. {
  27777. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  27778. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  27779. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  27780. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  27781. },
  27782. {
  27783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27785. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27786. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  27787. },
  27788. {
  27789. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27790. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27792. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  27793. },
  27794. {
  27795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  27799. }
  27800. #endif
  27801. };
  27802. wc_test_ret_t ret = 0;
  27803. int i;
  27804. word32 y;
  27805. byte shared[32];
  27806. curve25519_key userA;
  27807. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  27808. for (i = 0; i < X25519_TEST_CNT; i++) {
  27809. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  27810. sizeof(pb[i]), &userA) != 0) {
  27811. ret = WC_TEST_RET_ENC_I(i); break;
  27812. }
  27813. /* test against known test vector */
  27814. XMEMSET(shared, 0, sizeof(shared));
  27815. y = sizeof(shared);
  27816. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  27817. ret = WC_TEST_RET_ENC_I(i); break;
  27818. }
  27819. if (XMEMCMP(ss[i], shared, y)) {
  27820. ret = WC_TEST_RET_ENC_I(i); break;
  27821. }
  27822. }
  27823. wc_curve25519_free(&userA);
  27824. return ret;
  27825. }
  27826. /* Test the wc_curve25519_check_public API.
  27827. *
  27828. * returns 0 on success and -ve on failure.
  27829. */
  27830. static wc_test_ret_t curve25519_check_public_test(void)
  27831. {
  27832. wc_test_ret_t ret;
  27833. /* Little-endian values that will fail */
  27834. byte fail_le[][CURVE25519_KEYSIZE] = {
  27835. {
  27836. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27840. },
  27841. {
  27842. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27843. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27844. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27845. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27846. },
  27847. {
  27848. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27849. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27850. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27851. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  27852. },
  27853. };
  27854. /* Big-endian values that will fail */
  27855. byte fail_be[][CURVE25519_KEYSIZE] = {
  27856. {
  27857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27860. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27861. },
  27862. {
  27863. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27864. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27865. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27866. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27867. },
  27868. {
  27869. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27870. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27871. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27872. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27873. },
  27874. };
  27875. /* Good or valid public value */
  27876. byte good[CURVE25519_KEYSIZE] = {
  27877. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27878. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27879. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27880. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27881. };
  27882. int i;
  27883. /* Parameter checks */
  27884. /* NULL pointer */
  27885. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  27886. if (ret != BAD_FUNC_ARG) {
  27887. return WC_TEST_RET_ENC_EC(ret);
  27888. }
  27889. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  27890. if (ret != BAD_FUNC_ARG) {
  27891. return WC_TEST_RET_ENC_EC(ret);
  27892. }
  27893. /* Length of 0 treated differently to other invalid lengths for TLS */
  27894. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  27895. if (ret != BUFFER_E)
  27896. return WC_TEST_RET_ENC_EC(ret);
  27897. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  27898. if (ret != BUFFER_E)
  27899. return WC_TEST_RET_ENC_EC(ret);
  27900. /* Length not CURVE25519_KEYSIZE */
  27901. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  27902. if (i == CURVE25519_KEYSIZE)
  27903. continue;
  27904. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  27905. ECC_BAD_ARG_E) {
  27906. return WC_TEST_RET_ENC_I(i);
  27907. }
  27908. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  27909. ECC_BAD_ARG_E) {
  27910. return WC_TEST_RET_ENC_I(i);
  27911. }
  27912. }
  27913. /* Little-endian fail cases */
  27914. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  27915. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  27916. EC25519_LITTLE_ENDIAN) == 0) {
  27917. return WC_TEST_RET_ENC_I(i);
  27918. }
  27919. }
  27920. /* Big-endian fail cases */
  27921. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  27922. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  27923. EC25519_BIG_ENDIAN) == 0) {
  27924. return WC_TEST_RET_ENC_I(i);
  27925. }
  27926. }
  27927. /* Check a valid public value works! */
  27928. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  27929. EC25519_LITTLE_ENDIAN);
  27930. if (ret != 0) {
  27931. return WC_TEST_RET_ENC_EC(ret);
  27932. }
  27933. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  27934. EC25519_BIG_ENDIAN);
  27935. if (ret != 0) {
  27936. return WC_TEST_RET_ENC_EC(ret);
  27937. }
  27938. return 0;
  27939. }
  27940. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  27941. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  27942. defined(HAVE_CURVE25519_KEY_IMPORT)
  27943. static wc_test_ret_t curve255519_der_test(void)
  27944. {
  27945. wc_test_ret_t ret = 0;
  27946. /* certs/statickeys/x25519.der */
  27947. const byte kCurve25519PrivDer[] = {
  27948. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  27949. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  27950. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  27951. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  27952. };
  27953. /* certs/statickeys/x25519-pub.der */
  27954. const byte kCurve25519PubDer[] = {
  27955. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  27956. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  27957. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  27958. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  27959. };
  27960. curve25519_key key;
  27961. byte output[128];
  27962. word32 outputSz = 128;
  27963. word32 idx;
  27964. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  27965. if (ret != 0)
  27966. return WC_TEST_RET_ENC_EC(ret);
  27967. /* Test decode / encode of Curve25519 private key only */
  27968. if (ret == 0) {
  27969. idx = 0;
  27970. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  27971. (word32)sizeof(kCurve25519PrivDer));
  27972. if (ret < 0)
  27973. ret = WC_TEST_RET_ENC_EC(ret);
  27974. }
  27975. if (ret == 0) {
  27976. outputSz = (word32)sizeof(output);
  27977. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  27978. if (ret >= 0) {
  27979. outputSz = (word32)ret;
  27980. ret = 0;
  27981. }
  27982. else {
  27983. ret = WC_TEST_RET_ENC_EC(ret);
  27984. }
  27985. }
  27986. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  27987. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  27988. ret = WC_TEST_RET_ENC_NC;
  27989. }
  27990. /* Test decode / encode of Curve25519 public key only */
  27991. if (ret == 0) {
  27992. idx = 0;
  27993. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  27994. (word32)sizeof(kCurve25519PubDer));
  27995. if (ret < 0)
  27996. ret = WC_TEST_RET_ENC_EC(ret);
  27997. }
  27998. if (ret == 0) {
  27999. outputSz = (word32)sizeof(output);
  28000. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  28001. if (ret >= 0) {
  28002. outputSz = (word32)ret;
  28003. ret = 0;
  28004. }
  28005. else {
  28006. ret = WC_TEST_RET_ENC_EC(ret);
  28007. }
  28008. }
  28009. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  28010. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  28011. ret = WC_TEST_RET_ENC_NC;
  28012. }
  28013. wc_curve25519_free(&key);
  28014. return ret;
  28015. }
  28016. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  28017. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  28018. {
  28019. WC_RNG rng;
  28020. wc_test_ret_t ret;
  28021. #ifdef HAVE_CURVE25519_SHARED_SECRET
  28022. byte sharedA[32];
  28023. byte sharedB[32];
  28024. word32 y;
  28025. #endif
  28026. #ifdef HAVE_CURVE25519_KEY_EXPORT
  28027. byte exportBuf[32];
  28028. #endif
  28029. word32 x = 0;
  28030. curve25519_key userA, userB, pubKey;
  28031. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  28032. defined(HAVE_CURVE25519_KEY_IMPORT)
  28033. /* test vectors from
  28034. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  28035. */
  28036. /* secret key for party a */
  28037. byte sa[] = {
  28038. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  28039. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  28040. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  28041. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  28042. };
  28043. /* public key for party a */
  28044. byte pa[] = {
  28045. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  28046. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  28047. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  28048. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  28049. };
  28050. /* secret key for party b */
  28051. byte sb[] = {
  28052. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  28053. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  28054. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  28055. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  28056. };
  28057. /* public key for party b */
  28058. byte pb[] = {
  28059. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  28060. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  28061. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  28062. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  28063. };
  28064. /* expected shared key */
  28065. byte ss[] = {
  28066. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  28067. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  28068. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  28069. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  28070. };
  28071. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  28072. (void)x;
  28073. #ifndef HAVE_FIPS
  28074. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28075. #else
  28076. ret = wc_InitRng(&rng);
  28077. #endif
  28078. if (ret != 0)
  28079. return WC_TEST_RET_ENC_EC(ret);
  28080. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  28081. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  28082. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  28083. /* make curve25519 keys */
  28084. ret = wc_curve25519_make_key(&rng, 32, &userA);
  28085. if (ret != 0)
  28086. return WC_TEST_RET_ENC_EC(ret);
  28087. ret = wc_curve25519_make_key(&rng, 32, &userB);
  28088. if (ret != 0)
  28089. return WC_TEST_RET_ENC_EC(ret);
  28090. #ifdef HAVE_CURVE25519_SHARED_SECRET
  28091. /* find shared secret key */
  28092. x = sizeof(sharedA);
  28093. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  28094. printf("wc_curve25519_shared_secret 1 failed\n");
  28095. return WC_TEST_RET_ENC_EC(ret);
  28096. }
  28097. y = sizeof(sharedB);
  28098. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  28099. printf("wc_curve25519_shared_secret 2 failed\n");
  28100. return WC_TEST_RET_ENC_EC(ret);
  28101. }
  28102. /* compare shared secret keys to test they are the same */
  28103. if (y != x)
  28104. return WC_TEST_RET_ENC_NC;
  28105. if (XMEMCMP(sharedA, sharedB, x))
  28106. return WC_TEST_RET_ENC_NC;
  28107. #endif
  28108. #ifdef HAVE_CURVE25519_KEY_EXPORT
  28109. /* export a public key and import it for another user */
  28110. x = sizeof(exportBuf);
  28111. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  28112. if (ret != 0)
  28113. return WC_TEST_RET_ENC_EC(ret);
  28114. #ifdef HAVE_CURVE25519_KEY_IMPORT
  28115. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  28116. if (ret != 0)
  28117. return WC_TEST_RET_ENC_EC(ret);
  28118. #endif
  28119. #endif
  28120. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  28121. defined(HAVE_CURVE25519_KEY_IMPORT)
  28122. /* test shared key after importing a public key */
  28123. XMEMSET(sharedB, 0, sizeof(sharedB));
  28124. y = sizeof(sharedB);
  28125. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  28126. return WC_TEST_RET_ENC_NC;
  28127. }
  28128. if (XMEMCMP(sharedA, sharedB, y))
  28129. return WC_TEST_RET_ENC_NC;
  28130. /* import RFC test vectors and compare shared key */
  28131. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  28132. &userA);
  28133. if (ret != 0)
  28134. return WC_TEST_RET_ENC_EC(ret);
  28135. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  28136. &userB);
  28137. if (ret != 0)
  28138. return WC_TEST_RET_ENC_EC(ret);
  28139. /* test against known test vector */
  28140. XMEMSET(sharedB, 0, sizeof(sharedB));
  28141. y = sizeof(sharedB);
  28142. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  28143. if (ret != 0)
  28144. return WC_TEST_RET_ENC_EC(ret);
  28145. if (XMEMCMP(ss, sharedB, y))
  28146. return WC_TEST_RET_ENC_NC;
  28147. /* test swapping roles of keys and generating same shared key */
  28148. XMEMSET(sharedB, 0, sizeof(sharedB));
  28149. y = sizeof(sharedB);
  28150. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  28151. if (ret != 0)
  28152. return WC_TEST_RET_ENC_EC(ret);
  28153. if (XMEMCMP(ss, sharedB, y))
  28154. return WC_TEST_RET_ENC_NC;
  28155. /* test with 1 generated key and 1 from known test vector */
  28156. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  28157. &userA);
  28158. if (ret != 0)
  28159. return WC_TEST_RET_ENC_EC(ret);
  28160. wc_curve25519_free(&userB);
  28161. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  28162. ret = wc_curve25519_make_key(&rng, 32, &userB);
  28163. if (ret != 0)
  28164. return WC_TEST_RET_ENC_EC(ret);
  28165. x = sizeof(sharedA);
  28166. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  28167. if (ret != 0)
  28168. return WC_TEST_RET_ENC_EC(ret);
  28169. y = sizeof(sharedB);
  28170. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  28171. if (ret != 0)
  28172. return WC_TEST_RET_ENC_EC(ret);
  28173. /* compare shared secret keys to test they are the same */
  28174. if (y != x)
  28175. return WC_TEST_RET_ENC_NC;
  28176. if (XMEMCMP(sharedA, sharedB, x))
  28177. return WC_TEST_RET_ENC_NC;
  28178. ret = curve25519_overflow_test();
  28179. if (ret != 0)
  28180. return ret;
  28181. ret = curve25519_check_public_test();
  28182. if (ret != 0)
  28183. return ret;
  28184. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  28185. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  28186. defined(HAVE_CURVE25519_KEY_IMPORT)
  28187. ret = curve255519_der_test();
  28188. if (ret != 0)
  28189. return ret;
  28190. #endif
  28191. /* clean up keys when done */
  28192. wc_curve25519_free(&pubKey);
  28193. wc_curve25519_free(&userB);
  28194. wc_curve25519_free(&userA);
  28195. wc_FreeRng(&rng);
  28196. return 0;
  28197. }
  28198. #endif /* HAVE_CURVE25519 */
  28199. #ifdef HAVE_ED25519
  28200. #ifdef WOLFSSL_TEST_CERT
  28201. static wc_test_ret_t ed25519_test_cert(void)
  28202. {
  28203. DecodedCert cert[2];
  28204. DecodedCert* serverCert = NULL;
  28205. DecodedCert* caCert = NULL;
  28206. #ifdef HAVE_ED25519_VERIFY
  28207. ed25519_key key;
  28208. ed25519_key* pubKey = NULL;
  28209. int verify;
  28210. #endif /* HAVE_ED25519_VERIFY */
  28211. wc_test_ret_t ret;
  28212. byte* tmp;
  28213. size_t bytes;
  28214. XFILE file;
  28215. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28216. if (tmp == NULL) {
  28217. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28218. }
  28219. #ifdef USE_CERT_BUFFERS_256
  28220. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  28221. bytes = sizeof_ca_ed25519_cert;
  28222. #elif !defined(NO_FILESYSTEM)
  28223. file = XFOPEN(caEd25519Cert, "rb");
  28224. if (file == NULL) {
  28225. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28226. }
  28227. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  28228. XFCLOSE(file);
  28229. if (bytes == 0)
  28230. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28231. #else
  28232. /* No certificate to use. */
  28233. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28234. #endif
  28235. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  28236. caCert = &cert[0];
  28237. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  28238. if (ret != 0)
  28239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28240. #ifdef USE_CERT_BUFFERS_256
  28241. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  28242. bytes = sizeof_server_ed25519_cert;
  28243. #elif !defined(NO_FILESYSTEM)
  28244. file = XFOPEN(serverEd25519Cert, "rb");
  28245. if (file == NULL) {
  28246. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28247. }
  28248. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  28249. XFCLOSE(file);
  28250. if (bytes == 0)
  28251. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28252. #else
  28253. /* No certificate to use. */
  28254. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28255. #endif
  28256. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  28257. serverCert = &cert[1];
  28258. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  28259. if (ret != 0)
  28260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28261. #ifdef HAVE_ED25519_VERIFY
  28262. ret = wc_ed25519_init(&key);
  28263. if (ret < 0)
  28264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28265. pubKey = &key;
  28266. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  28267. pubKey);
  28268. if (ret < 0)
  28269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28270. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  28271. serverCert->source + serverCert->certBegin,
  28272. serverCert->sigIndex - serverCert->certBegin,
  28273. &verify, pubKey);
  28274. if (ret < 0 || verify != 1)
  28275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28276. #endif /* HAVE_ED25519_VERIFY */
  28277. done:
  28278. if (tmp != NULL)
  28279. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28280. #ifdef HAVE_ED25519_VERIFY
  28281. wc_ed25519_free(pubKey);
  28282. #endif /* HAVE_ED25519_VERIFY */
  28283. if (caCert != NULL)
  28284. FreeDecodedCert(caCert);
  28285. if (serverCert != NULL)
  28286. FreeDecodedCert(serverCert);
  28287. return ret;
  28288. }
  28289. static wc_test_ret_t ed25519_test_make_cert(void)
  28290. {
  28291. WC_RNG rng;
  28292. Cert cert;
  28293. DecodedCert decode;
  28294. ed25519_key key;
  28295. ed25519_key* privKey = NULL;
  28296. wc_test_ret_t ret = 0;
  28297. byte* tmp = NULL;
  28298. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  28299. #ifndef HAVE_FIPS
  28300. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28301. #else
  28302. ret = wc_InitRng(&rng);
  28303. #endif
  28304. if (ret != 0)
  28305. return WC_TEST_RET_ENC_EC(ret);
  28306. wc_ed25519_init(&key);
  28307. privKey = &key;
  28308. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  28309. cert.daysValid = 365 * 2;
  28310. cert.selfSigned = 1;
  28311. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  28312. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  28313. cert.isCA = 0;
  28314. #ifdef WOLFSSL_CERT_EXT
  28315. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  28316. if (ret < 0)
  28317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28318. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  28319. if (ret < 0)
  28320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28321. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  28322. if (ret < 0)
  28323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28324. #endif
  28325. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28326. if (tmp == NULL) {
  28327. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28328. }
  28329. cert.sigType = CTC_ED25519;
  28330. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  28331. if (ret < 0)
  28332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28333. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  28334. ED25519_TYPE, privKey, &rng);
  28335. if (ret < 0)
  28336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28337. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  28338. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  28339. FreeDecodedCert(&decode);
  28340. if (ret != 0)
  28341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28342. done:
  28343. if (tmp != NULL)
  28344. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28345. wc_ed25519_free(privKey);
  28346. wc_FreeRng(&rng);
  28347. return ret;
  28348. }
  28349. #endif /* WOLFSSL_TEST_CERT */
  28350. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  28351. defined(HAVE_ED25519_KEY_IMPORT)
  28352. static wc_test_ret_t ed25519ctx_test(void)
  28353. {
  28354. wc_test_ret_t ret;
  28355. byte out[ED25519_SIG_SIZE];
  28356. word32 outlen;
  28357. #ifdef HAVE_ED25519_VERIFY
  28358. int verify = 0;
  28359. #endif /* HAVE_ED25519_VERIFY */
  28360. ed25519_key key;
  28361. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  28362. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  28363. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  28364. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  28365. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  28366. };
  28367. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  28368. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  28369. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  28370. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  28371. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  28372. };
  28373. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  28374. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  28375. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  28376. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  28377. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  28378. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  28379. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  28380. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  28381. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  28382. };
  28383. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  28384. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  28385. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  28386. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  28387. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  28388. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  28389. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  28390. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  28391. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  28392. };
  28393. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  28394. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  28395. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  28396. };
  28397. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  28398. 0x66,0x6f,0x6f
  28399. };
  28400. outlen = sizeof(out);
  28401. XMEMSET(out, 0, sizeof(out));
  28402. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  28403. if (ret != 0)
  28404. return 10800;
  28405. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  28406. sizeof(pKeyCtx), &key);
  28407. if (ret == 0)
  28408. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  28409. contextCtx, sizeof(contextCtx));
  28410. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  28411. ret = WC_TEST_RET_ENC_NC;
  28412. #if defined(HAVE_ED25519_VERIFY)
  28413. /* test verify on good msg */
  28414. if (ret == 0)
  28415. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  28416. &verify, &key, contextCtx, sizeof(contextCtx));
  28417. if (ret == 0 && verify != 1)
  28418. ret = WC_TEST_RET_ENC_NC;
  28419. #endif
  28420. if (ret == 0)
  28421. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  28422. NULL, 0);
  28423. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  28424. ret = WC_TEST_RET_ENC_NC;
  28425. #if defined(HAVE_ED25519_VERIFY)
  28426. /* test verify on good msg */
  28427. if (ret == 0)
  28428. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  28429. &verify, &key, NULL, 0);
  28430. if (ret == 0 && verify != 1)
  28431. ret = WC_TEST_RET_ENC_NC;
  28432. #endif
  28433. wc_ed25519_free(&key);
  28434. return ret;
  28435. }
  28436. static wc_test_ret_t ed25519ph_test(void)
  28437. {
  28438. wc_test_ret_t ret = 0;
  28439. byte out[ED25519_SIG_SIZE];
  28440. word32 outlen;
  28441. #ifdef HAVE_ED25519_VERIFY
  28442. int verify = 0;
  28443. #endif /* HAVE_ED25519_VERIFY */
  28444. ed25519_key key;
  28445. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  28446. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  28447. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  28448. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  28449. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  28450. };
  28451. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  28452. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  28453. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  28454. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  28455. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  28456. };
  28457. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  28458. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  28459. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  28460. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  28461. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  28462. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  28463. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  28464. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  28465. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  28466. };
  28467. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  28468. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  28469. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  28470. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  28471. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  28472. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  28473. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  28474. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  28475. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  28476. };
  28477. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  28478. 0x61,0x62,0x63
  28479. };
  28480. /* SHA-512 hash of msgPh */
  28481. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  28482. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  28483. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  28484. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  28485. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  28486. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  28487. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  28488. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  28489. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  28490. };
  28491. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  28492. 0x66,0x6f,0x6f
  28493. };
  28494. outlen = sizeof(out);
  28495. XMEMSET(out, 0, sizeof(out));
  28496. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  28497. if (ret != 0)
  28498. return WC_TEST_RET_ENC_EC(ret);
  28499. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  28500. sizeof(pKeyPh), &key);
  28501. if (ret == 0)
  28502. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  28503. NULL, 0);
  28504. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  28505. ret = WC_TEST_RET_ENC_NC;
  28506. #if defined(HAVE_ED25519_VERIFY)
  28507. /* test verify on good msg */
  28508. if (ret == 0)
  28509. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  28510. &verify, &key, NULL, 0);
  28511. if (ret == 0 && verify != 1)
  28512. ret = WC_TEST_RET_ENC_NC;
  28513. #endif
  28514. if (ret == 0)
  28515. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  28516. contextPh2, sizeof(contextPh2));
  28517. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  28518. ret = WC_TEST_RET_ENC_NC;
  28519. #if defined(HAVE_ED25519_VERIFY)
  28520. /* test verify on good msg */
  28521. if (ret == 0)
  28522. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  28523. &key, contextPh2, sizeof(contextPh2));
  28524. if (ret == 0 && verify != 1)
  28525. ret = WC_TEST_RET_ENC_NC;
  28526. #endif
  28527. if (ret == 0)
  28528. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  28529. NULL, 0);
  28530. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  28531. ret = WC_TEST_RET_ENC_NC;
  28532. #if defined(HAVE_ED25519_VERIFY)
  28533. if (ret == 0)
  28534. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  28535. &verify, &key, NULL, 0);
  28536. if (ret == 0 && verify != 1)
  28537. ret = WC_TEST_RET_ENC_NC;
  28538. #endif
  28539. if (ret == 0)
  28540. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  28541. contextPh2, sizeof(contextPh2));
  28542. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  28543. ret = WC_TEST_RET_ENC_NC;
  28544. #if defined(HAVE_ED25519_VERIFY)
  28545. if (ret == 0)
  28546. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  28547. &key, contextPh2, sizeof(contextPh2));
  28548. if (ret == 0 && verify != 1)
  28549. ret = WC_TEST_RET_ENC_NC;
  28550. #endif
  28551. wc_ed25519_free(&key);
  28552. return ret;
  28553. }
  28554. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  28555. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  28556. {
  28557. wc_test_ret_t ret;
  28558. WC_RNG rng;
  28559. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  28560. defined(HAVE_ED25519_KEY_IMPORT)
  28561. byte out[ED25519_SIG_SIZE];
  28562. byte exportPKey[ED25519_KEY_SIZE];
  28563. byte exportSKey[ED25519_KEY_SIZE];
  28564. word32 exportPSz;
  28565. word32 exportSSz;
  28566. int i;
  28567. word32 outlen;
  28568. #ifdef HAVE_ED25519_VERIFY
  28569. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  28570. int j;
  28571. #endif
  28572. int verify;
  28573. #endif /* HAVE_ED25519_VERIFY */
  28574. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  28575. word32 keySz, sigSz;
  28576. ed25519_key key;
  28577. ed25519_key key2;
  28578. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  28579. defined(HAVE_ED25519_KEY_IMPORT)
  28580. /* test vectors from
  28581. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  28582. */
  28583. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  28584. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28585. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28586. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28587. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  28588. };
  28589. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  28590. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  28591. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  28592. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  28593. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  28594. };
  28595. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  28596. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  28597. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  28598. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  28599. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  28600. };
  28601. /* uncompressed test */
  28602. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  28603. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28604. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28605. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28606. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  28607. };
  28608. /* compressed prefix test */
  28609. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  28610. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28611. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28612. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28613. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  28614. };
  28615. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  28616. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  28617. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  28618. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  28619. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  28620. };
  28621. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  28622. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  28623. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  28624. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  28625. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  28626. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  28627. };
  28628. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  28629. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  28630. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  28631. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  28632. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  28633. };
  28634. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  28635. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  28636. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  28637. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  28638. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  28639. };
  28640. /* uncompressed test */
  28641. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  28642. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  28643. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  28644. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  28645. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  28646. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  28647. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  28648. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  28649. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  28650. 0xd7
  28651. };
  28652. /* compressed prefix */
  28653. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  28654. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  28655. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  28656. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  28657. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  28658. };
  28659. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  28660. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  28661. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  28662. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  28663. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  28664. };
  28665. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  28666. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  28667. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  28668. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  28669. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  28670. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  28671. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  28672. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  28673. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  28674. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  28675. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  28676. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  28677. };
  28678. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  28679. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  28680. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  28681. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  28682. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  28683. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  28684. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  28685. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  28686. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  28687. };
  28688. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  28689. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  28690. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  28691. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  28692. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  28693. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  28694. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  28695. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  28696. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  28697. };
  28698. /* uncompressed test */
  28699. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  28700. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  28701. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  28702. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  28703. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  28704. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  28705. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  28706. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  28707. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  28708. };
  28709. /* compressed prefix */
  28710. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  28711. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  28712. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  28713. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  28714. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  28715. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  28716. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  28717. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  28718. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  28719. };
  28720. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  28721. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  28722. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  28723. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  28724. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  28725. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  28726. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  28727. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  28728. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  28729. };
  28730. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  28731. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  28732. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  28733. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  28734. /* test of a 1024 byte long message */
  28735. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  28736. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  28737. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  28738. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  28739. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  28740. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  28741. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  28742. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  28743. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  28744. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  28745. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  28746. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  28747. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  28748. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  28749. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  28750. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  28751. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  28752. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  28753. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  28754. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  28755. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  28756. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  28757. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  28758. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  28759. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  28760. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  28761. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  28762. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  28763. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  28764. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  28765. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  28766. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  28767. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  28768. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  28769. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  28770. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  28771. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  28772. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  28773. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  28774. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  28775. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  28776. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  28777. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  28778. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  28779. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  28780. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  28781. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  28782. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  28783. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  28784. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  28785. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  28786. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  28787. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  28788. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  28789. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  28790. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  28791. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  28792. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  28793. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  28794. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  28795. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  28796. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  28797. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  28798. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  28799. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  28800. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  28801. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  28802. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  28803. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  28804. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  28805. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  28806. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  28807. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  28808. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  28809. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  28810. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  28811. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  28812. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  28813. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  28814. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  28815. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  28816. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  28817. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  28818. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  28819. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  28820. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  28821. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  28822. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  28823. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  28824. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  28825. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  28826. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  28827. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  28828. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  28829. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  28830. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  28831. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  28832. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  28833. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  28834. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  28835. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  28836. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  28837. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  28838. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  28839. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  28840. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  28841. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  28842. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  28843. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  28844. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  28845. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  28846. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  28847. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  28848. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  28849. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  28850. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  28851. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  28852. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  28853. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  28854. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  28855. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  28856. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  28857. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  28858. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  28859. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  28860. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  28861. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  28862. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  28863. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  28864. };
  28865. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  28866. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  28867. sizeof(msg2),
  28868. sizeof(msg3),
  28869. 0 /*sizeof(msg1)*/,
  28870. 0 /*sizeof(msg1)*/,
  28871. sizeof(msg4)
  28872. };
  28873. #ifndef NO_ASN
  28874. static byte privateEd25519[] = {
  28875. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  28876. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  28877. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28878. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28879. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28880. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  28881. };
  28882. static byte badPrivateEd25519[] = {
  28883. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  28884. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  28885. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28886. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28887. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28888. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  28889. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  28890. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  28891. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  28892. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  28893. 0xf7,0x07,0x51,0x1a,
  28894. 0x00 /* add additional bytes to make the pubkey bigger */
  28895. };
  28896. static byte publicEd25519[] = {
  28897. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  28898. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  28899. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  28900. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  28901. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  28902. 0xf7,0x07,0x51,0x1a
  28903. };
  28904. /* size has been altered to catch if sanity check is done */
  28905. static byte badPublicEd25519[] = {
  28906. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  28907. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  28908. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  28909. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  28910. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  28911. 0xf7,0x07,0x51,0x1a,
  28912. 0x00 /* add an additional byte to make the pubkey appear bigger */
  28913. };
  28914. static byte privPubEd25519[] = {
  28915. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  28916. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  28917. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  28918. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  28919. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  28920. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  28921. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  28922. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  28923. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  28924. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  28925. 0x51,0x1a
  28926. };
  28927. word32 idx;
  28928. #endif /* NO_ASN */
  28929. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  28930. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  28931. ed25519_key key3;
  28932. #endif
  28933. /* create ed25519 keys */
  28934. #ifndef HAVE_FIPS
  28935. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28936. #else
  28937. ret = wc_InitRng(&rng);
  28938. #endif
  28939. if (ret != 0)
  28940. return WC_TEST_RET_ENC_EC(ret);
  28941. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  28942. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  28943. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  28944. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  28945. #endif
  28946. #ifdef HAVE_ED25519_MAKE_KEY
  28947. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  28948. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  28949. #endif
  28950. /* helper functions for signature and key size */
  28951. keySz = wc_ed25519_size(&key);
  28952. sigSz = wc_ed25519_sig_size(&key);
  28953. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  28954. defined(HAVE_ED25519_KEY_IMPORT)
  28955. for (i = 0; i < 6; i++) {
  28956. outlen = sizeof(out);
  28957. XMEMSET(out, 0, sizeof(out));
  28958. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  28959. pKeySz[i], &key) != 0)
  28960. return WC_TEST_RET_ENC_I(i);
  28961. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  28962. return WC_TEST_RET_ENC_I(i);
  28963. if (XMEMCMP(out, sigs[i], 64))
  28964. return WC_TEST_RET_ENC_I(i);
  28965. #if defined(HAVE_ED25519_VERIFY)
  28966. /* test verify on good msg */
  28967. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  28968. &key) != 0 || verify != 1)
  28969. return WC_TEST_RET_ENC_I(i);
  28970. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  28971. /* test verify on good msg using streaming interface directly */
  28972. if (wc_ed25519_verify_msg_init(out, outlen,
  28973. &key, (byte)Ed25519, NULL, 0) != 0)
  28974. return WC_TEST_RET_ENC_I(i);
  28975. for (j = 0; j < msgSz[i]; j += i) {
  28976. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  28977. return WC_TEST_RET_ENC_I(i);
  28978. }
  28979. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  28980. &key) != 0 || verify != 1)
  28981. return WC_TEST_RET_ENC_I(i);
  28982. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  28983. /* test verify on bad msg */
  28984. out[outlen-1] = out[outlen-1] + 1;
  28985. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  28986. &key) == 0 || verify == 1)
  28987. return WC_TEST_RET_ENC_I(i);
  28988. #endif /* HAVE_ED25519_VERIFY */
  28989. /* test api for import/exporting keys */
  28990. exportPSz = sizeof(exportPKey);
  28991. exportSSz = sizeof(exportSKey);
  28992. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  28993. return WC_TEST_RET_ENC_I(i);
  28994. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  28995. return WC_TEST_RET_ENC_I(i);
  28996. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  28997. return WC_TEST_RET_ENC_I(i);
  28998. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  28999. exportPKey, exportPSz, &key2) != 0)
  29000. return WC_TEST_RET_ENC_I(i);
  29001. /* clear "out" buffer and test sign with imported keys */
  29002. outlen = sizeof(out);
  29003. XMEMSET(out, 0, sizeof(out));
  29004. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  29005. return WC_TEST_RET_ENC_I(i);
  29006. #if defined(HAVE_ED25519_VERIFY)
  29007. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  29008. &key2) != 0 || verify != 1)
  29009. return WC_TEST_RET_ENC_I(i);
  29010. if (XMEMCMP(out, sigs[i], 64))
  29011. return WC_TEST_RET_ENC_I(i);
  29012. #endif /* HAVE_ED25519_VERIFY */
  29013. }
  29014. ret = ed25519ctx_test();
  29015. if (ret != 0)
  29016. return ret;
  29017. ret = ed25519ph_test();
  29018. if (ret != 0)
  29019. return ret;
  29020. #ifndef NO_ASN
  29021. /* Try ASN.1 encoded private-only key and public key. */
  29022. idx = 0;
  29023. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  29024. sizeof(privateEd25519));
  29025. if (ret != 0)
  29026. return WC_TEST_RET_ENC_EC(ret);
  29027. idx = 0;
  29028. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  29029. sizeof(badPrivateEd25519)) == 0)
  29030. return WC_TEST_RET_ENC_NC;
  29031. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29032. if (ret != BAD_FUNC_ARG)
  29033. return WC_TEST_RET_ENC_EC(ret);
  29034. /* try with a buffer size that is too large */
  29035. idx = 0;
  29036. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  29037. sizeof(badPublicEd25519)) == 0)
  29038. return WC_TEST_RET_ENC_NC;
  29039. idx = 0;
  29040. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  29041. sizeof(publicEd25519));
  29042. if (ret != 0)
  29043. return WC_TEST_RET_ENC_EC(ret);
  29044. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29045. if (ret != 0)
  29046. return WC_TEST_RET_ENC_EC(ret);
  29047. if (XMEMCMP(out, sigs[0], 64))
  29048. return WC_TEST_RET_ENC_NC;
  29049. #if defined(HAVE_ED25519_VERIFY)
  29050. /* test verify on good msg */
  29051. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  29052. if (ret != 0 || verify != 1)
  29053. return WC_TEST_RET_ENC_EC(ret);
  29054. #endif /* HAVE_ED25519_VERIFY */
  29055. wc_ed25519_free(&key3);
  29056. wc_ed25519_init(&key3);
  29057. idx = 0;
  29058. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  29059. sizeof(privPubEd25519));
  29060. if (ret != 0)
  29061. return WC_TEST_RET_ENC_EC(ret);
  29062. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29063. if (ret != 0)
  29064. return WC_TEST_RET_ENC_EC(ret);
  29065. if (XMEMCMP(out, sigs[0], 64))
  29066. return WC_TEST_RET_ENC_NC;
  29067. wc_ed25519_free(&key3);
  29068. #endif /* NO_ASN */
  29069. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  29070. /* clean up keys when done */
  29071. wc_ed25519_free(&key);
  29072. wc_ed25519_free(&key2);
  29073. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  29074. wc_FreeRng(&rng);
  29075. #endif
  29076. /* hush warnings of unused keySz and sigSz */
  29077. (void)keySz;
  29078. (void)sigSz;
  29079. #ifdef WOLFSSL_TEST_CERT
  29080. ret = ed25519_test_cert();
  29081. if (ret < 0)
  29082. return ret;
  29083. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  29084. ret = ed25519_test_make_cert();
  29085. if (ret < 0)
  29086. return ret;
  29087. #endif /* WOLFSSL_CERT_GEN */
  29088. #endif /* WOLFSSL_TEST_CERT */
  29089. return 0;
  29090. }
  29091. #endif /* HAVE_ED25519 */
  29092. #ifdef HAVE_CURVE448
  29093. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29094. defined(HAVE_CURVE448_KEY_IMPORT)
  29095. /* Test the wc_curve448_check_public API.
  29096. *
  29097. * returns 0 on success and -ve on failure.
  29098. */
  29099. static wc_test_ret_t curve448_check_public_test(void)
  29100. {
  29101. /* Little-endian values that will fail */
  29102. byte fail_le[][CURVE448_KEY_SIZE] = {
  29103. {
  29104. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29107. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29108. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29109. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29110. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29111. },
  29112. {
  29113. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29116. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29117. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29120. },
  29121. };
  29122. /* Big-endian values that will fail */
  29123. byte fail_be[][CURVE448_KEY_SIZE] = {
  29124. {
  29125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29126. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29127. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29130. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29132. },
  29133. {
  29134. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29136. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29137. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  29141. },
  29142. };
  29143. /* Good or valid public value */
  29144. byte good[CURVE448_KEY_SIZE] = {
  29145. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29146. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29147. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29148. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29149. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29150. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29151. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  29152. };
  29153. int i;
  29154. wc_test_ret_t ret;
  29155. /* Parameter checks */
  29156. /* NULL pointer */
  29157. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  29158. if (ret != BAD_FUNC_ARG)
  29159. return WC_TEST_RET_ENC_EC(ret);
  29160. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  29161. if (ret != BAD_FUNC_ARG)
  29162. return WC_TEST_RET_ENC_EC(ret);
  29163. /* Length of 0 treated differently to other invalid lengths for TLS */
  29164. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  29165. if (ret != BUFFER_E)
  29166. return WC_TEST_RET_ENC_EC(ret);
  29167. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  29168. if (ret != BUFFER_E)
  29169. return WC_TEST_RET_ENC_EC(ret);
  29170. /* Length not CURVE448_KEY_SIZE */
  29171. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  29172. if (i == CURVE448_KEY_SIZE)
  29173. continue;
  29174. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  29175. ECC_BAD_ARG_E) {
  29176. return WC_TEST_RET_ENC_I(i);
  29177. }
  29178. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  29179. ECC_BAD_ARG_E) {
  29180. return WC_TEST_RET_ENC_I(i);
  29181. }
  29182. }
  29183. /* Little-endian fail cases */
  29184. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  29185. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  29186. EC448_LITTLE_ENDIAN) == 0) {
  29187. return WC_TEST_RET_ENC_I(i);
  29188. }
  29189. }
  29190. /* Big-endian fail cases */
  29191. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  29192. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  29193. EC448_BIG_ENDIAN) == 0) {
  29194. return WC_TEST_RET_ENC_I(i);
  29195. }
  29196. }
  29197. /* Check a valid public value works! */
  29198. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  29199. EC448_LITTLE_ENDIAN);
  29200. if (ret != 0)
  29201. return WC_TEST_RET_ENC_EC(ret);
  29202. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  29203. if (ret != 0)
  29204. return WC_TEST_RET_ENC_EC(ret);
  29205. return 0;
  29206. }
  29207. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  29208. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  29209. {
  29210. WC_RNG rng;
  29211. wc_test_ret_t ret;
  29212. #ifdef HAVE_CURVE448_SHARED_SECRET
  29213. byte sharedA[CURVE448_KEY_SIZE];
  29214. byte sharedB[CURVE448_KEY_SIZE];
  29215. word32 y;
  29216. #endif
  29217. #ifdef HAVE_CURVE448_KEY_EXPORT
  29218. byte exportBuf[CURVE448_KEY_SIZE];
  29219. #endif
  29220. word32 x;
  29221. curve448_key userA, userB, pubKey;
  29222. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29223. defined(HAVE_CURVE448_KEY_IMPORT)
  29224. /* test vectors from
  29225. https://www.rfc-editor.org/rfc/rfc7748.html
  29226. */
  29227. /* secret key for party a */
  29228. byte sa[] = {
  29229. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  29230. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  29231. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  29232. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  29233. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  29234. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  29235. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  29236. };
  29237. /* public key for party a */
  29238. byte pa[] = {
  29239. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  29240. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  29241. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  29242. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  29243. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  29244. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  29245. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  29246. };
  29247. /* secret key for party b */
  29248. byte sb[] = {
  29249. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  29250. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  29251. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  29252. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  29253. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  29254. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  29255. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  29256. };
  29257. /* public key for party b */
  29258. byte pb[] = {
  29259. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  29260. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  29261. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  29262. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  29263. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  29264. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  29265. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  29266. };
  29267. /* expected shared key */
  29268. byte ss[] = {
  29269. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  29270. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  29271. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  29272. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  29273. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  29274. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  29275. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  29276. };
  29277. #endif /* HAVE_CURVE448_SHARED_SECRET */
  29278. (void)x;
  29279. #ifndef HAVE_FIPS
  29280. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29281. #else
  29282. ret = wc_InitRng(&rng);
  29283. #endif
  29284. if (ret != 0)
  29285. return WC_TEST_RET_ENC_EC(ret);
  29286. wc_curve448_init(&userA);
  29287. wc_curve448_init(&userB);
  29288. wc_curve448_init(&pubKey);
  29289. /* make curve448 keys */
  29290. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  29291. if (ret != 0)
  29292. return WC_TEST_RET_ENC_EC(ret);
  29293. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  29294. if (ret != 0)
  29295. return WC_TEST_RET_ENC_EC(ret);
  29296. #ifdef HAVE_CURVE448_SHARED_SECRET
  29297. /* find shared secret key */
  29298. x = sizeof(sharedA);
  29299. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  29300. if (ret != 0)
  29301. return WC_TEST_RET_ENC_EC(ret);
  29302. y = sizeof(sharedB);
  29303. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  29304. if (ret != 0)
  29305. return WC_TEST_RET_ENC_EC(ret);
  29306. /* compare shared secret keys to test they are the same */
  29307. if (y != x)
  29308. return WC_TEST_RET_ENC_NC;
  29309. if (XMEMCMP(sharedA, sharedB, x))
  29310. return WC_TEST_RET_ENC_NC;
  29311. #endif
  29312. #ifdef HAVE_CURVE448_KEY_EXPORT
  29313. /* export a public key and import it for another user */
  29314. x = sizeof(exportBuf);
  29315. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  29316. if (ret != 0)
  29317. return WC_TEST_RET_ENC_EC(ret);
  29318. #ifdef HAVE_CURVE448_KEY_IMPORT
  29319. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  29320. if (ret != 0)
  29321. return WC_TEST_RET_ENC_EC(ret);
  29322. #endif
  29323. #endif
  29324. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29325. defined(HAVE_CURVE448_KEY_IMPORT)
  29326. /* test shared key after importing a public key */
  29327. XMEMSET(sharedB, 0, sizeof(sharedB));
  29328. y = sizeof(sharedB);
  29329. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  29330. if (ret != 0)
  29331. return WC_TEST_RET_ENC_EC(ret);
  29332. if (XMEMCMP(sharedA, sharedB, y))
  29333. return WC_TEST_RET_ENC_NC;
  29334. /* import RFC test vectors and compare shared key */
  29335. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  29336. if (ret != 0)
  29337. return WC_TEST_RET_ENC_EC(ret);
  29338. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  29339. if (ret != 0)
  29340. return WC_TEST_RET_ENC_EC(ret);
  29341. /* test against known test vector */
  29342. XMEMSET(sharedB, 0, sizeof(sharedB));
  29343. y = sizeof(sharedB);
  29344. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  29345. if (ret != 0)
  29346. return WC_TEST_RET_ENC_EC(ret);
  29347. if (XMEMCMP(ss, sharedB, y))
  29348. return WC_TEST_RET_ENC_NC;
  29349. /* test swapping roles of keys and generating same shared key */
  29350. XMEMSET(sharedB, 0, sizeof(sharedB));
  29351. y = sizeof(sharedB);
  29352. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  29353. if (ret != 0)
  29354. return WC_TEST_RET_ENC_EC(ret);
  29355. if (XMEMCMP(ss, sharedB, y))
  29356. return WC_TEST_RET_ENC_NC;
  29357. /* test with 1 generated key and 1 from known test vector */
  29358. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  29359. if (ret != 0)
  29360. return WC_TEST_RET_ENC_EC(ret);
  29361. ret = wc_curve448_make_key(&rng, 56, &userB);
  29362. if (ret != 0)
  29363. return WC_TEST_RET_ENC_EC(ret);
  29364. x = sizeof(sharedA);
  29365. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  29366. if (ret != 0)
  29367. return WC_TEST_RET_ENC_EC(ret);
  29368. y = sizeof(sharedB);
  29369. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  29370. if (ret != 0)
  29371. return WC_TEST_RET_ENC_EC(ret);
  29372. /* compare shared secret keys to test they are the same */
  29373. if (y != x)
  29374. return WC_TEST_RET_ENC_NC;
  29375. if (XMEMCMP(sharedA, sharedB, x))
  29376. return WC_TEST_RET_ENC_NC;
  29377. ret = curve448_check_public_test();
  29378. if (ret != 0)
  29379. return ret;
  29380. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  29381. /* clean up keys when done */
  29382. wc_curve448_free(&pubKey);
  29383. wc_curve448_free(&userB);
  29384. wc_curve448_free(&userA);
  29385. wc_FreeRng(&rng);
  29386. return 0;
  29387. }
  29388. #endif /* HAVE_CURVE448 */
  29389. #ifdef HAVE_ED448
  29390. #ifdef WOLFSSL_TEST_CERT
  29391. static wc_test_ret_t ed448_test_cert(void)
  29392. {
  29393. DecodedCert cert[2];
  29394. DecodedCert* serverCert = NULL;
  29395. DecodedCert* caCert = NULL;
  29396. #ifdef HAVE_ED448_VERIFY
  29397. ed448_key key;
  29398. ed448_key* pubKey = NULL;
  29399. int verify;
  29400. #endif /* HAVE_ED448_VERIFY */
  29401. wc_test_ret_t ret;
  29402. byte* tmp;
  29403. size_t bytes;
  29404. XFILE file;
  29405. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29406. if (tmp == NULL) {
  29407. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29408. }
  29409. #ifdef USE_CERT_BUFFERS_256
  29410. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  29411. bytes = sizeof_ca_ed448_cert;
  29412. #elif !defined(NO_FILESYSTEM)
  29413. file = XFOPEN(caEd448Cert, "rb");
  29414. if (file == NULL) {
  29415. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29416. }
  29417. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  29418. XFCLOSE(file);
  29419. if (bytes == 0)
  29420. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29421. #else
  29422. /* No certificate to use. */
  29423. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  29424. #endif
  29425. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  29426. caCert = &cert[0];
  29427. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  29428. if (ret != 0)
  29429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29430. #ifdef USE_CERT_BUFFERS_256
  29431. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  29432. bytes = sizeof_server_ed448_cert;
  29433. #elif !defined(NO_FILESYSTEM)
  29434. file = XFOPEN(serverEd448Cert, "rb");
  29435. if (file == NULL) {
  29436. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29437. }
  29438. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  29439. XFCLOSE(file);
  29440. if (bytes == 0)
  29441. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29442. #else
  29443. /* No certificate to use. */
  29444. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  29445. #endif
  29446. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  29447. serverCert = &cert[1];
  29448. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  29449. if (ret != 0)
  29450. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29451. #ifdef HAVE_ED448_VERIFY
  29452. ret = wc_ed448_init(&key);
  29453. if (ret < 0)
  29454. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29455. pubKey = &key;
  29456. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  29457. if (ret < 0)
  29458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29459. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  29460. serverCert->source + serverCert->certBegin,
  29461. serverCert->sigIndex - serverCert->certBegin,
  29462. &verify, pubKey, NULL, 0);
  29463. if (ret < 0 || verify != 1) {
  29464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29465. }
  29466. #endif /* HAVE_ED448_VERIFY */
  29467. done:
  29468. if (tmp != NULL)
  29469. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29470. #ifdef HAVE_ED448_VERIFY
  29471. wc_ed448_free(pubKey);
  29472. #endif /* HAVE_ED448_VERIFY */
  29473. if (caCert != NULL)
  29474. FreeDecodedCert(caCert);
  29475. if (serverCert != NULL)
  29476. FreeDecodedCert(serverCert);
  29477. return ret;
  29478. }
  29479. static wc_test_ret_t ed448_test_make_cert(void)
  29480. {
  29481. WC_RNG rng;
  29482. Cert cert;
  29483. DecodedCert decode;
  29484. ed448_key key;
  29485. ed448_key* privKey = NULL;
  29486. wc_test_ret_t ret = 0;
  29487. byte* tmp = NULL;
  29488. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  29489. #ifndef HAVE_FIPS
  29490. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29491. #else
  29492. ret = wc_InitRng(&rng);
  29493. #endif
  29494. if (ret != 0)
  29495. return WC_TEST_RET_ENC_EC(ret);
  29496. wc_ed448_init(&key);
  29497. privKey = &key;
  29498. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  29499. cert.daysValid = 365 * 2;
  29500. cert.selfSigned = 1;
  29501. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  29502. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  29503. cert.isCA = 0;
  29504. #ifdef WOLFSSL_CERT_EXT
  29505. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  29506. if (ret < 0)
  29507. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29508. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  29509. if (ret < 0)
  29510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29511. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  29512. if (ret < 0)
  29513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29514. #endif
  29515. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29516. if (tmp == NULL) {
  29517. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  29518. }
  29519. cert.sigType = CTC_ED448;
  29520. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  29521. if (ret < 0)
  29522. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29523. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  29524. privKey, &rng);
  29525. if (ret < 0)
  29526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29527. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  29528. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  29529. FreeDecodedCert(&decode);
  29530. if (ret != 0)
  29531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  29532. done:
  29533. if (tmp != NULL)
  29534. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29535. wc_ed448_free(privKey);
  29536. wc_FreeRng(&rng);
  29537. return ret;
  29538. }
  29539. #endif /* WOLFSSL_TEST_CERT */
  29540. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  29541. defined(HAVE_ED448_KEY_IMPORT)
  29542. static wc_test_ret_t ed448_ctx_test(void)
  29543. {
  29544. wc_test_ret_t ret;
  29545. byte out[ED448_SIG_SIZE];
  29546. word32 outlen;
  29547. #ifdef HAVE_ED448_VERIFY
  29548. int verify;
  29549. #endif /* HAVE_ED448_VERIFY */
  29550. ed448_key key;
  29551. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  29552. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  29553. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  29554. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  29555. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  29556. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  29557. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  29558. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  29559. 0x4e
  29560. };
  29561. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  29562. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  29563. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  29564. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  29565. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  29566. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  29567. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  29568. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  29569. 0x80
  29570. };
  29571. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  29572. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  29573. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  29574. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  29575. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  29576. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  29577. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  29578. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  29579. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  29580. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  29581. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  29582. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  29583. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  29584. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  29585. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  29586. 0x3c, 0x00
  29587. };
  29588. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  29589. 0x03
  29590. };
  29591. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  29592. 0x66,0x6f,0x6f
  29593. };
  29594. outlen = sizeof(out);
  29595. XMEMSET(out, 0, sizeof(out));
  29596. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  29597. if (ret != 0)
  29598. return WC_TEST_RET_ENC_EC(ret);
  29599. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  29600. sizeof(pKeyCtx), &key);
  29601. if (ret != 0)
  29602. return WC_TEST_RET_ENC_EC(ret);
  29603. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  29604. contextCtx, sizeof(contextCtx));
  29605. if (ret != 0)
  29606. return WC_TEST_RET_ENC_EC(ret);
  29607. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  29608. return WC_TEST_RET_ENC_NC;
  29609. #if defined(HAVE_ED448_VERIFY)
  29610. /* test verify on good msg */
  29611. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  29612. contextCtx, sizeof(contextCtx));
  29613. if (ret != 0 || verify != 1)
  29614. return WC_TEST_RET_ENC_EC(ret);
  29615. #endif
  29616. wc_ed448_free(&key);
  29617. return 0;
  29618. }
  29619. static wc_test_ret_t ed448ph_test(void)
  29620. {
  29621. wc_test_ret_t ret;
  29622. byte out[ED448_SIG_SIZE];
  29623. word32 outlen;
  29624. #ifdef HAVE_ED448_VERIFY
  29625. int verify;
  29626. #endif /* HAVE_ED448_VERIFY */
  29627. ed448_key key;
  29628. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  29629. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  29630. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  29631. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  29632. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  29633. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  29634. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  29635. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  29636. 0x49
  29637. };
  29638. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  29639. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  29640. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  29641. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  29642. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  29643. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  29644. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  29645. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  29646. 0x80
  29647. };
  29648. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  29649. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  29650. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  29651. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  29652. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  29653. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  29654. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  29655. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  29656. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  29657. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  29658. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  29659. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  29660. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  29661. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  29662. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  29663. 0x0f, 0x00
  29664. };
  29665. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  29666. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  29667. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  29668. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  29669. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  29670. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  29671. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  29672. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  29673. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  29674. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  29675. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  29676. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  29677. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  29678. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  29679. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  29680. 0x21, 0x00
  29681. };
  29682. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  29683. 0x61,0x62,0x63
  29684. };
  29685. /* SHA-512 hash of msgPh */
  29686. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  29687. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  29688. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  29689. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  29690. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  29691. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  29692. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  29693. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  29694. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  29695. };
  29696. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  29697. 0x66,0x6f,0x6f
  29698. };
  29699. outlen = sizeof(out);
  29700. XMEMSET(out, 0, sizeof(out));
  29701. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  29702. if (ret != 0)
  29703. return WC_TEST_RET_ENC_EC(ret);
  29704. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  29705. sizeof(pKeyPh), &key);
  29706. if (ret != 0)
  29707. return WC_TEST_RET_ENC_EC(ret);
  29708. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  29709. 0);
  29710. if (ret != 0)
  29711. return WC_TEST_RET_ENC_EC(ret);
  29712. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  29713. return WC_TEST_RET_ENC_NC;
  29714. #if defined(HAVE_ED448_VERIFY)
  29715. /* test verify on good msg */
  29716. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  29717. NULL, 0);
  29718. if (ret != 0 || verify != 1) {
  29719. return WC_TEST_RET_ENC_EC(ret);
  29720. }
  29721. #endif
  29722. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  29723. contextPh2, sizeof(contextPh2));
  29724. if (ret != 0)
  29725. return WC_TEST_RET_ENC_EC(ret);
  29726. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  29727. return WC_TEST_RET_ENC_NC;
  29728. #if defined(HAVE_ED448_VERIFY)
  29729. /* test verify on good msg */
  29730. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  29731. contextPh2, sizeof(contextPh2));
  29732. if (ret != 0 || verify != 1) {
  29733. return WC_TEST_RET_ENC_EC(ret);
  29734. }
  29735. #endif
  29736. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  29737. 0);
  29738. if (ret != 0)
  29739. return WC_TEST_RET_ENC_EC(ret);
  29740. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  29741. return WC_TEST_RET_ENC_NC;
  29742. #if defined(HAVE_ED448_VERIFY)
  29743. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  29744. &key, NULL, 0);
  29745. if (ret != 0 || verify != 1) {
  29746. return WC_TEST_RET_ENC_EC(ret);
  29747. }
  29748. #endif
  29749. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  29750. contextPh2, sizeof(contextPh2));
  29751. if (ret != 0)
  29752. return WC_TEST_RET_ENC_EC(ret);
  29753. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  29754. return WC_TEST_RET_ENC_NC;
  29755. #if defined(HAVE_ED448_VERIFY)
  29756. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  29757. &key, contextPh2, sizeof(contextPh2));
  29758. if (ret != 0 || verify != 1) {
  29759. return WC_TEST_RET_ENC_EC(ret);
  29760. }
  29761. #endif
  29762. wc_ed448_free(&key);
  29763. return 0;
  29764. }
  29765. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  29766. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  29767. {
  29768. wc_test_ret_t ret;
  29769. WC_RNG rng;
  29770. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  29771. defined(HAVE_ED448_KEY_IMPORT)
  29772. byte out[ED448_SIG_SIZE];
  29773. int i;
  29774. word32 outlen;
  29775. #ifdef HAVE_ED448_VERIFY
  29776. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  29777. int j;
  29778. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  29779. int verify;
  29780. #endif /* HAVE_ED448_VERIFY */
  29781. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  29782. word32 keySz, sigSz;
  29783. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29784. ed448_key *key = NULL;
  29785. ed448_key *key2 = NULL;
  29786. #else
  29787. ed448_key key[1];
  29788. ed448_key key2[1];
  29789. #endif
  29790. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  29791. defined(HAVE_ED448_KEY_IMPORT)
  29792. /* test vectors from
  29793. https://tools.ietf.org/html/rfc8032
  29794. */
  29795. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  29796. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  29797. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  29798. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  29799. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  29800. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  29801. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  29802. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  29803. 0x5b
  29804. };
  29805. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  29806. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  29807. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  29808. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  29809. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  29810. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  29811. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  29812. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  29813. 0x4e
  29814. };
  29815. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  29816. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  29817. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  29818. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  29819. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  29820. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  29821. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  29822. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  29823. 0x1b
  29824. };
  29825. /* uncompressed test */
  29826. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  29827. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  29828. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  29829. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  29830. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  29831. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  29832. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  29833. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  29834. 0x5b
  29835. };
  29836. /* compressed prefix test */
  29837. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  29838. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  29839. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  29840. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  29841. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  29842. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  29843. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  29844. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  29845. 0x5b
  29846. };
  29847. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  29848. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  29849. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  29850. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  29851. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  29852. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  29853. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  29854. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  29855. 0xe8
  29856. };
  29857. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  29858. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  29859. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  29860. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  29861. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  29862. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  29863. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  29864. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  29865. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  29866. 0x80
  29867. };
  29868. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  29869. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  29870. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  29871. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  29872. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  29873. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  29874. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  29875. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  29876. 0x80
  29877. };
  29878. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  29879. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  29880. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  29881. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  29882. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  29883. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  29884. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  29885. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  29886. 0x80
  29887. };
  29888. /* uncompressed test */
  29889. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  29890. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  29891. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  29892. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  29893. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  29894. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  29895. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  29896. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  29897. 0x80
  29898. };
  29899. /* compressed prefix */
  29900. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  29901. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  29902. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  29903. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  29904. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  29905. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  29906. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  29907. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  29908. 0x80
  29909. };
  29910. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  29911. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  29912. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  29913. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  29914. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  29915. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  29916. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  29917. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  29918. 0x00
  29919. };
  29920. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  29921. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  29922. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  29923. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  29924. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  29925. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  29926. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  29927. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  29928. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  29929. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  29930. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  29931. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  29932. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  29933. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  29934. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  29935. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  29936. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  29937. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  29938. 0x26, 0x00
  29939. };
  29940. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  29941. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  29942. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  29943. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  29944. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  29945. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  29946. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  29947. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  29948. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  29949. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  29950. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  29951. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  29952. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  29953. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  29954. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  29955. 0x3a, 0x00
  29956. };
  29957. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  29958. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  29959. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  29960. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  29961. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  29962. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  29963. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  29964. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  29965. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  29966. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  29967. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  29968. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  29969. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  29970. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  29971. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  29972. 0x3c, 0x00
  29973. };
  29974. /* uncompressed test */
  29975. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  29976. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  29977. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  29978. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  29979. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  29980. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  29981. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  29982. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  29983. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  29984. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  29985. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  29986. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  29987. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  29988. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  29989. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  29990. 0x26, 0x00
  29991. };
  29992. /* compressed prefix */
  29993. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  29994. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  29995. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  29996. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  29997. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  29998. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  29999. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  30000. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  30001. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  30002. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  30003. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  30004. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  30005. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  30006. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  30007. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  30008. 0x26, 0x00
  30009. };
  30010. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  30011. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  30012. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  30013. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  30014. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  30015. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  30016. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  30017. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  30018. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  30019. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  30020. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  30021. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  30022. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  30023. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  30024. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  30025. 0x1a, 0x00
  30026. };
  30027. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  30028. #define SIGSZ sizeof(sig1)
  30029. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  30030. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  30031. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  30032. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  30033. /* test of a 1023 byte long message */
  30034. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  30035. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  30036. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  30037. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  30038. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  30039. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  30040. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  30041. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  30042. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  30043. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  30044. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  30045. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  30046. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  30047. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  30048. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  30049. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  30050. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  30051. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  30052. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  30053. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  30054. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  30055. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  30056. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  30057. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  30058. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  30059. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  30060. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  30061. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  30062. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  30063. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  30064. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  30065. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  30066. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  30067. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  30068. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  30069. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  30070. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  30071. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  30072. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  30073. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  30074. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  30075. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  30076. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  30077. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  30078. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  30079. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  30080. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  30081. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  30082. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  30083. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  30084. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  30085. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  30086. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  30087. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  30088. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  30089. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  30090. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  30091. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  30092. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  30093. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  30094. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  30095. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  30096. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  30097. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  30098. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  30099. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  30100. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  30101. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  30102. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  30103. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  30104. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  30105. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  30106. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  30107. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  30108. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  30109. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  30110. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  30111. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  30112. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  30113. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  30114. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  30115. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  30116. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  30117. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  30118. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  30119. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  30120. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  30121. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  30122. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  30123. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  30124. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  30125. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  30126. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  30127. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  30128. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  30129. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  30130. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  30131. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  30132. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  30133. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  30134. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  30135. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  30136. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  30137. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  30138. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  30139. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  30140. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  30141. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  30142. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  30143. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  30144. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  30145. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  30146. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  30147. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  30148. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  30149. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  30150. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  30151. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  30152. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  30153. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  30154. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  30155. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  30156. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  30157. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  30158. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  30159. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  30160. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  30161. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  30162. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  30163. };
  30164. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  30165. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  30166. sizeof(msg2),
  30167. sizeof(msg3),
  30168. 0 /*sizeof(msg1)*/,
  30169. 0 /*sizeof(msg1)*/,
  30170. sizeof(msg4)
  30171. };
  30172. #ifndef NO_ASN
  30173. static const byte privateEd448[] = {
  30174. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  30175. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  30176. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  30177. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  30178. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  30179. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  30180. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  30181. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  30182. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  30183. 0x5b
  30184. };
  30185. static const byte publicEd448[] = {
  30186. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  30187. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  30188. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  30189. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  30190. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  30191. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  30192. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  30193. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  30194. 0xaf, 0xe8, 0x25, 0x61, 0x80
  30195. };
  30196. static const byte privPubEd448[] = {
  30197. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  30198. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  30199. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  30200. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  30201. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  30202. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  30203. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  30204. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  30205. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  30206. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  30207. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  30208. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  30209. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  30210. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  30211. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  30212. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  30213. 0xaf, 0xe8, 0x25, 0x61, 0x80
  30214. };
  30215. word32 idx;
  30216. #endif /* NO_ASN */
  30217. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  30218. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30219. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30220. ed448_key *key3 = NULL;
  30221. #else
  30222. ed448_key key3[1];
  30223. #endif
  30224. #endif
  30225. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30226. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30227. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30228. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30229. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30230. #endif
  30231. #endif
  30232. /* create ed448 keys */
  30233. #ifndef HAVE_FIPS
  30234. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30235. #else
  30236. ret = wc_InitRng(&rng);
  30237. #endif
  30238. if (ret != 0) {
  30239. XMEMSET(&rng, 0, sizeof(rng));
  30240. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30241. }
  30242. ret = wc_ed448_init(key);
  30243. if (ret < 0)
  30244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30245. ret = wc_ed448_init(key2);
  30246. if (ret < 0)
  30247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30248. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30249. ret = wc_ed448_init(key3);
  30250. if (ret < 0)
  30251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30252. #endif
  30253. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  30254. if (ret < 0)
  30255. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30256. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  30257. if (ret < 0)
  30258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30259. /* helper functions for signature and key size */
  30260. keySz = wc_ed448_size(key);
  30261. sigSz = wc_ed448_sig_size(key);
  30262. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  30263. defined(HAVE_ED448_KEY_IMPORT)
  30264. for (i = 0; i < 6; i++) {
  30265. outlen = sizeof(out);
  30266. XMEMSET(out, 0, sizeof(out));
  30267. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  30268. pKeySz[i], key) != 0)
  30269. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30270. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  30271. 0) != 0)
  30272. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30273. if (XMEMCMP(out, sigs[i], 114))
  30274. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30275. #if defined(HAVE_ED448_VERIFY)
  30276. /* test verify on good msg */
  30277. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  30278. NULL, 0) != 0 || verify != 1)
  30279. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30280. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  30281. /* test verify on good msg using streaming interface directly */
  30282. if (wc_ed448_verify_msg_init(out, outlen,
  30283. key, (byte)Ed448, NULL, 0) != 0)
  30284. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30285. for (j = 0; j < msgSz[i]; j += i) {
  30286. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  30287. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30288. }
  30289. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  30290. key) != 0 || verify != 1)
  30291. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30292. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  30293. /* test verify on bad msg */
  30294. out[outlen-2] = out[outlen-2] + 1;
  30295. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  30296. NULL, 0) == 0 || verify == 1)
  30297. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30298. #endif /* HAVE_ED448_VERIFY */
  30299. /* test api for import/exporting keys */
  30300. {
  30301. byte *exportPKey = NULL;
  30302. byte *exportSKey = NULL;
  30303. word32 exportPSz = ED448_KEY_SIZE;
  30304. word32 exportSSz = ED448_KEY_SIZE;
  30305. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30306. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30307. if ((exportPKey == NULL) || (exportSKey == NULL))
  30308. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  30309. ret = 0;
  30310. do {
  30311. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  30312. ret = WC_TEST_RET_ENC_I(i);
  30313. break;
  30314. }
  30315. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  30316. ret = WC_TEST_RET_ENC_I(i);
  30317. break;
  30318. }
  30319. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  30320. ret = WC_TEST_RET_ENC_I(i);
  30321. break;
  30322. }
  30323. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  30324. exportPKey, exportPSz, key2) != 0) {
  30325. ret = WC_TEST_RET_ENC_I(i);
  30326. break;
  30327. }
  30328. /* clear "out" buffer and test sign with imported keys */
  30329. outlen = sizeof(out);
  30330. XMEMSET(out, 0, sizeof(out));
  30331. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  30332. 0) != 0) {
  30333. ret = WC_TEST_RET_ENC_I(i);
  30334. break;
  30335. }
  30336. } while(0);
  30337. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30338. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30339. if (ret != 0)
  30340. goto out;
  30341. }
  30342. #if defined(HAVE_ED448_VERIFY)
  30343. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  30344. NULL, 0) != 0 || verify != 1)
  30345. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30346. if (XMEMCMP(out, sigs[i], SIGSZ))
  30347. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30348. #endif /* HAVE_ED448_VERIFY */
  30349. }
  30350. ret = ed448_ctx_test();
  30351. if (ret != 0)
  30352. goto out;
  30353. ret = ed448ph_test();
  30354. if (ret != 0)
  30355. goto out;
  30356. #ifndef NO_ASN
  30357. /* Try ASN.1 encoded private-only key and public key. */
  30358. idx = 0;
  30359. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  30360. sizeof(privateEd448));
  30361. if (ret != 0)
  30362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30363. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  30364. if (ret != BAD_FUNC_ARG)
  30365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30366. idx = 0;
  30367. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  30368. if (ret != 0)
  30369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30370. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  30371. if (ret != 0)
  30372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30373. if (XMEMCMP(out, sigs[0], SIGSZ))
  30374. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  30375. #if defined(HAVE_ED448_VERIFY)
  30376. /* test verify on good msg */
  30377. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  30378. NULL, 0);
  30379. if (ret != 0 || verify != 1)
  30380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30381. #endif /* HAVE_ED448_VERIFY */
  30382. wc_ed448_free(key3);
  30383. ret = wc_ed448_init(key3);
  30384. if (ret < 0)
  30385. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30386. idx = 0;
  30387. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  30388. sizeof(privPubEd448));
  30389. if (ret != 0)
  30390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30391. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  30392. if (ret != 0)
  30393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30394. if (XMEMCMP(out, sigs[0], SIGSZ))
  30395. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  30396. #endif /* NO_ASN */
  30397. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  30398. ret = 0;
  30399. out:
  30400. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30401. if (key) {
  30402. wc_ed448_free(key);
  30403. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30404. }
  30405. if (key2) {
  30406. wc_ed448_free(key2);
  30407. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30408. }
  30409. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30410. if (key3) {
  30411. wc_ed448_free(key3);
  30412. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30413. }
  30414. #endif
  30415. #else
  30416. wc_ed448_free(key);
  30417. wc_ed448_free(key2);
  30418. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30419. wc_ed448_free(key3);
  30420. #endif
  30421. #endif
  30422. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  30423. wc_FreeRng(&rng);
  30424. #endif
  30425. if (ret < 0)
  30426. return ret;
  30427. /* hush warnings of unused keySz and sigSz */
  30428. (void)keySz;
  30429. (void)sigSz;
  30430. #ifdef WOLFSSL_TEST_CERT
  30431. ret = ed448_test_cert();
  30432. if (ret < 0)
  30433. return ret;
  30434. #ifdef WOLFSSL_CERT_GEN
  30435. ret = ed448_test_make_cert();
  30436. if (ret < 0)
  30437. return ret;
  30438. #endif /* WOLFSSL_CERT_GEN */
  30439. #endif /* WOLFSSL_TEST_CERT */
  30440. return 0;
  30441. }
  30442. #endif /* HAVE_ED448 */
  30443. #ifdef WOLFSSL_HAVE_KYBER
  30444. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  30445. #ifdef WOLFSSL_KYBER512
  30446. static wc_test_ret_t kyber512_kat(void)
  30447. {
  30448. KyberKey key;
  30449. wc_test_ret_t ret;
  30450. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  30451. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  30452. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  30453. byte ss[KYBER_SS_SZ];
  30454. byte ss_dec[KYBER_SS_SZ];
  30455. const byte kyber512_rand[] = {
  30456. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  30457. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  30458. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  30459. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  30460. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30461. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30462. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30463. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30464. };
  30465. const byte kyber512enc_rand[] = {
  30466. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  30467. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  30468. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  30469. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  30470. };
  30471. const byte kyber512_pk[] = {
  30472. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  30473. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  30474. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  30475. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  30476. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  30477. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  30478. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  30479. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  30480. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  30481. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  30482. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  30483. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  30484. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  30485. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  30486. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  30487. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  30488. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  30489. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  30490. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  30491. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  30492. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  30493. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  30494. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  30495. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  30496. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  30497. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  30498. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  30499. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  30500. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  30501. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  30502. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  30503. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  30504. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  30505. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  30506. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  30507. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  30508. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  30509. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  30510. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  30511. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  30512. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  30513. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  30514. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  30515. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  30516. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  30517. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  30518. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  30519. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  30520. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  30521. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  30522. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  30523. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  30524. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  30525. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  30526. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  30527. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  30528. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  30529. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  30530. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  30531. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  30532. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  30533. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  30534. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  30535. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  30536. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  30537. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  30538. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  30539. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  30540. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  30541. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  30542. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  30543. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  30544. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  30545. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  30546. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  30547. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  30548. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  30549. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  30550. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  30551. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  30552. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  30553. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  30554. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  30555. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  30556. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  30557. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  30558. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  30559. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  30560. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  30561. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  30562. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  30563. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  30564. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  30565. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  30566. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  30567. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  30568. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30569. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30570. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30571. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  30572. };
  30573. const byte kyber512_sk[] = {
  30574. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  30575. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  30576. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  30577. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  30578. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  30579. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  30580. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  30581. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  30582. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  30583. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  30584. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  30585. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  30586. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  30587. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  30588. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  30589. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  30590. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  30591. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  30592. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  30593. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  30594. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  30595. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  30596. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  30597. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  30598. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  30599. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  30600. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  30601. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  30602. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  30603. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  30604. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  30605. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  30606. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  30607. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  30608. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  30609. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  30610. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  30611. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  30612. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  30613. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  30614. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  30615. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  30616. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  30617. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  30618. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  30619. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  30620. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  30621. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  30622. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  30623. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  30624. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  30625. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  30626. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  30627. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  30628. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  30629. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  30630. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  30631. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  30632. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  30633. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  30634. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  30635. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  30636. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  30637. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  30638. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  30639. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  30640. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  30641. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  30642. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  30643. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  30644. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  30645. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  30646. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  30647. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  30648. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  30649. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  30650. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  30651. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  30652. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  30653. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  30654. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  30655. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  30656. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  30657. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  30658. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  30659. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  30660. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  30661. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  30662. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  30663. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  30664. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  30665. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  30666. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  30667. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  30668. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  30669. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  30670. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  30671. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  30672. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  30673. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  30674. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  30675. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  30676. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  30677. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  30678. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  30679. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  30680. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  30681. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  30682. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  30683. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  30684. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  30685. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  30686. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  30687. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  30688. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  30689. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  30690. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  30691. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  30692. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  30693. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  30694. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  30695. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  30696. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  30697. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  30698. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  30699. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  30700. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  30701. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  30702. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  30703. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  30704. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  30705. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  30706. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  30707. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  30708. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  30709. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  30710. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  30711. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  30712. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  30713. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  30714. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  30715. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  30716. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  30717. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  30718. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  30719. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  30720. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  30721. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  30722. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  30723. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  30724. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  30725. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  30726. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  30727. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  30728. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  30729. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  30730. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  30731. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  30732. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  30733. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  30734. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  30735. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  30736. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  30737. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  30738. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  30739. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  30740. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  30741. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  30742. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  30743. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  30744. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  30745. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  30746. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  30747. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  30748. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  30749. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  30750. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  30751. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  30752. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  30753. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  30754. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  30755. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  30756. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  30757. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  30758. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  30759. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  30760. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  30761. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  30762. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  30763. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  30764. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  30765. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  30766. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30767. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30768. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30769. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  30770. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  30771. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  30772. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  30773. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  30774. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30775. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30776. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30777. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30778. };
  30779. const byte kyber512_ct[] = {
  30780. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  30781. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  30782. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  30783. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  30784. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  30785. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  30786. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  30787. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  30788. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  30789. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  30790. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  30791. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  30792. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  30793. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  30794. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  30795. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  30796. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  30797. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  30798. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  30799. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  30800. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  30801. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  30802. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  30803. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  30804. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  30805. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  30806. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  30807. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  30808. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  30809. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  30810. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  30811. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  30812. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  30813. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  30814. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  30815. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  30816. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  30817. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  30818. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  30819. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  30820. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  30821. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  30822. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  30823. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  30824. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  30825. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  30826. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  30827. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  30828. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  30829. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  30830. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  30831. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  30832. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  30833. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  30834. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  30835. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  30836. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  30837. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  30838. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  30839. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  30840. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  30841. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  30842. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  30843. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  30844. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  30845. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  30846. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  30847. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  30848. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  30849. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  30850. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  30851. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  30852. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  30853. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  30854. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  30855. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  30856. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  30857. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  30858. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  30859. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  30860. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  30861. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  30862. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  30863. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  30864. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  30865. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  30866. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  30867. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  30868. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  30869. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  30870. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  30871. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  30872. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  30873. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  30874. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  30875. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  30876. };
  30877. const byte kyber512_ss[] = {
  30878. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  30879. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  30880. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  30881. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  30882. };
  30883. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  30884. if (ret != 0)
  30885. return WC_TEST_RET_ENC_EC(ret);
  30886. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  30887. sizeof(kyber512_rand));
  30888. if (ret != 0)
  30889. return WC_TEST_RET_ENC_EC(ret);
  30890. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30891. if (ret != 0)
  30892. return WC_TEST_RET_ENC_EC(ret);
  30893. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30894. if (ret != 0)
  30895. return WC_TEST_RET_ENC_EC(ret);
  30896. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  30897. return WC_TEST_RET_ENC_NC;
  30898. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  30899. return WC_TEST_RET_ENC_NC;
  30900. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  30901. sizeof(kyber512enc_rand));
  30902. if (ret != 0)
  30903. return WC_TEST_RET_ENC_EC(ret);
  30904. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  30905. return WC_TEST_RET_ENC_NC;
  30906. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  30907. return WC_TEST_RET_ENC_NC;
  30908. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  30909. if (ret != 0)
  30910. return WC_TEST_RET_ENC_EC(ret);
  30911. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  30912. return WC_TEST_RET_ENC_NC;
  30913. wc_KyberKey_Free(&key);
  30914. return 0;
  30915. }
  30916. #endif /* WOLFSSL_KYBER512 */
  30917. #ifdef WOLFSSL_KYBER768
  30918. static wc_test_ret_t kyber768_kat(void)
  30919. {
  30920. KyberKey key;
  30921. wc_test_ret_t ret;
  30922. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  30923. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  30924. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  30925. byte ss[KYBER_SS_SZ];
  30926. byte ss_dec[KYBER_SS_SZ];
  30927. const byte kyber768_rand[] = {
  30928. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  30929. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  30930. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  30931. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  30932. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30933. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30934. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30935. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30936. };
  30937. const byte kyber768enc_rand[] = {
  30938. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  30939. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  30940. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  30941. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  30942. };
  30943. const byte kyber768_pk[] = {
  30944. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  30945. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  30946. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  30947. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  30948. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  30949. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  30950. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  30951. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  30952. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  30953. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  30954. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  30955. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  30956. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  30957. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  30958. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  30959. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  30960. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  30961. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  30962. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  30963. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  30964. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  30965. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  30966. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  30967. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  30968. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  30969. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  30970. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  30971. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  30972. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  30973. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  30974. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  30975. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  30976. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  30977. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  30978. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  30979. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  30980. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  30981. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  30982. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  30983. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  30984. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  30985. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  30986. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  30987. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  30988. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  30989. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  30990. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  30991. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  30992. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  30993. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  30994. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  30995. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  30996. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  30997. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  30998. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  30999. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  31000. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  31001. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  31002. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  31003. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  31004. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  31005. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  31006. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  31007. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  31008. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  31009. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  31010. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  31011. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  31012. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  31013. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  31014. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  31015. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  31016. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  31017. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  31018. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  31019. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  31020. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  31021. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  31022. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  31023. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  31024. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  31025. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  31026. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  31027. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  31028. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  31029. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  31030. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  31031. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  31032. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  31033. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  31034. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  31035. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  31036. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  31037. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  31038. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  31039. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  31040. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  31041. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  31042. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  31043. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  31044. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  31045. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  31046. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  31047. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  31048. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  31049. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  31050. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  31051. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  31052. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  31053. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  31054. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  31055. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  31056. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  31057. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  31058. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  31059. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  31060. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  31061. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  31062. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  31063. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  31064. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  31065. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  31066. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  31067. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  31068. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  31069. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  31070. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  31071. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  31072. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  31073. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  31074. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  31075. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  31076. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  31077. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  31078. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  31079. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  31080. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  31081. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  31082. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  31083. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  31084. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  31085. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  31086. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  31087. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  31088. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31089. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31090. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31091. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  31092. };
  31093. const byte kyber768_sk[] = {
  31094. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  31095. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  31096. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  31097. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  31098. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  31099. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  31100. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  31101. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  31102. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  31103. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  31104. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  31105. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  31106. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  31107. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  31108. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  31109. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  31110. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  31111. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  31112. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  31113. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  31114. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  31115. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  31116. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  31117. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  31118. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  31119. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  31120. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  31121. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  31122. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  31123. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  31124. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  31125. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  31126. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  31127. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  31128. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  31129. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  31130. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  31131. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  31132. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  31133. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  31134. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  31135. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  31136. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  31137. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  31138. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  31139. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  31140. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  31141. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  31142. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  31143. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  31144. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  31145. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  31146. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  31147. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  31148. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  31149. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  31150. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  31151. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  31152. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  31153. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  31154. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  31155. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  31156. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  31157. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  31158. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  31159. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  31160. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  31161. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  31162. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  31163. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  31164. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  31165. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  31166. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  31167. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  31168. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  31169. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  31170. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  31171. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  31172. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  31173. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  31174. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  31175. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  31176. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  31177. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  31178. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  31179. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  31180. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  31181. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  31182. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  31183. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  31184. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  31185. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  31186. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  31187. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  31188. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  31189. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  31190. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  31191. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  31192. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  31193. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  31194. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  31195. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  31196. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  31197. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  31198. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  31199. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  31200. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  31201. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  31202. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  31203. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  31204. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  31205. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  31206. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  31207. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  31208. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  31209. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  31210. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  31211. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  31212. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  31213. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  31214. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  31215. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  31216. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  31217. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  31218. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  31219. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  31220. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  31221. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  31222. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  31223. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  31224. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  31225. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  31226. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  31227. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  31228. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  31229. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  31230. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  31231. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  31232. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  31233. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  31234. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  31235. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  31236. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  31237. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  31238. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  31239. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  31240. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  31241. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  31242. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  31243. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  31244. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  31245. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  31246. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  31247. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  31248. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  31249. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  31250. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  31251. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  31252. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  31253. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  31254. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  31255. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  31256. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  31257. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  31258. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  31259. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  31260. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  31261. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  31262. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  31263. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  31264. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  31265. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  31266. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  31267. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  31268. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  31269. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  31270. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  31271. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  31272. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  31273. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  31274. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  31275. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  31276. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  31277. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  31278. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  31279. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  31280. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  31281. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  31282. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  31283. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  31284. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  31285. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  31286. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  31287. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  31288. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  31289. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  31290. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  31291. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  31292. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  31293. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  31294. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  31295. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  31296. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  31297. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  31298. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  31299. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  31300. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  31301. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  31302. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  31303. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  31304. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  31305. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  31306. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  31307. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  31308. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  31309. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  31310. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  31311. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  31312. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  31313. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  31314. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  31315. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  31316. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  31317. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  31318. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  31319. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  31320. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  31321. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  31322. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  31323. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  31324. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  31325. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  31326. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  31327. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  31328. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  31329. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  31330. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  31331. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  31332. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  31333. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  31334. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  31335. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  31336. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  31337. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  31338. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  31339. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  31340. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  31341. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  31342. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  31343. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  31344. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  31345. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  31346. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  31347. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  31348. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  31349. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  31350. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  31351. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  31352. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  31353. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  31354. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  31355. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  31356. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  31357. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  31358. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  31359. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  31360. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  31361. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  31362. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  31363. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  31364. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  31365. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  31366. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  31367. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  31368. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  31369. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  31370. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  31371. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  31372. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  31373. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  31374. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  31375. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  31376. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  31377. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  31378. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  31379. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  31380. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  31381. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  31382. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31383. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31384. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31385. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  31386. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  31387. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  31388. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  31389. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  31390. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  31391. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  31392. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  31393. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  31394. };
  31395. const byte kyber768_ct[] = {
  31396. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  31397. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  31398. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  31399. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  31400. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  31401. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  31402. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  31403. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  31404. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  31405. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  31406. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  31407. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  31408. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  31409. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  31410. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  31411. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  31412. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  31413. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  31414. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  31415. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  31416. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  31417. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  31418. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  31419. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  31420. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  31421. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  31422. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  31423. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  31424. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  31425. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  31426. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  31427. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  31428. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  31429. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  31430. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  31431. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  31432. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  31433. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  31434. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  31435. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  31436. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  31437. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  31438. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  31439. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  31440. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  31441. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  31442. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  31443. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  31444. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  31445. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  31446. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  31447. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  31448. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  31449. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  31450. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  31451. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  31452. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  31453. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  31454. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  31455. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  31456. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  31457. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  31458. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  31459. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  31460. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  31461. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  31462. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  31463. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  31464. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  31465. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  31466. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  31467. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  31468. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  31469. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  31470. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  31471. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  31472. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  31473. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  31474. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  31475. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  31476. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  31477. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  31478. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  31479. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  31480. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  31481. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  31482. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  31483. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  31484. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  31485. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  31486. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  31487. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  31488. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  31489. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  31490. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  31491. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  31492. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  31493. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  31494. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  31495. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  31496. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  31497. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  31498. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  31499. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  31500. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  31501. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  31502. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  31503. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  31504. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  31505. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  31506. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  31507. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  31508. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  31509. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  31510. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  31511. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  31512. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  31513. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  31514. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  31515. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  31516. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  31517. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  31518. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  31519. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  31520. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  31521. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  31522. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  31523. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  31524. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  31525. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  31526. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  31527. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  31528. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  31529. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  31530. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  31531. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  31532. };
  31533. const byte kyber768_ss[] = {
  31534. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  31535. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  31536. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  31537. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  31538. };
  31539. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  31540. if (ret != 0)
  31541. return WC_TEST_RET_ENC_EC(ret);
  31542. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  31543. sizeof(kyber768_rand));
  31544. if (ret != 0)
  31545. return WC_TEST_RET_ENC_EC(ret);
  31546. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  31547. if (ret != 0)
  31548. return WC_TEST_RET_ENC_EC(ret);
  31549. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  31550. if (ret != 0)
  31551. return WC_TEST_RET_ENC_EC(ret);
  31552. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  31553. return WC_TEST_RET_ENC_NC;
  31554. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  31555. return WC_TEST_RET_ENC_NC;
  31556. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  31557. sizeof(kyber768enc_rand));
  31558. if (ret != 0)
  31559. return WC_TEST_RET_ENC_EC(ret);
  31560. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  31561. return WC_TEST_RET_ENC_NC;
  31562. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  31563. return WC_TEST_RET_ENC_NC;
  31564. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  31565. if (ret != 0)
  31566. return WC_TEST_RET_ENC_EC(ret);
  31567. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  31568. return WC_TEST_RET_ENC_NC;
  31569. wc_KyberKey_Free(&key);
  31570. return 0;
  31571. }
  31572. #endif /* WOLFSSL_KYBER768 */
  31573. #ifdef WOLFSSL_KYBER1024
  31574. static wc_test_ret_t kyber1024_kat(void)
  31575. {
  31576. KyberKey key;
  31577. wc_test_ret_t ret;
  31578. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  31579. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  31580. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  31581. byte ss[KYBER_SS_SZ];
  31582. byte ss_dec[KYBER_SS_SZ];
  31583. const byte kyber1024_rand[] = {
  31584. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  31585. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  31586. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  31587. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  31588. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  31589. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  31590. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  31591. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  31592. };
  31593. const byte kyber1024enc_rand[] = {
  31594. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  31595. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  31596. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  31597. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  31598. };
  31599. const byte kyber1024_pk[] = {
  31600. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  31601. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  31602. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  31603. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  31604. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  31605. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  31606. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  31607. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  31608. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  31609. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  31610. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  31611. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  31612. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  31613. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  31614. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  31615. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  31616. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  31617. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  31618. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  31619. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  31620. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  31621. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  31622. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  31623. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  31624. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  31625. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  31626. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  31627. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  31628. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  31629. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  31630. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  31631. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  31632. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  31633. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  31634. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  31635. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  31636. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  31637. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  31638. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  31639. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  31640. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  31641. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  31642. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  31643. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  31644. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  31645. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  31646. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  31647. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  31648. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  31649. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  31650. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  31651. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  31652. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  31653. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  31654. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  31655. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  31656. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  31657. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  31658. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  31659. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  31660. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  31661. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  31662. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  31663. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  31664. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  31665. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  31666. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  31667. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  31668. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  31669. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  31670. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  31671. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  31672. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  31673. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  31674. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  31675. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  31676. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  31677. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  31678. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  31679. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  31680. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  31681. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  31682. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  31683. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  31684. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  31685. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  31686. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  31687. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  31688. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  31689. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  31690. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  31691. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  31692. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  31693. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  31694. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  31695. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  31696. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  31697. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  31698. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  31699. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  31700. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  31701. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  31702. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  31703. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  31704. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  31705. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  31706. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  31707. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  31708. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  31709. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  31710. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  31711. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  31712. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  31713. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  31714. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  31715. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  31716. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  31717. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  31718. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  31719. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  31720. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  31721. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  31722. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  31723. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  31724. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  31725. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  31726. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  31727. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  31728. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  31729. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  31730. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  31731. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  31732. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  31733. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  31734. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  31735. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  31736. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  31737. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  31738. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  31739. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  31740. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  31741. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  31742. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  31743. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  31744. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  31745. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  31746. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  31747. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  31748. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  31749. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  31750. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  31751. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  31752. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  31753. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  31754. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  31755. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  31756. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  31757. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  31758. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  31759. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  31760. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  31761. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  31762. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  31763. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  31764. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  31765. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  31766. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  31767. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  31768. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  31769. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  31770. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  31771. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  31772. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  31773. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  31774. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  31775. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  31776. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  31777. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  31778. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  31779. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  31780. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  31781. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  31782. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  31783. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  31784. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  31785. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  31786. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  31787. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  31788. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  31789. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  31790. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  31791. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  31792. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31793. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31794. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31795. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  31796. };
  31797. const byte kyber1024_sk[] = {
  31798. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  31799. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  31800. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  31801. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  31802. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  31803. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  31804. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  31805. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  31806. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  31807. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  31808. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  31809. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  31810. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  31811. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  31812. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  31813. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  31814. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  31815. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  31816. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  31817. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  31818. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  31819. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  31820. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  31821. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  31822. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  31823. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  31824. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  31825. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  31826. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  31827. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  31828. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  31829. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  31830. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  31831. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  31832. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  31833. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  31834. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  31835. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  31836. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  31837. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  31838. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  31839. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  31840. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  31841. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  31842. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  31843. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  31844. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  31845. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  31846. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  31847. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  31848. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  31849. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  31850. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  31851. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  31852. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  31853. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  31854. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  31855. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  31856. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  31857. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  31858. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  31859. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  31860. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  31861. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  31862. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  31863. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  31864. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  31865. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  31866. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  31867. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  31868. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  31869. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  31870. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  31871. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  31872. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  31873. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  31874. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  31875. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  31876. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  31877. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  31878. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  31879. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  31880. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  31881. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  31882. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  31883. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  31884. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  31885. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  31886. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  31887. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  31888. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  31889. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  31890. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  31891. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  31892. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  31893. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  31894. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  31895. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  31896. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  31897. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  31898. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  31899. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  31900. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  31901. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  31902. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  31903. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  31904. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  31905. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  31906. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  31907. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  31908. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  31909. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  31910. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  31911. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  31912. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  31913. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  31914. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  31915. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  31916. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  31917. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  31918. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  31919. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  31920. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  31921. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  31922. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  31923. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  31924. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  31925. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  31926. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  31927. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  31928. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  31929. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  31930. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  31931. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  31932. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  31933. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  31934. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  31935. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  31936. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  31937. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  31938. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  31939. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  31940. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  31941. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  31942. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  31943. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  31944. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  31945. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  31946. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  31947. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  31948. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  31949. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  31950. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  31951. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  31952. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  31953. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  31954. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  31955. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  31956. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  31957. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  31958. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  31959. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  31960. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  31961. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  31962. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  31963. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  31964. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  31965. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  31966. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  31967. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  31968. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  31969. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  31970. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  31971. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  31972. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  31973. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  31974. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  31975. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  31976. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  31977. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  31978. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  31979. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  31980. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  31981. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  31982. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  31983. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  31984. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  31985. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  31986. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  31987. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  31988. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  31989. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  31990. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  31991. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  31992. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  31993. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  31994. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  31995. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  31996. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  31997. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  31998. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  31999. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  32000. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  32001. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  32002. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  32003. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  32004. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  32005. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  32006. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  32007. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  32008. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  32009. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  32010. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  32011. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  32012. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  32013. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  32014. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  32015. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  32016. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  32017. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  32018. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  32019. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  32020. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  32021. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  32022. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  32023. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  32024. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  32025. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  32026. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  32027. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  32028. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  32029. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  32030. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  32031. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  32032. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  32033. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  32034. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  32035. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  32036. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  32037. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  32038. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  32039. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  32040. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  32041. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  32042. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  32043. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  32044. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  32045. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  32046. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  32047. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  32048. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  32049. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  32050. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  32051. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  32052. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  32053. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  32054. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  32055. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  32056. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  32057. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  32058. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  32059. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  32060. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  32061. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  32062. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  32063. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  32064. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  32065. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  32066. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  32067. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  32068. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  32069. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  32070. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  32071. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  32072. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  32073. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  32074. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  32075. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  32076. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  32077. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  32078. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  32079. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  32080. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  32081. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  32082. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  32083. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  32084. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  32085. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  32086. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  32087. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  32088. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  32089. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  32090. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  32091. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  32092. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  32093. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  32094. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  32095. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  32096. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  32097. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  32098. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  32099. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  32100. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  32101. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  32102. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  32103. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  32104. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  32105. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  32106. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  32107. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  32108. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  32109. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  32110. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  32111. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  32112. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  32113. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  32114. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  32115. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  32116. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  32117. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  32118. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  32119. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  32120. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  32121. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  32122. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  32123. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  32124. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  32125. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  32126. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  32127. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  32128. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  32129. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  32130. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  32131. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  32132. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  32133. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  32134. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  32135. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  32136. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  32137. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  32138. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  32139. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  32140. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  32141. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  32142. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  32143. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  32144. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  32145. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  32146. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  32147. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  32148. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  32149. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  32150. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  32151. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  32152. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  32153. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  32154. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  32155. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  32156. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  32157. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  32158. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  32159. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  32160. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  32161. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  32162. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  32163. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  32164. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  32165. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  32166. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  32167. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  32168. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  32169. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  32170. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  32171. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  32172. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  32173. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  32174. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  32175. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  32176. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  32177. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  32178. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  32179. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  32180. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  32181. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  32182. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  32183. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  32184. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  32185. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  32186. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  32187. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  32188. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  32189. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  32190. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  32191. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  32192. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  32193. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  32194. };
  32195. const byte kyber1024_ct[] = {
  32196. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  32197. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  32198. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  32199. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  32200. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  32201. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  32202. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  32203. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  32204. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  32205. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  32206. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  32207. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  32208. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  32209. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  32210. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  32211. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  32212. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  32213. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  32214. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  32215. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  32216. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  32217. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  32218. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  32219. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  32220. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  32221. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  32222. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  32223. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  32224. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  32225. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  32226. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  32227. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  32228. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  32229. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  32230. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  32231. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  32232. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  32233. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  32234. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  32235. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  32236. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  32237. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  32238. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  32239. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  32240. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  32241. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  32242. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  32243. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  32244. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  32245. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  32246. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  32247. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  32248. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  32249. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  32250. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  32251. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  32252. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  32253. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  32254. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  32255. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  32256. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  32257. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  32258. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  32259. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  32260. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  32261. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  32262. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  32263. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  32264. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  32265. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  32266. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  32267. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  32268. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  32269. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  32270. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  32271. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  32272. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  32273. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  32274. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  32275. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  32276. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  32277. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  32278. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  32279. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  32280. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  32281. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  32282. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  32283. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  32284. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  32285. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  32286. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  32287. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  32288. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  32289. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  32290. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  32291. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  32292. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  32293. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  32294. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  32295. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  32296. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  32297. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  32298. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  32299. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  32300. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  32301. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  32302. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  32303. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  32304. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  32305. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  32306. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  32307. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  32308. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  32309. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  32310. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  32311. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  32312. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  32313. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  32314. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  32315. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  32316. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  32317. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  32318. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  32319. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  32320. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  32321. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  32322. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  32323. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  32324. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  32325. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  32326. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  32327. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  32328. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  32329. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  32330. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  32331. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  32332. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  32333. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  32334. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  32335. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  32336. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  32337. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  32338. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  32339. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  32340. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  32341. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  32342. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  32343. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  32344. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  32345. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  32346. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  32347. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  32348. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  32349. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  32350. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  32351. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  32352. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  32353. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  32354. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  32355. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  32356. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  32357. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  32358. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  32359. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  32360. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  32361. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  32362. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  32363. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  32364. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  32365. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  32366. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  32367. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  32368. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  32369. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  32370. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  32371. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  32372. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  32373. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  32374. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  32375. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  32376. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  32377. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  32378. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  32379. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  32380. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  32381. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  32382. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  32383. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  32384. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  32385. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  32386. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  32387. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  32388. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  32389. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  32390. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  32391. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  32392. };
  32393. const byte kyber1024_ss[] = {
  32394. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  32395. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  32396. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  32397. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  32398. };
  32399. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  32400. if (ret != 0)
  32401. return WC_TEST_RET_ENC_EC(ret);
  32402. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  32403. sizeof(kyber1024_rand));
  32404. if (ret != 0)
  32405. return WC_TEST_RET_ENC_EC(ret);
  32406. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  32407. if (ret != 0)
  32408. return WC_TEST_RET_ENC_EC(ret);
  32409. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  32410. if (ret != 0)
  32411. return WC_TEST_RET_ENC_EC(ret);
  32412. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  32413. return WC_TEST_RET_ENC_NC;
  32414. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  32415. return WC_TEST_RET_ENC_NC;
  32416. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  32417. sizeof(kyber1024enc_rand));
  32418. if (ret != 0)
  32419. return WC_TEST_RET_ENC_EC(ret);
  32420. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  32421. return WC_TEST_RET_ENC_NC;
  32422. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  32423. return WC_TEST_RET_ENC_NC;
  32424. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  32425. if (ret != 0)
  32426. return WC_TEST_RET_ENC_EC(ret);
  32427. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  32428. return WC_TEST_RET_ENC_NC;
  32429. wc_KyberKey_Free(&key);
  32430. return 0;
  32431. }
  32432. #endif /* WOLFSSL_KYBER1024 */
  32433. #endif /* WOLFSSL_WC_KYBER */
  32434. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  32435. {
  32436. wc_test_ret_t ret;
  32437. KyberKey key;
  32438. WC_RNG rng;
  32439. int i;
  32440. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  32441. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  32442. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  32443. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  32444. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  32445. byte ss[KYBER_SS_SZ];
  32446. byte ss_dec[KYBER_SS_SZ];
  32447. int testData[][4] = {
  32448. #ifdef WOLFSSL_KYBER512
  32449. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  32450. KYBER512_CIPHER_TEXT_SIZE },
  32451. #endif
  32452. #ifdef WOLFSSL_KYBER768
  32453. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  32454. KYBER768_CIPHER_TEXT_SIZE },
  32455. #endif
  32456. #ifdef WOLFSSL_KYBER1024
  32457. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  32458. KYBER1024_CIPHER_TEXT_SIZE },
  32459. #endif
  32460. };
  32461. #ifndef HAVE_FIPS
  32462. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  32463. #else
  32464. ret = wc_InitRng(&rng);
  32465. #endif
  32466. if (ret != 0)
  32467. return WC_TEST_RET_ENC_EC(ret);
  32468. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  32469. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  32470. if (ret != 0)
  32471. return WC_TEST_RET_ENC_I(i);
  32472. ret = wc_KyberKey_MakeKey(&key, &rng);
  32473. if (ret != 0)
  32474. return WC_TEST_RET_ENC_I(i);
  32475. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  32476. if (ret != 0)
  32477. return WC_TEST_RET_ENC_I(i);
  32478. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  32479. if (ret != 0)
  32480. return WC_TEST_RET_ENC_I(i);
  32481. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  32482. if (ret != 0)
  32483. return WC_TEST_RET_ENC_I(i);
  32484. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  32485. if (ret != 0)
  32486. return WC_TEST_RET_ENC_I(i);
  32487. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  32488. if (ret != 0)
  32489. return WC_TEST_RET_ENC_I(i);
  32490. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  32491. if (ret != 0)
  32492. return WC_TEST_RET_ENC_I(i);
  32493. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  32494. return WC_TEST_RET_ENC_I(i);
  32495. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  32496. if (ret != 0)
  32497. return WC_TEST_RET_ENC_I(i);
  32498. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  32499. if (ret != 0)
  32500. return WC_TEST_RET_ENC_I(i);
  32501. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  32502. if (ret != 0)
  32503. return WC_TEST_RET_ENC_I(i);
  32504. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  32505. return WC_TEST_RET_ENC_I(i);
  32506. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  32507. if (ret != 0)
  32508. return WC_TEST_RET_ENC_I(i);
  32509. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  32510. return WC_TEST_RET_ENC_I(i);
  32511. wc_KyberKey_Free(&key);
  32512. }
  32513. wc_FreeRng(&rng);
  32514. #ifdef WOLFSSL_WC_KYBER
  32515. #ifdef WOLFSSL_KYBER512
  32516. ret = kyber512_kat();
  32517. if (ret != 0)
  32518. return ret;
  32519. #endif
  32520. #ifdef WOLFSSL_KYBER768
  32521. ret = kyber768_kat();
  32522. if (ret != 0)
  32523. return ret;
  32524. #endif
  32525. #ifdef WOLFSSL_KYBER1024
  32526. ret = kyber1024_kat();
  32527. if (ret != 0)
  32528. return ret;
  32529. #endif
  32530. #endif /* WOLFSSL_WC_KYBER */
  32531. return 0;
  32532. }
  32533. #endif /* WOLFSSL_HAVE_KYBER */
  32534. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  32535. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  32536. void *context)
  32537. {
  32538. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  32539. * BE USED FOR TESTING PURPOSES! Production applications should
  32540. * write only to non-volatile storage. */
  32541. XMEMCPY(context, priv, privSz);
  32542. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  32543. }
  32544. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  32545. void *context)
  32546. {
  32547. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  32548. * BE USED FOR TESTING PURPOSES! */
  32549. XMEMCPY(priv, context, privSz);
  32550. return WC_XMSS_RC_READ_TO_MEMORY;
  32551. }
  32552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  32553. {
  32554. int i = 0;
  32555. int j = 0;
  32556. int ret = -1;
  32557. int ret2 = -1;
  32558. XmssKey signingKey;
  32559. XmssKey verifyKey;
  32560. WC_RNG rng;
  32561. word32 pkSz = 0;
  32562. word32 skSz = 0;
  32563. word32 sigSz = 0;
  32564. word32 bufSz = 0;
  32565. unsigned char * sk = NULL;
  32566. unsigned char * old_sk = NULL;
  32567. const char * msg = "XMSS post quantum signature test";
  32568. word32 msgSz = (word32) XSTRLEN(msg);
  32569. const char * param = "XMSSMT-SHA2_20/4_256";
  32570. byte * sig = NULL;
  32571. #ifndef HAVE_FIPS
  32572. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  32573. #else
  32574. ret = wc_InitRng(&rng);
  32575. #endif
  32576. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32577. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  32578. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32579. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  32580. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32581. /* Set the parameter string to the signing key, and
  32582. * get sizes for secret key, pub key, and signature. */
  32583. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  32584. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32585. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  32586. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32587. if (pkSz != XMSS_SHA256_PUBLEN) {
  32588. return WC_TEST_RET_ENC_EC(pkSz);
  32589. }
  32590. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  32591. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32592. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  32593. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32594. /* Allocate signature array. */
  32595. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32596. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  32597. bufSz = sigSz;
  32598. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  32599. fprintf(stderr, "param: %s\n", param);
  32600. fprintf(stderr, "pkSz: %d\n", pkSz);
  32601. fprintf(stderr, "skSz: %d\n", skSz);
  32602. fprintf(stderr, "sigSz: %d\n", sigSz);
  32603. #endif
  32604. /* Allocate current and old secret keys.*/
  32605. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32606. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  32607. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32608. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  32609. XMEMSET(sk, 0, skSz);
  32610. XMEMSET(old_sk, 0, skSz);
  32611. XMEMSET(sig, 0, sigSz);
  32612. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  32613. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32614. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  32615. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32616. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  32617. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32618. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  32619. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32620. /* Export the pub to a verify key. */
  32621. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  32622. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  32623. /* Repeat a few times to check that:
  32624. * 1. The secret key is mutated on each sign.
  32625. * 2. We can verify each new signature.
  32626. * Only do a few times, because the full signature space
  32627. * for this parameter set is huge. */
  32628. for (i = 0; i < 10; ++i) {
  32629. XMEMCPY(old_sk, sk, skSz);
  32630. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  32631. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  32632. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  32633. /* Old secret key and current secret key should not match. */
  32634. ret = XMEMCMP(old_sk, sk, skSz);
  32635. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  32636. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  32637. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  32638. /* Flip bits in a few places throughout the signature, stepping in multiple
  32639. * of hash size. These should all fail with -1. */
  32640. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  32641. sig[j] ^= 1;
  32642. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  32643. msgSz);
  32644. if (ret2 != -1) {
  32645. /* Verify passed when it should have failed. */
  32646. return WC_TEST_RET_ENC_I(j);
  32647. }
  32648. /* Flip this spot back. */
  32649. sig[j] ^= 1;
  32650. }
  32651. }
  32652. /* Cleanup everything. */
  32653. if (sig != NULL) {
  32654. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32655. sig = NULL;
  32656. }
  32657. if (sk != NULL) {
  32658. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32659. sk = NULL;
  32660. }
  32661. if (old_sk != NULL) {
  32662. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32663. old_sk = NULL;
  32664. }
  32665. wc_XmssKey_Free(&signingKey);
  32666. wc_FreeRng(&rng);
  32667. return ret;
  32668. }
  32669. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  32670. #if defined(WOLFSSL_HAVE_XMSS) && defined(WOLFSSL_XMSS_VERIFY_ONLY) && \
  32671. !defined(WOLFSSL_SMALL_STACK)
  32672. /* A simple xmss verify only test using:
  32673. * XMSS-SHA2_10_256
  32674. * pub len: 68
  32675. * msg len: 32
  32676. * sig len: 2500
  32677. *
  32678. * These were generated with the test xmss_fast, from the unpatched
  32679. * xmss-reference repository:
  32680. * https://github.com/XMSS/xmss-reference
  32681. * */
  32682. static byte xmss_pub[XMSS_SHA256_PUBLEN] =
  32683. {
  32684. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  32685. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  32686. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  32687. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  32688. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  32689. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  32690. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  32691. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  32692. 0xC9,0xB7,0x39,0x4E
  32693. };
  32694. static byte xmss_msg[32] =
  32695. {
  32696. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  32697. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  32698. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  32699. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  32700. };
  32701. /* This was actually the 5th signature produced from
  32702. * xmss_fast test in xmss-reference. */
  32703. static byte xmss_sig[2500] =
  32704. {
  32705. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  32706. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  32707. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  32708. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  32709. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  32710. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  32711. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  32712. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  32713. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  32714. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  32715. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  32716. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  32717. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  32718. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  32719. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  32720. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  32721. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  32722. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  32723. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  32724. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  32725. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  32726. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  32727. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  32728. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  32729. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  32730. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  32731. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  32732. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  32733. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  32734. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  32735. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  32736. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  32737. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  32738. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  32739. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  32740. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  32741. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  32742. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  32743. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  32744. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  32745. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  32746. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  32747. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  32748. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  32749. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  32750. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  32751. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  32752. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  32753. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  32754. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  32755. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  32756. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  32757. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  32758. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  32759. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  32760. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  32761. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  32762. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  32763. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  32764. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  32765. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  32766. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  32767. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  32768. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  32769. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  32770. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  32771. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  32772. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  32773. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  32774. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  32775. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  32776. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  32777. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  32778. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  32779. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  32780. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  32781. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  32782. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  32783. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  32784. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  32785. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  32786. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  32787. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  32788. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  32789. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  32790. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  32791. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  32792. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  32793. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  32794. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  32795. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  32796. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  32797. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  32798. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  32799. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  32800. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  32801. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  32802. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  32803. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  32804. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  32805. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  32806. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  32807. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  32808. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  32809. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  32810. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  32811. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  32812. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  32813. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  32814. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  32815. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  32816. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  32817. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  32818. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  32819. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  32820. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  32821. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  32822. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  32823. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  32824. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  32825. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  32826. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  32827. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  32828. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  32829. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  32830. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  32831. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  32832. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  32833. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  32834. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  32835. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  32836. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  32837. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  32838. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  32839. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  32840. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  32841. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  32842. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  32843. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  32844. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  32845. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  32846. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  32847. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  32848. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  32849. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  32850. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  32851. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  32852. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  32853. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  32854. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  32855. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  32856. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  32857. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  32858. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  32859. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  32860. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  32861. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  32862. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  32863. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  32864. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  32865. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  32866. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  32867. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  32868. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  32869. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  32870. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  32871. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  32872. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  32873. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  32874. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  32875. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  32876. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  32877. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  32878. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  32879. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  32880. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  32881. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  32882. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  32883. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  32884. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  32885. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  32886. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  32887. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  32888. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  32889. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  32890. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  32891. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  32892. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  32893. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  32894. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  32895. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  32896. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  32897. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  32898. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  32899. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  32900. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  32901. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  32902. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  32903. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  32904. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  32905. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  32906. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  32907. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  32908. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  32909. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  32910. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  32911. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  32912. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  32913. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  32914. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  32915. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  32916. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  32917. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  32918. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  32919. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  32920. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  32921. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  32922. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  32923. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  32924. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  32925. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  32926. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  32927. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  32928. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  32929. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  32930. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  32931. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  32932. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  32933. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  32934. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  32935. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  32936. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  32937. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  32938. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  32939. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  32940. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  32941. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  32942. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  32943. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  32944. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  32945. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  32946. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  32947. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  32948. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  32949. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  32950. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  32951. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  32952. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  32953. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  32954. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  32955. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  32956. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  32957. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  32958. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  32959. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  32960. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  32961. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  32962. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  32963. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  32964. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  32965. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  32966. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  32967. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  32968. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  32969. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  32970. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  32971. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  32972. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  32973. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  32974. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  32975. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  32976. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  32977. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  32978. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  32979. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  32980. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  32981. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  32982. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  32983. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  32984. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  32985. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  32986. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  32987. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  32988. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  32989. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  32990. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  32991. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  32992. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  32993. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  32994. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  32995. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  32996. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  32997. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  32998. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  32999. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  33000. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  33001. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  33002. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  33003. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  33004. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  33005. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  33006. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  33007. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  33008. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  33009. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  33010. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  33011. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  33012. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  33013. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  33014. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  33015. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  33016. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  33017. 0xB3,0x5D,0x73,0x52
  33018. };
  33019. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  33020. {
  33021. int ret = -1;
  33022. int ret2 = -1;
  33023. int j = 0;
  33024. XmssKey verifyKey;
  33025. word32 pkSz = 0;
  33026. word32 sigSz = 0;
  33027. const char * param = "XMSS-SHA2_10_256";
  33028. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33029. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33030. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  33031. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33032. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  33033. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33034. if (pkSz != XMSS_SHA256_PUBLEN) {
  33035. return WC_TEST_RET_ENC_EC(pkSz);
  33036. }
  33037. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  33038. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33039. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  33040. fprintf(stderr, "param: %s\n", param);
  33041. fprintf(stderr, "pkSz: %d\n", pkSz);
  33042. fprintf(stderr, "sigSz: %d\n", sigSz);
  33043. #endif
  33044. if (sigSz != sizeof(xmss_sig)) {
  33045. return WC_TEST_RET_ENC_EC(sigSz);
  33046. }
  33047. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  33048. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33049. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33050. (byte *) xmss_msg, sizeof(xmss_msg));
  33051. if (ret != 0) {
  33052. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  33053. return WC_TEST_RET_ENC_EC(ret);
  33054. }
  33055. /* Flip bits in message. This should fail. */
  33056. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  33057. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33058. (byte *) xmss_msg, sizeof(xmss_msg));
  33059. if (ret2 != -1) {
  33060. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  33061. return WC_TEST_RET_ENC_EC(ret);
  33062. }
  33063. /* Flip it back. This should pass again. */
  33064. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  33065. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33066. (byte *) xmss_msg, sizeof(xmss_msg));
  33067. if (ret != 0) {
  33068. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  33069. return WC_TEST_RET_ENC_EC(ret);
  33070. }
  33071. /* Flip bits in a few places throughout the signature, stepping in multiple
  33072. * of hash size. These should all fail with -1. */
  33073. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  33074. xmss_sig[j] ^= 1;
  33075. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33076. (byte *) xmss_msg, sizeof(xmss_msg));
  33077. if (ret2 != -1) {
  33078. /* Verify passed when it should have failed. */
  33079. return WC_TEST_RET_ENC_I(j);
  33080. }
  33081. /* Flip this spot back. */
  33082. xmss_sig[j] ^= 1;
  33083. }
  33084. /* Cleanup everything. */
  33085. wc_XmssKey_Free(&verifyKey);
  33086. return ret;
  33087. }
  33088. #endif /* if defined(WOLFSSL_HAVE_XMSS) && defined(WOLFSSL_XMSS_VERIFY_ONLY) &&
  33089. * !defined(WOLFSSL_SMALL_STACK) */
  33090. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  33091. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  33092. {
  33093. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  33094. * BE USED FOR TESTING PURPOSES! Production applications should
  33095. * write only to non-volatile storage. */
  33096. XMEMCPY(context, priv, privSz);
  33097. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  33098. }
  33099. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  33100. {
  33101. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  33102. * BE USED FOR TESTING PURPOSES! */
  33103. XMEMCPY(priv, context, privSz);
  33104. return WC_LMS_RC_READ_TO_MEMORY;
  33105. }
  33106. /* LMS signature sizes are a function of their parameters. This
  33107. * test has a signature of 8688 bytes. */
  33108. #define WC_TEST_LMS_SIG_LEN (8688)
  33109. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  33110. {
  33111. int i = 0;
  33112. int j = 0;
  33113. int ret = -1;
  33114. int ret2 = -1;
  33115. int sigsLeft = 0;
  33116. LmsKey signingKey;
  33117. LmsKey verifyKey;
  33118. WC_RNG rng;
  33119. word32 sigSz = 0;
  33120. const char * msg = "LMS HSS post quantum signature test";
  33121. word32 msgSz = (word32) XSTRLEN(msg);
  33122. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  33123. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  33124. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33125. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  33126. DYNAMIC_TYPE_TMP_BUFFER);
  33127. if (sig == NULL) {
  33128. return WC_TEST_RET_ENC_ERRNO;
  33129. }
  33130. #else
  33131. byte sig[WC_TEST_LMS_SIG_LEN];
  33132. #endif
  33133. XMEMSET(priv, 0, sizeof(priv));
  33134. XMEMSET(old_priv, 0, sizeof(old_priv));
  33135. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  33136. #ifndef HAVE_FIPS
  33137. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  33138. #else
  33139. ret = wc_InitRng(&rng);
  33140. #endif
  33141. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33142. /* This test:
  33143. * levels: 1
  33144. * height: 5
  33145. * winternitz: 1
  33146. *
  33147. * max sigs: 2 ** (1 * 5) = 32
  33148. * signature length: 8688
  33149. */
  33150. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  33151. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33152. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33153. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33154. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  33155. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33156. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  33157. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33158. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  33159. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33160. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  33161. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33162. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  33163. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33164. XMEMCPY(old_priv, priv, sizeof(priv));
  33165. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  33166. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33167. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  33168. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33169. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  33170. printf("error: got %d, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  33171. return WC_TEST_RET_ENC_EC(sigSz);
  33172. }
  33173. /* 2 ** 5 should be the max number of signatures */
  33174. for (i = 0; i < 32; ++i) {
  33175. /* We should have remaining signstures. */
  33176. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  33177. if (sigsLeft == 0) {
  33178. return WC_TEST_RET_ENC_EC(sigsLeft);
  33179. }
  33180. /* Sign with key. The private key will be updated on every signature. */
  33181. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  33182. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33183. /* The updated private key should not match the old one. */
  33184. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  33185. printf("error: current priv key should not match old: %d\n", i);
  33186. return WC_TEST_RET_ENC_I(i);
  33187. }
  33188. XMEMCPY(old_priv, priv, sizeof(priv));
  33189. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  33190. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33191. /* Flip bits in a few places throughout the signature, stepping in multiple
  33192. * of hash size. These should all fail with -1. */
  33193. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  33194. sig[j] ^= 1;
  33195. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  33196. msgSz);
  33197. if (ret2 != -1) {
  33198. /* Verify passed when it should have failed. */
  33199. return WC_TEST_RET_ENC_I(j);
  33200. }
  33201. /* Flip this spot back. */
  33202. sig[j] ^= 1;
  33203. }
  33204. }
  33205. /* This should be the last signature. */
  33206. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  33207. if (sigsLeft != 0) {
  33208. return WC_TEST_RET_ENC_EC(sigsLeft);
  33209. }
  33210. wc_LmsKey_Free(&signingKey);
  33211. wc_LmsKey_Free(&verifyKey);
  33212. wc_FreeRng(&rng);
  33213. return ret;
  33214. }
  33215. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  33216. #if defined(WOLFSSL_HAVE_LMS) && defined(WOLFSSL_LMS_VERIFY_ONLY) && \
  33217. !defined(WOLFSSL_SMALL_STACK)
  33218. /* A simple LMS verify only test.
  33219. *
  33220. * Note: LMS signature sizes are a function of their parameters. This
  33221. * test has a signature of 1456 bytes:
  33222. * levels: 1
  33223. * height: 10
  33224. * winternitz: 8
  33225. * max sigs: 2 ** (1 * 10) = 1024
  33226. * signature length: 1456
  33227. * */
  33228. /* "wolfSSL LMS example message!" without null terminator. */
  33229. static byte lms_msg[28] =
  33230. {
  33231. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  33232. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  33233. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  33234. 0x61,0x67,0x65,0x21
  33235. };
  33236. static byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  33237. {
  33238. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  33239. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  33240. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  33241. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  33242. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  33243. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  33244. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  33245. 0xC5,0xB4,0x8F,0x23
  33246. };
  33247. #define LMS_L1H10W8_SIGLEN (1456)
  33248. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  33249. {
  33250. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  33251. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  33252. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  33253. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  33254. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  33255. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  33256. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  33257. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  33258. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  33259. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  33260. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  33261. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  33262. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  33263. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  33264. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  33265. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  33266. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  33267. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  33268. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  33269. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  33270. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  33271. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  33272. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  33273. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  33274. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  33275. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  33276. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  33277. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  33278. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  33279. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  33280. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  33281. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  33282. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  33283. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  33284. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  33285. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  33286. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  33287. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  33288. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  33289. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  33290. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  33291. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  33292. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  33293. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  33294. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  33295. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  33296. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  33297. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  33298. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  33299. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  33300. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  33301. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  33302. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  33303. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  33304. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  33305. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  33306. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  33307. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  33308. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  33309. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  33310. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  33311. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  33312. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  33313. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  33314. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  33315. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  33316. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  33317. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  33318. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  33319. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  33320. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  33321. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  33322. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  33323. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  33324. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  33325. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  33326. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  33327. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  33328. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  33329. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  33330. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  33331. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  33332. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  33333. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  33334. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  33335. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  33336. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  33337. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  33338. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  33339. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  33340. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  33341. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  33342. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  33343. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  33344. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  33345. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  33346. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  33347. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  33348. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  33349. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  33350. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  33351. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  33352. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  33353. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  33354. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  33355. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  33356. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  33357. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  33358. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  33359. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  33360. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  33361. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  33362. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  33363. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  33364. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  33365. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  33366. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  33367. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  33368. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  33369. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  33370. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  33371. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  33372. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  33373. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  33374. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  33375. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  33376. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  33377. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  33378. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  33379. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  33380. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  33381. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  33382. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  33383. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  33384. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  33385. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  33386. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  33387. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  33388. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  33389. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  33390. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  33391. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  33392. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  33393. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  33394. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  33395. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  33396. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  33397. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  33398. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  33399. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  33400. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  33401. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  33402. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  33403. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  33404. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  33405. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  33406. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  33407. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  33408. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  33409. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  33410. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  33411. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  33412. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  33413. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  33414. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  33415. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  33416. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  33417. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  33418. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  33419. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  33420. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  33421. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  33422. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  33423. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  33424. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  33425. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  33426. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  33427. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  33428. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  33429. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  33430. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  33431. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  33432. };
  33433. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  33434. {
  33435. int ret = -1;
  33436. int ret2 = -1;
  33437. int j = 0;
  33438. LmsKey verifyKey;
  33439. word32 sigSz = 0;
  33440. word32 msgSz = sizeof(lms_msg);
  33441. word32 pubLen = 0;
  33442. int levels = 0;
  33443. int height = 0;
  33444. int winternitz = 0;
  33445. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33446. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33447. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  33448. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33449. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  33450. HSS_MAX_PUBLIC_KEY_LEN);
  33451. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33452. /* Verify parameters, pub length, and sig length are correct. */
  33453. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  33454. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33455. if (levels != 1 || height != 10 || winternitz != 8) {
  33456. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  33457. winternitz);
  33458. return -1;
  33459. }
  33460. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubLen);
  33461. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33462. if (pubLen != HSS_MAX_PUBLIC_KEY_LEN) {
  33463. printf("error: got %d, expected %d\n", pubLen, HSS_MAX_PUBLIC_KEY_LEN);
  33464. return WC_TEST_RET_ENC_EC(pubLen);
  33465. }
  33466. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  33467. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33468. if (sigSz != LMS_L1H10W8_SIGLEN) {
  33469. printf("error: got %d, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  33470. return WC_TEST_RET_ENC_EC(sigSz);
  33471. }
  33472. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  33473. (byte *) lms_msg, msgSz);
  33474. if (ret != 0) {
  33475. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  33476. return WC_TEST_RET_ENC_EC(ret);
  33477. }
  33478. /* Flip bits in message. This should fail. */
  33479. lms_msg[msgSz / 2] ^= 1;
  33480. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  33481. (byte *) lms_msg, msgSz);
  33482. if (ret2 != -1) {
  33483. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  33484. return WC_TEST_RET_ENC_EC(ret);
  33485. }
  33486. /* Flip it back. This should pass again. */
  33487. lms_msg[msgSz / 2] ^= 1;
  33488. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  33489. (byte *) lms_msg, msgSz);
  33490. if (ret != 0) {
  33491. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  33492. return WC_TEST_RET_ENC_EC(ret);
  33493. }
  33494. /* Flip bits in a few places throughout the signature, stepping in multiple
  33495. * of hash size. These should all fail with -1. */
  33496. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  33497. lms_L1H10W8_sig[j] ^= 1;
  33498. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  33499. LMS_L1H10W8_SIGLEN,
  33500. (byte *) lms_msg, msgSz);
  33501. if (ret2 != -1) {
  33502. /* Verify passed when it should have failed. */
  33503. return WC_TEST_RET_ENC_I(j);
  33504. }
  33505. /* Flip this spot back. */
  33506. lms_L1H10W8_sig[j] ^= 1;
  33507. }
  33508. wc_LmsKey_Free(&verifyKey);
  33509. return ret;
  33510. }
  33511. #endif /* if defined(WOLFSSL_HAVE_LMS) && defined(WOLFSSL_LMS_VERIFY_ONLY) &&
  33512. * !defined(WOLFSSL_SMALL_STACK) */
  33513. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  33514. * see print_fiducials() below.
  33515. */
  33516. #ifdef WOLFCRYPT_HAVE_ECCSI
  33517. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  33518. ecc_point* pvt)
  33519. {
  33520. wc_test_ret_t ret;
  33521. byte id[1] = { 0x00 };
  33522. int valid;
  33523. word32 sz;
  33524. byte data[256];
  33525. byte hash[WC_MAX_DIGEST_SIZE];
  33526. byte hashSz;
  33527. byte sig[257];
  33528. word32 sigSz;
  33529. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  33530. if (ret != BAD_FUNC_ARG)
  33531. return WC_TEST_RET_ENC_EC(ret);
  33532. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  33533. if (ret != BAD_FUNC_ARG)
  33534. return WC_TEST_RET_ENC_EC(ret);
  33535. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  33536. if (ret != BAD_FUNC_ARG)
  33537. return WC_TEST_RET_ENC_EC(ret);
  33538. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  33539. if (ret != BAD_FUNC_ARG)
  33540. return WC_TEST_RET_ENC_EC(ret);
  33541. wc_FreeEccsiKey(NULL);
  33542. /* Create a valid key. */
  33543. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  33544. if (ret != 0)
  33545. return WC_TEST_RET_ENC_EC(ret);
  33546. ret = wc_MakeEccsiKey(NULL, NULL);
  33547. if (ret != BAD_FUNC_ARG)
  33548. return WC_TEST_RET_ENC_EC(ret);
  33549. ret = wc_MakeEccsiKey(key, NULL);
  33550. if (ret != BAD_FUNC_ARG)
  33551. return WC_TEST_RET_ENC_EC(ret);
  33552. ret = wc_MakeEccsiKey(NULL, rng);
  33553. if (ret != BAD_FUNC_ARG)
  33554. return WC_TEST_RET_ENC_EC(ret);
  33555. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  33556. NULL);
  33557. if (ret != BAD_FUNC_ARG)
  33558. return WC_TEST_RET_ENC_EC(ret);
  33559. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  33560. if (ret != BAD_FUNC_ARG)
  33561. return WC_TEST_RET_ENC_EC(ret);
  33562. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  33563. if (ret != BAD_FUNC_ARG)
  33564. return WC_TEST_RET_ENC_EC(ret);
  33565. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  33566. if (ret != BAD_FUNC_ARG)
  33567. return WC_TEST_RET_ENC_EC(ret);
  33568. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  33569. if (ret != BAD_FUNC_ARG)
  33570. return WC_TEST_RET_ENC_EC(ret);
  33571. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  33572. if (ret != BAD_FUNC_ARG)
  33573. return WC_TEST_RET_ENC_EC(ret);
  33574. /* No key set */
  33575. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  33576. if (ret != BAD_STATE_E)
  33577. return WC_TEST_RET_ENC_EC(ret);
  33578. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  33579. NULL);
  33580. if (ret != BAD_FUNC_ARG)
  33581. return WC_TEST_RET_ENC_EC(ret);
  33582. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  33583. NULL);
  33584. if (ret != BAD_FUNC_ARG)
  33585. return WC_TEST_RET_ENC_EC(ret);
  33586. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  33587. &valid);
  33588. if (ret != BAD_FUNC_ARG)
  33589. return WC_TEST_RET_ENC_EC(ret);
  33590. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  33591. &valid);
  33592. if (ret != BAD_FUNC_ARG)
  33593. return WC_TEST_RET_ENC_EC(ret);
  33594. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  33595. &valid);
  33596. if (ret != BAD_FUNC_ARG)
  33597. return WC_TEST_RET_ENC_EC(ret);
  33598. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  33599. &valid);
  33600. if (ret != BAD_FUNC_ARG)
  33601. return WC_TEST_RET_ENC_EC(ret);
  33602. /* No key set */
  33603. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  33604. &valid);
  33605. if (ret != BAD_STATE_E)
  33606. return WC_TEST_RET_ENC_EC(ret);
  33607. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  33608. if (ret != BAD_FUNC_ARG)
  33609. return WC_TEST_RET_ENC_EC(ret);
  33610. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  33611. if (ret != BAD_FUNC_ARG)
  33612. return WC_TEST_RET_ENC_EC(ret);
  33613. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  33614. if (ret != BAD_FUNC_ARG)
  33615. return WC_TEST_RET_ENC_EC(ret);
  33616. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  33617. if (ret != BAD_FUNC_ARG)
  33618. return WC_TEST_RET_ENC_EC(ret);
  33619. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  33620. if (ret != BAD_FUNC_ARG)
  33621. return WC_TEST_RET_ENC_EC(ret);
  33622. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  33623. if (ret != BAD_FUNC_ARG)
  33624. return WC_TEST_RET_ENC_EC(ret);
  33625. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  33626. if (ret != BAD_FUNC_ARG)
  33627. return WC_TEST_RET_ENC_EC(ret);
  33628. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  33629. if (ret != BAD_FUNC_ARG)
  33630. return WC_TEST_RET_ENC_EC(ret);
  33631. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  33632. if (ret != BAD_FUNC_ARG)
  33633. return WC_TEST_RET_ENC_EC(ret);
  33634. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  33635. if (ret != BAD_FUNC_ARG)
  33636. return WC_TEST_RET_ENC_EC(ret);
  33637. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  33638. if (ret != BAD_FUNC_ARG)
  33639. return WC_TEST_RET_ENC_EC(ret);
  33640. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  33641. if (ret != BAD_FUNC_ARG)
  33642. return WC_TEST_RET_ENC_EC(ret);
  33643. /* No key created so no curve information. */
  33644. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  33645. if (ret != LENGTH_ONLY_E)
  33646. return WC_TEST_RET_ENC_EC(ret);
  33647. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  33648. if (ret != BAD_FUNC_ARG)
  33649. return WC_TEST_RET_ENC_EC(ret);
  33650. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  33651. if (ret != BAD_FUNC_ARG)
  33652. return WC_TEST_RET_ENC_EC(ret);
  33653. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  33654. if (ret != BAD_FUNC_ARG)
  33655. return WC_TEST_RET_ENC_EC(ret);
  33656. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  33657. if (ret != BAD_FUNC_ARG)
  33658. return WC_TEST_RET_ENC_EC(ret);
  33659. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  33660. if (ret != BAD_FUNC_ARG)
  33661. return WC_TEST_RET_ENC_EC(ret);
  33662. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  33663. if (ret != BAD_FUNC_ARG)
  33664. return WC_TEST_RET_ENC_EC(ret);
  33665. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  33666. if (ret != BAD_FUNC_ARG)
  33667. return WC_TEST_RET_ENC_EC(ret);
  33668. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  33669. if (ret != BAD_FUNC_ARG)
  33670. return WC_TEST_RET_ENC_EC(ret);
  33671. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  33672. if (ret != BAD_FUNC_ARG)
  33673. return WC_TEST_RET_ENC_EC(ret);
  33674. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  33675. if (ret != BAD_FUNC_ARG)
  33676. return WC_TEST_RET_ENC_EC(ret);
  33677. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  33678. if (ret != BAD_FUNC_ARG)
  33679. return WC_TEST_RET_ENC_EC(ret);
  33680. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  33681. if (ret != BAD_FUNC_ARG)
  33682. return WC_TEST_RET_ENC_EC(ret);
  33683. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  33684. if (ret != BAD_FUNC_ARG)
  33685. return WC_TEST_RET_ENC_EC(ret);
  33686. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  33687. if (ret != BAD_FUNC_ARG)
  33688. return WC_TEST_RET_ENC_EC(ret);
  33689. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  33690. if (ret != BAD_FUNC_ARG)
  33691. return WC_TEST_RET_ENC_EC(ret);
  33692. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  33693. if (ret != BAD_FUNC_ARG)
  33694. return WC_TEST_RET_ENC_EC(ret);
  33695. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  33696. if (ret != BAD_FUNC_ARG)
  33697. return WC_TEST_RET_ENC_EC(ret);
  33698. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  33699. if (ret != BAD_FUNC_ARG)
  33700. return WC_TEST_RET_ENC_EC(ret);
  33701. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  33702. if (ret != BAD_FUNC_ARG)
  33703. return WC_TEST_RET_ENC_EC(ret);
  33704. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  33705. if (ret != BAD_FUNC_ARG)
  33706. return WC_TEST_RET_ENC_EC(ret);
  33707. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  33708. if (ret != BAD_FUNC_ARG)
  33709. return WC_TEST_RET_ENC_EC(ret);
  33710. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  33711. if (ret != BAD_FUNC_ARG)
  33712. return WC_TEST_RET_ENC_EC(ret);
  33713. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  33714. if (ret != BAD_FUNC_ARG)
  33715. return WC_TEST_RET_ENC_EC(ret);
  33716. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  33717. if (ret != BAD_FUNC_ARG)
  33718. return WC_TEST_RET_ENC_EC(ret);
  33719. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  33720. if (ret != BAD_FUNC_ARG)
  33721. return WC_TEST_RET_ENC_EC(ret);
  33722. ret = wc_ExportEccsiKey(NULL, data, NULL);
  33723. if (ret != BAD_FUNC_ARG)
  33724. return WC_TEST_RET_ENC_EC(ret);
  33725. ret = wc_ExportEccsiKey(key, data, NULL);
  33726. if (ret != BAD_FUNC_ARG)
  33727. return WC_TEST_RET_ENC_EC(ret);
  33728. ret = wc_ExportEccsiKey(NULL, data, &sz);
  33729. if (ret != BAD_FUNC_ARG)
  33730. return WC_TEST_RET_ENC_EC(ret);
  33731. /* No key to export */
  33732. ret = wc_ExportEccsiKey(key, NULL, &sz);
  33733. if (ret != BAD_STATE_E)
  33734. return WC_TEST_RET_ENC_EC(ret);
  33735. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  33736. if (ret != BAD_FUNC_ARG)
  33737. return WC_TEST_RET_ENC_EC(ret);
  33738. ret = wc_ImportEccsiKey(key, NULL, 0);
  33739. if (ret != BAD_FUNC_ARG)
  33740. return WC_TEST_RET_ENC_EC(ret);
  33741. ret = wc_ImportEccsiKey(NULL, data, 0);
  33742. if (ret != BAD_FUNC_ARG)
  33743. return WC_TEST_RET_ENC_EC(ret);
  33744. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  33745. if (ret != BAD_FUNC_ARG)
  33746. return WC_TEST_RET_ENC_EC(ret);
  33747. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  33748. if (ret != BAD_FUNC_ARG)
  33749. return WC_TEST_RET_ENC_EC(ret);
  33750. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  33751. if (ret != BAD_FUNC_ARG)
  33752. return WC_TEST_RET_ENC_EC(ret);
  33753. /* No key to export */
  33754. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  33755. if (ret != BAD_STATE_E)
  33756. return WC_TEST_RET_ENC_EC(ret);
  33757. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  33758. if (ret != BAD_FUNC_ARG)
  33759. return WC_TEST_RET_ENC_EC(ret);
  33760. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  33761. if (ret != BAD_FUNC_ARG)
  33762. return WC_TEST_RET_ENC_EC(ret);
  33763. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  33764. if (ret != BAD_FUNC_ARG)
  33765. return WC_TEST_RET_ENC_EC(ret);
  33766. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  33767. if (ret != BAD_FUNC_ARG)
  33768. return WC_TEST_RET_ENC_EC(ret);
  33769. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  33770. if (ret != BAD_FUNC_ARG)
  33771. return WC_TEST_RET_ENC_EC(ret);
  33772. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  33773. if (ret != BAD_FUNC_ARG)
  33774. return WC_TEST_RET_ENC_EC(ret);
  33775. /* No key to export */
  33776. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  33777. if (ret != BAD_STATE_E)
  33778. return WC_TEST_RET_ENC_EC(ret);
  33779. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  33780. if (ret != BAD_FUNC_ARG)
  33781. return WC_TEST_RET_ENC_EC(ret);
  33782. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  33783. if (ret != BAD_FUNC_ARG)
  33784. return WC_TEST_RET_ENC_EC(ret);
  33785. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  33786. if (ret != BAD_FUNC_ARG)
  33787. return WC_TEST_RET_ENC_EC(ret);
  33788. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  33789. if (ret != BAD_FUNC_ARG)
  33790. return WC_TEST_RET_ENC_EC(ret);
  33791. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  33792. if (ret != BAD_FUNC_ARG)
  33793. return WC_TEST_RET_ENC_EC(ret);
  33794. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  33795. if (ret != BAD_FUNC_ARG)
  33796. return WC_TEST_RET_ENC_EC(ret);
  33797. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  33798. if (ret != BAD_FUNC_ARG)
  33799. return WC_TEST_RET_ENC_EC(ret);
  33800. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  33801. &hashSz);
  33802. if (ret != BAD_FUNC_ARG)
  33803. return WC_TEST_RET_ENC_EC(ret);
  33804. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  33805. if (ret != BAD_FUNC_ARG)
  33806. return WC_TEST_RET_ENC_EC(ret);
  33807. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  33808. if (ret != BAD_STATE_E)
  33809. return WC_TEST_RET_ENC_EC(ret);
  33810. ret = wc_SetEccsiHash(NULL, NULL, 1);
  33811. if (ret != BAD_FUNC_ARG)
  33812. return WC_TEST_RET_ENC_EC(ret);
  33813. ret = wc_SetEccsiHash(key, NULL, 1);
  33814. if (ret != BAD_FUNC_ARG)
  33815. return WC_TEST_RET_ENC_EC(ret);
  33816. ret = wc_SetEccsiHash(NULL, hash, 1);
  33817. if (ret != BAD_FUNC_ARG)
  33818. return WC_TEST_RET_ENC_EC(ret);
  33819. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  33820. if (ret != BAD_FUNC_ARG)
  33821. return WC_TEST_RET_ENC_EC(ret);
  33822. ret = wc_SetEccsiPair(key, NULL, NULL);
  33823. if (ret != BAD_FUNC_ARG)
  33824. return WC_TEST_RET_ENC_EC(ret);
  33825. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  33826. if (ret != BAD_FUNC_ARG)
  33827. return WC_TEST_RET_ENC_EC(ret);
  33828. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  33829. if (ret != BAD_FUNC_ARG)
  33830. return WC_TEST_RET_ENC_EC(ret);
  33831. ret = wc_SetEccsiPair(key, ssk, NULL);
  33832. if (ret != BAD_FUNC_ARG)
  33833. return WC_TEST_RET_ENC_EC(ret);
  33834. ret = wc_SetEccsiPair(key, NULL, pvt);
  33835. if (ret != BAD_FUNC_ARG)
  33836. return WC_TEST_RET_ENC_EC(ret);
  33837. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  33838. if (ret != BAD_FUNC_ARG)
  33839. return WC_TEST_RET_ENC_EC(ret);
  33840. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  33841. if (ret != BAD_FUNC_ARG)
  33842. return WC_TEST_RET_ENC_EC(ret);
  33843. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  33844. if (ret != BAD_FUNC_ARG)
  33845. return WC_TEST_RET_ENC_EC(ret);
  33846. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  33847. &sigSz);
  33848. if (ret != BAD_FUNC_ARG)
  33849. return WC_TEST_RET_ENC_EC(ret);
  33850. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  33851. &sigSz);
  33852. if (ret != BAD_FUNC_ARG)
  33853. return WC_TEST_RET_ENC_EC(ret);
  33854. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  33855. &sigSz);
  33856. if (ret != BAD_FUNC_ARG)
  33857. return WC_TEST_RET_ENC_EC(ret);
  33858. /* Key not set. */
  33859. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  33860. &sigSz);
  33861. if (ret != BAD_STATE_E)
  33862. return WC_TEST_RET_ENC_EC(ret);
  33863. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  33864. if (ret != BAD_FUNC_ARG)
  33865. return WC_TEST_RET_ENC_EC(ret);
  33866. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  33867. if (ret != BAD_FUNC_ARG)
  33868. return WC_TEST_RET_ENC_EC(ret);
  33869. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  33870. if (ret != BAD_FUNC_ARG)
  33871. return WC_TEST_RET_ENC_EC(ret);
  33872. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  33873. if (ret != BAD_FUNC_ARG)
  33874. return WC_TEST_RET_ENC_EC(ret);
  33875. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  33876. &valid);
  33877. if (ret != BAD_FUNC_ARG)
  33878. return WC_TEST_RET_ENC_EC(ret);
  33879. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  33880. if (ret != BAD_FUNC_ARG)
  33881. return WC_TEST_RET_ENC_EC(ret);
  33882. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  33883. &valid);
  33884. if (ret != BAD_FUNC_ARG)
  33885. return WC_TEST_RET_ENC_EC(ret);
  33886. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  33887. &valid);
  33888. if (ret != BAD_FUNC_ARG)
  33889. return WC_TEST_RET_ENC_EC(ret);
  33890. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  33891. &valid);
  33892. if (ret != BAD_FUNC_ARG)
  33893. return WC_TEST_RET_ENC_EC(ret);
  33894. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  33895. &valid);
  33896. if (ret != BAD_STATE_E)
  33897. return WC_TEST_RET_ENC_EC(ret);
  33898. ret = wc_SetEccsiPair(key, ssk, pvt);
  33899. if (ret != 0)
  33900. return WC_TEST_RET_ENC_EC(ret);
  33901. /* Identity hash not set. */
  33902. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  33903. &sigSz);
  33904. if (ret != BAD_STATE_E)
  33905. return WC_TEST_RET_ENC_EC(ret);
  33906. wc_FreeEccsiKey(key);
  33907. return 0;
  33908. }
  33909. /* RFC 6507: Appendix A */
  33910. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  33911. {
  33912. wc_test_ret_t ret;
  33913. int verified;
  33914. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  33915. word32 msgSz = sizeof(msg);
  33916. byte hash[WC_SHA256_DIGEST_SIZE];
  33917. byte hashSz = WC_SHA256_DIGEST_SIZE;
  33918. static const byte id[] = {
  33919. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  33920. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  33921. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  33922. 0x33, 0x00
  33923. };
  33924. word32 idSz = sizeof(id);
  33925. static const byte sig[] = {
  33926. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  33927. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  33928. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  33929. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  33930. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  33931. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  33932. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  33933. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  33934. 0x04,
  33935. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  33936. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  33937. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  33938. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  33939. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  33940. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  33941. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  33942. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  33943. };
  33944. word32 sigSz = sizeof(sig);
  33945. static const byte pubData[] = {
  33946. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  33947. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  33948. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  33949. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  33950. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  33951. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  33952. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  33953. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  33954. };
  33955. static const byte expHash[] = {
  33956. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  33957. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  33958. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  33959. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  33960. };
  33961. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  33962. if (ret != 0)
  33963. return WC_TEST_RET_ENC_EC(ret);
  33964. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  33965. if (ret != 0)
  33966. return WC_TEST_RET_ENC_EC(ret);
  33967. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  33968. &hashSz);
  33969. if (ret != 0)
  33970. return WC_TEST_RET_ENC_EC(ret);
  33971. if (hashSz != sizeof(expHash))
  33972. return WC_TEST_RET_ENC_NC;
  33973. if (XMEMCMP(hash, expHash, hashSz) != 0)
  33974. return WC_TEST_RET_ENC_NC;
  33975. ret = wc_SetEccsiHash(key, hash, hashSz);
  33976. if (ret != 0)
  33977. return WC_TEST_RET_ENC_EC(ret);
  33978. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  33979. &verified);
  33980. if (ret != 0)
  33981. return WC_TEST_RET_ENC_EC(ret);
  33982. if (!verified)
  33983. return WC_TEST_RET_ENC_NC;
  33984. return 0;
  33985. }
  33986. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  33987. {
  33988. wc_test_ret_t ret;
  33989. byte data[32 * 3];
  33990. word32 sz;
  33991. ecc_point* decPvt = NULL;
  33992. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33993. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  33994. DYNAMIC_TYPE_TMP_BUFFER);
  33995. if (decSsk == NULL)
  33996. return WC_TEST_RET_ENC_ERRNO;
  33997. #else
  33998. mp_int decSsk[1];
  33999. #endif
  34000. ret = mp_init(decSsk);
  34001. if (ret != 0)
  34002. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34003. decPvt = wc_ecc_new_point();
  34004. if (decPvt == NULL)
  34005. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34006. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  34007. if (ret != LENGTH_ONLY_E)
  34008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34009. if (sz != 32 * 3)
  34010. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34011. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  34012. if (ret != 0)
  34013. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34014. if (sz != 32* 3)
  34015. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34016. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  34017. if (ret != 0)
  34018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34019. ret = mp_cmp(ssk, decSsk);
  34020. if (ret != MP_EQ)
  34021. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34022. ret = wc_ecc_cmp_point(pvt, decPvt);
  34023. if (ret != MP_EQ)
  34024. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34025. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  34026. if (ret != LENGTH_ONLY_E)
  34027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34028. if (sz != 32)
  34029. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34030. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  34031. if (ret != 0)
  34032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34033. if (sz != 32)
  34034. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34035. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  34036. if (ret != 0)
  34037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34038. ret = mp_cmp(ssk, decSsk);
  34039. if (ret != MP_EQ)
  34040. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34041. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  34042. if (ret != LENGTH_ONLY_E)
  34043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34044. if (sz != 32 * 2)
  34045. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34046. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  34047. if (ret != 0)
  34048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34049. if (sz != 32 * 2)
  34050. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34051. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  34052. if (ret != 0)
  34053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34054. ret = wc_ecc_cmp_point(pvt, decPvt);
  34055. if (ret != MP_EQ)
  34056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34057. sz = sizeof(data);
  34058. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  34059. if (ret != 0)
  34060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34061. if (sz != 32 * 2 + 1)
  34062. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34063. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  34064. if (ret != 0)
  34065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34066. ret = wc_ecc_cmp_point(pvt, decPvt);
  34067. if (ret != MP_EQ)
  34068. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34069. wc_ecc_del_point(decPvt);
  34070. out:
  34071. mp_free(decSsk);
  34072. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34073. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34074. #endif
  34075. return ret;
  34076. }
  34077. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  34078. {
  34079. wc_test_ret_t ret;
  34080. byte data[32 * 3];
  34081. byte out[32 * 3];
  34082. word32 sz;
  34083. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  34084. if (ret != LENGTH_ONLY_E)
  34085. return WC_TEST_RET_ENC_EC(ret);
  34086. if (sz != 32 * 3)
  34087. return WC_TEST_RET_ENC_NC;
  34088. ret = wc_ExportEccsiKey(priv, data, &sz);
  34089. if (ret != 0)
  34090. return WC_TEST_RET_ENC_EC(ret);
  34091. ret = wc_ImportEccsiKey(priv, data, sz);
  34092. if (ret != 0)
  34093. return WC_TEST_RET_ENC_EC(ret);
  34094. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  34095. if (ret != LENGTH_ONLY_E)
  34096. return WC_TEST_RET_ENC_EC(ret);
  34097. if (sz != 32 * 3)
  34098. return WC_TEST_RET_ENC_NC;
  34099. ret = wc_ExportEccsiKey(priv, out, &sz);
  34100. if (ret != 0)
  34101. return WC_TEST_RET_ENC_EC(ret);
  34102. if (sz != 32 * 3)
  34103. return WC_TEST_RET_ENC_NC;
  34104. if (XMEMCMP(data, out, sz) != 0)
  34105. return WC_TEST_RET_ENC_NC;
  34106. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  34107. if (ret != LENGTH_ONLY_E)
  34108. return WC_TEST_RET_ENC_EC(ret);
  34109. if (sz != 32)
  34110. return WC_TEST_RET_ENC_NC;
  34111. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  34112. if (ret != 0)
  34113. return WC_TEST_RET_ENC_EC(ret);
  34114. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  34115. if (ret != 0)
  34116. return WC_TEST_RET_ENC_EC(ret);
  34117. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  34118. if (ret != LENGTH_ONLY_E)
  34119. return WC_TEST_RET_ENC_EC(ret);
  34120. if (sz != 32)
  34121. return WC_TEST_RET_ENC_NC;
  34122. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  34123. if (ret != 0)
  34124. return WC_TEST_RET_ENC_EC(ret);
  34125. if (sz != 32)
  34126. return WC_TEST_RET_ENC_NC;
  34127. if (XMEMCMP(data, out, sz) != 0)
  34128. return WC_TEST_RET_ENC_NC;
  34129. return 0;
  34130. }
  34131. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  34132. {
  34133. wc_test_ret_t ret;
  34134. byte data[32 * 2 + 1];
  34135. byte pubData[32 * 2 + 1];
  34136. word32 sz;
  34137. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  34138. if (ret != LENGTH_ONLY_E)
  34139. return WC_TEST_RET_ENC_EC(ret);
  34140. if (sz != 32 * 2)
  34141. return WC_TEST_RET_ENC_NC;
  34142. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  34143. if (ret != 0)
  34144. return WC_TEST_RET_ENC_EC(ret);
  34145. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  34146. if (ret != 0)
  34147. return WC_TEST_RET_ENC_EC(ret);
  34148. sz = sizeof(pubData);
  34149. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  34150. if (ret != 0)
  34151. return WC_TEST_RET_ENC_EC(ret);
  34152. if (sz != 32 * 2)
  34153. return WC_TEST_RET_ENC_NC;
  34154. if (XMEMCMP(data, pubData, sz) != 0)
  34155. return WC_TEST_RET_ENC_NC;
  34156. sz = sizeof(pubData);
  34157. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  34158. if (ret != 0)
  34159. return WC_TEST_RET_ENC_EC(ret);
  34160. if (sz != 32 * 2 + 1)
  34161. return WC_TEST_RET_ENC_NC;
  34162. if (pubData[0] != 0x04)
  34163. return WC_TEST_RET_ENC_NC;
  34164. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  34165. return WC_TEST_RET_ENC_NC;
  34166. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  34167. if (ret != 0)
  34168. return WC_TEST_RET_ENC_EC(ret);
  34169. return 0;
  34170. }
  34171. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  34172. mp_int* ssk, ecc_point* pvt)
  34173. {
  34174. wc_test_ret_t ret;
  34175. char mail[] = "test@wolfssl.com";
  34176. byte* id = (byte*)mail;
  34177. word32 idSz = (word32) XSTRLEN(mail);
  34178. int valid;
  34179. ret = wc_MakeEccsiKey(priv, rng);
  34180. if (ret != 0)
  34181. return WC_TEST_RET_ENC_EC(ret);
  34182. ret = eccsi_imp_exp_key_test(priv);
  34183. if (ret < 0)
  34184. return ret;
  34185. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  34186. if (ret < 0)
  34187. return ret;
  34188. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  34189. if (ret != 0)
  34190. return WC_TEST_RET_ENC_EC(ret);
  34191. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  34192. &valid);
  34193. if (ret != 0)
  34194. return WC_TEST_RET_ENC_EC(ret);
  34195. if (!valid)
  34196. return WC_TEST_RET_ENC_NC;
  34197. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  34198. if (ret != 0)
  34199. return ret;
  34200. return 0;
  34201. }
  34202. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  34203. mp_int* ssk, ecc_point* pvt)
  34204. {
  34205. wc_test_ret_t ret;
  34206. byte hashPriv[WC_MAX_DIGEST_SIZE];
  34207. byte hashPub[WC_MAX_DIGEST_SIZE];
  34208. byte hashSz;
  34209. byte sig[144];
  34210. word32 sigSz;
  34211. int verified, valid;
  34212. char mail[] = "test@wolfssl.com";
  34213. byte* id = (byte*)mail;
  34214. word32 idSz = (word32) XSTRLEN(mail);
  34215. byte msg[] = { 0x00 };
  34216. word32 msgSz = sizeof(msg);
  34217. #ifdef WOLFSSL_SHA384
  34218. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  34219. &hashSz);
  34220. if (ret != BAD_FUNC_ARG)
  34221. return WC_TEST_RET_ENC_EC(ret);
  34222. #endif
  34223. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  34224. &hashSz);
  34225. if (ret != 0)
  34226. return WC_TEST_RET_ENC_EC(ret);
  34227. if (hashSz != 32)
  34228. return WC_TEST_RET_ENC_NC;
  34229. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  34230. &hashSz);
  34231. if (ret != 0)
  34232. return WC_TEST_RET_ENC_EC(ret);
  34233. if (hashSz != 32)
  34234. return WC_TEST_RET_ENC_NC;
  34235. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  34236. return WC_TEST_RET_ENC_NC;
  34237. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  34238. if (ret != 0)
  34239. return WC_TEST_RET_ENC_EC(ret);
  34240. ret = wc_SetEccsiPair(priv, ssk, pvt);
  34241. if (ret != 0)
  34242. return WC_TEST_RET_ENC_EC(ret);
  34243. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  34244. &sigSz);
  34245. if (ret != LENGTH_ONLY_E)
  34246. return WC_TEST_RET_ENC_EC(ret);
  34247. if (sigSz != 129)
  34248. return WC_TEST_RET_ENC_NC;
  34249. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  34250. &sigSz);
  34251. if (ret != 0)
  34252. return WC_TEST_RET_ENC_EC(ret);
  34253. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34254. if (ret != 0)
  34255. return WC_TEST_RET_ENC_EC(ret);
  34256. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34257. &verified);
  34258. if (ret != 0)
  34259. return WC_TEST_RET_ENC_EC(ret);
  34260. if (!verified)
  34261. return WC_TEST_RET_ENC_NC;
  34262. /* Check that changing HS results in verification failure. */
  34263. hashPub[0] ^= 0x80;
  34264. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34265. if (ret != 0)
  34266. return WC_TEST_RET_ENC_EC(ret);
  34267. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34268. &verified);
  34269. if (ret != 0)
  34270. return WC_TEST_RET_ENC_EC(ret);
  34271. if (verified)
  34272. return WC_TEST_RET_ENC_NC;
  34273. hashPub[0] ^= 0x80;
  34274. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34275. if (ret != 0)
  34276. return WC_TEST_RET_ENC_EC(ret);
  34277. /* Check that changing msg results in verification failure. */
  34278. msg[0] ^= 0x80;
  34279. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34280. &verified);
  34281. if (ret != 0)
  34282. return WC_TEST_RET_ENC_EC(ret);
  34283. if (verified)
  34284. return WC_TEST_RET_ENC_NC;
  34285. msg[0] ^= 0x80;
  34286. /* Check that changing signature results in verification failure. */
  34287. sig[0] ^= 0x80;
  34288. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34289. &verified);
  34290. if (ret != 0)
  34291. return WC_TEST_RET_ENC_EC(ret);
  34292. if (verified)
  34293. return WC_TEST_RET_ENC_NC;
  34294. sig[0] ^= 0x80;
  34295. /* Check that key state hasn't been invalidated. */
  34296. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34297. &verified);
  34298. if (ret != 0)
  34299. return WC_TEST_RET_ENC_EC(ret);
  34300. if (!verified)
  34301. return WC_TEST_RET_ENC_NC;
  34302. /* Check that verifying with the private key works. */
  34303. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34304. &verified);
  34305. if (ret != 0)
  34306. return WC_TEST_RET_ENC_EC(ret);
  34307. if (!verified)
  34308. return WC_TEST_RET_ENC_NC;
  34309. /* Check that the KPAK is converted from montgomery form. */
  34310. ret = eccsi_imp_exp_key_test(priv);
  34311. if (ret != 0)
  34312. return ret;
  34313. /* Check that KPAK can converted to Montgomery form again. */
  34314. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34315. &verified);
  34316. if (ret != 0)
  34317. return WC_TEST_RET_ENC_EC(ret);
  34318. if (!verified)
  34319. return WC_TEST_RET_ENC_NC;
  34320. /* Check that the KPAK is converted from montgomery form. */
  34321. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  34322. &valid);
  34323. if (ret != 0)
  34324. return WC_TEST_RET_ENC_EC(ret);
  34325. if (!valid)
  34326. return WC_TEST_RET_ENC_NC;
  34327. /* Check that KPAK can converted to Montgomery form again. */
  34328. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34329. &verified);
  34330. if (ret != 0)
  34331. return WC_TEST_RET_ENC_EC(ret);
  34332. if (!verified)
  34333. return WC_TEST_RET_ENC_NC;
  34334. /* Check that the KPAK is converted from montgomery form. */
  34335. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  34336. if (ret != 0)
  34337. return ret;
  34338. return 0;
  34339. }
  34340. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  34341. {
  34342. wc_test_ret_t ret = 0;
  34343. WC_RNG rng;
  34344. int rng_inited = 0;
  34345. EccsiKey* priv = NULL;
  34346. EccsiKey* pub = NULL;
  34347. mp_int* ssk = NULL;
  34348. ecc_point* pvt = NULL;
  34349. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  34350. DYNAMIC_TYPE_TMP_BUFFER);
  34351. if (priv == NULL)
  34352. ret = WC_TEST_RET_ENC_NC;
  34353. else
  34354. XMEMSET(priv, 0, sizeof(*priv));
  34355. if (ret == 0) {
  34356. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  34357. DYNAMIC_TYPE_TMP_BUFFER);
  34358. if (pub == NULL)
  34359. ret = WC_TEST_RET_ENC_NC;
  34360. else
  34361. XMEMSET(pub, 0, sizeof(*pub));
  34362. }
  34363. if (ret == 0) {
  34364. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  34365. DYNAMIC_TYPE_TMP_BUFFER);
  34366. if (ssk == NULL)
  34367. ret = WC_TEST_RET_ENC_NC;
  34368. else
  34369. XMEMSET(ssk, 0, sizeof(*ssk));
  34370. }
  34371. if (ret == 0) {
  34372. #ifndef HAVE_FIPS
  34373. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34374. #else
  34375. ret = wc_InitRng(&rng);
  34376. #endif
  34377. if (ret != 0)
  34378. ret = WC_TEST_RET_ENC_EC(ret);
  34379. else
  34380. rng_inited = 1;
  34381. }
  34382. if (ret == 0) {
  34383. pvt = wc_ecc_new_point();
  34384. if (pvt == NULL)
  34385. ret = WC_TEST_RET_ENC_NC;
  34386. }
  34387. if (ret == 0) {
  34388. ret = mp_init(ssk);
  34389. if (ret != 0)
  34390. ret = WC_TEST_RET_ENC_EC(ret);
  34391. }
  34392. if (ret == 0) {
  34393. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  34394. }
  34395. if (ret == 0) {
  34396. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  34397. if (ret != 0)
  34398. ret = WC_TEST_RET_ENC_EC(ret);
  34399. }
  34400. if (ret == 0) {
  34401. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  34402. if (ret != 0)
  34403. ret = WC_TEST_RET_ENC_EC(ret);
  34404. }
  34405. if (ret == 0) {
  34406. ret = eccsi_kat_verify_test(pub, pvt);
  34407. }
  34408. if (ret == 0) {
  34409. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  34410. }
  34411. if (ret == 0) {
  34412. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  34413. }
  34414. if (pvt != NULL)
  34415. wc_ecc_del_point(pvt);
  34416. if (rng_inited)
  34417. wc_FreeRng(&rng);
  34418. if (ssk != NULL) {
  34419. mp_free(ssk);
  34420. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34421. }
  34422. if (pub != NULL) {
  34423. wc_FreeEccsiKey(pub);
  34424. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34425. }
  34426. if (priv != NULL) {
  34427. wc_FreeEccsiKey(priv);
  34428. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34429. }
  34430. return ret;
  34431. }
  34432. #endif /* WOLFCRYPT_HAVE_ECCSI */
  34433. #ifdef WOLFCRYPT_HAVE_SAKKE
  34434. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  34435. {
  34436. wc_test_ret_t ret;
  34437. byte id[1] = { 0x00 };
  34438. int valid;
  34439. byte data[256];
  34440. word32 sz;
  34441. byte auth[257];
  34442. word16 authSz;
  34443. byte ssv[256];
  34444. word16 ssvSz;
  34445. word32 len;
  34446. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  34447. if (ret != BAD_FUNC_ARG)
  34448. return WC_TEST_RET_ENC_EC(ret);
  34449. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  34450. if (ret != BAD_FUNC_ARG)
  34451. return WC_TEST_RET_ENC_EC(ret);
  34452. wc_FreeSakkeKey(NULL);
  34453. XMEMSET(key, 0, sizeof(*key));
  34454. wc_FreeSakkeKey(key);
  34455. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  34456. if (ret != 0)
  34457. return WC_TEST_RET_ENC_EC(ret);
  34458. ret = wc_MakeSakkeKey(NULL, NULL);
  34459. if (ret != BAD_FUNC_ARG)
  34460. return WC_TEST_RET_ENC_EC(ret);
  34461. ret = wc_MakeSakkeKey(key, NULL);
  34462. if (ret != BAD_FUNC_ARG)
  34463. return WC_TEST_RET_ENC_EC(ret);
  34464. ret = wc_MakeSakkeKey(NULL, rng);
  34465. if (ret != BAD_FUNC_ARG)
  34466. return WC_TEST_RET_ENC_EC(ret);
  34467. ret = wc_MakeSakkePublicKey(NULL, NULL);
  34468. if (ret != BAD_FUNC_ARG)
  34469. return WC_TEST_RET_ENC_EC(ret);
  34470. ret = wc_MakeSakkePublicKey(key, NULL);
  34471. if (ret != BAD_FUNC_ARG)
  34472. return WC_TEST_RET_ENC_EC(ret);
  34473. ret = wc_MakeSakkePublicKey(NULL, rsk);
  34474. if (ret != BAD_FUNC_ARG)
  34475. return WC_TEST_RET_ENC_EC(ret);
  34476. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  34477. if (ret != BAD_FUNC_ARG)
  34478. return WC_TEST_RET_ENC_EC(ret);
  34479. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  34480. if (ret != BAD_FUNC_ARG)
  34481. return WC_TEST_RET_ENC_EC(ret);
  34482. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  34483. if (ret != BAD_FUNC_ARG)
  34484. return WC_TEST_RET_ENC_EC(ret);
  34485. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  34486. if (ret != BAD_FUNC_ARG)
  34487. return WC_TEST_RET_ENC_EC(ret);
  34488. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  34489. if (ret != BAD_FUNC_ARG)
  34490. return WC_TEST_RET_ENC_EC(ret);
  34491. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  34492. if (ret != BAD_FUNC_ARG)
  34493. return WC_TEST_RET_ENC_EC(ret);
  34494. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  34495. if (ret != BAD_FUNC_ARG)
  34496. return WC_TEST_RET_ENC_EC(ret);
  34497. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  34498. if (ret != BAD_FUNC_ARG)
  34499. return WC_TEST_RET_ENC_EC(ret);
  34500. ret = wc_ExportSakkeKey(key, data, NULL);
  34501. if (ret != BAD_FUNC_ARG)
  34502. return WC_TEST_RET_ENC_EC(ret);
  34503. ret = wc_ExportSakkeKey(NULL, data, &sz);
  34504. if (ret != BAD_FUNC_ARG)
  34505. return WC_TEST_RET_ENC_EC(ret);
  34506. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  34507. if (ret != BAD_FUNC_ARG)
  34508. return WC_TEST_RET_ENC_EC(ret);
  34509. ret = wc_ImportSakkeKey(key, NULL, 1);
  34510. if (ret != BAD_FUNC_ARG)
  34511. return WC_TEST_RET_ENC_EC(ret);
  34512. ret = wc_ImportSakkeKey(NULL, data, 1);
  34513. if (ret != BAD_FUNC_ARG)
  34514. return WC_TEST_RET_ENC_EC(ret);
  34515. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  34516. if (ret != BAD_FUNC_ARG)
  34517. return WC_TEST_RET_ENC_EC(ret);
  34518. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  34519. if (ret != BAD_FUNC_ARG)
  34520. return WC_TEST_RET_ENC_EC(ret);
  34521. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  34522. if (ret != BAD_FUNC_ARG)
  34523. return WC_TEST_RET_ENC_EC(ret);
  34524. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  34525. if (ret != BAD_FUNC_ARG)
  34526. return WC_TEST_RET_ENC_EC(ret);
  34527. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  34528. if (ret != BAD_FUNC_ARG)
  34529. return WC_TEST_RET_ENC_EC(ret);
  34530. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  34531. if (ret != BAD_FUNC_ARG)
  34532. return WC_TEST_RET_ENC_EC(ret);
  34533. sz = sizeof(data);
  34534. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  34535. if (ret != BAD_FUNC_ARG)
  34536. return WC_TEST_RET_ENC_EC(ret);
  34537. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  34538. if (ret != BAD_FUNC_ARG)
  34539. return WC_TEST_RET_ENC_EC(ret);
  34540. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  34541. if (ret != BAD_FUNC_ARG)
  34542. return WC_TEST_RET_ENC_EC(ret);
  34543. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  34544. if (ret != BAD_FUNC_ARG)
  34545. return WC_TEST_RET_ENC_EC(ret);
  34546. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  34547. if (ret != BAD_FUNC_ARG)
  34548. return WC_TEST_RET_ENC_EC(ret);
  34549. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  34550. if (ret != BAD_FUNC_ARG)
  34551. return WC_TEST_RET_ENC_EC(ret);
  34552. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  34553. if (ret != BAD_FUNC_ARG)
  34554. return WC_TEST_RET_ENC_EC(ret);
  34555. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  34556. if (ret != BAD_FUNC_ARG)
  34557. return WC_TEST_RET_ENC_EC(ret);
  34558. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  34559. if (ret != BAD_FUNC_ARG)
  34560. return WC_TEST_RET_ENC_EC(ret);
  34561. ret = wc_ImportSakkeRsk(key, NULL, sz);
  34562. if (ret != BAD_FUNC_ARG)
  34563. return WC_TEST_RET_ENC_EC(ret);
  34564. ret = wc_ImportSakkeRsk(NULL, data, sz);
  34565. if (ret != BAD_FUNC_ARG)
  34566. return WC_TEST_RET_ENC_EC(ret);
  34567. ret = wc_ImportSakkeRsk(key, data, 1);
  34568. if (ret != BUFFER_E)
  34569. return WC_TEST_RET_ENC_EC(ret);
  34570. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  34571. if (ret != BAD_FUNC_ARG)
  34572. return WC_TEST_RET_ENC_EC(ret);
  34573. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  34574. if (ret != BAD_FUNC_ARG)
  34575. return WC_TEST_RET_ENC_EC(ret);
  34576. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  34577. if (ret != BAD_FUNC_ARG)
  34578. return WC_TEST_RET_ENC_EC(ret);
  34579. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  34580. if (ret != BAD_FUNC_ARG)
  34581. return WC_TEST_RET_ENC_EC(ret);
  34582. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  34583. if (ret != BAD_FUNC_ARG)
  34584. return WC_TEST_RET_ENC_EC(ret);
  34585. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  34586. if (ret != BAD_FUNC_ARG)
  34587. return WC_TEST_RET_ENC_EC(ret);
  34588. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  34589. if (ret != BAD_FUNC_ARG)
  34590. return WC_TEST_RET_ENC_EC(ret);
  34591. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  34592. if (ret != LENGTH_ONLY_E)
  34593. return WC_TEST_RET_ENC_EC(ret);
  34594. len--;
  34595. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  34596. if (ret != BUFFER_E)
  34597. return WC_TEST_RET_ENC_EC(ret);
  34598. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  34599. if (ret != BAD_FUNC_ARG)
  34600. return WC_TEST_RET_ENC_EC(ret);
  34601. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  34602. if (ret != BAD_FUNC_ARG)
  34603. return WC_TEST_RET_ENC_EC(ret);
  34604. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  34605. if (ret != BAD_FUNC_ARG)
  34606. return WC_TEST_RET_ENC_EC(ret);
  34607. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  34608. if (ret != BAD_FUNC_ARG)
  34609. return WC_TEST_RET_ENC_EC(ret);
  34610. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  34611. if (ret != BAD_FUNC_ARG)
  34612. return WC_TEST_RET_ENC_EC(ret);
  34613. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  34614. if (ret != BAD_FUNC_ARG)
  34615. return WC_TEST_RET_ENC_EC(ret);
  34616. ret = wc_GetSakkeAuthSize(NULL, NULL);
  34617. if (ret != BAD_FUNC_ARG)
  34618. return WC_TEST_RET_ENC_EC(ret);
  34619. ret = wc_GetSakkeAuthSize(key, NULL);
  34620. if (ret != BAD_FUNC_ARG)
  34621. return WC_TEST_RET_ENC_EC(ret);
  34622. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  34623. if (ret != BAD_FUNC_ARG)
  34624. return WC_TEST_RET_ENC_EC(ret);
  34625. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  34626. if (ret != BAD_FUNC_ARG)
  34627. return WC_TEST_RET_ENC_EC(ret);
  34628. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  34629. if (ret != BAD_FUNC_ARG)
  34630. return WC_TEST_RET_ENC_EC(ret);
  34631. ret = wc_MakeSakkePointI(NULL, id, 1);
  34632. if (ret != BAD_FUNC_ARG)
  34633. return WC_TEST_RET_ENC_EC(ret);
  34634. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  34635. if (ret != BAD_FUNC_ARG)
  34636. return WC_TEST_RET_ENC_EC(ret);
  34637. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  34638. if (ret != BAD_FUNC_ARG)
  34639. return WC_TEST_RET_ENC_EC(ret);
  34640. ret = wc_MakeSakkePointI(key, NULL, 1);
  34641. if (ret != BAD_FUNC_ARG)
  34642. return WC_TEST_RET_ENC_EC(ret);
  34643. ret = wc_MakeSakkePointI(NULL, id, 1);
  34644. if (ret != BAD_FUNC_ARG)
  34645. return WC_TEST_RET_ENC_EC(ret);
  34646. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  34647. if (ret != BAD_FUNC_ARG)
  34648. return WC_TEST_RET_ENC_EC(ret);
  34649. ret = wc_GenerateSakkePointITable(key, data, NULL);
  34650. if (ret != BAD_FUNC_ARG)
  34651. return WC_TEST_RET_ENC_EC(ret);
  34652. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  34653. if (ret != BAD_FUNC_ARG)
  34654. return WC_TEST_RET_ENC_EC(ret);
  34655. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  34656. if (ret != LENGTH_ONLY_E)
  34657. return WC_TEST_RET_ENC_EC(ret);
  34658. len--;
  34659. ret = wc_GenerateSakkePointITable(key, data, &len);
  34660. if (ret != BUFFER_E)
  34661. return WC_TEST_RET_ENC_EC(ret);
  34662. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  34663. if (ret != BAD_FUNC_ARG)
  34664. return WC_TEST_RET_ENC_EC(ret);
  34665. ret = wc_SetSakkePointITable(key, NULL, 1);
  34666. if (ret != BAD_FUNC_ARG)
  34667. return WC_TEST_RET_ENC_EC(ret);
  34668. ret = wc_SetSakkePointITable(NULL, data, 1);
  34669. if (ret != BAD_FUNC_ARG)
  34670. return WC_TEST_RET_ENC_EC(ret);
  34671. ret = wc_SetSakkePointITable(key, data, 1);
  34672. if (ret != BUFFER_E)
  34673. return WC_TEST_RET_ENC_EC(ret);
  34674. ret = wc_ClearSakkePointITable(NULL);
  34675. if (ret != BAD_FUNC_ARG)
  34676. return WC_TEST_RET_ENC_EC(ret);
  34677. ret = wc_GetSakkePointI(NULL, data, NULL);
  34678. if (ret != BAD_FUNC_ARG)
  34679. return WC_TEST_RET_ENC_EC(ret);
  34680. ret = wc_GetSakkePointI(key, data, NULL);
  34681. if (ret != BAD_FUNC_ARG)
  34682. return WC_TEST_RET_ENC_EC(ret);
  34683. ret = wc_GetSakkePointI(NULL, data, &sz);
  34684. if (ret != BAD_FUNC_ARG)
  34685. return WC_TEST_RET_ENC_EC(ret);
  34686. sz = 1;
  34687. ret = wc_GetSakkePointI(key, data, &sz);
  34688. if (ret != BUFFER_E)
  34689. return WC_TEST_RET_ENC_EC(ret);
  34690. sz = 256;
  34691. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  34692. if (ret != BAD_FUNC_ARG)
  34693. return WC_TEST_RET_ENC_EC(ret);
  34694. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  34695. if (ret != BAD_FUNC_ARG)
  34696. return WC_TEST_RET_ENC_EC(ret);
  34697. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  34698. if (ret != BAD_FUNC_ARG)
  34699. return WC_TEST_RET_ENC_EC(ret);
  34700. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  34701. if (ret != BAD_FUNC_ARG)
  34702. return WC_TEST_RET_ENC_EC(ret);
  34703. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  34704. if (ret != BAD_FUNC_ARG)
  34705. return WC_TEST_RET_ENC_EC(ret);
  34706. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  34707. if (ret != BAD_FUNC_ARG)
  34708. return WC_TEST_RET_ENC_EC(ret);
  34709. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  34710. if (ret != BAD_FUNC_ARG)
  34711. return WC_TEST_RET_ENC_EC(ret);
  34712. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  34713. if (ret != BUFFER_E)
  34714. return WC_TEST_RET_ENC_EC(ret);
  34715. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  34716. if (ret != BUFFER_E)
  34717. return WC_TEST_RET_ENC_EC(ret);
  34718. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  34719. if (ret != BAD_FUNC_ARG)
  34720. return WC_TEST_RET_ENC_EC(ret);
  34721. ret = wc_SetSakkeIdentity(key, NULL, 1);
  34722. if (ret != BAD_FUNC_ARG)
  34723. return WC_TEST_RET_ENC_EC(ret);
  34724. ret = wc_SetSakkeIdentity(NULL, id, 1);
  34725. if (ret != BAD_FUNC_ARG)
  34726. return WC_TEST_RET_ENC_EC(ret);
  34727. ssvSz = sizeof(ssv);
  34728. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  34729. auth, NULL);
  34730. if (ret != BAD_FUNC_ARG)
  34731. return WC_TEST_RET_ENC_EC(ret);
  34732. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  34733. auth, NULL);
  34734. if (ret != BAD_FUNC_ARG)
  34735. return WC_TEST_RET_ENC_EC(ret);
  34736. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  34737. auth, NULL);
  34738. if (ret != BAD_FUNC_ARG)
  34739. return WC_TEST_RET_ENC_EC(ret);
  34740. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  34741. auth, &authSz);
  34742. if (ret != BAD_FUNC_ARG)
  34743. return WC_TEST_RET_ENC_EC(ret);
  34744. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  34745. auth, NULL);
  34746. if (ret != BAD_FUNC_ARG)
  34747. return WC_TEST_RET_ENC_EC(ret);
  34748. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  34749. auth, &authSz);
  34750. if (ret != BAD_FUNC_ARG)
  34751. return WC_TEST_RET_ENC_EC(ret);
  34752. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  34753. auth, &authSz);
  34754. if (ret != BAD_FUNC_ARG)
  34755. return WC_TEST_RET_ENC_EC(ret);
  34756. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  34757. auth, &authSz);
  34758. if (ret != BAD_STATE_E)
  34759. return WC_TEST_RET_ENC_EC(ret);
  34760. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  34761. if (ret != BAD_FUNC_ARG)
  34762. return WC_TEST_RET_ENC_EC(ret);
  34763. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  34764. if (ret != BAD_FUNC_ARG)
  34765. return WC_TEST_RET_ENC_EC(ret);
  34766. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  34767. if (ret != BAD_FUNC_ARG)
  34768. return WC_TEST_RET_ENC_EC(ret);
  34769. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  34770. if (ret != BAD_FUNC_ARG)
  34771. return WC_TEST_RET_ENC_EC(ret);
  34772. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  34773. if (ret != BAD_FUNC_ARG)
  34774. return WC_TEST_RET_ENC_EC(ret);
  34775. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  34776. if (ret != BAD_FUNC_ARG)
  34777. return WC_TEST_RET_ENC_EC(ret);
  34778. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  34779. if (ret != BAD_FUNC_ARG)
  34780. return WC_TEST_RET_ENC_EC(ret);
  34781. ssvSz = sizeof(ssv);
  34782. authSz = sizeof(auth);
  34783. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  34784. authSz);
  34785. if (ret != BAD_FUNC_ARG)
  34786. return WC_TEST_RET_ENC_EC(ret);
  34787. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  34788. authSz);
  34789. if (ret != BAD_FUNC_ARG)
  34790. return WC_TEST_RET_ENC_EC(ret);
  34791. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  34792. authSz);
  34793. if (ret != BAD_FUNC_ARG)
  34794. return WC_TEST_RET_ENC_EC(ret);
  34795. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  34796. authSz);
  34797. if (ret != BAD_FUNC_ARG)
  34798. return WC_TEST_RET_ENC_EC(ret);
  34799. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  34800. authSz);
  34801. if (ret != BAD_FUNC_ARG)
  34802. return WC_TEST_RET_ENC_EC(ret);
  34803. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  34804. authSz);
  34805. if (ret != BAD_FUNC_ARG)
  34806. return WC_TEST_RET_ENC_EC(ret);
  34807. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  34808. authSz);
  34809. if (ret != BAD_FUNC_ARG)
  34810. return WC_TEST_RET_ENC_EC(ret);
  34811. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  34812. authSz);
  34813. if (ret != BAD_STATE_E)
  34814. return WC_TEST_RET_ENC_EC(ret);
  34815. ret = wc_SetSakkeIdentity(key, id, 1);
  34816. if (ret != 0)
  34817. return WC_TEST_RET_ENC_EC(ret);
  34818. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  34819. authSz);
  34820. if (ret != BAD_STATE_E)
  34821. return WC_TEST_RET_ENC_EC(ret);
  34822. ret = wc_SetSakkeIdentity(key, id, 0);
  34823. if (ret != 0)
  34824. return WC_TEST_RET_ENC_EC(ret);
  34825. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  34826. if (ret != 0)
  34827. return WC_TEST_RET_ENC_EC(ret);
  34828. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  34829. authSz);
  34830. if (ret != BAD_STATE_E)
  34831. return WC_TEST_RET_ENC_EC(ret);
  34832. wc_FreeSakkeKey(key);
  34833. return 0;
  34834. }
  34835. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  34836. {
  34837. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  34838. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  34839. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  34840. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  34841. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  34842. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  34843. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  34844. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  34845. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  34846. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  34847. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  34848. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  34849. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  34850. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  34851. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  34852. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  34853. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  34854. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  34855. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  34856. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  34857. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  34858. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  34859. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  34860. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  34861. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  34862. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  34863. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  34864. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  34865. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  34866. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  34867. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  34868. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  34869. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  34870. };
  34871. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  34872. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  34873. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  34874. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  34875. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  34876. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  34877. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  34878. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  34879. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  34880. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  34881. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  34882. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  34883. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  34884. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  34885. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  34886. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  34887. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  34888. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  34889. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  34890. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  34891. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  34892. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  34893. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  34894. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  34895. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  34896. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  34897. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  34898. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  34899. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  34900. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  34901. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  34902. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  34903. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  34904. };
  34905. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  34906. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  34907. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  34908. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  34909. 0x33, 0x00
  34910. };
  34911. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  34912. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  34913. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  34914. };
  34915. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  34916. 0x04,
  34917. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  34918. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  34919. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  34920. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  34921. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  34922. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  34923. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  34924. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  34925. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  34926. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  34927. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  34928. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  34929. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  34930. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  34931. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  34932. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  34933. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  34934. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  34935. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  34936. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  34937. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  34938. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  34939. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  34940. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  34941. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  34942. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  34943. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  34944. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  34945. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  34946. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  34947. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  34948. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  34949. };
  34950. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  34951. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  34952. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  34953. };
  34954. wc_test_ret_t ret;
  34955. int valid;
  34956. byte pubKey[sizeof(pubData) + 1];
  34957. word32 sz = sizeof(pubKey);
  34958. byte tmpSsv[sizeof(encSsv)];
  34959. byte* iTable = NULL;
  34960. word32 iTableLen;
  34961. byte* table = NULL;
  34962. word32 len;
  34963. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  34964. if (ret != 0)
  34965. return WC_TEST_RET_ENC_EC(ret);
  34966. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  34967. if (ret != 0)
  34968. return WC_TEST_RET_ENC_EC(ret);
  34969. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  34970. if (ret != 0)
  34971. return WC_TEST_RET_ENC_EC(ret);
  34972. if (valid != 1)
  34973. return WC_TEST_RET_ENC_NC;
  34974. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  34975. if (ret != 0)
  34976. return WC_TEST_RET_ENC_EC(ret);
  34977. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  34978. if (ret != 0)
  34979. return WC_TEST_RET_ENC_EC(ret);
  34980. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  34981. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  34982. auth, sizeof(auth));
  34983. if (ret != 0)
  34984. return WC_TEST_RET_ENC_EC(ret);
  34985. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  34986. return WC_TEST_RET_ENC_NC;
  34987. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  34988. if (ret != 0)
  34989. return WC_TEST_RET_ENC_EC(ret);
  34990. iTableLen = 0;
  34991. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  34992. if (ret != LENGTH_ONLY_E)
  34993. return WC_TEST_RET_ENC_EC(ret);
  34994. if (iTableLen != 0) {
  34995. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34996. if (iTable == NULL)
  34997. return WC_TEST_RET_ENC_ERRNO;
  34998. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  34999. if (ret != 0)
  35000. return WC_TEST_RET_ENC_EC(ret);
  35001. }
  35002. len = 0;
  35003. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  35004. if (ret != LENGTH_ONLY_E)
  35005. return WC_TEST_RET_ENC_EC(ret);
  35006. if (len > 0) {
  35007. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35008. if (table == NULL)
  35009. return WC_TEST_RET_ENC_ERRNO;
  35010. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  35011. if (ret != 0)
  35012. return WC_TEST_RET_ENC_EC(ret);
  35013. }
  35014. ret = wc_SetSakkeRsk(key, rsk, table, len);
  35015. if (ret != 0)
  35016. return WC_TEST_RET_ENC_EC(ret);
  35017. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  35018. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  35019. auth, sizeof(auth));
  35020. if (ret != 0)
  35021. return WC_TEST_RET_ENC_EC(ret);
  35022. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  35023. return WC_TEST_RET_ENC_NC;
  35024. /* Don't reference table that is about to be freed. */
  35025. ret = wc_ClearSakkePointITable(key);
  35026. if (ret != 0)
  35027. return WC_TEST_RET_ENC_EC(ret);
  35028. /* Dispose of tables */
  35029. if (iTable != NULL)
  35030. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35031. if (table != NULL)
  35032. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35033. /* Make sure the key public key is exportable - convert to Montgomery form
  35034. * in Validation.
  35035. */
  35036. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  35037. if (ret != 0)
  35038. return WC_TEST_RET_ENC_EC(ret);
  35039. if (sz != sizeof(pubData))
  35040. return WC_TEST_RET_ENC_NC;
  35041. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  35042. return WC_TEST_RET_ENC_NC;
  35043. sz = sizeof(pubData) + 1;
  35044. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  35045. if (ret != 0)
  35046. return WC_TEST_RET_ENC_EC(ret);
  35047. if (sz != sizeof(pubData) + 1)
  35048. return WC_TEST_RET_ENC_NC;
  35049. if (pubKey[0] != 0x04)
  35050. return WC_TEST_RET_ENC_NC;
  35051. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  35052. return WC_TEST_RET_ENC_NC;
  35053. return 0;
  35054. }
  35055. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  35056. {
  35057. static const byte pubData[] = {
  35058. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  35059. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  35060. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  35061. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  35062. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  35063. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  35064. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  35065. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  35066. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  35067. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  35068. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  35069. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  35070. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  35071. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  35072. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  35073. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  35074. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  35075. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  35076. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  35077. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  35078. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  35079. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  35080. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  35081. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  35082. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  35083. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  35084. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  35085. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  35086. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  35087. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  35088. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  35089. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  35090. };
  35091. static const byte id[] = {
  35092. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  35093. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  35094. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  35095. 0x33, 0x00
  35096. };
  35097. static word32 idSz = sizeof(id);
  35098. byte ssv[] = {
  35099. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  35100. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  35101. };
  35102. static word16 ssvSz = sizeof(ssv);
  35103. static const byte expAuth[] = {
  35104. 0x04,
  35105. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  35106. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  35107. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  35108. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  35109. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  35110. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  35111. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  35112. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  35113. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  35114. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  35115. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  35116. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  35117. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  35118. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  35119. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  35120. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  35121. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  35122. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  35123. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  35124. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  35125. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  35126. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  35127. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  35128. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  35129. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  35130. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  35131. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  35132. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  35133. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  35134. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  35135. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  35136. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  35137. };
  35138. static const byte encSsv[] = {
  35139. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  35140. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  35141. };
  35142. wc_test_ret_t ret;
  35143. byte auth[257];
  35144. word16 authSz = sizeof(auth);
  35145. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  35146. if (ret != 0)
  35147. return WC_TEST_RET_ENC_EC(ret);
  35148. ret = wc_SetSakkeIdentity(key, id, idSz);
  35149. if (ret != 0)
  35150. return WC_TEST_RET_ENC_EC(ret);
  35151. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35152. auth, &authSz);
  35153. if (ret != 0)
  35154. return WC_TEST_RET_ENC_EC(ret);
  35155. if (authSz != 257)
  35156. return WC_TEST_RET_ENC_NC;
  35157. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  35158. return WC_TEST_RET_ENC_NC;
  35159. if (XMEMCMP(auth, expAuth, authSz) != 0)
  35160. return WC_TEST_RET_ENC_NC;
  35161. return 0;
  35162. }
  35163. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  35164. WC_RNG* rng, ecc_point* rsk)
  35165. {
  35166. wc_test_ret_t ret;
  35167. byte data[440];
  35168. byte pubData[257];
  35169. word32 sz;
  35170. char mail[] = "test@wolfssl.com";
  35171. byte* id = (byte*)mail;
  35172. word32 idSz = (word32)XSTRLEN(mail);
  35173. int valid;
  35174. ecc_point* pubKey = rsk;
  35175. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  35176. if (ret != 0)
  35177. return WC_TEST_RET_ENC_EC(ret);
  35178. ret = wc_MakeSakkeKey(priv, rng);
  35179. if (ret != 0)
  35180. return WC_TEST_RET_ENC_EC(ret);
  35181. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  35182. if (ret != LENGTH_ONLY_E)
  35183. return WC_TEST_RET_ENC_EC(ret);
  35184. if (sz != 384)
  35185. return WC_TEST_RET_ENC_NC;
  35186. sz--;
  35187. ret = wc_ExportSakkeKey(priv, data, &sz);
  35188. if (ret == 0)
  35189. return WC_TEST_RET_ENC_NC;
  35190. sz++;
  35191. ret = wc_ExportSakkeKey(priv, data, &sz);
  35192. if (ret != 0)
  35193. return WC_TEST_RET_ENC_EC(ret);
  35194. if (sz != 384)
  35195. return WC_TEST_RET_ENC_NC;
  35196. ret = wc_ImportSakkeKey(key, data, sz - 1);
  35197. if (ret == 0)
  35198. return WC_TEST_RET_ENC_NC;
  35199. ret = wc_ImportSakkeKey(key, data, sz);
  35200. if (ret != 0)
  35201. return WC_TEST_RET_ENC_EC(ret);
  35202. wc_FreeSakkeKey(key);
  35203. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  35204. if (ret != 0)
  35205. return WC_TEST_RET_ENC_EC(ret);
  35206. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  35207. if (ret != LENGTH_ONLY_E)
  35208. return WC_TEST_RET_ENC_EC(ret);
  35209. if (sz != 128)
  35210. return WC_TEST_RET_ENC_NC;
  35211. sz--;
  35212. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  35213. if (ret == 0)
  35214. return WC_TEST_RET_ENC_NC;
  35215. sz++;
  35216. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  35217. if (ret != 0)
  35218. return WC_TEST_RET_ENC_EC(ret);
  35219. if (sz != 128)
  35220. return WC_TEST_RET_ENC_NC;
  35221. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  35222. if (ret == 0)
  35223. return WC_TEST_RET_ENC_NC;
  35224. ret = wc_ImportSakkePrivateKey(key, data, sz);
  35225. if (ret != 0)
  35226. return WC_TEST_RET_ENC_EC(ret);
  35227. ret = wc_MakeSakkePublicKey(key, pubKey);
  35228. if (ret != 0)
  35229. return WC_TEST_RET_ENC_EC(ret);
  35230. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  35231. if (ret != LENGTH_ONLY_E)
  35232. return WC_TEST_RET_ENC_EC(ret);
  35233. if (sz != 256)
  35234. return WC_TEST_RET_ENC_NC;
  35235. sz--;
  35236. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  35237. if (ret == 0)
  35238. return WC_TEST_RET_ENC_NC;
  35239. sz++;
  35240. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  35241. if (ret != 0)
  35242. return WC_TEST_RET_ENC_EC(ret);
  35243. if (sz != 256)
  35244. return WC_TEST_RET_ENC_NC;
  35245. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  35246. if (ret == 0)
  35247. return WC_TEST_RET_ENC_NC;
  35248. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  35249. if (ret != 0)
  35250. return WC_TEST_RET_ENC_EC(ret);
  35251. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  35252. if (ret != 0)
  35253. return WC_TEST_RET_ENC_EC(ret);
  35254. if (sz != 256)
  35255. return WC_TEST_RET_ENC_NC;
  35256. if (XMEMCMP(data, pubData, sz) != 0)
  35257. return WC_TEST_RET_ENC_NC;
  35258. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  35259. if (ret != 0)
  35260. return WC_TEST_RET_ENC_EC(ret);
  35261. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  35262. if (ret != 0)
  35263. return WC_TEST_RET_ENC_EC(ret);
  35264. if (valid != 1)
  35265. return WC_TEST_RET_ENC_NC;
  35266. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  35267. if (ret != 0)
  35268. return WC_TEST_RET_ENC_EC(ret);
  35269. if (valid != 1)
  35270. return WC_TEST_RET_ENC_NC;
  35271. sz = sizeof(data);
  35272. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  35273. if (ret != 0)
  35274. return WC_TEST_RET_ENC_EC(ret);
  35275. if (sz != 256)
  35276. return WC_TEST_RET_ENC_NC;
  35277. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  35278. if (ret != 0)
  35279. return WC_TEST_RET_ENC_EC(ret);
  35280. sz = sizeof(pubData);
  35281. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  35282. if (ret != 0)
  35283. return WC_TEST_RET_ENC_EC(ret);
  35284. if (sz != sizeof(pubData))
  35285. return WC_TEST_RET_ENC_NC;
  35286. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  35287. if (ret != 0)
  35288. return WC_TEST_RET_ENC_EC(ret);
  35289. wc_FreeSakkeKey(key);
  35290. return 0;
  35291. }
  35292. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  35293. ecc_point* rsk)
  35294. {
  35295. wc_test_ret_t ret;
  35296. byte ssv[16];
  35297. word16 ssvSz;
  35298. byte auth[257];
  35299. word16 authSz;
  35300. char mail[] = "test@wolfssl.com";
  35301. byte* id = (byte*)mail;
  35302. word32 idSz = (word32)XSTRLEN(mail);
  35303. byte pointI[256];
  35304. word32 sz;
  35305. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  35306. if (ret != LENGTH_ONLY_E)
  35307. return WC_TEST_RET_ENC_EC(ret);
  35308. if (ssvSz != 16)
  35309. return WC_TEST_RET_ENC_NC;
  35310. ssvSz += 128;
  35311. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  35312. if (ret == 0)
  35313. return WC_TEST_RET_ENC_NC;
  35314. ssvSz -= 128;
  35315. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  35316. if (ret != 0)
  35317. return WC_TEST_RET_ENC_EC(ret);
  35318. if (ssvSz != 16)
  35319. return WC_TEST_RET_ENC_NC;
  35320. ret = wc_GetSakkeAuthSize(pub, &authSz);
  35321. if (ret != 0)
  35322. return WC_TEST_RET_ENC_EC(ret);
  35323. ret = wc_SetSakkeIdentity(pub, id, idSz);
  35324. if (ret != 0)
  35325. return WC_TEST_RET_ENC_EC(ret);
  35326. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35327. NULL, &authSz);
  35328. if (ret != LENGTH_ONLY_E)
  35329. return WC_TEST_RET_ENC_EC(ret);
  35330. if (authSz != 257)
  35331. return WC_TEST_RET_ENC_NC;
  35332. authSz--;
  35333. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35334. auth, &authSz);
  35335. if (ret == 0)
  35336. return WC_TEST_RET_ENC_NC;
  35337. authSz++;
  35338. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35339. auth, &authSz);
  35340. if (ret != 0)
  35341. return WC_TEST_RET_ENC_EC(ret);
  35342. if (authSz != 257)
  35343. return WC_TEST_RET_ENC_NC;
  35344. ret = wc_GetSakkePointI(pub, NULL, &sz);
  35345. if (ret != LENGTH_ONLY_E)
  35346. return WC_TEST_RET_ENC_EC(ret);
  35347. if (sz != 256)
  35348. return WC_TEST_RET_ENC_NC;
  35349. ret = wc_GetSakkePointI(pub, pointI, &sz);
  35350. if (ret != 0)
  35351. return WC_TEST_RET_ENC_EC(ret);
  35352. if (sz != 256)
  35353. return WC_TEST_RET_ENC_NC;
  35354. /* Bogus identity - make it check and regenerate I. */
  35355. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  35356. if (ret != 0)
  35357. return WC_TEST_RET_ENC_EC(ret);
  35358. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35359. auth, &authSz);
  35360. if (ret != 0)
  35361. return WC_TEST_RET_ENC_EC(ret);
  35362. if (authSz != 257)
  35363. return WC_TEST_RET_ENC_NC;
  35364. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  35365. if (ret != 0)
  35366. return WC_TEST_RET_ENC_EC(ret);
  35367. ret = wc_SetSakkeIdentity(priv, id, idSz);
  35368. if (ret != 0)
  35369. return WC_TEST_RET_ENC_EC(ret);
  35370. authSz--;
  35371. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35372. authSz);
  35373. if (ret == 0)
  35374. return WC_TEST_RET_ENC_NC;
  35375. authSz++;
  35376. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35377. authSz);
  35378. if (ret != 0)
  35379. return WC_TEST_RET_ENC_EC(ret);
  35380. ssv[0] ^= 0x80;
  35381. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35382. authSz);
  35383. if (ret != SAKKE_VERIFY_FAIL_E)
  35384. return WC_TEST_RET_ENC_EC(ret);
  35385. ssv[0] ^= 0x80;
  35386. /* Bogus identity - make it check and regenerate I. */
  35387. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  35388. if (ret != 0)
  35389. return WC_TEST_RET_ENC_EC(ret);
  35390. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35391. authSz);
  35392. if (ret != 0)
  35393. return WC_TEST_RET_ENC_EC(ret);
  35394. return 0;
  35395. }
  35396. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  35397. {
  35398. wc_test_ret_t ret = 0;
  35399. WC_RNG rng;
  35400. int rng_inited = 0;
  35401. SakkeKey* priv = NULL;
  35402. SakkeKey* pub = NULL;
  35403. SakkeKey* key = NULL;
  35404. ecc_point* rsk = NULL;
  35405. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  35406. DYNAMIC_TYPE_TMP_BUFFER);
  35407. if (priv == NULL)
  35408. ret = WC_TEST_RET_ENC_NC;
  35409. else
  35410. XMEMSET(priv, 0, sizeof(*priv));
  35411. if (ret == 0) {
  35412. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  35413. DYNAMIC_TYPE_TMP_BUFFER);
  35414. if (pub == NULL)
  35415. ret = WC_TEST_RET_ENC_NC;
  35416. else
  35417. XMEMSET(pub, 0, sizeof(*pub));
  35418. }
  35419. if (ret == 0) {
  35420. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  35421. DYNAMIC_TYPE_TMP_BUFFER);
  35422. if (key == NULL)
  35423. ret = WC_TEST_RET_ENC_NC;
  35424. else
  35425. XMEMSET(key, 0, sizeof(*key));
  35426. }
  35427. if (ret == 0) {
  35428. #ifndef HAVE_FIPS
  35429. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35430. #else
  35431. ret = wc_InitRng(&rng);
  35432. #endif
  35433. if (ret == 0)
  35434. rng_inited = 1;
  35435. else
  35436. ret = WC_TEST_RET_ENC_EC(ret);
  35437. }
  35438. if (ret == 0) {
  35439. rsk = wc_ecc_new_point();
  35440. if (rsk == NULL)
  35441. ret = WC_TEST_RET_ENC_NC;
  35442. }
  35443. if (ret == 0) {
  35444. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  35445. if (ret != 0)
  35446. ret = WC_TEST_RET_ENC_EC(ret);
  35447. }
  35448. if (ret == 0) {
  35449. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  35450. if (ret != 0)
  35451. ret = WC_TEST_RET_ENC_EC(ret);
  35452. }
  35453. if (ret == 0) {
  35454. ret = sakke_api_test(&rng, key, rsk);
  35455. }
  35456. if (ret == 0) {
  35457. ret = sakke_kat_derive_test(pub, rsk);
  35458. }
  35459. if (ret == 0) {
  35460. ret = sakke_kat_encapsulate_test(pub);
  35461. }
  35462. if (ret == 0) {
  35463. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  35464. }
  35465. if (ret == 0) {
  35466. ret = sakke_op_test(priv, pub, &rng, rsk);
  35467. }
  35468. if (rsk != NULL) {
  35469. wc_ecc_forcezero_point(rsk);
  35470. wc_ecc_del_point(rsk);
  35471. }
  35472. if (rng_inited)
  35473. wc_FreeRng(&rng);
  35474. if (key != NULL)
  35475. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35476. if (pub != NULL) {
  35477. wc_FreeSakkeKey(pub);
  35478. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35479. }
  35480. if (priv != NULL) {
  35481. wc_FreeSakkeKey(priv);
  35482. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35483. }
  35484. return ret;
  35485. }
  35486. #endif /* WOLFCRYPT_HAVE_SAKKE */
  35487. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  35488. typedef struct CMAC_Test_Case {
  35489. int type;
  35490. int partial;
  35491. const byte* m;
  35492. word32 mSz;
  35493. const byte* k;
  35494. word32 kSz;
  35495. const byte* t;
  35496. word32 tSz;
  35497. } CMAC_Test_Case;
  35498. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  35499. {
  35500. #ifdef WOLFSSL_AES_128
  35501. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  35502. {
  35503. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  35504. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  35505. };
  35506. #define KLEN_128 (sizeof(k128))
  35507. #endif
  35508. #ifdef WOLFSSL_AES_192
  35509. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  35510. {
  35511. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  35512. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  35513. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  35514. };
  35515. #define KLEN_192 (sizeof(k192))
  35516. #endif
  35517. #ifdef WOLFSSL_AES_256
  35518. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  35519. {
  35520. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  35521. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  35522. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  35523. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  35524. };
  35525. #define KLEN_256 (sizeof(k256))
  35526. #endif
  35527. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  35528. {
  35529. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  35530. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  35531. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  35532. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  35533. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  35534. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  35535. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  35536. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  35537. };
  35538. #define MLEN_0 (0)
  35539. #define MLEN_128 (128/8)
  35540. #define MLEN_320 (320/8)
  35541. #define MLEN_319 (MLEN_320 - 1)
  35542. #define MLEN_512 (512/8)
  35543. #ifdef WOLFSSL_AES_128
  35544. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  35545. {
  35546. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  35547. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  35548. };
  35549. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  35550. {
  35551. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  35552. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  35553. };
  35554. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  35555. {
  35556. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  35557. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  35558. };
  35559. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  35560. {
  35561. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  35562. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  35563. };
  35564. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  35565. {
  35566. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  35567. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  35568. };
  35569. #endif
  35570. #ifdef WOLFSSL_AES_192
  35571. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  35572. {
  35573. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  35574. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  35575. };
  35576. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  35577. {
  35578. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  35579. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  35580. };
  35581. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  35582. {
  35583. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  35584. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  35585. };
  35586. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  35587. {
  35588. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  35589. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  35590. };
  35591. #endif
  35592. #ifdef WOLFSSL_AES_256
  35593. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  35594. {
  35595. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  35596. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  35597. };
  35598. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  35599. {
  35600. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  35601. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  35602. };
  35603. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  35604. {
  35605. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  35606. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  35607. };
  35608. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  35609. {
  35610. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  35611. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  35612. };
  35613. #endif
  35614. const CMAC_Test_Case testCases[] =
  35615. {
  35616. #ifdef WOLFSSL_AES_128
  35617. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  35618. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  35619. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  35620. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  35621. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  35622. #endif
  35623. #ifdef WOLFSSL_AES_192
  35624. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  35625. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  35626. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  35627. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  35628. #endif
  35629. #ifdef WOLFSSL_AES_256
  35630. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  35631. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  35632. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  35633. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  35634. #endif
  35635. #ifdef WOLFSSL_AES_128
  35636. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  35637. #endif
  35638. };
  35639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35640. Cmac *cmac;
  35641. #else
  35642. Cmac cmac[1];
  35643. #endif
  35644. byte tag[AES_BLOCK_SIZE];
  35645. const CMAC_Test_Case* tc;
  35646. word32 i, tagSz;
  35647. wc_test_ret_t ret;
  35648. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35649. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  35650. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35651. #endif
  35652. for (i = 0, tc = testCases;
  35653. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  35654. i++, tc++) {
  35655. XMEMSET(tag, 0, sizeof(tag));
  35656. tagSz = AES_BLOCK_SIZE;
  35657. #if !defined(HAVE_FIPS) || \
  35658. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  35659. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  35660. #else
  35661. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  35662. #endif
  35663. if (ret != 0)
  35664. {
  35665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35666. }
  35667. if (tc->partial) {
  35668. ret = wc_CmacUpdate(cmac, tc->m,
  35669. tc->mSz/2 - tc->partial);
  35670. if (ret != 0)
  35671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35672. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  35673. tc->mSz/2 + tc->partial);
  35674. if (ret != 0)
  35675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35676. }
  35677. else {
  35678. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  35679. if (ret != 0)
  35680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35681. }
  35682. ret = wc_CmacFinal(cmac, tag, &tagSz);
  35683. if (ret != 0)
  35684. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35685. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  35686. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35687. XMEMSET(tag, 0, sizeof(tag));
  35688. tagSz = sizeof(tag);
  35689. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  35690. tc->k, tc->kSz);
  35691. if (ret != 0)
  35692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35693. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  35694. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35695. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  35696. tc->k, tc->kSz);
  35697. if (ret != 0)
  35698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35699. }
  35700. ret = 0;
  35701. out:
  35702. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35703. if (cmac)
  35704. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  35705. #endif
  35706. return ret;
  35707. }
  35708. #endif /* NO_AES && WOLFSSL_CMAC */
  35709. #if defined(WOLFSSL_SIPHASH)
  35710. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  35711. /* Test vectors from:
  35712. * https://github.com/veorq/SipHash/blob/master/vectors.h
  35713. */
  35714. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  35715. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  35716. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  35717. };
  35718. static const unsigned char siphash_msg[64] = {
  35719. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  35720. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  35721. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  35722. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  35723. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  35724. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  35725. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  35726. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  35727. };
  35728. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  35729. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  35730. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  35731. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  35732. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  35733. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  35734. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  35735. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  35736. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  35737. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  35738. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  35739. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  35740. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  35741. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  35742. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  35743. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  35744. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  35745. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  35746. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  35747. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  35748. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  35749. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  35750. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  35751. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  35752. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  35753. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  35754. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  35755. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  35756. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  35757. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  35758. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  35759. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  35760. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  35761. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  35762. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  35763. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  35764. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  35765. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  35766. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  35767. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  35768. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  35769. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  35770. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  35771. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  35772. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  35773. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  35774. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  35775. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  35776. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  35777. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  35778. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  35779. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  35780. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  35781. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  35782. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  35783. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  35784. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  35785. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  35786. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  35787. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  35788. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  35789. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  35790. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  35791. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  35792. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  35793. };
  35794. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  35795. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  35796. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  35797. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  35798. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  35799. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  35800. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  35801. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  35802. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  35803. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  35804. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  35805. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  35806. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  35807. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  35808. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  35809. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  35810. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  35811. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  35812. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  35813. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  35814. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  35815. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  35816. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  35817. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  35818. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  35819. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  35820. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  35821. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  35822. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  35823. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  35824. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  35825. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  35826. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  35827. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  35828. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  35829. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  35830. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  35831. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  35832. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  35833. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  35834. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  35835. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  35836. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  35837. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  35838. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  35839. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  35840. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  35841. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  35842. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  35843. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  35844. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  35845. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  35846. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  35847. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  35848. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  35849. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  35850. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  35851. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  35852. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  35853. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  35854. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  35855. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  35856. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  35857. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  35858. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  35859. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  35860. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  35861. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  35862. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  35863. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  35864. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  35865. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  35866. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  35867. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  35868. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  35869. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  35870. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  35871. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  35872. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  35873. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  35874. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  35875. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  35876. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  35877. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  35878. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  35879. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  35880. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  35881. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  35882. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  35883. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  35884. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  35885. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  35886. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  35887. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  35888. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  35889. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  35890. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  35891. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  35892. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  35893. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  35894. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  35895. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  35896. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  35897. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  35898. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  35899. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  35900. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  35901. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  35902. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  35903. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  35904. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  35905. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  35906. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  35907. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  35908. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  35909. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  35910. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  35911. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  35912. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  35913. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  35914. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  35915. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  35916. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  35917. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  35918. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  35919. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  35920. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  35921. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  35922. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  35923. };
  35924. #endif
  35925. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  35926. {
  35927. wc_test_ret_t ret = 0;
  35928. int i;
  35929. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  35930. unsigned char res[SIPHASH_MAC_SIZE_16];
  35931. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  35932. SipHash siphash;
  35933. for (i = 0; i < 64; i++) {
  35934. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  35935. if (ret != 0)
  35936. return WC_TEST_RET_ENC_I(i);
  35937. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  35938. if (ret != 0)
  35939. return WC_TEST_RET_ENC_I(i);
  35940. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  35941. if (ret != 0)
  35942. return WC_TEST_RET_ENC_I(i);
  35943. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  35944. return WC_TEST_RET_ENC_I(i);
  35945. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  35946. if (ret != 0)
  35947. return WC_TEST_RET_ENC_I(i);
  35948. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  35949. return WC_TEST_RET_ENC_I(i);
  35950. }
  35951. for (i = 0; i < 64; i++) {
  35952. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  35953. if (ret != 0)
  35954. return WC_TEST_RET_ENC_I(i);
  35955. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  35956. if (ret != 0)
  35957. return WC_TEST_RET_ENC_I(i);
  35958. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  35959. if (ret != 0)
  35960. return WC_TEST_RET_ENC_I(i);
  35961. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  35962. return WC_TEST_RET_ENC_I(i);
  35963. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  35964. if (ret != 0)
  35965. return WC_TEST_RET_ENC_I(i);
  35966. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  35967. return WC_TEST_RET_ENC_I(i);
  35968. }
  35969. #endif
  35970. /* Testing bad parameters. */
  35971. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  35972. if (ret != BAD_FUNC_ARG)
  35973. return WC_TEST_RET_ENC_EC(ret);
  35974. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  35975. if (ret != BAD_FUNC_ARG)
  35976. return WC_TEST_RET_ENC_EC(ret);
  35977. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  35978. if (ret != BAD_FUNC_ARG)
  35979. return WC_TEST_RET_ENC_EC(ret);
  35980. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  35981. if (ret != BAD_FUNC_ARG)
  35982. return WC_TEST_RET_ENC_EC(ret);
  35983. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  35984. if (ret != 0)
  35985. return WC_TEST_RET_ENC_EC(ret);
  35986. ret = wc_SipHashUpdate(NULL, NULL, 0);
  35987. if (ret != BAD_FUNC_ARG)
  35988. return WC_TEST_RET_ENC_EC(ret);
  35989. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  35990. if (ret != BAD_FUNC_ARG)
  35991. return WC_TEST_RET_ENC_EC(ret);
  35992. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  35993. if (ret != BAD_FUNC_ARG)
  35994. return WC_TEST_RET_ENC_EC(ret);
  35995. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  35996. if (ret != BAD_FUNC_ARG)
  35997. return WC_TEST_RET_ENC_EC(ret);
  35998. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  35999. if (ret != BAD_FUNC_ARG)
  36000. return WC_TEST_RET_ENC_EC(ret);
  36001. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  36002. if (ret != BAD_FUNC_ARG)
  36003. return WC_TEST_RET_ENC_EC(ret);
  36004. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  36005. if (ret != BAD_FUNC_ARG)
  36006. return WC_TEST_RET_ENC_EC(ret);
  36007. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  36008. if (ret != BAD_FUNC_ARG)
  36009. return WC_TEST_RET_ENC_EC(ret);
  36010. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  36011. if (ret != BAD_FUNC_ARG)
  36012. return WC_TEST_RET_ENC_EC(ret);
  36013. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  36014. if (ret != BAD_FUNC_ARG)
  36015. return WC_TEST_RET_ENC_EC(ret);
  36016. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  36017. if (ret != BAD_FUNC_ARG)
  36018. return WC_TEST_RET_ENC_EC(ret);
  36019. /* Test cache with multiple non blocksize bytes */
  36020. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36021. if (ret != 0)
  36022. return WC_TEST_RET_ENC_EC(ret);
  36023. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  36024. if (ret != 0)
  36025. return WC_TEST_RET_ENC_EC(ret);
  36026. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  36027. if (ret != 0)
  36028. return WC_TEST_RET_ENC_EC(ret);
  36029. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  36030. if (ret != 0)
  36031. return WC_TEST_RET_ENC_EC(ret);
  36032. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36033. if (ret != 0)
  36034. return WC_TEST_RET_ENC_EC(ret);
  36035. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  36036. if (ret != 0)
  36037. return WC_TEST_RET_ENC_EC(ret);
  36038. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  36039. if (ret != 0)
  36040. return WC_TEST_RET_ENC_EC(ret);
  36041. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  36042. return WC_TEST_RET_ENC_NC;
  36043. return 0;
  36044. }
  36045. #endif /* WOLFSSL_SIPHASH */
  36046. #ifdef HAVE_LIBZ
  36047. static const byte sample_text[] =
  36048. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  36049. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  36050. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  36051. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  36052. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  36053. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  36054. "\n"
  36055. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  36056. "four loko you probably haven't heard of them high life. Messenger bag\n"
  36057. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  36058. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  36059. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  36060. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  36061. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  36062. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  36063. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  36064. "four loko whatever street art yr farm-to-table.\n"
  36065. "\n"
  36066. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  36067. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  36068. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  36069. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  36070. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  36071. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  36072. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  36073. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  36074. "locavore.\n"
  36075. "\n"
  36076. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  36077. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  36078. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  36079. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  36080. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  36081. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  36082. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  36083. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  36084. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  36085. "neutra PBR selvage.\n"
  36086. "\n"
  36087. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  36088. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  36089. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  36090. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  36091. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  36092. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  36093. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  36094. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  36095. "mollit magna, sriracha sartorial helvetica.\n"
  36096. "\n"
  36097. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  36098. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  36099. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  36100. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  36101. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  36102. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  36103. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  36104. "\n"
  36105. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  36106. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  36107. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  36108. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  36109. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  36110. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  36111. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  36112. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  36113. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  36114. "\n"
  36115. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  36116. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  36117. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  36118. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  36119. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  36120. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  36121. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  36122. "bag dolor terry richardson sapiente.\n";
  36123. static const byte sample_text_gz[] = {
  36124. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  36125. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  36126. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  36127. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  36128. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  36129. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  36130. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  36131. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  36132. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  36133. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  36134. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  36135. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  36136. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  36137. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  36138. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  36139. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  36140. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  36141. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  36142. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  36143. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  36144. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  36145. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  36146. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  36147. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  36148. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  36149. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  36150. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  36151. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  36152. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  36153. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  36154. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  36155. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  36156. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  36157. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  36158. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  36159. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  36160. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  36161. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  36162. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  36163. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  36164. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  36165. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  36166. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  36167. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  36168. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  36169. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  36170. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  36171. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  36172. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  36173. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  36174. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  36175. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  36176. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  36177. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  36178. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  36179. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  36180. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  36181. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  36182. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  36183. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  36184. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  36185. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  36186. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  36187. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  36188. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  36189. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  36190. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  36191. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  36192. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  36193. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  36194. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  36195. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  36196. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  36197. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  36198. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  36199. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  36200. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  36201. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  36202. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  36203. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  36204. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  36205. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  36206. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  36207. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  36208. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  36209. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  36210. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  36211. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  36212. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  36213. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  36214. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  36215. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  36216. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  36217. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  36218. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  36219. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  36220. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  36221. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  36222. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  36223. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  36224. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  36225. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  36226. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  36227. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  36228. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  36229. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  36230. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  36231. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  36232. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  36233. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  36234. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  36235. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  36236. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  36237. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  36238. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  36239. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  36240. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  36241. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  36242. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  36243. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  36244. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  36245. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  36246. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  36247. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  36248. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  36249. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  36250. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  36251. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  36252. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  36253. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  36254. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  36255. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  36256. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  36257. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  36258. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  36259. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  36260. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  36261. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  36262. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  36263. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  36264. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  36265. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  36266. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  36267. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  36268. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  36269. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  36270. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  36271. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  36272. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  36273. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  36274. };
  36275. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  36276. {
  36277. wc_test_ret_t ret = 0;
  36278. word32 dSz = sizeof(sample_text);
  36279. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  36280. byte *c;
  36281. byte *d;
  36282. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36283. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36284. if (c == NULL || d == NULL) {
  36285. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  36286. }
  36287. /* follow calloc and initialize to 0 */
  36288. XMEMSET(c, 0, cSz);
  36289. XMEMSET(d, 0, dSz);
  36290. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  36291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36292. }
  36293. cSz = (word32)ret;
  36294. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  36295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36296. }
  36297. dSz = (word32)ret;
  36298. if (XMEMCMP(d, sample_text, dSz) != 0) {
  36299. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  36300. }
  36301. /* GZIP tests */
  36302. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  36303. XMEMSET(c, 0, cSz);
  36304. XMEMSET(d, 0, dSz);
  36305. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  36306. if (ret < 0)
  36307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36308. cSz = (word32)ret;
  36309. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  36310. if (ret < 0)
  36311. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36312. if (XMEMCMP(d, sample_text, dSz) != 0) {
  36313. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  36314. }
  36315. /* Try with gzip generated output */
  36316. XMEMSET(d, 0, dSz);
  36317. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  36318. LIBZ_WINBITS_GZIP);
  36319. if (ret < 0)
  36320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36321. dSz = (word32)ret;
  36322. if (XMEMCMP(d, sample_text, dSz) != 0) {
  36323. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  36324. }
  36325. ret = 0; /* success */
  36326. exit:
  36327. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36328. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36329. return ret;
  36330. }
  36331. #endif /* HAVE_LIBZ */
  36332. #ifdef HAVE_PKCS7
  36333. /* External Debugging/Testing Note:
  36334. *
  36335. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  36336. * additional testing. To dump bundles to files DER encoded files, please
  36337. * define:
  36338. *
  36339. * #define PKCS7_OUTPUT_TEST_BUNDLES
  36340. */
  36341. /* Loads certs and keys for use with PKCS7 tests, from either files
  36342. * or buffers.
  36343. *
  36344. * rsaClientCertBuf - output buffer for RSA client cert
  36345. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  36346. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  36347. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  36348. *
  36349. * rsaServerCertBuf - output buffer for RSA server cert
  36350. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  36351. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  36352. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  36353. *
  36354. * rsaCaCertBuf - output buffer for RSA CA cert
  36355. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  36356. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  36357. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  36358. *
  36359. * eccClientCertBuf - output buffer for ECC cert
  36360. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  36361. * eccClientPrivKeyBuf - output buffer for ECC private key
  36362. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  36363. *
  36364. * Returns 0 on success, negative on error
  36365. */
  36366. static wc_test_ret_t pkcs7_load_certs_keys(
  36367. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  36368. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  36369. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  36370. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  36371. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  36372. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  36373. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  36374. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  36375. {
  36376. #ifndef NO_FILESYSTEM
  36377. XFILE certFile;
  36378. XFILE keyFile;
  36379. (void)certFile;
  36380. (void)keyFile;
  36381. #endif
  36382. #ifndef NO_RSA
  36383. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  36384. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  36385. return BAD_FUNC_ARG;
  36386. #endif
  36387. #ifdef HAVE_ECC
  36388. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  36389. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  36390. return BAD_FUNC_ARG;
  36391. #endif
  36392. /* RSA */
  36393. #ifndef NO_RSA
  36394. #ifdef USE_CERT_BUFFERS_1024
  36395. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  36396. return WC_TEST_RET_ENC_NC;
  36397. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  36398. sizeof_client_cert_der_1024);
  36399. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  36400. if (rsaServerCertBuf != NULL) {
  36401. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  36402. return WC_TEST_RET_ENC_NC;
  36403. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  36404. sizeof_server_cert_der_1024);
  36405. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  36406. }
  36407. if (rsaCaCertBuf != NULL) {
  36408. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  36409. return WC_TEST_RET_ENC_NC;
  36410. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  36411. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  36412. }
  36413. #elif defined(USE_CERT_BUFFERS_2048)
  36414. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  36415. return WC_TEST_RET_ENC_NC;
  36416. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  36417. sizeof_client_cert_der_2048);
  36418. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  36419. if (rsaServerCertBuf != NULL) {
  36420. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  36421. return WC_TEST_RET_ENC_NC;
  36422. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  36423. sizeof_server_cert_der_2048);
  36424. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  36425. }
  36426. if (rsaCaCertBuf != NULL) {
  36427. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  36428. return WC_TEST_RET_ENC_NC;
  36429. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  36430. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  36431. }
  36432. #else
  36433. certFile = XFOPEN(clientCert, "rb");
  36434. if (!certFile)
  36435. return WC_TEST_RET_ENC_ERRNO;
  36436. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  36437. *rsaClientCertBufSz, certFile);
  36438. XFCLOSE(certFile);
  36439. if (*rsaClientCertBufSz == 0)
  36440. return WC_TEST_RET_ENC_ERRNO;
  36441. if (rsaServerCertBuf != NULL) {
  36442. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  36443. if (!certFile)
  36444. return WC_TEST_RET_ENC_ERRNO;
  36445. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  36446. *rsaServerCertBufSz, certFile);
  36447. XFCLOSE(certFile);
  36448. if (*rsaServerCertBufSz == 0)
  36449. return WC_TEST_RET_ENC_ERRNO;
  36450. }
  36451. if (rsaCaCertBuf != NULL) {
  36452. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  36453. if (!certFile)
  36454. return WC_TEST_RET_ENC_ERRNO;
  36455. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  36456. certFile);
  36457. XFCLOSE(certFile);
  36458. if (*rsaCaCertBufSz == 0)
  36459. return WC_TEST_RET_ENC_ERRNO;
  36460. }
  36461. #endif
  36462. #ifdef USE_CERT_BUFFERS_1024
  36463. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  36464. return WC_TEST_RET_ENC_NC;
  36465. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  36466. sizeof_client_key_der_1024);
  36467. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  36468. if (rsaServerPrivKeyBuf != NULL) {
  36469. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  36470. return WC_TEST_RET_ENC_NC;
  36471. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  36472. sizeof_server_key_der_1024);
  36473. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  36474. }
  36475. if (rsaCaPrivKeyBuf != NULL) {
  36476. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  36477. return WC_TEST_RET_ENC_NC;
  36478. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  36479. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  36480. }
  36481. #elif defined(USE_CERT_BUFFERS_2048)
  36482. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  36483. return WC_TEST_RET_ENC_NC;
  36484. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  36485. sizeof_client_key_der_2048);
  36486. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  36487. if (rsaServerPrivKeyBuf != NULL) {
  36488. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  36489. return WC_TEST_RET_ENC_NC;
  36490. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  36491. sizeof_server_key_der_2048);
  36492. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  36493. }
  36494. if (rsaCaPrivKeyBuf != NULL) {
  36495. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  36496. return WC_TEST_RET_ENC_NC;
  36497. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  36498. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  36499. }
  36500. #else
  36501. keyFile = XFOPEN(clientKey, "rb");
  36502. if (!keyFile)
  36503. return WC_TEST_RET_ENC_ERRNO;
  36504. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  36505. *rsaClientPrivKeyBufSz, keyFile);
  36506. XFCLOSE(keyFile);
  36507. if (*rsaClientPrivKeyBufSz == 0)
  36508. return WC_TEST_RET_ENC_ERRNO;
  36509. if (rsaServerPrivKeyBuf != NULL) {
  36510. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  36511. if (!keyFile)
  36512. return WC_TEST_RET_ENC_ERRNO;
  36513. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  36514. *rsaServerPrivKeyBufSz, keyFile);
  36515. XFCLOSE(keyFile);
  36516. if (*rsaServerPrivKeyBufSz == 0)
  36517. return WC_TEST_RET_ENC_ERRNO;
  36518. }
  36519. if (rsaCaPrivKeyBuf != NULL) {
  36520. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  36521. if (!keyFile)
  36522. return WC_TEST_RET_ENC_ERRNO;
  36523. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  36524. *rsaCaPrivKeyBufSz, keyFile);
  36525. XFCLOSE(keyFile);
  36526. if (*rsaCaPrivKeyBufSz == 0)
  36527. return WC_TEST_RET_ENC_ERRNO;
  36528. }
  36529. #endif /* USE_CERT_BUFFERS */
  36530. #endif /* NO_RSA */
  36531. /* ECC */
  36532. #ifdef HAVE_ECC
  36533. #ifdef USE_CERT_BUFFERS_256
  36534. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  36535. return WC_TEST_RET_ENC_NC;
  36536. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  36537. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  36538. #else
  36539. certFile = XFOPEN(eccClientCert, "rb");
  36540. if (!certFile)
  36541. return WC_TEST_RET_ENC_ERRNO;
  36542. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  36543. *eccClientCertBufSz, certFile);
  36544. XFCLOSE(certFile);
  36545. if (*eccClientCertBufSz == 0)
  36546. return WC_TEST_RET_ENC_ERRNO;
  36547. #endif /* USE_CERT_BUFFERS_256 */
  36548. #ifdef USE_CERT_BUFFERS_256
  36549. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  36550. return WC_TEST_RET_ENC_NC;
  36551. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  36552. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  36553. #else
  36554. keyFile = XFOPEN(eccClientKey, "rb");
  36555. if (!keyFile)
  36556. return WC_TEST_RET_ENC_ERRNO;
  36557. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  36558. *eccClientPrivKeyBufSz, keyFile);
  36559. XFCLOSE(keyFile);
  36560. if (*eccClientPrivKeyBufSz == 0)
  36561. return WC_TEST_RET_ENC_ERRNO;
  36562. #endif /* USE_CERT_BUFFERS_256 */
  36563. #endif /* HAVE_ECC */
  36564. #ifdef NO_RSA
  36565. (void)rsaClientCertBuf;
  36566. (void)rsaClientCertBufSz;
  36567. (void)rsaClientPrivKeyBuf;
  36568. (void)rsaClientPrivKeyBufSz;
  36569. (void)rsaServerCertBuf;
  36570. (void)rsaServerCertBufSz;
  36571. (void)rsaServerPrivKeyBuf;
  36572. (void)rsaServerPrivKeyBufSz;
  36573. (void)rsaCaCertBuf;
  36574. (void)rsaCaCertBufSz;
  36575. (void)rsaCaPrivKeyBuf;
  36576. (void)rsaCaPrivKeyBufSz;
  36577. #endif
  36578. #ifndef HAVE_ECC
  36579. (void)eccClientCertBuf;
  36580. (void)eccClientCertBufSz;
  36581. (void)eccClientPrivKeyBuf;
  36582. (void)eccClientPrivKeyBufSz;
  36583. #endif
  36584. #ifndef NO_FILESYSTEM
  36585. (void)certFile;
  36586. (void)keyFile;
  36587. #endif
  36588. return 0;
  36589. }
  36590. typedef struct {
  36591. const byte* content;
  36592. word32 contentSz;
  36593. int contentOID;
  36594. int encryptOID;
  36595. int keyWrapOID;
  36596. int keyAgreeOID;
  36597. byte* cert;
  36598. size_t certSz;
  36599. byte* privateKey;
  36600. word32 privateKeySz;
  36601. byte* optionalUkm;
  36602. word32 optionalUkmSz;
  36603. int ktriOptions; /* KTRI options flags */
  36604. int kariOptions; /* KARI options flags */
  36605. /* KEKRI specific */
  36606. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  36607. word32 secretKeySz; /* size of secretKey, bytes */
  36608. const byte* secretKeyId; /* key identifier */
  36609. word32 secretKeyIdSz; /* size of key identifier, bytes */
  36610. void* timePtr; /* time_t pointer */
  36611. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  36612. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  36613. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  36614. word32 otherAttrSz; /* size of otherAttr, bytes */
  36615. int kekriOptions; /* KEKRI options flags */
  36616. /* PWRI specific */
  36617. const char* password;
  36618. word32 passwordSz;
  36619. const byte* salt;
  36620. word32 saltSz;
  36621. int kdfOID;
  36622. int hashOID;
  36623. int kdfIterations;
  36624. int pwriOptions; /* PWRI options flags */
  36625. /* ORI specific */
  36626. int isOri;
  36627. int oriOptions; /* ORI options flags */
  36628. const char* outFileName;
  36629. } pkcs7EnvelopedVector;
  36630. static const byte asnDataOid[] = {
  36631. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  36632. };
  36633. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  36634. * and giving wolfCrypt the value for oriOID and oriValue to place in
  36635. * OtherRecipientInfo.
  36636. *
  36637. * Returns 0 on success, negative upon error. */
  36638. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  36639. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  36640. void* ctx)
  36641. {
  36642. int i;
  36643. /* make sure buffers are large enough */
  36644. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  36645. return WC_TEST_RET_ENC_NC;
  36646. /* our simple encryption algorithm will be take the bitwise complement */
  36647. oriValue[0] = 0x04; /*ASN OCTET STRING */
  36648. oriValue[1] = (byte)cekSz; /* length */
  36649. for (i = 0; i < (int)cekSz; i++) {
  36650. oriValue[2 + i] = ~cek[i];
  36651. }
  36652. *oriValueSz = 2 + cekSz;
  36653. /* set oriType to ASN.1 encoded data OID */
  36654. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  36655. *oriTypeSz = sizeof(asnDataOid);
  36656. (void)pkcs7;
  36657. (void)ctx;
  36658. return 0;
  36659. }
  36660. /* ORI decrypt callback, responsible for providing a decrypted content
  36661. * encryption key (CEK) placed into decryptedKey and size placed into
  36662. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  36663. * in decrypting the encrypted CEK.
  36664. *
  36665. * Returns 0 on success, negative upon error. */
  36666. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  36667. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  36668. word32* decryptedKeySz, void* ctx)
  36669. {
  36670. int i;
  36671. /* make sure oriType matches what we expect */
  36672. if (oriTypeSz != sizeof(asnDataOid))
  36673. return WC_TEST_RET_ENC_NC;
  36674. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  36675. return WC_TEST_RET_ENC_NC;
  36676. /* make sure decrypted buffer is large enough */
  36677. if (*decryptedKeySz < oriValueSz)
  36678. return WC_TEST_RET_ENC_NC;
  36679. /* decrypt encrypted CEK using simple bitwise complement,
  36680. only for example */
  36681. for (i = 0; i < (int)oriValueSz - 2; i++) {
  36682. decryptedKey[i] = ~oriValue[2 + i];
  36683. }
  36684. *decryptedKeySz = oriValueSz - 2;
  36685. (void)pkcs7;
  36686. (void)ctx;
  36687. return 0;
  36688. }
  36689. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36690. /* returns 0 on success */
  36691. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  36692. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  36693. byte* in, int inSz, byte* out, void* usrCtx)
  36694. {
  36695. wc_test_ret_t ret;
  36696. int keyId = -1, keySz;
  36697. word32 keyIdSz = 8;
  36698. const byte* key;
  36699. byte keyIdRaw[8];
  36700. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36701. Aes *aes;
  36702. #else
  36703. Aes aes[1];
  36704. #endif
  36705. /* looking for KEY ID
  36706. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  36707. */
  36708. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  36709. /* 0x06, 0x0B do not pass in tag and length */
  36710. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36711. 0x01, 0x09, 0x10, 0x02, 0x25
  36712. };
  36713. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  36714. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36715. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36716. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36717. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36718. };
  36719. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  36720. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36721. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36722. };
  36723. /* test user context passed in */
  36724. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  36725. return WC_TEST_RET_ENC_NC;
  36726. }
  36727. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36728. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  36729. return WC_TEST_RET_ENC_ERRNO;
  36730. #endif
  36731. /* if needing to find keyIdSz can call with NULL */
  36732. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  36733. &keyIdSz);
  36734. if (ret != LENGTH_ONLY_E) {
  36735. printf("Unexpected error %d when getting keyIdSz\n", ret);
  36736. printf("Possibly no KEY ID attribute set\n");
  36737. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36738. }
  36739. else {
  36740. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  36741. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  36742. &keyIdSz);
  36743. if (ret < 0) {
  36744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36745. }
  36746. if (keyIdSz < 3) {
  36747. printf("keyIdSz is smaller than expected\n");
  36748. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36749. }
  36750. if (keyIdSz > 2 + sizeof(int)) {
  36751. printf("example case was only expecting a keyId of int size\n");
  36752. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36753. }
  36754. /* keyIdRaw[0] OCTET TAG */
  36755. /* keyIdRaw[1] Length */
  36756. #ifdef BIG_ENDIAN_ORDER
  36757. if (keyIdRaw[1] == 0x01) {
  36758. keyId = 1;
  36759. }
  36760. #else
  36761. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  36762. #endif
  36763. }
  36764. /* Use keyID here if found to select key and decrypt in HSM or in this
  36765. * example just select key and do software decryption */
  36766. if (keyId == 1) {
  36767. key = altKey;
  36768. keySz = sizeof(altKey);
  36769. }
  36770. else {
  36771. key = defKey;
  36772. keySz = sizeof(defKey);
  36773. }
  36774. switch (encryptOID) {
  36775. #ifdef WOLFSSL_AES_256
  36776. case AES256CBCb:
  36777. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  36778. WARNING_OUT(BAD_FUNC_ARG, out);
  36779. break;
  36780. #endif
  36781. #ifdef WOLFSSL_AES_128
  36782. case AES128CBCb:
  36783. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  36784. ERROR_OUT(BAD_FUNC_ARG, out);
  36785. break;
  36786. #endif
  36787. default:
  36788. printf("Unsupported content cipher type for example");
  36789. ERROR_OUT(ALGO_ID_E, out);
  36790. };
  36791. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  36792. if (ret == 0) {
  36793. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  36794. if (ret == 0)
  36795. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  36796. wc_AesFree(aes);
  36797. }
  36798. out:
  36799. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36800. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36801. #endif
  36802. (void)aad;
  36803. (void)aadSz;
  36804. (void)authTag;
  36805. (void)authTagSz;
  36806. return (int)ret;
  36807. }
  36808. #endif /* !NO_AES && HAVE_AES_CBC */
  36809. #define PKCS7_BUF_SIZE 2048
  36810. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  36811. byte* rsaPrivKey, word32 rsaPrivKeySz,
  36812. byte* eccCert, word32 eccCertSz,
  36813. byte* eccPrivKey, word32 eccPrivKeySz)
  36814. {
  36815. wc_test_ret_t ret = 0;
  36816. int testSz = 0, i;
  36817. int envelopedSz, decodedSz;
  36818. byte *enveloped = NULL;
  36819. byte *decoded = NULL;
  36820. PKCS7* pkcs7 = NULL;
  36821. #ifdef ECC_TIMING_RESISTANT
  36822. WC_RNG rng;
  36823. #endif
  36824. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36825. XFILE pkcs7File;
  36826. #endif
  36827. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36828. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36829. 0x72,0x6c,0x64
  36830. };
  36831. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  36832. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  36833. byte optionalUkm[] = {
  36834. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  36835. };
  36836. #endif /* NO_AES */
  36837. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  36838. !defined(NO_SHA)
  36839. /* encryption key for kekri recipient types */
  36840. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  36841. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  36842. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  36843. };
  36844. /* encryption key identifier */
  36845. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  36846. 0x02,0x02,0x03,0x04
  36847. };
  36848. #endif
  36849. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  36850. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  36851. #ifndef HAVE_FIPS
  36852. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  36853. #else
  36854. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  36855. #endif
  36856. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  36857. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  36858. };
  36859. #endif
  36860. #define MAX_TESTVECTORS_LEN 13
  36861. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  36862. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  36863. if (testSz == MAX_TESTVECTORS_LEN) { \
  36864. ret = WC_TEST_RET_ENC_NC; \
  36865. goto out; \
  36866. } \
  36867. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  36868. }
  36869. pkcs7EnvelopedVector *testVectors = NULL;
  36870. #ifdef ECC_TIMING_RESISTANT
  36871. XMEMSET(&rng, 0, sizeof(rng));
  36872. #endif
  36873. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36874. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36875. if (testVectors == NULL) {
  36876. ret = WC_TEST_RET_ENC_ERRNO;
  36877. goto out;
  36878. }
  36879. {
  36880. /* key transport key encryption technique */
  36881. #ifndef NO_RSA
  36882. #ifndef NO_DES3
  36883. ADD_PKCS7ENVELOPEDVECTOR(
  36884. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  36885. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  36886. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36887. "pkcs7envelopedDataDES3.der");
  36888. #endif
  36889. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36890. #ifdef WOLFSSL_AES_128
  36891. ADD_PKCS7ENVELOPEDVECTOR(
  36892. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  36893. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  36894. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36895. "pkcs7envelopedDataAES128CBC.der");
  36896. #endif
  36897. #ifdef WOLFSSL_AES_192
  36898. ADD_PKCS7ENVELOPEDVECTOR(
  36899. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  36900. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  36901. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36902. "pkcs7envelopedDataAES192CBC.der");
  36903. #endif
  36904. #ifdef WOLFSSL_AES_256
  36905. ADD_PKCS7ENVELOPEDVECTOR(
  36906. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  36907. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  36908. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36909. "pkcs7envelopedDataAES256CBC.der");
  36910. /* explicitly using SKID for SubjectKeyIdentifier */
  36911. ADD_PKCS7ENVELOPEDVECTOR(
  36912. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  36913. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  36914. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36915. "pkcs7envelopedDataAES256CBC_SKID.der");
  36916. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  36917. ADD_PKCS7ENVELOPEDVECTOR(
  36918. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  36919. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  36920. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  36921. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  36922. #endif
  36923. #endif /* !NO_AES && HAVE_AES_CBC */
  36924. #endif
  36925. /* key agreement key encryption technique*/
  36926. #ifdef HAVE_ECC
  36927. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36928. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  36929. ADD_PKCS7ENVELOPEDVECTOR(
  36930. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  36931. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  36932. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  36933. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36934. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  36935. #endif
  36936. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  36937. ADD_PKCS7ENVELOPEDVECTOR(
  36938. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  36939. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  36940. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  36941. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36942. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  36943. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  36944. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  36945. ADD_PKCS7ENVELOPEDVECTOR(
  36946. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  36947. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  36948. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  36949. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36950. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  36951. /* with optional user keying material (ukm) */
  36952. ADD_PKCS7ENVELOPEDVECTOR(
  36953. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  36954. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  36955. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  36956. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36957. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  36958. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  36959. #endif /* !NO_AES && HAVE_AES_CBC */
  36960. #endif
  36961. /* kekri (KEKRecipientInfo) recipient types */
  36962. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36963. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  36964. ADD_PKCS7ENVELOPEDVECTOR(
  36965. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  36966. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  36967. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  36968. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  36969. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  36970. #endif
  36971. #endif /* !NO_AES && HAVE_AES_CBC */
  36972. /* pwri (PasswordRecipientInfo) recipient types */
  36973. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  36974. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  36975. ADD_PKCS7ENVELOPEDVECTOR(
  36976. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  36977. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  36978. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  36979. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  36980. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  36981. #endif
  36982. #endif
  36983. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  36984. /* ori (OtherRecipientInfo) recipient types */
  36985. ADD_PKCS7ENVELOPEDVECTOR(
  36986. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  36987. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  36988. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  36989. #endif
  36990. };
  36991. #undef MAX_TESTVECTORS_LEN
  36992. #undef ADD_PKCS7ENVELOPEDVECTOR
  36993. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36994. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36995. if ((! enveloped) || (! decoded)) {
  36996. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36997. }
  36998. #ifdef ECC_TIMING_RESISTANT
  36999. #ifndef HAVE_FIPS
  37000. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37001. #else
  37002. ret = wc_InitRng(&rng);
  37003. #endif
  37004. if (ret != 0)
  37005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37006. #endif
  37007. for (i = 0; i < testSz; i++) {
  37008. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  37009. #ifdef WOLFSSL_ASYNC_CRYPT
  37010. INVALID_DEVID /* async PKCS7 is not supported */
  37011. #else
  37012. devId
  37013. #endif
  37014. );
  37015. if (pkcs7 == NULL) {
  37016. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37017. }
  37018. if (testVectors[i].secretKey != NULL) {
  37019. /* KEKRI recipient type */
  37020. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37021. if (ret != 0)
  37022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37023. pkcs7->content = (byte*)testVectors[i].content;
  37024. pkcs7->contentSz = testVectors[i].contentSz;
  37025. pkcs7->contentOID = testVectors[i].contentOID;
  37026. pkcs7->encryptOID = testVectors[i].encryptOID;
  37027. pkcs7->ukm = testVectors[i].optionalUkm;
  37028. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37029. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  37030. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  37031. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  37032. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  37033. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  37034. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  37035. if (ret < 0) {
  37036. wc_PKCS7_Free(pkcs7);
  37037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37038. }
  37039. /* set key, for decryption */
  37040. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  37041. testVectors[i].secretKeySz);
  37042. if (ret != 0) {
  37043. wc_PKCS7_Free(pkcs7);
  37044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37045. }
  37046. } else if (testVectors[i].password != NULL) {
  37047. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  37048. /* PWRI recipient type */
  37049. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37050. if (ret != 0)
  37051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37052. pkcs7->content = (byte*)testVectors[i].content;
  37053. pkcs7->contentSz = testVectors[i].contentSz;
  37054. pkcs7->contentOID = testVectors[i].contentOID;
  37055. pkcs7->encryptOID = testVectors[i].encryptOID;
  37056. pkcs7->ukm = testVectors[i].optionalUkm;
  37057. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37058. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  37059. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  37060. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  37061. testVectors[i].kdfOID,
  37062. testVectors[i].hashOID, testVectors[i].kdfIterations,
  37063. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  37064. if (ret < 0) {
  37065. wc_PKCS7_Free(pkcs7);
  37066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37067. }
  37068. /* set password, for decryption */
  37069. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  37070. testVectors[i].passwordSz);
  37071. if (ret < 0) {
  37072. wc_PKCS7_Free(pkcs7);
  37073. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37074. }
  37075. #endif /* ! NO_PWDBASED && ! NO_SHA */
  37076. } else if (testVectors[i].isOri == 1) {
  37077. /* ORI recipient type */
  37078. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37079. if (ret != 0)
  37080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37081. pkcs7->content = (byte*)testVectors[i].content;
  37082. pkcs7->contentSz = testVectors[i].contentSz;
  37083. pkcs7->contentOID = testVectors[i].contentOID;
  37084. pkcs7->encryptOID = testVectors[i].encryptOID;
  37085. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  37086. testVectors[i].oriOptions);
  37087. if (ret < 0) {
  37088. wc_PKCS7_Free(pkcs7);
  37089. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37090. }
  37091. /* set decrypt callback for decryption */
  37092. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  37093. if (ret < 0) {
  37094. wc_PKCS7_Free(pkcs7);
  37095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37096. }
  37097. } else {
  37098. /* KTRI or KARI recipient types */
  37099. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37100. if (ret != 0)
  37101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37102. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  37103. (word32)testVectors[i].certSz);
  37104. if (ret != 0) {
  37105. wc_PKCS7_Free(pkcs7);
  37106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37107. }
  37108. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  37109. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  37110. pkcs7->privateKey = testVectors[i].privateKey;
  37111. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  37112. pkcs7->content = (byte*)testVectors[i].content;
  37113. pkcs7->contentSz = testVectors[i].contentSz;
  37114. pkcs7->contentOID = testVectors[i].contentOID;
  37115. pkcs7->encryptOID = testVectors[i].encryptOID;
  37116. pkcs7->ukm = testVectors[i].optionalUkm;
  37117. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37118. /* set SubjectIdentifier type for KTRI types */
  37119. if (testVectors[i].ktriOptions & CMS_SKID) {
  37120. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  37121. if (ret != 0) {
  37122. wc_PKCS7_Free(pkcs7);
  37123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37124. }
  37125. } else if (testVectors[i].ktriOptions &
  37126. CMS_ISSUER_AND_SERIAL_NUMBER) {
  37127. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  37128. CMS_ISSUER_AND_SERIAL_NUMBER);
  37129. if (ret != 0) {
  37130. wc_PKCS7_Free(pkcs7);
  37131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37132. }
  37133. }
  37134. }
  37135. #ifdef ECC_TIMING_RESISTANT
  37136. pkcs7->rng = &rng;
  37137. #endif
  37138. /* encode envelopedData */
  37139. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  37140. PKCS7_BUF_SIZE);
  37141. if (envelopedSz <= 0) {
  37142. wc_PKCS7_Free(pkcs7);
  37143. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  37144. }
  37145. /* decode envelopedData */
  37146. pkcs7->contentOID = 0;
  37147. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  37148. decoded, PKCS7_BUF_SIZE);
  37149. if (pkcs7->contentOID != testVectors[i].contentOID ||
  37150. decodedSz <= 0) {
  37151. wc_PKCS7_Free(pkcs7);
  37152. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37153. }
  37154. /* test decode result */
  37155. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  37156. wc_PKCS7_Free(pkcs7);
  37157. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37158. }
  37159. #ifndef NO_PKCS7_STREAM
  37160. { /* test reading byte by byte */
  37161. int z;
  37162. for (z = 0; z < envelopedSz; z++) {
  37163. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  37164. decoded, PKCS7_BUF_SIZE);
  37165. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  37166. printf("unexpected error %d\n", decodedSz);
  37167. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37168. }
  37169. }
  37170. /* test decode result */
  37171. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  37172. printf("stream read compare failed\n");
  37173. wc_PKCS7_Free(pkcs7);
  37174. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37175. }
  37176. }
  37177. #endif
  37178. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  37179. /* output pkcs7 envelopedData for external testing */
  37180. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  37181. if (!pkcs7File) {
  37182. wc_PKCS7_Free(pkcs7);
  37183. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37184. }
  37185. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  37186. XFCLOSE(pkcs7File);
  37187. if (ret != envelopedSz) {
  37188. wc_PKCS7_Free(pkcs7);
  37189. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37190. } else {
  37191. /* reset ret to 0 for success */
  37192. ret = 0;
  37193. }
  37194. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  37195. wc_PKCS7_Free(pkcs7);
  37196. pkcs7 = NULL;
  37197. }
  37198. #ifdef ECC_TIMING_RESISTANT
  37199. wc_FreeRng(&rng);
  37200. #endif
  37201. (void)eccCert;
  37202. (void)eccCertSz;
  37203. (void)eccPrivKey;
  37204. (void)eccPrivKeySz;
  37205. (void)rsaCert;
  37206. (void)rsaCertSz;
  37207. (void)rsaPrivKey;
  37208. (void)rsaPrivKeySz;
  37209. out:
  37210. if (testVectors)
  37211. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37212. if (enveloped)
  37213. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37214. if (decoded)
  37215. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37216. return ret;
  37217. }
  37218. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  37219. {
  37220. wc_test_ret_t ret = 0;
  37221. byte* rsaCert = NULL;
  37222. byte* rsaPrivKey = NULL;
  37223. word32 rsaCertSz = 0;
  37224. word32 rsaPrivKeySz = 0;
  37225. byte* eccCert = NULL;
  37226. byte* eccPrivKey = NULL;
  37227. word32 eccCertSz = 0;
  37228. word32 eccPrivKeySz = 0;
  37229. #ifndef NO_RSA
  37230. /* read client RSA cert and key in DER format */
  37231. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37232. if (rsaCert == NULL)
  37233. return WC_TEST_RET_ENC_ERRNO;
  37234. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37235. if (rsaPrivKey == NULL) {
  37236. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37237. return WC_TEST_RET_ENC_NC;
  37238. }
  37239. rsaCertSz = FOURK_BUF;
  37240. rsaPrivKeySz = FOURK_BUF;
  37241. #endif /* NO_RSA */
  37242. #ifdef HAVE_ECC
  37243. /* read client ECC cert and key in DER format */
  37244. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37245. if (eccCert == NULL) {
  37246. #ifndef NO_RSA
  37247. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37248. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37249. #endif
  37250. return WC_TEST_RET_ENC_NC;
  37251. }
  37252. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37253. if (eccPrivKey == NULL) {
  37254. #ifndef NO_RSA
  37255. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37256. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37257. #endif
  37258. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37259. return WC_TEST_RET_ENC_NC;
  37260. }
  37261. eccCertSz = FOURK_BUF;
  37262. eccPrivKeySz = FOURK_BUF;
  37263. #endif /* HAVE_ECC */
  37264. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  37265. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  37266. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  37267. eccPrivKey, &eccPrivKeySz);
  37268. if (ret < 0) {
  37269. #ifndef NO_RSA
  37270. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37271. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37272. #endif
  37273. #ifdef HAVE_ECC
  37274. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37275. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37276. #endif
  37277. return WC_TEST_RET_ENC_EC(ret);
  37278. }
  37279. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  37280. rsaPrivKey, (word32)rsaPrivKeySz,
  37281. eccCert, (word32)eccCertSz,
  37282. eccPrivKey, (word32)eccPrivKeySz);
  37283. #ifndef NO_RSA
  37284. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37285. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37286. #endif
  37287. #ifdef HAVE_ECC
  37288. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37289. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37290. #endif
  37291. return ret;
  37292. }
  37293. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  37294. typedef struct {
  37295. const byte* content;
  37296. word32 contentSz;
  37297. int contentOID;
  37298. int encryptOID;
  37299. int keyWrapOID;
  37300. int keyAgreeOID;
  37301. byte* cert;
  37302. size_t certSz;
  37303. byte* privateKey;
  37304. word32 privateKeySz;
  37305. PKCS7Attrib* authAttribs;
  37306. word32 authAttribsSz;
  37307. PKCS7Attrib* unauthAttribs;
  37308. word32 unauthAttribsSz;
  37309. /* KARI / KTRI specific */
  37310. byte* optionalUkm;
  37311. word32 optionalUkmSz;
  37312. int ktriOptions; /* KTRI options flags */
  37313. int kariOptions; /* KARI options flags */
  37314. /* KEKRI specific */
  37315. byte* secretKey; /* key, only for kekri RecipientInfo types */
  37316. word32 secretKeySz; /* size of secretKey, bytes */
  37317. byte* secretKeyId; /* key identifier */
  37318. word32 secretKeyIdSz; /* size of key identifier, bytes */
  37319. void* timePtr; /* time_t pointer */
  37320. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  37321. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  37322. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  37323. word32 otherAttrSz; /* size of otherAttr, bytes */
  37324. int kekriOptions; /* KEKRI options flags */
  37325. /* PWRI specific */
  37326. char* password; /* password */
  37327. word32 passwordSz; /* password size, bytes */
  37328. byte* salt; /* KDF salt */
  37329. word32 saltSz; /* KDF salt size, bytes */
  37330. int kdfOID; /* KDF OID */
  37331. int hashOID; /* KDF hash algorithm OID */
  37332. int kdfIterations; /* KDF iterations */
  37333. int kekEncryptOID; /* KEK encryption algorithm OID */
  37334. int pwriOptions; /* PWRI options flags */
  37335. /* ORI specific */
  37336. int isOri;
  37337. int oriOptions; /* ORI options flags */
  37338. const char* outFileName;
  37339. } pkcs7AuthEnvelopedVector;
  37340. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  37341. byte* rsaPrivKey, word32 rsaPrivKeySz,
  37342. byte* eccCert, word32 eccCertSz,
  37343. byte* eccPrivKey, word32 eccPrivKeySz)
  37344. {
  37345. wc_test_ret_t ret = 0;
  37346. int testSz = 0, i;
  37347. int envelopedSz, decodedSz;
  37348. byte *enveloped = NULL;
  37349. byte *decoded = NULL;
  37350. WC_RNG rng;
  37351. PKCS7* pkcs7;
  37352. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  37353. XFILE pkcs7File;
  37354. #endif
  37355. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  37356. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  37357. 0x72,0x6c,0x64
  37358. };
  37359. byte senderNonce[PKCS7_NONCE_SZ + 2];
  37360. #ifdef HAVE_ECC
  37361. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37362. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  37363. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  37364. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  37365. 0x09, 0x05 };
  37366. PKCS7Attrib attribs[] =
  37367. {
  37368. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  37369. sizeof(senderNonce) }
  37370. };
  37371. #endif
  37372. #endif
  37373. #endif
  37374. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  37375. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  37376. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  37377. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  37378. };
  37379. #endif /* NO_AES */
  37380. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37381. /* encryption key for kekri recipient types */
  37382. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  37383. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37384. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  37385. };
  37386. /* encryption key identifier */
  37387. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  37388. 0x02,0x02,0x03,0x04
  37389. };
  37390. #endif
  37391. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  37392. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  37393. #ifndef HAVE_FIPS
  37394. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  37395. #else
  37396. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  37397. #endif
  37398. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  37399. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  37400. };
  37401. #endif
  37402. #define MAX_TESTVECTORS_LEN 20
  37403. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  37404. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  37405. if (testSz == MAX_TESTVECTORS_LEN) { \
  37406. ret = WC_TEST_RET_ENC_NC; \
  37407. goto out; \
  37408. } \
  37409. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  37410. sizeof _this_vector); \
  37411. }
  37412. pkcs7AuthEnvelopedVector *testVectors = NULL;
  37413. XMEMSET(&rng, 0, sizeof(rng));
  37414. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  37415. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37416. if (testVectors == NULL) {
  37417. ret = WC_TEST_RET_ENC_ERRNO;
  37418. goto out;
  37419. }
  37420. {
  37421. /* key transport key encryption technique */
  37422. #ifndef NO_RSA
  37423. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37424. #ifdef WOLFSSL_AES_128
  37425. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37426. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  37427. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37428. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  37429. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  37430. #endif
  37431. #ifdef WOLFSSL_AES_192
  37432. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37433. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  37434. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37435. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  37436. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  37437. #endif
  37438. #ifdef WOLFSSL_AES_256
  37439. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37440. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  37441. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37442. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  37443. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  37444. /* test with contentType set to FirmwarePkgData */
  37445. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37446. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  37447. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  37448. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  37449. 0, 0, 0, 0, 0, 0, 0, 0,
  37450. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  37451. /* explicitly using SKID for SubjectKeyIdentifier */
  37452. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37453. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  37454. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  37455. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  37456. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  37457. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  37458. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37459. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  37460. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  37461. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  37462. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  37463. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  37464. #endif
  37465. #else /* NO_AES || !HAVE_AESGCM */
  37466. (void)rsaCert;
  37467. (void)rsaCertSz;
  37468. (void)rsaPrivKey;
  37469. (void)rsaPrivKeySz;
  37470. #endif /* NO_AES || !HAVE_AESGCM */
  37471. #endif
  37472. /* key agreement key encryption technique*/
  37473. #ifdef HAVE_ECC
  37474. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37475. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37476. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37477. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  37478. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37479. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  37480. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  37481. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  37482. #endif
  37483. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  37484. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37485. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37486. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37487. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  37488. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  37489. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  37490. /* with authenticated attributes */
  37491. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37492. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37493. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37494. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  37495. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37496. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  37497. 0, 0, 0,
  37498. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  37499. /* with unauthenticated attributes */
  37500. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37501. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37502. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37503. eccPrivKeySz, NULL, 0, attribs,
  37504. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  37505. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  37506. 0, 0, 0,
  37507. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  37508. /* with authenticated AND unauthenticated attributes */
  37509. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37510. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37511. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37512. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  37513. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  37514. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  37515. 0, 0, 0, 0, 0, 0,
  37516. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  37517. /* with authenticated AND unauthenticated attributes AND
  37518. * contentType of FirmwarePkgData */
  37519. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37520. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  37521. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37522. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  37523. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  37524. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  37525. 0, 0, 0, 0, 0, 0,
  37526. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  37527. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  37528. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  37529. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37530. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37531. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37532. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  37533. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  37534. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  37535. /* with optional user keying material (ukm) */
  37536. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37537. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  37538. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37539. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  37540. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  37541. 0, 0, 0, 0, 0, 0,
  37542. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  37543. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  37544. #endif /* NO_AES */
  37545. #endif
  37546. /* kekri (KEKRecipientInfo) recipient types */
  37547. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37548. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37549. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37550. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  37551. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  37552. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  37553. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  37554. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  37555. #endif
  37556. #endif
  37557. /* pwri (PasswordRecipientInfo) recipient types */
  37558. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  37559. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  37560. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37561. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  37562. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37563. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  37564. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  37565. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  37566. #endif
  37567. #endif
  37568. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37569. #ifdef WOLFSSL_AES_128
  37570. /* ori (OtherRecipientInfo) recipient types */
  37571. ADD_PKCS7AUTHENVELOPEDVECTOR(
  37572. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  37573. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  37574. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  37575. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  37576. #endif
  37577. #endif
  37578. }
  37579. #undef MAX_TESTVECTORS_LEN
  37580. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  37581. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37582. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37583. if ((! enveloped) || (! decoded)) {
  37584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37585. }
  37586. /* generate senderNonce */
  37587. {
  37588. #ifndef HAVE_FIPS
  37589. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37590. #else
  37591. ret = wc_InitRng(&rng);
  37592. #endif
  37593. if (ret != 0)
  37594. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37595. senderNonce[0] = 0x04;
  37596. senderNonce[1] = PKCS7_NONCE_SZ;
  37597. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  37598. if (ret != 0) {
  37599. wc_FreeRng(&rng);
  37600. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37601. }
  37602. }
  37603. for (i = 0; i < testSz; i++) {
  37604. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  37605. #ifdef WOLFSSL_ASYNC_CRYPT
  37606. INVALID_DEVID /* async PKCS7 is not supported */
  37607. #else
  37608. devId
  37609. #endif
  37610. );
  37611. if (pkcs7 == NULL) {
  37612. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37613. }
  37614. if (testVectors[i].secretKey != NULL) {
  37615. /* KEKRI recipient type */
  37616. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37617. if (ret != 0)
  37618. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37619. pkcs7->content = (byte*)testVectors[i].content;
  37620. pkcs7->contentSz = testVectors[i].contentSz;
  37621. pkcs7->contentOID = testVectors[i].contentOID;
  37622. pkcs7->encryptOID = testVectors[i].encryptOID;
  37623. pkcs7->ukm = testVectors[i].optionalUkm;
  37624. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37625. pkcs7->authAttribs = testVectors[i].authAttribs;
  37626. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  37627. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  37628. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  37629. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  37630. testVectors[i].secretKey, testVectors[i].secretKeySz,
  37631. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  37632. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  37633. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  37634. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  37635. if (ret < 0) {
  37636. wc_PKCS7_Free(pkcs7);
  37637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37638. }
  37639. /* set key, for decryption */
  37640. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  37641. testVectors[i].secretKeySz);
  37642. if (ret != 0) {
  37643. wc_PKCS7_Free(pkcs7);
  37644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37645. }
  37646. } else if (testVectors[i].password != NULL) {
  37647. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  37648. /* PWRI recipient type */
  37649. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37650. if (ret != 0)
  37651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37652. pkcs7->content = (byte*)testVectors[i].content;
  37653. pkcs7->contentSz = testVectors[i].contentSz;
  37654. pkcs7->contentOID = testVectors[i].contentOID;
  37655. pkcs7->encryptOID = testVectors[i].encryptOID;
  37656. pkcs7->ukm = testVectors[i].optionalUkm;
  37657. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37658. pkcs7->authAttribs = testVectors[i].authAttribs;
  37659. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  37660. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  37661. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  37662. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  37663. (byte*)testVectors[i].password,
  37664. testVectors[i].passwordSz, testVectors[i].salt,
  37665. testVectors[i].saltSz, testVectors[i].kdfOID,
  37666. testVectors[i].hashOID, testVectors[i].kdfIterations,
  37667. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  37668. if (ret < 0) {
  37669. wc_PKCS7_Free(pkcs7);
  37670. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37671. }
  37672. /* set password, for decryption */
  37673. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  37674. testVectors[i].passwordSz);
  37675. if (ret < 0) {
  37676. wc_PKCS7_Free(pkcs7);
  37677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37678. }
  37679. #endif /* ! NO_PWDBASED && ! NO_SHA */
  37680. } else if (testVectors[i].isOri == 1) {
  37681. /* ORI recipient type */
  37682. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37683. if (ret != 0)
  37684. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37685. pkcs7->content = (byte*)testVectors[i].content;
  37686. pkcs7->contentSz = testVectors[i].contentSz;
  37687. pkcs7->contentOID = testVectors[i].contentOID;
  37688. pkcs7->encryptOID = testVectors[i].encryptOID;
  37689. pkcs7->authAttribs = testVectors[i].authAttribs;
  37690. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  37691. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  37692. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  37693. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  37694. testVectors[i].oriOptions);
  37695. if (ret < 0) {
  37696. wc_PKCS7_Free(pkcs7);
  37697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37698. }
  37699. /* set decrypt callback for decryption */
  37700. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  37701. if (ret < 0) {
  37702. wc_PKCS7_Free(pkcs7);
  37703. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37704. }
  37705. } else {
  37706. /* KTRI or KARI recipient types */
  37707. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  37708. (word32)testVectors[i].certSz);
  37709. if (ret != 0) {
  37710. wc_PKCS7_Free(pkcs7);
  37711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37712. }
  37713. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  37714. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  37715. pkcs7->privateKey = testVectors[i].privateKey;
  37716. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  37717. pkcs7->content = (byte*)testVectors[i].content;
  37718. pkcs7->contentSz = testVectors[i].contentSz;
  37719. pkcs7->contentOID = testVectors[i].contentOID;
  37720. pkcs7->encryptOID = testVectors[i].encryptOID;
  37721. pkcs7->ukm = testVectors[i].optionalUkm;
  37722. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37723. pkcs7->authAttribs = testVectors[i].authAttribs;
  37724. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  37725. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  37726. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  37727. /* set SubjectIdentifier type for KTRI types */
  37728. if (testVectors[i].ktriOptions & CMS_SKID) {
  37729. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  37730. if (ret != 0) {
  37731. wc_PKCS7_Free(pkcs7);
  37732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37733. }
  37734. } else if (testVectors[i].ktriOptions &
  37735. CMS_ISSUER_AND_SERIAL_NUMBER) {
  37736. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  37737. CMS_ISSUER_AND_SERIAL_NUMBER);
  37738. if (ret != 0) {
  37739. wc_PKCS7_Free(pkcs7);
  37740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37741. }
  37742. }
  37743. }
  37744. #ifdef ECC_TIMING_RESISTANT
  37745. pkcs7->rng = &rng;
  37746. #endif
  37747. /* encode envelopedData */
  37748. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  37749. PKCS7_BUF_SIZE);
  37750. if (envelopedSz <= 0) {
  37751. wc_PKCS7_Free(pkcs7);
  37752. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  37753. }
  37754. #ifndef NO_PKCS7_STREAM
  37755. { /* test reading byte by byte */
  37756. int z;
  37757. for (z = 0; z < envelopedSz; z++) {
  37758. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  37759. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  37760. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  37761. printf("unexpected error %d\n", decodedSz);
  37762. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37763. }
  37764. }
  37765. /* test decode result */
  37766. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  37767. printf("stream read compare failed\n");
  37768. wc_PKCS7_Free(pkcs7);
  37769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37770. }
  37771. }
  37772. #endif
  37773. /* decode envelopedData */
  37774. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  37775. envelopedSz, decoded,
  37776. PKCS7_BUF_SIZE);
  37777. if (decodedSz <= 0) {
  37778. wc_PKCS7_Free(pkcs7);
  37779. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37780. }
  37781. /* test decode result */
  37782. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  37783. wc_PKCS7_Free(pkcs7);
  37784. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37785. }
  37786. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  37787. /* output pkcs7 envelopedData for external testing */
  37788. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  37789. if (!pkcs7File) {
  37790. wc_PKCS7_Free(pkcs7);
  37791. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37792. }
  37793. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  37794. XFCLOSE(pkcs7File);
  37795. if (ret != envelopedSz) {
  37796. wc_PKCS7_Free(pkcs7);
  37797. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37798. } else {
  37799. /* reset ret to 0 for success */
  37800. ret = 0;
  37801. }
  37802. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  37803. wc_PKCS7_Free(pkcs7);
  37804. pkcs7 = NULL;
  37805. }
  37806. wc_FreeRng(&rng);
  37807. (void)eccCert;
  37808. (void)eccCertSz;
  37809. (void)eccPrivKey;
  37810. (void)eccPrivKeySz;
  37811. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37812. (void)secretKey;
  37813. (void)secretKeyId;
  37814. #endif
  37815. #ifdef NO_RSA
  37816. (void)rsaCert;
  37817. (void)rsaCertSz;
  37818. (void)rsaPrivKey;
  37819. (void)rsaPrivKeySz;
  37820. #endif
  37821. out:
  37822. if (testVectors)
  37823. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37824. if (enveloped)
  37825. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37826. if (decoded)
  37827. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37828. return ret;
  37829. }
  37830. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  37831. {
  37832. wc_test_ret_t ret = 0;
  37833. byte* rsaCert = NULL;
  37834. byte* rsaPrivKey = NULL;
  37835. word32 rsaCertSz = 0;
  37836. word32 rsaPrivKeySz = 0;
  37837. byte* eccCert = NULL;
  37838. byte* eccPrivKey = NULL;
  37839. word32 eccCertSz = 0;
  37840. word32 eccPrivKeySz = 0;
  37841. #ifndef NO_RSA
  37842. /* read client RSA cert and key in DER format */
  37843. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37844. if (rsaCert == NULL)
  37845. return WC_TEST_RET_ENC_ERRNO;
  37846. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37847. if (rsaPrivKey == NULL) {
  37848. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37849. return WC_TEST_RET_ENC_NC;
  37850. }
  37851. rsaCertSz = FOURK_BUF;
  37852. rsaPrivKeySz = FOURK_BUF;
  37853. #endif /* NO_RSA */
  37854. #ifdef HAVE_ECC
  37855. /* read client ECC cert and key in DER format */
  37856. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37857. if (eccCert == NULL) {
  37858. #ifndef NO_RSA
  37859. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37860. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37861. #endif
  37862. return WC_TEST_RET_ENC_NC;
  37863. }
  37864. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37865. if (eccPrivKey == NULL) {
  37866. #ifndef NO_RSA
  37867. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37868. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37869. #endif
  37870. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37871. return WC_TEST_RET_ENC_NC;
  37872. }
  37873. eccCertSz = FOURK_BUF;
  37874. eccPrivKeySz = FOURK_BUF;
  37875. #endif /* HAVE_ECC */
  37876. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  37877. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  37878. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  37879. eccPrivKey, &eccPrivKeySz);
  37880. if (ret < 0) {
  37881. #ifndef NO_RSA
  37882. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37883. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37884. #endif
  37885. #ifdef HAVE_ECC
  37886. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37887. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37888. #endif
  37889. return WC_TEST_RET_ENC_EC(ret);
  37890. }
  37891. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  37892. rsaPrivKey, (word32)rsaPrivKeySz,
  37893. eccCert, (word32)eccCertSz,
  37894. eccPrivKey, (word32)eccPrivKeySz);
  37895. #ifndef NO_RSA
  37896. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37897. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37898. #endif
  37899. #ifdef HAVE_ECC
  37900. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37901. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37902. #endif
  37903. return ret;
  37904. }
  37905. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  37906. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  37907. static const byte p7DefKey[] = {
  37908. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37909. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37910. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37911. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  37912. };
  37913. static const byte p7AltKey[] = {
  37914. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37915. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  37916. };
  37917. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  37918. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  37919. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  37920. {
  37921. wc_test_ret_t ret;
  37922. if (cek == NULL || out == NULL)
  37923. return BAD_FUNC_ARG;
  37924. /* test case sanity checks */
  37925. if (keyIdSz != 1) {
  37926. return WC_TEST_RET_ENC_NC;
  37927. }
  37928. if (keyId[0] != 0x00) {
  37929. return WC_TEST_RET_ENC_NC;
  37930. }
  37931. if (type != (int)PKCS7_KEKRI) {
  37932. return WC_TEST_RET_ENC_NC;
  37933. }
  37934. switch (keyWrapAlgo) {
  37935. case AES256_WRAP:
  37936. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  37937. out, outSz, NULL);
  37938. if (ret <= 0)
  37939. return (int)ret;
  37940. break;
  37941. default:
  37942. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  37943. return BAD_KEYWRAP_ALG_E;
  37944. };
  37945. (void)pkcs7;
  37946. (void)direction;
  37947. (void)orginKey; /* used with KAKRI */
  37948. (void)orginKeySz;
  37949. return (int)ret;
  37950. }
  37951. /* returns key size on success */
  37952. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  37953. {
  37954. wc_test_ret_t ret;
  37955. word32 atrSz;
  37956. byte atr[256];
  37957. /* Additionally can look for fwWrappedFirmwareKey
  37958. * 1.2.840.113529.1.9.16.1.16 */
  37959. const unsigned char fwWrappedFirmwareKey[] = {
  37960. /* 0x06, 0x0B */
  37961. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  37962. 0x01, 0x09, 0x10, 0x02, 0x27
  37963. };
  37964. /* find keyID in fwWrappedFirmwareKey */
  37965. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  37966. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  37967. if (ret == LENGTH_ONLY_E) {
  37968. XMEMSET(atr, 0, sizeof(atr));
  37969. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  37970. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  37971. /* keyIdRaw[0] OCTET TAG */
  37972. /* keyIdRaw[1] Length */
  37973. if (ret > 0) {
  37974. PKCS7* envPkcs7;
  37975. envPkcs7 = wc_PKCS7_New(NULL, 0);
  37976. if (envPkcs7 == NULL) {
  37977. return MEMORY_E;
  37978. }
  37979. wc_PKCS7_Init(envPkcs7, NULL, 0);
  37980. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  37981. if (ret == 0) {
  37982. /* expecting FIRMWARE_PKG_DATA content */
  37983. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  37984. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  37985. key, keySz);
  37986. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  37987. /* the contentOID should have been set to the inner
  37988. * FIRMWARE_PKG_DATA content */
  37989. ret = BAD_STATE_E;
  37990. }
  37991. }
  37992. wc_PKCS7_Free(envPkcs7);
  37993. }
  37994. }
  37995. return ret;
  37996. }
  37997. /* create a KEKRI enveloped data
  37998. * return size on success */
  37999. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  38000. word32 outSz)
  38001. {
  38002. wc_test_ret_t ret;
  38003. PKCS7* pkcs7;
  38004. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  38005. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  38006. if (pkcs7 == NULL)
  38007. return WC_TEST_RET_ENC_ERRNO;
  38008. pkcs7->content = in;
  38009. pkcs7->contentSz = inSz;
  38010. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  38011. pkcs7->encryptOID = AES256CBCb;
  38012. pkcs7->ukm = NULL;
  38013. pkcs7->ukmSz = 0;
  38014. /* add recipient (KEKRI type) */
  38015. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  38016. sizeof(p7DefKey), (byte*)keyId,
  38017. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  38018. if (ret < 0) {
  38019. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  38020. wc_PKCS7_Free(pkcs7);
  38021. return WC_TEST_RET_ENC_EC(ret);
  38022. }
  38023. /* encode envelopedData, returns size */
  38024. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  38025. if (ret <= 0) {
  38026. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  38027. wc_PKCS7_Free(pkcs7);
  38028. return WC_TEST_RET_ENC_EC(ret);
  38029. }
  38030. wc_PKCS7_Free(pkcs7);
  38031. return ret;
  38032. }
  38033. /*
  38034. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  38035. * returns size of buffer output on success
  38036. */
  38037. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  38038. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  38039. byte* key, word32 keySz)
  38040. {
  38041. wc_test_ret_t ret;
  38042. int attribNum = 1;
  38043. PKCS7* pkcs7;
  38044. /* KEY ID
  38045. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  38046. */
  38047. const unsigned char fwDecryptKeyID[] = {
  38048. 0x06, 0x0B,
  38049. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38050. 0x01, 0x09, 0x10, 0x02, 0x25
  38051. };
  38052. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  38053. const unsigned char fwWrappedFirmwareKey[] = {
  38054. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38055. 0x01, 0x09, 0x10, 0x02, 0x27
  38056. };
  38057. byte keyID[] = { 0x04, 0x01, 0x00 };
  38058. byte env[256];
  38059. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  38060. PKCS7Attrib attribs[] =
  38061. {
  38062. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  38063. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  38064. };
  38065. keyID[2] = keyHint;
  38066. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  38067. if (keyHint == 0) {
  38068. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  38069. sizeof(env));
  38070. if (ret <= 0) {
  38071. return ret;
  38072. }
  38073. attribs[1].valueSz = (int)ret;
  38074. attribNum++;
  38075. }
  38076. /* init PKCS7 */
  38077. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  38078. if (pkcs7 == NULL)
  38079. return WC_TEST_RET_ENC_ERRNO;
  38080. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  38081. if (ret != 0) {
  38082. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  38083. wc_PKCS7_Free(pkcs7);
  38084. return WC_TEST_RET_ENC_EC(ret);
  38085. }
  38086. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  38087. if (ret != 0) {
  38088. wc_PKCS7_Free(pkcs7);
  38089. return WC_TEST_RET_ENC_EC(ret);
  38090. }
  38091. /* encode Signed Encrypted FirmwarePkgData */
  38092. if (encryptKeySz == 16) {
  38093. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  38094. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  38095. (byte*)data, sizeof(data), NULL, 0,
  38096. attribs, attribNum, out, *outSz);
  38097. }
  38098. else {
  38099. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  38100. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  38101. (byte*)data, sizeof(data), NULL, 0,
  38102. attribs, attribNum, out, *outSz);
  38103. }
  38104. if (ret <= 0) {
  38105. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  38106. "ret = %d\n", ret);
  38107. wc_PKCS7_Free(pkcs7);
  38108. return WC_TEST_RET_ENC_EC(ret);
  38109. } else {
  38110. *outSz = (int)ret;
  38111. }
  38112. wc_PKCS7_Free(pkcs7);
  38113. return ret;
  38114. }
  38115. /* test verification and decryption of PKCS7 bundle
  38116. * return 0 on success
  38117. */
  38118. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  38119. {
  38120. wc_test_ret_t ret = 0;
  38121. int usrCtx = 1; /* test value to pass as user context to callback */
  38122. PKCS7* pkcs7 = NULL;
  38123. byte* sid = NULL;
  38124. word32 sidSz;
  38125. byte key[256];
  38126. word32 keySz = sizeof(key);
  38127. byte *decoded = NULL;
  38128. int decodedSz = FOURK_BUF/2;
  38129. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  38130. #ifdef NO_SHA
  38131. #ifdef USE_CERT_BUFFERS_1024
  38132. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  38133. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  38134. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  38135. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  38136. #else
  38137. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  38138. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  38139. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  38140. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  38141. #endif
  38142. #else /* !NO_SHA */
  38143. #ifdef USE_CERT_BUFFERS_1024
  38144. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  38145. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  38146. 0x10, 0x69, 0x59, 0xec,
  38147. #else
  38148. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  38149. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  38150. 0xD7, 0x85, 0x65, 0xC0
  38151. #endif
  38152. #endif /* !NO_SHA */
  38153. };
  38154. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38155. if (decoded == NULL) {
  38156. ret = MEMORY_E;
  38157. goto out;
  38158. }
  38159. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  38160. if (pkcs7 == NULL) {
  38161. ret = MEMORY_E;
  38162. goto out;
  38163. }
  38164. /* Test verify */
  38165. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  38166. if (ret != 0)
  38167. goto out;
  38168. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  38169. if (ret != 0)
  38170. goto out;
  38171. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  38172. if (ret != 0)
  38173. goto out;
  38174. /* Get size of SID and print it out */
  38175. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  38176. if (ret != LENGTH_ONLY_E)
  38177. goto out;
  38178. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38179. if (sid == NULL) {
  38180. ret = MEMORY_E;
  38181. goto out;
  38182. }
  38183. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  38184. if (ret != 0)
  38185. goto out;
  38186. ret = XMEMCMP(sid, expectedSid, sidSz);
  38187. if (ret != 0) {
  38188. ret = PKCS7_NO_SIGNER_E; /* close enough */
  38189. goto out;
  38190. }
  38191. /* get expected fwWrappedFirmwareKey */
  38192. if (keyHint == 0) {
  38193. ret = getFirmwareKey(pkcs7, key, keySz);
  38194. if (ret < 0)
  38195. goto out;
  38196. pkcs7->encryptionKey = key;
  38197. pkcs7->encryptionKeySz = (int)ret;
  38198. }
  38199. else {
  38200. decodedSz = PKCS7_BUF_SIZE;
  38201. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  38202. if (ret != 0)
  38203. goto out;
  38204. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  38205. if (ret != 0)
  38206. goto out;
  38207. }
  38208. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  38209. pkcs7->contentSz, decoded, decodedSz);
  38210. if (decodedSz < 0) {
  38211. ret = decodedSz;
  38212. goto out;
  38213. }
  38214. ret = 0;
  38215. out:
  38216. if (decoded)
  38217. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38218. if (pkcs7)
  38219. wc_PKCS7_Free(pkcs7);
  38220. if (sid)
  38221. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38222. return ret;
  38223. }
  38224. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  38225. {
  38226. wc_test_ret_t ret = 0;
  38227. word32 derSz;
  38228. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38229. if (! derBuf)
  38230. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38231. /* Doing default generation and verify */
  38232. derSz = FOURK_BUF;
  38233. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  38234. certSz, key, keySz);
  38235. if (ret <= 0) {
  38236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38237. }
  38238. ret = verifyBundle(derBuf, derSz, 0);
  38239. if (ret != 0)
  38240. ERROR_OUT(ret, out);
  38241. /* test choosing other key with keyID */
  38242. derSz = FOURK_BUF;
  38243. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  38244. cert, certSz, key, keySz);
  38245. if (ret <= 0) {
  38246. ERROR_OUT(ret, out);
  38247. }
  38248. ret = verifyBundle(derBuf, derSz, 1);
  38249. if (ret != 0)
  38250. ERROR_OUT(ret, out);
  38251. /* test fail case with wrong keyID */
  38252. derSz = FOURK_BUF;
  38253. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  38254. cert, certSz, key, keySz);
  38255. if (ret <= 0) {
  38256. ERROR_OUT(ret, out);
  38257. }
  38258. ret = verifyBundle(derBuf, derSz, 1);
  38259. if (ret == 0) {
  38260. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38261. }
  38262. ret = 0;
  38263. out:
  38264. if (derBuf)
  38265. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38266. return ret;
  38267. }
  38268. #endif /* !NO_AES && HAVE_AES_CBC */
  38269. #ifndef NO_PKCS7_ENCRYPTED_DATA
  38270. typedef struct {
  38271. const byte* content;
  38272. word32 contentSz;
  38273. int contentOID;
  38274. int encryptOID;
  38275. byte* encryptionKey;
  38276. word32 encryptionKeySz;
  38277. PKCS7Attrib* attribs;
  38278. word32 attribsSz;
  38279. const char* outFileName;
  38280. } pkcs7EncryptedVector;
  38281. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  38282. {
  38283. wc_test_ret_t ret = 0;
  38284. int i, testSz;
  38285. int encryptedSz, decodedSz, attribIdx;
  38286. PKCS7* pkcs7;
  38287. byte *encrypted;
  38288. byte *decoded;
  38289. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38290. XFILE pkcs7File;
  38291. #endif
  38292. PKCS7Attrib* expectedAttrib;
  38293. PKCS7DecodedAttrib* decodedAttrib;
  38294. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  38295. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38296. 0x72,0x6c,0x64
  38297. };
  38298. #ifndef NO_DES3
  38299. byte desKey[] = {
  38300. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  38301. };
  38302. byte des3Key[] = {
  38303. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  38304. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  38305. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  38306. };
  38307. #endif
  38308. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  38309. #ifdef WOLFSSL_AES_128
  38310. byte aes128Key[] = {
  38311. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38312. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  38313. };
  38314. #endif
  38315. #ifdef WOLFSSL_AES_192
  38316. byte aes192Key[] = {
  38317. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38318. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38319. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  38320. };
  38321. #endif
  38322. #ifdef WOLFSSL_AES_256
  38323. byte aes256Key[] = {
  38324. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38325. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38326. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38327. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  38328. };
  38329. #endif
  38330. #ifdef WOLFSSL_AES_256
  38331. /* Attribute example from RFC 4134, Section 7.2
  38332. * OID = 1.2.5555
  38333. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  38334. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  38335. static byte genAttr[] = { 0x04, 47,
  38336. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  38337. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  38338. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  38339. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  38340. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  38341. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  38342. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  38343. static byte genAttr2[] = { 0x04, 47,
  38344. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  38345. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  38346. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  38347. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  38348. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  38349. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  38350. PKCS7Attrib attribs[] =
  38351. {
  38352. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  38353. };
  38354. PKCS7Attrib multiAttribs[] =
  38355. {
  38356. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  38357. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  38358. };
  38359. #endif
  38360. #endif /* NO_AES */
  38361. const pkcs7EncryptedVector testVectors[] =
  38362. {
  38363. #ifndef NO_DES3
  38364. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  38365. NULL, 0, "pkcs7encryptedDataDES3.der"},
  38366. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  38367. NULL, 0, "pkcs7encryptedDataDES.der"},
  38368. #endif /* NO_DES3 */
  38369. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  38370. #ifdef WOLFSSL_AES_128
  38371. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  38372. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  38373. #endif
  38374. #ifdef WOLFSSL_AES_192
  38375. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  38376. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  38377. #endif
  38378. #ifdef WOLFSSL_AES_256
  38379. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  38380. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  38381. /* test with optional unprotected attributes */
  38382. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  38383. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38384. "pkcs7encryptedDataAES256CBC_attribs.der"},
  38385. /* test with multiple optional unprotected attributes */
  38386. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  38387. sizeof(aes256Key), multiAttribs,
  38388. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  38389. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  38390. /* test with contentType set to FirmwarePkgData */
  38391. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  38392. sizeof(aes256Key), NULL, 0,
  38393. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  38394. #endif
  38395. #endif /* !NO_AES && HAVE_AES_CBC */
  38396. };
  38397. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38398. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38399. if ((! encrypted) || (! decoded)) {
  38400. ERROR_OUT(MEMORY_E, out);
  38401. }
  38402. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  38403. for (i = 0; i < testSz; i++) {
  38404. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  38405. if (pkcs7 == NULL) {
  38406. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38407. }
  38408. pkcs7->content = (byte*)testVectors[i].content;
  38409. pkcs7->contentSz = testVectors[i].contentSz;
  38410. pkcs7->contentOID = testVectors[i].contentOID;
  38411. pkcs7->encryptOID = testVectors[i].encryptOID;
  38412. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  38413. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  38414. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  38415. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  38416. /* encode encryptedData */
  38417. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  38418. PKCS7_BUF_SIZE);
  38419. if (encryptedSz <= 0) {
  38420. wc_PKCS7_Free(pkcs7);
  38421. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  38422. }
  38423. /* decode encryptedData */
  38424. #ifndef NO_PKCS7_STREAM
  38425. { /* test reading byte by byte */
  38426. int z;
  38427. for (z = 0; z < encryptedSz; z++) {
  38428. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  38429. decoded, PKCS7_BUF_SIZE);
  38430. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  38431. printf("unexpected error %d\n", decodedSz);
  38432. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  38433. }
  38434. }
  38435. /* test decode result */
  38436. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  38437. printf("stream read failed\n");
  38438. wc_PKCS7_Free(pkcs7);
  38439. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38440. }
  38441. }
  38442. #endif
  38443. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  38444. decoded, PKCS7_BUF_SIZE);
  38445. if (decodedSz <= 0){
  38446. wc_PKCS7_Free(pkcs7);
  38447. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  38448. }
  38449. /* test decode result */
  38450. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  38451. wc_PKCS7_Free(pkcs7);
  38452. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38453. }
  38454. /* verify decoded unprotected attributes */
  38455. if (pkcs7->decodedAttrib != NULL) {
  38456. decodedAttrib = pkcs7->decodedAttrib;
  38457. attribIdx = 1;
  38458. while (decodedAttrib != NULL) {
  38459. /* expected attribute, stored list is reversed */
  38460. expectedAttrib = &(pkcs7->unprotectedAttribs
  38461. [pkcs7->unprotectedAttribsSz - attribIdx]);
  38462. /* verify oid */
  38463. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  38464. decodedAttrib->oidSz) != 0) {
  38465. wc_PKCS7_Free(pkcs7);
  38466. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38467. }
  38468. /* verify value */
  38469. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  38470. decodedAttrib->valueSz) != 0) {
  38471. wc_PKCS7_Free(pkcs7);
  38472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38473. }
  38474. decodedAttrib = decodedAttrib->next;
  38475. attribIdx++;
  38476. }
  38477. }
  38478. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38479. /* output pkcs7 envelopedData for external testing */
  38480. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  38481. if (!pkcs7File) {
  38482. wc_PKCS7_Free(pkcs7);
  38483. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38484. }
  38485. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  38486. if (ret < 0)
  38487. ret = WC_TEST_RET_ENC_ERRNO;
  38488. else
  38489. ret = 0;
  38490. XFCLOSE(pkcs7File);
  38491. #endif
  38492. wc_PKCS7_Free(pkcs7);
  38493. }
  38494. out:
  38495. if (encrypted)
  38496. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38497. if (decoded)
  38498. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38499. return ret;
  38500. }
  38501. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  38502. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  38503. typedef struct {
  38504. const byte* content;
  38505. word32 contentSz;
  38506. int contentOID;
  38507. const char* outFileName;
  38508. } pkcs7CompressedVector;
  38509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  38510. {
  38511. wc_test_ret_t ret = 0;
  38512. int i, testSz;
  38513. int compressedSz, decodedSz;
  38514. PKCS7* pkcs7;
  38515. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38516. byte *compressed;
  38517. byte *decoded;
  38518. #else
  38519. byte compressed[PKCS7_BUF_SIZE];
  38520. byte decoded[PKCS7_BUF_SIZE];
  38521. #endif
  38522. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38523. XFILE pkcs7File;
  38524. #endif
  38525. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  38526. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38527. 0x72,0x6c,0x64
  38528. };
  38529. const pkcs7CompressedVector testVectors[] =
  38530. {
  38531. {data, (word32)sizeof(data), DATA,
  38532. "pkcs7compressedData_data_zlib.der"},
  38533. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  38534. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  38535. };
  38536. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38537. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38538. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38539. if ((! compressed) || (! decoded)) {
  38540. ERROR_OUT(MEMORY_E, out);
  38541. }
  38542. #endif
  38543. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  38544. for (i = 0; i < testSz; i++) {
  38545. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  38546. if (pkcs7 == NULL) {
  38547. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38548. }
  38549. pkcs7->content = (byte*)testVectors[i].content;
  38550. pkcs7->contentSz = testVectors[i].contentSz;
  38551. pkcs7->contentOID = testVectors[i].contentOID;
  38552. /* encode compressedData */
  38553. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  38554. PKCS7_BUF_SIZE);
  38555. if (compressedSz <= 0) {
  38556. wc_PKCS7_Free(pkcs7);
  38557. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  38558. }
  38559. /* decode compressedData */
  38560. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  38561. compressedSz, decoded,
  38562. PKCS7_BUF_SIZE);
  38563. if (decodedSz <= 0){
  38564. wc_PKCS7_Free(pkcs7);
  38565. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  38566. }
  38567. /* test decode result */
  38568. if (XMEMCMP(decoded, testVectors[i].content,
  38569. testVectors[i].contentSz) != 0) {
  38570. wc_PKCS7_Free(pkcs7);
  38571. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38572. }
  38573. /* make sure content type is the same */
  38574. if (testVectors[i].contentOID != pkcs7->contentOID) {
  38575. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38576. }
  38577. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38578. /* output pkcs7 compressedData for external testing */
  38579. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  38580. if (!pkcs7File) {
  38581. wc_PKCS7_Free(pkcs7);
  38582. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38583. }
  38584. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  38585. if (ret < 0)
  38586. ret = WC_TEST_RET_ENC_ERRNO;
  38587. else
  38588. ret = 0;
  38589. XFCLOSE(pkcs7File);
  38590. #endif
  38591. wc_PKCS7_Free(pkcs7);
  38592. }
  38593. out:
  38594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38595. if (compressed)
  38596. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38597. if (decoded)
  38598. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38599. #endif
  38600. return ret;
  38601. } /* pkcs7compressed_test() */
  38602. #undef PKCS7_BUF_SIZE
  38603. #endif /* HAVE_LIBZ */
  38604. typedef struct {
  38605. const byte* content;
  38606. word32 contentSz;
  38607. int hashOID;
  38608. int signOID;
  38609. byte* privateKey;
  38610. word32 privateKeySz;
  38611. byte* cert;
  38612. size_t certSz;
  38613. byte* caCert;
  38614. size_t caCertSz;
  38615. PKCS7Attrib* signedAttribs;
  38616. word32 signedAttribsSz;
  38617. const char* outFileName;
  38618. int contentOID;
  38619. byte* contentType;
  38620. word32 contentTypeSz;
  38621. int sidType;
  38622. int encryptOID; /* for single-shot encrypt alg OID */
  38623. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  38624. byte* encryptKey; /* for single-shot, encryptedData */
  38625. word32 encryptKeySz; /* for single-shot, encryptedData */
  38626. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  38627. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  38628. word16 detachedSignature; /* generate detached signature (0:1) */
  38629. } pkcs7SignedVector;
  38630. static wc_test_ret_t pkcs7signed_run_vectors(
  38631. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  38632. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  38633. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  38634. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  38635. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  38636. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  38637. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  38638. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  38639. {
  38640. wc_test_ret_t ret;
  38641. int testSz = 0, i;
  38642. int encodedSz;
  38643. byte* out = NULL;
  38644. word32 outSz;
  38645. WC_RNG rng;
  38646. PKCS7* pkcs7 = NULL;
  38647. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38648. XFILE file;
  38649. #endif
  38650. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  38651. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38652. 0x72,0x6c,0x64
  38653. };
  38654. static byte transIdOid[] =
  38655. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  38656. 0x09, 0x07 };
  38657. static byte messageTypeOid[] =
  38658. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  38659. 0x09, 0x02 };
  38660. static byte senderNonceOid[] =
  38661. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  38662. 0x09, 0x05 };
  38663. #ifndef NO_SHA
  38664. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  38665. #else
  38666. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  38667. #endif
  38668. static byte messageType[] = { 0x13, 2, '1', '9' };
  38669. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  38670. static PKCS7Attrib attribs[] =
  38671. {
  38672. { transIdOid, sizeof(transIdOid), transId,
  38673. sizeof(transId) - 1 }, /* take off the null */
  38674. { messageTypeOid, sizeof(messageTypeOid), messageType,
  38675. sizeof(messageType) },
  38676. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  38677. sizeof(senderNonce) }
  38678. };
  38679. /* for testing custom contentType, FirmwarePkgData */
  38680. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  38681. 0x48, 0x86, 0xF7, 0x0D,
  38682. 0x01, 0x09, 0x10, 0x01, 0x10 };
  38683. #define MAX_TESTVECTORS_LEN 20
  38684. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  38685. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  38686. if (testSz == MAX_TESTVECTORS_LEN) { \
  38687. ret = WC_TEST_RET_ENC_NC; \
  38688. goto out; \
  38689. } \
  38690. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  38691. sizeof _this_vector); \
  38692. }
  38693. pkcs7SignedVector *testVectors = NULL;
  38694. XMEMSET(&rng, 0, sizeof(rng));
  38695. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  38696. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38697. if (testVectors == NULL) {
  38698. ret = WC_TEST_RET_ENC_ERRNO;
  38699. goto out;
  38700. }
  38701. {
  38702. #ifndef NO_RSA
  38703. #ifndef NO_SHA
  38704. /* RSA with SHA */
  38705. ADD_PKCS7SIGNEDVECTOR(
  38706. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  38707. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38708. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38709. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  38710. 0, 0);
  38711. /* RSA with SHA, no signed attributes */
  38712. ADD_PKCS7SIGNEDVECTOR(
  38713. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  38714. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  38715. NULL, 0, NULL, 0,
  38716. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38717. NULL, 0, 0);
  38718. #endif
  38719. #ifdef WOLFSSL_SHA224
  38720. /* RSA with SHA224 */
  38721. ADD_PKCS7SIGNEDVECTOR(
  38722. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  38723. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38724. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38725. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38726. NULL, 0, 0);
  38727. #endif
  38728. #ifndef NO_SHA256
  38729. /* RSA with SHA256 */
  38730. ADD_PKCS7SIGNEDVECTOR(
  38731. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  38732. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38733. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38734. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38735. NULL, 0, 0);
  38736. /* RSA with SHA256, detached signature */
  38737. ADD_PKCS7SIGNEDVECTOR(
  38738. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  38739. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38740. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38741. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  38742. NULL, 0, NULL, 0, 1);
  38743. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  38744. ADD_PKCS7SIGNEDVECTOR(
  38745. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  38746. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38747. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38748. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  38749. NULL, 0, NULL, 0, 0);
  38750. /* RSA with SHA256 and custom contentType */
  38751. ADD_PKCS7SIGNEDVECTOR(
  38752. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  38753. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38754. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38755. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  38756. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  38757. NULL, 0, 0);
  38758. /* RSA with SHA256 and FirmwarePkgData contentType */
  38759. ADD_PKCS7SIGNEDVECTOR(
  38760. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  38761. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38762. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38763. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  38764. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  38765. /* RSA with SHA256 using server cert and ca cert */
  38766. ADD_PKCS7SIGNEDVECTOR(
  38767. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  38768. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  38769. rsaCaCertBuf, rsaCaCertBufSz,
  38770. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38771. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  38772. NULL, 0, NULL, 0, 0);
  38773. #endif
  38774. #if defined(WOLFSSL_SHA384)
  38775. /* RSA with SHA384 */
  38776. ADD_PKCS7SIGNEDVECTOR(
  38777. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  38778. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38779. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38780. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38781. NULL, 0, 0);
  38782. #endif
  38783. #if defined(WOLFSSL_SHA512)
  38784. /* RSA with SHA512 */
  38785. ADD_PKCS7SIGNEDVECTOR(
  38786. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  38787. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  38788. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38789. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38790. NULL, 0, 0);
  38791. #endif
  38792. #endif /* NO_RSA */
  38793. #ifdef HAVE_ECC
  38794. #ifndef NO_SHA
  38795. /* ECDSA with SHA */
  38796. ADD_PKCS7SIGNEDVECTOR(
  38797. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  38798. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38799. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38800. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38801. NULL, 0, 0);
  38802. /* ECDSA with SHA, no signed attributes */
  38803. ADD_PKCS7SIGNEDVECTOR(
  38804. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  38805. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  38806. NULL, 0, NULL, 0,
  38807. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38808. NULL, 0, 0);
  38809. #endif
  38810. #ifdef WOLFSSL_SHA224
  38811. /* ECDSA with SHA224 */
  38812. ADD_PKCS7SIGNEDVECTOR(
  38813. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  38814. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38815. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38816. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38817. NULL, 0, 0);
  38818. #endif
  38819. #ifndef NO_SHA256
  38820. /* ECDSA with SHA256 */
  38821. ADD_PKCS7SIGNEDVECTOR(
  38822. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  38823. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38824. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38825. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38826. NULL, 0, 0);
  38827. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  38828. ADD_PKCS7SIGNEDVECTOR(
  38829. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  38830. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38831. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38832. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  38833. NULL, 0, NULL, 0, 0);
  38834. /* ECDSA with SHA256 and custom contentType */
  38835. ADD_PKCS7SIGNEDVECTOR(
  38836. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  38837. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38838. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38839. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  38840. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  38841. NULL, 0, 0);
  38842. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  38843. ADD_PKCS7SIGNEDVECTOR(
  38844. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  38845. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38846. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38847. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  38848. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  38849. #endif
  38850. #ifdef WOLFSSL_SHA384
  38851. /* ECDSA with SHA384 */
  38852. ADD_PKCS7SIGNEDVECTOR(
  38853. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  38854. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38855. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38856. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38857. NULL, 0, 0);
  38858. #endif
  38859. #ifdef WOLFSSL_SHA512
  38860. /* ECDSA with SHA512 */
  38861. ADD_PKCS7SIGNEDVECTOR(
  38862. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  38863. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  38864. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  38865. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  38866. NULL, 0, 0);
  38867. #endif
  38868. #endif /* HAVE_ECC */
  38869. };
  38870. #undef MAX_TESTVECTORS_LEN
  38871. #undef ADD_PKCS7SIGNEDVECTOR
  38872. outSz = FOURK_BUF;
  38873. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38874. if (out == NULL)
  38875. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38876. XMEMSET(out, 0, outSz);
  38877. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  38878. if (ret < 0)
  38879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38880. #ifndef HAVE_FIPS
  38881. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38882. #else
  38883. ret = wc_InitRng(&rng);
  38884. #endif
  38885. if (ret != 0)
  38886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38887. for (i = 0; i < testSz; i++) {
  38888. if (pkcs7)
  38889. wc_PKCS7_Free(pkcs7);
  38890. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  38891. if (pkcs7 == NULL)
  38892. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38893. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  38894. (word32)testVectors[i].certSz);
  38895. if (ret != 0)
  38896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38897. /* load CA certificate, if present */
  38898. if (testVectors[i].caCert != NULL) {
  38899. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  38900. (word32)testVectors[i].caCertSz);
  38901. if (ret != 0)
  38902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38903. }
  38904. pkcs7->rng = &rng;
  38905. pkcs7->content = (byte*)testVectors[i].content;
  38906. pkcs7->contentSz = testVectors[i].contentSz;
  38907. pkcs7->contentOID = testVectors[i].contentOID;
  38908. pkcs7->hashOID = testVectors[i].hashOID;
  38909. pkcs7->encryptOID = testVectors[i].signOID;
  38910. pkcs7->privateKey = testVectors[i].privateKey;
  38911. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  38912. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  38913. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  38914. /* optional custom contentType, default is DATA,
  38915. overrides contentOID if set */
  38916. if (testVectors[i].contentType != NULL) {
  38917. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  38918. testVectors[i].contentTypeSz);
  38919. if (ret != 0)
  38920. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38921. }
  38922. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  38923. default is IssuerAndSerialNumber */
  38924. if (testVectors[i].sidType == CMS_SKID) {
  38925. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  38926. if (ret != 0)
  38927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38928. }
  38929. /* generate senderNonce */
  38930. {
  38931. senderNonce[0] = 0x04;
  38932. senderNonce[1] = PKCS7_NONCE_SZ;
  38933. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  38934. if (ret != 0)
  38935. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38936. }
  38937. /* generate transactionID (used with SCEP) */
  38938. {
  38939. #ifndef NO_SHA
  38940. wc_Sha sha;
  38941. byte digest[WC_SHA_DIGEST_SIZE];
  38942. #else
  38943. wc_Sha256 sha;
  38944. byte digest[WC_SHA256_DIGEST_SIZE];
  38945. #endif
  38946. int j,k;
  38947. transId[0] = 0x13;
  38948. transId[1] = sizeof(digest) * 2;
  38949. #ifndef NO_SHA
  38950. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  38951. if (ret != 0)
  38952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38953. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  38954. wc_ShaFinal(&sha, digest);
  38955. wc_ShaFree(&sha);
  38956. #else
  38957. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  38958. if (ret != 0)
  38959. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38960. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  38961. wc_Sha256Final(&sha, digest);
  38962. wc_Sha256Free(&sha);
  38963. #endif
  38964. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  38965. #if defined(WOLF_C89)
  38966. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  38967. #else
  38968. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  38969. #endif
  38970. }
  38971. }
  38972. /* enable detached signature generation, if set */
  38973. if (testVectors[i].detachedSignature == 1) {
  38974. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  38975. if (ret != 0)
  38976. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38977. }
  38978. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  38979. if (encodedSz < 0)
  38980. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  38981. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38982. /* write PKCS#7 to output file for more testing */
  38983. file = XFOPEN(testVectors[i].outFileName, "wb");
  38984. if (!file) {
  38985. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38986. }
  38987. ret = (int)XFWRITE(out, 1, encodedSz, file);
  38988. XFCLOSE(file);
  38989. if (ret != (int)encodedSz)
  38990. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38991. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  38992. wc_PKCS7_Free(pkcs7);
  38993. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  38994. if (pkcs7 == NULL)
  38995. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38996. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  38997. if (testVectors[i].detachedSignature == 1) {
  38998. /* set content for verifying detached signatures */
  38999. pkcs7->content = (byte*)testVectors[i].content;
  39000. pkcs7->contentSz = testVectors[i].contentSz;
  39001. }
  39002. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  39003. if (ret < 0)
  39004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39005. /* verify contentType extracted successfully for custom content types */
  39006. if (testVectors[i].contentTypeSz > 0) {
  39007. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  39008. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39009. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  39010. pkcs7->contentTypeSz) != 0) {
  39011. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39012. }
  39013. }
  39014. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  39015. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39016. {
  39017. /* check getting signed attributes */
  39018. #ifndef NO_SHA
  39019. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  39020. #else
  39021. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  39022. #endif
  39023. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  39024. int oidSz = (int)sizeof(transIdOid) - 2;
  39025. int bufSz = 0;
  39026. if (testVectors[i].signedAttribs != NULL) {
  39027. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  39028. NULL, (word32*)&bufSz);
  39029. if (ret != LENGTH_ONLY_E)
  39030. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39031. ret = 0;
  39032. }
  39033. if (bufSz > (int)sizeof(buf))
  39034. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39035. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  39036. buf, (word32*)&bufSz);
  39037. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  39038. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  39039. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39040. }
  39041. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39042. file = XFOPEN("./pkcs7cert.der", "wb");
  39043. if (!file)
  39044. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39045. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  39046. if (ret < 0)
  39047. ret = WC_TEST_RET_ENC_ERRNO;
  39048. else
  39049. ret = 0;
  39050. XFCLOSE(file);
  39051. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  39052. }
  39053. out:
  39054. if (pkcs7 != NULL)
  39055. wc_PKCS7_Free(pkcs7);
  39056. if (out != NULL)
  39057. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39058. if (testVectors != NULL)
  39059. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39060. wc_FreeRng(&rng);
  39061. if (ret > 0)
  39062. return 0;
  39063. (void)rsaClientCertBuf;
  39064. (void)rsaClientCertBufSz;
  39065. (void)rsaClientPrivKeyBuf;
  39066. (void)rsaClientPrivKeyBufSz;
  39067. (void)rsaServerCertBuf;
  39068. (void)rsaServerCertBufSz;
  39069. (void)rsaServerPrivKeyBuf;
  39070. (void)rsaServerPrivKeyBufSz;
  39071. (void)rsaCaCertBuf;
  39072. (void)rsaCaCertBufSz;
  39073. (void)rsaCaPrivKeyBuf;
  39074. (void)rsaCaPrivKeyBufSz;
  39075. (void)eccClientCertBuf;
  39076. (void)eccClientCertBufSz;
  39077. (void)eccClientPrivKeyBuf;
  39078. (void)eccClientPrivKeyBufSz;
  39079. return ret;
  39080. }
  39081. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  39082. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  39083. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  39084. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  39085. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  39086. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  39087. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  39088. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  39089. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  39090. {
  39091. wc_test_ret_t ret;
  39092. int testSz = 0, i;
  39093. int encodedSz;
  39094. byte* out = NULL;
  39095. word32 outSz;
  39096. WC_RNG rng;
  39097. PKCS7* pkcs7 = NULL;
  39098. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39099. XFILE file;
  39100. #endif
  39101. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  39102. !defined(NO_PKCS7_ENCRYPTED_DATA)
  39103. byte* encryptedTmp = NULL;
  39104. int encryptedTmpSz;
  39105. #endif
  39106. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39107. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39108. 0x72,0x6c,0x64
  39109. };
  39110. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39111. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39112. static byte aes256Key[] = {
  39113. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39114. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39115. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39116. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39117. };
  39118. #endif
  39119. static byte messageTypeOid[] =
  39120. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  39121. 0x09, 0x02 };
  39122. static byte messageType[] = { 0x13, 2, '1', '9' };
  39123. PKCS7Attrib attribs[] =
  39124. {
  39125. { messageTypeOid, sizeof(messageTypeOid), messageType,
  39126. sizeof(messageType) },
  39127. };
  39128. #define MAX_TESTVECTORS_LEN 19
  39129. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  39130. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  39131. if (testSz == MAX_TESTVECTORS_LEN) { \
  39132. ret = WC_TEST_RET_ENC_NC; \
  39133. goto out; \
  39134. } \
  39135. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  39136. sizeof _this_vector); \
  39137. }
  39138. pkcs7SignedVector *testVectors = NULL;
  39139. XMEMSET(&rng, 0, sizeof(rng));
  39140. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  39141. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39142. if (testVectors == NULL) {
  39143. ret = WC_TEST_RET_ENC_ERRNO;
  39144. goto out;
  39145. }
  39146. {
  39147. #ifndef NO_RSA
  39148. #ifndef NO_SHA256
  39149. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  39150. ADD_PKCS7SIGNEDVECTOR(
  39151. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39152. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39153. NULL, 0,
  39154. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  39155. 0, 0, NULL, 0, NULL, 0, 0);
  39156. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  39157. ADD_PKCS7SIGNEDVECTOR(
  39158. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39159. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39160. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39161. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  39162. NULL, 0, NULL, 0, 0);
  39163. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  39164. ADD_PKCS7SIGNEDVECTOR(
  39165. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39166. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39167. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39168. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  39169. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  39170. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  39171. ADD_PKCS7SIGNEDVECTOR(
  39172. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  39173. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  39174. rsaCaCertBuf, rsaCaCertBufSz,
  39175. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39176. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  39177. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39178. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39179. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39180. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  39181. ADD_PKCS7SIGNEDVECTOR(
  39182. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39183. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39184. NULL, 0,
  39185. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  39186. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  39187. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  39188. ADD_PKCS7SIGNEDVECTOR(
  39189. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39190. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39191. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39192. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  39193. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  39194. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39195. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  39196. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39197. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  39198. ADD_PKCS7SIGNEDVECTOR(
  39199. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39200. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39201. NULL, 0,
  39202. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  39203. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39204. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  39205. ADD_PKCS7SIGNEDVECTOR(
  39206. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39207. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39208. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39209. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  39210. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39211. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39212. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  39213. no attribs */
  39214. ADD_PKCS7SIGNEDVECTOR(
  39215. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39216. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39217. NULL, 0,
  39218. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  39219. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  39220. 0, 0);
  39221. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  39222. attribs */
  39223. ADD_PKCS7SIGNEDVECTOR(
  39224. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39225. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39226. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39227. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  39228. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  39229. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39230. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  39231. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  39232. #endif /* NO_SHA256 */
  39233. #endif /* NO_RSA */
  39234. #ifdef HAVE_ECC
  39235. #ifndef NO_SHA256
  39236. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  39237. ADD_PKCS7SIGNEDVECTOR(
  39238. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39239. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39240. NULL, 0,
  39241. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39242. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39243. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  39244. ADD_PKCS7SIGNEDVECTOR(
  39245. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39246. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39247. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39248. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39249. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39250. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  39251. ADD_PKCS7SIGNEDVECTOR(
  39252. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39253. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39254. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39255. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  39256. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  39257. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39258. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39259. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  39260. ADD_PKCS7SIGNEDVECTOR(
  39261. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39262. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39263. NULL, 0,
  39264. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39265. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  39266. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  39267. ADD_PKCS7SIGNEDVECTOR(
  39268. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39269. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39270. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39271. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39272. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  39273. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39274. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  39275. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39276. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  39277. ADD_PKCS7SIGNEDVECTOR(
  39278. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39279. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39280. NULL, 0,
  39281. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39282. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39283. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  39284. ADD_PKCS7SIGNEDVECTOR(
  39285. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39286. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39287. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39288. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39289. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39290. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39291. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  39292. no attribs */
  39293. ADD_PKCS7SIGNEDVECTOR(
  39294. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39295. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39296. NULL, 0,
  39297. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  39298. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  39299. 0, 0);
  39300. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  39301. attribs */
  39302. ADD_PKCS7SIGNEDVECTOR(
  39303. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39304. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39305. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39306. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  39307. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  39308. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39309. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  39310. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  39311. #endif /* NO_SHA256 */
  39312. #endif /* HAVE_ECC */
  39313. };
  39314. #undef MAX_TESTVECTORS_LEN
  39315. #undef ADD_PKCS7SIGNEDVECTOR
  39316. outSz = FOURK_BUF;
  39317. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39318. if (out == NULL)
  39319. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39320. XMEMSET(out, 0, outSz);
  39321. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  39322. if (ret < 0)
  39323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39324. #ifndef HAVE_FIPS
  39325. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  39326. #else
  39327. ret = wc_InitRng(&rng);
  39328. #endif
  39329. if (ret != 0)
  39330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39331. for (i = 0; i < testSz; i++) {
  39332. if (pkcs7)
  39333. wc_PKCS7_Free(pkcs7);
  39334. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39335. if (pkcs7 == NULL)
  39336. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39337. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  39338. (word32)testVectors[i].certSz);
  39339. if (ret != 0)
  39340. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39341. /* load CA certificate, if present */
  39342. if (testVectors[i].caCert != NULL) {
  39343. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  39344. (word32)testVectors[i].caCertSz);
  39345. if (ret != 0)
  39346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39347. }
  39348. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  39349. default is IssuerAndSerialNumber */
  39350. if (testVectors[i].sidType == CMS_SKID) {
  39351. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  39352. if (ret != 0)
  39353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39354. }
  39355. if (testVectors[i].encCompFlag == 0) {
  39356. /* encode Signed FirmwarePkgData */
  39357. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  39358. testVectors[i].privateKey, testVectors[i].privateKeySz,
  39359. testVectors[i].signOID, testVectors[i].hashOID,
  39360. (byte*)testVectors[i].content, testVectors[i].contentSz,
  39361. testVectors[i].signedAttribs,
  39362. testVectors[i].signedAttribsSz, out, outSz);
  39363. if (encodedSz < 0)
  39364. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  39365. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39366. } else if (testVectors[i].encCompFlag == 1) {
  39367. /* encode Signed Encrypted FirmwarePkgData */
  39368. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  39369. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  39370. testVectors[i].privateKey, testVectors[i].privateKeySz,
  39371. testVectors[i].encryptOID, testVectors[i].signOID,
  39372. testVectors[i].hashOID, (byte*)testVectors[i].content,
  39373. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  39374. testVectors[i].unprotectedAttribsSz,
  39375. testVectors[i].signedAttribs,
  39376. testVectors[i].signedAttribsSz, out, outSz);
  39377. if (encodedSz <= 0)
  39378. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  39379. #endif
  39380. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39381. } else if (testVectors[i].encCompFlag == 2) {
  39382. /* encode Signed Compressed FirmwarePkgData */
  39383. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  39384. testVectors[i].privateKey, testVectors[i].privateKeySz,
  39385. testVectors[i].signOID, testVectors[i].hashOID,
  39386. (byte*)testVectors[i].content, testVectors[i].contentSz,
  39387. testVectors[i].signedAttribs,
  39388. testVectors[i].signedAttribsSz, out, outSz);
  39389. if (encodedSz <= 0)
  39390. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  39391. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39392. } else if (testVectors[i].encCompFlag == 3) {
  39393. /* encode Signed Encrypted Compressed FirmwarePkgData */
  39394. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  39395. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  39396. testVectors[i].privateKey, testVectors[i].privateKeySz,
  39397. testVectors[i].encryptOID, testVectors[i].signOID,
  39398. testVectors[i].hashOID, (byte*)testVectors[i].content,
  39399. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  39400. testVectors[i].unprotectedAttribsSz,
  39401. testVectors[i].signedAttribs,
  39402. testVectors[i].signedAttribsSz, out, outSz);
  39403. if (encodedSz <= 0)
  39404. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  39405. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  39406. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  39407. } else {
  39408. /* unsupported SignedData single-shot combination */
  39409. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39410. }
  39411. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39412. /* write PKCS#7 to output file for more testing */
  39413. file = XFOPEN(testVectors[i].outFileName, "wb");
  39414. if (!file)
  39415. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39416. ret = (int)XFWRITE(out, 1, encodedSz, file);
  39417. XFCLOSE(file);
  39418. file = NULL;
  39419. if (ret != (int)encodedSz)
  39420. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39421. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  39422. wc_PKCS7_Free(pkcs7);
  39423. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39424. if (pkcs7 == NULL)
  39425. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39426. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  39427. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  39428. if (ret < 0)
  39429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39430. #ifndef NO_PKCS7_STREAM
  39431. {
  39432. word32 z;
  39433. for (z = 0; z < outSz && ret != 0; z++) {
  39434. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  39435. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  39436. printf("unexpected error %d\n", ret);
  39437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39438. }
  39439. }
  39440. }
  39441. #endif
  39442. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  39443. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39444. if (testVectors[i].encCompFlag == 0) {
  39445. /* verify decoded content matches expected */
  39446. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  39447. XMEMCMP(pkcs7->content, testVectors[i].content,
  39448. pkcs7->contentSz)) {
  39449. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39450. }
  39451. }
  39452. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39453. else if (testVectors[i].encCompFlag == 1) {
  39454. /* decrypt inner encryptedData */
  39455. pkcs7->encryptionKey = testVectors[i].encryptKey;
  39456. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  39457. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  39458. pkcs7->contentSz, out, outSz);
  39459. if (ret < 0)
  39460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39461. /* compare decrypted to expected */
  39462. if (((word32)ret != testVectors[i].contentSz) ||
  39463. XMEMCMP(out, testVectors[i].content, ret))
  39464. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39465. }
  39466. #endif
  39467. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39468. else if (testVectors[i].encCompFlag == 2) {
  39469. /* decompress inner compressedData */
  39470. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  39471. pkcs7->contentSz, out, outSz);
  39472. if (ret < 0)
  39473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39474. /* compare decompressed to expected */
  39475. if (((word32)ret != testVectors[i].contentSz) ||
  39476. XMEMCMP(out, testVectors[i].content, ret))
  39477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39478. }
  39479. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39480. else if (testVectors[i].encCompFlag == 3) {
  39481. encryptedTmpSz = FOURK_BUF;
  39482. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  39483. DYNAMIC_TYPE_TMP_BUFFER);
  39484. if (encryptedTmp == NULL)
  39485. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39486. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  39487. /* decrypt inner encryptedData */
  39488. pkcs7->encryptionKey = testVectors[i].encryptKey;
  39489. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  39490. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  39491. pkcs7->contentSz, encryptedTmp,
  39492. encryptedTmpSz);
  39493. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  39494. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39495. /* decompress inner compressedData */
  39496. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  39497. encryptedTmpSz, out, outSz);
  39498. if (ret < 0)
  39499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39500. /* compare decompressed to expected */
  39501. if (((word32)ret != testVectors[i].contentSz) ||
  39502. XMEMCMP(out, testVectors[i].content, ret))
  39503. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39504. }
  39505. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  39506. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  39507. }
  39508. out:
  39509. if (pkcs7 != NULL)
  39510. wc_PKCS7_Free(pkcs7);
  39511. if (out != NULL)
  39512. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39513. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  39514. !defined(NO_PKCS7_ENCRYPTED_DATA)
  39515. if (encryptedTmp != NULL)
  39516. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39517. #endif
  39518. if (testVectors != NULL)
  39519. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39520. wc_FreeRng(&rng);
  39521. if (ret > 0)
  39522. return 0;
  39523. (void)eccClientCertBuf;
  39524. (void)eccClientCertBufSz;
  39525. (void)eccClientPrivKeyBuf;
  39526. (void)eccClientPrivKeyBufSz;
  39527. (void)rsaClientCertBuf;
  39528. (void)rsaClientCertBufSz;
  39529. (void)rsaClientPrivKeyBuf;
  39530. (void)rsaClientPrivKeyBufSz;
  39531. (void)rsaServerCertBuf;
  39532. (void)rsaServerCertBufSz;
  39533. (void)rsaServerPrivKeyBuf;
  39534. (void)rsaServerPrivKeyBufSz;
  39535. (void)rsaCaCertBuf;
  39536. (void)rsaCaCertBufSz;
  39537. (void)rsaCaPrivKeyBuf;
  39538. (void)rsaCaPrivKeyBufSz;
  39539. return ret;
  39540. }
  39541. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  39542. {
  39543. wc_test_ret_t ret = 0;
  39544. byte* rsaClientCertBuf = NULL;
  39545. byte* rsaServerCertBuf = NULL;
  39546. byte* rsaCaCertBuf = NULL;
  39547. byte* eccClientCertBuf = NULL;
  39548. byte* rsaClientPrivKeyBuf = NULL;
  39549. byte* rsaServerPrivKeyBuf = NULL;
  39550. byte* rsaCaPrivKeyBuf = NULL;
  39551. byte* eccClientPrivKeyBuf = NULL;
  39552. word32 rsaClientCertBufSz = 0;
  39553. word32 rsaServerCertBufSz = 0;
  39554. word32 rsaCaCertBufSz = 0;
  39555. word32 eccClientCertBufSz = 0;
  39556. word32 rsaClientPrivKeyBufSz = 0;
  39557. word32 rsaServerPrivKeyBufSz = 0;
  39558. word32 rsaCaPrivKeyBufSz = 0;
  39559. word32 eccClientPrivKeyBufSz = 0;
  39560. #ifndef NO_RSA
  39561. /* read client RSA cert and key in DER format */
  39562. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39563. DYNAMIC_TYPE_TMP_BUFFER);
  39564. if (rsaClientCertBuf == NULL)
  39565. ret = WC_TEST_RET_ENC_NC;
  39566. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39567. DYNAMIC_TYPE_TMP_BUFFER);
  39568. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  39569. ret = WC_TEST_RET_ENC_ERRNO;
  39570. }
  39571. rsaClientCertBufSz = FOURK_BUF;
  39572. rsaClientPrivKeyBufSz = FOURK_BUF;
  39573. /* read server RSA cert and key in DER format */
  39574. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39575. DYNAMIC_TYPE_TMP_BUFFER);
  39576. if (ret == 0 && rsaServerCertBuf == NULL)
  39577. ret = WC_TEST_RET_ENC_NC;
  39578. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39579. DYNAMIC_TYPE_TMP_BUFFER);
  39580. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  39581. ret = WC_TEST_RET_ENC_ERRNO;
  39582. }
  39583. rsaServerCertBufSz = FOURK_BUF;
  39584. rsaServerPrivKeyBufSz = FOURK_BUF;
  39585. /* read CA RSA cert and key in DER format, for use with server cert */
  39586. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39587. DYNAMIC_TYPE_TMP_BUFFER);
  39588. if (ret == 0 && rsaCaCertBuf == NULL)
  39589. ret = WC_TEST_RET_ENC_NC;
  39590. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39591. DYNAMIC_TYPE_TMP_BUFFER);
  39592. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  39593. ret = WC_TEST_RET_ENC_ERRNO;
  39594. }
  39595. rsaCaCertBufSz = FOURK_BUF;
  39596. rsaCaPrivKeyBufSz = FOURK_BUF;
  39597. #endif /* NO_RSA */
  39598. #ifdef HAVE_ECC
  39599. /* read client ECC cert and key in DER format */
  39600. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39601. DYNAMIC_TYPE_TMP_BUFFER);
  39602. if (ret == 0 && eccClientCertBuf == NULL) {
  39603. ret = WC_TEST_RET_ENC_ERRNO;
  39604. }
  39605. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  39606. DYNAMIC_TYPE_TMP_BUFFER);
  39607. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  39608. ret = WC_TEST_RET_ENC_ERRNO;
  39609. }
  39610. eccClientCertBufSz = FOURK_BUF;
  39611. eccClientPrivKeyBufSz = FOURK_BUF;
  39612. #endif /* HAVE_ECC */
  39613. if (ret >= 0)
  39614. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  39615. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  39616. rsaServerCertBuf, &rsaServerCertBufSz,
  39617. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  39618. rsaCaCertBuf, &rsaCaCertBufSz,
  39619. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  39620. eccClientCertBuf, &eccClientCertBufSz,
  39621. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  39622. if (ret < 0) {
  39623. ret = WC_TEST_RET_ENC_EC(ret);
  39624. }
  39625. if (ret >= 0)
  39626. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  39627. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  39628. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  39629. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  39630. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  39631. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  39632. eccClientCertBuf, (word32)eccClientCertBufSz,
  39633. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  39634. if (ret >= 0)
  39635. ret = pkcs7signed_run_SingleShotVectors(
  39636. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  39637. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  39638. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  39639. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  39640. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  39641. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  39642. eccClientCertBuf, (word32)eccClientCertBufSz,
  39643. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  39644. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  39645. if (ret >= 0)
  39646. ret = pkcs7callback_test(
  39647. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  39648. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  39649. #endif
  39650. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39651. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39652. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39653. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39654. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39655. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39656. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39657. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39658. return ret;
  39659. }
  39660. #endif /* HAVE_PKCS7 */
  39661. #if defined(WOLFSSL_PUBLIC_MP) && \
  39662. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39663. defined(USE_FAST_MATH))
  39664. /* Maximum number of bytes in a number to test. */
  39665. #define MP_MAX_TEST_BYTE_LEN 32
  39666. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  39667. {
  39668. byte d[MP_MAX_TEST_BYTE_LEN];
  39669. wc_test_ret_t ret;
  39670. (void)heap;
  39671. do {
  39672. ret = wc_RNG_GenerateBlock(rng, d, len);
  39673. if (ret != 0)
  39674. return ret;
  39675. ret = mp_read_unsigned_bin(n, d, len);
  39676. if (ret != 0)
  39677. return ret;
  39678. } while (mp_iszero(n));
  39679. return 0;
  39680. }
  39681. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  39682. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  39683. {
  39684. int i, j;
  39685. mp_digit rem;
  39686. mp_digit rem2;
  39687. wc_test_ret_t ret;
  39688. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39689. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  39690. for (i = 0; i < 10; i++) {
  39691. for (j = 1; j < 10; j++) {
  39692. ret = randNum(a, j, rng, NULL);
  39693. if (ret != 0)
  39694. return WC_TEST_RET_ENC_EC(ret);
  39695. ret = mp_div_3(a, r, &rem);
  39696. if (ret != 0)
  39697. return WC_TEST_RET_ENC_EC(ret);
  39698. ret = mp_mul_d(r, 3, r);
  39699. if (ret != 0)
  39700. return WC_TEST_RET_ENC_EC(ret);
  39701. ret = mp_add_d(r, rem, r);
  39702. if (ret != 0)
  39703. return WC_TEST_RET_ENC_EC(ret);
  39704. ret = mp_cmp(r, a);
  39705. if (ret != MP_EQ)
  39706. return WC_TEST_RET_ENC_NC;
  39707. }
  39708. }
  39709. ret = mp_div_3(a, r, &rem);
  39710. if (ret != 0)
  39711. return WC_TEST_RET_ENC_NC;
  39712. ret = mp_div_3(a, a, NULL);
  39713. if (ret != 0)
  39714. return WC_TEST_RET_ENC_NC;
  39715. ret = mp_cmp(r, a);
  39716. if (ret != MP_EQ)
  39717. return WC_TEST_RET_ENC_NC;
  39718. #endif
  39719. #if defined(WOLFSSL_SP_MATH_ALL)
  39720. ret = mp_div_d(a, 10, r, &rem);
  39721. if (ret != 0)
  39722. return WC_TEST_RET_ENC_EC(ret);
  39723. ret = mp_div_d(a, 10, a, NULL);
  39724. if (ret != 0)
  39725. return WC_TEST_RET_ENC_EC(ret);
  39726. ret = mp_cmp(r, a);
  39727. if (ret != MP_EQ)
  39728. return WC_TEST_RET_ENC_NC;
  39729. ret = mp_div_d(a, 12, r, &rem);
  39730. if (ret != 0)
  39731. return WC_TEST_RET_ENC_EC(ret);
  39732. ret = mp_div_d(a, 12, a, NULL);
  39733. if (ret != 0)
  39734. return WC_TEST_RET_ENC_EC(ret);
  39735. ret = mp_cmp(r, a);
  39736. if (ret != MP_EQ)
  39737. return WC_TEST_RET_ENC_NC;
  39738. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  39739. if (ret != 0)
  39740. return WC_TEST_RET_ENC_EC(ret);
  39741. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  39742. if (ret != 0)
  39743. return WC_TEST_RET_ENC_EC(ret);
  39744. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  39745. if (ret != 0)
  39746. return WC_TEST_RET_ENC_EC(ret);
  39747. ret = mp_cmp(r, a);
  39748. if (ret != MP_EQ)
  39749. return WC_TEST_RET_ENC_NC;
  39750. if (rem != rem2)
  39751. return WC_TEST_RET_ENC_NC;
  39752. #endif
  39753. (void)a;
  39754. (void)r;
  39755. (void)rng;
  39756. (void)i;
  39757. (void)j;
  39758. (void)rem;
  39759. (void)rem2;
  39760. (void)ret;
  39761. return 0;
  39762. }
  39763. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  39764. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  39765. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39766. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  39767. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  39768. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  39769. {
  39770. wc_test_ret_t ret;
  39771. int i, j;
  39772. int size;
  39773. char str[30];
  39774. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  39775. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  39776. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  39777. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  39778. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  39779. for (i = 0; i < 10; i++) {
  39780. for (j = 2; j < 12; j++) {
  39781. ret = randNum(a, j, rng, NULL);
  39782. if (ret != 0)
  39783. return WC_TEST_RET_ENC_EC(ret);
  39784. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  39785. if (ret != MP_OKAY)
  39786. return WC_TEST_RET_ENC_EC(ret);
  39787. ret = mp_toradix(a, str, MP_RADIX_DEC);
  39788. if (ret != MP_OKAY)
  39789. return WC_TEST_RET_ENC_EC(ret);
  39790. if ((int)XSTRLEN(str) != size - 1)
  39791. return WC_TEST_RET_ENC_NC;
  39792. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  39793. if (ret != MP_OKAY)
  39794. return WC_TEST_RET_ENC_EC(ret);
  39795. ret = mp_cmp(a, r);
  39796. if (ret != MP_EQ)
  39797. return WC_TEST_RET_ENC_NC;
  39798. }
  39799. }
  39800. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  39801. if (ret != MP_VAL)
  39802. return WC_TEST_RET_ENC_EC(ret);
  39803. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  39804. if (ret != MP_VAL)
  39805. return WC_TEST_RET_ENC_EC(ret);
  39806. ret = mp_read_radix(r, badStr3, MP_RADIX_DEC);
  39807. if (ret != MP_VAL)
  39808. return WC_TEST_RET_ENC_EC(ret);
  39809. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  39810. if (ret != MP_OKAY)
  39811. return WC_TEST_RET_ENC_EC(ret);
  39812. if (!mp_iszero(r))
  39813. return WC_TEST_RET_ENC_NC;
  39814. mp_set(r, 1);
  39815. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  39816. if (ret != MP_OKAY)
  39817. return WC_TEST_RET_ENC_EC(ret);
  39818. if (!mp_iszero(r))
  39819. return WC_TEST_RET_ENC_NC;
  39820. mp_zero(a);
  39821. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  39822. if (ret != 0)
  39823. return WC_TEST_RET_ENC_EC(ret);
  39824. if (size != 2)
  39825. return WC_TEST_RET_ENC_NC;
  39826. ret = mp_toradix(a, str, MP_RADIX_DEC);
  39827. if (ret != 0)
  39828. return WC_TEST_RET_ENC_EC(ret);
  39829. if ((int)XSTRLEN(str) != size - 1)
  39830. return WC_TEST_RET_ENC_NC;
  39831. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  39832. if (ret != 0)
  39833. return WC_TEST_RET_ENC_EC(ret);
  39834. if (!mp_iszero(r))
  39835. return WC_TEST_RET_ENC_NC;
  39836. return 0;
  39837. }
  39838. #endif
  39839. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  39840. defined(HAVE_ECC))
  39841. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  39842. {
  39843. wc_test_ret_t ret;
  39844. int i, j;
  39845. int size;
  39846. char str[30];
  39847. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  39848. static char longStr[2 * sizeof(a->dp) + 2];
  39849. #endif
  39850. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  39851. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  39852. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  39853. for (i = 0; i < 10; i++) {
  39854. for (j = 2; j < 12; j++) {
  39855. ret = randNum(a, j, rng, NULL);
  39856. if (ret != 0)
  39857. return WC_TEST_RET_ENC_EC(ret);
  39858. mp_radix_size(a, MP_RADIX_HEX, &size);
  39859. mp_toradix(a, str, MP_RADIX_HEX);
  39860. if ((int)XSTRLEN(str) != size - 1)
  39861. return WC_TEST_RET_ENC_NC;
  39862. mp_read_radix(r, str, MP_RADIX_HEX);
  39863. ret = mp_cmp(a, r);
  39864. if (ret != MP_EQ)
  39865. return WC_TEST_RET_ENC_NC;
  39866. }
  39867. }
  39868. ret = mp_read_radix(r, badStr1, MP_RADIX_HEX);
  39869. if (ret != MP_VAL)
  39870. return WC_TEST_RET_ENC_EC(ret);
  39871. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  39872. if (ret != MP_VAL)
  39873. return WC_TEST_RET_ENC_EC(ret);
  39874. mp_set(r, 1);
  39875. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  39876. if (ret != MP_OKAY)
  39877. return WC_TEST_RET_ENC_EC(ret);
  39878. if (!mp_iszero(r))
  39879. return WC_TEST_RET_ENC_NC;
  39880. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  39881. /* Fixed MP data size - string can be too long. */
  39882. longStr[0] = '8';
  39883. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  39884. longStr[sizeof(longStr)-1] = '\0';
  39885. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  39886. if (ret != MP_VAL)
  39887. return WC_TEST_RET_ENC_EC(ret);
  39888. #endif
  39889. mp_zero(a);
  39890. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  39891. if (ret != 0)
  39892. return WC_TEST_RET_ENC_EC(ret);
  39893. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  39894. if (size != 3)
  39895. #else
  39896. if (size != 2)
  39897. #endif
  39898. return WC_TEST_RET_ENC_NC;
  39899. ret = mp_toradix(a, str, MP_RADIX_HEX);
  39900. if (ret != 0)
  39901. return WC_TEST_RET_ENC_EC(ret);
  39902. if ((int)XSTRLEN(str) != size - 1)
  39903. return WC_TEST_RET_ENC_NC;
  39904. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  39905. if (ret != 0)
  39906. return WC_TEST_RET_ENC_EC(ret);
  39907. if (!mp_iszero(r))
  39908. return WC_TEST_RET_ENC_NC;
  39909. #ifdef WOLFSSL_SP_MATH
  39910. ret = mp_toradix(a, str, 8);
  39911. if (ret != MP_VAL)
  39912. return WC_TEST_RET_ENC_EC(ret);
  39913. ret = mp_radix_size(a, 8, &size);
  39914. if (ret != MP_VAL)
  39915. return WC_TEST_RET_ENC_EC(ret);
  39916. #endif
  39917. return 0;
  39918. }
  39919. #endif
  39920. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  39921. {
  39922. int i;
  39923. wc_test_ret_t ret;
  39924. ret = randNum(a, 4, rng, NULL);
  39925. if (ret != 0)
  39926. return WC_TEST_RET_ENC_EC(ret);
  39927. for (i = 0; i < 4; i++) {
  39928. mp_copy(r1, a);
  39929. ret = mp_lshd(r1, i);
  39930. if (ret != MP_OKAY)
  39931. return WC_TEST_RET_ENC_EC(ret);
  39932. #ifndef WOLFSSL_SP_MATH
  39933. mp_rshd(r1, i);
  39934. #else
  39935. mp_rshb(r1, i * SP_WORD_SIZE);
  39936. #endif
  39937. ret = mp_cmp(a, r1);
  39938. if (ret != MP_EQ)
  39939. return WC_TEST_RET_ENC_NC;
  39940. }
  39941. #ifndef WOLFSSL_SP_MATH
  39942. for (i = 0; i < DIGIT_BIT+1; i++) {
  39943. ret = mp_mul_2d(a, i, r1);
  39944. if (ret != MP_OKAY)
  39945. return WC_TEST_RET_ENC_EC(ret);
  39946. mp_rshb(r1, i);
  39947. ret = mp_cmp(a, r1);
  39948. if (ret != MP_EQ)
  39949. return WC_TEST_RET_ENC_NC;
  39950. }
  39951. #endif
  39952. return 0;
  39953. }
  39954. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  39955. {
  39956. int i, j;
  39957. wc_test_ret_t ret;
  39958. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  39959. mp_zero(a);
  39960. mp_set_bit(a, i);
  39961. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  39962. return WC_TEST_RET_ENC_NC;
  39963. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  39964. mp_zero(r1);
  39965. mp_set_bit(r1, i);
  39966. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  39967. if (ret != MP_OKAY)
  39968. return WC_TEST_RET_ENC_EC(ret);
  39969. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  39970. if (ret != MP_OKAY)
  39971. return WC_TEST_RET_ENC_EC(ret);
  39972. ret = mp_cmp(a, r1);
  39973. if (ret != MP_EQ)
  39974. return WC_TEST_RET_ENC_NC;
  39975. }
  39976. }
  39977. mp_zero(r1);
  39978. ret = mp_add_d(r1, 1, r1);
  39979. if (ret != MP_OKAY)
  39980. return WC_TEST_RET_ENC_EC(ret);
  39981. if (r1->used != 1)
  39982. return WC_TEST_RET_ENC_NC;
  39983. ret = mp_sub_d(r1, 1, r1);
  39984. if (ret != MP_OKAY)
  39985. return WC_TEST_RET_ENC_EC(ret);
  39986. if (r1->used != 0)
  39987. return WC_TEST_RET_ENC_NC;
  39988. return 0;
  39989. }
  39990. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  39991. {
  39992. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  39993. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  39994. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  39995. };
  39996. byte out[24];
  39997. int i, j, k;
  39998. const byte* p;
  39999. wc_test_ret_t ret;
  40000. for (i = 0; i < (int)sizeof(in); i++) {
  40001. p = in + sizeof(in) - i;
  40002. ret = mp_read_unsigned_bin(a, p, i);
  40003. if (ret != 0)
  40004. return WC_TEST_RET_ENC_EC(ret);
  40005. for (j = i; j < (int)sizeof(out); j++) {
  40006. XMEMSET(out, 0xff, sizeof(out));
  40007. ret = mp_to_unsigned_bin_len(a, out, j);
  40008. if (ret != 0)
  40009. return WC_TEST_RET_ENC_EC(ret);
  40010. for (k = 0; k < j - i; k++) {
  40011. if (out[k] != 0)
  40012. return WC_TEST_RET_ENC_NC;
  40013. }
  40014. for (; k < j; k++) {
  40015. if (out[k] != p[k - (j - i)])
  40016. return WC_TEST_RET_ENC_NC;
  40017. }
  40018. }
  40019. }
  40020. /* Length too small. */
  40021. ret = mp_to_unsigned_bin_len(a, out, 1);
  40022. if (ret != MP_VAL)
  40023. return WC_TEST_RET_ENC_EC(ret);
  40024. ret = mp_read_unsigned_bin(a, NULL, 0);
  40025. if (ret != 0)
  40026. return WC_TEST_RET_ENC_EC(ret);
  40027. if (!mp_iszero(a))
  40028. return WC_TEST_RET_ENC_NC;
  40029. return 0;
  40030. }
  40031. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  40032. static wc_test_ret_t mp_test_set_int(mp_int* a)
  40033. {
  40034. #if SP_ULONG_BITS == 64
  40035. unsigned long n = 0xfedcba9876543210UL;
  40036. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  40037. byte out[8] = { 0 };
  40038. #elif SP_ULONG_BITS == 32
  40039. unsigned long n = 0xfedcba98UL;
  40040. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  40041. byte out[4] = { 0 };
  40042. #elif SP_ULONG_BITS == 16
  40043. unsigned long n = 0xfedc;
  40044. byte exp[2] = { 0xfe, 0xdc };
  40045. byte out[2] = { 0 };
  40046. #elif SP_ULONG_BITS == 8
  40047. unsigned long n = 0xfe;
  40048. byte exp[1] = { 0xfe };
  40049. byte out[1] = { 0 };
  40050. #endif
  40051. wc_test_ret_t ret;
  40052. ret = mp_set_int(a, n);
  40053. if (ret != 0)
  40054. return WC_TEST_RET_ENC_EC(ret);
  40055. ret = mp_unsigned_bin_size(a);
  40056. if (ret != sizeof(exp))
  40057. return WC_TEST_RET_ENC_NC;
  40058. ret = mp_to_unsigned_bin(a, out);
  40059. if (ret != 0)
  40060. return WC_TEST_RET_ENC_EC(ret);
  40061. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  40062. return WC_TEST_RET_ENC_NC;
  40063. return 0;
  40064. }
  40065. #endif
  40066. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  40067. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  40068. {
  40069. byte buffer[16];
  40070. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  40071. char hexStr[] = "abcdef0123456789";
  40072. #ifndef WOLFSSL_SP_INT_NEGATIVE
  40073. char negStr[] = "-1234";
  40074. #endif
  40075. #endif
  40076. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  40077. defined(HAVE_COMP_KEY)
  40078. char decStr[] = "0987654321";
  40079. #endif
  40080. wc_test_ret_t ret;
  40081. #ifdef WOLFSSL_SP_MATH_ALL
  40082. mp_digit rho;
  40083. int size;
  40084. #endif
  40085. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  40086. int result;
  40087. #endif
  40088. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  40089. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  40090. mp_digit rd;
  40091. #endif
  40092. (void)rng;
  40093. (void)r;
  40094. ret = mp_init(NULL);
  40095. if (ret != MP_VAL)
  40096. return WC_TEST_RET_ENC_EC(ret);
  40097. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  40098. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  40099. if (ret != MP_OKAY)
  40100. return WC_TEST_RET_ENC_EC(ret);
  40101. #endif
  40102. mp_free(NULL);
  40103. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  40104. ret = mp_grow(NULL, 1);
  40105. if (ret != MP_VAL)
  40106. return WC_TEST_RET_ENC_EC(ret);
  40107. #ifdef WOLFSSL_SP_MATH
  40108. ret = mp_grow(a, SP_INT_DIGITS + 1);
  40109. if (ret != MP_MEM)
  40110. return WC_TEST_RET_ENC_EC(ret);
  40111. #endif
  40112. #endif
  40113. mp_clear(NULL);
  40114. ret = mp_abs(NULL, NULL);
  40115. if (ret != MP_VAL)
  40116. return WC_TEST_RET_ENC_EC(ret);
  40117. ret = mp_abs(a, NULL);
  40118. if (ret != MP_VAL)
  40119. return WC_TEST_RET_ENC_EC(ret);
  40120. ret = mp_abs(NULL, b);
  40121. if (ret != MP_VAL)
  40122. return WC_TEST_RET_ENC_EC(ret);
  40123. ret = mp_unsigned_bin_size(NULL);
  40124. if (ret != 0)
  40125. return WC_TEST_RET_ENC_EC(ret);
  40126. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  40127. if (ret != MP_VAL)
  40128. return WC_TEST_RET_ENC_EC(ret);
  40129. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  40130. if (ret != MP_VAL)
  40131. return WC_TEST_RET_ENC_EC(ret);
  40132. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  40133. if (ret != MP_VAL)
  40134. return WC_TEST_RET_ENC_EC(ret);
  40135. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  40136. if (ret != MP_VAL)
  40137. return WC_TEST_RET_ENC_EC(ret);
  40138. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  40139. ret = mp_read_radix(NULL, NULL, 16);
  40140. if (ret != MP_VAL)
  40141. return WC_TEST_RET_ENC_EC(ret);
  40142. ret = mp_read_radix(a, NULL, 16);
  40143. if (ret != MP_VAL)
  40144. return WC_TEST_RET_ENC_EC(ret);
  40145. ret = mp_read_radix(NULL, hexStr, 16);
  40146. if (ret != MP_VAL)
  40147. return WC_TEST_RET_ENC_EC(ret);
  40148. #ifndef WOLFSSL_SP_INT_NEGATIVE
  40149. ret = mp_read_radix(a, negStr, 16);
  40150. if (ret != MP_VAL)
  40151. return WC_TEST_RET_ENC_EC(ret);
  40152. #ifdef WOLFSSL_SP_MATH_ALL
  40153. ret = mp_read_radix(a, negStr, 10);
  40154. if (ret != MP_VAL)
  40155. return WC_TEST_RET_ENC_EC(ret);
  40156. #endif /* WOLFSSL_SP_MATH_ALL */
  40157. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  40158. #endif
  40159. #ifndef WOLFSSL_SP_MATH_ALL
  40160. /* Radix 10 only supported with ALL. */
  40161. ret = mp_read_radix(a, decStr, 10);
  40162. if (ret != MP_VAL)
  40163. return WC_TEST_RET_ENC_EC(ret);
  40164. #endif
  40165. /* Radix 8 not supported SP_INT. */
  40166. ret = mp_read_radix(a, "0123", 8);
  40167. if (ret != MP_VAL)
  40168. return WC_TEST_RET_ENC_EC(ret);
  40169. ret = mp_count_bits(NULL);
  40170. if (ret != 0)
  40171. return WC_TEST_RET_ENC_EC(ret);
  40172. ret = mp_is_bit_set(NULL, 0);
  40173. if (ret != 0)
  40174. return WC_TEST_RET_ENC_EC(ret);
  40175. ret = mp_leading_bit(NULL);
  40176. if (ret != 0)
  40177. return WC_TEST_RET_ENC_EC(ret);
  40178. mp_zero(a);
  40179. ret = mp_leading_bit(a);
  40180. if (ret != 0)
  40181. return WC_TEST_RET_ENC_EC(ret);
  40182. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  40183. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  40184. !defined(NO_RSA)
  40185. ret = mp_set_bit(NULL, 1);
  40186. if (ret != MP_VAL)
  40187. return WC_TEST_RET_ENC_EC(ret);
  40188. #endif
  40189. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  40190. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40191. ret = mp_to_unsigned_bin(NULL, NULL);
  40192. if (ret != MP_VAL)
  40193. return WC_TEST_RET_ENC_EC(ret);
  40194. ret = mp_to_unsigned_bin(a, NULL);
  40195. if (ret != MP_VAL)
  40196. return WC_TEST_RET_ENC_EC(ret);
  40197. ret = mp_to_unsigned_bin(NULL, buffer);
  40198. if (ret != MP_VAL)
  40199. return WC_TEST_RET_ENC_EC(ret);
  40200. #endif
  40201. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  40202. if (ret != MP_VAL)
  40203. return WC_TEST_RET_ENC_EC(ret);
  40204. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  40205. if (ret != MP_VAL)
  40206. return WC_TEST_RET_ENC_EC(ret);
  40207. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  40208. if (ret != MP_VAL)
  40209. return WC_TEST_RET_ENC_EC(ret);
  40210. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  40211. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40212. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  40213. if (ret != MP_VAL)
  40214. return WC_TEST_RET_ENC_EC(ret);
  40215. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  40216. if (ret != MP_VAL)
  40217. return WC_TEST_RET_ENC_EC(ret);
  40218. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  40219. if (ret != MP_VAL)
  40220. return WC_TEST_RET_ENC_EC(ret);
  40221. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  40222. if (ret != MP_OKAY)
  40223. return WC_TEST_RET_ENC_EC(ret);
  40224. #endif
  40225. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  40226. ret = mp_copy(NULL, NULL);
  40227. if (ret != MP_VAL)
  40228. return WC_TEST_RET_ENC_EC(ret);
  40229. ret = mp_copy(a, NULL);
  40230. if (ret != MP_VAL)
  40231. return WC_TEST_RET_ENC_EC(ret);
  40232. ret = mp_copy(NULL, b);
  40233. if (ret != MP_VAL)
  40234. return WC_TEST_RET_ENC_EC(ret);
  40235. #endif
  40236. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  40237. ret = sp_2expt(NULL, 1);
  40238. if (ret != MP_VAL)
  40239. return WC_TEST_RET_ENC_EC(ret);
  40240. #endif
  40241. ret = mp_set(NULL, 0);
  40242. if (ret != MP_VAL)
  40243. return WC_TEST_RET_ENC_EC(ret);
  40244. ret = mp_cmp_d(NULL, 0);
  40245. if (ret != MP_LT)
  40246. return WC_TEST_RET_ENC_EC(ret);
  40247. ret = mp_cmp(NULL, NULL);
  40248. if (ret != MP_EQ)
  40249. return WC_TEST_RET_ENC_NC;
  40250. ret = mp_cmp(a, NULL);
  40251. if (ret != MP_GT)
  40252. return WC_TEST_RET_ENC_NC;
  40253. ret = mp_cmp(NULL, b);
  40254. if (ret != MP_LT)
  40255. return WC_TEST_RET_ENC_NC;
  40256. #ifdef WOLFSSL_SP_MATH_ALL
  40257. mp_rshd(NULL, 1);
  40258. #endif
  40259. mp_zero(NULL);
  40260. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  40261. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40262. ret = mp_lshd(NULL, 0);
  40263. if (ret != MP_VAL)
  40264. return WC_TEST_RET_ENC_EC(ret);
  40265. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  40266. if (ret != MP_VAL)
  40267. return WC_TEST_RET_ENC_EC(ret);
  40268. #endif
  40269. #if defined(WOLFSSL_SP_MATH_ALL)
  40270. ret = mp_div(NULL, NULL, a, b);
  40271. if (ret != MP_VAL)
  40272. return WC_TEST_RET_ENC_EC(ret);
  40273. ret = mp_div(a, NULL, a, b);
  40274. if (ret != MP_VAL)
  40275. return WC_TEST_RET_ENC_EC(ret);
  40276. ret = mp_div(NULL, b, a, b);
  40277. if (ret != MP_VAL)
  40278. return WC_TEST_RET_ENC_EC(ret);
  40279. ret = mp_div(a, b, NULL, NULL);
  40280. if (ret != MP_VAL)
  40281. return WC_TEST_RET_ENC_EC(ret);
  40282. #endif
  40283. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  40284. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  40285. ret = mp_mod(NULL, NULL, NULL);
  40286. if (ret != MP_VAL)
  40287. return WC_TEST_RET_ENC_EC(ret);
  40288. ret = mp_mod(a, NULL, NULL);
  40289. if (ret != MP_VAL)
  40290. return WC_TEST_RET_ENC_EC(ret);
  40291. ret = mp_mod(NULL, b, NULL);
  40292. if (ret != MP_VAL)
  40293. return WC_TEST_RET_ENC_EC(ret);
  40294. ret = mp_mod(NULL, NULL, r);
  40295. if (ret != MP_VAL)
  40296. return WC_TEST_RET_ENC_EC(ret);
  40297. ret = mp_mod(a, b, NULL);
  40298. if (ret != MP_VAL)
  40299. return WC_TEST_RET_ENC_EC(ret);
  40300. ret = mp_mod(a, NULL, r);
  40301. if (ret != MP_VAL)
  40302. return WC_TEST_RET_ENC_EC(ret);
  40303. ret = mp_mod(NULL, b, r);
  40304. if (ret != MP_VAL)
  40305. return WC_TEST_RET_ENC_EC(ret);
  40306. #endif
  40307. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  40308. ret = mp_set_int(NULL, 0);
  40309. if (ret != MP_VAL)
  40310. return WC_TEST_RET_ENC_EC(ret);
  40311. #endif
  40312. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  40313. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  40314. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  40315. if (ret != MP_VAL)
  40316. return WC_TEST_RET_ENC_EC(ret);
  40317. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  40318. if (ret != MP_VAL)
  40319. return WC_TEST_RET_ENC_EC(ret);
  40320. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  40321. if (ret != MP_VAL)
  40322. return WC_TEST_RET_ENC_EC(ret);
  40323. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  40324. if (ret != MP_VAL)
  40325. return WC_TEST_RET_ENC_EC(ret);
  40326. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  40327. if (ret != MP_VAL)
  40328. return WC_TEST_RET_ENC_EC(ret);
  40329. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  40330. if (ret != MP_VAL)
  40331. return WC_TEST_RET_ENC_EC(ret);
  40332. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  40333. if (ret != MP_VAL)
  40334. return WC_TEST_RET_ENC_EC(ret);
  40335. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  40336. if (ret != MP_VAL)
  40337. return WC_TEST_RET_ENC_EC(ret);
  40338. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  40339. if (ret != MP_VAL)
  40340. return WC_TEST_RET_ENC_EC(ret);
  40341. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  40342. if (ret != MP_VAL)
  40343. return WC_TEST_RET_ENC_EC(ret);
  40344. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  40345. if (ret != MP_VAL)
  40346. return WC_TEST_RET_ENC_EC(ret);
  40347. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  40348. if (ret != MP_VAL)
  40349. return WC_TEST_RET_ENC_EC(ret);
  40350. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  40351. if (ret != MP_VAL)
  40352. return WC_TEST_RET_ENC_EC(ret);
  40353. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  40354. if (ret != MP_VAL)
  40355. return WC_TEST_RET_ENC_EC(ret);
  40356. ret = mp_exptmod_nct(a, a, a, NULL);
  40357. if (ret != MP_VAL)
  40358. return WC_TEST_RET_ENC_EC(ret);
  40359. ret = mp_exptmod_nct(a, a, NULL, a);
  40360. if (ret != MP_VAL)
  40361. return WC_TEST_RET_ENC_EC(ret);
  40362. ret = mp_exptmod_nct(a, NULL, a, a);
  40363. if (ret != MP_VAL)
  40364. return WC_TEST_RET_ENC_EC(ret);
  40365. ret = mp_exptmod_nct(NULL, a, a, a);
  40366. if (ret != MP_VAL)
  40367. return WC_TEST_RET_ENC_EC(ret);
  40368. #endif
  40369. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  40370. !defined(WC_NO_RNG)
  40371. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  40372. if (ret != MP_VAL)
  40373. return WC_TEST_RET_ENC_EC(ret);
  40374. ret = mp_rand_prime(a, 32, NULL, NULL);
  40375. if (ret != MP_VAL)
  40376. return WC_TEST_RET_ENC_EC(ret);
  40377. ret = mp_rand_prime(NULL, 32, rng, NULL);
  40378. if (ret != MP_VAL)
  40379. return WC_TEST_RET_ENC_EC(ret);
  40380. ret = mp_rand_prime(a, 0, rng, NULL);
  40381. if (ret != MP_VAL)
  40382. return WC_TEST_RET_ENC_EC(ret);
  40383. #endif
  40384. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40385. ret = mp_mul(NULL, NULL, NULL);
  40386. if (ret != MP_VAL)
  40387. return WC_TEST_RET_ENC_EC(ret);
  40388. ret = mp_mul(a, NULL, NULL);
  40389. if (ret != MP_VAL)
  40390. return WC_TEST_RET_ENC_EC(ret);
  40391. ret = mp_mul(NULL, b, NULL);
  40392. if (ret != MP_VAL)
  40393. return WC_TEST_RET_ENC_EC(ret);
  40394. ret = mp_mul(NULL, NULL, r);
  40395. if (ret != MP_VAL)
  40396. return WC_TEST_RET_ENC_EC(ret);
  40397. ret = mp_mul(a, b, NULL);
  40398. if (ret != MP_VAL)
  40399. return WC_TEST_RET_ENC_EC(ret);
  40400. ret = mp_mul(a, NULL, r);
  40401. if (ret != MP_VAL)
  40402. return WC_TEST_RET_ENC_EC(ret);
  40403. ret = mp_mul(NULL, b, r);
  40404. if (ret != MP_VAL)
  40405. return WC_TEST_RET_ENC_EC(ret);
  40406. #endif
  40407. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  40408. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  40409. ret = mp_sqr(NULL, NULL);
  40410. if (ret != MP_VAL)
  40411. return WC_TEST_RET_ENC_EC(ret);
  40412. ret = mp_sqr(a, NULL);
  40413. if (ret != MP_VAL)
  40414. return WC_TEST_RET_ENC_EC(ret);
  40415. ret = mp_sqr(NULL, r);
  40416. if (ret != MP_VAL)
  40417. return WC_TEST_RET_ENC_EC(ret);
  40418. #endif
  40419. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40420. ret = mp_sqrmod(NULL, NULL, NULL);
  40421. if (ret != MP_VAL)
  40422. return WC_TEST_RET_ENC_EC(ret);
  40423. ret = mp_sqrmod(a, NULL, NULL);
  40424. if (ret != MP_VAL)
  40425. return WC_TEST_RET_ENC_EC(ret);
  40426. ret = mp_sqrmod(NULL, a, NULL);
  40427. if (ret != MP_VAL)
  40428. return WC_TEST_RET_ENC_EC(ret);
  40429. ret = mp_sqrmod(NULL, NULL, a);
  40430. if (ret != MP_VAL)
  40431. return WC_TEST_RET_ENC_EC(ret);
  40432. ret = mp_sqrmod(a, b, NULL);
  40433. if (ret != MP_VAL)
  40434. return WC_TEST_RET_ENC_EC(ret);
  40435. ret = mp_sqrmod(a, NULL, b);
  40436. if (ret != MP_VAL)
  40437. return WC_TEST_RET_ENC_EC(ret);
  40438. ret = mp_sqrmod(NULL, a, b);
  40439. if (ret != MP_VAL)
  40440. return WC_TEST_RET_ENC_EC(ret);
  40441. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  40442. if (ret != MP_VAL)
  40443. return WC_TEST_RET_ENC_EC(ret);
  40444. ret = mp_mulmod(a, NULL, NULL, NULL);
  40445. if (ret != MP_VAL)
  40446. return WC_TEST_RET_ENC_EC(ret);
  40447. ret = mp_mulmod(NULL, a, NULL, NULL);
  40448. if (ret != MP_VAL)
  40449. return WC_TEST_RET_ENC_EC(ret);
  40450. ret = mp_mulmod(NULL, NULL, a, NULL);
  40451. if (ret != MP_VAL)
  40452. return WC_TEST_RET_ENC_EC(ret);
  40453. ret = mp_mulmod(NULL, NULL, NULL, a);
  40454. if (ret != MP_VAL)
  40455. return WC_TEST_RET_ENC_EC(ret);
  40456. ret = mp_mulmod(a, b, b, NULL);
  40457. if (ret != MP_VAL)
  40458. return WC_TEST_RET_ENC_EC(ret);
  40459. ret = mp_mulmod(a, b, NULL, a);
  40460. if (ret != MP_VAL)
  40461. return WC_TEST_RET_ENC_EC(ret);
  40462. ret = mp_mulmod(a, NULL, b, a);
  40463. if (ret != MP_VAL)
  40464. return WC_TEST_RET_ENC_EC(ret);
  40465. ret = mp_mulmod(NULL, b, b, a);
  40466. if (ret != MP_VAL)
  40467. return WC_TEST_RET_ENC_EC(ret);
  40468. #endif
  40469. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  40470. !defined(NO_RSA) || !defined(NO_DSA)
  40471. ret = mp_add_d(NULL, 1, NULL);
  40472. if (ret != MP_VAL)
  40473. return WC_TEST_RET_ENC_EC(ret);
  40474. ret = mp_add_d(a, 1, NULL);
  40475. if (ret != MP_VAL)
  40476. return WC_TEST_RET_ENC_EC(ret);
  40477. ret = mp_add_d(NULL, 1, b);
  40478. if (ret != MP_VAL)
  40479. return WC_TEST_RET_ENC_EC(ret);
  40480. #endif
  40481. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40482. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  40483. ret = mp_sub_d(NULL, 1, NULL);
  40484. if (ret != MP_VAL)
  40485. return WC_TEST_RET_ENC_EC(ret);
  40486. ret = mp_sub_d(a, 1, NULL);
  40487. if (ret != MP_VAL)
  40488. return WC_TEST_RET_ENC_EC(ret);
  40489. ret = mp_sub_d(NULL, 1, b);
  40490. if (ret != MP_VAL)
  40491. return WC_TEST_RET_ENC_EC(ret);
  40492. #endif
  40493. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40494. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  40495. ret = mp_div_d(NULL, 0, NULL, NULL);
  40496. if (ret != MP_VAL)
  40497. return WC_TEST_RET_ENC_EC(ret);
  40498. ret = mp_div_d(a, 0, NULL, NULL);
  40499. if (ret != MP_VAL)
  40500. return WC_TEST_RET_ENC_EC(ret);
  40501. ret = mp_div_d(NULL, 1, NULL, NULL);
  40502. if (ret != MP_VAL)
  40503. return WC_TEST_RET_ENC_EC(ret);
  40504. #endif
  40505. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  40506. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  40507. ret = mp_mod_d(NULL, 0, NULL);
  40508. if (ret != MP_VAL)
  40509. return WC_TEST_RET_ENC_EC(ret);
  40510. ret = mp_mod_d(a, 0, NULL);
  40511. if (ret != MP_VAL)
  40512. return WC_TEST_RET_ENC_EC(ret);
  40513. ret = mp_mod_d(NULL, 0, &rd);
  40514. if (ret != MP_VAL)
  40515. return WC_TEST_RET_ENC_EC(ret);
  40516. #endif
  40517. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  40518. ret = mp_gcd(NULL, NULL, NULL);
  40519. if (ret != MP_VAL)
  40520. return WC_TEST_RET_ENC_EC(ret);
  40521. ret = mp_gcd(a, NULL, NULL);
  40522. if (ret != MP_VAL)
  40523. return WC_TEST_RET_ENC_EC(ret);
  40524. ret = mp_gcd(NULL, a, NULL);
  40525. if (ret != MP_VAL)
  40526. return WC_TEST_RET_ENC_EC(ret);
  40527. ret = mp_gcd(NULL, NULL, a);
  40528. if (ret != MP_VAL)
  40529. return WC_TEST_RET_ENC_EC(ret);
  40530. ret = mp_gcd(a, b, NULL);
  40531. if (ret != MP_VAL)
  40532. return WC_TEST_RET_ENC_EC(ret);
  40533. ret = mp_gcd(a, NULL, b);
  40534. if (ret != MP_VAL)
  40535. return WC_TEST_RET_ENC_EC(ret);
  40536. ret = mp_gcd(NULL, a, b);
  40537. if (ret != MP_VAL)
  40538. return WC_TEST_RET_ENC_EC(ret);
  40539. #endif
  40540. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  40541. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  40542. if (ret != MP_VAL)
  40543. return WC_TEST_RET_ENC_EC(ret);
  40544. ret = mp_div_2_mod_ct(a, NULL, NULL);
  40545. if (ret != MP_VAL)
  40546. return WC_TEST_RET_ENC_EC(ret);
  40547. ret = mp_div_2_mod_ct(NULL, b, NULL);
  40548. if (ret != MP_VAL)
  40549. return WC_TEST_RET_ENC_EC(ret);
  40550. ret = mp_div_2_mod_ct(NULL, NULL, a);
  40551. if (ret != MP_VAL)
  40552. return WC_TEST_RET_ENC_EC(ret);
  40553. ret = mp_div_2_mod_ct(a, b, NULL);
  40554. if (ret != MP_VAL)
  40555. return WC_TEST_RET_ENC_EC(ret);
  40556. ret = mp_div_2_mod_ct(a, b, NULL);
  40557. if (ret != MP_VAL)
  40558. return WC_TEST_RET_ENC_EC(ret);
  40559. ret = mp_div_2_mod_ct(NULL, b, a);
  40560. if (ret != MP_VAL)
  40561. return WC_TEST_RET_ENC_EC(ret);
  40562. ret = mp_div_2(NULL, NULL);
  40563. if (ret != MP_VAL)
  40564. return WC_TEST_RET_ENC_EC(ret);
  40565. ret = mp_div_2(a, NULL);
  40566. if (ret != MP_VAL)
  40567. return WC_TEST_RET_ENC_EC(ret);
  40568. ret = mp_div_2(NULL, a);
  40569. if (ret != MP_VAL)
  40570. return WC_TEST_RET_ENC_EC(ret);
  40571. #endif
  40572. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40573. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  40574. ret = mp_invmod(NULL, NULL, NULL);
  40575. if (ret != MP_VAL)
  40576. return WC_TEST_RET_ENC_EC(ret);
  40577. ret = mp_invmod(a, NULL, NULL);
  40578. if (ret != MP_VAL)
  40579. return WC_TEST_RET_ENC_EC(ret);
  40580. ret = mp_invmod(NULL, b, NULL);
  40581. if (ret != MP_VAL)
  40582. return WC_TEST_RET_ENC_EC(ret);
  40583. ret = mp_invmod(NULL, NULL, a);
  40584. if (ret != MP_VAL)
  40585. return WC_TEST_RET_ENC_EC(ret);
  40586. ret = mp_invmod(a, b, NULL);
  40587. if (ret != MP_VAL)
  40588. return WC_TEST_RET_ENC_EC(ret);
  40589. ret = mp_invmod(a, NULL, a);
  40590. if (ret != MP_VAL)
  40591. return WC_TEST_RET_ENC_EC(ret);
  40592. ret = mp_invmod(NULL, b, a);
  40593. if (ret != MP_VAL)
  40594. return WC_TEST_RET_ENC_EC(ret);
  40595. #endif
  40596. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  40597. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  40598. if (ret != MP_VAL)
  40599. return WC_TEST_RET_ENC_EC(ret);
  40600. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  40601. if (ret != MP_VAL)
  40602. return WC_TEST_RET_ENC_EC(ret);
  40603. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  40604. if (ret != MP_VAL)
  40605. return WC_TEST_RET_ENC_EC(ret);
  40606. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  40607. if (ret != MP_VAL)
  40608. return WC_TEST_RET_ENC_EC(ret);
  40609. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  40610. if (ret != MP_VAL)
  40611. return WC_TEST_RET_ENC_EC(ret);
  40612. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  40613. if (ret != MP_VAL)
  40614. return WC_TEST_RET_ENC_EC(ret);
  40615. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  40616. if (ret != MP_VAL)
  40617. return WC_TEST_RET_ENC_EC(ret);
  40618. #endif
  40619. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  40620. ret = mp_lcm(NULL, NULL, NULL);
  40621. if (ret != MP_VAL)
  40622. return WC_TEST_RET_ENC_EC(ret);
  40623. ret = mp_lcm(a, NULL, NULL);
  40624. if (ret != MP_VAL)
  40625. return WC_TEST_RET_ENC_EC(ret);
  40626. ret = mp_lcm(NULL, b, NULL);
  40627. if (ret != MP_VAL)
  40628. return WC_TEST_RET_ENC_EC(ret);
  40629. ret = mp_lcm(NULL, NULL, a);
  40630. if (ret != MP_VAL)
  40631. return WC_TEST_RET_ENC_EC(ret);
  40632. ret = mp_lcm(a, b, NULL);
  40633. if (ret != MP_VAL)
  40634. return WC_TEST_RET_ENC_EC(ret);
  40635. ret = mp_lcm(a, NULL, a);
  40636. if (ret != MP_VAL)
  40637. return WC_TEST_RET_ENC_EC(ret);
  40638. ret = mp_lcm(NULL, b, a);
  40639. if (ret != MP_VAL)
  40640. return WC_TEST_RET_ENC_EC(ret);
  40641. #endif
  40642. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  40643. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  40644. if (ret != MP_VAL)
  40645. return WC_TEST_RET_ENC_EC(ret);
  40646. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  40647. if (ret != MP_VAL)
  40648. return WC_TEST_RET_ENC_EC(ret);
  40649. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  40650. if (ret != MP_VAL)
  40651. return WC_TEST_RET_ENC_EC(ret);
  40652. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  40653. if (ret != MP_VAL)
  40654. return WC_TEST_RET_ENC_EC(ret);
  40655. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  40656. if (ret != MP_VAL)
  40657. return WC_TEST_RET_ENC_EC(ret);
  40658. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  40659. if (ret != MP_VAL)
  40660. return WC_TEST_RET_ENC_EC(ret);
  40661. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  40662. if (ret != MP_VAL)
  40663. return WC_TEST_RET_ENC_EC(ret);
  40664. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  40665. if (ret != MP_VAL)
  40666. return WC_TEST_RET_ENC_EC(ret);
  40667. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  40668. if (ret != MP_VAL)
  40669. return WC_TEST_RET_ENC_EC(ret);
  40670. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  40671. if (ret != MP_VAL)
  40672. return WC_TEST_RET_ENC_EC(ret);
  40673. ret = mp_exptmod(a, NULL, NULL, NULL);
  40674. if (ret != MP_VAL)
  40675. return WC_TEST_RET_ENC_EC(ret);
  40676. ret = mp_exptmod(NULL, b, NULL, NULL);
  40677. if (ret != MP_VAL)
  40678. return WC_TEST_RET_ENC_EC(ret);
  40679. ret = mp_exptmod(NULL, NULL, b, NULL);
  40680. if (ret != MP_VAL)
  40681. return WC_TEST_RET_ENC_EC(ret);
  40682. ret = mp_exptmod(NULL, NULL, NULL, a);
  40683. if (ret != MP_VAL)
  40684. return WC_TEST_RET_ENC_EC(ret);
  40685. ret = mp_exptmod(a, b, b, NULL);
  40686. if (ret != MP_VAL)
  40687. return WC_TEST_RET_ENC_EC(ret);
  40688. ret = mp_exptmod(a, b, NULL, a);
  40689. if (ret != MP_VAL)
  40690. return WC_TEST_RET_ENC_EC(ret);
  40691. ret = mp_exptmod(a, NULL, b, a);
  40692. if (ret != MP_VAL)
  40693. return WC_TEST_RET_ENC_EC(ret);
  40694. ret = mp_exptmod(NULL, b, b, a);
  40695. if (ret != MP_VAL)
  40696. return WC_TEST_RET_ENC_EC(ret);
  40697. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  40698. if (ret != MP_VAL)
  40699. return WC_TEST_RET_ENC_EC(ret);
  40700. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  40701. if (ret != MP_VAL)
  40702. return WC_TEST_RET_ENC_EC(ret);
  40703. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  40704. if (ret != MP_VAL)
  40705. return WC_TEST_RET_ENC_EC(ret);
  40706. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  40707. if (ret != MP_VAL)
  40708. return WC_TEST_RET_ENC_EC(ret);
  40709. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  40710. if (ret != MP_VAL)
  40711. return WC_TEST_RET_ENC_EC(ret);
  40712. ret = mp_exptmod_nct(a, b, b, NULL);
  40713. if (ret != MP_VAL)
  40714. return WC_TEST_RET_ENC_EC(ret);
  40715. ret = mp_exptmod_nct(a, b, NULL, a);
  40716. if (ret != MP_VAL)
  40717. return WC_TEST_RET_ENC_EC(ret);
  40718. ret = mp_exptmod_nct(a, NULL, b, a);
  40719. if (ret != MP_VAL)
  40720. return WC_TEST_RET_ENC_EC(ret);
  40721. ret = mp_exptmod_nct(NULL, b, b, a);
  40722. if (ret != MP_VAL)
  40723. return WC_TEST_RET_ENC_EC(ret);
  40724. #endif
  40725. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  40726. ret = mp_cnt_lsb(NULL);
  40727. if (ret != 0)
  40728. return WC_TEST_RET_ENC_EC(ret);
  40729. #endif
  40730. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  40731. ret = mp_prime_is_prime(NULL, 1, NULL);
  40732. if (ret != MP_VAL)
  40733. return WC_TEST_RET_ENC_EC(ret);
  40734. ret = mp_prime_is_prime(a, 1, NULL);
  40735. if (ret != MP_VAL)
  40736. return WC_TEST_RET_ENC_EC(ret);
  40737. ret = mp_prime_is_prime(NULL, 1, &result);
  40738. if (ret != MP_VAL)
  40739. return WC_TEST_RET_ENC_EC(ret);
  40740. ret = mp_prime_is_prime(a, 0, &result);
  40741. if (ret != MP_VAL)
  40742. return WC_TEST_RET_ENC_EC(ret);
  40743. ret = mp_prime_is_prime(a, 1024, &result);
  40744. if (ret != MP_VAL)
  40745. return WC_TEST_RET_ENC_EC(ret);
  40746. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  40747. if (ret != MP_VAL)
  40748. return WC_TEST_RET_ENC_EC(ret);
  40749. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  40750. if (ret != MP_VAL)
  40751. return WC_TEST_RET_ENC_EC(ret);
  40752. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  40753. if (ret != MP_VAL)
  40754. return WC_TEST_RET_ENC_EC(ret);
  40755. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  40756. if (ret != MP_VAL)
  40757. return WC_TEST_RET_ENC_EC(ret);
  40758. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  40759. if (ret != MP_VAL)
  40760. return WC_TEST_RET_ENC_EC(ret);
  40761. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  40762. if (ret != MP_VAL)
  40763. return WC_TEST_RET_ENC_EC(ret);
  40764. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  40765. if (ret != MP_VAL)
  40766. return WC_TEST_RET_ENC_EC(ret);
  40767. #endif
  40768. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  40769. ret = mp_exch(NULL, NULL);
  40770. if (ret != MP_VAL)
  40771. return WC_TEST_RET_ENC_EC(ret);
  40772. ret = mp_exch(a, NULL);
  40773. if (ret != MP_VAL)
  40774. return WC_TEST_RET_ENC_EC(ret);
  40775. ret = mp_exch(NULL, b);
  40776. if (ret != MP_VAL)
  40777. return WC_TEST_RET_ENC_EC(ret);
  40778. #endif
  40779. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  40780. defined(WOLFSSL_SP_MATH_ALL)
  40781. ret = mp_mul_d(NULL, 1, NULL);
  40782. if (ret != MP_VAL)
  40783. return WC_TEST_RET_ENC_EC(ret);
  40784. ret = mp_mul_d(a, 1, NULL);
  40785. if (ret != MP_VAL)
  40786. return WC_TEST_RET_ENC_EC(ret);
  40787. ret = mp_mul_d(NULL, 1, b);
  40788. if (ret != MP_VAL)
  40789. return WC_TEST_RET_ENC_EC(ret);
  40790. #endif
  40791. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40792. ret = mp_add(NULL, NULL, NULL);
  40793. if (ret != MP_VAL)
  40794. return WC_TEST_RET_ENC_EC(ret);
  40795. ret = mp_add(a, NULL, NULL);
  40796. if (ret != MP_VAL)
  40797. return WC_TEST_RET_ENC_EC(ret);
  40798. ret = mp_add(NULL, b, NULL);
  40799. if (ret != MP_VAL)
  40800. return WC_TEST_RET_ENC_EC(ret);
  40801. ret = mp_add(NULL, NULL, r);
  40802. if (ret != MP_VAL)
  40803. return WC_TEST_RET_ENC_EC(ret);
  40804. ret = mp_add(a, b, NULL);
  40805. if (ret != MP_VAL)
  40806. return WC_TEST_RET_ENC_EC(ret);
  40807. ret = mp_add(a, NULL, r);
  40808. if (ret != MP_VAL)
  40809. return WC_TEST_RET_ENC_EC(ret);
  40810. ret = mp_add(NULL, b, r);
  40811. if (ret != MP_VAL)
  40812. return WC_TEST_RET_ENC_EC(ret);
  40813. #endif
  40814. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  40815. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  40816. ret = mp_sub(NULL, NULL, NULL);
  40817. if (ret != MP_VAL)
  40818. return WC_TEST_RET_ENC_EC(ret);
  40819. ret = mp_sub(a, NULL, NULL);
  40820. if (ret != MP_VAL)
  40821. return WC_TEST_RET_ENC_EC(ret);
  40822. ret = mp_sub(NULL, b, NULL);
  40823. if (ret != MP_VAL)
  40824. return WC_TEST_RET_ENC_EC(ret);
  40825. ret = mp_sub(NULL, NULL, r);
  40826. if (ret != MP_VAL)
  40827. return WC_TEST_RET_ENC_EC(ret);
  40828. ret = mp_sub(a, b, NULL);
  40829. if (ret != MP_VAL)
  40830. return WC_TEST_RET_ENC_EC(ret);
  40831. ret = mp_sub(a, NULL, r);
  40832. if (ret != MP_VAL)
  40833. return WC_TEST_RET_ENC_EC(ret);
  40834. ret = mp_sub(NULL, b, r);
  40835. if (ret != MP_VAL)
  40836. return WC_TEST_RET_ENC_EC(ret);
  40837. #endif
  40838. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  40839. defined(WOLFSSL_CUSTOM_CURVES))
  40840. ret = mp_addmod(NULL, NULL, NULL, NULL);
  40841. if (ret != MP_VAL)
  40842. return WC_TEST_RET_ENC_EC(ret);
  40843. ret = mp_addmod(a, NULL, NULL, NULL);
  40844. if (ret != MP_VAL)
  40845. return WC_TEST_RET_ENC_EC(ret);
  40846. ret = mp_addmod(NULL, b, NULL, NULL);
  40847. if (ret != MP_VAL)
  40848. return WC_TEST_RET_ENC_EC(ret);
  40849. ret = mp_addmod(NULL, NULL, b, NULL);
  40850. if (ret != MP_VAL)
  40851. return WC_TEST_RET_ENC_EC(ret);
  40852. ret = mp_addmod(NULL, NULL, NULL, a);
  40853. if (ret != MP_VAL)
  40854. return WC_TEST_RET_ENC_EC(ret);
  40855. ret = mp_addmod(a, b, b, NULL);
  40856. if (ret != MP_VAL)
  40857. return WC_TEST_RET_ENC_EC(ret);
  40858. ret = mp_addmod(a, b, NULL, a);
  40859. if (ret != MP_VAL)
  40860. return WC_TEST_RET_ENC_EC(ret);
  40861. ret = mp_addmod(a, NULL, b, a);
  40862. if (ret != MP_VAL)
  40863. return WC_TEST_RET_ENC_EC(ret);
  40864. ret = mp_addmod(NULL, b, b, a);
  40865. if (ret != MP_VAL)
  40866. return WC_TEST_RET_ENC_EC(ret);
  40867. #endif
  40868. #ifdef WOLFSSL_SP_MATH_ALL
  40869. ret = mp_submod(NULL, NULL, NULL, NULL);
  40870. if (ret != MP_VAL)
  40871. return WC_TEST_RET_ENC_EC(ret);
  40872. ret = mp_submod(a, NULL, NULL, NULL);
  40873. if (ret != MP_VAL)
  40874. return WC_TEST_RET_ENC_EC(ret);
  40875. ret = mp_submod(NULL, b, NULL, NULL);
  40876. if (ret != MP_VAL)
  40877. return WC_TEST_RET_ENC_EC(ret);
  40878. ret = mp_submod(NULL, NULL, b, NULL);
  40879. if (ret != MP_VAL)
  40880. return WC_TEST_RET_ENC_EC(ret);
  40881. ret = mp_submod(NULL, NULL, NULL, a);
  40882. if (ret != MP_VAL)
  40883. return WC_TEST_RET_ENC_EC(ret);
  40884. ret = mp_submod(a, b, b, NULL);
  40885. if (ret != MP_VAL)
  40886. return WC_TEST_RET_ENC_EC(ret);
  40887. ret = mp_submod(a, b, NULL, a);
  40888. if (ret != MP_VAL)
  40889. return WC_TEST_RET_ENC_EC(ret);
  40890. ret = mp_submod(a, NULL, b, a);
  40891. if (ret != MP_VAL)
  40892. return WC_TEST_RET_ENC_EC(ret);
  40893. ret = mp_submod(NULL, b, b, a);
  40894. if (ret != MP_VAL)
  40895. return WC_TEST_RET_ENC_EC(ret);
  40896. #endif
  40897. #ifdef WOLFSSL_SP_MATH_ALL
  40898. ret = mp_div_2d(NULL, 1, a, b);
  40899. if (ret != MP_VAL)
  40900. return WC_TEST_RET_ENC_EC(ret);
  40901. ret = mp_mod_2d(NULL, 1, NULL);
  40902. if (ret != MP_VAL)
  40903. return WC_TEST_RET_ENC_EC(ret);
  40904. ret = mp_mod_2d(a, 1, NULL);
  40905. if (ret != MP_VAL)
  40906. return WC_TEST_RET_ENC_EC(ret);
  40907. ret = mp_mod_2d(NULL, 1, b);
  40908. if (ret != MP_VAL)
  40909. return WC_TEST_RET_ENC_EC(ret);
  40910. ret = mp_mul_2d(NULL, 1, NULL);
  40911. if (ret != MP_VAL)
  40912. return WC_TEST_RET_ENC_EC(ret);
  40913. ret = mp_mul_2d(a, 1, NULL);
  40914. if (ret != MP_VAL)
  40915. return WC_TEST_RET_ENC_EC(ret);
  40916. ret = mp_mul_2d(NULL, 1, b);
  40917. if (ret != MP_VAL)
  40918. return WC_TEST_RET_ENC_EC(ret);
  40919. #endif
  40920. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  40921. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  40922. ret = mp_montgomery_reduce(NULL, NULL, 1);
  40923. if (ret != MP_VAL)
  40924. return WC_TEST_RET_ENC_EC(ret);
  40925. ret = mp_montgomery_reduce(a, NULL, 1);
  40926. if (ret != MP_VAL)
  40927. return WC_TEST_RET_ENC_EC(ret);
  40928. ret = mp_montgomery_reduce(NULL, b, 1);
  40929. if (ret != MP_VAL)
  40930. return WC_TEST_RET_ENC_EC(ret);
  40931. mp_zero(b);
  40932. ret = mp_montgomery_reduce(a, b, 1);
  40933. if (ret != MP_VAL)
  40934. return WC_TEST_RET_ENC_EC(ret);
  40935. #endif
  40936. #ifdef WOLFSSL_SP_MATH_ALL
  40937. ret = mp_montgomery_setup(NULL, NULL);
  40938. if (ret != MP_VAL)
  40939. return WC_TEST_RET_ENC_EC(ret);
  40940. ret = mp_montgomery_setup(a, NULL);
  40941. if (ret != MP_VAL)
  40942. return WC_TEST_RET_ENC_EC(ret);
  40943. ret = mp_montgomery_setup(NULL, &rho);
  40944. if (ret != MP_VAL)
  40945. return WC_TEST_RET_ENC_EC(ret);
  40946. ret = mp_montgomery_calc_normalization(NULL, NULL);
  40947. if (ret != MP_VAL)
  40948. return WC_TEST_RET_ENC_EC(ret);
  40949. ret = mp_montgomery_calc_normalization(a, NULL);
  40950. if (ret != MP_VAL)
  40951. return WC_TEST_RET_ENC_EC(ret);
  40952. ret = mp_montgomery_calc_normalization(NULL, b);
  40953. if (ret != MP_VAL)
  40954. return WC_TEST_RET_ENC_EC(ret);
  40955. #endif
  40956. ret = mp_unsigned_bin_size(NULL);
  40957. if (ret != 0)
  40958. return WC_TEST_RET_ENC_EC(ret);
  40959. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  40960. ret = mp_tohex(NULL, NULL);
  40961. if (ret != MP_VAL)
  40962. return WC_TEST_RET_ENC_EC(ret);
  40963. ret = mp_tohex(a, NULL);
  40964. if (ret != MP_VAL)
  40965. return WC_TEST_RET_ENC_EC(ret);
  40966. ret = mp_tohex(NULL, hexStr);
  40967. if (ret != MP_VAL)
  40968. return WC_TEST_RET_ENC_EC(ret);
  40969. #endif
  40970. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  40971. ret = mp_todecimal(NULL, NULL);
  40972. if (ret != MP_VAL)
  40973. return WC_TEST_RET_ENC_EC(ret);
  40974. ret = mp_todecimal(a, NULL);
  40975. if (ret != MP_VAL)
  40976. return WC_TEST_RET_ENC_EC(ret);
  40977. ret = mp_todecimal(NULL, decStr);
  40978. if (ret != MP_VAL)
  40979. return WC_TEST_RET_ENC_EC(ret);
  40980. #endif
  40981. #ifdef WOLFSSL_SP_MATH_ALL
  40982. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  40983. if (ret != MP_VAL)
  40984. return WC_TEST_RET_ENC_EC(ret);
  40985. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  40986. if (ret != MP_VAL)
  40987. return WC_TEST_RET_ENC_EC(ret);
  40988. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  40989. if (ret != MP_VAL)
  40990. return WC_TEST_RET_ENC_EC(ret);
  40991. ret = mp_toradix(a, hexStr, 3);
  40992. if (ret != MP_VAL)
  40993. return WC_TEST_RET_ENC_EC(ret);
  40994. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  40995. if (ret != MP_VAL)
  40996. return WC_TEST_RET_ENC_EC(ret);
  40997. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  40998. if (ret != MP_VAL)
  40999. return WC_TEST_RET_ENC_EC(ret);
  41000. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  41001. if (ret != MP_VAL)
  41002. return WC_TEST_RET_ENC_EC(ret);
  41003. ret = mp_radix_size(a, 3, &size);
  41004. if (ret != MP_VAL)
  41005. return WC_TEST_RET_ENC_EC(ret);
  41006. #endif
  41007. return 0;
  41008. }
  41009. #endif
  41010. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  41011. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  41012. {
  41013. int i, j;
  41014. wc_test_ret_t ret;
  41015. mp_zero(a);
  41016. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41017. if (mp_is_bit_set(a, i))
  41018. return WC_TEST_RET_ENC_NC;
  41019. for (j = 0; j < i; j++) {
  41020. if (!mp_is_bit_set(a, j))
  41021. return WC_TEST_RET_ENC_NC;
  41022. }
  41023. if (mp_set_bit(a, i) != 0)
  41024. return WC_TEST_RET_ENC_NC;
  41025. if (!mp_is_bit_set(a, i))
  41026. return WC_TEST_RET_ENC_NC;
  41027. }
  41028. mp_zero(a);
  41029. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41030. if (mp_is_bit_set(a, i))
  41031. return WC_TEST_RET_ENC_NC;
  41032. }
  41033. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41034. mp_zero(a);
  41035. if (mp_set_bit(a, i) != 0)
  41036. return WC_TEST_RET_ENC_NC;
  41037. for (j = 0; j < i; j++) {
  41038. if (mp_is_bit_set(a, j))
  41039. return WC_TEST_RET_ENC_NC;
  41040. }
  41041. if (!mp_is_bit_set(a, i))
  41042. return WC_TEST_RET_ENC_NC;
  41043. }
  41044. #ifdef WOLFSSL_KEY_GEN
  41045. for (i = 0; i < DIGIT_BIT * 2; i++) {
  41046. mp_set(a, 1);
  41047. ret = mp_2expt(a, i);
  41048. if (ret != 0)
  41049. return WC_TEST_RET_ENC_EC(ret);
  41050. for (j = 0; j < i; j++) {
  41051. if (mp_is_bit_set(a, j))
  41052. return WC_TEST_RET_ENC_NC;
  41053. }
  41054. if (!mp_is_bit_set(a, i))
  41055. return WC_TEST_RET_ENC_NC;
  41056. }
  41057. #endif
  41058. #ifdef WOLFSSL_SP_MATH
  41059. mp_zero(a);
  41060. for (j = 1; j <= 3; j++) {
  41061. i = SP_INT_MAX_BITS - j;
  41062. if (mp_is_bit_set(a, i))
  41063. return WC_TEST_RET_ENC_NC;
  41064. if (mp_set_bit(a, i) != 0)
  41065. return WC_TEST_RET_ENC_NC;
  41066. if (!mp_is_bit_set(a, i))
  41067. return WC_TEST_RET_ENC_NC;
  41068. #ifdef WOLFSSL_KEY_GEN
  41069. ret = mp_2expt(a, i);
  41070. if (ret != 0)
  41071. return WC_TEST_RET_ENC_EC(ret);
  41072. if (!mp_is_bit_set(a, i))
  41073. return WC_TEST_RET_ENC_NC;
  41074. #endif
  41075. }
  41076. mp_zero(a);
  41077. for (j = 0; j <= 3; j++) {
  41078. i = SP_INT_MAX_BITS + j;
  41079. if (mp_is_bit_set(a, i))
  41080. return WC_TEST_RET_ENC_NC;
  41081. if (mp_set_bit(a, i) != MP_VAL)
  41082. return WC_TEST_RET_ENC_NC;
  41083. #ifdef WOLFSSL_KEY_GEN
  41084. ret = mp_2expt(a, i);
  41085. if (ret != MP_VAL)
  41086. return WC_TEST_RET_ENC_EC(ret);
  41087. #endif
  41088. }
  41089. #endif
  41090. (void)ret;
  41091. return 0;
  41092. }
  41093. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  41094. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  41095. {
  41096. wc_test_ret_t ret;
  41097. mp_zero(a);
  41098. mp_zero(b);
  41099. ret = mp_cmp_d(a, 0);
  41100. if (ret != MP_EQ)
  41101. return WC_TEST_RET_ENC_EC(ret);
  41102. ret = mp_cmp_d(a, 1);
  41103. if (ret != MP_LT)
  41104. return WC_TEST_RET_ENC_EC(ret);
  41105. ret = mp_cmp(a, b);
  41106. if (ret != MP_EQ)
  41107. return WC_TEST_RET_ENC_NC;
  41108. mp_set(a, 1);
  41109. ret = mp_cmp_d(a, 0);
  41110. if (ret != MP_GT)
  41111. return WC_TEST_RET_ENC_EC(ret);
  41112. ret = mp_cmp_d(a, 1);
  41113. if (ret != MP_EQ)
  41114. return WC_TEST_RET_ENC_EC(ret);
  41115. ret = mp_cmp_d(a, 2);
  41116. if (ret != MP_LT)
  41117. return WC_TEST_RET_ENC_EC(ret);
  41118. ret = mp_cmp(a, b);
  41119. if (ret != MP_GT)
  41120. return WC_TEST_RET_ENC_NC;
  41121. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  41122. ret = mp_cmp_d(b, -1);
  41123. if (ret != MP_GT)
  41124. return WC_TEST_RET_ENC_EC(ret);
  41125. ret = mp_cmp(a, b);
  41126. if (ret != MP_LT)
  41127. return WC_TEST_RET_ENC_NC;
  41128. ret = mp_cmp(b, a);
  41129. if (ret != MP_GT)
  41130. return WC_TEST_RET_ENC_NC;
  41131. ret = mp_cmp(b, b);
  41132. if (ret != MP_EQ)
  41133. return WC_TEST_RET_ENC_NC;
  41134. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  41135. defined(WOLFSSL_SP_INT_NEGATIVE)
  41136. mp_read_radix(a, "-1", MP_RADIX_HEX);
  41137. mp_read_radix(a, "1", MP_RADIX_HEX);
  41138. ret = mp_cmp(a, b);
  41139. if (ret != MP_LT)
  41140. return WC_TEST_RET_ENC_NC;
  41141. ret = mp_cmp(b, a);
  41142. if (ret != MP_GT)
  41143. return WC_TEST_RET_ENC_NC;
  41144. mp_read_radix(b, "-2", MP_RADIX_HEX);
  41145. ret = mp_cmp(a, b);
  41146. if (ret != MP_GT)
  41147. return WC_TEST_RET_ENC_NC;
  41148. ret = mp_cmp(b, a);
  41149. if (ret != MP_LT)
  41150. return WC_TEST_RET_ENC_NC;
  41151. mp_read_radix(a, "-2", MP_RADIX_HEX);
  41152. ret = mp_cmp(a, b);
  41153. if (ret != MP_EQ)
  41154. return WC_TEST_RET_ENC_NC;
  41155. #endif
  41156. return 0;
  41157. }
  41158. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  41159. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  41160. {
  41161. wc_test_ret_t ret;
  41162. int i, j, k;
  41163. #ifndef WOLFSSL_SP_MATH
  41164. for (i = 0; i < 10; i++) {
  41165. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  41166. ret = randNum(a, j, rng, NULL);
  41167. if (ret != MP_OKAY)
  41168. return WC_TEST_RET_ENC_EC(ret);
  41169. mp_copy(a, b);
  41170. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  41171. ret = mp_mul_2d(a, k, a);
  41172. if (ret != MP_OKAY)
  41173. return WC_TEST_RET_ENC_EC(ret);
  41174. mp_rshb(a, k);
  41175. ret = mp_cmp(a, b);
  41176. if (ret != MP_EQ)
  41177. return WC_TEST_RET_ENC_NC;
  41178. }
  41179. }
  41180. }
  41181. #endif
  41182. for (i = 0; i < 10; i++) {
  41183. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  41184. ret = randNum(a, j, rng, NULL);
  41185. if (ret != MP_OKAY)
  41186. return WC_TEST_RET_ENC_EC(ret);
  41187. mp_copy(a, b);
  41188. for (k = 0; k < 10; k++) {
  41189. ret = mp_lshd(a, k);
  41190. if (ret != MP_OKAY)
  41191. return WC_TEST_RET_ENC_EC(ret);
  41192. #ifndef WOLFSSL_SP_MATH
  41193. mp_rshd(a, k);
  41194. #else
  41195. mp_rshb(a, k * SP_WORD_SIZE);
  41196. #endif
  41197. ret = mp_cmp(a, b);
  41198. if (ret != MP_EQ)
  41199. return WC_TEST_RET_ENC_NC;
  41200. }
  41201. }
  41202. }
  41203. #ifndef WOLFSSL_SP_MATH
  41204. mp_zero(a);
  41205. mp_rshd(a, 1);
  41206. if (!mp_iszero(a))
  41207. return WC_TEST_RET_ENC_NC;
  41208. mp_set(a, 1);
  41209. mp_rshd(a, 1);
  41210. if (!mp_iszero(a))
  41211. return WC_TEST_RET_ENC_NC;
  41212. mp_set(a, 1);
  41213. mp_rshd(a, 2);
  41214. if (!mp_iszero(a))
  41215. return WC_TEST_RET_ENC_NC;
  41216. #endif
  41217. return 0;
  41218. }
  41219. #endif
  41220. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  41221. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  41222. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  41223. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  41224. WC_RNG* rng)
  41225. {
  41226. wc_test_ret_t ret;
  41227. int i, j, k;
  41228. mp_zero(a);
  41229. mp_zero(d);
  41230. ret = mp_div(a, d, r, rem);
  41231. if (ret != MP_VAL)
  41232. return WC_TEST_RET_ENC_EC(ret);
  41233. mp_set(d, 1);
  41234. ret = mp_div(a, d, r, rem);
  41235. if (ret != MP_OKAY)
  41236. return WC_TEST_RET_ENC_EC(ret);
  41237. if (!mp_iszero(r))
  41238. return WC_TEST_RET_ENC_EC(ret);
  41239. if (!mp_iszero(rem))
  41240. return WC_TEST_RET_ENC_EC(ret);
  41241. mp_set(a, 1);
  41242. ret = mp_div(a, d, r, rem);
  41243. if (ret != MP_OKAY)
  41244. return WC_TEST_RET_ENC_EC(ret);
  41245. if (!mp_isone(r))
  41246. return WC_TEST_RET_ENC_EC(ret);
  41247. if (!mp_iszero(rem))
  41248. return WC_TEST_RET_ENC_EC(ret);
  41249. for (i = 0; i < 100; i++) {
  41250. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  41251. ret = randNum(d, j, rng, NULL);
  41252. if (ret != MP_OKAY)
  41253. return WC_TEST_RET_ENC_EC(ret);
  41254. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  41255. ret = randNum(a, k, rng, NULL);
  41256. if (ret != MP_OKAY)
  41257. return WC_TEST_RET_ENC_EC(ret);
  41258. ret = mp_div(a, d, NULL, rem);
  41259. if (ret != MP_OKAY)
  41260. return WC_TEST_RET_ENC_EC(ret);
  41261. ret = mp_div(a, d, r, NULL);
  41262. if (ret != MP_OKAY)
  41263. return WC_TEST_RET_ENC_EC(ret);
  41264. ret = mp_div(a, d, r, rem);
  41265. if (ret != MP_OKAY)
  41266. return WC_TEST_RET_ENC_EC(ret);
  41267. mp_mul(r, d, r);
  41268. mp_add(r, rem, r);
  41269. if (mp_cmp(r, a) != MP_EQ)
  41270. return WC_TEST_RET_ENC_NC;
  41271. }
  41272. }
  41273. }
  41274. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  41275. if (ret != MP_OKAY)
  41276. return WC_TEST_RET_ENC_EC(ret);
  41277. mp_add(d, d, a);
  41278. mp_set(rem, 1);
  41279. mp_div(a, d, NULL, rem);
  41280. if (ret != MP_OKAY)
  41281. return WC_TEST_RET_ENC_EC(ret);
  41282. if (!mp_iszero(rem))
  41283. return WC_TEST_RET_ENC_EC(ret);
  41284. mp_set(r, 1);
  41285. mp_div(a, d, r, NULL);
  41286. if (ret != MP_OKAY)
  41287. return WC_TEST_RET_ENC_EC(ret);
  41288. if (mp_cmp_d(r, 2) != MP_EQ)
  41289. return WC_TEST_RET_ENC_EC(ret);
  41290. mp_set(r, 1);
  41291. mp_set(rem, 1);
  41292. mp_div(a, d, r, rem);
  41293. if (ret != MP_OKAY)
  41294. return WC_TEST_RET_ENC_EC(ret);
  41295. if (mp_cmp_d(r, 2) != MP_EQ)
  41296. return WC_TEST_RET_ENC_EC(ret);
  41297. if (!mp_iszero(rem))
  41298. return WC_TEST_RET_ENC_EC(ret);
  41299. mp_set(a, 0xfe);
  41300. mp_lshd(a, 3);
  41301. mp_add_d(a, 0xff, a);
  41302. mp_set(d, 0xfe);
  41303. mp_lshd(d, 2);
  41304. ret = mp_div(a, d, r, rem);
  41305. if (ret != MP_OKAY)
  41306. return WC_TEST_RET_ENC_EC(ret);
  41307. mp_mul(r, d, d);
  41308. mp_add(rem, d, d);
  41309. if (mp_cmp(a, d) != MP_EQ)
  41310. return WC_TEST_RET_ENC_NC;
  41311. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  41312. * a digit. So mp_div must detect and handle.
  41313. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  41314. */
  41315. mp_zero(a);
  41316. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  41317. mp_zero(d);
  41318. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  41319. mp_add_d(d, 1, d);
  41320. ret = mp_div(a, d, r, rem);
  41321. if (ret != MP_OKAY)
  41322. return WC_TEST_RET_ENC_EC(ret);
  41323. /* Make sure [d | d] / d is handled. */
  41324. mp_zero(a);
  41325. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  41326. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  41327. mp_zero(d);
  41328. mp_set_bit(d, DIGIT_BIT - 1);
  41329. ret = mp_div(a, d, r, rem);
  41330. if (ret != MP_OKAY)
  41331. return WC_TEST_RET_ENC_EC(ret);
  41332. mp_zero(a);
  41333. mp_set_bit(a, DIGIT_BIT);
  41334. mp_set_bit(a, 0);
  41335. mp_zero(d);
  41336. if (mp_cmp(r, a) != MP_EQ)
  41337. return WC_TEST_RET_ENC_NC;
  41338. if (mp_cmp(rem, d) != MP_EQ)
  41339. return WC_TEST_RET_ENC_NC;
  41340. return 0;
  41341. }
  41342. #endif
  41343. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  41344. !defined(WC_NO_RNG)
  41345. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  41346. {
  41347. wc_test_ret_t ret;
  41348. int res;
  41349. ret = mp_rand_prime(a, 1, rng, NULL);
  41350. #if defined(WOLFSSL_SP_MATH_ALL)
  41351. if (ret != 0)
  41352. return WC_TEST_RET_ENC_EC(ret);
  41353. #else
  41354. if (ret != MP_VAL)
  41355. return WC_TEST_RET_ENC_NC;
  41356. #endif
  41357. #ifndef WOLFSSL_SP_MATH
  41358. ret = mp_rand_prime(a, -5, rng, NULL);
  41359. if (ret != 0 || (a->dp[0] & 3) != 3)
  41360. return WC_TEST_RET_ENC_NC;
  41361. #endif
  41362. ret = mp_prime_is_prime(a, 1, &res);
  41363. if (ret != MP_OKAY)
  41364. return WC_TEST_RET_ENC_EC(ret);
  41365. #ifndef WOLFSSL_SP_MATH
  41366. if (res != MP_YES)
  41367. return WC_TEST_RET_ENC_EC(res);
  41368. #else
  41369. if (res != MP_NO)
  41370. return WC_TEST_RET_ENC_EC(res);
  41371. #endif
  41372. ret = mp_prime_is_prime(a, 0, &res);
  41373. if (ret != MP_VAL)
  41374. return WC_TEST_RET_ENC_EC(ret);
  41375. ret = mp_prime_is_prime(a, -1, &res);
  41376. if (ret != MP_VAL)
  41377. return WC_TEST_RET_ENC_EC(ret);
  41378. ret = mp_prime_is_prime(a, 257, &res);
  41379. if (ret != MP_VAL)
  41380. return WC_TEST_RET_ENC_EC(ret);
  41381. mp_set(a, 1);
  41382. ret = mp_prime_is_prime(a, 1, &res);
  41383. if (ret != MP_OKAY)
  41384. return WC_TEST_RET_ENC_EC(ret);
  41385. if (res != MP_NO)
  41386. return WC_TEST_RET_ENC_EC(res);
  41387. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  41388. if (ret != MP_OKAY)
  41389. return WC_TEST_RET_ENC_EC(ret);
  41390. if (res != MP_NO)
  41391. return WC_TEST_RET_ENC_EC(res);
  41392. mp_set(a, 2);
  41393. ret = mp_prime_is_prime(a, 1, &res);
  41394. if (ret != MP_OKAY)
  41395. return WC_TEST_RET_ENC_EC(ret);
  41396. if (res != MP_YES)
  41397. return WC_TEST_RET_ENC_EC(res);
  41398. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  41399. if (ret != MP_OKAY)
  41400. return WC_TEST_RET_ENC_EC(ret);
  41401. if (res != MP_YES)
  41402. return WC_TEST_RET_ENC_EC(res);
  41403. mp_set(a, 0xfb);
  41404. ret = mp_prime_is_prime(a, 1, &res);
  41405. if (ret != MP_OKAY)
  41406. return WC_TEST_RET_ENC_EC(ret);
  41407. if (res != MP_YES)
  41408. return WC_TEST_RET_ENC_EC(res);
  41409. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  41410. if (ret != MP_OKAY)
  41411. return WC_TEST_RET_ENC_EC(ret);
  41412. if (res != MP_YES)
  41413. return WC_TEST_RET_ENC_EC(res);
  41414. mp_set(a, 0x6);
  41415. ret = mp_prime_is_prime(a, 1, &res);
  41416. if (ret != MP_OKAY)
  41417. return WC_TEST_RET_ENC_EC(ret);
  41418. if (res != MP_NO)
  41419. return WC_TEST_RET_ENC_EC(res);
  41420. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  41421. if (ret != MP_OKAY)
  41422. return WC_TEST_RET_ENC_EC(ret);
  41423. if (res != MP_NO)
  41424. return WC_TEST_RET_ENC_EC(res);
  41425. mp_set_int(a, 0x655 * 0x65b);
  41426. ret = mp_prime_is_prime(a, 10, &res);
  41427. if (ret != MP_OKAY)
  41428. return WC_TEST_RET_ENC_EC(ret);
  41429. if (res != MP_NO)
  41430. return WC_TEST_RET_ENC_EC(res);
  41431. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  41432. if (ret != MP_OKAY)
  41433. return WC_TEST_RET_ENC_EC(ret);
  41434. if (res != MP_NO)
  41435. return WC_TEST_RET_ENC_EC(res);
  41436. return 0;
  41437. }
  41438. #endif
  41439. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  41440. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  41441. WC_RNG* rng)
  41442. {
  41443. wc_test_ret_t ret;
  41444. int i;
  41445. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  41446. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  41447. { 12, 56, 168 }, { 56, 12, 168 }
  41448. };
  41449. (void)exp;
  41450. mp_set(a, 0);
  41451. mp_set(b, 1);
  41452. ret = mp_lcm(a, a, r);
  41453. if (ret != MP_VAL)
  41454. return WC_TEST_RET_ENC_EC(ret);
  41455. ret = mp_lcm(a, b, r);
  41456. if (ret != MP_VAL)
  41457. return WC_TEST_RET_ENC_EC(ret);
  41458. ret = mp_lcm(b, a, r);
  41459. if (ret != MP_VAL)
  41460. return WC_TEST_RET_ENC_EC(ret);
  41461. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  41462. mp_set(a, kat[i][0]);
  41463. mp_set(b, kat[i][1]);
  41464. ret = mp_lcm(a, b, r);
  41465. if (ret != MP_OKAY)
  41466. return WC_TEST_RET_ENC_EC(ret);
  41467. mp_set(exp, kat[i][2]);
  41468. ret = mp_cmp(r, exp);
  41469. if (ret != MP_EQ)
  41470. return WC_TEST_RET_ENC_NC;
  41471. }
  41472. (void)rng;
  41473. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  41474. !defined(WC_NO_RNG)
  41475. ret = mp_rand_prime(a, 20, rng, NULL);
  41476. if (ret != MP_OKAY)
  41477. return WC_TEST_RET_ENC_EC(ret);
  41478. ret = mp_rand_prime(b, 20, rng, NULL);
  41479. if (ret != MP_OKAY)
  41480. return WC_TEST_RET_ENC_EC(ret);
  41481. ret = mp_mul(a, b, exp);
  41482. if (ret != MP_OKAY)
  41483. return WC_TEST_RET_ENC_EC(ret);
  41484. ret = mp_lcm(a, b, r);
  41485. if (ret != MP_OKAY)
  41486. return WC_TEST_RET_ENC_EC(ret);
  41487. ret = mp_cmp(r, exp);
  41488. if (ret != MP_EQ)
  41489. return WC_TEST_RET_ENC_NC;
  41490. ret = mp_lcm(b, a, r);
  41491. if (ret != MP_OKAY)
  41492. return WC_TEST_RET_ENC_EC(ret);
  41493. ret = mp_cmp(r, exp);
  41494. if (ret != MP_EQ)
  41495. return WC_TEST_RET_ENC_NC;
  41496. #endif
  41497. mp_set(a, 11);
  41498. mp_zero(b);
  41499. ret = mp_gcd(a, b, r);
  41500. if (ret != MP_OKAY)
  41501. return WC_TEST_RET_ENC_EC(ret);
  41502. ret = mp_cmp_d(r, 11);
  41503. if (ret != MP_EQ)
  41504. return WC_TEST_RET_ENC_EC(ret);
  41505. ret = mp_gcd(b, a, r);
  41506. if (ret != MP_OKAY)
  41507. return WC_TEST_RET_ENC_EC(ret);
  41508. ret = mp_cmp_d(r, 11);
  41509. if (ret != MP_EQ)
  41510. return WC_TEST_RET_ENC_EC(ret);
  41511. ret = mp_gcd(b, b, r);
  41512. if (ret != MP_VAL)
  41513. return WC_TEST_RET_ENC_EC(ret);
  41514. return 0;
  41515. }
  41516. #endif
  41517. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  41518. defined(WOLFSSL_SP_MATH_ALL)
  41519. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  41520. {
  41521. wc_test_ret_t ret;
  41522. int i;
  41523. int j;
  41524. mp_set(a, 10);
  41525. ret = mp_mod_2d(a, 0, r);
  41526. if (ret != MP_OKAY)
  41527. return WC_TEST_RET_ENC_EC(ret);
  41528. if (!mp_iszero(r))
  41529. return WC_TEST_RET_ENC_NC;
  41530. ret = mp_mod_2d(a, 1, r);
  41531. if (ret != MP_OKAY)
  41532. return WC_TEST_RET_ENC_EC(ret);
  41533. if (!mp_iszero(r))
  41534. return WC_TEST_RET_ENC_NC;
  41535. ret = mp_mod_2d(a, 2, r);
  41536. if (ret != MP_OKAY)
  41537. return WC_TEST_RET_ENC_EC(ret);
  41538. ret = mp_cmp_d(r, 2);
  41539. if (ret != 0)
  41540. return WC_TEST_RET_ENC_EC(ret);
  41541. for (i = 2; i < 20; i++) {
  41542. ret = randNum(a, i, rng, NULL);
  41543. if (ret != 0)
  41544. return WC_TEST_RET_ENC_EC(ret);
  41545. for (j = 1; j <= mp_count_bits(a); j++) {
  41546. /* Get top part */
  41547. ret = mp_div_2d(a, j, t, NULL);
  41548. if (ret != 0)
  41549. return WC_TEST_RET_ENC_EC(ret);
  41550. ret = mp_mul_2d(t, j, t);
  41551. if (ret != 0)
  41552. return WC_TEST_RET_ENC_EC(ret);
  41553. /* Get bottom part */
  41554. ret = mp_mod_2d(a, j, r);
  41555. if (ret != 0)
  41556. return WC_TEST_RET_ENC_EC(ret);
  41557. /* Reassemble */
  41558. ret = mp_add(t, r, r);
  41559. if (ret != 0)
  41560. return WC_TEST_RET_ENC_EC(ret);
  41561. ret = mp_cmp(a, r);
  41562. if (ret != MP_EQ)
  41563. return WC_TEST_RET_ENC_NC;
  41564. }
  41565. }
  41566. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  41567. /* Test negative value being moded. */
  41568. for (j = 0; j < 20; j++) {
  41569. ret = randNum(a, 2, rng, NULL);
  41570. if (ret != 0)
  41571. return WC_TEST_RET_ENC_EC(ret);
  41572. a->sign = MP_NEG;
  41573. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  41574. ret = mp_mod_2d(a, i, r);
  41575. if (ret != 0)
  41576. return WC_TEST_RET_ENC_EC(ret);
  41577. mp_zero(t);
  41578. ret = mp_set_bit(t, i);
  41579. if (ret != 0)
  41580. return WC_TEST_RET_ENC_EC(ret);
  41581. ret = mp_mod(a, t, t);
  41582. if (ret != 0)
  41583. return WC_TEST_RET_ENC_EC(ret);
  41584. ret = mp_cmp(r, t);
  41585. if (ret != MP_EQ)
  41586. return WC_TEST_RET_ENC_NC;
  41587. }
  41588. }
  41589. #endif
  41590. return 0;
  41591. }
  41592. #endif
  41593. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  41594. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  41595. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  41596. {
  41597. wc_test_ret_t ret;
  41598. mp_digit r;
  41599. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  41600. mp_digit rem;
  41601. int i;
  41602. #endif
  41603. (void)rng;
  41604. ret = mp_set(a, 1);
  41605. if (ret != MP_OKAY)
  41606. return WC_TEST_RET_ENC_EC(ret);
  41607. ret = mp_mod_d(a, 0, &r);
  41608. if (ret != MP_VAL)
  41609. return WC_TEST_RET_ENC_EC(ret);
  41610. mp_zero(a);
  41611. ret = mp_mod_d(a, 1, &r);
  41612. if (ret != MP_OKAY)
  41613. return WC_TEST_RET_ENC_EC(ret);
  41614. ret = mp_mod_d(a, 3, &r);
  41615. if (ret != MP_OKAY)
  41616. return WC_TEST_RET_ENC_EC(ret);
  41617. ret = mp_mod_d(a, 5, &r);
  41618. if (ret != MP_OKAY)
  41619. return WC_TEST_RET_ENC_EC(ret);
  41620. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  41621. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  41622. ret = randNum(a, i, rng, NULL);
  41623. if (ret != MP_OKAY)
  41624. return WC_TEST_RET_ENC_EC(ret);
  41625. ret = mp_mod_d(a, 3, &r);
  41626. if (ret != MP_OKAY)
  41627. return WC_TEST_RET_ENC_EC(ret);
  41628. ret = mp_div_d(a, 3, a, &rem);
  41629. if (ret != MP_OKAY)
  41630. return WC_TEST_RET_ENC_EC(ret);
  41631. if (r != rem)
  41632. return WC_TEST_RET_ENC_NC;
  41633. }
  41634. #endif
  41635. return 0;
  41636. }
  41637. #endif
  41638. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  41639. WC_RNG* rng)
  41640. {
  41641. wc_test_ret_t ret;
  41642. int i;
  41643. for (i = 1; i < 16; i++) {
  41644. ret = randNum(a, i, rng, NULL);
  41645. if (ret != 0)
  41646. return WC_TEST_RET_ENC_EC(ret);
  41647. ret = mp_mul(a, a, r1);
  41648. if (ret != 0)
  41649. return WC_TEST_RET_ENC_EC(ret);
  41650. ret = mp_sqr(a, r2);
  41651. if (ret != 0)
  41652. return WC_TEST_RET_ENC_EC(ret);
  41653. ret = mp_cmp(r1, r2);
  41654. if (ret != MP_EQ)
  41655. return WC_TEST_RET_ENC_NC;
  41656. }
  41657. ret = mp_set(b, 0);
  41658. if (ret != MP_OKAY)
  41659. return WC_TEST_RET_ENC_EC(ret);
  41660. ret = mp_mul(a, b, r1);
  41661. if (ret != MP_OKAY)
  41662. return WC_TEST_RET_ENC_EC(ret);
  41663. if (!mp_iszero(r1))
  41664. return WC_TEST_RET_ENC_EC(ret);
  41665. ret = mp_sqr(b, r1);
  41666. if (ret != MP_OKAY)
  41667. return WC_TEST_RET_ENC_EC(ret);
  41668. if (!mp_iszero(r1))
  41669. return WC_TEST_RET_ENC_NC;
  41670. #ifdef WOLFSSL_SP_MATH_ALL
  41671. ret = mp_set(a, 1);
  41672. if (ret != MP_OKAY)
  41673. return WC_TEST_RET_ENC_EC(ret);
  41674. i = (SP_INT_DIGITS / 2) + 1;
  41675. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  41676. if (ret != MP_OKAY)
  41677. return WC_TEST_RET_ENC_EC(ret);
  41678. ret = mp_set(b, 1);
  41679. if (ret != MP_OKAY)
  41680. return WC_TEST_RET_ENC_EC(ret);
  41681. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  41682. if (ret != MP_OKAY)
  41683. return WC_TEST_RET_ENC_EC(ret);
  41684. ret = mp_mul(a, b, r1);
  41685. if (ret != MP_OKAY)
  41686. return WC_TEST_RET_ENC_EC(ret);
  41687. ret = mp_mul(a, a, r1);
  41688. if (ret == MP_OKAY)
  41689. return WC_TEST_RET_ENC_NC;
  41690. ret = mp_sqr(a, r1);
  41691. if (ret == MP_OKAY)
  41692. return WC_TEST_RET_ENC_NC;
  41693. ret = mp_sqr(b, r1);
  41694. if (ret != MP_OKAY)
  41695. return WC_TEST_RET_ENC_EC(ret);
  41696. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  41697. (defined(HAVE_ECC) && defined(FP_ECC))
  41698. ret = mp_mulmod(a, b, b, r1);
  41699. if (ret != MP_OKAY)
  41700. return WC_TEST_RET_ENC_EC(ret);
  41701. ret = mp_mulmod(a, a, b, r1);
  41702. if (ret == MP_OKAY)
  41703. return WC_TEST_RET_ENC_NC;
  41704. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  41705. ret = mp_sqrmod(a, b, r1);
  41706. if (ret == MP_OKAY)
  41707. return WC_TEST_RET_ENC_NC;
  41708. ret = mp_sqrmod(b, a, r1);
  41709. if (ret != MP_OKAY)
  41710. return WC_TEST_RET_ENC_EC(ret);
  41711. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  41712. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  41713. #endif /* WOLFSSL_SP_MATH_ALL */
  41714. return 0;
  41715. }
  41716. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  41717. defined(OPENSSL_EXTRA)
  41718. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  41719. {
  41720. wc_test_ret_t ret;
  41721. mp_set(a, 0);
  41722. mp_set(m, 1);
  41723. ret = mp_invmod(a, m, r);
  41724. if (ret != MP_VAL)
  41725. return WC_TEST_RET_ENC_EC(ret);
  41726. ret = mp_invmod(m, a, r);
  41727. if (ret != MP_VAL)
  41728. return WC_TEST_RET_ENC_EC(ret);
  41729. mp_set(a, 2);
  41730. mp_set(m, 4);
  41731. ret = mp_invmod(a, m, r);
  41732. if (ret != MP_VAL)
  41733. return WC_TEST_RET_ENC_EC(ret);
  41734. mp_set(a, 3);
  41735. mp_set(m, 6);
  41736. ret = mp_invmod(a, m, r);
  41737. if (ret != MP_VAL)
  41738. return WC_TEST_RET_ENC_EC(ret);
  41739. mp_set(a, 5*9);
  41740. mp_set(m, 6*9);
  41741. ret = mp_invmod(a, m, r);
  41742. if (ret != MP_VAL)
  41743. return WC_TEST_RET_ENC_EC(ret);
  41744. mp_set(a, 1);
  41745. mp_set(m, 4);
  41746. ret = mp_invmod(a, m, r);
  41747. if (ret != MP_OKAY)
  41748. return WC_TEST_RET_ENC_EC(ret);
  41749. if (!mp_isone(r))
  41750. return WC_TEST_RET_ENC_NC;
  41751. mp_set(a, 3);
  41752. mp_set(m, 4);
  41753. ret = mp_invmod(a, m, r);
  41754. if (ret != MP_OKAY)
  41755. return WC_TEST_RET_ENC_EC(ret);
  41756. ret = mp_cmp_d(r, 3);
  41757. if (ret != 0)
  41758. return WC_TEST_RET_ENC_EC(ret);
  41759. mp_set(a, 3);
  41760. mp_set(m, 5);
  41761. ret = mp_invmod(a, m, r);
  41762. if (ret != MP_OKAY)
  41763. return WC_TEST_RET_ENC_EC(ret);
  41764. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  41765. /* Maximum 'a' */
  41766. mp_set(a, 0);
  41767. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  41768. mp_sub_d(a, 1, a);
  41769. /* Modulus too big. */
  41770. mp_set(m, 0);
  41771. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  41772. ret = mp_invmod(a, m, r);
  41773. if (ret != MP_VAL)
  41774. return WC_TEST_RET_ENC_EC(ret);
  41775. /* Maximum modulus - even. */
  41776. mp_set(m, 0);
  41777. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  41778. ret = mp_invmod(a, m, r);
  41779. if (ret != MP_OKAY)
  41780. return WC_TEST_RET_ENC_EC(ret);
  41781. #endif
  41782. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  41783. mp_read_radix(a, "-3", 16);
  41784. ret = mp_invmod(a, m, r);
  41785. if (ret != MP_OKAY)
  41786. return WC_TEST_RET_ENC_EC(ret);
  41787. #endif
  41788. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  41789. mp_set(a, 0);
  41790. mp_set(m, 3);
  41791. ret = mp_invmod_mont_ct(a, m, r, 1);
  41792. if (ret != MP_VAL)
  41793. return WC_TEST_RET_ENC_EC(ret);
  41794. mp_set(a, 1);
  41795. mp_set(m, 0);
  41796. ret = mp_invmod_mont_ct(a, m, r, 1);
  41797. if (ret != MP_VAL)
  41798. return WC_TEST_RET_ENC_EC(ret);
  41799. mp_set(a, 1);
  41800. mp_set(m, 1);
  41801. ret = mp_invmod_mont_ct(a, m, r, 1);
  41802. if (ret != MP_VAL)
  41803. return WC_TEST_RET_ENC_EC(ret);
  41804. mp_set(a, 1);
  41805. mp_set(m, 2);
  41806. ret = mp_invmod_mont_ct(a, m, r, 1);
  41807. if (ret != MP_VAL)
  41808. return WC_TEST_RET_ENC_EC(ret);
  41809. mp_set(a, 1);
  41810. mp_set(m, 3);
  41811. ret = mp_invmod_mont_ct(a, m, r, 1);
  41812. if (ret != MP_OKAY)
  41813. return WC_TEST_RET_ENC_EC(ret);
  41814. #endif
  41815. return 0;
  41816. }
  41817. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  41818. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  41819. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  41820. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  41821. {
  41822. wc_test_ret_t ret;
  41823. mp_set(b, 0x2);
  41824. mp_set(e, 0x3);
  41825. mp_set(m, 0x0);
  41826. ret = mp_exptmod_ex(b, e, 1, m, r);
  41827. if (ret != MP_VAL)
  41828. return WC_TEST_RET_ENC_EC(ret);
  41829. ret = mp_exptmod_nct(b, e, m, r);
  41830. if (ret != MP_VAL)
  41831. return WC_TEST_RET_ENC_EC(ret);
  41832. mp_set(b, 0x2);
  41833. mp_set(e, 0x3);
  41834. mp_set(m, 0x1);
  41835. ret = mp_exptmod_ex(b, e, 1, m, r);
  41836. if (ret != MP_OKAY)
  41837. return WC_TEST_RET_ENC_EC(ret);
  41838. if (!mp_iszero(r))
  41839. return WC_TEST_RET_ENC_NC;
  41840. ret = mp_exptmod_nct(b, e, m, r);
  41841. if (ret != MP_OKAY)
  41842. return WC_TEST_RET_ENC_EC(ret);
  41843. if (!mp_iszero(r))
  41844. return WC_TEST_RET_ENC_NC;
  41845. mp_set(b, 0x2);
  41846. mp_set(e, 0x0);
  41847. mp_set(m, 0x7);
  41848. ret = mp_exptmod_ex(b, e, 1, m, r);
  41849. if (ret != MP_OKAY)
  41850. return WC_TEST_RET_ENC_EC(ret);
  41851. if (!mp_isone(r))
  41852. return WC_TEST_RET_ENC_NC;
  41853. ret = mp_exptmod_nct(b, e, m, r);
  41854. if (ret != MP_OKAY)
  41855. return WC_TEST_RET_ENC_EC(ret);
  41856. if (!mp_isone(r))
  41857. return WC_TEST_RET_ENC_NC;
  41858. mp_set(b, 0x0);
  41859. mp_set(e, 0x3);
  41860. mp_set(m, 0x7);
  41861. ret = mp_exptmod_ex(b, e, 1, m, r);
  41862. if (ret != MP_OKAY)
  41863. return WC_TEST_RET_ENC_EC(ret);
  41864. if (!mp_iszero(r))
  41865. return WC_TEST_RET_ENC_NC;
  41866. ret = mp_exptmod_nct(b, e, m, r);
  41867. if (ret != MP_OKAY)
  41868. return WC_TEST_RET_ENC_EC(ret);
  41869. if (!mp_iszero(r))
  41870. return WC_TEST_RET_ENC_NC;
  41871. mp_set(b, 0x10);
  41872. mp_set(e, 0x3);
  41873. mp_set(m, 0x7);
  41874. ret = mp_exptmod_ex(b, e, 1, m, r);
  41875. if (ret != MP_OKAY)
  41876. return WC_TEST_RET_ENC_EC(ret);
  41877. ret = mp_exptmod_nct(b, e, m, r);
  41878. if (ret != MP_OKAY)
  41879. return WC_TEST_RET_ENC_EC(ret);
  41880. mp_set(b, 0x7);
  41881. mp_set(e, 0x3);
  41882. mp_set(m, 0x7);
  41883. ret = mp_exptmod_ex(b, e, 1, m, r);
  41884. if (ret != MP_OKAY)
  41885. return WC_TEST_RET_ENC_EC(ret);
  41886. if (!mp_iszero(r))
  41887. return WC_TEST_RET_ENC_NC;
  41888. ret = mp_exptmod_nct(b, e, m, r);
  41889. if (ret != MP_OKAY)
  41890. return WC_TEST_RET_ENC_EC(ret);
  41891. if (!mp_iszero(r))
  41892. return WC_TEST_RET_ENC_NC;
  41893. #ifndef WOLFSSL_SP_MATH
  41894. mp_set(b, 0x01);
  41895. mp_mul_2d(b, DIGIT_BIT, b);
  41896. mp_add_d(b, 1, b);
  41897. mp_set(e, 0x3);
  41898. mp_copy(b, m);
  41899. ret = mp_exptmod_ex(b, e, 1, m, r);
  41900. if (ret != MP_OKAY)
  41901. return WC_TEST_RET_ENC_EC(ret);
  41902. if (!mp_iszero(r))
  41903. return WC_TEST_RET_ENC_NC;
  41904. ret = mp_exptmod_nct(b, e, m, r);
  41905. if (ret != MP_OKAY)
  41906. return WC_TEST_RET_ENC_EC(ret);
  41907. if (!mp_iszero(r))
  41908. return WC_TEST_RET_ENC_NC;
  41909. #endif
  41910. mp_set(b, 0x2);
  41911. mp_set(e, 0x3);
  41912. mp_set(m, 0x7);
  41913. ret = mp_exptmod_ex(b, e, 1, m, r);
  41914. if (ret != MP_OKAY)
  41915. return WC_TEST_RET_ENC_EC(ret);
  41916. ret = mp_exptmod_nct(b, e, m, r);
  41917. if (ret != MP_OKAY)
  41918. return WC_TEST_RET_ENC_EC(ret);
  41919. #ifdef WOLFSSL_SP_MATH_ALL
  41920. mp_set(b, 0x2);
  41921. mp_set(e, 0x3);
  41922. mp_set(m, 0x01);
  41923. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  41924. mp_add_d(m, 0x01, m);
  41925. ret = mp_exptmod_ex(b, e, 1, m, r);
  41926. if (ret != MP_VAL)
  41927. return WC_TEST_RET_ENC_EC(ret);
  41928. ret = mp_exptmod_nct(b, e, m, r);
  41929. if (ret != MP_VAL)
  41930. return WC_TEST_RET_ENC_EC(ret);
  41931. #endif
  41932. return 0;
  41933. }
  41934. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  41935. * OPENSSL_EXTRA */
  41936. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  41937. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  41938. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  41939. {
  41940. wc_test_ret_t ret;
  41941. mp_digit mp;
  41942. static int exp[] = { 7, 8, 16, 27, 32, 64,
  41943. 127, 128, 255, 256,
  41944. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  41945. 383, 384, 2033, 2048
  41946. #endif
  41947. };
  41948. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  41949. 0x01, 0x9f, 0x13, 0xbd,
  41950. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  41951. 0x1f, 0x13d, 0x45, 0x615
  41952. #endif
  41953. };
  41954. int bits[] = { 256, 384,
  41955. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  41956. 2048,
  41957. #endif
  41958. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  41959. 3072
  41960. #endif
  41961. };
  41962. int i;
  41963. int j;
  41964. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  41965. if (exp[i] >= DIGIT_BIT)
  41966. continue;
  41967. mp_zero(m);
  41968. ret = mp_set_bit(m, exp[i]);
  41969. if (ret != MP_OKAY)
  41970. return WC_TEST_RET_ENC_EC(ret);
  41971. ret = mp_sub_d(m, sub[i], m);
  41972. if (ret != MP_OKAY)
  41973. return WC_TEST_RET_ENC_EC(ret);
  41974. ret = mp_montgomery_setup(m, &mp);
  41975. if (ret != MP_OKAY)
  41976. return WC_TEST_RET_ENC_EC(ret);
  41977. ret = mp_montgomery_calc_normalization(n, m);
  41978. if (ret != MP_OKAY)
  41979. return WC_TEST_RET_ENC_EC(ret);
  41980. for (j = 0; j < 10; j++) {
  41981. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  41982. if (ret != 0)
  41983. return WC_TEST_RET_ENC_EC(ret);
  41984. ret = mp_mod(a, m, a);
  41985. if (ret != 0)
  41986. return WC_TEST_RET_ENC_EC(ret);
  41987. /* r = a * a */
  41988. ret = mp_sqrmod(a, m, r);
  41989. if (ret != MP_OKAY)
  41990. return WC_TEST_RET_ENC_EC(ret);
  41991. /* Convert to Montgomery form = a*n */
  41992. ret = mp_mulmod(a, n, m, a);
  41993. if (ret != MP_OKAY)
  41994. return WC_TEST_RET_ENC_EC(ret);
  41995. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  41996. ret = mp_sqr(a, a);
  41997. if (ret != MP_OKAY)
  41998. return WC_TEST_RET_ENC_EC(ret);
  41999. ret = mp_montgomery_reduce(a, m, mp);
  42000. if (ret != MP_OKAY)
  42001. return WC_TEST_RET_ENC_EC(ret);
  42002. ret = mp_montgomery_reduce(a, m, mp);
  42003. if (ret != MP_OKAY)
  42004. return WC_TEST_RET_ENC_EC(ret);
  42005. if (mp_cmp(a, r) != MP_EQ)
  42006. return WC_TEST_RET_ENC_NC;
  42007. }
  42008. }
  42009. /* Force carries. */
  42010. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  42011. /* a = 2^(bits*2) - 1 */
  42012. mp_zero(a);
  42013. mp_set_bit(a, bits[i] * 2);
  42014. mp_sub_d(a, 1, a);
  42015. /* m = 2^(bits) - 1 */
  42016. mp_zero(m);
  42017. mp_set_bit(m, bits[i]);
  42018. mp_sub_d(m, 1, m);
  42019. mp = 1;
  42020. /* result = r = 2^(bits) - 1 */
  42021. mp_zero(r);
  42022. mp_set_bit(r, bits[i]);
  42023. mp_sub_d(r, 1, r);
  42024. ret = mp_montgomery_reduce(a, m, mp);
  42025. if (ret != MP_OKAY)
  42026. return WC_TEST_RET_ENC_EC(ret);
  42027. /* Result is m or 0 if reduced to range of modulus. */
  42028. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  42029. return WC_TEST_RET_ENC_NC;
  42030. }
  42031. return 0;
  42032. }
  42033. #endif
  42034. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  42035. {
  42036. WC_RNG rng;
  42037. int rng_inited = 0;
  42038. wc_test_ret_t ret;
  42039. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  42040. int i, j;
  42041. #ifndef WOLFSSL_SP_MATH
  42042. int k;
  42043. #endif
  42044. mp_digit d = 0;
  42045. #endif
  42046. #ifdef WOLFSSL_SMALL_STACK
  42047. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42048. DYNAMIC_TYPE_TMP_BUFFER),
  42049. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42050. DYNAMIC_TYPE_TMP_BUFFER),
  42051. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42052. DYNAMIC_TYPE_TMP_BUFFER),
  42053. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42054. DYNAMIC_TYPE_TMP_BUFFER),
  42055. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42056. DYNAMIC_TYPE_TMP_BUFFER);
  42057. if ((a == NULL) ||
  42058. (b == NULL) ||
  42059. (r1 == NULL) ||
  42060. (r2 == NULL) ||
  42061. (p == NULL))
  42062. {
  42063. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  42064. }
  42065. #else
  42066. mp_int a[1], b[1], r1[1], r2[1], p[1];
  42067. #endif
  42068. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  42069. if (ret != 0)
  42070. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42071. #ifdef WOLFSSL_SP_MATH_ALL
  42072. mp_init_copy(p, a);
  42073. #else
  42074. ret = mp_init(p);
  42075. if (ret != 0)
  42076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42077. #endif
  42078. #ifndef HAVE_FIPS
  42079. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42080. #else
  42081. ret = wc_InitRng(&rng);
  42082. #endif
  42083. if (ret != 0)
  42084. goto done;
  42085. rng_inited = 1;
  42086. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  42087. mp_set_int(a, 0);
  42088. if (a->used != 0 || a->dp[0] != 0)
  42089. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42090. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  42091. for (i = 0; i < 4 * j; i++) {
  42092. /* New values to use. */
  42093. ret = randNum(p, j, &rng, NULL);
  42094. if (ret != 0)
  42095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42096. ret = randNum(a, j, &rng, NULL);
  42097. if (ret != 0)
  42098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42099. ret = randNum(b, j, &rng, NULL);
  42100. if (ret != 0)
  42101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42102. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  42103. if (ret != 0)
  42104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42105. d &= MP_MASK;
  42106. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  42107. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  42108. /* Ensure sqrmod produce same result as mulmod. */
  42109. ret = mp_sqrmod(a, p, r1);
  42110. if (ret != 0)
  42111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42112. ret = mp_mulmod(a, a, p, r2);
  42113. if (ret != 0)
  42114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42115. if (mp_cmp(r1, r2) != 0)
  42116. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42117. #endif
  42118. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  42119. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  42120. !defined(WOLFSSL_SP_INT_NEGATIVE))
  42121. ret = mp_addmod(a, b, p, r1);
  42122. if (ret != 0)
  42123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42124. ret = mp_submod(r1, b, p, r2);
  42125. if (ret != 0)
  42126. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42127. ret = mp_mod(a, p, r1);
  42128. if (ret != 0)
  42129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42130. if (mp_cmp(r1, r2) != MP_EQ)
  42131. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42132. #else
  42133. /* Ensure add with mod produce same result as sub with mod. */
  42134. ret = mp_addmod(a, b, p, r1);
  42135. if (ret != 0)
  42136. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42137. b->sign ^= 1;
  42138. ret = mp_submod(a, b, p, r2);
  42139. if (ret != 0)
  42140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42141. if (mp_cmp(r1, r2) != 0)
  42142. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42143. #endif
  42144. #endif
  42145. /* Ensure add digit produce same result as sub digit. */
  42146. ret = mp_add_d(a, d, r1);
  42147. if (ret != 0)
  42148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42149. ret = mp_sub_d(r1, d, r2);
  42150. if (ret != 0)
  42151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42152. if (mp_cmp(a, r2) != 0)
  42153. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42154. /* Invert - if p is even it will use the slow impl.
  42155. * - if p and a are even it will fail.
  42156. */
  42157. ret = mp_invmod(a, p, r1);
  42158. if (ret != 0 && ret != MP_VAL)
  42159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42160. #ifndef WOLFSSL_SP_MATH
  42161. /* Shift up and down number all bits in a digit. */
  42162. for (k = 0; k < DIGIT_BIT; k++) {
  42163. mp_mul_2d(a, k, r1);
  42164. mp_div_2d(r1, k, r2, p);
  42165. if (mp_cmp(a, r2) != 0)
  42166. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42167. if (!mp_iszero(p))
  42168. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42169. mp_rshb(r1, k);
  42170. if (mp_cmp(a, r1) != 0)
  42171. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42172. }
  42173. #endif
  42174. }
  42175. }
  42176. /* Test adding and subtracting zero from zero. */
  42177. mp_zero(a);
  42178. ret = mp_add_d(a, 0, r1);
  42179. if (ret != 0)
  42180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42181. if (!mp_iszero(r1)) {
  42182. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42183. }
  42184. ret = mp_sub_d(a, 0, r2);
  42185. if (ret != 0)
  42186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42187. if (!mp_iszero(r2)) {
  42188. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42189. }
  42190. #if DIGIT_BIT >= 32
  42191. /* Check that setting a 32-bit digit works. */
  42192. d &= 0xffffffffU;
  42193. mp_set_int(a, d);
  42194. if (a->used != 1 || a->dp[0] != d)
  42195. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42196. #endif
  42197. /* Check setting a bit and testing a bit works. */
  42198. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  42199. mp_zero(a);
  42200. mp_set_bit(a, i);
  42201. if (!mp_is_bit_set(a, i))
  42202. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42203. }
  42204. #endif
  42205. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  42206. mp_zero(a);
  42207. i = mp_cnt_lsb(a);
  42208. if (i != 0)
  42209. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42210. mp_set(a, 1);
  42211. i = mp_cnt_lsb(a);
  42212. if (i != 0)
  42213. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42214. mp_set(a, 32);
  42215. i = mp_cnt_lsb(a);
  42216. if (i != 5)
  42217. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42218. mp_zero(a);
  42219. mp_set_bit(a, 129);
  42220. i = mp_cnt_lsb(a);
  42221. if (i != 129)
  42222. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42223. #endif
  42224. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  42225. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  42226. goto done;
  42227. #endif
  42228. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  42229. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  42230. goto done;
  42231. #endif
  42232. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  42233. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42234. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  42235. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  42236. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  42237. goto done;
  42238. #endif
  42239. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  42240. defined(HAVE_ECC))
  42241. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  42242. goto done;
  42243. #endif
  42244. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  42245. goto done;
  42246. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  42247. goto done;
  42248. if ((ret = mp_test_read_to_bin(a)) != 0)
  42249. goto done;
  42250. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  42251. if ((ret = mp_test_set_int(a)) != 0)
  42252. goto done;
  42253. #endif
  42254. if ((ret = mp_test_cmp(a, r1)) != 0)
  42255. goto done;
  42256. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  42257. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  42258. goto done;
  42259. #endif
  42260. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  42261. if ((ret = mp_test_set_is_bit(a)) != 0)
  42262. goto done;
  42263. #endif
  42264. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  42265. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  42266. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  42267. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  42268. goto done;
  42269. #endif
  42270. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  42271. !defined(WC_NO_RNG)
  42272. if ((ret = mp_test_prime(a, &rng)) != 0)
  42273. goto done;
  42274. #endif
  42275. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  42276. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  42277. goto done;
  42278. #endif
  42279. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  42280. defined(WOLFSSL_SP_MATH_ALL)
  42281. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  42282. goto done;
  42283. #endif
  42284. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  42285. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  42286. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  42287. goto done;
  42288. #endif
  42289. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  42290. goto done;
  42291. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  42292. defined(OPENSSL_EXTRA)
  42293. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  42294. goto done;
  42295. #endif
  42296. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  42297. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  42298. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  42299. goto done;
  42300. #endif
  42301. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  42302. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  42303. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  42304. goto done;
  42305. #endif
  42306. done:
  42307. #ifdef WOLFSSL_SMALL_STACK
  42308. if (p) {
  42309. mp_clear(p);
  42310. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42311. }
  42312. if (r2) {
  42313. mp_clear(r2);
  42314. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42315. }
  42316. if (r1) {
  42317. mp_clear(r1);
  42318. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42319. }
  42320. if (b) {
  42321. mp_clear(b);
  42322. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42323. }
  42324. if (a) {
  42325. mp_clear(a);
  42326. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42327. }
  42328. #else
  42329. mp_clear(p);
  42330. mp_clear(r2);
  42331. mp_clear(r1);
  42332. mp_clear(b);
  42333. mp_clear(a);
  42334. #endif
  42335. if (rng_inited)
  42336. wc_FreeRng(&rng);
  42337. return ret;
  42338. }
  42339. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  42340. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  42341. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  42342. typedef struct pairs_t {
  42343. const unsigned char* coeff;
  42344. int coeffSz;
  42345. int exp;
  42346. } pairs_t;
  42347. /*
  42348. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  42349. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  42350. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  42351. */
  42352. static const unsigned char c192a[] =
  42353. {
  42354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  42355. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  42356. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  42357. };
  42358. static const unsigned char c0a[] =
  42359. {
  42360. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  42361. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  42362. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  42363. };
  42364. static const pairs_t ecPairsA[] =
  42365. {
  42366. {c192a, sizeof(c192a), 192},
  42367. {c0a, sizeof(c0a), 0}
  42368. };
  42369. static const int kA[] = {173, 293};
  42370. static const unsigned char controlPrime[] = {
  42371. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  42372. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  42373. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  42374. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  42375. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  42376. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  42377. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  42378. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  42379. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  42380. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  42381. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  42382. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  42383. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  42384. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  42385. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  42386. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  42387. };
  42388. static const unsigned char testOne[] = { 1 };
  42389. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  42390. {
  42391. wc_test_ret_t ret;
  42392. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42393. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42394. if (ki == NULL)
  42395. return MEMORY_E;
  42396. #else
  42397. mp_int ki[1];
  42398. #endif
  42399. ret = mp_init(ki);
  42400. if (ret != 0)
  42401. ret = WC_TEST_RET_ENC_EC(ret);
  42402. if (ret == 0) {
  42403. ret = mp_set(ki, k);
  42404. if (ret != 0)
  42405. ret = WC_TEST_RET_ENC_EC(ret);
  42406. }
  42407. if (ret == 0) {
  42408. ret = mp_sub_d(p1, 1, p2);
  42409. if (ret != 0)
  42410. ret = WC_TEST_RET_ENC_EC(ret);
  42411. }
  42412. if (ret == 0) {
  42413. ret = mp_mul(p2, ki, p2);
  42414. if (ret != 0)
  42415. ret = WC_TEST_RET_ENC_EC(ret);
  42416. }
  42417. if (ret == 0) {
  42418. ret = mp_add_d(p2, 1, p2);
  42419. if (ret != 0)
  42420. ret = WC_TEST_RET_ENC_EC(ret);
  42421. }
  42422. mp_clear(ki);
  42423. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42424. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42425. #endif
  42426. return ret;
  42427. }
  42428. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  42429. const pairs_t* ecPairs, int ecPairsSz,
  42430. const int* k)
  42431. {
  42432. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42433. mp_int *x = NULL, *y = NULL;
  42434. #else
  42435. mp_int x[1], y[1];
  42436. #endif
  42437. wc_test_ret_t ret;
  42438. int i;
  42439. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42440. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  42441. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  42442. ret = MEMORY_E;
  42443. goto out;
  42444. }
  42445. #endif
  42446. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  42447. if (ret != 0) {
  42448. ret = WC_TEST_RET_ENC_EC(ret);
  42449. goto out;
  42450. }
  42451. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  42452. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  42453. if (ret != 0) {
  42454. ret = WC_TEST_RET_ENC_EC(ret);
  42455. break;
  42456. }
  42457. /* p1 = 2^exp */
  42458. ret = mp_2expt(y, ecPairs[i].exp);
  42459. if (ret != 0) {
  42460. ret = WC_TEST_RET_ENC_EC(ret);
  42461. break;
  42462. }
  42463. /* p1 = p1 * m */
  42464. ret = mp_mul(x, y, x);
  42465. if (ret != 0) {
  42466. ret = WC_TEST_RET_ENC_EC(ret);
  42467. break;
  42468. }
  42469. /* p1 += */
  42470. ret = mp_add(p1, x, p1);
  42471. if (ret != 0) {
  42472. ret = WC_TEST_RET_ENC_EC(ret);
  42473. break;
  42474. }
  42475. mp_zero(x);
  42476. mp_zero(y);
  42477. }
  42478. if (ret == 0)
  42479. ret = GenerateNextP(p1, p2, k[0]);
  42480. if (ret == 0)
  42481. ret = GenerateNextP(p1, p3, k[1]);
  42482. out:
  42483. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42484. if (x != NULL) {
  42485. mp_clear(x);
  42486. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42487. }
  42488. if (y != NULL) {
  42489. mp_clear(y);
  42490. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42491. }
  42492. #else
  42493. mp_clear(x);
  42494. mp_clear(y);
  42495. #endif
  42496. return ret;
  42497. }
  42498. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  42499. {
  42500. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42501. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  42502. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  42503. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  42504. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42505. #else
  42506. mp_int n[1],
  42507. p1[1],
  42508. p2[1],
  42509. p3[1];
  42510. #endif
  42511. wc_test_ret_t ret;
  42512. int isPrime = 0;
  42513. WC_RNG rng;
  42514. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42515. if ((n == NULL) ||
  42516. (p1 == NULL) ||
  42517. (p2 == NULL) ||
  42518. (p3 == NULL))
  42519. ERROR_OUT(MEMORY_E, out);
  42520. #endif
  42521. ret = wc_InitRng(&rng);
  42522. if (ret != 0)
  42523. ret = WC_TEST_RET_ENC_EC(ret);
  42524. if (ret == 0) {
  42525. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  42526. if (ret != 0)
  42527. ret = WC_TEST_RET_ENC_EC(ret);
  42528. }
  42529. if (ret == 0)
  42530. ret = GenerateP(p1, p2, p3,
  42531. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  42532. if (ret == 0) {
  42533. ret = mp_mul(p1, p2, n);
  42534. if (ret != 0)
  42535. ret = WC_TEST_RET_ENC_EC(ret);
  42536. }
  42537. if (ret == 0) {
  42538. ret = mp_mul(n, p3, n);
  42539. if (ret != 0)
  42540. ret = WC_TEST_RET_ENC_EC(ret);
  42541. }
  42542. if (ret != 0)
  42543. ERROR_OUT(ret, out);
  42544. /* Check the old prime test using the number that false positives.
  42545. * This test result should indicate as not prime. */
  42546. ret = mp_prime_is_prime(n, 40, &isPrime);
  42547. if (ret != 0)
  42548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42549. if (isPrime)
  42550. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42551. /* This test result should fail. It should indicate the value as prime. */
  42552. ret = mp_prime_is_prime(n, 8, &isPrime);
  42553. if (ret != 0)
  42554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42555. if (!isPrime)
  42556. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42557. /* This test result should indicate the value as not prime. */
  42558. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  42559. if (ret != 0)
  42560. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42561. if (isPrime)
  42562. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42563. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  42564. if (ret != 0)
  42565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42566. /* This test result should indicate the value as prime. */
  42567. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  42568. if (ret != 0)
  42569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42570. if (!isPrime)
  42571. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42572. /* This test result should indicate the value as prime. */
  42573. isPrime = -1;
  42574. ret = mp_prime_is_prime(n, 8, &isPrime);
  42575. if (ret != 0)
  42576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42577. if (!isPrime)
  42578. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42579. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  42580. if (ret != 0)
  42581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42582. /* This test result should indicate the value as not prime. */
  42583. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  42584. if (ret != 0)
  42585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42586. if (isPrime)
  42587. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42588. ret = mp_prime_is_prime(n, 8, &isPrime);
  42589. if (ret != 0)
  42590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42591. if (isPrime)
  42592. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42593. ret = 0;
  42594. out:
  42595. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42596. if (n != NULL) {
  42597. mp_clear(n);
  42598. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42599. }
  42600. if (p1 != NULL) {
  42601. mp_clear(p1);
  42602. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42603. }
  42604. if (p2 != NULL) {
  42605. mp_clear(p2);
  42606. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42607. }
  42608. if (p3 != NULL) {
  42609. mp_clear(p3);
  42610. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42611. }
  42612. #else
  42613. mp_clear(p3);
  42614. mp_clear(p2);
  42615. mp_clear(p1);
  42616. mp_clear(n);
  42617. #endif
  42618. wc_FreeRng(&rng);
  42619. return ret;
  42620. }
  42621. #endif /* WOLFSSL_PUBLIC_MP */
  42622. #if defined(ASN_BER_TO_DER) && \
  42623. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  42624. defined(OPENSSL_EXTRA_X509_SMALL))
  42625. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  42626. typedef struct berDerTestData {
  42627. const byte *in;
  42628. word32 inSz;
  42629. const byte *out;
  42630. word32 outSz;
  42631. } berDerTestData;
  42632. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  42633. {
  42634. wc_test_ret_t ret;
  42635. int i;
  42636. word32 len = 0, l;
  42637. byte out[32];
  42638. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  42639. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  42640. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  42641. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  42642. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  42643. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  42644. };
  42645. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  42646. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  42647. 0x30, 0x80,
  42648. 0x02, 0x01, 0x01,
  42649. 0x30, 0x80,
  42650. 0x24, 0x80,
  42651. 0x04, 0x01, 0x01,
  42652. 0x04, 0x02, 0x02, 0x03,
  42653. 0x00, 0x00,
  42654. 0x06, 0x01, 0x01,
  42655. 0x00, 0x00,
  42656. 0x31, 0x80,
  42657. 0x06, 0x01, 0x01,
  42658. 0x00, 0x00,
  42659. 0x00, 0x00,
  42660. };
  42661. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  42662. 0x30, 0x12,
  42663. 0x02, 0x01, 0x01,
  42664. 0x30, 0x08,
  42665. 0x04, 0x03, 0x01, 0x02, 0x03,
  42666. 0x06, 0x01, 0x01,
  42667. 0x31, 0x03,
  42668. 0x06, 0x01, 0x01
  42669. };
  42670. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  42671. berDerTestData testData[] = {
  42672. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  42673. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  42674. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  42675. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  42676. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  42677. };
  42678. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  42679. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  42680. if (ret != LENGTH_ONLY_E)
  42681. return WC_TEST_RET_ENC_I(i);
  42682. if (len != testData[i].outSz)
  42683. return WC_TEST_RET_ENC_I(i);
  42684. len = testData[i].outSz;
  42685. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  42686. if (ret != 0)
  42687. return WC_TEST_RET_ENC_I(i);
  42688. if (XMEMCMP(out, testData[i].out, len) != 0)
  42689. return WC_TEST_RET_ENC_I(i);
  42690. for (l = 1; l < testData[i].inSz; l++) {
  42691. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  42692. if (ret != ASN_PARSE_E)
  42693. return WC_TEST_RET_ENC_EC(ret);
  42694. len = testData[i].outSz;
  42695. ret = wc_BerToDer(testData[i].in, l, out, &len);
  42696. if (ret != ASN_PARSE_E)
  42697. return WC_TEST_RET_ENC_EC(ret);
  42698. }
  42699. for (l = 0; l < testData[i].outSz-1; l++) {
  42700. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  42701. if (ret != BUFFER_E)
  42702. return WC_TEST_RET_ENC_EC(ret);
  42703. }
  42704. }
  42705. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  42706. if (ret != BAD_FUNC_ARG)
  42707. return WC_TEST_RET_ENC_EC(ret);
  42708. ret = wc_BerToDer(out, 4, NULL, NULL);
  42709. if (ret != BAD_FUNC_ARG)
  42710. return WC_TEST_RET_ENC_EC(ret);
  42711. ret = wc_BerToDer(NULL, 4, NULL, &len);
  42712. if (ret != BAD_FUNC_ARG)
  42713. return WC_TEST_RET_ENC_EC(ret);
  42714. ret = wc_BerToDer(NULL, 4, out, NULL);
  42715. if (ret != BAD_FUNC_ARG)
  42716. return WC_TEST_RET_ENC_EC(ret);
  42717. ret = wc_BerToDer(out, 4, out, NULL);
  42718. if (ret != BAD_FUNC_ARG)
  42719. return WC_TEST_RET_ENC_EC(ret);
  42720. ret = wc_BerToDer(NULL, 4, out, &len);
  42721. if (ret != BAD_FUNC_ARG)
  42722. return WC_TEST_RET_ENC_EC(ret);
  42723. for (l = 1; l < sizeof(good4_out); l++) {
  42724. len = l;
  42725. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  42726. if (ret != BUFFER_E)
  42727. return WC_TEST_RET_ENC_EC(ret);
  42728. }
  42729. return 0;
  42730. }
  42731. #endif
  42732. #ifdef DEBUG_WOLFSSL
  42733. static THREAD_LS_T int log_cnt = 0;
  42734. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  42735. {
  42736. (void)logLevel;
  42737. (void)logMessage;
  42738. log_cnt++;
  42739. }
  42740. #endif /* DEBUG_WOLFSSL */
  42741. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  42742. {
  42743. wc_test_ret_t ret;
  42744. #ifdef DEBUG_WOLFSSL
  42745. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  42746. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  42747. byte b[256];
  42748. int i;
  42749. for (i = 0; i < (int)sizeof(b); i++)
  42750. b[i] = i;
  42751. ret = wolfSSL_Debugging_ON();
  42752. if (ret != 0)
  42753. return WC_TEST_RET_ENC_EC(ret);
  42754. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  42755. if (ret != 0)
  42756. return WC_TEST_RET_ENC_EC(ret);
  42757. WOLFSSL_MSG(msg);
  42758. WOLFSSL_BUFFER(a, sizeof(a));
  42759. WOLFSSL_BUFFER(b, sizeof(b));
  42760. WOLFSSL_BUFFER(NULL, 0);
  42761. WOLFSSL_ERROR(MEMORY_E);
  42762. WOLFSSL_ERROR_MSG(msg);
  42763. /* turn off logs */
  42764. wolfSSL_Debugging_OFF();
  42765. /* capture log count */
  42766. i = log_cnt;
  42767. /* validate no logs are output when disabled */
  42768. WOLFSSL_MSG(msg);
  42769. WOLFSSL_BUFFER(a, sizeof(a));
  42770. WOLFSSL_BUFFER(b, sizeof(b));
  42771. WOLFSSL_BUFFER(NULL, 0);
  42772. WOLFSSL_ERROR(MEMORY_E);
  42773. WOLFSSL_ERROR_MSG(msg);
  42774. /* check the logs were disabled */
  42775. if (i != log_cnt)
  42776. return WC_TEST_RET_ENC_NC;
  42777. /* restore callback and leave logging enabled */
  42778. wolfSSL_SetLoggingCb(NULL);
  42779. wolfSSL_Debugging_ON();
  42780. /* suppress unused args */
  42781. (void)a;
  42782. (void)b;
  42783. #else
  42784. ret = wolfSSL_Debugging_ON();
  42785. if (ret != NOT_COMPILED_IN)
  42786. return WC_TEST_RET_ENC_EC(ret);
  42787. wolfSSL_Debugging_OFF();
  42788. ret = wolfSSL_SetLoggingCb(NULL);
  42789. if (ret != NOT_COMPILED_IN)
  42790. return WC_TEST_RET_ENC_EC(ret);
  42791. #endif /* DEBUG_WOLFSSL */
  42792. return 0;
  42793. }
  42794. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  42795. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  42796. #else
  42797. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  42798. #endif
  42799. {
  42800. #ifdef WOLFSSL_PTHREADS
  42801. wolfSSL_Mutex m;
  42802. #endif
  42803. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  42804. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  42805. wc_test_ret_t ret;
  42806. #endif
  42807. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  42808. #ifndef WOLFSSL_STATIC_MEMORY
  42809. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  42810. #else
  42811. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  42812. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  42813. if (mm != NULL) {
  42814. ret = wc_InitMutex(mm);
  42815. if (ret != 0) {
  42816. WOLFSSL_MSG("Init Mutex failed");
  42817. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  42818. return WC_TEST_RET_ENC_EC(ret);
  42819. }
  42820. }
  42821. #endif
  42822. if (mm == NULL)
  42823. return WC_TEST_RET_ENC_ERRNO;
  42824. wc_FreeMutex(mm);
  42825. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  42826. #endif
  42827. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  42828. #ifdef WOLFSSL_PTHREADS
  42829. ret = wc_InitMutex(&m);
  42830. if (ret != 0)
  42831. return WC_TEST_RET_ENC_EC(ret);
  42832. ret = wc_LockMutex(&m);
  42833. if (ret != 0)
  42834. return WC_TEST_RET_ENC_EC(ret);
  42835. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  42836. /* trying to free a locked mutex is not portable behavior with pthread */
  42837. /* Attempting to destroy a locked mutex results in undefined behavior */
  42838. ret = wc_FreeMutex(&m);
  42839. if (ret != BAD_MUTEX_E)
  42840. return WC_TEST_RET_ENC_EC(ret);
  42841. #endif
  42842. ret = wc_UnLockMutex(&m);
  42843. if (ret != 0)
  42844. return WC_TEST_RET_ENC_EC(ret);
  42845. ret = wc_FreeMutex(&m);
  42846. if (ret != 0)
  42847. return WC_TEST_RET_ENC_EC(ret);
  42848. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  42849. /* Trying to use a pthread after free'ing is not portable behavior */
  42850. ret = wc_LockMutex(&m);
  42851. if (ret != BAD_MUTEX_E)
  42852. return WC_TEST_RET_ENC_EC(ret);
  42853. ret = wc_UnLockMutex(&m);
  42854. if (ret != BAD_MUTEX_E)
  42855. return WC_TEST_RET_ENC_EC(ret);
  42856. #endif
  42857. #endif
  42858. return 0;
  42859. }
  42860. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  42861. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  42862. !defined(WOLFSSL_STATIC_MEMORY)
  42863. static wc_test_ret_t malloc_cnt = 0;
  42864. static wc_test_ret_t realloc_cnt = 0;
  42865. static wc_test_ret_t free_cnt = 0;
  42866. #ifdef WOLFSSL_DEBUG_MEMORY
  42867. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  42868. {
  42869. (void) func;
  42870. (void) line;
  42871. #else
  42872. static void *my_Malloc_cb(size_t size)
  42873. {
  42874. #endif
  42875. malloc_cnt++;
  42876. #ifndef WOLFSSL_NO_MALLOC
  42877. return malloc(size);
  42878. #else
  42879. WOLFSSL_MSG("No malloc available");
  42880. (void)size;
  42881. return NULL;
  42882. #endif
  42883. }
  42884. #ifdef WOLFSSL_DEBUG_MEMORY
  42885. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  42886. {
  42887. (void) func;
  42888. (void) line;
  42889. #else
  42890. static void my_Free_cb(void *ptr)
  42891. {
  42892. #endif
  42893. free_cnt++;
  42894. #ifndef WOLFSSL_NO_MALLOC
  42895. free(ptr);
  42896. #else
  42897. WOLFSSL_MSG("No free available");
  42898. (void)ptr;
  42899. #endif
  42900. }
  42901. #ifdef WOLFSSL_DEBUG_MEMORY
  42902. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  42903. {
  42904. (void) func;
  42905. (void) line;
  42906. #else
  42907. static void *my_Realloc_cb(void *ptr, size_t size)
  42908. {
  42909. #endif
  42910. realloc_cnt++;
  42911. #ifndef WOLFSSL_NO_MALLOC
  42912. return realloc(ptr, size);
  42913. #else
  42914. WOLFSSL_MSG("No realloc available");
  42915. (void)ptr;
  42916. (void)size;
  42917. return NULL;
  42918. #endif
  42919. }
  42920. #endif /* !WOLFSSL_NO_MALLOC */
  42921. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  42922. {
  42923. wc_test_ret_t ret = 0;
  42924. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  42925. !defined(WOLFSSL_STATIC_MEMORY)
  42926. byte* b = NULL;
  42927. #endif
  42928. wolfSSL_Malloc_cb mc;
  42929. wolfSSL_Free_cb fc;
  42930. wolfSSL_Realloc_cb rc;
  42931. /* Save existing memory callbacks */
  42932. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  42933. if (ret != 0)
  42934. return WC_TEST_RET_ENC_EC(ret);
  42935. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  42936. !defined(WOLFSSL_STATIC_MEMORY)
  42937. /* test realloc */
  42938. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42939. if (b == NULL) {
  42940. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  42941. }
  42942. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42943. b = NULL;
  42944. /* Use API. */
  42945. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  42946. (wolfSSL_Free_cb)my_Free_cb,
  42947. (wolfSSL_Realloc_cb)my_Realloc_cb);
  42948. if (ret != 0) {
  42949. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  42950. }
  42951. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42952. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42953. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42954. #ifndef WOLFSSL_STATIC_MEMORY
  42955. #ifndef WOLFSSL_CHECK_MEM_ZERO
  42956. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  42957. #else
  42958. /* Checking zeroized memory means realloc is a malloc and free. */
  42959. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  42960. #endif
  42961. #else
  42962. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  42963. #endif
  42964. ret = WC_TEST_RET_ENC_NC;
  42965. #endif /* !WOLFSSL_NO_MALLOC */
  42966. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  42967. !defined(WOLFSSL_STATIC_MEMORY)
  42968. exit_memcb:
  42969. /* reset malloc/free/realloc counts */
  42970. malloc_cnt = 0;
  42971. free_cnt = 0;
  42972. realloc_cnt = 0;
  42973. #endif
  42974. /* restore memory callbacks */
  42975. wolfSSL_SetAllocators(mc, fc, rc);
  42976. return ret;
  42977. }
  42978. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  42979. #if defined(WOLFSSL_CAAM_BLOB)
  42980. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  42981. {
  42982. wc_test_ret_t ret = 0;
  42983. byte out[112];
  42984. byte blob[112];
  42985. word32 outSz;
  42986. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  42987. {
  42988. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  42989. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  42990. };
  42991. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  42992. {
  42993. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  42994. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  42995. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  42996. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  42997. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  42998. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  42999. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  43000. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  43001. };
  43002. XMEMSET(blob, 0, sizeof(blob));
  43003. XMEMSET(out, 0, sizeof(out));
  43004. outSz = sizeof(blob);
  43005. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  43006. if (ret != 0)
  43007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43008. blob[outSz - 2] += 1;
  43009. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43010. if (ret == 0) { /* should fail with altered blob */
  43011. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43012. }
  43013. XMEMSET(blob, 0, sizeof(blob));
  43014. outSz = sizeof(blob);
  43015. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  43016. if (ret != 0)
  43017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43018. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43019. if (ret != 0)
  43020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43021. if (XMEMCMP(out, iv, sizeof(iv))) {
  43022. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43023. }
  43024. XMEMSET(blob, 0, sizeof(blob));
  43025. outSz = sizeof(blob);
  43026. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  43027. if (ret != 0)
  43028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43029. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43030. if (ret != 0)
  43031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43032. if (XMEMCMP(out, text, sizeof(text))) {
  43033. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43034. }
  43035. exit_blob:
  43036. return ret;
  43037. }
  43038. #endif /* WOLFSSL_CAAM_BLOB */
  43039. #ifdef WOLF_CRYPTO_CB
  43040. /* Example custom context for crypto callback */
  43041. typedef struct {
  43042. int exampleVar; /* flag for testing if only crypt is enabled. */
  43043. } myCryptoDevCtx;
  43044. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  43045. /* Testing rsa cb when CB_ONLY_RSA is enabled
  43046. * When CB_ONLY_RSA is enabled, software imple. is not available.
  43047. *
  43048. * ctx callback ctx
  43049. * returen 0 on success, otherwise return negative
  43050. */
  43051. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  43052. {
  43053. wc_test_ret_t ret = 0;
  43054. #if !defined(NO_RSA)
  43055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43056. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  43057. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43058. byte* tmp = NULL;
  43059. #else
  43060. RsaKey key[1];
  43061. byte tmp[FOURK_BUF];
  43062. #endif
  43063. size_t bytes;
  43064. const word32 inLen = (word32)TEST_STRING_SZ;
  43065. word32 idx = 0;
  43066. word32 sigSz;
  43067. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  43068. byte out[RSA_TEST_BYTES];
  43069. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  43070. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  43071. !defined(NO_FILESYSTEM)
  43072. XFILE file;
  43073. #endif
  43074. #ifdef WOLFSSL_KEY_GEN
  43075. WC_RNG rng;
  43076. #endif
  43077. #ifdef USE_CERT_BUFFERS_1024
  43078. bytes = (size_t)sizeof_client_key_der_1024;
  43079. if (bytes < (size_t)sizeof_client_cert_der_1024)
  43080. bytes = (size_t)sizeof_client_cert_der_1024;
  43081. #elif defined(USE_CERT_BUFFERS_2048)
  43082. bytes = (size_t)sizeof_client_key_der_2048;
  43083. if (bytes < (size_t)sizeof_client_cert_der_2048)
  43084. bytes = (size_t)sizeof_client_cert_der_2048;
  43085. #elif defined(USE_CERT_BUFFERS_3072)
  43086. bytes = (size_t)sizeof_client_key_der_3072;
  43087. if (bytes < (size_t)sizeof_client_cert_der_3072)
  43088. bytes = (size_t)sizeof_client_cert_der_3072;
  43089. #elif defined(USE_CERT_BUFFERS_4096)
  43090. bytes = (size_t)sizeof_client_key_der_4096;
  43091. if (bytes < (size_t)sizeof_client_cert_der_4096)
  43092. bytes = (size_t)sizeof_client_cert_der_4096;
  43093. #else
  43094. bytes = FOURK_BUF;
  43095. #endif
  43096. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43097. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43098. if (tmp == NULL)
  43099. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  43100. #endif
  43101. #ifdef USE_CERT_BUFFERS_1024
  43102. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  43103. #elif defined(USE_CERT_BUFFERS_2048)
  43104. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  43105. #elif defined(USE_CERT_BUFFERS_3072)
  43106. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  43107. #elif defined(USE_CERT_BUFFERS_4096)
  43108. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  43109. #elif !defined(NO_FILESYSTEM)
  43110. file = XFOPEN(clientKey, "rb");
  43111. if (!file) {
  43112. ret = WC_TEST_RET_ENC_ERRNO;
  43113. err_sys("can't open ./certs/client-key.der, "
  43114. "Please run from wolfSSL home dir", ret);
  43115. ERROR_OUT(ret, exit_onlycb);
  43116. }
  43117. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  43118. XFCLOSE(file);
  43119. if (bytes == 0)
  43120. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  43121. #endif
  43122. #ifdef WOLFSSL_KEY_GEN
  43123. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  43124. * wc_MakeRsaKey() -> rsa cb ->
  43125. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  43126. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  43127. */
  43128. ctx->exampleVar = 99;
  43129. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  43130. if (ret != 0)
  43131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43132. /* wc_MakeRsaKey() -> rsa cb ->
  43133. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  43134. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  43135. */
  43136. ctx->exampleVar = 1;
  43137. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  43138. if (ret != NO_VALID_DEVID) {
  43139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43140. } else
  43141. /* reset return code */
  43142. ret = 0;
  43143. #endif
  43144. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  43145. if (ret != 0)
  43146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43147. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  43148. if (ret != 0)
  43149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43150. sigSz = (word32)wc_RsaEncryptSize(key);
  43151. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  43152. if (ret == 0) {
  43153. /* wc_SignatureGenerate() -> rsa cb ->
  43154. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  43155. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  43156. */
  43157. ctx->exampleVar = 99;
  43158. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  43159. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  43160. if (ret != 0)
  43161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43162. }
  43163. if (ret == 0) {
  43164. /* wc_SignatureGenerate() -> rsa cb ->
  43165. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  43166. * wc_SignatureGenerate(INVALID_DEVID) expects to
  43167. * return NO_VALID_DEVID(failure)
  43168. */
  43169. ctx->exampleVar = 1;
  43170. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  43171. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  43172. if (ret != NO_VALID_DEVID) {
  43173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43174. } else
  43175. /* reset return code */
  43176. ret = 0;
  43177. }
  43178. exit_onlycb:
  43179. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43180. if (key != NULL) {
  43181. wc_FreeRsaKey(key);
  43182. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43183. }
  43184. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43185. #else
  43186. wc_FreeRsaKey(key);
  43187. #endif
  43188. #endif
  43189. return ret;
  43190. }
  43191. #endif
  43192. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  43193. /* Testing rsa cb when CB_ONLY_ECC is enabled
  43194. * When CB_ONLY_ECC is enabled, software imple. is not available.
  43195. *
  43196. * ctx callback ctx
  43197. * returen 0 on success, otherwise return negative
  43198. */
  43199. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  43200. {
  43201. wc_test_ret_t ret = 0;
  43202. #if defined(HAVE_ECC)
  43203. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43204. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  43205. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43206. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  43207. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43208. byte* out = (byte*)XMALLOC(sizeof(byte),
  43209. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43210. #ifdef OPENSSL_EXTRA
  43211. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  43212. DYNAMIC_TYPE_TMP_BUFFER);
  43213. #endif
  43214. #else
  43215. ecc_key key[1];
  43216. ecc_key pub[1];
  43217. byte out[256];
  43218. #ifdef OPENSSL_EXTRA
  43219. unsigned char check[256];
  43220. #endif
  43221. #endif
  43222. #ifdef OPENSSL_EXTRA
  43223. EVP_PKEY* privKey = NULL;
  43224. EVP_PKEY* pubKey = NULL;
  43225. #ifdef USE_CERT_BUFFERS_256
  43226. ecc_key* pkey;
  43227. const unsigned char* cp;
  43228. #endif
  43229. EVP_MD_CTX mdCtx;
  43230. const char testData[] = "Hi There";
  43231. size_t checkSz = -1;
  43232. const unsigned char* p;
  43233. const unsigned char check_v[256] = {
  43234. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  43235. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  43236. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  43237. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  43238. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  43239. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  43240. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  43241. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  43242. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  43243. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  43244. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  43245. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  43246. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  43247. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  43248. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  43249. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  43250. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  43251. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  43252. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  43253. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  43254. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  43255. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  43256. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  43257. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  43258. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  43259. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  43260. };
  43261. #endif
  43262. WC_RNG rng;
  43263. EncryptedInfo encInfo;
  43264. int keyFormat = 0;
  43265. word32 keyIdx = 0;
  43266. byte in[] = "Everyone gets Friday off. ecc p";
  43267. word32 inLen = (word32)XSTRLEN((char*)in);
  43268. word32 outLen;
  43269. int verify;
  43270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43271. if (key == NULL || pub == NULL) {
  43272. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  43273. }
  43274. #endif
  43275. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  43276. if (ret != 0)
  43277. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43278. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  43279. ctx->exampleVar = 99;
  43280. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  43281. if (ret != 0)
  43282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43283. ctx->exampleVar = 1;
  43284. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  43285. if (ret != NO_VALID_DEVID) {
  43286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43287. } else
  43288. /* reset return code */
  43289. ret = 0;
  43290. #ifdef USE_CERT_BUFFERS_256
  43291. if (ret == 0) {
  43292. /* load ECC private key and perform private transform */
  43293. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  43294. key, sizeof_ecc_key_der_256);
  43295. }
  43296. if (ret != 0)
  43297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43298. /* wc_CryptoCb_EccSign cb test, no actual testing */
  43299. ctx->exampleVar = 99;
  43300. if (ret == 0) {
  43301. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  43302. }
  43303. if (ret != 0)
  43304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43305. ctx->exampleVar = 1;
  43306. if (ret == 0) {
  43307. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  43308. }
  43309. if (ret != NO_VALID_DEVID) {
  43310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43311. }
  43312. else
  43313. ret = 0;
  43314. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  43315. ctx->exampleVar = 99;
  43316. if (ret == 0) {
  43317. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  43318. }
  43319. if (ret != 0)
  43320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43321. ctx->exampleVar = 1;
  43322. if (ret == 0) {
  43323. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  43324. }
  43325. if (ret != NO_VALID_DEVID) {
  43326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43327. }
  43328. else
  43329. ret = 0;
  43330. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  43331. /* make public key for shared secret */
  43332. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  43333. ctx->exampleVar = 99;
  43334. if (ret == 0) {
  43335. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  43336. }
  43337. if (ret != 0)
  43338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43339. ctx->exampleVar = 1;
  43340. if (ret == 0) {
  43341. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  43342. }
  43343. if (ret != NO_VALID_DEVID) {
  43344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43345. }
  43346. else
  43347. ret = 0;
  43348. #ifdef OPENSSL_EXTRA
  43349. (void)pkey;
  43350. cp = ecc_clikey_der_256;
  43351. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  43352. sizeof_ecc_clikey_der_256);
  43353. if (privKey == NULL) {
  43354. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43355. }
  43356. pkey = (ecc_key*)privKey->ecc->internal;
  43357. pkey->devId = devId;
  43358. p = ecc_clikeypub_der_256;
  43359. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  43360. if (pubKey == NULL) {
  43361. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43362. }
  43363. pkey = (ecc_key*)pubKey->ecc->internal;
  43364. pkey->devId = devId;
  43365. /* sign */
  43366. EVP_MD_CTX_init(&mdCtx);
  43367. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  43368. if (ret != WOLFSSL_SUCCESS) {
  43369. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43370. }
  43371. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  43372. (unsigned int)XSTRLEN(testData));
  43373. if (ret != WOLFSSL_SUCCESS) {
  43374. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43375. }
  43376. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  43377. if (ret != WOLFSSL_SUCCESS) {
  43378. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43379. }
  43380. ctx->exampleVar = 99;
  43381. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  43382. /* just called crypt callback as dummy
  43383. * EVP_DigestSignFinal returns 0 internally.
  43384. */
  43385. if (ret != 0)
  43386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43387. ctx->exampleVar = 1;
  43388. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  43389. /* just called crypt callback as dummy
  43390. * EVP_DigestSignFinal returns 0 internally.
  43391. */
  43392. if (ret != 0)
  43393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43394. /* restore checkSz for verify */
  43395. checkSz = 71;
  43396. ret = EVP_MD_CTX_cleanup(&mdCtx);
  43397. if (ret != SSL_SUCCESS) {
  43398. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43399. }
  43400. /* verify */
  43401. EVP_MD_CTX_init(&mdCtx);
  43402. if (ret == SSL_SUCCESS) {
  43403. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  43404. }
  43405. if (ret != WOLFSSL_SUCCESS) {
  43406. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43407. }
  43408. if (ret == WOLFSSL_SUCCESS) {
  43409. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  43410. (unsigned int)XSTRLEN(testData));
  43411. }
  43412. if (ret != WOLFSSL_SUCCESS) {
  43413. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43414. }
  43415. ctx->exampleVar = 99;
  43416. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  43417. /* just called crypt callback as dummy
  43418. * EVP_DigestSignFinal returns 0 internally.
  43419. */
  43420. if (ret != 0)
  43421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43422. ctx->exampleVar = 1;
  43423. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  43424. /* just called crypt callback as dummy
  43425. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  43426. */
  43427. if (ret != -1) {
  43428. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43429. }
  43430. ret = EVP_MD_CTX_cleanup(&mdCtx);
  43431. if (ret != SSL_SUCCESS) {
  43432. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  43433. } else
  43434. ret = 0;
  43435. #endif
  43436. #else
  43437. (void)verify;
  43438. (void)outLen;
  43439. (void)inLen;
  43440. (void)out;
  43441. (void)pub;
  43442. #ifdef OPENSSL_EXTRA
  43443. (void)privKey;
  43444. (void)pubKey;
  43445. (void)mdCtx;
  43446. (void)check;
  43447. (void)checkSz;
  43448. (void)p;
  43449. #endif
  43450. #endif
  43451. (void)keyFormat;
  43452. (void)encInfo;
  43453. exit_onlycb:
  43454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43455. if (key != NULL) {
  43456. wc_ecc_free(key);
  43457. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43458. }
  43459. if (pub != NULL) {
  43460. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43461. }
  43462. if (out != NULL) {
  43463. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43464. }
  43465. #ifdef OPENSSL_EXTRA
  43466. if (check) {
  43467. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43468. }
  43469. #endif
  43470. #else
  43471. wc_ecc_free(key);
  43472. #ifdef OPENSSL_EXTRA
  43473. if (privKey)
  43474. EVP_PKEY_free(privKey);
  43475. if (pubKey)
  43476. EVP_PKEY_free(pubKey);
  43477. #endif
  43478. #endif
  43479. #endif /* HAVE_ECC */
  43480. return ret;
  43481. }
  43482. #endif
  43483. /* Example crypto dev callback function that calls software version */
  43484. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  43485. {
  43486. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  43487. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  43488. if (info == NULL)
  43489. return BAD_FUNC_ARG;
  43490. #ifdef DEBUG_WOLFSSL
  43491. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  43492. #endif
  43493. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  43494. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  43495. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  43496. /* if RNG only supports crypto callback, just use seed */
  43497. ret = wc_GenerateSeed(&info->rng.rng->seed,
  43498. info->rng.out, info->rng.sz);
  43499. #elif !defined(WC_NO_RNG)
  43500. /* set devId to invalid, so software is used */
  43501. info->rng.rng->devId = INVALID_DEVID;
  43502. ret = wc_RNG_GenerateBlock(info->rng.rng,
  43503. info->rng.out, info->rng.sz);
  43504. /* reset devId */
  43505. info->rng.rng->devId = devIdArg;
  43506. #endif
  43507. }
  43508. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  43509. #ifndef WC_NO_RNG
  43510. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  43511. word32* seedWord32 = (word32*)seed;
  43512. word32 len;
  43513. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  43514. while (info->seed.sz > 0) {
  43515. len = (word32)sizeof(seed);
  43516. if (info->seed.sz < len)
  43517. len = info->seed.sz;
  43518. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  43519. info->seed.seed += len;
  43520. info->seed.sz -= len;
  43521. (*seedWord32)++;
  43522. }
  43523. ret = 0;
  43524. #endif
  43525. }
  43526. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  43527. #ifdef DEBUG_WOLFSSL
  43528. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  43529. #endif
  43530. #ifndef NO_RSA
  43531. if (info->pk.type == WC_PK_TYPE_RSA) {
  43532. /* set devId to invalid, so software is used */
  43533. info->pk.rsa.key->devId = INVALID_DEVID;
  43534. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  43535. #ifdef DEBUG_WOLFSSL
  43536. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43537. #endif
  43538. if (myCtx->exampleVar == 99) {
  43539. info->pk.rsa.key->devId = devIdArg;
  43540. return 0;
  43541. }
  43542. #endif
  43543. switch (info->pk.rsa.type) {
  43544. case RSA_PUBLIC_ENCRYPT:
  43545. case RSA_PUBLIC_DECRYPT:
  43546. /* perform software based RSA public op */
  43547. ret = wc_RsaFunction(
  43548. info->pk.rsa.in, info->pk.rsa.inLen,
  43549. info->pk.rsa.out, info->pk.rsa.outLen,
  43550. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  43551. break;
  43552. case RSA_PRIVATE_ENCRYPT:
  43553. case RSA_PRIVATE_DECRYPT:
  43554. /* perform software based RSA private op */
  43555. ret = wc_RsaFunction(
  43556. info->pk.rsa.in, info->pk.rsa.inLen,
  43557. info->pk.rsa.out, info->pk.rsa.outLen,
  43558. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  43559. break;
  43560. }
  43561. /* reset devId */
  43562. info->pk.rsa.key->devId = devIdArg;
  43563. }
  43564. #ifdef WOLFSSL_KEY_GEN
  43565. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  43566. info->pk.rsakg.key->devId = INVALID_DEVID;
  43567. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  43568. #ifdef DEBUG_WOLFSSL
  43569. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43570. #endif
  43571. if (myCtx->exampleVar == 99) {
  43572. info->pk.rsakg.key->devId = devIdArg;
  43573. return 0;
  43574. }
  43575. #endif
  43576. #ifdef HAVE_FIPS
  43577. for (;;) {
  43578. #endif
  43579. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  43580. info->pk.rsakg.e, info->pk.rsakg.rng);
  43581. #ifdef HAVE_FIPS
  43582. if (ret == PRIME_GEN_E)
  43583. continue;
  43584. break;
  43585. }
  43586. #endif
  43587. /* reset devId */
  43588. info->pk.rsakg.key->devId = devIdArg;
  43589. }
  43590. #endif
  43591. #endif /* !NO_RSA */
  43592. #ifdef HAVE_ECC
  43593. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  43594. /* set devId to invalid, so software is used */
  43595. info->pk.eckg.key->devId = INVALID_DEVID;
  43596. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  43597. #ifdef DEBUG_WOLFSSL
  43598. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43599. #endif
  43600. if (myCtx->exampleVar == 99) {
  43601. info->pk.eckg.key->devId = devIdArg;
  43602. return 0;
  43603. }
  43604. #endif
  43605. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  43606. info->pk.eckg.key, info->pk.eckg.curveId);
  43607. /* reset devId */
  43608. info->pk.eckg.key->devId = devIdArg;
  43609. }
  43610. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  43611. /* set devId to invalid, so software is used */
  43612. info->pk.eccsign.key->devId = INVALID_DEVID;
  43613. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  43614. #ifdef DEBUG_WOLFSSL
  43615. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43616. #endif
  43617. if (myCtx->exampleVar == 99) {
  43618. info->pk.eccsign.key->devId = devIdArg;
  43619. return 0;
  43620. }
  43621. #endif
  43622. ret = wc_ecc_sign_hash(
  43623. info->pk.eccsign.in, info->pk.eccsign.inlen,
  43624. info->pk.eccsign.out, info->pk.eccsign.outlen,
  43625. info->pk.eccsign.rng, info->pk.eccsign.key);
  43626. /* reset devId */
  43627. info->pk.eccsign.key->devId = devIdArg;
  43628. }
  43629. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  43630. /* set devId to invalid, so software is used */
  43631. info->pk.eccverify.key->devId = INVALID_DEVID;
  43632. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  43633. #ifdef DEBUG_WOLFSSL
  43634. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43635. #endif
  43636. if (myCtx->exampleVar == 99) {
  43637. info->pk.eccverify.key->devId = devIdArg;
  43638. return 0;
  43639. }
  43640. #endif
  43641. ret = wc_ecc_verify_hash(
  43642. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  43643. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  43644. info->pk.eccverify.res, info->pk.eccverify.key);
  43645. /* reset devId */
  43646. info->pk.eccverify.key->devId = devIdArg;
  43647. }
  43648. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  43649. /* set devId to invalid, so software is used */
  43650. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  43651. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  43652. #ifdef DEBUG_WOLFSSL
  43653. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  43654. #endif
  43655. if (myCtx->exampleVar == 99) {
  43656. info->pk.ecdh.private_key->devId = devIdArg;
  43657. return 0;
  43658. }
  43659. #endif
  43660. ret = wc_ecc_shared_secret(
  43661. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  43662. info->pk.ecdh.out, info->pk.ecdh.outlen);
  43663. /* reset devId */
  43664. info->pk.ecdh.private_key->devId = devIdArg;
  43665. }
  43666. #endif /* HAVE_ECC */
  43667. #ifdef HAVE_CURVE25519
  43668. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  43669. /* set devId to invalid, so software is used */
  43670. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  43671. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  43672. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  43673. /* reset devId */
  43674. info->pk.curve25519kg.key->devId = devIdArg;
  43675. }
  43676. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  43677. /* set devId to invalid, so software is used */
  43678. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  43679. ret = wc_curve25519_shared_secret_ex(
  43680. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  43681. info->pk.curve25519.out, info->pk.curve25519.outlen,
  43682. info->pk.curve25519.endian);
  43683. /* reset devId */
  43684. info->pk.curve25519.private_key->devId = devIdArg;
  43685. }
  43686. #endif /* HAVE_CURVE25519 */
  43687. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  43688. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  43689. /* set devId to invalid, so software is used */
  43690. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  43691. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  43692. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  43693. /* reset devId */
  43694. info->pk.ed25519kg.key->devId = devIdArg;
  43695. }
  43696. #ifdef HAVE_ED25519_SIGN
  43697. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  43698. /* set devId to invalid, so software is used */
  43699. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  43700. ret = wc_ed25519_sign_msg_ex(
  43701. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  43702. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  43703. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  43704. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  43705. /* reset devId */
  43706. info->pk.ed25519sign.key->devId = devIdArg;
  43707. }
  43708. #endif
  43709. #ifdef HAVE_ED25519_VERIFY
  43710. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  43711. /* set devId to invalid, so software is used */
  43712. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  43713. ret = wc_ed25519_verify_msg_ex(
  43714. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  43715. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  43716. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  43717. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  43718. info->pk.ed25519verify.contextLen);
  43719. /* reset devId */
  43720. info->pk.ed25519verify.key->devId = devIdArg;
  43721. }
  43722. #endif
  43723. #endif /* HAVE_ED25519 */
  43724. }
  43725. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  43726. #if !defined(NO_AES) || !defined(NO_DES3)
  43727. #ifdef HAVE_AESGCM
  43728. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  43729. if (info->cipher.enc) {
  43730. /* set devId to invalid, so software is used */
  43731. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  43732. ret = wc_AesGcmEncrypt(
  43733. info->cipher.aesgcm_enc.aes,
  43734. info->cipher.aesgcm_enc.out,
  43735. info->cipher.aesgcm_enc.in,
  43736. info->cipher.aesgcm_enc.sz,
  43737. info->cipher.aesgcm_enc.iv,
  43738. info->cipher.aesgcm_enc.ivSz,
  43739. info->cipher.aesgcm_enc.authTag,
  43740. info->cipher.aesgcm_enc.authTagSz,
  43741. info->cipher.aesgcm_enc.authIn,
  43742. info->cipher.aesgcm_enc.authInSz);
  43743. /* reset devId */
  43744. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  43745. }
  43746. else {
  43747. /* set devId to invalid, so software is used */
  43748. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  43749. ret = wc_AesGcmDecrypt(
  43750. info->cipher.aesgcm_dec.aes,
  43751. info->cipher.aesgcm_dec.out,
  43752. info->cipher.aesgcm_dec.in,
  43753. info->cipher.aesgcm_dec.sz,
  43754. info->cipher.aesgcm_dec.iv,
  43755. info->cipher.aesgcm_dec.ivSz,
  43756. info->cipher.aesgcm_dec.authTag,
  43757. info->cipher.aesgcm_dec.authTagSz,
  43758. info->cipher.aesgcm_dec.authIn,
  43759. info->cipher.aesgcm_dec.authInSz);
  43760. /* reset devId */
  43761. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  43762. }
  43763. }
  43764. #endif /* HAVE_AESGCM */
  43765. #ifdef HAVE_AES_CBC
  43766. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  43767. if (info->cipher.enc) {
  43768. /* set devId to invalid, so software is used */
  43769. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  43770. ret = wc_AesCbcEncrypt(
  43771. info->cipher.aescbc.aes,
  43772. info->cipher.aescbc.out,
  43773. info->cipher.aescbc.in,
  43774. info->cipher.aescbc.sz);
  43775. /* reset devId */
  43776. info->cipher.aescbc.aes->devId = devIdArg;
  43777. }
  43778. else {
  43779. /* set devId to invalid, so software is used */
  43780. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  43781. ret = wc_AesCbcDecrypt(
  43782. info->cipher.aescbc.aes,
  43783. info->cipher.aescbc.out,
  43784. info->cipher.aescbc.in,
  43785. info->cipher.aescbc.sz);
  43786. /* reset devId */
  43787. info->cipher.aescbc.aes->devId = devIdArg;
  43788. }
  43789. }
  43790. #endif /* HAVE_AES_CBC */
  43791. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  43792. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  43793. if (info->cipher.enc) {
  43794. /* set devId to invalid, so software is used */
  43795. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  43796. ret = wc_AesEcbEncrypt(
  43797. info->cipher.aesecb.aes,
  43798. info->cipher.aesecb.out,
  43799. info->cipher.aesecb.in,
  43800. info->cipher.aesecb.sz);
  43801. /* reset devId */
  43802. info->cipher.aesecb.aes->devId = devIdArg;
  43803. }
  43804. else {
  43805. /* set devId to invalid, so software is used */
  43806. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  43807. ret = wc_AesEcbDecrypt(
  43808. info->cipher.aesecb.aes,
  43809. info->cipher.aesecb.out,
  43810. info->cipher.aesecb.in,
  43811. info->cipher.aesecb.sz);
  43812. /* reset devId */
  43813. info->cipher.aesecb.aes->devId = devIdArg;
  43814. }
  43815. }
  43816. #endif /* HAVE_AES_ECB */
  43817. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  43818. !defined(HAVE_SELFTEST)
  43819. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  43820. /* set devId to invalid, so software is used */
  43821. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  43822. ret = wc_AesCtrEncrypt(
  43823. info->cipher.aesctr.aes,
  43824. info->cipher.aesctr.out,
  43825. info->cipher.aesctr.in,
  43826. info->cipher.aesctr.sz);
  43827. /* reset devId */
  43828. info->cipher.aesctr.aes->devId = devIdArg;
  43829. }
  43830. #endif /* WOLFSSL_AES_COUNTER */
  43831. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  43832. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  43833. if (info->cipher.enc) {
  43834. /* set devId to invalid, so software is used */
  43835. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  43836. ret = wc_AesCcmEncrypt(
  43837. info->cipher.aesccm_enc.aes,
  43838. info->cipher.aesccm_enc.out,
  43839. info->cipher.aesccm_enc.in,
  43840. info->cipher.aesccm_enc.sz,
  43841. info->cipher.aesccm_enc.nonce,
  43842. info->cipher.aesccm_enc.nonceSz,
  43843. info->cipher.aesccm_enc.authTag,
  43844. info->cipher.aesccm_enc.authTagSz,
  43845. info->cipher.aesccm_enc.authIn,
  43846. info->cipher.aesccm_enc.authInSz);
  43847. /* reset devId */
  43848. info->cipher.aesccm_enc.aes->devId = devIdArg;
  43849. }
  43850. else {
  43851. /* set devId to invalid, so software is used */
  43852. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  43853. ret = wc_AesCcmDecrypt(
  43854. info->cipher.aesccm_dec.aes,
  43855. info->cipher.aesccm_dec.out,
  43856. info->cipher.aesccm_dec.in,
  43857. info->cipher.aesccm_dec.sz,
  43858. info->cipher.aesccm_dec.nonce,
  43859. info->cipher.aesccm_dec.nonceSz,
  43860. info->cipher.aesccm_dec.authTag,
  43861. info->cipher.aesccm_dec.authTagSz,
  43862. info->cipher.aesccm_dec.authIn,
  43863. info->cipher.aesccm_dec.authInSz);
  43864. /* reset devId */
  43865. info->cipher.aesccm_dec.aes->devId = devIdArg;
  43866. }
  43867. }
  43868. #endif
  43869. #ifndef NO_DES3
  43870. if (info->cipher.type == WC_CIPHER_DES3) {
  43871. if (info->cipher.enc) {
  43872. /* set devId to invalid, so software is used */
  43873. info->cipher.des3.des->devId = INVALID_DEVID;
  43874. ret = wc_Des3_CbcEncrypt(
  43875. info->cipher.des3.des,
  43876. info->cipher.des3.out,
  43877. info->cipher.des3.in,
  43878. info->cipher.des3.sz);
  43879. /* reset devId */
  43880. info->cipher.des3.des->devId = devIdArg;
  43881. }
  43882. else {
  43883. /* set devId to invalid, so software is used */
  43884. info->cipher.des3.des->devId = INVALID_DEVID;
  43885. ret = wc_Des3_CbcDecrypt(
  43886. info->cipher.des3.des,
  43887. info->cipher.des3.out,
  43888. info->cipher.des3.in,
  43889. info->cipher.des3.sz);
  43890. /* reset devId */
  43891. info->cipher.des3.des->devId = devIdArg;
  43892. }
  43893. }
  43894. #endif /* !NO_DES3 */
  43895. #endif /* !NO_AES || !NO_DES3 */
  43896. }
  43897. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  43898. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  43899. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  43900. #if !defined(NO_SHA)
  43901. if (info->hash.type == WC_HASH_TYPE_SHA) {
  43902. if (info->hash.sha1 == NULL)
  43903. return NOT_COMPILED_IN;
  43904. /* set devId to invalid, so software is used */
  43905. info->hash.sha1->devId = INVALID_DEVID;
  43906. if (info->hash.in != NULL) {
  43907. ret = wc_ShaUpdate(
  43908. info->hash.sha1,
  43909. info->hash.in,
  43910. info->hash.inSz);
  43911. }
  43912. if (info->hash.digest != NULL) {
  43913. ret = wc_ShaFinal(
  43914. info->hash.sha1,
  43915. info->hash.digest);
  43916. }
  43917. /* reset devId */
  43918. info->hash.sha1->devId = devIdArg;
  43919. }
  43920. else
  43921. #endif
  43922. #if !defined(NO_SHA256)
  43923. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  43924. if (info->hash.sha256 == NULL)
  43925. return NOT_COMPILED_IN;
  43926. /* set devId to invalid, so software is used */
  43927. info->hash.sha256->devId = INVALID_DEVID;
  43928. if (info->hash.in != NULL) {
  43929. ret = wc_Sha256Update(
  43930. info->hash.sha256,
  43931. info->hash.in,
  43932. info->hash.inSz);
  43933. }
  43934. if (info->hash.digest != NULL) {
  43935. ret = wc_Sha256Final(
  43936. info->hash.sha256,
  43937. info->hash.digest);
  43938. }
  43939. /* reset devId */
  43940. info->hash.sha256->devId = devIdArg;
  43941. }
  43942. else
  43943. #endif
  43944. #ifdef WOLFSSL_SHA384
  43945. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  43946. if (info->hash.sha384 == NULL)
  43947. return NOT_COMPILED_IN;
  43948. #ifndef NO_SHA2_CRYPTO_CB
  43949. /* set devId to invalid, so software is used */
  43950. info->hash.sha384->devId = INVALID_DEVID;
  43951. #endif
  43952. if (info->hash.in != NULL) {
  43953. ret = wc_Sha384Update(
  43954. info->hash.sha384,
  43955. info->hash.in,
  43956. info->hash.inSz);
  43957. }
  43958. if (info->hash.digest != NULL) {
  43959. ret = wc_Sha384Final(
  43960. info->hash.sha384,
  43961. info->hash.digest);
  43962. }
  43963. #ifndef NO_SHA2_CRYPTO_CB
  43964. /* reset devId */
  43965. info->hash.sha384->devId = devIdArg;
  43966. #endif
  43967. }
  43968. else
  43969. #endif
  43970. #ifdef WOLFSSL_SHA512
  43971. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  43972. if (info->hash.sha512 == NULL)
  43973. return NOT_COMPILED_IN;
  43974. #ifndef NO_SHA2_CRYPTO_CB
  43975. /* set devId to invalid, so software is used */
  43976. info->hash.sha512->devId = INVALID_DEVID;
  43977. #endif
  43978. if (info->hash.in != NULL) {
  43979. ret = wc_Sha512Update(
  43980. info->hash.sha512,
  43981. info->hash.in,
  43982. info->hash.inSz);
  43983. }
  43984. if (info->hash.digest != NULL) {
  43985. ret = wc_Sha512Final(
  43986. info->hash.sha512,
  43987. info->hash.digest);
  43988. }
  43989. #ifndef NO_SHA2_CRYPTO_CB
  43990. /* reset devId */
  43991. info->hash.sha512->devId = devIdArg;
  43992. #endif
  43993. }
  43994. else
  43995. #endif
  43996. {
  43997. }
  43998. }
  43999. #endif /* !NO_SHA || !NO_SHA256 */
  44000. #ifndef NO_HMAC
  44001. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  44002. if (info->hmac.hmac == NULL)
  44003. return NOT_COMPILED_IN;
  44004. /* set devId to invalid, so software is used */
  44005. info->hmac.hmac->devId = INVALID_DEVID;
  44006. if (info->hash.in != NULL) {
  44007. ret = wc_HmacUpdate(
  44008. info->hmac.hmac,
  44009. info->hmac.in,
  44010. info->hmac.inSz);
  44011. }
  44012. else if (info->hash.digest != NULL) {
  44013. ret = wc_HmacFinal(
  44014. info->hmac.hmac,
  44015. info->hmac.digest);
  44016. }
  44017. /* reset devId */
  44018. info->hmac.hmac->devId = devIdArg;
  44019. }
  44020. #endif
  44021. (void)devIdArg;
  44022. (void)myCtx;
  44023. return ret;
  44024. }
  44025. #ifdef WOLF_CRYPTO_CB_FIND
  44026. static int myCryptoCbFind(int currentId, int algoType)
  44027. {
  44028. /* can have algo specific overrides here
  44029. switch (algoType) {
  44030. i.e.
  44031. WC_ALGO_TYPE_CMAC
  44032. WC_ALGO_TYPE_SEED
  44033. WC_ALGO_TYPE_HMAC
  44034. WC_ALGO_TYPE_HASH
  44035. WC_ALGO_TYPE_CIPHER
  44036. WC_ALGO_TYPE_PK
  44037. }
  44038. */
  44039. (void)algoType;
  44040. if (currentId == INVALID_DEVID) {
  44041. /* can override invalid devid found with 1 */
  44042. }
  44043. return currentId;
  44044. }
  44045. #endif /* WOLF_CRYPTO_CB_FIND */
  44046. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  44047. {
  44048. wc_test_ret_t ret = 0;
  44049. int origDevId = devId;
  44050. myCryptoDevCtx myCtx;
  44051. /* example data for callback */
  44052. myCtx.exampleVar = 1;
  44053. /* set devId to something other than INVALID_DEVID */
  44054. devId = 1;
  44055. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  44056. #ifdef WOLF_CRYPTO_CB_FIND
  44057. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  44058. #endif /* WOLF_CRYPTO_CB_FIND */
  44059. #ifndef WC_NO_RNG
  44060. if (ret == 0)
  44061. ret = random_test();
  44062. #endif /* WC_NO_RNG */
  44063. #if !defined(NO_RSA)
  44064. PRIVATE_KEY_UNLOCK();
  44065. if (ret == 0)
  44066. ret = rsa_test();
  44067. PRIVATE_KEY_LOCK();
  44068. #endif
  44069. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  44070. PRIVATE_KEY_UNLOCK();
  44071. if (ret == 0)
  44072. ret = rsa_onlycb_test(&myCtx);
  44073. PRIVATE_KEY_LOCK();
  44074. #endif
  44075. #if defined(HAVE_ECC)
  44076. PRIVATE_KEY_UNLOCK();
  44077. if (ret == 0)
  44078. ret = ecc_test();
  44079. PRIVATE_KEY_LOCK();
  44080. #endif
  44081. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44082. PRIVATE_KEY_UNLOCK();
  44083. if (ret == 0)
  44084. ret = ecc_onlycb_test(&myCtx);
  44085. PRIVATE_KEY_LOCK();
  44086. #endif
  44087. #ifdef HAVE_ED25519
  44088. if (ret == 0)
  44089. ret = ed25519_test();
  44090. #endif
  44091. #ifdef HAVE_CURVE25519
  44092. if (ret == 0)
  44093. ret = curve25519_test();
  44094. #endif
  44095. #ifndef NO_AES
  44096. #ifdef HAVE_AESGCM
  44097. if (ret == 0)
  44098. ret = aesgcm_test();
  44099. #endif
  44100. #ifdef HAVE_AES_CBC
  44101. if (ret == 0)
  44102. ret = aes_test();
  44103. #endif
  44104. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  44105. if (ret == 0)
  44106. ret = aesccm_test();
  44107. #endif
  44108. #endif /* !NO_AES */
  44109. #ifndef NO_DES3
  44110. if (ret == 0)
  44111. ret = des3_test();
  44112. #endif /* !NO_DES3 */
  44113. #ifndef NO_SHA
  44114. if (ret == 0)
  44115. ret = sha_test();
  44116. #endif
  44117. #ifndef NO_SHA256
  44118. if (ret == 0)
  44119. ret = sha256_test();
  44120. #endif
  44121. #ifdef WOLFSSL_SHA384
  44122. if (ret == 0)
  44123. ret = sha384_test();
  44124. #endif
  44125. #ifdef WOLFSSL_SHA512
  44126. if (ret == 0)
  44127. ret = sha512_test();
  44128. #endif
  44129. #ifndef NO_HMAC
  44130. #ifndef NO_SHA
  44131. if (ret == 0)
  44132. ret = hmac_sha_test();
  44133. #endif
  44134. #ifndef NO_SHA256
  44135. if (ret == 0)
  44136. ret = hmac_sha256_test();
  44137. #endif
  44138. #endif
  44139. #ifndef NO_PWDBASED
  44140. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  44141. if (ret == 0)
  44142. ret = pbkdf2_test();
  44143. #endif
  44144. #endif
  44145. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  44146. if (ret == 0)
  44147. ret = cmac_test();
  44148. #endif
  44149. /* restore devId */
  44150. devId = origDevId;
  44151. return ret;
  44152. }
  44153. #endif /* WOLF_CRYPTO_CB */
  44154. #ifdef WOLFSSL_CERT_PIV
  44155. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  44156. {
  44157. wc_test_ret_t ret;
  44158. wc_CertPIV piv;
  44159. /* Template for Identiv PIV cert, nonce and signature */
  44160. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  44161. 0x0A, 0x0B,
  44162. 0x53, 0x09, /* NIST PIV Cert */
  44163. 0x70, 0x02, /* Certificate */
  44164. 0x30, 0x00,
  44165. 0x71, 0x01, 0x05, /* Cert Info */
  44166. 0xFE, 0x00, /* Error Detection */
  44167. 0x0B, 0x01, 0x00, /* Nonce */
  44168. 0x0C, 0x01, 0x00, /* Signed Nonce */
  44169. };
  44170. /* PIV certificate data including certificate, info and error detection. */
  44171. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  44172. 0x53, 0x09, /* NIST PIV Cert */
  44173. 0x70, 0x02, /* Certificate */
  44174. 0x30, 0x00,
  44175. 0x71, 0x01, 0x04, /* Cert Info */
  44176. 0xFE, 0x00, /* Error Detection */
  44177. };
  44178. XMEMSET(&piv, 0, sizeof(piv));
  44179. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  44180. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  44181. if (ret != 0) {
  44182. return WC_TEST_RET_ENC_EC(ret);
  44183. }
  44184. if (!piv.isIdentiv) {
  44185. return WC_TEST_RET_ENC_NC;
  44186. }
  44187. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  44188. return WC_TEST_RET_ENC_NC;
  44189. }
  44190. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  44191. return WC_TEST_RET_ENC_NC;
  44192. }
  44193. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  44194. return WC_TEST_RET_ENC_NC;
  44195. }
  44196. if (!piv.isX509) {
  44197. return WC_TEST_RET_ENC_NC;
  44198. }
  44199. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  44200. return WC_TEST_RET_ENC_NC;
  44201. }
  44202. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  44203. return WC_TEST_RET_ENC_NC;
  44204. }
  44205. XMEMSET(&piv, 0, sizeof(piv));
  44206. /* Test with NIST PIV format */
  44207. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  44208. if (ret != 0) {
  44209. return WC_TEST_RET_ENC_EC(ret);
  44210. }
  44211. if (piv.isIdentiv) {
  44212. return WC_TEST_RET_ENC_NC;
  44213. }
  44214. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  44215. return WC_TEST_RET_ENC_NC;
  44216. }
  44217. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  44218. return WC_TEST_RET_ENC_NC;
  44219. }
  44220. if ((piv.compression != 0)) {
  44221. return WC_TEST_RET_ENC_NC;
  44222. }
  44223. if (!piv.isX509) {
  44224. return WC_TEST_RET_ENC_NC;
  44225. }
  44226. return ret;
  44227. }
  44228. #endif /* WOLFSSL_CERT_PIV */
  44229. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  44230. static time_t time_cb(time_t* t)
  44231. {
  44232. if (t != NULL) {
  44233. *t = 99;
  44234. }
  44235. return 99;
  44236. }
  44237. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  44238. {
  44239. time_t t;
  44240. wc_test_ret_t ret;
  44241. ret = wc_SetTimeCb(time_cb);
  44242. if (ret != 0)
  44243. return WC_TEST_RET_ENC_EC(ret);
  44244. t = wc_Time(NULL);
  44245. if (t != 99)
  44246. return WC_TEST_RET_ENC_NC;
  44247. ret = wc_GetTime(&t, sizeof(time_t));
  44248. if (ret != 0)
  44249. return WC_TEST_RET_ENC_EC(ret);
  44250. if (t != 99)
  44251. return WC_TEST_RET_ENC_NC;
  44252. ret = wc_SetTimeCb(NULL);
  44253. if (ret != 0)
  44254. return WC_TEST_RET_ENC_EC(ret);
  44255. return 0;
  44256. }
  44257. #endif
  44258. #ifdef WOLFSSL_AES_SIV
  44259. typedef struct {
  44260. const byte key[33];
  44261. word32 keySz;
  44262. const byte nonce[49];
  44263. word32 nonceSz;
  44264. const byte assoc[81];
  44265. word32 assocSz;
  44266. const byte plaintext[83];
  44267. word32 plaintextSz;
  44268. const byte siv[AES_BLOCK_SIZE+1];
  44269. const byte ciphertext[82];
  44270. word32 ciphertextSz;
  44271. } AesSivTestVector;
  44272. #define AES_SIV_TEST_VECTORS 7
  44273. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  44274. {
  44275. /* These test vectors come from chrony 4.1's SIV unit tests. */
  44276. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  44277. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44278. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44279. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  44280. "", 0,
  44281. "", 0,
  44282. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  44283. "", 0
  44284. },
  44285. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44286. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44287. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  44288. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  44289. "", 0,
  44290. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  44291. "", 0
  44292. },
  44293. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44294. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44295. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  44296. "", 0,
  44297. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  44298. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  44299. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  44300. },
  44301. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44302. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44303. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  44304. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  44305. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  44306. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  44307. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  44308. },
  44309. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44310. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44311. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  44312. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  44313. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  44314. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  44315. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  44316. },
  44317. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44318. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44319. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  44320. "\xd5", 17,
  44321. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  44322. "\xa0", 17,
  44323. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  44324. "\x08", 17,
  44325. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  44326. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  44327. "\xbd", 17
  44328. },
  44329. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  44330. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  44331. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  44332. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  44333. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  44334. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  44335. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  44336. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  44337. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  44338. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  44339. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  44340. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  44341. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  44342. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  44343. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  44344. "\x31", 81,
  44345. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  44346. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  44347. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  44348. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  44349. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  44350. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  44351. "\xa1", 81
  44352. }};
  44353. int i;
  44354. byte computedCiphertext[82];
  44355. byte computedPlaintext[82];
  44356. byte siv[AES_BLOCK_SIZE];
  44357. wc_test_ret_t ret = 0;
  44358. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  44359. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  44360. testVectors[i].assoc, testVectors[i].assocSz,
  44361. testVectors[i].nonce, testVectors[i].nonceSz,
  44362. testVectors[i].plaintext,
  44363. testVectors[i].plaintextSz, siv,
  44364. computedCiphertext);
  44365. if (ret != 0) {
  44366. return WC_TEST_RET_ENC_EC(ret);
  44367. }
  44368. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  44369. if (ret != 0) {
  44370. return WC_TEST_RET_ENC_NC;
  44371. }
  44372. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  44373. testVectors[i].ciphertextSz);
  44374. if (ret != 0) {
  44375. return WC_TEST_RET_ENC_NC;
  44376. }
  44377. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  44378. testVectors[i].assoc, testVectors[i].assocSz,
  44379. testVectors[i].nonce, testVectors[i].nonceSz,
  44380. computedCiphertext, testVectors[i].plaintextSz,
  44381. siv, computedPlaintext);
  44382. if (ret != 0) {
  44383. return WC_TEST_RET_ENC_EC(ret);
  44384. }
  44385. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  44386. testVectors[i].plaintextSz);
  44387. if (ret != 0) {
  44388. return WC_TEST_RET_ENC_NC;
  44389. }
  44390. }
  44391. return 0;
  44392. }
  44393. #endif
  44394. #undef ERROR_OUT
  44395. static const int fiducial4 = WC_TEST_RET_LN;
  44396. /* print the fiducial line numbers assigned above, allowing confirmation of
  44397. * source code version match when in doubt.
  44398. */
  44399. static void print_fiducials(void) {
  44400. printf(" [fiducial line numbers: %d %d %d %d]\n",
  44401. fiducial1, fiducial2, fiducial3, fiducial4);
  44402. }
  44403. #else
  44404. #ifndef NO_MAIN_DRIVER
  44405. int main(void) { return 0; }
  44406. #endif
  44407. #endif /* NO_CRYPT_TEST */