test.c 1.8 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. * Some common, optional build settings:
  23. * these can also be set in wolfssl/options.h or user_settings.h
  24. * -------------------------------------------------------------
  25. *
  26. * set the default devId for cryptocb to the value instead of INVALID_DEVID
  27. * WC_USE_DEVID=0x1234
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #ifndef WOLFSSL_USER_SETTINGS
  33. #include <wolfssl/options.h>
  34. #endif
  35. #include <wolfssl/wolfcrypt/settings.h>
  36. #ifndef NO_CRYPT_TEST
  37. #include <wolfssl/version.h>
  38. #include <wolfssl/wolfcrypt/types.h>
  39. #include <wolfssl/wolfcrypt/wc_port.h>
  40. #include <wolfssl/wolfcrypt/mem_track.h>
  41. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  42. #include <wolfssl/ssl.h>
  43. #define err_sys err_sys_remap /* remap err_sys */
  44. #include <wolfssl/test.h>
  45. #undef err_sys
  46. #endif
  47. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  48. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  49. #include <stdint.h>
  50. #endif
  51. #ifdef HAVE_STACK_SIZE_VERBOSE
  52. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  53. static WC_MAYBE_UNUSED ssize_t max_relative_stack =
  54. WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  55. #else
  56. static WC_MAYBE_UNUSED ssize_t max_relative_stack = -1;
  57. #endif
  58. #endif
  59. const byte const_byte_array[] = "A+Gd\0\0\0";
  60. #define CBPTR_EXPECTED 'A'
  61. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  62. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  63. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  64. #else
  65. static ssize_t max_relative_heap_allocs = -1;
  66. #endif
  67. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  68. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  69. #else
  70. static ssize_t max_relative_heap_bytes = -1;
  71. #endif
  72. #define PRINT_HEAP_CHECKPOINT() { \
  73. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  74. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  75. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  76. (long int)_rha, \
  77. _rha == 1 ? "" : "s", \
  78. (long int)_rhb); \
  79. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  80. return err_sys("heap allocs exceed designated max.", \
  81. WC_TEST_RET_ENC_NC); \
  82. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  83. return err_sys("heap bytes exceed designated max.", \
  84. WC_TEST_RET_ENC_NC); \
  85. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  86. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  87. }
  88. #else
  89. #define PRINT_HEAP_CHECKPOINT() WC_DO_NOTHING
  90. #endif /* WOLFSSL_TRACK_MEMORY_VERBOSE && !WOLFSSL_STATIC_MEMORY */
  91. #ifdef USE_FLAT_TEST_H
  92. #ifdef HAVE_CONFIG_H
  93. #include "test_paths.h"
  94. #endif
  95. #include "test.h"
  96. #else
  97. #ifdef HAVE_CONFIG_H
  98. #include "wolfcrypt/test/test_paths.h"
  99. #endif
  100. #include "wolfcrypt/test/test.h"
  101. #endif
  102. /* printf mappings */
  103. #ifndef WOLFSSL_LOG_PRINTF
  104. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  105. #include <mqx.h>
  106. #include <stdlib.h>
  107. /* see wc_port.h for fio.h and nio.h includes */
  108. #elif defined(FREESCALE_KSDK_BM)
  109. #include "fsl_debug_console.h"
  110. #undef printf
  111. #define printf PRINTF
  112. #elif defined(WOLFSSL_APACHE_MYNEWT)
  113. #include <assert.h>
  114. #include <string.h>
  115. #include "sysinit/sysinit.h"
  116. #include "os/os.h"
  117. #ifdef ARCH_sim
  118. #include "mcu/mcu_sim.h"
  119. #endif
  120. #include "os/os_time.h"
  121. #elif defined(WOLFSSL_ESPIDF)
  122. #include <time.h>
  123. #include <sys/time.h>
  124. #include <esp_log.h>
  125. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  126. #define ESPIDF_TAG "wc_test"
  127. #elif defined(WOLFSSL_ZEPHYR)
  128. #include <stdio.h>
  129. #define printf printk
  130. #elif defined(MICRIUM)
  131. #include <os.h>
  132. #if (OS_VERSION < 50000)
  133. #include <bsp_ser.h>
  134. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  135. #undef printf
  136. #define printf BSP_Ser_Printf
  137. #else
  138. #include <stdio.h>
  139. #endif
  140. #elif defined(WOLFSSL_PB)
  141. #include <stdarg.h>
  142. int wolfssl_pb_print(const char*, ...);
  143. #undef printf
  144. #define printf wolfssl_pb_print
  145. #elif defined(WOLFSSL_TELIT_M2MB)
  146. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  147. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  148. /* remap printf */
  149. #undef printf
  150. #define printf M2M_LOG_INFO
  151. /* OS requires occasional sleep() */
  152. #ifndef TEST_SLEEP_MS
  153. #define TEST_SLEEP_MS 50
  154. #endif
  155. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  156. /* don't use file system for these tests, since ./certs dir isn't loaded */
  157. #undef NO_FILESYSTEM
  158. #define NO_FILESYSTEM
  159. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  160. !defined(THREADX_NO_DC_PRINTF)
  161. #ifndef NETOS
  162. /* since just testing, use THREADX log printf instead (NETOS prototypes
  163. * this elsewhere) */
  164. int dc_log_printf(char*, ...);
  165. #endif
  166. #undef printf
  167. #define printf dc_log_printf
  168. #elif defined(ANDROID)
  169. #ifdef XMALLOC_USER
  170. #include <stdlib.h> /* we're using malloc / free direct here */
  171. #endif
  172. #ifndef STRING_USER
  173. #include <stdio.h>
  174. #endif
  175. #include <android/log.h>
  176. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  177. #ifndef NO_FILESYSTEM
  178. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  179. #endif
  180. #else
  181. #define printf(...) \
  182. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  183. #define fprintf(fp, ...) \
  184. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  185. #endif
  186. #elif defined(WOLFSSL_DEOS)
  187. #include <printx.h>
  188. #undef printf
  189. #define printf printx
  190. #elif defined(WOLFSSL_RENESAS_RSIP)
  191. #ifndef TEST_SLEEP
  192. #define TEST_SLEEP() vTaskDelay(50)
  193. #endif
  194. #undef vprintf
  195. #define vprintf rsip_vprintf
  196. #include <stdarg.h> /* for var args */
  197. int rsip_vprintf(const char* restrict format, va_list args)
  198. {
  199. int ret;
  200. char tmpBuf[80];
  201. ret = XVSNPRINTF(tmpBuf, sizeof(tmpBuf), format, args);
  202. printf(tmpBuf);
  203. return ret;
  204. }
  205. #else
  206. #ifdef XMALLOC_USER
  207. #include <stdlib.h> /* we're using malloc / free direct here */
  208. #endif
  209. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  210. #include <stdio.h>
  211. #endif
  212. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  213. #undef printf
  214. #define printf(...) ({})
  215. #endif
  216. /* enable way for customer to override test/bench printf */
  217. #ifdef XPRINTF
  218. #undef printf
  219. #define printf XPRINTF
  220. #elif !defined(printf)
  221. /* arrange for printf() to flush after every message -- this assures
  222. * redirected output (to a log file) records progress right up to the
  223. * moment of a crash/abort(); otherwise anything queued in stdout would
  224. * be lost.
  225. */
  226. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  227. #endif
  228. #endif
  229. #endif /* !WOLFSSL_LOG_PRINTF */
  230. #include <wolfssl/wolfcrypt/memory.h>
  231. #include <wolfssl/wolfcrypt/wc_port.h>
  232. #include <wolfssl/wolfcrypt/logging.h>
  233. #include <wolfssl/wolfcrypt/types.h>
  234. #include <wolfssl/wolfcrypt/asn.h>
  235. #include <wolfssl/wolfcrypt/md2.h>
  236. #include <wolfssl/wolfcrypt/md5.h>
  237. #include <wolfssl/wolfcrypt/md4.h>
  238. #include <wolfssl/wolfcrypt/sha.h>
  239. #include <wolfssl/wolfcrypt/sha256.h>
  240. #include <wolfssl/wolfcrypt/sha512.h>
  241. #include <wolfssl/wolfcrypt/hash.h>
  242. #include <wolfssl/wolfcrypt/rc2.h>
  243. #include <wolfssl/wolfcrypt/arc4.h>
  244. #if !defined(WC_NO_RNG)
  245. #include <wolfssl/wolfcrypt/random.h>
  246. #endif
  247. #include <wolfssl/wolfcrypt/wolfmath.h>
  248. #include <wolfssl/wolfcrypt/coding.h>
  249. #include <wolfssl/wolfcrypt/signature.h>
  250. #include <wolfssl/wolfcrypt/rsa.h>
  251. #include <wolfssl/wolfcrypt/des3.h>
  252. #include <wolfssl/wolfcrypt/aes.h>
  253. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  254. #include <wolfssl/wolfcrypt/cmac.h>
  255. #include <wolfssl/wolfcrypt/siphash.h>
  256. #include <wolfssl/wolfcrypt/poly1305.h>
  257. #include <wolfssl/wolfcrypt/camellia.h>
  258. #include <wolfssl/wolfcrypt/hmac.h>
  259. #include <wolfssl/wolfcrypt/kdf.h>
  260. #include <wolfssl/wolfcrypt/dh.h>
  261. #include <wolfssl/wolfcrypt/dsa.h>
  262. #include <wolfssl/wolfcrypt/srp.h>
  263. #include <wolfssl/wolfcrypt/chacha.h>
  264. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  265. #include <wolfssl/wolfcrypt/pwdbased.h>
  266. #include <wolfssl/wolfcrypt/ripemd.h>
  267. #include <wolfssl/wolfcrypt/error-crypt.h>
  268. #ifdef HAVE_ECC
  269. #include <wolfssl/wolfcrypt/ecc.h>
  270. #endif
  271. #ifdef WOLFSSL_SM2
  272. #include <wolfssl/wolfcrypt/sm2.h>
  273. #endif
  274. #ifdef HAVE_HPKE
  275. #include <wolfssl/wolfcrypt/hpke.h>
  276. #endif
  277. #ifdef HAVE_CURVE25519
  278. #include <wolfssl/wolfcrypt/curve25519.h>
  279. #endif
  280. #ifdef HAVE_ED25519
  281. #include <wolfssl/wolfcrypt/ed25519.h>
  282. #endif
  283. #ifdef HAVE_CURVE448
  284. #include <wolfssl/wolfcrypt/curve448.h>
  285. #endif
  286. #ifdef HAVE_ED448
  287. #include <wolfssl/wolfcrypt/ed448.h>
  288. #endif
  289. #ifdef WOLFSSL_HAVE_KYBER
  290. #include <wolfssl/wolfcrypt/kyber.h>
  291. #ifdef WOLFSSL_WC_KYBER
  292. #include <wolfssl/wolfcrypt/wc_kyber.h>
  293. #endif
  294. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  295. #include <wolfssl/wolfcrypt/ext_kyber.h>
  296. #endif
  297. #endif
  298. #if defined(WOLFSSL_HAVE_XMSS)
  299. #include <wolfssl/wolfcrypt/xmss.h>
  300. #ifdef HAVE_LIBXMSS
  301. #include <wolfssl/wolfcrypt/ext_xmss.h>
  302. #else
  303. #include <wolfssl/wolfcrypt/wc_xmss.h>
  304. #endif
  305. #endif
  306. #if defined(WOLFSSL_HAVE_LMS)
  307. #include <wolfssl/wolfcrypt/lms.h>
  308. #ifdef HAVE_LIBLMS
  309. #include <wolfssl/wolfcrypt/ext_lms.h>
  310. #else
  311. #include <wolfssl/wolfcrypt/wc_lms.h>
  312. #endif
  313. #endif
  314. #ifdef WOLFCRYPT_HAVE_ECCSI
  315. #include <wolfssl/wolfcrypt/eccsi.h>
  316. #endif
  317. #ifdef WOLFCRYPT_HAVE_SAKKE
  318. #include <wolfssl/wolfcrypt/sakke.h>
  319. #endif
  320. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  321. #include <wolfssl/wolfcrypt/blake2.h>
  322. #endif
  323. #ifdef WOLFSSL_SHA3
  324. #include <wolfssl/wolfcrypt/sha3.h>
  325. #endif
  326. #ifdef WOLFSSL_SM3
  327. #include <wolfssl/wolfcrypt/sm3.h>
  328. #endif
  329. #ifdef WOLFSSL_SM4
  330. #include <wolfssl/wolfcrypt/sm4.h>
  331. #endif
  332. #ifdef HAVE_LIBZ
  333. #include <wolfssl/wolfcrypt/compress.h>
  334. #endif
  335. #ifdef HAVE_PKCS7
  336. #include <wolfssl/wolfcrypt/pkcs7.h>
  337. #endif
  338. #ifdef HAVE_PKCS12
  339. #include <wolfssl/wolfcrypt/pkcs12.h>
  340. #endif
  341. #ifdef HAVE_FIPS
  342. #include <wolfssl/wolfcrypt/fips_test.h>
  343. #endif
  344. #ifdef HAVE_SELFTEST
  345. #include <wolfssl/wolfcrypt/selftest.h>
  346. #endif
  347. #ifdef WOLFSSL_ASYNC_CRYPT
  348. #include <wolfssl/wolfcrypt/async.h>
  349. #endif
  350. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  351. #include <wolfssl/wolfcrypt/logging.h>
  352. #endif
  353. #ifdef WOLFSSL_CAAM
  354. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  355. #endif
  356. #ifdef WOLF_CRYPTO_CB
  357. #include <wolfssl/wolfcrypt/cryptocb.h>
  358. #ifdef HAVE_INTEL_QA_SYNC
  359. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  360. #endif
  361. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  362. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  363. #endif
  364. #ifdef HAVE_RENESAS_SYNC
  365. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  366. #endif
  367. #endif
  368. #ifdef _MSC_VER
  369. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  370. #pragma warning(disable: 4996)
  371. #endif
  372. #ifdef OPENSSL_EXTRA
  373. #ifndef WOLFCRYPT_ONLY
  374. #include <wolfssl/openssl/evp.h>
  375. #include <wolfssl/openssl/hmac.h>
  376. #endif
  377. #include <wolfssl/openssl/rand.h>
  378. #include <wolfssl/openssl/aes.h>
  379. #include <wolfssl/openssl/des.h>
  380. #endif
  381. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  382. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  383. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  384. #define USE_CERT_BUFFERS_2048
  385. #endif
  386. #if !defined(USE_CERT_BUFFERS_256)
  387. #define USE_CERT_BUFFERS_256
  388. #endif
  389. #endif
  390. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  391. #define ENABLE_ECC384_CERT_GEN_TEST
  392. #endif
  393. #include <wolfssl/certs_test.h>
  394. #ifdef DEVKITPRO
  395. #include <wiiuse/wpad.h>
  396. #endif
  397. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  398. /* FIPS build has replaced ecc.h. */
  399. #define wc_ecc_key_get_priv(key) (&((key)->k))
  400. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  401. #endif
  402. #ifdef WOLFSSL_STATIC_MEMORY
  403. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  404. #else
  405. #define HEAP_HINT NULL
  406. #endif /* WOLFSSL_STATIC_MEMORY */
  407. /* these cases do not have intermediate hashing support */
  408. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  409. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  410. #define NO_INTM_HASH_TEST
  411. #endif
  412. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  413. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  414. defined(WOLFSSL_SECO_CAAM)
  415. #define HASH_SIZE_LIMIT
  416. #endif
  417. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  418. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  419. static void initDefaultName(void);
  420. #endif
  421. /* for async devices */
  422. #ifdef WOLFSSL_CAAM_DEVID
  423. static int devId = WOLFSSL_CAAM_DEVID;
  424. #else
  425. #ifdef WC_USE_DEVID
  426. static int devId = WC_USE_DEVID;
  427. #else
  428. static int devId = INVALID_DEVID;
  429. #endif
  430. #endif
  431. #ifdef HAVE_WNR
  432. const char* wnrConfigFile = "wnr-example.conf";
  433. #endif
  434. #define TEST_STRING "Everyone gets Friday off."
  435. #define TEST_STRING_SZ 25
  436. typedef struct testVector {
  437. const char* input;
  438. const char* output;
  439. size_t inLen;
  440. size_t outLen;
  441. } testVector;
  442. #ifndef WOLFSSL_TEST_SUBROUTINE
  443. #define WOLFSSL_TEST_SUBROUTINE
  444. #endif
  445. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  446. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  449. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  452. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  453. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  454. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  455. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  456. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  457. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  458. #if !defined(WOLFSSL_NOSHA512_224) && \
  459. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  461. #endif
  462. #if !defined(WOLFSSL_NOSHA512_256) && \
  463. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  464. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  465. #endif
  466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  470. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  472. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  473. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  474. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  475. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  476. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  477. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  478. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  479. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  480. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  481. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  482. defined(WOLFSSL_AFALG_XILINX_RSA)
  483. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  484. static wc_test_ret_t hkdf_test(void);
  485. #else
  486. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  487. #endif
  488. #endif /* HAVE_HKDF && ! NO_HMAC */
  489. #ifdef WOLFSSL_HAVE_PRF
  490. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  491. #ifdef WOLFSSL_BASE16
  492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void);
  493. #endif /* WOLFSSL_BASE16 */
  494. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  495. #endif /* WOLFSSL_HAVE_PRF */
  496. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  497. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void);
  498. #endif
  499. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  500. #ifdef WOLFSSL_TLS13
  501. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  502. #endif
  503. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  505. #ifdef WC_SRTP_KDF
  506. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void);
  507. #endif
  508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  509. #ifdef WC_RC2
  510. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  511. #endif
  512. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  514. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  515. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  516. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  517. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  519. #if defined(WOLFSSL_AES_CFB)
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void);
  521. #endif
  522. #ifdef WOLFSSL_AES_XTS
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void);
  524. #endif
  525. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  527. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  528. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  529. #if defined(WOLFSSL_SIPHASH)
  530. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  531. #endif
  532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  538. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  539. #ifdef WOLFSSL_SM4
  540. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  541. #endif
  542. #ifdef WC_RSA_NO_PADDING
  543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  544. #endif
  545. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  546. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  548. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  549. #ifndef WC_NO_RNG
  550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  551. #endif /* WC_NO_RNG */
  552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  554. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  555. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  556. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  557. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  558. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  559. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  561. #endif
  562. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  563. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void);
  564. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  565. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  566. #endif
  567. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  568. #ifdef HAVE_ECC
  569. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  570. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  571. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  572. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  573. #endif
  574. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  575. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  576. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  577. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  578. /* skip for ATECC508/608A, cannot import private key buffers */
  579. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  580. #endif
  581. #endif
  582. #ifdef HAVE_CURVE25519
  583. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  584. #endif
  585. #ifdef HAVE_ED25519
  586. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  587. #endif
  588. #ifdef HAVE_CURVE448
  589. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  590. #endif
  591. #ifdef HAVE_ED448
  592. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  593. #endif
  594. #ifdef WOLFSSL_HAVE_KYBER
  595. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  596. #endif
  597. #if defined(WOLFSSL_HAVE_XMSS)
  598. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  599. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void);
  600. #endif
  601. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  602. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void);
  603. #endif
  604. #endif
  605. #if defined(WOLFSSL_HAVE_LMS)
  606. #if !defined(WOLFSSL_SMALL_STACK)
  607. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  608. defined(HAVE_LIBLMS)
  609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void);
  610. #endif
  611. #endif
  612. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  613. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void);
  614. #endif
  615. #endif
  616. #ifdef WOLFCRYPT_HAVE_ECCSI
  617. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  618. #endif
  619. #ifdef WOLFCRYPT_HAVE_SAKKE
  620. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  621. #endif
  622. #ifdef HAVE_BLAKE2
  623. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  624. #endif
  625. #ifdef HAVE_BLAKE2S
  626. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  627. #endif
  628. #ifdef HAVE_LIBZ
  629. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  630. #endif
  631. #ifdef HAVE_PKCS7
  632. #ifndef NO_PKCS7_ENCRYPTED_DATA
  633. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  634. #endif
  635. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  636. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  637. #endif
  638. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  639. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  640. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  641. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  642. #endif
  643. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  644. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  645. word32 keySz);
  646. #endif
  647. #endif
  648. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  649. !defined(NO_FILESYSTEM)
  650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  651. #endif
  652. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  653. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  654. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  655. #endif
  656. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  657. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  659. #endif
  660. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  661. #if defined(WOLFSSL_PUBLIC_MP) && \
  662. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  663. defined(USE_FAST_MATH))
  664. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  665. #endif
  666. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  667. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  668. #endif
  669. #if defined(ASN_BER_TO_DER) && \
  670. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  671. defined(OPENSSL_EXTRA_X509_SMALL))
  672. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  673. #endif
  674. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  675. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  676. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  677. #endif
  678. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  679. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  680. #else
  681. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  682. #endif
  683. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  684. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  685. #endif
  686. #ifdef WOLFSSL_CAAM_BLOB
  687. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  688. #endif
  689. #ifdef HAVE_ARIA
  690. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  691. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  692. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  693. #endif
  694. #ifdef WOLF_CRYPTO_CB
  695. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  696. #endif
  697. #ifdef WOLFSSL_CERT_PIV
  698. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  699. #endif
  700. #ifdef WOLFSSL_AES_SIV
  701. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  702. #endif
  703. #if defined(WOLFSSL_AES_EAX) && \
  704. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  705. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void);
  706. #endif /* WOLFSSL_AES_EAX */
  707. /* General big buffer size for many tests. */
  708. #define FOURK_BUF 4096
  709. /* If not defined in user_settings, the ERROR_OUT pause is 120 seconds. */
  710. #ifndef WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION
  711. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION 120
  712. #endif
  713. #if defined(WOLFSSL_ESPIDF_ERROR_PAUSE)
  714. #if defined(CONFIG_FREERTOS_HZ)
  715. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  716. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * CONFIG_FREERTOS_HZ)
  717. #else
  718. /* If not defined, assume RTOS is 1000 ticks per second. */
  719. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  720. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * 1000)
  721. #endif
  722. /* When defined, pause at error condition rather than exit with error. */
  723. #define ERROR_OUT(err, eLabel) \
  724. do { \
  725. ret = (err); \
  726. ESP_LOGE(ESPIDF_TAG, "Failed: Error = %d during %s, line %d", \
  727. err, __FUNCTION__, __LINE__); \
  728. ESP_LOGI(ESPIDF_TAG, "Extended system info:"); \
  729. esp_ShowExtendedSystemInfo(); \
  730. ESP_LOGW(ESPIDF_TAG, "Paused for %d seconds! " \
  731. "WOLFSSL_ESPIDF_ERROR_PAUSE is enabled.", \
  732. WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION); \
  733. vTaskDelay(WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS); \
  734. goto eLabel; \
  735. } while (0)
  736. #else
  737. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  738. #endif
  739. /* Not all unexpected conditions are actually errors .*/
  740. #define WARNING_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  741. static void render_error_message(const char* msg, wc_test_ret_t es)
  742. {
  743. (void)msg;
  744. (void)es;
  745. #ifdef WOLFSSL_LINUXKM
  746. #define err_sys_printf lkm_printf
  747. #else
  748. #define err_sys_printf printf
  749. #endif
  750. switch (WC_TEST_RET_DEC_TAG(es)) {
  751. case WC_TEST_RET_TAG_NC:
  752. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  753. break;
  754. case WC_TEST_RET_TAG_EC:
  755. #ifdef NO_ERROR_STRINGS
  756. err_sys_printf("%s error L=%d code=%d\n", msg,
  757. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  758. #else
  759. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  760. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  761. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  762. #endif
  763. break;
  764. case WC_TEST_RET_TAG_ERRNO:
  765. {
  766. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  767. * flavor that always returns a non-null char pointer that must be used
  768. * directly, and a POSIX flavor that returns an error int, and iff success,
  769. * stores an error string in the supplied buffer. this is all most
  770. * infelicitous...
  771. */
  772. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  773. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  774. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  775. (defined(__USE_XOPEN2K) && \
  776. defined(_POSIX_C_SOURCE) && \
  777. (_POSIX_C_SOURCE >= 200112L)))
  778. char errno_buf[64], *errno_string;
  779. /* precisely mirror the gate used in glibc string.h */
  780. #if defined __USE_XOPEN2K && !defined __USE_GNU
  781. if (strerror_r(WC_TEST_RET_DEC_I(es),
  782. errno_buf, sizeof(errno_buf)) != 0)
  783. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  784. errno_string = errno_buf;
  785. #else
  786. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  787. errno_buf, sizeof(errno_buf));
  788. #endif
  789. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  790. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  791. errno_string);
  792. #else /* can't figure out how to strerror_r(), or don't want error strings */
  793. err_sys_printf("%s error L=%d errno=%d\n", msg,
  794. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  795. #endif
  796. break;
  797. }
  798. case WC_TEST_RET_TAG_I:
  799. err_sys_printf("%s error L=%d i=%d\n", msg,
  800. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  801. break;
  802. }
  803. #undef err_sys_printf
  804. }
  805. static void print_fiducials(void);
  806. #ifdef HAVE_STACK_SIZE
  807. static THREAD_RETURN err_sys(const char* msg, int es)
  808. #else
  809. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  810. #endif
  811. {
  812. render_error_message(msg, es);
  813. print_fiducials();
  814. #ifdef WOLFSSL_LINUXKM
  815. EXIT_TEST(es);
  816. #else
  817. EXIT_TEST(-1);
  818. #endif
  819. }
  820. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  821. /* func_args from test.h, so don't have to pull in other stuff */
  822. typedef struct func_args {
  823. int argc;
  824. char** argv;
  825. wc_test_ret_t return_code;
  826. } func_args;
  827. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  828. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  829. static void myFipsCb(int ok, int err, const char* hash)
  830. {
  831. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  832. printf("message = %s\n", wc_GetErrorString(err));
  833. printf("hash = %s\n", hash);
  834. if (err == IN_CORE_FIPS_E) {
  835. printf("In core integrity hash check failure, copy above hash\n");
  836. printf("into verifyCore[] in fips_test.c and rebuild\n");
  837. }
  838. }
  839. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  840. #ifdef WOLFSSL_STATIC_MEMORY
  841. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  842. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  843. #elif defined(BENCH_EMBEDDED)
  844. static byte gTestMemory[14000];
  845. #elif defined(WOLFSSL_CERT_EXT)
  846. static byte gTestMemory[140000];
  847. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  848. !defined(ALT_ECC_SIZE)
  849. static byte gTestMemory[160000];
  850. #else
  851. static byte gTestMemory[80000];
  852. #endif
  853. #endif
  854. #ifdef WOLFSSL_PB
  855. static int wolfssl_pb_print(const char* msg, ...)
  856. {
  857. int ret;
  858. va_list args;
  859. char tmpBuf[80];
  860. va_start(args, msg);
  861. ret = vsprint(tmpBuf, msg, args);
  862. va_end(args);
  863. fnDumpStringToSystemLog(tmpBuf);
  864. return ret;
  865. }
  866. #endif /* WOLFSSL_PB */
  867. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  868. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  869. /* Enable support for RNG with crypto callback */
  870. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  871. {
  872. int rc = CRYPTOCB_UNAVAILABLE;
  873. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  874. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  875. }
  876. (void)ctx;
  877. (void)thisDevId;
  878. return rc;
  879. }
  880. #endif
  881. #if defined(WC_KDF_NIST_SP_800_56C)
  882. #define INIT_SP80056C_TEST_VECTOR(_z, _fixedInfo, _derivedKey, _hashType) \
  883. { \
  884. .z = (const byte*)_z, .zSz = sizeof(_z) - 1, \
  885. .fixedInfo = (const byte*)_fixedInfo, \
  886. .fixedInfoSz = sizeof(_fixedInfo) - 1, \
  887. .derivedKey = (const byte*)_derivedKey, \
  888. .derivedKeySz = sizeof(_derivedKey) - 1, .hashType = _hashType, \
  889. }
  890. #define SP800_56C_MAX_OUT 128
  891. static WOLFSSL_TEST_SUBROUTINE wc_test_ret_t nist_sp80056c_kdf_test(void)
  892. {
  893. struct sp800_56c_test_vector {
  894. const byte* z;
  895. word32 zSz;
  896. const byte* fixedInfo;
  897. word32 fixedInfoSz;
  898. const byte* derivedKey;
  899. word32 derivedKeySz;
  900. enum wc_HashType hashType;
  901. };
  902. struct sp800_56c_test_vector* v;
  903. byte output[SP800_56C_MAX_OUT];
  904. word32 i;
  905. int ret;
  906. /* vectors from
  907. * https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
  908. *
  909. * (KeyManagement) */
  910. struct sp800_56c_test_vector vctors[] = {
  911. #if !defined(NO_SHA)
  912. /* SHA-1 */
  913. INIT_SP80056C_TEST_VECTOR(
  914. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  915. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  916. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  917. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  918. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  919. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  920. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  921. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  922. WC_HASH_TYPE_SHA),
  923. INIT_SP80056C_TEST_VECTOR(
  924. "\xae\x64\xab\x2b\x2b\x75\xa9\x4c\xf8\xef\x24\xda\x24\x56\xbd\x3a"
  925. "\xa3\x6d\xb6\x14\x29\xea\x55\x21",
  926. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  927. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  928. "\xcc\x96\x5a\x52\xd0\x5c\x94\x9e\x52\xc0\x35\xfd\x03\x53\x0d\xb7"
  929. "\xea\xa4\x08\x70\x2c\x9d\x35\x21\x1e\x67\x21\x54\x12\x45\x91\x51"
  930. "\xba\x22\x62\xbd\x1e\x28\xe5\x6b",
  931. WC_HASH_TYPE_SHA),
  932. INIT_SP80056C_TEST_VECTOR(
  933. "\x71\x5d\xc0\xea\x24\x6b\x46\x56\x63\xa8\x9c\xde\x04\x12\xc1\x92"
  934. "\xe1\x9e\x58\xd5\xb0\xb6\x36\x6d\xa7\x89\xad\xbf\x44\x9a\x38\xaa"
  935. "\x46\x69\xfe\x36\x30\xa2\x0f\x7f\xa3\x14\x9c\x9b\x4b\x0a\xb5\xcd"
  936. "\x3e\x14\x18\x2b\x75\x04\xd5\xd2\x75\x2b\xf6\x58\x7a\xab\xc9\xf4"
  937. "\xcb\x8f\xe5\x29\x23\x6a\xb8\x15\x36\xad\xd2\xbd\x25\xd6\xbf\x9d"
  938. "\x5f\x1d\xf5\x76\x16\x5a\xa5\x5c\x24\x99\x61\xd8\xf8\x75\x00\xed"
  939. "\x8d\xbf\xc5\xd2\x50\x53\x4c\x07\xd9\x9a\xc9\x17\xf9\x84\x60\x46"
  940. "\xac\x5c\xb8\xa2\x98\x74\x26\x22\xd3\xc9\x86\x18\x06\x92\x46\xe8"
  941. "\xad\x37\x11\x25\x57\xe0\xe6\x34\xb5\x81\x32\x7a\x4a\xd3\x2c\x7c"
  942. "\x76\x4b\xe8\xf8\x08\x0d\x37\x2c\x63\x20\x93\xa7\x67\xf1\x55\xbd"
  943. "\x22\xec\x00\x3c\xa6\x1c\x8b\x43\x32\x0f\x3a\xbe\xb5\xdd\xc4\xa3"
  944. "\xb1\x89\x82\xfd\xd6\x51\x0f\x88\x3c\x8d\xc1\xe0\xb1\x57\xff\xb9"
  945. "\xcf\xc0\xa5\x9c\xe2\xd4\x05\x5f\xfc\x73\xe7\x15\x2a\x6a\x95\x43"
  946. "\xb9\x19\xe7\x94\xe9\x49\x61\x33\xbe\x2a\x23\x18\xd9\x05\x6e\xfd"
  947. "\x74\x48\x2d\xc6\x3c\x0d\xb5\x8f\xe6\x42\x6b\x0f\xe7\x35\x45\x42"
  948. "\xc2\x19\x7a\xb6\xbd\x35\xf1\xa9\x2d\xce\x90\xb1\xc4\x6d\x32\xc1",
  949. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  950. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  951. "\x1b\x5f\xcc\x8d\x81\xa5\xd9\xa9\x36\x94\xe6\x47\x77\x88\xd8\x03"
  952. "\x15\x84\xc3\xf5\x2e\x9f\x11\x7a\xe8\x18\x4d\xba\x56\x47\x9d\x87"
  953. "\x66\x76\x92\xf1\x2d\x7b\xd3\x8b",
  954. WC_HASH_TYPE_SHA),
  955. INIT_SP80056C_TEST_VECTOR(
  956. "\x33\xe0\x50\xbd\x20\x9f\x2d\xf2\x77\x19\x78\xfc\xd1\xd4\xc8\x2e"
  957. "\x49\xd0\x1d\x65\xbb\x62\x03\x20\xd3\x0b\xfe\xa8\x7a\xa8\x69\xe1"
  958. "\x07\xa5\x17\xa4\xc8\x5b\x69\x28\x45\x21\xca\x54\xb7\x7f\x59\xe9"
  959. "\x4a\x85\x6d\xaa\x30\xa3\x85\xa5\x25\xd8\xa3\xf7\xe1\x5e\xe5\xe9"
  960. "\xaa\x12\x8d\x45\xef\x63\xf9\x0c\x10\xe0\x8f\xc5\x26\x36\x13\x77"
  961. "\x81\x54\x7a\x58\x9f\x97\x87\xf9\xd7\xdd\x61\x43\x41\x9a\x26\x16"
  962. "\x80\x16\x82\x40\xaa\xb2\x01\x3d\x80\x20\xdb\xe8\x4b\x7e\x2b\xed"
  963. "\xce\x67\x1b\x94\x03\xbd\x1f\x91\x71\xa2\x57\x90\xce\x66\x7d\xed",
  964. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  965. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  966. "\x35\x10\x85\x85\xfc\x55\x62\xb5\xf1\x8f\x20\x7b\xa8\x35\x63\xe4"
  967. "\x49\x28\x45\x6a\x5e\x53\x63\x7e\xae\x6b\xb5\xfc\x33\x88\xae\x02"
  968. "\x91\x79\x0a\x1d\xc8\x31\x4e\x28",
  969. WC_HASH_TYPE_SHA),
  970. INIT_SP80056C_TEST_VECTOR(
  971. "\x5c\x80\x4f\x45\x4d\x30\xd9\xc4\xdf\x85\x27\x1f\x93\x52\x8c\x91"
  972. "\xdf\x6b\x48\xab\x5f\x80\xb3\xb5\x9c\xaa\xc1\xb2\x8f\x8a\xcb\xa9"
  973. "\xcd\x3e\x39\xf3\xcb\x61\x45\x25\xd9\x52\x1d\x2e\x64\x4c\x53\xb8"
  974. "\x07\xb8\x10\xf3\x40\x06\x2f\x25\x7d\x7d\x6f\xbf\xe8\xd5\xe8\xf0"
  975. "\x72\xe9\xb6\xe9\xaf\xda\x94\x13\xea\xfb\x2e\x8b\x06\x99\xb1\xfb"
  976. "\x5a\x0c\xac\xed\xde\xae\xad\x7e\x9c\xfb\xb3\x6a\xe2\xb4\x20\x83"
  977. "\x5b\xd8\x3a\x19\xfb\x0b\x5e\x96\xbf\x8f\xa4\xd0\x9e\x34\x55\x25"
  978. "\x16\x7e\xcd\x91\x55\x41\x6f\x46\xf4\x08\xed\x31\xb6\x3c\x6e\x6d",
  979. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  980. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  981. "\xfa\xa0\x22\xce\x7f\xa9\xba\x95\xeb\xa3\x9f\x3f\x44\xf3\xee\x14"
  982. "\x96\x0a\x0b\x23\x9d\x01\x4b\x57\x70\xe4\x71\xd7\x5a\x99\xea\x87"
  983. "\x10\xe3\x8f\x0c\xef\x0f\xfc\x67",
  984. WC_HASH_TYPE_SHA),
  985. INIT_SP80056C_TEST_VECTOR(
  986. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  987. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  988. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  989. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  990. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  991. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  992. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  993. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  994. WC_HASH_TYPE_SHA),
  995. #endif
  996. #if defined(WOLFSSL_SHA224)
  997. /* SHA-224*/
  998. INIT_SP80056C_TEST_VECTOR(
  999. "\x8c\x5d\x6e\x5d\x36\x06\x83\xba\x55\xb0\x9d\xb1\x69\x6d\x7c\x64"
  1000. "\x02\xff\x87\x88\x5f\xf5\x07\x70\xf2\x76\x7b\x75\x54\x60\x20\x7e"
  1001. "\xd5\xc7\x43\xfd\x27\xe7\xeb\x1d\x0c\xa5\x91\xf8\x56\x38\x93\x11"
  1002. "\x73\x07\x44\xf2\x04\xd2\xe5\x5b\x8b\xd4\x46\xce\xca\x03\x1f\x7b"
  1003. "\xac\xcf\xf1\xa7\x1b\x68\x34\x59\xcc\x54\xd5\x01\xda\xbf\x4a\x84"
  1004. "\xcd\xb8\x6d\xfa\xda\xff\x31\x0f\xbd\xba\xf7\x4d\xd5\x1b\xa1\xe1"
  1005. "\xe1\x19\x1a\xf1\x4c\x9b\xf8\x94\x43\xbf\x58\x8e\x9c\xe3\x30\x34"
  1006. "\xaf\x5e\x89\xbf\x6f\xfc\x47\xd7\xd9\xca\x4a\x5e\x8f\xf8\xa0\x50"
  1007. "\x20\xbb\x0f\x95\xbc\xde\x01\x56\xd8\x7f\xb8\x60\xbd\x40\x83\xfa"
  1008. "\x5b\x53\x1a\x08\xa4\xfb\x7e\xe0\x20\x1a\xe8\xb3\xcc\xff\xe9\x9f"
  1009. "\x27\x0b\xc3\x53\x4b\xaa\xcf\xc0\x01\xcd\xd8\x0a\xd8\x7c\xce\x71"
  1010. "\xf0\x91\xe7\x66\xca\x5c\xc2\x75\xcb\x49\x14\x5a\x5e\xe6\x16\x2e"
  1011. "\xcc\xf5\x58\xce\xc4\xd3\xee\x53\x1e\x91\xe9\xa5\x29\x69\x63\x4d"
  1012. "\x3a\xf8\xd2\x6f\x8d\x15\xdc\x0d\x6f\x6e\x0a\x97\x4b\xe4\x34\x1b"
  1013. "\x68\xa0\x19\x90\xdb\xb8\x64\x95\x89\x1a\xd3\xaf\xc1\xe4\xce\xdf"
  1014. "\x4c\x6a\xe1\xf1\xcd\x60\x81\xcd\xee\xd8\xe6\xb3\x26\x4e\xc3\xbe"
  1015. "\x24\x58\xd1\xc0\xca\x24\x43\x41\x0c\xf3\xb4\x7c\x0c\x25\x4c\x7d"
  1016. "\xc8\xec\xb4\x3e\x6c\x23\x64\xe1\xc0\x62\x19\xcc\x7e\xfb\xff\xbb"
  1017. "\x63\xd7\xff\xfc\x74\x58\x12\xfd\x24\x0c\x33\xd4\x96\xb9\x99\x2f"
  1018. "\x96\x80\xa6\x3c\x07\x96\x3c\x0c\x49\xf3\xc1\xba\xef\xec\xaf\x32"
  1019. "\xe2\xaa\x8a\x2f\x7c\xd3\x0d\x8f\x05\x1e\xe2\xf5\x0f\xbf\x05\xab"
  1020. "\x13\x96\xa4\xea\x87\x44\x7d\x7b\x98\x1b\x5e\x46\x14\x28\x18\x71"
  1021. "\xa6\xf0\xf6\xbf\x1f\xe0\x02\x2f\x7e\xa1\x32\xbe\x0a\xe9\x19\x26"
  1022. "\xab\x12\xaf\x6d\xc4\x50\x64\xaa\xd5\x6b\x84\xb9\x0c\x70\x08\x37"
  1023. "\x09\xcf\xf7\xe3\x1b\x54\x8f\xb7\xfb\x2c\xf7\x5a\xbf\x96\xe0\x1c"
  1024. "\xcd\x3e\x94\x2e\xed\x91\x48\x0d\x4c\x24\xc6\xb7\xf9\x79\xfb\xbe"
  1025. "\x5d\xa2\x39\xb3\x76\x16\x7d\x68\x57\x35\x24\xff\xcb\x50\x99\x54"
  1026. "\xcc\x80\xa0\xe1\xa7\x1c\x40\xc4\xda\x17\xb8\xd1\x57\x2b\x21\x58"
  1027. "\x7a\x8d\x66\xcc\x62\x1c\x7c\xd1\x0f\x49\xab\xd5\xef\x86\x31\x13"
  1028. "\xe6\x19\x21\x08\x6f\xac\x25\x31\x2b\x74\x1c\x11\xa8\xfb\xc1\xe3"
  1029. "\x3c\x34\xd9\xda\x14\xa8\x22\x47\x7f\xcc\x36\x66\x70\x25\xc4\xf1"
  1030. "\x30\xae\x10\x0e\x36\xf1\x5d\xa0\x03\x74\xce\x87\x41\x67\x9f\x61",
  1031. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1032. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1033. "\x4b\x99\x6a\x60\xc0\x4a\x35\xc5\xe6\xd4\x74\xb1\x0a\x25\x8d\x56"
  1034. "\x2e\xa6\xdc\x52\xf6\xc6\x9b\xf3\x9e\xf8\x8c\x89\xe3\xcc\x8a\x54"
  1035. "\xda\x2f\x3c\x0b\x56\x1b\x53\xfe\x76\x55\x13\x63\xd6\x9c\x3c\xef"
  1036. "\x74\xe3\x4f\xe8\x8e\xb3\xac\x51",
  1037. WC_HASH_TYPE_SHA224),
  1038. INIT_SP80056C_TEST_VECTOR(
  1039. "\x52\x27\x2f\x50\xf4\x6f\x4e\xdc\x91\x51\x56\x90\x92\xf4\x6d\xf2"
  1040. "\xd9\x6e\xcc\x3b\x6d\xc1\x71\x4a\x4e\xa9\x49\xfa\x9f\x18\xff\x54"
  1041. "\xf8\x87\x23\x07\x3f\x64\xa6\x95\x3d\x04\x91\x4f\x45\xa2\x3e\xee"
  1042. "\x7c\xfc\x46\x67\x08\x0a\xa0\xf9",
  1043. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1044. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1045. "\x77\x56\xbc\xfd\xef\x3e\xe6\x9f\x6a\xc2\x3c\xd2\xdc\x60\x7d\x01"
  1046. "\xfa\x8c\xe1\xb2\x4f\x5c\xaa\xaa\x48\xe0\x4b\x81\x63\xe1\x73\x3a"
  1047. "\xed\x7a\x04\x0e\x73\xf2\xb5\x42\x36\x8f\x00\x54\x8b\x16\x3c\x3d"
  1048. "\xc9\x6d\x70\x09\x99\x16\xf1\x6b",
  1049. WC_HASH_TYPE_SHA224),
  1050. #endif
  1051. #if !defined(NO_SHA256)
  1052. /* SHA-256 */
  1053. INIT_SP80056C_TEST_VECTOR(
  1054. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1055. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1056. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1057. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1058. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1059. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1060. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1061. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1062. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1063. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc\xa6",
  1064. WC_HASH_TYPE_SHA256),
  1065. INIT_SP80056C_TEST_VECTOR(
  1066. "\x44\xf4\x84\x09\xf3\x1b\xf3\x50\x94\x51\xdb\x4d\x30\x4b\xeb\xd8"
  1067. "\x3c\x2a\xd6\x50\x1c\x1b\x85\xe6\x32\xbc\x92\x58\x8e\x2d\x48\xb5"
  1068. "\xd2\xb8\x44\x44\x62\xad\x94\xe8\xa4\x44\x94\x1e\xd4\x97\x5c\x97"
  1069. "\x91\x17\xd7\x5a\x0a\x2b\xa8\x10\xdf\xa8\x80\x4a\x0f\xe9\x42\x6d"
  1070. "\xd7\xeb\x95\xf2\x9c\xa4\x30\xda\x37\xef\xa5\x2c\x42\xda\x1d\xe2"
  1071. "\x23\x76\x3b\xdd\xc9\x5e\x46\x6a\xa3\xb8\xd2\x06\xb8\x21\x8e\xdf"
  1072. "\x23\x97\x3d\x05\xf3\xc7\xc2\x2b\x22\x46\x53\xd4\xf9\x21\x85\x45"
  1073. "\x79\x83\x26\x2f\x27\xbc\x55\xa0\xa7\xae\xe5\x43\x54\x55\xd4\x3c"
  1074. "\x0e\x0c\x6d\x80\x67\xad\xaf\x90\x42\xb6\xb7\x77\x7e\x19\x8d\x67"
  1075. "\x60\x83\x0d\x96\xb2\x9a\x6a\xae\xf5\x74\xd7\x5e\x2d\x65\x43\x55"
  1076. "\x0f\x75\x55\xcf\x89\x82\xc8\x21\xc4\xec\x9b\x82\x66\x2b\x09\x36"
  1077. "\x69\xfd\x24\x6d\x4d\xcc\xbf\xf3\x1d\x98\x4e\xf6\x3c\x4f\x95\xd7"
  1078. "\x39\xc7\x7e\x66\xf6\x9b\x6d\xb0\xd9\xfc\x1a\x97\x3f\x52\x33\xd1"
  1079. "\x1f\xe7\x15\x4b\x8a\x6b\xe1\x50\x46\x7d\x92\xda\xf0\x91\x44\x34"
  1080. "\xf0\xcf\x17\x6e\x96\x1b\x31\x3f\xe3\xe2\x3b\xfe\x37\x8b\x87\xba"
  1081. "\xf2\x73\x93\x7f\xfa\xa6\x85\x65\x8b\x09\x78\x1d\x26\x97\x86\x4b"
  1082. "\x0d\x82\x48\x5c\xf7\xd6\x2f\xfc\xea\x34\x33\x88\xcf\xec\x15\x27"
  1083. "\x3a\x39\x4b\x84\x32\xee\xb6\x32\x1b\xe3\x1d\x4f\xcd\x16\x15\xc8"
  1084. "\x1c\x69\x37\xc0\x8d\x92\x41\x6e\xdb\xdd\x20\x10\xfa\x8b\x6e\x0a"
  1085. "\x8a\x60\x3d\xad\xe0\x10\xbc\x9c\xd7\x6b\x14\xe3\xe2\x19\x0e\x3b"
  1086. "\xa3\x00\xe7\x1a\xdd\xbf\x24\x4c\xf8\x06\x29\x49\x76\xea\xf0\x7d"
  1087. "\x02\x3b\xdb\x57\xfc\x5d\x19\x64\xc7\xd4\x20\x6e\x72\x06\x1b\xfe"
  1088. "\x1e\xe4\xee\xbc\x92\x00\xe1\x3a\x6c\xba\x32\x86\x9d\xbe\x80\x82"
  1089. "\xcd\xf3\x64\x5b\x5a\x72\x7d\xd2\x7c\xa5\x03\xf4\xed\xeb\x73\xe8"
  1090. "\x8a\x3a\x55\x2c\x7e\x00\xd4\xee\x72\x42\x13\x72\x36\xa0\x96\x5c"
  1091. "\x1e\xc3\xeb\xc0\xb4\x8c\x2b\x46\x7e\xb8\x42\x41\x5a\x28\x3f\x55"
  1092. "\xe2\x20\xff\xd1\x88\x19\x25\x6d\xa2\x47\x4d\x28\xfc\x3b\x04\xe0"
  1093. "\xc0\x7e\x4d\x25\xc1\x74\x93\x41\xd2\x22\x97\x01\x5c\xd8\x17\x8c"
  1094. "\x39\x18\xbe\x8c\x5c\xdf\x0f\xbf\xbb\x9a\x5a\xcc\xdd\x82\xaf\x07"
  1095. "\x83\xef\xe4\xdf\x64\xa8\xd8\x92\x82\x8f\x8d\xe5\x8c\x5d\x56\x9b"
  1096. "\x5b\x08\x45\x58\x96\xc4\xd3\xc3\x4f\xd3\xce\x93\xc4\x34\xc3\x8e"
  1097. "\xf5\x6c\xed\x30\x56\x1c\x37\x1a\xf9\xf2\xd8\x64\xfd\xc5\xb6\x2f",
  1098. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1099. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1100. "\x42\x35\xac\x89\xc9\xf3\x3e\x5d\xbb\x11\x60\x1a\x29\x83\xe3\x76"
  1101. "\xe2\x15\x39\x97\x87\xd8\x39\xa0\x38\x55\xfe\x04\x53\x3e\x1a\x67"
  1102. "\x66\x91\x5e\x67\xc2\x5e\xeb\x04\x08\x28\xe9\x6c\xd3\xd6\xef\x0a"
  1103. "\xb7\xd1\x7b\x43\x13\x43\xa7\xf3\xaa\xc6\x8f\x0c\x4a\x7e\x77\x9b",
  1104. WC_HASH_TYPE_SHA256),
  1105. /* slightly modified vector to test generation when derivedKeySz %
  1106. * hashOutSz != 0*/
  1107. INIT_SP80056C_TEST_VECTOR(
  1108. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1109. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1110. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1111. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1112. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1113. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1114. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1115. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1116. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1117. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc",
  1118. WC_HASH_TYPE_SHA256),
  1119. #endif
  1120. #if defined(WOLFSSL_SHA384)
  1121. /* SHA-384 */
  1122. INIT_SP80056C_TEST_VECTOR(
  1123. "\x5e\xa1\xfc\x4a\xf7\x25\x6d\x20\x55\x98\x1b\x11\x05\x75\xe0\xa8"
  1124. "\xca\xe5\x31\x60\x13\x7d\x90\x4c\x59\xd9\x26\xeb\x1b\x84\x56\xe4"
  1125. "\x27\xaa\x8a\x45\x40\x88\x4c\x37\xde\x15\x9a\x58\x02\x8a\xbc\x0e"
  1126. "\x88\x76\x9c\xb7\x2f\xc5\xac\x45\x7c\xd5\x8e\x89\x08\x9b\x19\x6a"
  1127. "\x70\xbf\x53\x3c\x6d\xc9\x1c\x9c\x7e\x17\x41\xdb\x5e\x7a\xb6\xb0"
  1128. "\x84\x9f\x01\xde\xa6\x5f\xed\xd0\x6c\x77\x18\x7c\xd8\x8e\xd0\x30",
  1129. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1130. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1131. "\x8e\x6e\x26\x5f\x20\x82\xf1\x4d\x34\xda\x23\xe1\x03\x2c\x90\x24"
  1132. "\x83\x4a\xf0\x15\x72\xb6\x64\x77\x82\x41\x1b\xdd\xcb\x84\xa5\xda"
  1133. "\xee\x11\x7b\xa6\xfb\xa6\xd0\xeb\x28\x08\xef\x8a\xb0\x70\x05\xee"
  1134. "\xab\xe5\x2d\x2e\xfd\x31\x12\x1c\x7b\xf9\xd5\xfa\xfc\x40\xe0\x0c"
  1135. "\x6d\x6d\xbf\x39\xef\x43\xfe\x97\x15\xc7\x20\x2c\xdc\x2d\xb7\xe8"
  1136. "\x2b\x88\xd7\x48\xeb\x84\x25\x8b\xf8\x4d\x85\x82\xf2\xbf\xd9\x40",
  1137. WC_HASH_TYPE_SHA384),
  1138. #endif
  1139. #if defined(WOLFSSL_SHA512)
  1140. /* SHA-512 */
  1141. INIT_SP80056C_TEST_VECTOR(
  1142. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1143. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1144. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1145. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1146. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1147. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1148. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1149. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1150. "\xde\x34\x54\xe4",
  1151. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1152. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1153. "\x2d\x4a\x46\xa1\x70\x99\xba\xa8\x33\x0b\xc5\x9d\x4a\x1c\xf5\xae"
  1154. "\x3a\x30\x75\xb4\xc6\x2b\xb2\x6e\x7f\xc9\x89\x24\x72\x6d\x27\x4c"
  1155. "\x09\x64\x6f\x44\x08\xe6\x85\x8c\x43\xb4\x2d\xae\xd0\x15\xef\x26"
  1156. "\x17\x08\xd5\x5e\xf2\x4d\xaa\x7d\x3e\xa3\xd1\xc4\xa0\x8c\xfd\x24"
  1157. "\xdb\x60\x00\xa5\xb8\xa6\x7d\xe7\x46\xf3\xd3\xf4\xff\x34\x85\x15"
  1158. "\x8f\xd3\xb6\x91\x55\x79\x1d\xf4\x67\x47\xd4\xdb\xbe\x17\xc4\xb5"
  1159. "\x58\x46\x2e\x26\xbe\x5e\xd3\x5f\xe6\x80\xe2\x97\x14\x22\xc3\xb0"
  1160. "\x1b\x17\xe1\x67\xfc\x43\x7f\x84\x86\x9d\x85\x49\x53\x7b\x33\x38",
  1161. WC_HASH_TYPE_SHA512),
  1162. #endif
  1163. INIT_SP80056C_TEST_VECTOR(
  1164. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1165. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1166. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1167. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1168. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1169. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1170. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1171. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1172. "\xde\x34\x54\xe4",
  1173. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1174. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1175. "\x2d\x4a",
  1176. WC_HASH_TYPE_SHA512),
  1177. };
  1178. for (i = 0; i < sizeof(vctors) / sizeof(vctors[0]); i++) {
  1179. v = &vctors[i];
  1180. ret = wc_KDA_KDF_onestep(v->z, v->zSz, v->fixedInfo, v->fixedInfoSz,
  1181. v->derivedKeySz, v->hashType, output,
  1182. /* use derivedKeySz to force the function to use a temporary buff
  1183. for the last block */
  1184. v->derivedKeySz);
  1185. if (ret != 0)
  1186. return WC_TEST_RET_ENC_EC(ret);
  1187. if (XMEMCMP(output, v->derivedKey, v->derivedKeySz) != 0)
  1188. return WC_TEST_RET_ENC_NC;
  1189. }
  1190. /* negative tests */
  1191. ret = wc_KDA_KDF_onestep(NULL, 0, (byte*)"fixed_info",
  1192. sizeof("fixed_info"), 16, WC_HASH_TYPE_SHA256, output, 16);
  1193. if (ret != BAD_FUNC_ARG)
  1194. return WC_TEST_RET_ENC_NC;
  1195. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 1, 16,
  1196. WC_HASH_TYPE_SHA256, output, 16);
  1197. if (ret != BAD_FUNC_ARG)
  1198. return WC_TEST_RET_ENC_NC;
  1199. /* allow empty FixedInfo */
  1200. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 0, 16,
  1201. WC_HASH_TYPE_SHA256, output, 16);
  1202. if (ret != 0)
  1203. return WC_TEST_RET_ENC_EC(ret);
  1204. return 0;
  1205. }
  1206. #endif /* WC_KDF_NIST_SP_800_56C */
  1207. /* optional macro to add sleep between tests */
  1208. #ifndef TEST_SLEEP
  1209. #define TEST_SLEEP() WC_DO_NOTHING
  1210. #else
  1211. #define TEST_PASS test_pass
  1212. #include <stdarg.h> /* for var args */
  1213. static WC_INLINE void test_pass(const char* fmt, ...)
  1214. {
  1215. va_list args;
  1216. va_start(args, fmt);
  1217. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  1218. va_end(args);
  1219. PRINT_HEAP_CHECKPOINT();
  1220. TEST_SLEEP();
  1221. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  1222. }
  1223. #endif
  1224. /* set test pass output to printf if not overridden */
  1225. #ifndef TEST_PASS
  1226. /* redirect to printf */
  1227. #define TEST_PASS(...) { \
  1228. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  1229. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  1230. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  1231. } \
  1232. PRINT_HEAP_CHECKPOINT(); \
  1233. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  1234. }
  1235. #endif
  1236. #ifdef TEST_ALWAYS_RUN_TO_END
  1237. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  1238. #elif !defined(TEST_FAIL)
  1239. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  1240. #endif
  1241. #ifdef HAVE_STACK_SIZE
  1242. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  1243. #else
  1244. wc_test_ret_t wolfcrypt_test(void* args)
  1245. #endif
  1246. {
  1247. wc_test_ret_t ret;
  1248. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1249. long heap_baselineAllocs, heap_baselineBytes;
  1250. #endif
  1251. #ifdef TEST_ALWAYS_RUN_TO_END
  1252. int last_failed_test_ret = 0;
  1253. #endif
  1254. STACK_SIZE_INIT();
  1255. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1256. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  1257. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  1258. (void)wolfCrypt_heap_peakBytes_checkpoint();
  1259. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  1260. #endif
  1261. #ifdef WC_RNG_SEED_CB
  1262. wc_SetSeed_Cb(wc_GenerateSeed);
  1263. #endif
  1264. printf("------------------------------------------------------------------------------\n");
  1265. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  1266. #ifdef WOLF_CRYPTO_CB
  1267. if (devId != INVALID_DEVID)
  1268. printf(" CryptoCB with DevID:%X\n", devId);
  1269. #endif
  1270. printf("------------------------------------------------------------------------------\n");
  1271. if (args) {
  1272. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1273. int ch;
  1274. #endif
  1275. ((func_args*)args)->return_code = -1; /* error state */
  1276. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1277. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  1278. switch(ch) {
  1279. case 's':
  1280. #ifdef HAVE_STACK_SIZE_VERBOSE
  1281. max_relative_stack = (ssize_t)atoi(myoptarg);
  1282. break;
  1283. #else
  1284. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  1285. #endif
  1286. case 'm':
  1287. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1288. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  1289. break;
  1290. #else
  1291. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1292. #endif
  1293. case 'a':
  1294. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1295. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  1296. break;
  1297. #else
  1298. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1299. #endif
  1300. case 'h':
  1301. return err_sys("\
  1302. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  1303. [-a max_relative_heap_allocs] [-h]\n", 0);
  1304. default:
  1305. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  1306. }
  1307. }
  1308. #endif
  1309. }
  1310. #ifdef WOLFSSL_STATIC_MEMORY
  1311. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  1312. WOLFMEM_GENERAL, 1) != 0) {
  1313. printf("unable to load static memory.\n");
  1314. return(EXIT_FAILURE);
  1315. }
  1316. #ifndef OPENSSL_EXTRA
  1317. wolfSSL_SetGlobalHeapHint(HEAP_HINT);
  1318. #endif
  1319. #endif
  1320. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  1321. wolfSSL_Debugging_ON();
  1322. #endif
  1323. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  1324. wc_SetLoggingHeap(HEAP_HINT);
  1325. #endif
  1326. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  1327. wolfCrypt_SetCb_fips(myFipsCb);
  1328. #if FIPS_VERSION3_GE(6,0,0)
  1329. printf("FIPS module version in use: %s\n",
  1330. wolfCrypt_GetVersion_fips());
  1331. #endif
  1332. #endif
  1333. #if !defined(NO_BIG_INT)
  1334. if (CheckCtcSettings() != 1) {
  1335. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  1336. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  1337. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  1338. }
  1339. #if defined(USE_FAST_MATH) && \
  1340. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  1341. if (CheckFastMathSettings() != 1)
  1342. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  1343. WC_TEST_RET_ENC_NC);
  1344. #endif /* USE_FAST_MATH */
  1345. #endif /* !NO_BIG_INT */
  1346. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  1347. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  1348. initDefaultName();
  1349. #endif
  1350. #ifdef WOLFSSL_ASYNC_CRYPT
  1351. ret = wolfAsync_DevOpen(&devId);
  1352. if (ret < 0) {
  1353. printf("Async device open failed\nRunning without async\n");
  1354. }
  1355. #else
  1356. (void)devId;
  1357. #endif /* WOLFSSL_ASYNC_CRYPT */
  1358. #ifdef WOLF_CRYPTO_CB
  1359. #ifdef HAVE_INTEL_QA_SYNC
  1360. devId = wc_CryptoCb_InitIntelQa();
  1361. if (INVALID_DEVID == devId) {
  1362. printf("Couldn't init the Intel QA\n");
  1363. }
  1364. #endif
  1365. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1366. devId = wc_CryptoCb_InitOcteon();
  1367. if (INVALID_DEVID == devId) {
  1368. printf("Couldn't init the Cavium Octeon\n");
  1369. }
  1370. #endif
  1371. #ifdef HAVE_RENESAS_SYNC
  1372. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  1373. if (devId == INVALID_DEVID) {
  1374. printf("Couldn't get the Renesas device ID\n");
  1375. }
  1376. #endif
  1377. #endif
  1378. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  1379. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  1380. if (devId == INVALID_DEVID) {
  1381. /* for testing RNG with crypto callback register function */
  1382. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  1383. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  1384. }
  1385. #endif
  1386. #ifdef HAVE_SELFTEST
  1387. if ( (ret = wolfCrypt_SelfTest()) != 0)
  1388. TEST_FAIL("CAVP selftest failed!\n", ret);
  1389. else
  1390. TEST_PASS("CAVP selftest passed!\n");
  1391. #endif
  1392. if ( (ret = error_test()) != 0)
  1393. TEST_FAIL("error test failed!\n", ret);
  1394. else
  1395. TEST_PASS("error test passed!\n");
  1396. if ( (ret = memory_test()) != 0)
  1397. TEST_FAIL("MEMORY test failed!\n", ret);
  1398. else
  1399. TEST_PASS("MEMORY test passed!\n");
  1400. #ifndef NO_CODING
  1401. if ( (ret = base64_test()) != 0)
  1402. TEST_FAIL("base64 test failed!\n", ret);
  1403. else
  1404. TEST_PASS("base64 test passed!\n");
  1405. #ifdef WOLFSSL_BASE16
  1406. if ( (ret = base16_test()) != 0)
  1407. TEST_FAIL("base16 test failed!\n", ret);
  1408. else
  1409. TEST_PASS("base16 test passed!\n");
  1410. #endif
  1411. #endif /* !NO_CODING */
  1412. #ifndef NO_ASN
  1413. if ( (ret = asn_test()) != 0)
  1414. TEST_FAIL("asn test failed!\n", ret);
  1415. else
  1416. TEST_PASS("asn test passed!\n");
  1417. #endif
  1418. #ifndef WC_NO_RNG
  1419. if ( (ret = random_test()) != 0)
  1420. TEST_FAIL("RANDOM test failed!\n", ret);
  1421. else
  1422. TEST_PASS("RANDOM test passed!\n");
  1423. #endif /* WC_NO_RNG */
  1424. #ifndef NO_MD5
  1425. if ( (ret = md5_test()) != 0)
  1426. TEST_FAIL("MD5 test failed!\n", ret);
  1427. else
  1428. TEST_PASS("MD5 test passed!\n");
  1429. #endif
  1430. #ifdef WOLFSSL_MD2
  1431. if ( (ret = md2_test()) != 0)
  1432. TEST_FAIL("MD2 test failed!\n", ret);
  1433. else
  1434. TEST_PASS("MD2 test passed!\n");
  1435. #endif
  1436. #ifndef NO_MD4
  1437. if ( (ret = md4_test()) != 0)
  1438. TEST_FAIL("MD4 test failed!\n", ret);
  1439. else
  1440. TEST_PASS("MD4 test passed!\n");
  1441. #endif
  1442. #ifndef NO_SHA
  1443. if ( (ret = sha_test()) != 0)
  1444. TEST_FAIL("SHA test failed!\n", ret);
  1445. else
  1446. TEST_PASS("SHA test passed!\n");
  1447. #endif
  1448. #ifdef WOLFSSL_SHA224
  1449. if ( (ret = sha224_test()) != 0)
  1450. TEST_FAIL("SHA-224 test failed!\n", ret);
  1451. else
  1452. TEST_PASS("SHA-224 test passed!\n");
  1453. #endif
  1454. #ifndef NO_SHA256
  1455. if ( (ret = sha256_test()) != 0)
  1456. TEST_FAIL("SHA-256 test failed!\n", ret);
  1457. else
  1458. TEST_PASS("SHA-256 test passed!\n");
  1459. #endif
  1460. #ifdef WOLFSSL_SHA384
  1461. if ( (ret = sha384_test()) != 0)
  1462. TEST_FAIL("SHA-384 test failed!\n", ret);
  1463. else
  1464. TEST_PASS("SHA-384 test passed!\n");
  1465. #endif
  1466. #ifdef WOLFSSL_SHA512
  1467. if ((ret = sha512_test()) != 0) {
  1468. TEST_FAIL("SHA-512 test failed!\n", ret);
  1469. }
  1470. else {
  1471. TEST_PASS("SHA-512 test passed!\n");
  1472. }
  1473. #if !defined(WOLFSSL_NOSHA512_224) && \
  1474. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1475. if ((ret = sha512_224_test()) != 0) {
  1476. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1477. }
  1478. else
  1479. TEST_PASS("SHA-512/224 test passed!\n");
  1480. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1481. #if !defined(WOLFSSL_NOSHA512_256) && \
  1482. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1483. if ((ret = sha512_256_test()) != 0) {
  1484. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1485. }
  1486. else
  1487. TEST_PASS("SHA-512/256 test passed!\n");
  1488. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1489. #endif /* WOLFSSL_SHA512 */
  1490. #ifdef WOLFSSL_SHA3
  1491. if ( (ret = sha3_test()) != 0)
  1492. TEST_FAIL("SHA-3 test failed!\n", ret);
  1493. else
  1494. TEST_PASS("SHA-3 test passed!\n");
  1495. #endif
  1496. #ifdef WOLFSSL_SHAKE128
  1497. if ( (ret = shake128_test()) != 0)
  1498. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1499. else
  1500. TEST_PASS("SHAKE128 test passed!\n");
  1501. #endif
  1502. #ifdef WOLFSSL_SHAKE256
  1503. if ( (ret = shake256_test()) != 0)
  1504. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1505. else
  1506. TEST_PASS("SHAKE256 test passed!\n");
  1507. #endif
  1508. #ifdef WOLFSSL_SM3
  1509. if ( (ret = sm3_test()) != 0)
  1510. return err_sys("SM-3 test failed!\n", ret);
  1511. else
  1512. TEST_PASS("SM-3 test passed!\n");
  1513. #endif
  1514. #ifndef NO_HASH_WRAPPER
  1515. if ( (ret = hash_test()) != 0)
  1516. TEST_FAIL("Hash test failed!\n", ret);
  1517. else
  1518. TEST_PASS("Hash test passed!\n");
  1519. #endif
  1520. #ifdef WOLFSSL_RIPEMD
  1521. if ( (ret = ripemd_test()) != 0)
  1522. TEST_FAIL("RIPEMD test failed!\n", ret);
  1523. else
  1524. TEST_PASS("RIPEMD test passed!\n");
  1525. #endif
  1526. #ifdef HAVE_BLAKE2
  1527. if ( (ret = blake2b_test()) != 0)
  1528. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1529. else
  1530. TEST_PASS("BLAKE2b test passed!\n");
  1531. #endif
  1532. #ifdef HAVE_BLAKE2S
  1533. if ( (ret = blake2s_test()) != 0)
  1534. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1535. else
  1536. TEST_PASS("BLAKE2s test passed!\n");
  1537. #endif
  1538. #ifndef NO_HMAC
  1539. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1540. && (HAVE_FIPS_VERSION >= 5))
  1541. if ( (ret = hmac_md5_test()) != 0)
  1542. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1543. else
  1544. TEST_PASS("HMAC-MD5 test passed!\n");
  1545. #endif
  1546. #ifndef NO_SHA
  1547. if ( (ret = hmac_sha_test()) != 0)
  1548. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1549. else
  1550. TEST_PASS("HMAC-SHA test passed!\n");
  1551. #endif
  1552. #ifdef WOLFSSL_SHA224
  1553. if ( (ret = hmac_sha224_test()) != 0)
  1554. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1555. else
  1556. TEST_PASS("HMAC-SHA224 test passed!\n");
  1557. #endif
  1558. #ifndef NO_SHA256
  1559. if ( (ret = hmac_sha256_test()) != 0)
  1560. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1561. else
  1562. TEST_PASS("HMAC-SHA256 test passed!\n");
  1563. #endif
  1564. #ifdef WOLFSSL_SHA384
  1565. if ( (ret = hmac_sha384_test()) != 0)
  1566. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1567. else
  1568. TEST_PASS("HMAC-SHA384 test passed!\n");
  1569. #endif
  1570. #ifdef WOLFSSL_SHA512
  1571. if ( (ret = hmac_sha512_test()) != 0)
  1572. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1573. else
  1574. TEST_PASS("HMAC-SHA512 test passed!\n");
  1575. #endif
  1576. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1577. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1578. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1579. if ( (ret = hmac_sha3_test()) != 0)
  1580. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1581. else
  1582. TEST_PASS("HMAC-SHA3 test passed!\n");
  1583. #endif
  1584. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1585. PRIVATE_KEY_UNLOCK();
  1586. if ( (ret = hkdf_test()) != 0)
  1587. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1588. else
  1589. TEST_PASS("HMAC-KDF test passed!\n");
  1590. PRIVATE_KEY_LOCK();
  1591. #endif
  1592. #endif /* !NO_HMAC */
  1593. #ifdef WOLFSSL_WOLFSSH
  1594. PRIVATE_KEY_UNLOCK();
  1595. if ( (ret = sshkdf_test()) != 0)
  1596. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1597. else
  1598. TEST_PASS("SSH-KDF test passed!\n");
  1599. PRIVATE_KEY_LOCK();
  1600. #endif /* WOLFSSL_WOLFSSH */
  1601. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  1602. PRIVATE_KEY_UNLOCK();
  1603. if ( (ret = prf_test()) != 0)
  1604. TEST_FAIL("PRF test failed!\n", ret);
  1605. else
  1606. TEST_PASS("PRF test passed!\n");
  1607. PRIVATE_KEY_LOCK();
  1608. #endif
  1609. #ifdef WOLFSSL_HAVE_PRF
  1610. #if defined (HAVE_HKDF) && !defined(NO_HMAC)
  1611. #ifdef WOLFSSL_BASE16
  1612. PRIVATE_KEY_UNLOCK();
  1613. if ( (ret = tls12_kdf_test()) != 0)
  1614. TEST_FAIL("TLSv1.2 KDF test failed!\n", ret);
  1615. else
  1616. TEST_PASS("TLSv1.2 KDF test passed!\n");
  1617. PRIVATE_KEY_LOCK();
  1618. #endif /* WOLFSSL_BASE16 */
  1619. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  1620. #endif /* WOLFSSL_HAVE_PRF */
  1621. #ifdef WOLFSSL_TLS13
  1622. PRIVATE_KEY_UNLOCK();
  1623. if ( (ret = tls13_kdf_test()) != 0)
  1624. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1625. else
  1626. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1627. PRIVATE_KEY_LOCK();
  1628. #endif /* WOLFSSL_TLS13 */
  1629. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1630. if ( (ret = x963kdf_test()) != 0)
  1631. TEST_FAIL("X963-KDF test failed!\n", ret);
  1632. else
  1633. TEST_PASS("X963-KDF test passed!\n");
  1634. #endif
  1635. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1636. if ( (ret = hpke_test()) != 0)
  1637. TEST_FAIL("HPKE test failed!\n", ret);
  1638. else
  1639. TEST_PASS("HPKE test passed!\n");
  1640. #endif
  1641. #if defined(WC_SRTP_KDF)
  1642. PRIVATE_KEY_UNLOCK();
  1643. if ( (ret = srtpkdf_test()) != 0)
  1644. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1645. else
  1646. TEST_PASS("SRTP KDF test passed!\n");
  1647. PRIVATE_KEY_LOCK();
  1648. #endif
  1649. #if defined(WC_KDF_NIST_SP_800_56C)
  1650. if ( (ret = nist_sp80056c_kdf_test()) != 0)
  1651. TEST_FAIL("NIST SP 800-56C KDF test failed!\n", ret);
  1652. else
  1653. TEST_PASS("NIST SP 800-56C KDF test passed!\n");
  1654. #endif
  1655. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1656. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1657. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1658. if ( (ret = gmac_test()) != 0)
  1659. TEST_FAIL("GMAC test failed!\n", ret);
  1660. else
  1661. TEST_PASS("GMAC test passed!\n");
  1662. #endif
  1663. #ifdef WC_RC2
  1664. if ( (ret = rc2_test()) != 0)
  1665. TEST_FAIL("RC2 test failed!\n", ret);
  1666. else
  1667. TEST_PASS("RC2 test passed!\n");
  1668. #endif
  1669. #ifndef NO_RC4
  1670. if ( (ret = arc4_test()) != 0)
  1671. TEST_FAIL("ARC4 test failed!\n", ret);
  1672. else
  1673. TEST_PASS("ARC4 test passed!\n");
  1674. #endif
  1675. #ifdef HAVE_CHACHA
  1676. if ( (ret = chacha_test()) != 0)
  1677. TEST_FAIL("Chacha test failed!\n", ret);
  1678. else
  1679. TEST_PASS("Chacha test passed!\n");
  1680. #endif
  1681. #ifdef HAVE_XCHACHA
  1682. if ( (ret = XChaCha_test()) != 0)
  1683. TEST_FAIL("XChacha test failed!\n", ret);
  1684. else
  1685. TEST_PASS("XChacha test passed!\n");
  1686. #endif
  1687. #ifdef HAVE_POLY1305
  1688. if ( (ret = poly1305_test()) != 0)
  1689. TEST_FAIL("POLY1305 test failed!\n", ret);
  1690. else
  1691. TEST_PASS("POLY1305 test passed!\n");
  1692. #endif
  1693. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1694. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1695. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1696. else
  1697. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1698. #endif
  1699. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1700. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1701. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1702. else
  1703. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1704. #endif
  1705. #ifndef NO_DES3
  1706. if ( (ret = des_test()) != 0)
  1707. TEST_FAIL("DES test failed!\n", ret);
  1708. else
  1709. TEST_PASS("DES test passed!\n");
  1710. #endif
  1711. #ifndef NO_DES3
  1712. if ( (ret = des3_test()) != 0)
  1713. TEST_FAIL("DES3 test failed!\n", ret);
  1714. else
  1715. TEST_PASS("DES3 test passed!\n");
  1716. #endif
  1717. #ifndef NO_AES
  1718. if ( (ret = aes_test()) != 0)
  1719. TEST_FAIL("AES test failed!\n", ret);
  1720. else
  1721. TEST_PASS("AES test passed!\n");
  1722. #if defined(WOLFSSL_AES_192) && \
  1723. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1724. if ( (ret = aes192_test()) != 0)
  1725. TEST_FAIL("AES192 test failed!\n", ret);
  1726. else
  1727. TEST_PASS("AES192 test passed!\n");
  1728. #endif
  1729. #if defined(WOLFSSL_AES_256)
  1730. if ( (ret = aes256_test()) != 0)
  1731. TEST_FAIL("AES256 test failed!\n", ret);
  1732. else
  1733. TEST_PASS("AES256 test passed!\n");
  1734. #endif
  1735. #ifdef WOLFSSL_AES_OFB
  1736. if ( (ret = aesofb_test()) != 0)
  1737. TEST_FAIL("AES-OFB test failed!\n", ret);
  1738. else
  1739. TEST_PASS("AES-OFB test passed!\n");
  1740. #endif
  1741. #ifdef HAVE_AESGCM
  1742. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1743. if ( (ret = aesgcm_test()) != 0)
  1744. TEST_FAIL("AES-GCM test failed!\n", ret);
  1745. #endif
  1746. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1747. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1748. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1749. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1750. if ((ret = aesgcm_default_test()) != 0) {
  1751. TEST_FAIL("AES-GCM test failed!\n", ret);
  1752. }
  1753. #endif
  1754. if (ret == 0) {
  1755. TEST_PASS("AES-GCM test passed!\n");
  1756. }
  1757. #endif
  1758. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1759. if ( (ret = aesccm_test()) != 0)
  1760. TEST_FAIL("AES-CCM test failed!\n", ret);
  1761. else
  1762. TEST_PASS("AES-CCM test passed!\n");
  1763. #endif
  1764. #ifdef WOLFSSL_AES_CFB
  1765. if ( (ret = aes_cfb_test()) != 0)
  1766. TEST_FAIL("AES-CFB test failed!\n", ret);
  1767. else
  1768. TEST_PASS("AES-CFB test passed!\n");
  1769. #endif
  1770. #ifdef WOLFSSL_AES_XTS
  1771. if ( (ret = aes_xts_test()) != 0)
  1772. TEST_FAIL("AES-XTS test failed!\n", ret);
  1773. else
  1774. TEST_PASS("AES-XTS test passed!\n");
  1775. #endif
  1776. #ifdef HAVE_AES_KEYWRAP
  1777. if ( (ret = aeskeywrap_test()) != 0)
  1778. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1779. else
  1780. TEST_PASS("AES Key Wrap test passed!\n");
  1781. #endif
  1782. #ifdef WOLFSSL_AES_SIV
  1783. if ( (ret = aes_siv_test()) != 0)
  1784. TEST_FAIL("AES-SIV test failed!\n", ret);
  1785. else
  1786. TEST_PASS("AES-SIV test passed!\n");
  1787. #endif
  1788. #endif
  1789. #if defined(WOLFSSL_AES_EAX) && \
  1790. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1791. if ( (ret = aes_eax_test()) != 0)
  1792. TEST_FAIL("AES-EAX test failed!\n", ret);
  1793. else
  1794. TEST_PASS("AES-EAX test passed!\n");
  1795. #endif /* WOLFSSL_AES_EAX */
  1796. #ifdef HAVE_ARIA
  1797. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1798. TEST_FAIL("ARIA128 test failed!\n", ret);
  1799. else
  1800. TEST_PASS("ARIA128 test passed!\n");
  1801. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1802. TEST_FAIL("ARIA192 test failed!\n", ret);
  1803. else
  1804. TEST_PASS("ARIA192 test passed!\n");
  1805. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1806. TEST_FAIL("ARIA256 test failed!\n", ret);
  1807. else
  1808. TEST_PASS("ARIA256 test passed!\n");
  1809. #endif
  1810. #ifdef HAVE_CAMELLIA
  1811. if ( (ret = camellia_test()) != 0)
  1812. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1813. else
  1814. TEST_PASS("CAMELLIA test passed!\n");
  1815. #endif
  1816. #ifdef WOLFSSL_SM4
  1817. if ( (ret = sm4_test()) != 0)
  1818. return err_sys("SM-4 test failed!\n", ret);
  1819. else
  1820. TEST_PASS("SM-4 test passed!\n");
  1821. #endif
  1822. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1823. #ifdef WC_RSA_NO_PADDING
  1824. if ( (ret = rsa_no_pad_test()) != 0)
  1825. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1826. else
  1827. TEST_PASS("RSA NOPAD test passed!\n");
  1828. #endif
  1829. if ( (ret = rsa_test()) != 0)
  1830. TEST_FAIL("RSA test failed!\n", ret);
  1831. else
  1832. TEST_PASS("RSA test passed!\n");
  1833. #endif
  1834. #ifndef NO_DH
  1835. PRIVATE_KEY_UNLOCK();
  1836. if ( (ret = dh_test()) != 0)
  1837. TEST_FAIL("DH test failed!\n", ret);
  1838. else
  1839. TEST_PASS("DH test passed!\n");
  1840. PRIVATE_KEY_LOCK();
  1841. #endif
  1842. #ifndef NO_DSA
  1843. if ( (ret = dsa_test()) != 0)
  1844. TEST_FAIL("DSA test failed!\n", ret);
  1845. else
  1846. TEST_PASS("DSA test passed!\n");
  1847. #endif
  1848. #ifdef WOLFCRYPT_HAVE_SRP
  1849. if ( (ret = srp_test()) != 0)
  1850. TEST_FAIL("SRP test failed!\n", ret);
  1851. else
  1852. TEST_PASS("SRP test passed!\n");
  1853. #endif
  1854. #ifndef NO_PWDBASED
  1855. PRIVATE_KEY_UNLOCK();
  1856. if ( (ret = pwdbased_test()) != 0)
  1857. TEST_FAIL("PWDBASED test failed!\n", ret);
  1858. else
  1859. TEST_PASS("PWDBASED test passed!\n");
  1860. PRIVATE_KEY_LOCK();
  1861. #endif
  1862. #if defined(USE_CERT_BUFFERS_2048) && \
  1863. defined(HAVE_PKCS12) && \
  1864. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  1865. !defined(NO_CERTS) && !defined(NO_DES3)
  1866. if ( (ret = pkcs12_test()) != 0)
  1867. TEST_FAIL("PKCS12 test failed!\n", ret);
  1868. else
  1869. TEST_PASS("PKCS12 test passed!\n");
  1870. #endif
  1871. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1872. if ( (ret = openssl_test()) != 0)
  1873. TEST_FAIL("OPENSSL test failed!\n", ret);
  1874. else
  1875. TEST_PASS("OPENSSL test passed!\n");
  1876. if ( (ret = openSSL_evpMD_test()) != 0)
  1877. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1878. else
  1879. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1880. if ( (ret = openssl_pkey0_test()) != 0)
  1881. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1882. else
  1883. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1884. if ( (ret = openssl_pkey1_test()) != 0)
  1885. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1886. else
  1887. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1888. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1889. if ( (ret = openssl_evpSig_test()) != 0)
  1890. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1891. else
  1892. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1893. #endif
  1894. #endif
  1895. #if defined(HAVE_ECC)
  1896. PRIVATE_KEY_UNLOCK();
  1897. if ( (ret = ecc_test()) != 0)
  1898. TEST_FAIL("ECC test failed!\n", ret);
  1899. else
  1900. TEST_PASS("ECC test passed!\n");
  1901. PRIVATE_KEY_LOCK();
  1902. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1903. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1904. if ( (ret = ecc_encrypt_test()) != 0)
  1905. TEST_FAIL("ECC Enc test failed!\n", ret);
  1906. else
  1907. TEST_PASS("ECC Enc test passed!\n");
  1908. #endif
  1909. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1910. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1911. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1912. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1913. /* skip for ATECC508/608A, cannot import private key buffers */
  1914. if ( (ret = ecc_test_buffers()) != 0)
  1915. TEST_FAIL("ECC buffer test failed!\n", ret);
  1916. else
  1917. TEST_PASS("ECC buffer test passed!\n");
  1918. #endif
  1919. #endif
  1920. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1921. !defined(NO_FILESYSTEM)
  1922. if ( (ret = cert_test()) != 0)
  1923. TEST_FAIL("CERT test failed!\n", ret);
  1924. else
  1925. TEST_PASS("CERT test passed!\n");
  1926. #endif
  1927. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1928. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1929. if ( (ret = certext_test()) != 0)
  1930. TEST_FAIL("CERT EXT test failed!\n", ret);
  1931. else
  1932. TEST_PASS("CERT EXT test passed!\n");
  1933. #endif
  1934. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1935. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1936. if ( (ret = decodedCertCache_test()) != 0)
  1937. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1938. else
  1939. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1940. #endif
  1941. #ifdef HAVE_CURVE25519
  1942. if ( (ret = curve25519_test()) != 0)
  1943. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1944. else
  1945. TEST_PASS("CURVE25519 test passed!\n");
  1946. #endif
  1947. #ifdef HAVE_ED25519
  1948. PRIVATE_KEY_UNLOCK();
  1949. if ( (ret = ed25519_test()) != 0)
  1950. TEST_FAIL("ED25519 test failed!\n", ret);
  1951. else
  1952. TEST_PASS("ED25519 test passed!\n");
  1953. PRIVATE_KEY_LOCK();
  1954. #endif
  1955. #ifdef HAVE_CURVE448
  1956. if ( (ret = curve448_test()) != 0)
  1957. TEST_FAIL("CURVE448 test failed!\n", ret);
  1958. else
  1959. TEST_PASS("CURVE448 test passed!\n");
  1960. #endif
  1961. #ifdef HAVE_ED448
  1962. PRIVATE_KEY_UNLOCK();
  1963. if ( (ret = ed448_test()) != 0)
  1964. TEST_FAIL("ED448 test failed!\n", ret);
  1965. else
  1966. TEST_PASS("ED448 test passed!\n");
  1967. PRIVATE_KEY_LOCK();
  1968. #endif
  1969. #ifdef WOLFSSL_HAVE_KYBER
  1970. if ( (ret = kyber_test()) != 0)
  1971. TEST_FAIL("KYBER test failed!\n", ret);
  1972. else
  1973. TEST_PASS("KYBER test passed!\n");
  1974. #endif
  1975. #if defined(WOLFSSL_HAVE_XMSS)
  1976. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  1977. if ( (ret = xmss_test_verify_only()) != 0)
  1978. TEST_FAIL("XMSS Vfy test failed!\n", ret);
  1979. else
  1980. TEST_PASS("XMSS Vfy test passed!\n");
  1981. #endif
  1982. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1983. if ( (ret = xmss_test()) != 0)
  1984. TEST_FAIL("XMSS test failed!\n", ret);
  1985. else
  1986. TEST_PASS("XMSS test passed!\n");
  1987. #endif
  1988. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  1989. #if defined(WOLFSSL_HAVE_LMS)
  1990. #if !defined(WOLFSSL_SMALL_STACK)
  1991. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  1992. defined(HAVE_LIBLMS)
  1993. if ( (ret = lms_test_verify_only()) != 0)
  1994. TEST_FAIL("LMS Vfy test failed!\n", ret);
  1995. else
  1996. TEST_PASS("LMS Vfy test passed!\n");
  1997. #endif
  1998. #endif
  1999. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  2000. if ( (ret = lms_test()) != 0)
  2001. TEST_FAIL("LMS test failed!\n", ret);
  2002. else
  2003. TEST_PASS("LMS test passed!\n");
  2004. #endif
  2005. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  2006. #ifdef WOLFCRYPT_HAVE_ECCSI
  2007. if ( (ret = eccsi_test()) != 0)
  2008. TEST_FAIL("ECCSI test failed!\n", ret);
  2009. else
  2010. TEST_PASS("ECCSI test passed!\n");
  2011. #endif
  2012. #ifdef WOLFCRYPT_HAVE_SAKKE
  2013. if ( (ret = sakke_test()) != 0)
  2014. TEST_FAIL("SAKKE test failed!\n", ret);
  2015. else
  2016. TEST_PASS("SAKKE test passed!\n");
  2017. #endif
  2018. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  2019. if ( (ret = cmac_test()) != 0)
  2020. TEST_FAIL("CMAC test failed!\n", ret);
  2021. else
  2022. TEST_PASS("CMAC test passed!\n");
  2023. #endif
  2024. #if defined(WOLFSSL_SIPHASH)
  2025. if ( (ret = siphash_test()) != 0)
  2026. TEST_FAIL("SipHash test failed!\n", ret);
  2027. else
  2028. TEST_PASS("SipHash test passed!\n");
  2029. #endif
  2030. #ifdef HAVE_LIBZ
  2031. if ( (ret = compress_test()) != 0)
  2032. TEST_FAIL("COMPRESS test failed!\n", ret);
  2033. else
  2034. TEST_PASS("COMPRESS test passed!\n");
  2035. #endif
  2036. #ifdef HAVE_PKCS7
  2037. #ifndef NO_PKCS7_ENCRYPTED_DATA
  2038. if ( (ret = pkcs7encrypted_test()) != 0)
  2039. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  2040. else
  2041. TEST_PASS("PKCS7encrypted test passed!\n");
  2042. #endif
  2043. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  2044. if ( (ret = pkcs7compressed_test()) != 0)
  2045. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  2046. else
  2047. TEST_PASS("PKCS7compressed test passed!\n");
  2048. #endif
  2049. if ( (ret = pkcs7signed_test()) != 0)
  2050. TEST_FAIL("PKCS7signed test failed!\n", ret);
  2051. else
  2052. TEST_PASS("PKCS7signed test passed!\n");
  2053. if ( (ret = pkcs7enveloped_test()) != 0)
  2054. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  2055. else
  2056. TEST_PASS("PKCS7enveloped test passed!\n");
  2057. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  2058. if ( (ret = pkcs7authenveloped_test()) != 0)
  2059. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  2060. else
  2061. TEST_PASS("PKCS7authenveloped test passed!\n");
  2062. #endif
  2063. #endif
  2064. #if defined(WOLFSSL_PUBLIC_MP) && \
  2065. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  2066. defined(USE_FAST_MATH))
  2067. if ( (ret = mp_test()) != 0)
  2068. TEST_FAIL("mp test failed!\n", ret);
  2069. else
  2070. TEST_PASS("mp test passed!\n");
  2071. #endif
  2072. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  2073. if ( (ret = prime_test()) != 0)
  2074. TEST_FAIL("prime test failed!\n", ret);
  2075. else
  2076. TEST_PASS("prime test passed!\n");
  2077. #endif
  2078. #if defined(ASN_BER_TO_DER) && \
  2079. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  2080. defined(OPENSSL_EXTRA_X509_SMALL))
  2081. if ( (ret = berder_test()) != 0)
  2082. TEST_FAIL("ber-der test failed!\n", ret);
  2083. else
  2084. TEST_PASS("ber-der test passed!\n");
  2085. #endif
  2086. if ( (ret = logging_test()) != 0)
  2087. TEST_FAIL("logging test failed!\n", ret);
  2088. else
  2089. TEST_PASS("logging test passed!\n");
  2090. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  2091. if ( (ret = time_test()) != 0)
  2092. TEST_FAIL("time test failed!\n", ret);
  2093. else
  2094. TEST_PASS("time test passed!\n");
  2095. #endif
  2096. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  2097. if ((ret = wolfcrypt_mutex_test()) != 0)
  2098. #else
  2099. if ((ret = mutex_test()) != 0)
  2100. #endif
  2101. TEST_FAIL("mutex test failed!\n", ret);
  2102. else
  2103. TEST_PASS("mutex test passed!\n");
  2104. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  2105. if ( (ret = memcb_test()) != 0)
  2106. TEST_FAIL("memcb test failed!\n", ret);
  2107. else
  2108. TEST_PASS("memcb test passed!\n");
  2109. #endif
  2110. #ifdef WOLFSSL_CAAM_BLOB
  2111. if ( (ret = blob_test()) != 0)
  2112. TEST_FAIL("blob test failed!\n", ret);
  2113. else
  2114. TEST_PASS("blob test passed!\n");
  2115. #endif
  2116. #if defined(WOLF_CRYPTO_CB) && \
  2117. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2118. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  2119. if ( (ret = cryptocb_test()) != 0)
  2120. TEST_FAIL("crypto callback test failed!\n", ret);
  2121. else
  2122. TEST_PASS("crypto callback test passed!\n");
  2123. #endif
  2124. #ifdef WOLFSSL_CERT_PIV
  2125. if ( (ret = certpiv_test()) != 0)
  2126. TEST_FAIL("cert piv test failed!\n", ret);
  2127. else
  2128. TEST_PASS("cert piv test passed!\n");
  2129. #endif
  2130. #ifdef WOLF_CRYPTO_CB
  2131. #ifdef HAVE_INTEL_QA_SYNC
  2132. wc_CryptoCb_CleanupIntelQa(&devId);
  2133. #endif
  2134. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2135. wc_CryptoCb_CleanupOcteon(&devId);
  2136. #endif
  2137. #endif
  2138. #ifdef WOLFSSL_ASYNC_CRYPT
  2139. wolfAsync_DevClose(&devId);
  2140. #endif
  2141. /* cleanup the thread if fixed point cache is enabled and have thread local */
  2142. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  2143. wc_ecc_fp_free();
  2144. #endif
  2145. #ifdef TEST_ALWAYS_RUN_TO_END
  2146. if (last_failed_test_ret != 0)
  2147. ret = last_failed_test_ret;
  2148. #endif
  2149. if (args)
  2150. ((func_args*)args)->return_code = ret;
  2151. /* If hardware acceleration and respective metrics tracked, show results: */
  2152. #ifdef WOLFSSL_HW_METRICS
  2153. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  2154. esp_hw_show_mp_metrics();
  2155. #endif
  2156. #endif
  2157. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA)
  2158. wolfSSL_SetGlobalHeapHint(NULL);
  2159. #endif
  2160. TEST_PASS("Test complete\n");
  2161. EXIT_TEST(ret);
  2162. } /* end of wolfcrypt_test() */
  2163. #ifndef NO_MAIN_DRIVER
  2164. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  2165. int myoptind = 0;
  2166. char* myoptarg = NULL;
  2167. #endif
  2168. /* so overall tests can pull in test function */
  2169. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  2170. wc_test_ret_t wolf_test_task(void)
  2171. #else
  2172. #ifndef NO_MAIN_FUNCTION
  2173. int main(int argc, char** argv)
  2174. {
  2175. return (int)wolfcrypt_test_main(argc, argv);
  2176. }
  2177. #endif
  2178. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  2179. #endif
  2180. {
  2181. wc_test_ret_t ret;
  2182. func_args args = { 0, 0, 0 };
  2183. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  2184. /* set dummy wallclock time. */
  2185. struct timeval utctime;
  2186. struct timezone tz;
  2187. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2188. utctime.tv_usec = 0;
  2189. tz.tz_minuteswest = 0;
  2190. tz.tz_dsttime = 0;
  2191. settimeofday(&utctime, &tz);
  2192. #endif
  2193. #ifdef WOLFSSL_APACHE_MYNEWT
  2194. #ifdef ARCH_sim
  2195. mcu_sim_parse_args(argc, argv);
  2196. #endif
  2197. sysinit();
  2198. /* set dummy wallclock time. */
  2199. struct os_timeval utctime;
  2200. struct os_timezone tz;
  2201. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2202. utctime.tv_usec = 0;
  2203. tz.tz_minuteswest = 0;
  2204. tz.tz_dsttime = 0;
  2205. os_settimeofday(&utctime, &tz);
  2206. #endif
  2207. #ifdef WOLFSSL_ZEPHYR
  2208. /* set dummy wallclock time. */
  2209. struct timespec utctime;
  2210. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2211. utctime.tv_nsec = 0;
  2212. clock_settime(CLOCK_REALTIME, &utctime);
  2213. #endif
  2214. #ifdef DEVKITPRO
  2215. void *framebuffer;
  2216. GXRModeObj *rmode = NULL;
  2217. VIDEO_Init();
  2218. WPAD_Init();
  2219. rmode = VIDEO_GetPreferredMode(NULL);
  2220. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  2221. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  2222. #pragma GCC diagnostic pop
  2223. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  2224. VIDEO_Configure(rmode);
  2225. VIDEO_SetNextFramebuffer(framebuffer);
  2226. VIDEO_SetBlack(FALSE);
  2227. VIDEO_Flush();
  2228. VIDEO_WaitVSync();
  2229. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  2230. #endif
  2231. #ifdef HAVE_WNR
  2232. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  2233. err_sys("Whitewood netRandom global config failed",
  2234. WC_TEST_RET_ENC_EC(ret));
  2235. return WC_TEST_RET_ENC_EC(ret);
  2236. }
  2237. #endif
  2238. #ifndef WOLFSSL_ESPIDF
  2239. args.argc = argc;
  2240. args.argv = argv;
  2241. #endif
  2242. if ((ret = wolfCrypt_Init()) != 0) {
  2243. printf("wolfCrypt_Init failed %d\n", (int)ret);
  2244. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  2245. }
  2246. #ifdef HAVE_WC_INTROSPECTION
  2247. printf("Math: %s\n", wc_GetMathInfo());
  2248. #endif
  2249. #ifdef HAVE_STACK_SIZE
  2250. StackSizeCheck(&args, wolfcrypt_test);
  2251. #else
  2252. wolfcrypt_test(&args);
  2253. #endif
  2254. if ((ret = wolfCrypt_Cleanup()) != 0) {
  2255. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  2256. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  2257. }
  2258. #ifdef HAVE_WNR
  2259. if ((ret = wc_FreeNetRandom()) < 0)
  2260. err_sys("Failed to free netRandom context",
  2261. WC_TEST_RET_ENC_EC(ret));
  2262. #endif /* HAVE_WNR */
  2263. #ifdef DOLPHIN_EMULATOR
  2264. /* Returning from main panics the emulator. Just hang
  2265. * and let the user force quit the emulator window. */
  2266. printf("args.return_code: %d\n", args.return_code);
  2267. printf("Testing complete. You may close the window now\n");
  2268. while (1);
  2269. #endif
  2270. #if defined(WOLFSSL_ESPIDF)
  2271. /* ESP_LOGI to print takes up a lot less memory than printf */
  2272. ESP_LOGI(ESPIDF_TAG, "Exiting main with return code: % d\n",
  2273. args.return_code);
  2274. #else
  2275. /* gate this for target platforms wishing to avoid printf reference */
  2276. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  2277. #endif
  2278. return args.return_code;
  2279. } /* wolfcrypt_test_main or wolf_test_task */
  2280. #endif /* NO_MAIN_DRIVER */
  2281. /* helper to save DER, convert to PEM and save PEM */
  2282. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  2283. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  2284. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  2285. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2286. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  2287. #else
  2288. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  2289. #endif
  2290. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  2291. const char* fileDer, const char* filePem, int pemType, int calling_line)
  2292. {
  2293. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2294. wc_test_ret_t ret;
  2295. XFILE derFile;
  2296. derFile = XFOPEN(fileDer, "wb");
  2297. if (!derFile) {
  2298. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  2299. }
  2300. ret = (int)XFWRITE(der, 1, (size_t)derSz, derFile);
  2301. XFCLOSE(derFile);
  2302. if (ret != derSz) {
  2303. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  2304. }
  2305. #endif
  2306. #ifdef WOLFSSL_DER_TO_PEM
  2307. if (filePem) {
  2308. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2309. XFILE pemFile;
  2310. #endif
  2311. #ifndef WOLFSSL_NO_MALLOC
  2312. byte* pem;
  2313. #else
  2314. byte pem[1024];
  2315. #endif
  2316. int pemSz;
  2317. /* calculate PEM size */
  2318. pemSz = wc_DerToPem(der, (word32)derSz, NULL, 0, pemType);
  2319. if (pemSz < 0) {
  2320. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  2321. }
  2322. #ifndef WOLFSSL_NO_MALLOC
  2323. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2324. if (pem == NULL) {
  2325. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  2326. }
  2327. #else
  2328. if (pemSz > (int)sizeof(pem))
  2329. return BAD_FUNC_ARG;
  2330. #endif
  2331. /* Convert to PEM */
  2332. pemSz = wc_DerToPem(der, (word32)derSz, pem, pemSz, pemType);
  2333. if (pemSz < 0) {
  2334. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2335. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  2336. }
  2337. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2338. pemFile = XFOPEN(filePem, "wb");
  2339. if (!pemFile) {
  2340. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2341. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  2342. }
  2343. ret = (int)XFWRITE(pem, 1, (size_t)pemSz, pemFile);
  2344. XFCLOSE(pemFile);
  2345. if (ret != pemSz) {
  2346. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2347. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  2348. }
  2349. #endif
  2350. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2351. }
  2352. #endif /* WOLFSSL_DER_TO_PEM */
  2353. /* suppress unused variable warnings */
  2354. (void)der;
  2355. (void)derSz;
  2356. (void)filePem;
  2357. (void)fileDer;
  2358. (void)pemType;
  2359. (void)calling_line;
  2360. return 0;
  2361. }
  2362. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  2363. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  2364. {
  2365. const char* errStr;
  2366. char out[WOLFSSL_MAX_ERROR_SZ];
  2367. const char* unknownStr = wc_GetErrorString(0);
  2368. #ifdef NO_ERROR_STRINGS
  2369. /* Ensure a valid error code's string matches an invalid code's.
  2370. * The string is that error strings are not available.
  2371. */
  2372. WOLFSSL_ENTER("error_test NO_ERROR_STRINGS");
  2373. errStr = wc_GetErrorString(OPEN_RAN_E);
  2374. wc_ErrorString(OPEN_RAN_E, out);
  2375. if (XSTRCMP(errStr, unknownStr) != 0)
  2376. return WC_TEST_RET_ENC_NC;
  2377. if (XSTRCMP(out, unknownStr) != 0)
  2378. return WC_TEST_RET_ENC_NC;
  2379. #else
  2380. int i;
  2381. int j = 0;
  2382. /* Values that are not or no longer error codes. */
  2383. int missing[] = { -124, -166, -167, -168, -169, 0 };
  2384. /* Check that all errors have a string and it's the same through the two
  2385. * APIs. Check that the values that are not errors map to the unknown
  2386. * string.
  2387. */
  2388. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  2389. errStr = wc_GetErrorString(i);
  2390. wc_ErrorString(i, out);
  2391. if (i != missing[j]) {
  2392. if (XSTRCMP(errStr, unknownStr) == 0) {
  2393. WOLFSSL_MSG("errStr unknown");
  2394. return WC_TEST_RET_ENC_NC;
  2395. }
  2396. if (XSTRCMP(out, unknownStr) == 0) {
  2397. WOLFSSL_MSG("out unknown");
  2398. return WC_TEST_RET_ENC_NC;
  2399. }
  2400. if (XSTRCMP(errStr, out) != 0) {
  2401. WOLFSSL_MSG("errStr does not match output");
  2402. return WC_TEST_RET_ENC_NC;
  2403. }
  2404. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
  2405. WOLFSSL_MSG("errStr too long");
  2406. return WC_TEST_RET_ENC_NC;
  2407. }
  2408. }
  2409. else {
  2410. j++;
  2411. if (XSTRCMP(errStr, unknownStr) != 0)
  2412. return WC_TEST_RET_ENC_NC;
  2413. if (XSTRCMP(out, unknownStr) != 0)
  2414. return WC_TEST_RET_ENC_NC;
  2415. }
  2416. }
  2417. /* Check if the next possible value has been given a string. */
  2418. errStr = wc_GetErrorString(i);
  2419. wc_ErrorString(i, out);
  2420. if (XSTRCMP(errStr, unknownStr) != 0)
  2421. return WC_TEST_RET_ENC_NC;
  2422. if (XSTRCMP(out, unknownStr) != 0)
  2423. return WC_TEST_RET_ENC_NC;
  2424. #endif
  2425. return 0;
  2426. }
  2427. #ifndef NO_CODING
  2428. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  2429. {
  2430. wc_test_ret_t ret;
  2431. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  2432. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  2433. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  2434. byte out[128];
  2435. word32 outLen;
  2436. #ifdef WOLFSSL_BASE64_ENCODE
  2437. byte data[3];
  2438. word32 dataLen;
  2439. byte longData[79] = { 0 };
  2440. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  2441. #endif
  2442. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  2443. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  2444. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  2445. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  2446. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  2447. byte goodChar[] =
  2448. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2449. "abcdefghijklmnopqrstuvwxyz"
  2450. "0123456789+/;";
  2451. byte charTest[] = "A+Gd\0\0\0";
  2452. int i;
  2453. WOLFSSL_ENTER("base64_test");
  2454. /* Good Base64 encodings. */
  2455. outLen = sizeof(out);
  2456. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2457. if (ret != 0)
  2458. return WC_TEST_RET_ENC_EC(ret);
  2459. outLen = sizeof(out);
  2460. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  2461. if (ret != 0)
  2462. return WC_TEST_RET_ENC_EC(ret);
  2463. outLen = sizeof(goodChar);
  2464. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  2465. if (ret != 0)
  2466. return WC_TEST_RET_ENC_EC(ret);
  2467. if (outLen != 64 / 4 * 3)
  2468. return WC_TEST_RET_ENC_NC;
  2469. outLen = sizeof(out);
  2470. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2471. if (ret != 0)
  2472. return WC_TEST_RET_ENC_EC(ret);
  2473. /* Bad parameters. */
  2474. outLen = 1;
  2475. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2476. if (ret != BAD_FUNC_ARG)
  2477. return WC_TEST_RET_ENC_EC(ret);
  2478. outLen = sizeof(out);
  2479. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2480. if (ret != ASN_INPUT_E)
  2481. return WC_TEST_RET_ENC_EC(ret);
  2482. outLen = sizeof(out);
  2483. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2484. if (ret != ASN_INPUT_E)
  2485. return WC_TEST_RET_ENC_EC(ret);
  2486. /* Bad character at each offset 0-3. */
  2487. for (i = 0; i < 4; i++) {
  2488. outLen = sizeof(out);
  2489. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2490. if (ret != ASN_INPUT_E)
  2491. return WC_TEST_RET_ENC_I(i);
  2492. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2493. if (ret != ASN_INPUT_E)
  2494. return WC_TEST_RET_ENC_I(i);
  2495. }
  2496. /* Invalid character less than 0x2b */
  2497. for (i = 1; i < 0x2b; i++) {
  2498. outLen = sizeof(out);
  2499. charTest[0] = (byte)i;
  2500. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2501. if (ret != ASN_INPUT_E)
  2502. return WC_TEST_RET_ENC_I(i);
  2503. }
  2504. /* Bad characters in range 0x2b - 0x7a. */
  2505. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2506. outLen = sizeof(out);
  2507. charTest[0] = badChar[i];
  2508. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2509. if (ret != ASN_INPUT_E)
  2510. return WC_TEST_RET_ENC_I(i);
  2511. }
  2512. /* Invalid character greater than 0x7a */
  2513. for (i = 0x7b; i < 0x100; i++) {
  2514. outLen = sizeof(out);
  2515. charTest[0] = (byte)i;
  2516. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2517. if (ret != ASN_INPUT_E)
  2518. return WC_TEST_RET_ENC_I(i);
  2519. }
  2520. #ifdef WOLFSSL_BASE64_ENCODE
  2521. /* Decode and encode all symbols - non-alphanumeric. */
  2522. dataLen = sizeof(data);
  2523. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2524. if (ret != 0)
  2525. return WC_TEST_RET_ENC_EC(ret);
  2526. outLen = sizeof(out);
  2527. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  2528. if (ret != LENGTH_ONLY_E)
  2529. return WC_TEST_RET_ENC_EC(ret);
  2530. outLen = sizeof(out);
  2531. ret = Base64_Encode(data, dataLen, out, &outLen);
  2532. if (ret != 0)
  2533. return WC_TEST_RET_ENC_EC(ret);
  2534. outLen = 7;
  2535. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2536. if (ret != BUFFER_E)
  2537. return WC_TEST_RET_ENC_EC(ret);
  2538. outLen = sizeof(out);
  2539. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2540. if (ret != LENGTH_ONLY_E)
  2541. return WC_TEST_RET_ENC_EC(ret);
  2542. outLen = sizeof(out);
  2543. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2544. if (ret != 0)
  2545. return WC_TEST_RET_ENC_EC(ret);
  2546. outLen = sizeof(out);
  2547. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2548. if (ret != 0)
  2549. return WC_TEST_RET_ENC_EC(ret);
  2550. /* Data that results in an encoding longer than one line. */
  2551. outLen = sizeof(out);
  2552. dataLen = sizeof(longData);
  2553. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2554. if (ret != 0)
  2555. return WC_TEST_RET_ENC_EC(ret);
  2556. outLen = sizeof(out);
  2557. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2558. if (ret != 0)
  2559. return WC_TEST_RET_ENC_EC(ret);
  2560. outLen = sizeof(out);
  2561. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2562. if (ret != 0)
  2563. return WC_TEST_RET_ENC_EC(ret);
  2564. #endif
  2565. return 0;
  2566. }
  2567. #ifdef WOLFSSL_BASE16
  2568. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2569. {
  2570. wc_test_ret_t ret;
  2571. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2572. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2573. byte encoded[40];
  2574. word32 encodedLen;
  2575. byte plain[40];
  2576. word32 len;
  2577. WOLFSSL_ENTER("base16_test");
  2578. /* length returned includes null termination */
  2579. encodedLen = sizeof(encoded);
  2580. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2581. if (ret != 0)
  2582. return WC_TEST_RET_ENC_EC(ret);
  2583. len = (word32)XSTRLEN((char*)encoded);
  2584. if (len != encodedLen - 1)
  2585. return WC_TEST_RET_ENC_NC;
  2586. len = sizeof(plain);
  2587. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2588. if (ret != 0)
  2589. return WC_TEST_RET_ENC_EC(ret);
  2590. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2591. return WC_TEST_RET_ENC_NC;
  2592. if (encodedLen != sizeof(encodedTestData) ||
  2593. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2594. return WC_TEST_RET_ENC_NC;
  2595. }
  2596. return 0;
  2597. }
  2598. #endif /* WOLFSSL_BASE16 */
  2599. #endif /* !NO_CODING */
  2600. #ifndef NO_ASN
  2601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2602. {
  2603. wc_test_ret_t ret;
  2604. /* ASN1 encoded date buffer */
  2605. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {
  2606. 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2607. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a
  2608. };
  2609. byte format;
  2610. int length;
  2611. const byte* datePart;
  2612. #ifndef NO_ASN_TIME
  2613. struct tm timearg;
  2614. time_t now;
  2615. #endif
  2616. WOLFSSL_ENTER("asn_test");
  2617. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2618. &length);
  2619. if (ret != 0)
  2620. return WC_TEST_RET_ENC_EC(ret);
  2621. #ifndef NO_ASN_TIME
  2622. /* Parameter Validation tests. */
  2623. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2624. return WC_TEST_RET_ENC_EC(ret);
  2625. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2626. return WC_TEST_RET_ENC_EC(ret);
  2627. now = 0;
  2628. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2629. return WC_TEST_RET_ENC_EC(ret);
  2630. }
  2631. if (now == 0) {
  2632. printf("RTC/Time not set!\n");
  2633. return WC_TEST_RET_ENC_NC;
  2634. }
  2635. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2636. if (ret != 0)
  2637. return WC_TEST_RET_ENC_EC(ret);
  2638. #endif /* !NO_ASN_TIME */
  2639. return 0;
  2640. }
  2641. #endif /* !NO_ASN */
  2642. #ifdef WOLFSSL_MD2
  2643. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2644. {
  2645. wc_test_ret_t ret = 0;
  2646. Md2 md2;
  2647. byte hash[MD2_DIGEST_SIZE];
  2648. testVector a, b, c, d, e, f, g;
  2649. testVector test_md2[7];
  2650. int times = sizeof(test_md2) / sizeof(testVector), i;
  2651. WOLFSSL_ENTER("md2_test");
  2652. a.input = "";
  2653. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2654. "\x27\x73";
  2655. a.inLen = XSTRLEN(a.input);
  2656. a.outLen = MD2_DIGEST_SIZE;
  2657. b.input = "a";
  2658. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2659. "\xb5\xd1";
  2660. b.inLen = XSTRLEN(b.input);
  2661. b.outLen = MD2_DIGEST_SIZE;
  2662. c.input = "abc";
  2663. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2664. "\xd6\xbb";
  2665. c.inLen = XSTRLEN(c.input);
  2666. c.outLen = MD2_DIGEST_SIZE;
  2667. d.input = "message digest";
  2668. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2669. "\x06\xb0";
  2670. d.inLen = XSTRLEN(d.input);
  2671. d.outLen = MD2_DIGEST_SIZE;
  2672. e.input = "abcdefghijklmnopqrstuvwxyz";
  2673. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2674. "\x94\x0b";
  2675. e.inLen = XSTRLEN(e.input);
  2676. e.outLen = MD2_DIGEST_SIZE;
  2677. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2678. "6789";
  2679. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2680. "\x38\xcd";
  2681. f.inLen = XSTRLEN(f.input);
  2682. f.outLen = MD2_DIGEST_SIZE;
  2683. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2684. "9012345678901234567890";
  2685. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2686. "\xef\xd8";
  2687. g.inLen = XSTRLEN(g.input);
  2688. g.outLen = MD2_DIGEST_SIZE;
  2689. test_md2[0] = a;
  2690. test_md2[1] = b;
  2691. test_md2[2] = c;
  2692. test_md2[3] = d;
  2693. test_md2[4] = e;
  2694. test_md2[5] = f;
  2695. test_md2[6] = g;
  2696. wc_InitMd2(&md2);
  2697. for (i = 0; i < times; ++i) {
  2698. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2699. wc_Md2Final(&md2, hash);
  2700. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2701. return WC_TEST_RET_ENC_I(i);
  2702. }
  2703. for (i = 0; i < times; ++i) {
  2704. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2705. if (ret != 0) {
  2706. return WC_TEST_RET_ENC_I(i);
  2707. }
  2708. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2709. return WC_TEST_RET_ENC_I(i);
  2710. }
  2711. }
  2712. return 0;
  2713. }
  2714. #endif
  2715. #ifndef NO_MD5
  2716. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2717. {
  2718. wc_test_ret_t ret = 0;
  2719. wc_Md5 md5, md5Copy;
  2720. byte hash[WC_MD5_DIGEST_SIZE];
  2721. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2722. testVector a, b, c, d, e, f;
  2723. testVector test_md5[6];
  2724. int times = sizeof(test_md5) / sizeof(testVector), i;
  2725. WOLFSSL_ENTER("md5_test");
  2726. a.input = "";
  2727. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2728. "\x7e";
  2729. a.inLen = XSTRLEN(a.input);
  2730. a.outLen = WC_MD5_DIGEST_SIZE;
  2731. b.input = "abc";
  2732. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2733. "\x72";
  2734. b.inLen = XSTRLEN(b.input);
  2735. b.outLen = WC_MD5_DIGEST_SIZE;
  2736. c.input = "message digest";
  2737. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2738. "\xd0";
  2739. c.inLen = XSTRLEN(c.input);
  2740. c.outLen = WC_MD5_DIGEST_SIZE;
  2741. d.input = "abcdefghijklmnopqrstuvwxyz";
  2742. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2743. "\x3b";
  2744. d.inLen = XSTRLEN(d.input);
  2745. d.outLen = WC_MD5_DIGEST_SIZE;
  2746. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2747. "6789";
  2748. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2749. "\x9f";
  2750. e.inLen = XSTRLEN(e.input);
  2751. e.outLen = WC_MD5_DIGEST_SIZE;
  2752. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2753. "9012345678901234567890";
  2754. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2755. "\x7a";
  2756. f.inLen = XSTRLEN(f.input);
  2757. f.outLen = WC_MD5_DIGEST_SIZE;
  2758. test_md5[0] = a;
  2759. test_md5[1] = b;
  2760. test_md5[2] = c;
  2761. test_md5[3] = d;
  2762. test_md5[4] = e;
  2763. test_md5[5] = f;
  2764. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2765. if (ret != 0)
  2766. return WC_TEST_RET_ENC_EC(ret);
  2767. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2768. if (ret != 0) {
  2769. wc_Md5Free(&md5);
  2770. return WC_TEST_RET_ENC_EC(ret);
  2771. }
  2772. for (i = 0; i < times; ++i) {
  2773. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2774. (word32)test_md5[i].inLen);
  2775. if (ret != 0)
  2776. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2777. ret = wc_Md5GetHash(&md5, hashcopy);
  2778. if (ret != 0)
  2779. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2780. ret = wc_Md5Copy(&md5, &md5Copy);
  2781. if (ret != 0)
  2782. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2783. ret = wc_Md5Final(&md5, hash);
  2784. if (ret != 0)
  2785. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2786. wc_Md5Free(&md5Copy);
  2787. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2788. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2789. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2790. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2791. }
  2792. #ifndef NO_LARGE_HASH_TEST
  2793. /* BEGIN LARGE HASH TEST */ {
  2794. byte large_input[1024];
  2795. const char* large_digest =
  2796. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2797. for (i = 0; i < (int)sizeof(large_input); i++) {
  2798. large_input[i] = (byte)(i & 0xFF);
  2799. }
  2800. times = 100;
  2801. #ifdef WOLFSSL_PIC32MZ_HASH
  2802. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2803. #endif
  2804. for (i = 0; i < times; ++i) {
  2805. ret = wc_Md5Update(&md5, (byte*)large_input,
  2806. (word32)sizeof(large_input));
  2807. if (ret != 0)
  2808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2809. }
  2810. ret = wc_Md5Final(&md5, hash);
  2811. if (ret != 0)
  2812. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2813. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2814. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2815. } /* END LARGE HASH TEST */
  2816. #endif /* NO_LARGE_HASH_TEST */
  2817. exit:
  2818. wc_Md5Free(&md5);
  2819. wc_Md5Free(&md5Copy);
  2820. return ret;
  2821. }
  2822. #endif /* NO_MD5 */
  2823. #ifndef NO_MD4
  2824. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2825. {
  2826. Md4 md4;
  2827. byte hash[MD4_DIGEST_SIZE];
  2828. testVector a, b, c, d, e, f, g;
  2829. testVector test_md4[7];
  2830. int times = sizeof(test_md4) / sizeof(testVector), i;
  2831. WOLFSSL_ENTER("md4_test");
  2832. a.input = "";
  2833. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2834. "\xc0";
  2835. a.inLen = XSTRLEN(a.input);
  2836. a.outLen = MD4_DIGEST_SIZE;
  2837. b.input = "a";
  2838. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2839. "\x24";
  2840. b.inLen = XSTRLEN(b.input);
  2841. b.outLen = MD4_DIGEST_SIZE;
  2842. c.input = "abc";
  2843. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2844. "\x9d";
  2845. c.inLen = XSTRLEN(c.input);
  2846. c.outLen = MD4_DIGEST_SIZE;
  2847. d.input = "message digest";
  2848. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2849. "\x4b";
  2850. d.inLen = XSTRLEN(d.input);
  2851. d.outLen = MD4_DIGEST_SIZE;
  2852. e.input = "abcdefghijklmnopqrstuvwxyz";
  2853. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2854. "\xa9";
  2855. e.inLen = XSTRLEN(e.input);
  2856. e.outLen = MD4_DIGEST_SIZE;
  2857. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2858. "6789";
  2859. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2860. "\xe4";
  2861. f.inLen = XSTRLEN(f.input);
  2862. f.outLen = MD4_DIGEST_SIZE;
  2863. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2864. "9012345678901234567890";
  2865. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2866. "\x36";
  2867. g.inLen = XSTRLEN(g.input);
  2868. g.outLen = MD4_DIGEST_SIZE;
  2869. test_md4[0] = a;
  2870. test_md4[1] = b;
  2871. test_md4[2] = c;
  2872. test_md4[3] = d;
  2873. test_md4[4] = e;
  2874. test_md4[5] = f;
  2875. test_md4[6] = g;
  2876. wc_InitMd4(&md4);
  2877. for (i = 0; i < times; ++i) {
  2878. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2879. wc_Md4Final(&md4, hash);
  2880. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2881. return WC_TEST_RET_ENC_I(i);
  2882. }
  2883. return 0;
  2884. }
  2885. #endif /* NO_MD4 */
  2886. #ifndef NO_SHA
  2887. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2888. {
  2889. wc_test_ret_t ret = 0;
  2890. wc_Sha sha, shaCopy;
  2891. byte hash[WC_SHA_DIGEST_SIZE];
  2892. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2893. testVector a, b, c, d, e;
  2894. testVector test_sha[5];
  2895. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2896. WOLFSSL_ENTER("sha_test");
  2897. a.input = "";
  2898. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2899. "\x90\xaf\xd8\x07\x09";
  2900. a.inLen = XSTRLEN(a.input);
  2901. a.outLen = WC_SHA_DIGEST_SIZE;
  2902. b.input = "abc";
  2903. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2904. "\x6C\x9C\xD0\xD8\x9D";
  2905. b.inLen = XSTRLEN(b.input);
  2906. b.outLen = WC_SHA_DIGEST_SIZE;
  2907. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2908. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2909. "\xE5\xE5\x46\x70\xF1";
  2910. c.inLen = XSTRLEN(c.input);
  2911. c.outLen = WC_SHA_DIGEST_SIZE;
  2912. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2913. "aaaaaa";
  2914. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2915. "\x2A\x25\xEC\x64\x4D";
  2916. d.inLen = XSTRLEN(d.input);
  2917. d.outLen = WC_SHA_DIGEST_SIZE;
  2918. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2919. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2920. "aaaaaaaaaa";
  2921. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2922. "\x53\x99\x5E\x26\xA0";
  2923. e.inLen = XSTRLEN(e.input);
  2924. e.outLen = WC_SHA_DIGEST_SIZE;
  2925. test_sha[0] = a;
  2926. test_sha[1] = b;
  2927. test_sha[2] = c;
  2928. test_sha[3] = d;
  2929. test_sha[4] = e;
  2930. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2931. if (ret != 0)
  2932. return WC_TEST_RET_ENC_EC(ret);
  2933. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2934. if (ret != 0) {
  2935. wc_ShaFree(&sha);
  2936. return WC_TEST_RET_ENC_EC(ret);
  2937. }
  2938. for (i = 0; i < times; ++i) {
  2939. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2940. (word32)test_sha[i].inLen);
  2941. if (ret != 0)
  2942. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2943. ret = wc_ShaGetHash(&sha, hashcopy);
  2944. if (ret != 0)
  2945. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2946. ret = wc_ShaCopy(&sha, &shaCopy);
  2947. if (ret != 0)
  2948. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2949. ret = wc_ShaFinal(&sha, hash);
  2950. if (ret != 0)
  2951. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2952. wc_ShaFree(&shaCopy);
  2953. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2954. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2955. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2956. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2957. }
  2958. #ifndef NO_LARGE_HASH_TEST
  2959. /* BEGIN LARGE HASH TEST */ {
  2960. byte large_input[1024];
  2961. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2962. defined(HASH_SIZE_LIMIT)
  2963. const char* large_digest =
  2964. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2965. "\x15\x8c\x6d\xb6";
  2966. #else
  2967. const char* large_digest =
  2968. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2969. "\xc5\xd9\x0a\xac";
  2970. #endif
  2971. for (i = 0; i < (int)sizeof(large_input); i++) {
  2972. large_input[i] = (byte)(i & 0xFF);
  2973. }
  2974. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2975. defined(HASH_SIZE_LIMIT)
  2976. times = 20;
  2977. #else
  2978. times = 100;
  2979. #endif
  2980. #ifdef WOLFSSL_PIC32MZ_HASH
  2981. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2982. #endif
  2983. for (i = 0; i < times; ++i) {
  2984. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2985. (word32)sizeof(large_input));
  2986. if (ret != 0)
  2987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2988. }
  2989. ret = wc_ShaFinal(&sha, hash);
  2990. if (ret != 0)
  2991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2992. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2993. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2994. } /* END LARGE HASH TEST */
  2995. #endif /* NO_LARGE_HASH_TEST */
  2996. exit:
  2997. wc_ShaFree(&sha);
  2998. wc_ShaFree(&shaCopy);
  2999. return ret;
  3000. }
  3001. #endif /* NO_SHA */
  3002. #ifdef WOLFSSL_RIPEMD
  3003. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  3004. {
  3005. RipeMd ripemd;
  3006. wc_test_ret_t ret;
  3007. byte hash[RIPEMD_DIGEST_SIZE];
  3008. testVector a, b, c, d;
  3009. testVector test_ripemd[4];
  3010. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  3011. WOLFSSL_ENTER("ripemd_test");
  3012. a.input = "abc";
  3013. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  3014. "\xb0\x87\xf1\x5a\x0b\xfc";
  3015. a.inLen = XSTRLEN(a.input);
  3016. a.outLen = RIPEMD_DIGEST_SIZE;
  3017. b.input = "message digest";
  3018. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  3019. "\x5f\xfa\x21\x59\x5f\x36";
  3020. b.inLen = XSTRLEN(b.input);
  3021. b.outLen = RIPEMD_DIGEST_SIZE;
  3022. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3023. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  3024. "\xf4\x9a\xda\x62\xeb\x2b";
  3025. c.inLen = XSTRLEN(c.input);
  3026. c.outLen = RIPEMD_DIGEST_SIZE;
  3027. d.input = "12345678901234567890123456789012345678901234567890123456"
  3028. "789012345678901234567890";
  3029. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  3030. "\x82\xbf\x63\x32\x6b\xfb";
  3031. d.inLen = XSTRLEN(d.input);
  3032. d.outLen = RIPEMD_DIGEST_SIZE;
  3033. test_ripemd[0] = a;
  3034. test_ripemd[1] = b;
  3035. test_ripemd[2] = c;
  3036. test_ripemd[3] = d;
  3037. ret = wc_InitRipeMd(&ripemd);
  3038. if (ret != 0) {
  3039. return WC_TEST_RET_ENC_EC(ret);
  3040. }
  3041. for (i = 0; i < times; ++i) {
  3042. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  3043. (word32)test_ripemd[i].inLen);
  3044. if (ret != 0) {
  3045. return WC_TEST_RET_ENC_I(i);
  3046. }
  3047. ret = wc_RipeMdFinal(&ripemd, hash);
  3048. if (ret != 0) {
  3049. return WC_TEST_RET_ENC_I(i);
  3050. }
  3051. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  3052. return WC_TEST_RET_ENC_I(i);
  3053. }
  3054. return 0;
  3055. }
  3056. #endif /* WOLFSSL_RIPEMD */
  3057. #ifdef HAVE_BLAKE2
  3058. #define BLAKE2B_TESTS 3
  3059. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  3060. {
  3061. {
  3062. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  3063. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  3064. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  3065. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  3066. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  3067. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  3068. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  3069. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  3070. },
  3071. {
  3072. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  3073. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  3074. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  3075. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  3076. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  3077. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  3078. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  3079. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  3080. },
  3081. {
  3082. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  3083. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  3084. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  3085. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  3086. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  3087. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  3088. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  3089. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  3090. }
  3091. };
  3092. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  3093. {
  3094. Blake2b b2b;
  3095. byte digest[64];
  3096. byte input[64];
  3097. int i, ret;
  3098. WOLFSSL_ENTER("blake2b_test");
  3099. for (i = 0; i < (int)sizeof(input); i++)
  3100. input[i] = (byte)i;
  3101. for (i = 0; i < BLAKE2B_TESTS; i++) {
  3102. ret = wc_InitBlake2b(&b2b, 64);
  3103. if (ret != 0)
  3104. return WC_TEST_RET_ENC_I(i);
  3105. ret = wc_Blake2bUpdate(&b2b, input, (word32)i);
  3106. if (ret != 0)
  3107. return WC_TEST_RET_ENC_I(i);
  3108. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3109. if (ret != 0)
  3110. return WC_TEST_RET_ENC_I(i);
  3111. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  3112. return WC_TEST_RET_ENC_I(i);
  3113. }
  3114. }
  3115. return 0;
  3116. }
  3117. #endif /* HAVE_BLAKE2 */
  3118. #ifdef HAVE_BLAKE2S
  3119. #define BLAKE2S_TESTS 3
  3120. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  3121. {
  3122. {
  3123. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  3124. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  3125. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  3126. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  3127. },
  3128. {
  3129. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  3130. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  3131. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  3132. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  3133. },
  3134. {
  3135. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  3136. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  3137. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  3138. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  3139. }
  3140. };
  3141. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  3142. {
  3143. Blake2s b2s;
  3144. byte digest[32];
  3145. byte input[64];
  3146. int i, ret;
  3147. WOLFSSL_ENTER("blake2s_test");
  3148. for (i = 0; i < (int)sizeof(input); i++)
  3149. input[i] = (byte)i;
  3150. for (i = 0; i < BLAKE2S_TESTS; i++) {
  3151. ret = wc_InitBlake2s(&b2s, 32);
  3152. if (ret != 0)
  3153. return WC_TEST_RET_ENC_I(i);
  3154. ret = wc_Blake2sUpdate(&b2s, input, (word32)i);
  3155. if (ret != 0)
  3156. return WC_TEST_RET_ENC_I(i);
  3157. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3158. if (ret != 0)
  3159. return WC_TEST_RET_ENC_I(i);
  3160. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  3161. return WC_TEST_RET_ENC_I(i);
  3162. }
  3163. }
  3164. return 0;
  3165. }
  3166. #endif /* HAVE_BLAKE2S */
  3167. #ifdef WOLFSSL_SHA224
  3168. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  3169. {
  3170. wc_Sha224 sha, shaCopy;
  3171. byte hash[WC_SHA224_DIGEST_SIZE];
  3172. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  3173. wc_test_ret_t ret = 0;
  3174. testVector a, b, c;
  3175. testVector test_sha[3];
  3176. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3177. WOLFSSL_ENTER("sha224_test");
  3178. a.input = "";
  3179. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  3180. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  3181. a.inLen = XSTRLEN(a.input);
  3182. a.outLen = WC_SHA224_DIGEST_SIZE;
  3183. b.input = "abc";
  3184. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  3185. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  3186. b.inLen = XSTRLEN(b.input);
  3187. b.outLen = WC_SHA224_DIGEST_SIZE;
  3188. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3189. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  3190. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  3191. c.inLen = XSTRLEN(c.input);
  3192. c.outLen = WC_SHA224_DIGEST_SIZE;
  3193. test_sha[0] = a;
  3194. test_sha[1] = b;
  3195. test_sha[2] = c;
  3196. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  3197. if (ret != 0)
  3198. return WC_TEST_RET_ENC_EC(ret);
  3199. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  3200. if (ret != 0) {
  3201. wc_Sha224Free(&sha);
  3202. return WC_TEST_RET_ENC_EC(ret);
  3203. }
  3204. for (i = 0; i < times; ++i) {
  3205. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  3206. (word32)test_sha[i].inLen);
  3207. if (ret != 0)
  3208. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3209. ret = wc_Sha224GetHash(&sha, hashcopy);
  3210. if (ret != 0)
  3211. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3212. ret = wc_Sha224Copy(&sha, &shaCopy);
  3213. if (ret != 0)
  3214. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3215. ret = wc_Sha224Final(&sha, hash);
  3216. if (ret != 0)
  3217. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3218. wc_Sha224Free(&shaCopy);
  3219. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3220. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3221. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  3222. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3223. }
  3224. exit:
  3225. wc_Sha224Free(&sha);
  3226. wc_Sha224Free(&shaCopy);
  3227. return ret;
  3228. }
  3229. #endif
  3230. #ifndef NO_SHA256
  3231. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  3232. {
  3233. wc_Sha256 sha, shaCopy;
  3234. byte hash[WC_SHA256_DIGEST_SIZE];
  3235. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  3236. wc_test_ret_t ret = 0;
  3237. testVector a, b, c, d;
  3238. testVector test_sha[4];
  3239. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3240. byte i_hash[WC_SHA256_DIGEST_SIZE];
  3241. byte i_hashcopy[WC_SHA256_DIGEST_SIZE];
  3242. testVector interleave_test_sha[4];
  3243. wc_Sha256 i_sha, i_shaCopy;
  3244. #endif
  3245. #ifndef NO_LARGE_HASH_TEST
  3246. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3247. #ifdef WOLFSSL_SMALL_STACK
  3248. byte *large_input = NULL;
  3249. #else
  3250. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3251. #endif
  3252. #endif
  3253. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3254. WOLFSSL_ENTER("sha256_test");
  3255. a.input = "";
  3256. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  3257. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  3258. "\xb8\x55";
  3259. a.inLen = XSTRLEN(a.input);
  3260. a.outLen = WC_SHA256_DIGEST_SIZE;
  3261. b.input = "abc";
  3262. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  3263. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  3264. "\x15\xAD";
  3265. b.inLen = XSTRLEN(b.input);
  3266. b.outLen = WC_SHA256_DIGEST_SIZE;
  3267. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3268. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  3269. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  3270. "\x06\xC1";
  3271. c.inLen = XSTRLEN(c.input);
  3272. c.outLen = WC_SHA256_DIGEST_SIZE;
  3273. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  3274. "aaaaaa"; /* this is BLOCKSIZE length */
  3275. d.output = "\xFF\xE0\x54\xFE\x7A\xE0\xCB\x6D\xC6\x5C\x3A\xF9\xB6\x1D\x52"
  3276. "\x09\xF4\x39\x85\x1D\xB4\x3D\x0B\xA5\x99\x73\x37\xDF\x15\x46"
  3277. "\x68\xEB";
  3278. d.inLen = XSTRLEN(d.input);
  3279. d.outLen = WC_SHA256_DIGEST_SIZE;
  3280. test_sha[0] = a;
  3281. test_sha[1] = b;
  3282. test_sha[2] = c;
  3283. test_sha[3] = d;
  3284. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3285. interleave_test_sha[0] = a;
  3286. interleave_test_sha[1] = b;
  3287. interleave_test_sha[2] = c;
  3288. interleave_test_sha[3] = d;
  3289. #endif
  3290. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  3291. if (ret != 0)
  3292. return WC_TEST_RET_ENC_EC(ret);
  3293. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3294. ret = wc_InitSha256_ex(&i_sha, HEAP_HINT, devId);
  3295. if (ret != 0)
  3296. return WC_TEST_RET_ENC_EC(ret);
  3297. #endif
  3298. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  3299. if (ret != 0) {
  3300. wc_Sha256Free(&sha);
  3301. return WC_TEST_RET_ENC_EC(ret);
  3302. }
  3303. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3304. ret = wc_InitSha256_ex(&i_shaCopy, HEAP_HINT, devId);
  3305. if (ret != 0) {
  3306. wc_Sha256Free(&sha);
  3307. wc_Sha256Free(&i_sha);
  3308. return WC_TEST_RET_ENC_EC(ret);
  3309. }
  3310. #endif
  3311. for (i = 0; i < times; ++i) {
  3312. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  3313. (word32)test_sha[i].inLen);
  3314. if (ret != 0) {
  3315. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3316. }
  3317. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3318. ret = wc_Sha256Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3319. (word32)interleave_test_sha[i].inLen);
  3320. if (ret != 0) {
  3321. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3322. }
  3323. #endif
  3324. ret = wc_Sha256GetHash(&sha, hashcopy);
  3325. if (ret != 0)
  3326. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3327. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3328. ret = wc_Sha256GetHash(&i_sha, i_hashcopy);
  3329. if (ret != 0)
  3330. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3331. #endif
  3332. ret = wc_Sha256Copy(&sha, &shaCopy);
  3333. if (ret != 0)
  3334. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3335. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3336. ret = wc_Sha256Copy(&i_sha, &i_shaCopy);
  3337. if (ret != 0)
  3338. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3339. #endif
  3340. ret = wc_Sha256Final(&sha, hash);
  3341. if (ret != 0)
  3342. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3343. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3344. ret = wc_Sha256Final(&i_sha, i_hash);
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3347. #endif
  3348. wc_Sha256Free(&shaCopy);
  3349. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3350. wc_Sha256Free(&i_shaCopy);
  3351. #endif
  3352. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3353. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3354. }
  3355. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3356. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3357. }
  3358. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3359. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3360. WC_SHA256_DIGEST_SIZE) != 0) {
  3361. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3362. }
  3363. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3364. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3365. }
  3366. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3367. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3368. }
  3369. if (XMEMCMP(i_hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3370. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3371. }
  3372. #endif
  3373. }
  3374. #ifndef NO_LARGE_HASH_TEST
  3375. /* BEGIN LARGE HASH TEST */ {
  3376. #ifdef HASH_SIZE_LIMIT
  3377. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3378. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  3379. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  3380. #else
  3381. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3382. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  3383. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  3384. #endif
  3385. #ifdef WOLFSSL_SMALL_STACK
  3386. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3387. DYNAMIC_TYPE_TMP_BUFFER);
  3388. if (large_input == NULL) {
  3389. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3390. }
  3391. #endif
  3392. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3393. large_input[i] = (byte)(i & 0xFF);
  3394. }
  3395. #ifdef HASH_SIZE_LIMIT
  3396. times = 20;
  3397. #else
  3398. times = 100;
  3399. #endif
  3400. #ifdef WOLFSSL_PIC32MZ_HASH
  3401. wc_Sha256SizeSet(&sha, times * LARGE_HASH_TEST_INPUT_SZ);
  3402. #endif
  3403. for (i = 0; i < times; ++i) {
  3404. ret = wc_Sha256Update(&sha, (byte*)large_input,
  3405. LARGE_HASH_TEST_INPUT_SZ);
  3406. if (ret != 0)
  3407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3408. }
  3409. ret = wc_Sha256Final(&sha, hash);
  3410. if (ret != 0)
  3411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3412. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  3413. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3414. } /* END LARGE HASH TEST */
  3415. #undef LARGE_HASH_TEST_INPUT_SZ
  3416. #endif /* NO_LARGE_HASH_TEST */
  3417. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
  3418. unsigned char data_hb[WC_SHA256_BLOCK_SIZE] = {
  3419. 0x61, 0x62, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00,
  3420. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3421. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3422. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3423. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3424. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3425. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3426. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18
  3427. };
  3428. ret = wc_Sha256HashBlock(&sha, data_hb, hash);
  3429. if (ret != 0) {
  3430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3431. }
  3432. if (XMEMCMP(hash, b.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3433. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3434. }
  3435. #endif
  3436. exit:
  3437. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3438. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3439. #endif
  3440. wc_Sha256Free(&sha);
  3441. wc_Sha256Free(&shaCopy);
  3442. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3443. wc_Sha256Free(&i_sha);
  3444. wc_Sha256Free(&i_shaCopy);
  3445. #endif
  3446. return ret;
  3447. }
  3448. #endif
  3449. #ifdef WOLFSSL_SHA512
  3450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  3451. {
  3452. /*
  3453. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  3454. */
  3455. wc_Sha512 sha, shaCopy;
  3456. byte hash[WC_SHA512_DIGEST_SIZE];
  3457. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  3458. wc_test_ret_t ret = 0;
  3459. testVector a, b, c;
  3460. testVector test_sha[3];
  3461. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3462. wc_Sha512 i_sha, i_shaCopy;
  3463. byte i_hash[WC_SHA512_DIGEST_SIZE];
  3464. byte i_hashcopy[WC_SHA512_DIGEST_SIZE];
  3465. testVector interleave_test_sha[3];
  3466. #endif
  3467. #ifndef NO_LARGE_HASH_TEST
  3468. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3469. #ifdef WOLFSSL_SMALL_STACK
  3470. byte *large_input = NULL;
  3471. #else
  3472. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3473. #endif
  3474. #endif
  3475. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3476. WOLFSSL_ENTER("sha512_test");
  3477. a.input = "";
  3478. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  3479. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  3480. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  3481. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  3482. "\xf9\x27\xda\x3e";
  3483. a.inLen = XSTRLEN(a.input);
  3484. a.outLen = WC_SHA512_DIGEST_SIZE;
  3485. b.input = "abc";
  3486. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3487. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  3488. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  3489. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  3490. "\xa5\x4c\xa4\x9f";
  3491. b.inLen = XSTRLEN(b.input);
  3492. b.outLen = WC_SHA512_DIGEST_SIZE;
  3493. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3494. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3495. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  3496. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  3497. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  3498. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  3499. "\x87\x4b\xe9\x09";
  3500. c.inLen = XSTRLEN(c.input);
  3501. c.outLen = WC_SHA512_DIGEST_SIZE;
  3502. test_sha[0] = a;
  3503. test_sha[1] = b;
  3504. test_sha[2] = c;
  3505. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3506. interleave_test_sha[0] = a;
  3507. interleave_test_sha[1] = b;
  3508. interleave_test_sha[2] = c;
  3509. #endif
  3510. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  3511. if (ret != 0)
  3512. return WC_TEST_RET_ENC_EC(ret);
  3513. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3514. ret = wc_InitSha512_ex(&i_sha, HEAP_HINT, devId);
  3515. if (ret != 0)
  3516. return WC_TEST_RET_ENC_EC(ret);
  3517. #endif
  3518. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  3519. if (ret != 0) {
  3520. wc_Sha512Free(&sha);
  3521. return WC_TEST_RET_ENC_EC(ret);
  3522. }
  3523. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3524. ret = wc_InitSha512_ex(&i_shaCopy, HEAP_HINT, devId);
  3525. if (ret != 0) {
  3526. wc_Sha512Free(&sha);
  3527. wc_Sha512Free(&i_sha);
  3528. return WC_TEST_RET_ENC_EC(ret);
  3529. }
  3530. #endif
  3531. for (i = 0; i < times; ++i) {
  3532. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  3533. (word32)test_sha[i].inLen);
  3534. if (ret != 0)
  3535. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3536. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3537. ret = wc_Sha512Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3538. (word32)interleave_test_sha[i].inLen);
  3539. if (ret != 0)
  3540. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3541. #endif
  3542. ret = wc_Sha512GetHash(&sha, hashcopy);
  3543. if (ret != 0)
  3544. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3545. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3546. ret = wc_Sha512GetHash(&i_sha, i_hashcopy);
  3547. if (ret != 0)
  3548. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3549. #endif
  3550. ret = wc_Sha512Copy(&sha, &shaCopy);
  3551. if (ret != 0)
  3552. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3553. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3554. ret = wc_Sha512Copy(&i_sha, &i_shaCopy);
  3555. if (ret != 0)
  3556. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3557. #endif
  3558. ret = wc_Sha512Final(&sha, hash);
  3559. if (ret != 0)
  3560. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3561. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3562. ret = wc_Sha512Final(&i_sha, i_hash);
  3563. if (ret != 0)
  3564. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3565. #endif
  3566. wc_Sha512Free(&shaCopy);
  3567. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3568. wc_Sha512Free(&i_shaCopy);
  3569. #endif
  3570. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3571. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3572. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  3573. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3574. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3575. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3576. WC_SHA512_DIGEST_SIZE) != 0) {
  3577. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3578. }
  3579. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3580. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3581. }
  3582. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0) {
  3583. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3584. }
  3585. if (XMEMCMP(i_hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3586. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3587. }
  3588. #endif
  3589. }
  3590. #ifndef NO_LARGE_HASH_TEST
  3591. /* BEGIN LARGE HASH TEST */ {
  3592. #ifdef HASH_SIZE_LIMIT
  3593. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3594. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  3595. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  3596. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  3597. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  3598. #else
  3599. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3600. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  3601. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  3602. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  3603. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  3604. #endif
  3605. #ifdef WOLFSSL_SMALL_STACK
  3606. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3607. DYNAMIC_TYPE_TMP_BUFFER);
  3608. if (large_input == NULL) {
  3609. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3610. }
  3611. #endif
  3612. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3613. large_input[i] = (byte)(i & 0xFF);
  3614. }
  3615. #ifdef HASH_SIZE_LIMIT
  3616. times = 20;
  3617. #else
  3618. times = 100;
  3619. #endif
  3620. for (i = 0; i < times; ++i) {
  3621. ret = wc_Sha512Update(&sha, (byte*)large_input,
  3622. LARGE_HASH_TEST_INPUT_SZ);
  3623. if (ret != 0)
  3624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3625. }
  3626. ret = wc_Sha512Final(&sha, hash);
  3627. if (ret != 0)
  3628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3629. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  3630. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3631. #ifndef NO_UNALIGNED_MEMORY_TEST
  3632. /* Unaligned memory access test */
  3633. for (i = 1; i < 16; i++) {
  3634. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  3635. LARGE_HASH_TEST_INPUT_SZ - i);
  3636. if (ret != 0)
  3637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3638. ret = wc_Sha512Final(&sha, hash);
  3639. }
  3640. #endif
  3641. } /* END LARGE HASH TEST */
  3642. #undef LARGE_HASH_TEST_INPUT_SZ
  3643. #endif /* NO_LARGE_HASH_TEST */
  3644. exit:
  3645. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3646. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3647. #endif
  3648. wc_Sha512Free(&sha);
  3649. wc_Sha512Free(&shaCopy);
  3650. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3651. wc_Sha512Free(&i_sha);
  3652. wc_Sha512Free(&i_shaCopy);
  3653. #endif
  3654. return ret;
  3655. }
  3656. #if !defined(WOLFSSL_NOSHA512_224) && \
  3657. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  3659. {
  3660. /*
  3661. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3662. **
  3663. ** NIST SHA512/224 Document Example:
  3664. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  3665. */
  3666. wc_Sha512 sha, shaCopy;
  3667. byte hash[WC_SHA512_224_DIGEST_SIZE];
  3668. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  3669. wc_test_ret_t ret = 0;
  3670. testVector a, b, c;
  3671. testVector test_sha[3];
  3672. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3673. WOLFSSL_ENTER("sha512_224_test");
  3674. a.input = "";
  3675. a.output = "\x6e\xd0\xdd\x02"
  3676. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  3677. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  3678. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  3679. a.inLen = XSTRLEN(a.input);
  3680. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  3681. /*
  3682. ** See page 1 in above document for the SHA512/224 "abc" test:
  3683. */
  3684. b.input = "abc";
  3685. /*
  3686. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3687. */
  3688. b.output = "\x46\x34\x27\x0f"
  3689. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3690. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3691. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3692. b.inLen = XSTRLEN(b.input);
  3693. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3694. /*
  3695. ** See page 4 in above for the 2-block test:
  3696. */
  3697. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3698. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3699. /*
  3700. ** see page 9 in above document for the 2-block test message digest:
  3701. */
  3702. c.output = "\x23\xfe\xc5\xbb"
  3703. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3704. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3705. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3706. c.inLen = XSTRLEN(c.input);
  3707. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3708. test_sha[0] = a;
  3709. test_sha[1] = b;
  3710. test_sha[2] = c;
  3711. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3712. if (ret != 0)
  3713. return WC_TEST_RET_ENC_EC(ret);
  3714. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3715. if (ret != 0) {
  3716. wc_Sha512_224Free(&sha);
  3717. return WC_TEST_RET_ENC_EC(ret);
  3718. }
  3719. for (i = 0; i < times; ++i) {
  3720. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3721. (word32)test_sha[i].inLen);
  3722. if (ret != 0)
  3723. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3724. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3725. if (ret != 0)
  3726. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3727. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3728. if (ret != 0)
  3729. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3730. ret = wc_Sha512_224Final(&sha, hash);
  3731. if (ret != 0)
  3732. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3733. wc_Sha512_224Free(&shaCopy);
  3734. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3735. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3736. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3737. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3738. }
  3739. #ifndef NO_LARGE_HASH_TEST
  3740. /* BEGIN LARGE HASH TEST */ {
  3741. byte large_input[1024];
  3742. #ifdef HASH_SIZE_LIMIT
  3743. const char* large_digest =
  3744. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3745. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3746. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3747. "\x14\x6c\xcf\x6a";
  3748. #else
  3749. const char* large_digest =
  3750. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3751. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3752. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3753. "\xe2\x4e\x85\x05";
  3754. #endif
  3755. for (i = 0; i < (int)sizeof(large_input); i++) {
  3756. large_input[i] = (byte)(i & 0xFF);
  3757. }
  3758. #ifdef HASH_SIZE_LIMIT
  3759. times = 20;
  3760. #else
  3761. times = 100;
  3762. #endif
  3763. for (i = 0; i < times; ++i) {
  3764. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3765. (word32)sizeof(large_input));
  3766. if (ret != 0)
  3767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3768. }
  3769. ret = wc_Sha512_224Final(&sha, hash);
  3770. if (ret != 0)
  3771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3772. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3773. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3774. #ifndef NO_UNALIGNED_MEMORY_TEST
  3775. /* Unaligned memory access test */
  3776. for (i = 1; i < 16; i++) {
  3777. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3778. (word32)sizeof(large_input) - i);
  3779. if (ret != 0)
  3780. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3781. ret = wc_Sha512_224Final(&sha, hash);
  3782. }
  3783. #endif
  3784. } /* END LARGE HASH TEST */
  3785. #endif /* NO_LARGE_HASH_TEST */
  3786. exit:
  3787. wc_Sha512_224Free(&sha);
  3788. wc_Sha512_224Free(&shaCopy);
  3789. return ret;
  3790. } /* sha512_224_test */
  3791. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3792. #if !defined(WOLFSSL_NOSHA512_256) && \
  3793. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3794. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3795. {
  3796. /*
  3797. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3798. ** NIST SHA512/256 Document Example:
  3799. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3800. */
  3801. wc_Sha512 sha, shaCopy;
  3802. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3803. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3804. wc_test_ret_t ret = 0;
  3805. testVector a, b, c;
  3806. testVector test_sha[3];
  3807. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3808. WOLFSSL_ENTER("sha512_256_test");
  3809. a.input = "";
  3810. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3811. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3812. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3813. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3814. a.inLen = XSTRLEN(a.input);
  3815. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3816. /*
  3817. ** See page 1 of above document for "abc" example:
  3818. */
  3819. b.input = "abc";
  3820. /*
  3821. ** See page 4 of above document for "abc" example digest:
  3822. */
  3823. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3824. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3825. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3826. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3827. b.inLen = XSTRLEN(b.input);
  3828. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3829. /*
  3830. ** See page 4 of above document for Two Block Message Sample:
  3831. */
  3832. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3833. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3834. /*
  3835. ** See page 10 of above document for Two Block Message Sample digest:
  3836. */
  3837. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3838. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3839. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3840. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3841. c.inLen = XSTRLEN(c.input);
  3842. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3843. test_sha[0] = a;
  3844. test_sha[1] = b;
  3845. test_sha[2] = c;
  3846. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3847. if (ret != 0)
  3848. return WC_TEST_RET_ENC_EC(ret);
  3849. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3850. if (ret != 0) {
  3851. wc_Sha512_256Free(&sha);
  3852. return WC_TEST_RET_ENC_EC(ret);
  3853. }
  3854. for (i = 0; i < times; ++i) {
  3855. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3856. (word32)test_sha[i].inLen);
  3857. if (ret != 0)
  3858. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3859. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3860. if (ret != 0)
  3861. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3862. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3863. if (ret != 0)
  3864. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3865. ret = wc_Sha512_256Final(&sha, hash);
  3866. if (ret != 0)
  3867. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3868. wc_Sha512_256Free(&shaCopy);
  3869. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3870. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3871. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3872. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3873. }
  3874. #ifndef NO_LARGE_HASH_TEST
  3875. /* BEGIN LARGE HASH TEST */ {
  3876. byte large_input[1024];
  3877. #ifdef HASH_SIZE_LIMIT
  3878. const char* large_digest =
  3879. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3880. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3881. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3882. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3883. #else
  3884. const char* large_digest =
  3885. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3886. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3887. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3888. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3889. #endif
  3890. for (i = 0; i < (int)sizeof(large_input); i++) {
  3891. large_input[i] = (byte)(i & 0xFF);
  3892. }
  3893. #ifdef HASH_SIZE_LIMIT
  3894. times = 20;
  3895. #else
  3896. times = 100;
  3897. #endif
  3898. for (i = 0; i < times; ++i) {
  3899. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3900. (word32)sizeof(large_input));
  3901. if (ret != 0)
  3902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3903. }
  3904. ret = wc_Sha512_256Final(&sha, hash);
  3905. if (ret != 0)
  3906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3907. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3908. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3909. #ifndef NO_UNALIGNED_MEMORY_TEST
  3910. /* Unaligned memory access test */
  3911. for (i = 1; i < 16; i++) {
  3912. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3913. (word32)sizeof(large_input) - i);
  3914. if (ret != 0)
  3915. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3916. ret = wc_Sha512_256Final(&sha, hash);
  3917. }
  3918. #endif
  3919. } /* END LARGE HASH TEST */
  3920. #endif /* NO_LARGE_HASH_TEST */
  3921. exit:
  3922. wc_Sha512_256Free(&sha);
  3923. wc_Sha512_256Free(&shaCopy);
  3924. return ret;
  3925. } /* sha512_256_test */
  3926. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3927. #endif /* WOLFSSL_SHA512 */
  3928. #ifdef WOLFSSL_SHA384
  3929. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3930. {
  3931. wc_Sha384 sha, shaCopy;
  3932. byte hash[WC_SHA384_DIGEST_SIZE];
  3933. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3934. wc_test_ret_t ret = 0;
  3935. testVector a, b, c;
  3936. testVector test_sha[3];
  3937. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3938. WOLFSSL_ENTER("sha384_test");
  3939. a.input = "";
  3940. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3941. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3942. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3943. "\x98\xb9\x5b";
  3944. a.inLen = XSTRLEN(a.input);
  3945. a.outLen = WC_SHA384_DIGEST_SIZE;
  3946. b.input = "abc";
  3947. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3948. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3949. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3950. "\xc8\x25\xa7";
  3951. b.inLen = XSTRLEN(b.input);
  3952. b.outLen = WC_SHA384_DIGEST_SIZE;
  3953. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3954. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3955. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3956. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3957. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3958. "\x74\x60\x39";
  3959. c.inLen = XSTRLEN(c.input);
  3960. c.outLen = WC_SHA384_DIGEST_SIZE;
  3961. test_sha[0] = a;
  3962. test_sha[1] = b;
  3963. test_sha[2] = c;
  3964. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3965. if (ret != 0)
  3966. return WC_TEST_RET_ENC_EC(ret);
  3967. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3968. if (ret != 0) {
  3969. wc_Sha384Free(&sha);
  3970. return WC_TEST_RET_ENC_EC(ret);
  3971. }
  3972. for (i = 0; i < times; ++i) {
  3973. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3974. (word32)test_sha[i].inLen);
  3975. if (ret != 0)
  3976. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3977. ret = wc_Sha384GetHash(&sha, hashcopy);
  3978. if (ret != 0)
  3979. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3980. ret = wc_Sha384Copy(&sha, &shaCopy);
  3981. if (ret != 0)
  3982. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3983. ret = wc_Sha384Final(&sha, hash);
  3984. if (ret != 0)
  3985. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3986. wc_Sha384Free(&shaCopy);
  3987. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3988. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3989. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3990. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3991. }
  3992. #ifndef NO_LARGE_HASH_TEST
  3993. /* BEGIN LARGE HASH TEST */ {
  3994. byte large_input[1024];
  3995. #ifdef HASH_SIZE_LIMIT
  3996. const char* large_digest =
  3997. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3998. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3999. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  4000. #else
  4001. const char* large_digest =
  4002. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  4003. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  4004. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  4005. #endif
  4006. for (i = 0; i < (int)sizeof(large_input); i++) {
  4007. large_input[i] = (byte)(i & 0xFF);
  4008. }
  4009. #ifdef HASH_SIZE_LIMIT
  4010. times = 20;
  4011. #else
  4012. times = 100;
  4013. #endif
  4014. for (i = 0; i < times; ++i) {
  4015. ret = wc_Sha384Update(&sha, (byte*)large_input,
  4016. (word32)sizeof(large_input));
  4017. if (ret != 0)
  4018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4019. }
  4020. ret = wc_Sha384Final(&sha, hash);
  4021. if (ret != 0)
  4022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4023. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  4024. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4025. } /* END LARGE HASH TEST */
  4026. #endif /* NO_LARGE_HASH_TEST */
  4027. exit:
  4028. wc_Sha384Free(&sha);
  4029. wc_Sha384Free(&shaCopy);
  4030. return ret;
  4031. }
  4032. #endif /* WOLFSSL_SHA384 */
  4033. #ifdef WOLFSSL_SHA3
  4034. #ifndef WOLFSSL_NOSHA3_224
  4035. static wc_test_ret_t sha3_224_test(void)
  4036. {
  4037. wc_Sha3 sha;
  4038. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4039. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  4040. testVector a, b, c;
  4041. testVector test_sha[3];
  4042. wc_test_ret_t ret = 0;
  4043. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4044. a.input = "";
  4045. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  4046. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  4047. a.inLen = XSTRLEN(a.input);
  4048. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  4049. b.input = "abc";
  4050. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  4051. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  4052. b.inLen = XSTRLEN(b.input);
  4053. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  4054. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4055. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  4056. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  4057. c.inLen = XSTRLEN(c.input);
  4058. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  4059. test_sha[0] = a;
  4060. test_sha[1] = b;
  4061. test_sha[2] = c;
  4062. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  4063. if (ret != 0)
  4064. return WC_TEST_RET_ENC_EC(ret);
  4065. for (i = 0; i < times; ++i) {
  4066. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  4067. (word32)test_sha[i].inLen);
  4068. if (ret != 0)
  4069. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4070. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  4071. if (ret != 0)
  4072. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4073. ret = wc_Sha3_224_Final(&sha, hash);
  4074. if (ret != 0)
  4075. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4076. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  4077. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4078. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  4079. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4080. }
  4081. #ifndef NO_LARGE_HASH_TEST
  4082. /* BEGIN LARGE HASH TEST */ {
  4083. byte large_input[1024];
  4084. const char* large_digest =
  4085. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  4086. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  4087. for (i = 0; i < (int)sizeof(large_input); i++) {
  4088. large_input[i] = (byte)(i & 0xFF);
  4089. }
  4090. times = 100;
  4091. for (i = 0; i < times; ++i) {
  4092. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  4093. (word32)sizeof(large_input));
  4094. if (ret != 0)
  4095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4096. }
  4097. ret = wc_Sha3_224_Final(&sha, hash);
  4098. if (ret != 0)
  4099. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4100. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  4101. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4102. } /* END LARGE HASH TEST */
  4103. #endif /* NO_LARGE_HASH_TEST */
  4104. exit:
  4105. wc_Sha3_224_Free(&sha);
  4106. return ret;
  4107. }
  4108. #endif /* WOLFSSL_NOSHA3_224 */
  4109. #ifndef WOLFSSL_NOSHA3_256
  4110. static wc_test_ret_t sha3_256_test(void)
  4111. {
  4112. wc_Sha3 sha;
  4113. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4114. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  4115. testVector a, b, c;
  4116. testVector test_sha[3];
  4117. wc_test_ret_t ret = 0;
  4118. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4119. byte large_input[1024];
  4120. const char* large_digest =
  4121. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  4122. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  4123. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4124. /* test vector with hash of empty string */
  4125. const char* Keccak256EmptyOut =
  4126. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  4127. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  4128. #endif
  4129. /*
  4130. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  4131. */
  4132. a.input = "";
  4133. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  4134. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  4135. "\x43\x4a";
  4136. a.inLen = XSTRLEN(a.input);
  4137. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  4138. b.input = "abc";
  4139. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  4140. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  4141. "\x15\x32";
  4142. b.inLen = XSTRLEN(b.input);
  4143. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  4144. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4145. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  4146. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  4147. "\x33\x76";
  4148. c.inLen = XSTRLEN(c.input);
  4149. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  4150. test_sha[0] = a;
  4151. test_sha[1] = b;
  4152. test_sha[2] = c;
  4153. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  4154. if (ret != 0)
  4155. return WC_TEST_RET_ENC_EC(ret);
  4156. for (i = 0; i < times; ++i) {
  4157. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  4158. (word32)test_sha[i].inLen);
  4159. if (ret != 0)
  4160. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4161. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  4162. if (ret != 0)
  4163. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4164. ret = wc_Sha3_256_Final(&sha, hash);
  4165. if (ret != 0)
  4166. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4167. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  4168. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4169. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  4170. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4171. }
  4172. #ifndef NO_LARGE_HASH_TEST
  4173. /* BEGIN LARGE HASH TEST */ {
  4174. for (i = 0; i < (int)sizeof(large_input); i++) {
  4175. large_input[i] = (byte)(i & 0xFF);
  4176. }
  4177. times = 100;
  4178. for (i = 0; i < times; ++i) {
  4179. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  4180. (word32)sizeof(large_input));
  4181. if (ret != 0)
  4182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4183. }
  4184. ret = wc_Sha3_256_Final(&sha, hash);
  4185. if (ret != 0)
  4186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4187. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  4188. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4189. } /* END LARGE HASH TEST */
  4190. #endif /* NO_LARGE_HASH_TEST */
  4191. /* this is a software only variant of SHA3 not supported by external hardware devices */
  4192. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4193. /* Test for Keccak256 */
  4194. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  4195. if (ret != 0) {
  4196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4197. }
  4198. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  4199. if (ret != 0) {
  4200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4201. }
  4202. ret = wc_Sha3_256_Final(&sha, hash);
  4203. if (ret != 0) {
  4204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4205. }
  4206. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4207. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4208. }
  4209. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  4210. exit:
  4211. wc_Sha3_256_Free(&sha);
  4212. return ret;
  4213. }
  4214. #endif /* WOLFSSL_NOSHA3_256 */
  4215. #ifndef WOLFSSL_NOSHA3_384
  4216. static wc_test_ret_t sha3_384_test(void)
  4217. {
  4218. wc_Sha3 sha;
  4219. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4220. byte buf[64];
  4221. #ifndef NO_INTM_HASH_TEST
  4222. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  4223. #endif
  4224. testVector a, b, c;
  4225. testVector test_sha[3];
  4226. wc_test_ret_t ret;
  4227. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4228. /*
  4229. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  4230. */
  4231. a.input = "";
  4232. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  4233. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  4234. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  4235. "\xd5\xf0\x04";
  4236. a.inLen = XSTRLEN(a.input);
  4237. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  4238. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4239. /* NIST test vector with a length that is a multiple of 4 */
  4240. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  4241. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  4242. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  4243. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  4244. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  4245. "\x19\x87\x22";
  4246. b.inLen = XSTRLEN(b.input);
  4247. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4248. #else
  4249. b.input = "abc";
  4250. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  4251. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  4252. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  4253. "\x37\x6d\x25";
  4254. b.inLen = XSTRLEN(b.input);
  4255. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4256. #endif
  4257. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4258. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  4259. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  4260. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  4261. "\x65\x7c\x22";
  4262. c.inLen = XSTRLEN(c.input);
  4263. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  4264. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4265. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  4266. #else
  4267. test_sha[0] = a;
  4268. #endif
  4269. test_sha[1] = b;
  4270. test_sha[2] = c;
  4271. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  4272. if (ret != 0)
  4273. return WC_TEST_RET_ENC_EC(ret);
  4274. for (i = 0; i < times; ++i) {
  4275. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  4276. ret = wc_Sha3_384_Update(&sha, buf,
  4277. (word32)test_sha[i].inLen);
  4278. if (ret != 0)
  4279. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4280. #ifndef NO_INTM_HASH_TEST
  4281. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  4282. if (ret != 0)
  4283. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4284. #endif
  4285. ret = wc_Sha3_384_Final(&sha, hash);
  4286. if (ret != 0)
  4287. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4288. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  4289. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4290. #ifndef NO_INTM_HASH_TEST
  4291. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  4292. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4293. #endif
  4294. }
  4295. #ifndef NO_LARGE_HASH_TEST
  4296. /* BEGIN LARGE HASH TEST */ {
  4297. byte large_input[1024];
  4298. const char* large_digest =
  4299. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  4300. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  4301. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  4302. for (i = 0; i < (int)sizeof(large_input); i++) {
  4303. large_input[i] = (byte)(i & 0xFF);
  4304. }
  4305. times = 100;
  4306. for (i = 0; i < times; ++i) {
  4307. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  4308. (word32)sizeof(large_input));
  4309. if (ret != 0)
  4310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4311. }
  4312. ret = wc_Sha3_384_Final(&sha, hash);
  4313. if (ret != 0)
  4314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4315. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  4316. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4317. } /* END LARGE HASH TEST */
  4318. #endif /* NO_LARGE_HASH_TEST */
  4319. exit:
  4320. wc_Sha3_384_Free(&sha);
  4321. return ret;
  4322. }
  4323. #endif /* WOLFSSL_NOSHA3_384 */
  4324. #ifndef WOLFSSL_NOSHA3_512
  4325. static wc_test_ret_t sha3_512_test(void)
  4326. {
  4327. wc_Sha3 sha;
  4328. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4329. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  4330. testVector a, b, c;
  4331. testVector test_sha[3];
  4332. wc_test_ret_t ret;
  4333. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4334. /*
  4335. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  4336. */
  4337. a.input = "";
  4338. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  4339. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  4340. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  4341. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  4342. "\x28\x1d\xcd\x26";
  4343. a.inLen = XSTRLEN(a.input);
  4344. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  4345. b.input = "abc";
  4346. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  4347. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  4348. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  4349. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  4350. "\x4e\xec\x53\xf0";
  4351. b.inLen = XSTRLEN(b.input);
  4352. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  4353. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4354. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  4355. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  4356. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  4357. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  4358. "\x39\xd6\xe7\x5e";
  4359. c.inLen = XSTRLEN(c.input);
  4360. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  4361. test_sha[0] = a;
  4362. test_sha[1] = b;
  4363. test_sha[2] = c;
  4364. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  4365. if (ret != 0)
  4366. return WC_TEST_RET_ENC_EC(ret);
  4367. for (i = 0; i < times; ++i) {
  4368. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  4369. (word32)test_sha[i].inLen);
  4370. if (ret != 0)
  4371. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4372. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  4373. if (ret != 0)
  4374. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4375. ret = wc_Sha3_512_Final(&sha, hash);
  4376. if (ret != 0)
  4377. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4378. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  4379. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4380. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  4381. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4382. }
  4383. #ifndef NO_LARGE_HASH_TEST
  4384. /* BEGIN LARGE HASH TEST */ {
  4385. byte large_input[1024];
  4386. const char* large_digest =
  4387. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  4388. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  4389. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  4390. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  4391. for (i = 0; i < (int)sizeof(large_input); i++) {
  4392. large_input[i] = (byte)(i & 0xFF);
  4393. }
  4394. times = 100;
  4395. for (i = 0; i < times; ++i) {
  4396. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  4397. (word32)sizeof(large_input));
  4398. if (ret != 0)
  4399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4400. }
  4401. ret = wc_Sha3_512_Final(&sha, hash);
  4402. if (ret != 0)
  4403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4404. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  4405. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4406. } /* END LARGE HASH TEST */
  4407. #endif /* NO_LARGE_HASH_TEST */
  4408. exit:
  4409. wc_Sha3_512_Free(&sha);
  4410. return ret;
  4411. }
  4412. #endif /* WOLFSSL_NOSHA3_512 */
  4413. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  4414. {
  4415. wc_test_ret_t ret;
  4416. (void)ret;
  4417. WOLFSSL_ENTER("sha3_test");
  4418. #ifndef WOLFSSL_NOSHA3_224
  4419. if ((ret = sha3_224_test()) != 0)
  4420. return ret;
  4421. #endif
  4422. #ifndef WOLFSSL_NOSHA3_256
  4423. if ((ret = sha3_256_test()) != 0)
  4424. return ret;
  4425. #endif
  4426. #ifndef WOLFSSL_NOSHA3_384
  4427. if ((ret = sha3_384_test()) != 0)
  4428. return ret;
  4429. #endif
  4430. #ifndef WOLFSSL_NOSHA3_512
  4431. if ((ret = sha3_512_test()) != 0)
  4432. return ret;
  4433. #endif
  4434. return 0;
  4435. }
  4436. #endif /* WOLFSSL_SHA3 */
  4437. #ifdef WOLFSSL_SHAKE128
  4438. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4439. size_t large_input_buf_size)
  4440. {
  4441. testVector a, b, c, d, e;
  4442. testVector test_sha[5];
  4443. wc_test_ret_t ret = 0;
  4444. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4445. static const char large_digest[] =
  4446. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  4447. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  4448. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  4449. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  4450. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  4451. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  4452. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  4453. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  4454. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  4455. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  4456. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  4457. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  4458. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  4459. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  4460. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4461. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  4462. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  4463. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  4464. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  4465. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  4466. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  4467. byte hash[sizeof(large_digest) - 1];
  4468. /*
  4469. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4470. */
  4471. a.input = "";
  4472. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4473. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4474. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4475. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4476. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4477. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4478. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4479. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  4480. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  4481. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  4482. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  4483. "\xb0\xfc\xa9";
  4484. a.inLen = XSTRLEN(a.input);
  4485. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  4486. b.input = "abc";
  4487. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4488. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4489. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4490. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4491. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4492. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4493. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4494. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  4495. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  4496. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  4497. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  4498. "\x47\x58\x4e";
  4499. b.inLen = XSTRLEN(b.input);
  4500. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  4501. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4502. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4503. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4504. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4505. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4506. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4507. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4508. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4509. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  4510. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  4511. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  4512. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  4513. "\xb4\xc9\x54";
  4514. c.inLen = XSTRLEN(c.input);
  4515. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  4516. /* Taken from NIST CAVP test vectors - full rate output. */
  4517. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4518. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4519. "\x85\xe0";
  4520. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4521. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4522. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4523. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4524. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4525. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4526. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4527. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4528. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4529. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  4530. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  4531. "\x55\x4d\x09";
  4532. d.inLen = 32;
  4533. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  4534. /* Taken from NIST CAVP test vectors - more than one output block. */
  4535. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4536. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4537. "\x47\xe4";
  4538. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4539. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4540. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4541. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4542. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4543. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4544. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4545. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4546. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4547. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4548. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4549. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4550. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4551. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4552. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4553. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4554. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  4555. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  4556. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  4557. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  4558. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  4559. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  4560. "\xd9\xe9\xe6\xf9\xff\xa5";
  4561. e.inLen = 32;
  4562. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  4563. test_sha[0] = a;
  4564. test_sha[1] = b;
  4565. test_sha[2] = c;
  4566. test_sha[3] = d;
  4567. test_sha[4] = e;
  4568. for (i = 0; i < times; ++i) {
  4569. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4570. if (ret != 0)
  4571. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4572. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  4573. (word32)test_sha[i].inLen);
  4574. if (ret != 0)
  4575. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4576. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4577. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  4578. if (ret != 0)
  4579. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4580. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4581. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4582. }
  4583. #ifndef NO_LARGE_HASH_TEST
  4584. /* BEGIN LARGE HASH TEST */ {
  4585. for (i = 0; i < (int)large_input_buf_size; i++) {
  4586. large_input_buf[i] = (byte)(i & 0xFF);
  4587. }
  4588. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4589. if (ret != 0)
  4590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4591. /* Absorb is non-incremental. */
  4592. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  4593. (word32)large_input_buf_size);
  4594. if (ret != 0)
  4595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4596. /* Able to squeeze out blocks incrementally. */
  4597. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  4598. if (ret != 0)
  4599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4600. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4601. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  4602. if (ret != 0)
  4603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4604. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4605. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4606. } /* END LARGE HASH TEST */
  4607. #endif /* NO_LARGE_HASH_TEST */
  4608. exit:
  4609. return ret;
  4610. }
  4611. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  4612. {
  4613. wc_Shake sha;
  4614. byte hash[250];
  4615. testVector a, b, c, d, e;
  4616. testVector test_sha[5];
  4617. wc_test_ret_t ret = 0;
  4618. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4619. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  4620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4621. byte *large_input = NULL;
  4622. #else
  4623. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  4624. #endif
  4625. static const char large_digest[] =
  4626. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  4627. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  4628. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  4629. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  4630. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  4631. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  4632. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  4633. "\xfa\x1b";
  4634. WOLFSSL_ENTER("shake128_test");
  4635. /*
  4636. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4637. */
  4638. a.input = "";
  4639. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4640. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4641. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4642. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4643. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4644. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4645. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4646. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  4647. a.inLen = XSTRLEN(a.input);
  4648. a.outLen = 114;
  4649. b.input = "abc";
  4650. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4651. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4652. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4653. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4654. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4655. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4656. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4657. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  4658. b.inLen = XSTRLEN(b.input);
  4659. b.outLen = 114;
  4660. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4661. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4662. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4663. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4664. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4665. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4666. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4667. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4668. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  4669. c.inLen = XSTRLEN(c.input);
  4670. c.outLen = 114;
  4671. /* Taken from NIST CAVP test vectors - full rate output. */
  4672. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4673. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4674. "\x85\xe0";
  4675. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4676. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4677. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4678. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4679. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4680. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4681. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4682. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4683. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4684. "\xa1";
  4685. d.inLen = 32;
  4686. d.outLen = 136;
  4687. /* Taken from NIST CAVP test vectors - more than one output block. */
  4688. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4689. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4690. "\x47\xe4";
  4691. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4692. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4693. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4694. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4695. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4696. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4697. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4698. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4699. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4700. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4701. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4702. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4703. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4704. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4705. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4706. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4707. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4708. e.inLen = 32;
  4709. e.outLen = 250;
  4710. test_sha[0] = a;
  4711. test_sha[1] = b;
  4712. test_sha[2] = c;
  4713. test_sha[3] = d;
  4714. test_sha[4] = e;
  4715. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4716. if (ret != 0)
  4717. return WC_TEST_RET_ENC_EC(ret);
  4718. for (i = 0; i < times; ++i) {
  4719. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4720. (word32)test_sha[i].inLen);
  4721. if (ret != 0)
  4722. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4723. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4724. if (ret != 0)
  4725. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4726. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4727. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4728. }
  4729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4730. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4731. DYNAMIC_TYPE_TMP_BUFFER);
  4732. if (large_input == NULL)
  4733. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4734. #endif
  4735. #ifndef NO_LARGE_HASH_TEST
  4736. /* BEGIN LARGE HASH TEST */ {
  4737. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4738. large_input[i] = (byte)(i & 0xFF);
  4739. }
  4740. times = 100;
  4741. for (i = 0; i < times; ++i) {
  4742. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4743. SHAKE128_LARGE_INPUT_BUFSIZ);
  4744. if (ret != 0)
  4745. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4746. }
  4747. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4748. if (ret != 0)
  4749. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4750. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4751. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4752. } /* END LARGE HASH TEST */
  4753. #endif /* NO_LARGE_HASH_TEST */
  4754. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4755. exit:
  4756. wc_Shake128_Free(&sha);
  4757. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4758. if (large_input != NULL)
  4759. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4760. #endif
  4761. return ret;
  4762. }
  4763. #endif
  4764. #ifdef WOLFSSL_SHAKE256
  4765. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4766. size_t large_input_buf_size)
  4767. {
  4768. testVector a, b, c, d, e;
  4769. testVector test_sha[5];
  4770. wc_test_ret_t ret = 0;
  4771. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4772. static const char large_digest[] =
  4773. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4774. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4775. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4776. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4777. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4778. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4779. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4780. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4781. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4782. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4783. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4784. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4785. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4786. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4787. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4788. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4789. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4790. byte hash[sizeof(large_digest) - 1];
  4791. /*
  4792. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4793. */
  4794. a.input = "";
  4795. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4796. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4797. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4798. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4799. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4800. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4801. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4802. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4803. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4804. "\xdd";
  4805. a.inLen = XSTRLEN(a.input);
  4806. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4807. b.input = "abc";
  4808. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4809. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4810. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4811. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4812. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4813. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4814. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4815. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4816. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4817. "\x29";
  4818. b.inLen = XSTRLEN(b.input);
  4819. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4820. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4821. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4822. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4823. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4824. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4825. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4826. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4827. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4828. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4829. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4830. "\x8d";
  4831. c.inLen = XSTRLEN(c.input);
  4832. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4833. /* Taken from NIST CAVP test vectors - full rate output. */
  4834. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4835. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4836. "\x85\xe0";
  4837. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4838. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4839. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4840. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4841. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4842. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4843. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4844. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4845. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4846. "\xc2";
  4847. d.inLen = 32;
  4848. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4849. /* Taken from NIST CAVP test vectors - more than one output block. */
  4850. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4851. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4852. "\x47\xe4";
  4853. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4854. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4855. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4856. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4857. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4858. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4859. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4860. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4861. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4862. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4863. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4864. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4865. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4866. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4867. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4868. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4869. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4870. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4871. "\x68\x44";
  4872. e.inLen = 32;
  4873. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4874. test_sha[0] = a;
  4875. test_sha[1] = b;
  4876. test_sha[2] = c;
  4877. test_sha[3] = d;
  4878. test_sha[4] = e;
  4879. for (i = 0; i < times; ++i) {
  4880. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4881. if (ret != 0)
  4882. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4883. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4884. (word32)test_sha[i].inLen);
  4885. if (ret != 0)
  4886. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4887. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4888. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4889. if (ret != 0)
  4890. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4891. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4892. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4893. }
  4894. #ifndef NO_LARGE_HASH_TEST
  4895. /* BEGIN LARGE HASH TEST */ {
  4896. for (i = 0; i < (int)large_input_buf_size; i++) {
  4897. large_input_buf[i] = (byte)(i & 0xFF);
  4898. }
  4899. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4900. if (ret != 0)
  4901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4902. /* Absorb is non-incremental. */
  4903. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4904. (word32)large_input_buf_size);
  4905. if (ret != 0)
  4906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4907. /* Able to squeeze out blocks incrementally. */
  4908. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4909. if (ret != 0)
  4910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4911. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4912. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4913. if (ret != 0)
  4914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4915. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4916. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4917. } /* END LARGE HASH TEST */
  4918. #endif /* NO_LARGE_HASH_TEST */
  4919. exit:
  4920. return ret;
  4921. }
  4922. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4923. {
  4924. wc_Shake sha;
  4925. byte hash[250];
  4926. testVector a, b, c, d, e;
  4927. testVector test_sha[5];
  4928. wc_test_ret_t ret = 0;
  4929. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4930. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4932. byte *large_input = NULL;
  4933. #else
  4934. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4935. #endif
  4936. const char* large_digest =
  4937. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4938. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4939. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4940. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4941. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4942. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4943. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4944. "\xea\x26";
  4945. WOLFSSL_ENTER("shake256_test");
  4946. /*
  4947. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4948. */
  4949. a.input = "";
  4950. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4951. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4952. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4953. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4954. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4955. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4956. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4957. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4958. a.inLen = XSTRLEN(a.input);
  4959. a.outLen = 114;
  4960. b.input = "abc";
  4961. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4962. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4963. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4964. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4965. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4966. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4967. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4968. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4969. b.inLen = XSTRLEN(b.input);
  4970. b.outLen = 114;
  4971. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4972. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4973. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4974. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4975. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4976. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4977. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4978. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4979. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4980. c.inLen = XSTRLEN(c.input);
  4981. c.outLen = 114;
  4982. /* Taken from NIST CAVP test vectors - full rate output. */
  4983. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4984. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4985. "\x85\xe0";
  4986. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4987. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4988. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4989. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4990. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4991. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4992. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4993. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4994. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4995. "\xc2";
  4996. d.inLen = 32;
  4997. d.outLen = 136;
  4998. /* Taken from NIST CAVP test vectors - more than one output block. */
  4999. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  5000. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  5001. "\x47\xe4";
  5002. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  5003. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  5004. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  5005. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  5006. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  5007. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  5008. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  5009. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  5010. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  5011. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  5012. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  5013. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  5014. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  5015. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  5016. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  5017. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  5018. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  5019. e.inLen = 32;
  5020. e.outLen = 250;
  5021. test_sha[0] = a;
  5022. test_sha[1] = b;
  5023. test_sha[2] = c;
  5024. test_sha[3] = d;
  5025. test_sha[4] = e;
  5026. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  5027. if (ret != 0)
  5028. return WC_TEST_RET_ENC_EC(ret);
  5029. for (i = 0; i < times; ++i) {
  5030. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  5031. (word32)test_sha[i].inLen);
  5032. if (ret != 0)
  5033. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5034. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  5035. if (ret != 0)
  5036. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5037. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  5038. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5039. }
  5040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5041. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  5042. DYNAMIC_TYPE_TMP_BUFFER);
  5043. if (large_input == NULL)
  5044. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  5045. #endif
  5046. #ifndef NO_LARGE_HASH_TEST
  5047. /* BEGIN LARGE HASH TEST */ {
  5048. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  5049. large_input[i] = (byte)(i & 0xFF);
  5050. }
  5051. times = 100;
  5052. for (i = 0; i < times; ++i) {
  5053. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  5054. SHAKE256_LARGE_INPUT_BUFSIZ);
  5055. if (ret != 0)
  5056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5057. }
  5058. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  5059. if (ret != 0)
  5060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5061. if (XMEMCMP(hash, large_digest, 114) != 0)
  5062. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  5063. } /* END LARGE HASH TEST */
  5064. #endif /* NO_LARGE_HASH_TEST */
  5065. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  5066. exit:
  5067. wc_Shake256_Free(&sha);
  5068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5069. if (large_input != NULL)
  5070. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5071. #endif
  5072. return ret;
  5073. }
  5074. #endif
  5075. #ifdef WOLFSSL_SM3
  5076. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  5077. {
  5078. wc_Sm3 sm3, sm3Copy;
  5079. byte hash[WC_SM3_DIGEST_SIZE];
  5080. byte hashGet[WC_SM3_DIGEST_SIZE];
  5081. byte hashCopy[WC_SM3_DIGEST_SIZE];
  5082. wc_test_ret_t ret = 0;
  5083. WOLFSSL_ENTER("sm3_test");
  5084. testVector a, b, c;
  5085. testVector test_sm3[3];
  5086. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  5087. a.input = "";
  5088. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  5089. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  5090. "\xaa\x2b";
  5091. a.inLen = XSTRLEN(a.input);
  5092. a.outLen = WC_SM3_DIGEST_SIZE;
  5093. b.input = "abc";
  5094. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  5095. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  5096. "\xa8\xe0";
  5097. b.inLen = XSTRLEN(b.input);
  5098. b.outLen = WC_SM3_DIGEST_SIZE;
  5099. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  5100. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  5101. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  5102. "\x8c\x05";
  5103. c.inLen = XSTRLEN(c.input);
  5104. c.outLen = WC_SM3_DIGEST_SIZE;
  5105. test_sm3[0] = a;
  5106. test_sm3[1] = b;
  5107. test_sm3[2] = c;
  5108. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  5109. if (ret != 0)
  5110. return WC_TEST_RET_ENC_EC(ret);
  5111. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  5112. if (ret != 0) {
  5113. wc_Sm3Free(&sm3);
  5114. return WC_TEST_RET_ENC_EC(ret);
  5115. }
  5116. /* Test all the KATs. */
  5117. for (i = 0; i < times; ++i) {
  5118. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  5119. (word32)test_sm3[i].inLen);
  5120. if (ret != 0) {
  5121. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5122. }
  5123. /* Get the final hash but leave ready for more updates. */
  5124. ret = wc_Sm3GetHash(&sm3, hashGet);
  5125. if (ret != 0)
  5126. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5127. /* Make a copy of the hash. */
  5128. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  5129. if (ret != 0)
  5130. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5131. /* Get the final hash with original. */
  5132. ret = wc_Sm3Final(&sm3, hash);
  5133. if (ret != 0)
  5134. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5135. /* Get the final hash with copy. */
  5136. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  5137. if (ret != 0)
  5138. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5139. /* Dispose of copy. */
  5140. wc_Sm3Free(&sm3Copy);
  5141. /* Check hashes match expected. */
  5142. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  5143. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5144. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  5145. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5146. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  5147. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5148. }
  5149. #ifndef NO_LARGE_HASH_TEST
  5150. {
  5151. word32 sz;
  5152. byte large_input[1024];
  5153. #ifdef HASH_SIZE_LIMIT
  5154. const char* large_digest =
  5155. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  5156. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  5157. times = 20;
  5158. #else
  5159. const char* large_digest =
  5160. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  5161. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  5162. times = 100;
  5163. #endif
  5164. /* Set large input to something. */
  5165. for (i = 0; i < (int)sizeof(large_input); i++) {
  5166. large_input[i] = (byte)(i & 0xFF);
  5167. }
  5168. /* Hash a large number of times. */
  5169. for (i = 0; i < times; ++i) {
  5170. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  5171. (word32)sizeof(large_input));
  5172. if (ret != 0)
  5173. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5174. }
  5175. /* Calculate hash and compare to expected. */
  5176. ret = wc_Sm3Final(&sm3, hash);
  5177. if (ret != 0)
  5178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5179. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5181. /* Check updating with various sizes works. */
  5182. for (sz = 1; sz <= 64; sz++) {
  5183. /* Hash a large number of times. */
  5184. for (i = 0; i < times; ++i) {
  5185. word32 o;
  5186. /* Update sz bytes at a time from large input buffer. */
  5187. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  5188. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  5189. if (ret != 0)
  5190. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  5191. }
  5192. /* Check for left-overs. */
  5193. if (o < (word32)sizeof(large_input)) {
  5194. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  5195. (word32)sizeof(large_input) - o);
  5196. if (ret != 0)
  5197. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5198. }
  5199. }
  5200. /* Calculate hash and compare to expected. */
  5201. ret = wc_Sm3Final(&sm3, hash);
  5202. if (ret != 0)
  5203. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5204. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5205. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5206. }
  5207. }
  5208. #endif /* NO_LARGE_HASH_TEST */
  5209. exit:
  5210. wc_Sm3Free(&sm3);
  5211. wc_Sm3Free(&sm3Copy);
  5212. return ret;
  5213. }
  5214. #endif
  5215. #ifndef NO_HASH_WRAPPER
  5216. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  5217. {
  5218. wc_HashAlg hash;
  5219. int ret, exp_ret;
  5220. int i, j;
  5221. int digestSz;
  5222. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  5223. byte out[WC_MAX_DIGEST_SIZE];
  5224. byte hashOut[WC_MAX_DIGEST_SIZE];
  5225. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5226. enum wc_HashType hashType;
  5227. #endif
  5228. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  5229. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  5230. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  5231. WC_HASH_TYPE_SHA3_224,
  5232. WC_HASH_TYPE_SHA3_256,
  5233. WC_HASH_TYPE_SHA3_384,
  5234. WC_HASH_TYPE_SHA3_512 };
  5235. enum wc_HashType typesNoImpl[] = {
  5236. #ifdef NO_MD5
  5237. WC_HASH_TYPE_MD5,
  5238. #endif
  5239. #ifdef NO_SHA
  5240. WC_HASH_TYPE_SHA,
  5241. #endif
  5242. #ifndef WOLFSSL_SHA224
  5243. WC_HASH_TYPE_SHA224,
  5244. #endif
  5245. #ifdef NO_SHA256
  5246. WC_HASH_TYPE_SHA256,
  5247. #endif
  5248. #ifndef WOLFSSL_SHA384
  5249. WC_HASH_TYPE_SHA384,
  5250. #endif
  5251. #ifndef WOLFSSL_SHA512
  5252. WC_HASH_TYPE_SHA512,
  5253. #endif
  5254. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  5255. WC_HASH_TYPE_SHA3_224,
  5256. #endif
  5257. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  5258. WC_HASH_TYPE_SHA3_256,
  5259. #endif
  5260. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  5261. WC_HASH_TYPE_SHA3_384,
  5262. #endif
  5263. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  5264. WC_HASH_TYPE_SHA3_512,
  5265. #endif
  5266. WC_HASH_TYPE_NONE
  5267. };
  5268. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  5269. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  5270. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  5271. WC_HASH_TYPE_BLAKE2B,
  5272. WC_HASH_TYPE_NONE };
  5273. WOLFSSL_ENTER("hash_test");
  5274. /* Parameter Validation testing. */
  5275. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  5276. if (ret != BAD_FUNC_ARG)
  5277. return WC_TEST_RET_ENC_EC(ret);
  5278. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5279. if (ret != BAD_FUNC_ARG)
  5280. return WC_TEST_RET_ENC_EC(ret);
  5281. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5282. if (ret != BAD_FUNC_ARG)
  5283. return WC_TEST_RET_ENC_EC(ret);
  5284. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  5285. if (ret != BAD_FUNC_ARG)
  5286. return WC_TEST_RET_ENC_EC(ret);
  5287. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  5288. if (ret != BAD_FUNC_ARG)
  5289. return WC_TEST_RET_ENC_EC(ret);
  5290. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  5291. if (ret != BAD_FUNC_ARG)
  5292. return WC_TEST_RET_ENC_EC(ret);
  5293. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  5294. if (ret != BAD_FUNC_ARG)
  5295. return WC_TEST_RET_ENC_EC(ret);
  5296. /* Try invalid hash algorithms. */
  5297. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  5298. ret = wc_HashInit(&hash, typesBad[i]);
  5299. if (ret != BAD_FUNC_ARG)
  5300. return WC_TEST_RET_ENC_I(i);
  5301. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  5302. if (ret != BAD_FUNC_ARG)
  5303. return WC_TEST_RET_ENC_I(i);
  5304. ret = wc_HashFinal(&hash, typesBad[i], out);
  5305. if (ret != BAD_FUNC_ARG)
  5306. return WC_TEST_RET_ENC_I(i);
  5307. wc_HashFree(&hash, typesBad[i]);
  5308. }
  5309. /* Try valid hash algorithms. */
  5310. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  5311. exp_ret = 0;
  5312. if (typesGood[i] == typesNoImpl[j]) {
  5313. /* Recognized but no implementation compiled in. */
  5314. exp_ret = HASH_TYPE_E;
  5315. j++;
  5316. }
  5317. ret = wc_HashInit(&hash, typesGood[i]);
  5318. if (ret != exp_ret)
  5319. return WC_TEST_RET_ENC_I(i);
  5320. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  5321. if (ret != exp_ret)
  5322. return WC_TEST_RET_ENC_I(i);
  5323. ret = wc_HashFinal(&hash, typesGood[i], out);
  5324. if (ret != exp_ret)
  5325. return WC_TEST_RET_ENC_I(i);
  5326. wc_HashFree(&hash, typesGood[i]);
  5327. digestSz = wc_HashGetDigestSize(typesGood[i]);
  5328. if (exp_ret < 0 && digestSz != exp_ret)
  5329. return WC_TEST_RET_ENC_I(i);
  5330. if (exp_ret == 0 && digestSz < 0)
  5331. return WC_TEST_RET_ENC_I(i);
  5332. if (exp_ret == 0) {
  5333. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  5334. digestSz - 1);
  5335. if (ret != BUFFER_E)
  5336. return WC_TEST_RET_ENC_I(i);
  5337. }
  5338. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, (word32)digestSz);
  5339. if (ret != exp_ret)
  5340. return WC_TEST_RET_ENC_I(i);
  5341. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  5342. return WC_TEST_RET_ENC_I(i);
  5343. ret = wc_HashGetBlockSize(typesGood[i]);
  5344. if (exp_ret < 0 && ret != exp_ret)
  5345. return WC_TEST_RET_ENC_I(i);
  5346. if (exp_ret == 0 && ret < 0)
  5347. return WC_TEST_RET_ENC_I(i);
  5348. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5349. ret = wc_HashGetOID(typesGood[i]);
  5350. if (ret == BAD_FUNC_ARG ||
  5351. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  5352. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  5353. return WC_TEST_RET_ENC_I(i);
  5354. }
  5355. hashType = wc_OidGetHash(ret);
  5356. if (exp_ret == 0 && hashType != typesGood[i])
  5357. return WC_TEST_RET_ENC_I(i);
  5358. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5359. }
  5360. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  5361. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  5362. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  5363. return WC_TEST_RET_ENC_I(i);
  5364. }
  5365. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5366. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  5367. #ifdef WOLFSSL_MD2
  5368. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5369. return WC_TEST_RET_ENC_EC(ret);
  5370. #else
  5371. if (ret != HASH_TYPE_E)
  5372. return WC_TEST_RET_ENC_EC(ret);
  5373. #endif
  5374. hashType = wc_OidGetHash(646); /* Md2h */
  5375. #ifdef WOLFSSL_MD2
  5376. if (hashType != WC_HASH_TYPE_MD2)
  5377. return WC_TEST_RET_ENC_NC;
  5378. #else
  5379. if (hashType != WC_HASH_TYPE_NONE)
  5380. return WC_TEST_RET_ENC_NC;
  5381. #endif
  5382. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  5383. #ifndef NO_MD5
  5384. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5385. return WC_TEST_RET_ENC_EC(ret);
  5386. #else
  5387. if (ret != HASH_TYPE_E)
  5388. return WC_TEST_RET_ENC_EC(ret);
  5389. #endif
  5390. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  5391. if (ret != BAD_FUNC_ARG)
  5392. return WC_TEST_RET_ENC_EC(ret);
  5393. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  5394. if (ret != BAD_FUNC_ARG)
  5395. return WC_TEST_RET_ENC_EC(ret);
  5396. hashType = wc_OidGetHash(0);
  5397. if (hashType != WC_HASH_TYPE_NONE)
  5398. return WC_TEST_RET_ENC_NC;
  5399. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5400. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  5401. #ifdef WOLFSSL_MD2
  5402. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5403. return WC_TEST_RET_ENC_EC(ret);
  5404. #else
  5405. if (ret != HASH_TYPE_E)
  5406. return WC_TEST_RET_ENC_EC(ret);
  5407. #endif
  5408. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  5409. #ifdef WOLFSSL_MD2
  5410. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5411. return WC_TEST_RET_ENC_EC(ret);
  5412. #else
  5413. if (ret != HASH_TYPE_E)
  5414. return WC_TEST_RET_ENC_EC(ret);
  5415. #endif
  5416. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  5417. #ifndef NO_MD4
  5418. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5419. return WC_TEST_RET_ENC_EC(ret);
  5420. #else
  5421. if (ret != HASH_TYPE_E)
  5422. return WC_TEST_RET_ENC_EC(ret);
  5423. #endif
  5424. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  5425. #ifndef NO_MD4
  5426. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5427. return WC_TEST_RET_ENC_EC(ret);
  5428. #else
  5429. if (ret != HASH_TYPE_E)
  5430. return WC_TEST_RET_ENC_EC(ret);
  5431. #endif
  5432. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  5433. #if !defined(NO_MD5) && !defined(NO_SHA)
  5434. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5435. return WC_TEST_RET_ENC_EC(ret);
  5436. #else
  5437. if (ret != HASH_TYPE_E)
  5438. return WC_TEST_RET_ENC_EC(ret);
  5439. #endif
  5440. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  5441. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5442. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5443. return WC_TEST_RET_ENC_EC(ret);
  5444. #else
  5445. if (ret != HASH_TYPE_E)
  5446. return WC_TEST_RET_ENC_EC(ret);
  5447. #endif
  5448. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  5449. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5450. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5451. return WC_TEST_RET_ENC_EC(ret);
  5452. #else
  5453. if (ret != HASH_TYPE_E)
  5454. return WC_TEST_RET_ENC_EC(ret);
  5455. #endif
  5456. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  5457. if (ret != BAD_FUNC_ARG)
  5458. return WC_TEST_RET_ENC_EC(ret);
  5459. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  5460. if (ret != BAD_FUNC_ARG)
  5461. return WC_TEST_RET_ENC_EC(ret);
  5462. #if !defined(NO_CERTS) && !defined(NO_ASN)
  5463. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5464. ret = wc_GetCTC_HashOID(MD2);
  5465. if (ret == 0)
  5466. return WC_TEST_RET_ENC_EC(ret);
  5467. #endif
  5468. #ifndef NO_MD5
  5469. ret = wc_GetCTC_HashOID(WC_MD5);
  5470. if (ret == 0)
  5471. return WC_TEST_RET_ENC_EC(ret);
  5472. #endif
  5473. #ifndef NO_SHA
  5474. ret = wc_GetCTC_HashOID(WC_SHA);
  5475. if (ret == 0)
  5476. return WC_TEST_RET_ENC_EC(ret);
  5477. #endif
  5478. #ifdef WOLFSSL_SHA224
  5479. ret = wc_GetCTC_HashOID(WC_SHA224);
  5480. if (ret == 0)
  5481. return WC_TEST_RET_ENC_EC(ret);
  5482. #endif
  5483. #ifndef NO_SHA256
  5484. ret = wc_GetCTC_HashOID(WC_SHA256);
  5485. if (ret == 0)
  5486. return WC_TEST_RET_ENC_EC(ret);
  5487. #endif
  5488. #ifdef WOLFSSL_SHA384
  5489. ret = wc_GetCTC_HashOID(WC_SHA384);
  5490. if (ret == 0)
  5491. return WC_TEST_RET_ENC_EC(ret);
  5492. #endif
  5493. #ifdef WOLFSSL_SHA512
  5494. ret = wc_GetCTC_HashOID(WC_SHA512);
  5495. if (ret == 0)
  5496. return WC_TEST_RET_ENC_EC(ret);
  5497. #endif
  5498. ret = wc_GetCTC_HashOID(-1);
  5499. if (ret != 0)
  5500. return WC_TEST_RET_ENC_EC(ret);
  5501. #endif
  5502. return 0;
  5503. }
  5504. #endif /* !NO_HASH_WRAPPER */
  5505. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  5506. defined(HAVE_FIPS_VERSION) && \
  5507. (HAVE_FIPS_VERSION >= 5))
  5508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  5509. {
  5510. Hmac hmac;
  5511. byte hash[WC_MD5_DIGEST_SIZE];
  5512. const char* keys[]=
  5513. {
  5514. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5515. "Jefe",
  5516. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  5517. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5518. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5519. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5520. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5521. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5522. };
  5523. testVector a, b, c, d;
  5524. testVector test_hmac[4];
  5525. wc_test_ret_t ret;
  5526. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5527. WOLFSSL_ENTER("hmac_md5_test");
  5528. /* Following test vectors are from RFC 2202 section 2 */
  5529. a.input = "Hi There";
  5530. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5531. "\x9d";
  5532. a.inLen = XSTRLEN(a.input);
  5533. a.outLen = WC_MD5_DIGEST_SIZE;
  5534. b.input = "what do ya want for nothing?";
  5535. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  5536. "\x38";
  5537. b.inLen = XSTRLEN(b.input);
  5538. b.outLen = WC_MD5_DIGEST_SIZE;
  5539. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5540. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5541. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5542. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5543. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  5544. "\xf6";
  5545. c.inLen = XSTRLEN(c.input);
  5546. c.outLen = WC_MD5_DIGEST_SIZE;
  5547. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5548. d.output = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0"
  5549. "\xcd";
  5550. d.inLen = XSTRLEN(d.input);
  5551. d.outLen = WC_MD5_DIGEST_SIZE;
  5552. test_hmac[0] = a;
  5553. test_hmac[1] = b;
  5554. test_hmac[2] = c;
  5555. test_hmac[3] = d;
  5556. for (i = 0; i < times; ++i) {
  5557. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5558. if (i == 1) {
  5559. continue; /* cavium can't handle short keys, fips not allowed */
  5560. }
  5561. #endif
  5562. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  5563. if (ret != 0)
  5564. return WC_TEST_RET_ENC_EC(ret);
  5565. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  5566. (word32)XSTRLEN(keys[i]));
  5567. if (ret != 0)
  5568. return WC_TEST_RET_ENC_EC(ret);
  5569. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5570. (word32)test_hmac[i].inLen);
  5571. if (ret != 0)
  5572. return WC_TEST_RET_ENC_EC(ret);
  5573. ret = wc_HmacFinal(&hmac, hash);
  5574. if (ret != 0)
  5575. return WC_TEST_RET_ENC_EC(ret);
  5576. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  5577. return WC_TEST_RET_ENC_I(i);
  5578. wc_HmacFree(&hmac);
  5579. }
  5580. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5581. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  5582. return WC_TEST_RET_ENC_EC(ret);
  5583. #endif
  5584. return 0;
  5585. }
  5586. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  5587. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5588. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  5589. {
  5590. Hmac hmac;
  5591. byte hash[WC_SHA_DIGEST_SIZE];
  5592. const char* keys[]=
  5593. {
  5594. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5595. "\x0b\x0b\x0b",
  5596. "Jefe",
  5597. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5598. "\xAA\xAA\xAA",
  5599. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5600. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5601. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5602. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5603. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5604. };
  5605. testVector a, b, c, d;
  5606. testVector test_hmac[4];
  5607. wc_test_ret_t ret;
  5608. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5609. WOLFSSL_ENTER("hmac_sha_test");
  5610. /* Following test vectors are from RFC 2202 section 3 */
  5611. a.input = "Hi There";
  5612. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5613. "\x8e\xf1\x46\xbe\x00";
  5614. a.inLen = XSTRLEN(a.input);
  5615. a.outLen = WC_SHA_DIGEST_SIZE;
  5616. b.input = "what do ya want for nothing?";
  5617. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  5618. "\x9c\x25\x9a\x7c\x79";
  5619. b.inLen = XSTRLEN(b.input);
  5620. b.outLen = WC_SHA_DIGEST_SIZE;
  5621. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5622. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5623. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5624. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5625. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  5626. "\x4f\x63\xf1\x75\xd3";
  5627. c.inLen = XSTRLEN(c.input);
  5628. c.outLen = WC_SHA_DIGEST_SIZE;
  5629. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5630. d.output = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b"
  5631. "\x55\xed\x40\x21\x12";
  5632. d.inLen = XSTRLEN(d.input);
  5633. d.outLen = WC_SHA_DIGEST_SIZE;
  5634. test_hmac[0] = a;
  5635. test_hmac[1] = b;
  5636. test_hmac[2] = c;
  5637. test_hmac[3] = d;
  5638. #if FIPS_VERSION3_GE(6,0,0)
  5639. int allowShortKeyWithFips = 1;
  5640. #endif
  5641. for (i = 0; i < times; ++i) {
  5642. #if defined(HAVE_CAVIUM) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0))
  5643. if (i == 1)
  5644. continue; /* cavium can't handle short keys, fips not allowed */
  5645. #endif
  5646. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5647. return WC_TEST_RET_ENC_EC(ret);
  5648. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  5649. (word32)XSTRLEN(keys[i]));
  5650. #if FIPS_VERSION3_GE(6,0,0)
  5651. if (i == 1) {
  5652. if (ret != HMAC_MIN_KEYLEN_E)
  5653. return WC_TEST_RET_ENC_EC(ret);
  5654. /* Now use the ex and allow short keys with FIPS option */
  5655. ret = wc_HmacSetKey_ex(&hmac, WC_SHA, (byte*) keys[i],
  5656. (word32)XSTRLEN(keys[i]), allowShortKeyWithFips);
  5657. }
  5658. #endif
  5659. if (ret != 0)
  5660. return WC_TEST_RET_ENC_EC(ret);
  5661. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5662. (word32)test_hmac[i].inLen);
  5663. if (ret != 0)
  5664. return WC_TEST_RET_ENC_EC(ret);
  5665. ret = wc_HmacFinal(&hmac, hash);
  5666. if (ret != 0)
  5667. return WC_TEST_RET_ENC_EC(ret);
  5668. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  5669. return WC_TEST_RET_ENC_I(i);
  5670. wc_HmacFree(&hmac);
  5671. }
  5672. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5673. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  5674. return WC_TEST_RET_ENC_EC(ret);
  5675. #endif
  5676. return 0;
  5677. }
  5678. #endif
  5679. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5680. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  5681. {
  5682. Hmac hmac;
  5683. byte hash[WC_SHA224_DIGEST_SIZE];
  5684. const char* keys[]=
  5685. {
  5686. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5687. "\x0b\x0b\x0b",
  5688. "Jefe",
  5689. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5690. "\xAA\xAA\xAA",
  5691. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5692. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5693. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5694. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5695. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5696. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5697. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5698. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5699. "\xAA\xAA\xAA"
  5700. };
  5701. testVector a, b, c, d;
  5702. testVector test_hmac[4];
  5703. wc_test_ret_t ret;
  5704. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5705. WOLFSSL_ENTER("hmac_sha224_test");
  5706. /* Following test vectors are from RFC 4231 section 4 */
  5707. a.input = "Hi There";
  5708. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5709. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5710. a.inLen = XSTRLEN(a.input);
  5711. a.outLen = WC_SHA224_DIGEST_SIZE;
  5712. b.input = "what do ya want for nothing?";
  5713. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  5714. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  5715. b.inLen = XSTRLEN(b.input);
  5716. b.outLen = WC_SHA224_DIGEST_SIZE;
  5717. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5718. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5719. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5720. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5721. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  5722. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  5723. c.inLen = XSTRLEN(c.input);
  5724. c.outLen = WC_SHA224_DIGEST_SIZE;
  5725. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5726. d.output = "\x95\xe9\xa0\xdb\x96\x20\x95\xad\xae\xbe\x9b\x2d\x6f\x0d\xbc\xe2\xd4\x99\xf1\x12\xf2\xd2\xb7\x27\x3f\xa6\x87\x0e";
  5727. d.inLen = XSTRLEN(d.input);
  5728. d.outLen = WC_SHA224_DIGEST_SIZE;
  5729. test_hmac[0] = a;
  5730. test_hmac[1] = b;
  5731. test_hmac[2] = c;
  5732. test_hmac[3] = d;
  5733. for (i = 0; i < times; ++i) {
  5734. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5735. if (i == 1)
  5736. continue; /* cavium can't handle short keys, fips not allowed */
  5737. #endif
  5738. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5739. return WC_TEST_RET_ENC_EC(ret);
  5740. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5741. (word32)XSTRLEN(keys[i]));
  5742. if (ret != 0)
  5743. return WC_TEST_RET_ENC_EC(ret);
  5744. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5745. (word32)test_hmac[i].inLen);
  5746. if (ret != 0)
  5747. return WC_TEST_RET_ENC_EC(ret);
  5748. ret = wc_HmacFinal(&hmac, hash);
  5749. if (ret != 0)
  5750. return WC_TEST_RET_ENC_EC(ret);
  5751. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5752. return WC_TEST_RET_ENC_I(i);
  5753. wc_HmacFree(&hmac);
  5754. }
  5755. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5756. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5757. return WC_TEST_RET_ENC_EC(ret);
  5758. #endif
  5759. return 0;
  5760. }
  5761. #endif
  5762. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5763. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5764. {
  5765. Hmac hmac;
  5766. byte hash[WC_SHA256_DIGEST_SIZE];
  5767. const char* keys[]=
  5768. {
  5769. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5770. "\x0b\x0b\x0b",
  5771. "Jefe",
  5772. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5773. "\xAA\xAA\xAA",
  5774. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5775. "\xAA\xAA\xAA",
  5776. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5777. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5778. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5779. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5780. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5781. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5782. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5783. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5784. "\xAA\xAA\xAA"
  5785. };
  5786. testVector a, b, c, d, e;
  5787. testVector test_hmac[5];
  5788. wc_test_ret_t ret;
  5789. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5790. WOLFSSL_ENTER("hmac_sha256_test");
  5791. /* Following test vectors are from RFC 4231 section 4 */
  5792. a.input = "Hi There";
  5793. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5794. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5795. "\xcf\xf7";
  5796. a.inLen = XSTRLEN(a.input);
  5797. a.outLen = WC_SHA256_DIGEST_SIZE;
  5798. b.input = "what do ya want for nothing?";
  5799. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5800. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5801. "\x38\x43";
  5802. b.inLen = XSTRLEN(b.input);
  5803. b.outLen = WC_SHA256_DIGEST_SIZE;
  5804. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5805. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5806. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5807. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5808. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5809. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5810. "\x65\xfe";
  5811. c.inLen = XSTRLEN(c.input);
  5812. c.outLen = WC_SHA256_DIGEST_SIZE;
  5813. d.input = 0;
  5814. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5815. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5816. "\x3e\x46";
  5817. d.inLen = 0;
  5818. d.outLen = WC_SHA256_DIGEST_SIZE;
  5819. e.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5820. e.output = "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7"
  5821. "\x7f\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3"
  5822. "\x7f\x54";
  5823. e.inLen = XSTRLEN(e.input);;
  5824. e.outLen = WC_SHA256_DIGEST_SIZE;
  5825. test_hmac[0] = a;
  5826. test_hmac[1] = b;
  5827. test_hmac[2] = c;
  5828. test_hmac[3] = d;
  5829. test_hmac[4] = e;
  5830. for (i = 0; i < times; ++i) {
  5831. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5832. if (i == 1)
  5833. continue; /* cavium can't handle short keys, fips not allowed */
  5834. #endif
  5835. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5836. if (i == 3)
  5837. continue; /* QuickAssist can't handle empty HMAC */
  5838. #endif
  5839. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5840. return WC_TEST_RET_ENC_I(i);
  5841. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5842. (word32)XSTRLEN(keys[i]));
  5843. if (ret != 0)
  5844. return WC_TEST_RET_ENC_I(i);
  5845. if (test_hmac[i].input != NULL) {
  5846. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5847. (word32)test_hmac[i].inLen);
  5848. if (ret != 0)
  5849. return WC_TEST_RET_ENC_I(i);
  5850. }
  5851. ret = wc_HmacFinal(&hmac, hash);
  5852. if (ret != 0)
  5853. return WC_TEST_RET_ENC_I(i);
  5854. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5855. return WC_TEST_RET_ENC_I(i);
  5856. wc_HmacFree(&hmac);
  5857. }
  5858. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5859. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5860. return WC_TEST_RET_ENC_EC(ret);
  5861. #if FIPS_VERSION3_GE(6,0,0)
  5862. if ((ret = wc_HmacSizeByType(21)) != HMAC_KAT_FIPS_E)
  5863. #else
  5864. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5865. #endif
  5866. {
  5867. return WC_TEST_RET_ENC_EC(ret);
  5868. }
  5869. #endif
  5870. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5871. return WC_TEST_RET_ENC_EC(ret);
  5872. return 0;
  5873. }
  5874. #endif
  5875. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5876. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5877. {
  5878. Hmac hmac;
  5879. byte hash[WC_SHA384_DIGEST_SIZE];
  5880. const char* keys[]=
  5881. {
  5882. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5883. "\x0b\x0b\x0b",
  5884. "Jefe",
  5885. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5886. "\xAA\xAA\xAA",
  5887. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5888. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5889. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5890. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5891. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5892. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5893. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5894. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5895. "\xAA\xAA\xAA"
  5896. };
  5897. testVector a, b, c, d;
  5898. testVector test_hmac[4];
  5899. wc_test_ret_t ret;
  5900. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5901. WOLFSSL_ENTER("hmac_sha384_test");
  5902. /* Following test vectors are from RFC 4231 section 4 */
  5903. a.input = "Hi There";
  5904. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5905. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5906. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5907. "\xfa\x9c\xb6";
  5908. a.inLen = XSTRLEN(a.input);
  5909. a.outLen = WC_SHA384_DIGEST_SIZE;
  5910. b.input = "what do ya want for nothing?";
  5911. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5912. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5913. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5914. "\xb2\x16\x49";
  5915. b.inLen = XSTRLEN(b.input);
  5916. b.outLen = WC_SHA384_DIGEST_SIZE;
  5917. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5918. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5919. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5920. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5921. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5922. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5923. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5924. "\xa3\x4f\x27";
  5925. c.inLen = XSTRLEN(c.input);
  5926. c.outLen = WC_SHA384_DIGEST_SIZE;
  5927. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5928. d.output = "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5"
  5929. "\xb4\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a"
  5930. "\xc4\xc6\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63"
  5931. "\xf4\x49\x52";
  5932. d.inLen = XSTRLEN(d.input);
  5933. d.outLen = WC_SHA384_DIGEST_SIZE;
  5934. test_hmac[0] = a;
  5935. test_hmac[1] = b;
  5936. test_hmac[2] = c;
  5937. test_hmac[3] = d;
  5938. for (i = 0; i < times; ++i) {
  5939. #if defined(HAVE_FIPS)
  5940. if (i == 1)
  5941. continue; /* fips not allowed */
  5942. #endif
  5943. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5944. return WC_TEST_RET_ENC_EC(ret);
  5945. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5946. (word32)XSTRLEN(keys[i]));
  5947. if (ret != 0)
  5948. return WC_TEST_RET_ENC_EC(ret);
  5949. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5950. (word32)test_hmac[i].inLen);
  5951. if (ret != 0)
  5952. return WC_TEST_RET_ENC_EC(ret);
  5953. ret = wc_HmacFinal(&hmac, hash);
  5954. if (ret != 0)
  5955. return WC_TEST_RET_ENC_EC(ret);
  5956. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5957. return WC_TEST_RET_ENC_I(i);
  5958. wc_HmacFree(&hmac);
  5959. }
  5960. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5961. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5962. return WC_TEST_RET_ENC_EC(ret);
  5963. #endif
  5964. return 0;
  5965. }
  5966. #endif
  5967. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5968. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5969. {
  5970. Hmac hmac;
  5971. byte hash[WC_SHA512_DIGEST_SIZE];
  5972. const char* keys[]=
  5973. {
  5974. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5975. "\x0b\x0b\x0b",
  5976. "Jefe",
  5977. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5978. "\xAA\xAA\xAA",
  5979. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5980. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5981. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5982. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5983. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5984. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5985. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5986. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5987. "\xAA\xAA\xAA"
  5988. };
  5989. testVector a, b, c, d;
  5990. testVector test_hmac[4];
  5991. wc_test_ret_t ret;
  5992. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5993. WOLFSSL_ENTER("hmac_sha512_test");
  5994. /* Following test vectors are from RFC 4231 section 4 */
  5995. a.input = "Hi There";
  5996. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5997. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5998. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5999. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  6000. "\x3a\x12\x68\x54";
  6001. a.inLen = XSTRLEN(a.input);
  6002. a.outLen = WC_SHA512_DIGEST_SIZE;
  6003. b.input = "what do ya want for nothing?";
  6004. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  6005. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  6006. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  6007. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  6008. "\x38\xbc\xe7\x37";
  6009. b.inLen = XSTRLEN(b.input);
  6010. b.outLen = WC_SHA512_DIGEST_SIZE;
  6011. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6012. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6013. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6014. "\xDD\xDD\xDD\xDD\xDD\xDD";
  6015. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  6016. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  6017. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  6018. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  6019. "\xe1\x32\x92\xfb";
  6020. c.inLen = XSTRLEN(c.input);
  6021. c.outLen = WC_SHA512_DIGEST_SIZE;
  6022. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  6023. d.output = "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8"
  6024. "\xb4\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8"
  6025. "\xf3\x52\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d"
  6026. "\x6a\x1e\x52\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98"
  6027. "\x5d\x78\x65\x98";
  6028. d.inLen = XSTRLEN(d.input);
  6029. d.outLen = WC_SHA512_DIGEST_SIZE;
  6030. test_hmac[0] = a;
  6031. test_hmac[1] = b;
  6032. test_hmac[2] = c;
  6033. test_hmac[3] = d;
  6034. for (i = 0; i < times; ++i) {
  6035. #if defined(HAVE_FIPS)
  6036. if (i == 1)
  6037. continue; /* fips not allowed */
  6038. #endif
  6039. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6040. return WC_TEST_RET_ENC_EC(ret);
  6041. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  6042. (word32)XSTRLEN(keys[i]));
  6043. if (ret != 0)
  6044. return WC_TEST_RET_ENC_EC(ret);
  6045. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  6046. (word32)test_hmac[i].inLen);
  6047. if (ret != 0)
  6048. return WC_TEST_RET_ENC_EC(ret);
  6049. ret = wc_HmacFinal(&hmac, hash);
  6050. if (ret != 0)
  6051. return WC_TEST_RET_ENC_EC(ret);
  6052. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  6053. return WC_TEST_RET_ENC_I(i);
  6054. wc_HmacFree(&hmac);
  6055. }
  6056. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6057. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  6058. return WC_TEST_RET_ENC_EC(ret);
  6059. #endif
  6060. return 0;
  6061. }
  6062. #endif
  6063. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  6064. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  6065. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  6066. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  6067. {
  6068. Hmac hmac;
  6069. byte hash[WC_SHA3_512_DIGEST_SIZE];
  6070. const char* key[4] =
  6071. {
  6072. "Jefe",
  6073. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  6074. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  6075. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  6076. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  6077. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6078. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6079. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6080. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6081. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6082. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6083. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6084. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6085. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6086. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6087. };
  6088. const char* input[4] =
  6089. {
  6090. "what do ya want for nothing?",
  6091. "Hi There",
  6092. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6093. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6094. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6095. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6096. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  6097. "Big Key Input"
  6098. };
  6099. const int hashType[4] =
  6100. {
  6101. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  6102. };
  6103. const int hashSz[4] =
  6104. {
  6105. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  6106. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  6107. };
  6108. const char* output[16] =
  6109. {
  6110. /* key = jefe, input = what do ya want for nothing? */
  6111. /* HMAC-SHA3-224 */
  6112. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  6113. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  6114. /* HMAC-SHA3-256 */
  6115. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  6116. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  6117. /* HMAC-SHA3-384 */
  6118. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  6119. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  6120. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  6121. /* HMAC-SHA3-512 */
  6122. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  6123. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  6124. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  6125. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  6126. /* key = 0b..., input = Hi There */
  6127. /* HMAC-SHA3-224 */
  6128. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  6129. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  6130. /* HMAC-SHA3-256 */
  6131. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  6132. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  6133. /* HMAC-SHA3-384 */
  6134. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  6135. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  6136. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  6137. /* HMAC-SHA3-512 */
  6138. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  6139. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  6140. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  6141. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  6142. /* key = aa..., output = dd... */
  6143. /* HMAC-SHA3-224 */
  6144. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  6145. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  6146. /* HMAC-SHA3-256 */
  6147. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  6148. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  6149. /* HMAC-SHA3-384 */
  6150. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  6151. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  6152. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  6153. /* HMAC-SHA3-512 */
  6154. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  6155. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  6156. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  6157. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  6158. /* key = big key, input = Big Key Input */
  6159. /* HMAC-SHA3-224 */
  6160. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  6161. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  6162. /* HMAC-SHA3-256 */
  6163. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  6164. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  6165. /* HMAC-SHA3-384 */
  6166. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  6167. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  6168. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  6169. /* HMAC-SHA3-512 */
  6170. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  6171. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  6172. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  6173. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  6174. };
  6175. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  6176. j, jMax = sizeof(hashType) / sizeof(hashType[0]);
  6177. int ret;
  6178. WOLFSSL_ENTER("hmac_sha3_test");
  6179. #ifdef HAVE_FIPS
  6180. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  6181. * short. Skip it in FIPS builds. */
  6182. i = 1;
  6183. #endif
  6184. for (; i < iMax; i++) {
  6185. for (j = 0; j < jMax; j++) {
  6186. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6187. return WC_TEST_RET_ENC_EC(ret);
  6188. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  6189. (word32)XSTRLEN(key[i]));
  6190. if (ret != 0)
  6191. return WC_TEST_RET_ENC_EC(ret);
  6192. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  6193. (word32)XSTRLEN(input[i]));
  6194. if (ret != 0)
  6195. return WC_TEST_RET_ENC_EC(ret);
  6196. ret = wc_HmacFinal(&hmac, hash);
  6197. if (ret != 0)
  6198. return WC_TEST_RET_ENC_EC(ret);
  6199. if (XMEMCMP(hash, output[(i*jMax) + j], (size_t)hashSz[j]) != 0)
  6200. return WC_TEST_RET_ENC_NC;
  6201. wc_HmacFree(&hmac);
  6202. if (i > 0)
  6203. continue;
  6204. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6205. ret = wc_HmacSizeByType(hashType[j]);
  6206. if (ret != hashSz[j])
  6207. return WC_TEST_RET_ENC_EC(ret);
  6208. #endif
  6209. }
  6210. }
  6211. return 0;
  6212. }
  6213. #endif
  6214. #ifdef WC_RC2
  6215. typedef struct rc2TestVector {
  6216. const char* input;
  6217. const char* output;
  6218. const char* key; /* Key, variable up to 128 bytes */
  6219. const char* iv; /* IV, 8-bytes */
  6220. int inLen;
  6221. int outLen;
  6222. int keyLen;
  6223. int effectiveKeyBits; /* Up to 1024 bits supported */
  6224. } rc2TestVector;
  6225. static wc_test_ret_t rc2_ecb_test(void)
  6226. {
  6227. wc_test_ret_t ret = 0;
  6228. byte cipher[RC2_BLOCK_SIZE];
  6229. byte plain[RC2_BLOCK_SIZE];
  6230. rc2TestVector a, b, c, d, e, f, g, h;
  6231. rc2TestVector test_rc2[8];
  6232. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  6233. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6234. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  6235. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6236. a.inLen = RC2_BLOCK_SIZE;
  6237. a.outLen = RC2_BLOCK_SIZE;
  6238. a.keyLen = 8;
  6239. a.effectiveKeyBits = 63;
  6240. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6241. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  6242. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6243. b.inLen = RC2_BLOCK_SIZE;
  6244. b.outLen = RC2_BLOCK_SIZE;
  6245. b.keyLen = 8;
  6246. b.effectiveKeyBits = 64;
  6247. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  6248. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  6249. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6250. c.inLen = RC2_BLOCK_SIZE;
  6251. c.outLen = RC2_BLOCK_SIZE;
  6252. c.keyLen = 8;
  6253. c.effectiveKeyBits = 64;
  6254. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6255. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  6256. d.key = "\x88";
  6257. d.inLen = RC2_BLOCK_SIZE;
  6258. d.outLen = RC2_BLOCK_SIZE;
  6259. d.keyLen = 1;
  6260. d.effectiveKeyBits = 64;
  6261. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6262. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  6263. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6264. e.inLen = RC2_BLOCK_SIZE;
  6265. e.outLen = RC2_BLOCK_SIZE;
  6266. e.keyLen = 7;
  6267. e.effectiveKeyBits = 64;
  6268. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6269. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  6270. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6271. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6272. f.inLen = RC2_BLOCK_SIZE;
  6273. f.outLen = RC2_BLOCK_SIZE;
  6274. f.keyLen = 16;
  6275. f.effectiveKeyBits = 64;
  6276. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6277. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  6278. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6279. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6280. g.inLen = RC2_BLOCK_SIZE;
  6281. g.outLen = RC2_BLOCK_SIZE;
  6282. g.keyLen = 16;
  6283. g.effectiveKeyBits = 128;
  6284. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6285. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  6286. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6287. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6288. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6289. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6290. "\x1e";
  6291. h.inLen = RC2_BLOCK_SIZE;
  6292. h.outLen = RC2_BLOCK_SIZE;
  6293. h.keyLen = 33;
  6294. h.effectiveKeyBits = 129;
  6295. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  6296. test_rc2[0] = a;
  6297. test_rc2[1] = b;
  6298. test_rc2[2] = c;
  6299. test_rc2[3] = d;
  6300. test_rc2[4] = e;
  6301. test_rc2[5] = f;
  6302. test_rc2[6] = g;
  6303. test_rc2[7] = h;
  6304. for (i = 0; i < times; ++i) {
  6305. Rc2 enc;
  6306. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  6307. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  6308. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  6309. NULL, test_rc2[i].effectiveKeyBits);
  6310. if (ret != 0) {
  6311. return WC_TEST_RET_ENC_EC(ret);
  6312. }
  6313. /* ECB encrypt */
  6314. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  6315. (word32)test_rc2[i].outLen);
  6316. if (ret != 0) {
  6317. return WC_TEST_RET_ENC_EC(ret);
  6318. }
  6319. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  6320. return WC_TEST_RET_ENC_NC;
  6321. }
  6322. /* ECB decrypt */
  6323. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  6324. if (ret != 0) {
  6325. return WC_TEST_RET_ENC_EC(ret);
  6326. }
  6327. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  6328. return WC_TEST_RET_ENC_NC;
  6329. }
  6330. }
  6331. return 0;
  6332. }
  6333. static wc_test_ret_t rc2_cbc_test(void)
  6334. {
  6335. wc_test_ret_t ret = 0;
  6336. byte cipher[128];
  6337. byte plain[128];
  6338. rc2TestVector a, b, c, d, e, f, g, h, i;
  6339. rc2TestVector test_rc2[9];
  6340. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  6341. /* key length = 7, effective key bits = 63 */
  6342. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6343. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6344. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  6345. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  6346. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6347. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6348. a.inLen = RC2_BLOCK_SIZE*2;
  6349. a.outLen = RC2_BLOCK_SIZE*2;
  6350. a.keyLen = 8;
  6351. a.effectiveKeyBits = 63;
  6352. /* key length = 8, effective key bits = 64, all 0xFF */
  6353. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  6354. "\xff\xff\xff\xff\xff\xff\xff\xff";
  6355. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  6356. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  6357. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6358. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6359. b.inLen = RC2_BLOCK_SIZE*2;
  6360. b.outLen = RC2_BLOCK_SIZE*2;
  6361. b.keyLen = 8;
  6362. b.effectiveKeyBits = 64;
  6363. /* key length = 8, effective key bits = 64 */
  6364. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  6365. "\x10\x00\x00\x00\x00\x00\x00\x01";
  6366. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  6367. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  6368. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6369. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6370. c.inLen = RC2_BLOCK_SIZE*2;
  6371. c.outLen = RC2_BLOCK_SIZE*2;
  6372. c.keyLen = 8;
  6373. c.effectiveKeyBits = 64;
  6374. /* key length = 1, effective key bits = 64 */
  6375. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6376. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6377. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  6378. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  6379. d.key = "\x88";
  6380. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6381. d.inLen = RC2_BLOCK_SIZE*2;
  6382. d.outLen = RC2_BLOCK_SIZE*2;
  6383. d.keyLen = 1;
  6384. d.effectiveKeyBits = 64;
  6385. /* key length = 7, effective key bits = 64 */
  6386. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6387. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6388. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  6389. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  6390. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6391. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6392. e.inLen = RC2_BLOCK_SIZE*2;
  6393. e.outLen = RC2_BLOCK_SIZE*2;
  6394. e.keyLen = 7;
  6395. e.effectiveKeyBits = 64;
  6396. /* key length = 16, effective key bits = 64 */
  6397. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6398. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6399. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  6400. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  6401. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6402. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6403. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6404. f.inLen = RC2_BLOCK_SIZE*2;
  6405. f.outLen = RC2_BLOCK_SIZE*2;
  6406. f.keyLen = 16;
  6407. f.effectiveKeyBits = 64;
  6408. /* key length = 16, effective bits = 128 */
  6409. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6410. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6411. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  6412. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  6413. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6414. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6415. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6416. g.inLen = RC2_BLOCK_SIZE*2;
  6417. g.outLen = RC2_BLOCK_SIZE*2;
  6418. g.keyLen = 16;
  6419. g.effectiveKeyBits = 128;
  6420. /* key length = 33, effective bits = 129 */
  6421. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6422. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6423. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  6424. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  6425. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6426. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6427. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6428. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6429. "\x1e";
  6430. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6431. h.inLen = RC2_BLOCK_SIZE*2;
  6432. h.outLen = RC2_BLOCK_SIZE*2;
  6433. h.keyLen = 33;
  6434. h.effectiveKeyBits = 129;
  6435. /* key length = 10, effective bits = 40 */
  6436. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  6437. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  6438. "\x11\x22\x33\x44\x55\x66\x77\x88"
  6439. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  6440. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  6441. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  6442. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  6443. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  6444. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  6445. "\x3E\x96";
  6446. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  6447. i.inLen = RC2_BLOCK_SIZE*4;
  6448. i.outLen = RC2_BLOCK_SIZE*4;
  6449. i.keyLen = 10;
  6450. i.effectiveKeyBits = 40;
  6451. test_rc2[0] = a;
  6452. test_rc2[1] = b;
  6453. test_rc2[2] = c;
  6454. test_rc2[3] = d;
  6455. test_rc2[4] = e;
  6456. test_rc2[5] = f;
  6457. test_rc2[6] = g;
  6458. test_rc2[7] = h;
  6459. test_rc2[8] = i;
  6460. for (j = 0; j < times; ++j) {
  6461. Rc2 rc2;
  6462. XMEMSET(cipher, 0, sizeof(cipher));
  6463. XMEMSET(plain, 0, sizeof(plain));
  6464. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  6465. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  6466. if (ret != 0) {
  6467. return WC_TEST_RET_ENC_EC(ret);
  6468. }
  6469. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  6470. test_rc2[j].inLen);
  6471. if (ret != 0) {
  6472. return WC_TEST_RET_ENC_EC(ret);
  6473. }
  6474. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  6475. return WC_TEST_RET_ENC_NC;
  6476. }
  6477. /* reset IV for decrypt, since overridden by encrypt operation */
  6478. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  6479. if (ret != 0) {
  6480. return WC_TEST_RET_ENC_EC(ret);
  6481. }
  6482. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  6483. if (ret != 0) {
  6484. return WC_TEST_RET_ENC_EC(ret);
  6485. }
  6486. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  6487. return WC_TEST_RET_ENC_NC;
  6488. }
  6489. }
  6490. return 0;
  6491. }
  6492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  6493. {
  6494. wc_test_ret_t ret = 0;
  6495. WOLFSSL_ENTER("rc2_test");
  6496. ret = rc2_ecb_test();
  6497. if (ret != 0) {
  6498. return ret;
  6499. }
  6500. return rc2_cbc_test();
  6501. }
  6502. #endif
  6503. #ifndef NO_RC4
  6504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  6505. {
  6506. byte cipher[16];
  6507. byte plain[16];
  6508. wc_test_ret_t ret;
  6509. const char* keys[] =
  6510. {
  6511. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6512. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6513. "\x00\x00\x00\x00\x00\x00\x00\x00",
  6514. "\xef\x01\x23\x45"
  6515. };
  6516. testVector a, b, c, d;
  6517. testVector test_arc4[4];
  6518. int times = sizeof(test_arc4) / sizeof(testVector), i;
  6519. WOLFSSL_ENTER("arc4_test");
  6520. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  6521. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  6522. a.inLen = 8;
  6523. a.outLen = 8;
  6524. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6525. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  6526. b.inLen = 8;
  6527. b.outLen = 8;
  6528. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6529. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  6530. c.inLen = 8;
  6531. c.outLen = 8;
  6532. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  6533. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  6534. d.inLen = 10;
  6535. d.outLen = 10;
  6536. test_arc4[0] = a;
  6537. test_arc4[1] = b;
  6538. test_arc4[2] = c;
  6539. test_arc4[3] = d;
  6540. for (i = 0; i < times; ++i) {
  6541. Arc4 enc;
  6542. Arc4 dec;
  6543. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  6544. if (i == 3)
  6545. keylen = 4;
  6546. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  6547. if (ret != 0)
  6548. return WC_TEST_RET_ENC_EC(ret);
  6549. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  6550. if (ret != 0)
  6551. return WC_TEST_RET_ENC_EC(ret);
  6552. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], (word32)keylen);
  6553. if (ret != 0)
  6554. return WC_TEST_RET_ENC_EC(ret);
  6555. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], (word32)keylen);
  6556. if (ret != 0)
  6557. return WC_TEST_RET_ENC_EC(ret);
  6558. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  6559. (word32)test_arc4[i].outLen);
  6560. if (ret != 0)
  6561. return WC_TEST_RET_ENC_EC(ret);
  6562. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  6563. if (ret != 0)
  6564. return WC_TEST_RET_ENC_EC(ret);
  6565. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  6566. return WC_TEST_RET_ENC_I(i);
  6567. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  6568. return WC_TEST_RET_ENC_I(i);
  6569. wc_Arc4Free(&enc);
  6570. wc_Arc4Free(&dec);
  6571. }
  6572. return 0;
  6573. }
  6574. #endif
  6575. #ifdef HAVE_CHACHA
  6576. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  6577. {
  6578. ChaCha enc;
  6579. ChaCha dec;
  6580. byte cipher[128];
  6581. byte plain[128];
  6582. byte sliver[64];
  6583. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6584. word32 keySz = 32;
  6585. wc_test_ret_t ret = 0;
  6586. int i;
  6587. int times = 4;
  6588. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6589. {
  6590. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6592. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6594. };
  6595. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6596. {
  6597. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6598. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6599. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6601. };
  6602. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6603. {
  6604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6608. };
  6609. /* 128 bit key */
  6610. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  6611. {
  6612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6614. };
  6615. const byte* keys[] = {key1, key2, key3, key4};
  6616. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6617. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6618. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  6619. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6620. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  6621. #ifndef BENCH_EMBEDDED
  6622. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  6623. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  6624. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  6625. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  6626. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  6627. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  6628. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  6629. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  6630. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  6631. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  6632. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  6633. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  6634. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  6635. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  6636. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  6637. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  6638. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  6639. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  6640. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  6641. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  6642. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  6643. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  6644. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  6645. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  6646. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  6647. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  6648. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  6649. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  6650. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  6651. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  6652. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  6653. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  6654. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  6655. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  6656. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  6657. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  6658. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  6659. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  6660. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  6661. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  6662. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  6663. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  6664. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  6665. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  6666. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  6667. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  6668. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  6669. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  6670. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  6671. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  6672. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  6673. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  6674. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  6675. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  6676. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  6677. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  6678. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  6679. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  6680. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  6681. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  6682. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  6683. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  6684. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  6685. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  6686. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  6687. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  6688. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  6689. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  6690. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  6691. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  6692. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  6693. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  6694. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  6695. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  6696. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  6697. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  6698. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  6699. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  6700. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  6701. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  6702. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  6703. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  6704. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  6705. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  6706. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  6707. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  6708. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  6709. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  6710. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  6711. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  6712. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  6713. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  6714. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  6715. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  6716. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  6717. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  6718. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  6719. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  6720. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  6721. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  6722. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  6723. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  6724. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  6725. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  6726. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  6727. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  6728. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  6729. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  6730. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  6731. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  6732. };
  6733. #define CHACHA_BIG_TEST_SIZE 1305
  6734. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  6735. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  6736. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  6737. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  6738. #else
  6739. byte* cipher_big;
  6740. byte* plain_big;
  6741. byte* input_big;
  6742. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6743. int block_size;
  6744. #endif /* BENCH_EMBEDDED */
  6745. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  6746. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  6747. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  6748. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  6749. byte* test_chacha[4];
  6750. test_chacha[0] = a;
  6751. test_chacha[1] = b;
  6752. test_chacha[2] = c;
  6753. test_chacha[3] = d;
  6754. WOLFSSL_ENTER("chacha_test");
  6755. #ifndef BENCH_EMBEDDED
  6756. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6757. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6758. DYNAMIC_TYPE_TMP_BUFFER);
  6759. if (cipher_big == NULL) {
  6760. return MEMORY_E;
  6761. }
  6762. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6763. DYNAMIC_TYPE_TMP_BUFFER);
  6764. if (plain_big == NULL) {
  6765. return MEMORY_E;
  6766. }
  6767. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6768. DYNAMIC_TYPE_TMP_BUFFER);
  6769. if (input_big == NULL) {
  6770. return MEMORY_E;
  6771. }
  6772. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6773. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6774. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6775. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6776. #endif /* BENCH_EMBEDDED */
  6777. for (i = 0; i < times; ++i) {
  6778. if (i < 3) {
  6779. keySz = 32;
  6780. }
  6781. else {
  6782. keySz = 16;
  6783. }
  6784. XMEMCPY(plain, keys[i], keySz);
  6785. XMEMSET(cipher, 0, 32);
  6786. XMEMCPY(cipher + 4, ivs[i], 8);
  6787. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6788. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6789. if (ret != 0)
  6790. return ret;
  6791. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6792. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6793. if (ret != 0)
  6794. return ret;
  6795. XMEMCPY(plain, input, 8);
  6796. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6797. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6798. if (ret != 0)
  6799. return ret;
  6800. if (XMEMCMP(test_chacha[i], cipher, 8))
  6801. return WC_TEST_RET_ENC_I(i);
  6802. if (XMEMCMP(plain, input, 8))
  6803. return WC_TEST_RET_ENC_I(i);
  6804. }
  6805. /* test of starting at a different counter
  6806. encrypts all of the information and decrypts starting at 2nd chunk */
  6807. XMEMSET(plain, 0, sizeof(plain));
  6808. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6809. XMEMSET(cipher, 0, sizeof(cipher));
  6810. XMEMCPY(cipher + 4, ivs[0], 8);
  6811. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6812. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6813. if (ret != 0)
  6814. return ret;
  6815. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6816. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6817. if (ret != 0)
  6818. return ret;
  6819. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6820. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6821. if (ret != 0)
  6822. return ret;
  6823. if (XMEMCMP(plain + 64, sliver, 64))
  6824. return WC_TEST_RET_ENC_NC;
  6825. #ifndef BENCH_EMBEDDED
  6826. /* test of encrypting more data */
  6827. keySz = 32;
  6828. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6829. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6830. if (ret != 0)
  6831. return ret;
  6832. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6833. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6834. if (ret != 0)
  6835. return ret;
  6836. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6837. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6838. CHACHA_BIG_TEST_SIZE);
  6839. if (ret != 0)
  6840. return ret;
  6841. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6842. return WC_TEST_RET_ENC_NC;
  6843. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6844. return WC_TEST_RET_ENC_NC;
  6845. for (i = 0; i < 18; ++i) {
  6846. /* this will test all paths
  6847. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6848. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6849. */
  6850. block_size = (2 << (i%9)) - (i<9?1:0);
  6851. keySz = 32;
  6852. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6853. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6854. if (ret != 0)
  6855. return ret;
  6856. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6857. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6858. if (ret != 0)
  6859. return ret;
  6860. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , (word32)block_size);
  6861. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, (word32)block_size);
  6862. if (ret != 0)
  6863. return ret;
  6864. if (XMEMCMP(plain_big, input_big, block_size))
  6865. return WC_TEST_RET_ENC_I(i);
  6866. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6867. return WC_TEST_RET_ENC_I(i);
  6868. }
  6869. /* Streaming test */
  6870. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6871. int j, rem;
  6872. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6873. if (ret != 0)
  6874. return WC_TEST_RET_ENC_EC(ret);
  6875. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6876. if (ret != 0)
  6877. return WC_TEST_RET_ENC_EC(ret);
  6878. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6879. if (ret != 0)
  6880. return WC_TEST_RET_ENC_EC(ret);
  6881. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6882. if (ret != 0)
  6883. return WC_TEST_RET_ENC_EC(ret);
  6884. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6885. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)i);
  6886. if (ret != 0)
  6887. return WC_TEST_RET_ENC_EC(ret);
  6888. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)i);
  6889. if (ret != 0)
  6890. return WC_TEST_RET_ENC_EC(ret);
  6891. }
  6892. rem = CHACHA_BIG_TEST_SIZE - j;
  6893. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)rem);
  6894. if (ret != 0)
  6895. return WC_TEST_RET_ENC_EC(ret);
  6896. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)rem);
  6897. if (ret != 0)
  6898. return WC_TEST_RET_ENC_EC(ret);
  6899. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6900. return WC_TEST_RET_ENC_NC;
  6901. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6902. return WC_TEST_RET_ENC_NC;
  6903. }
  6904. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6905. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6906. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6907. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6908. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6909. #endif /* BENCH_EMBEDDED */
  6910. return 0;
  6911. }
  6912. #endif /* HAVE_CHACHA */
  6913. #ifdef HAVE_POLY1305
  6914. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6915. {
  6916. byte tag[16];
  6917. Poly1305 enc;
  6918. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6919. {
  6920. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6921. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6922. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6923. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6924. 0x75,0x70
  6925. };
  6926. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6927. {
  6928. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6929. 0x6c,0x64,0x21
  6930. };
  6931. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6932. {
  6933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6937. };
  6938. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6939. {
  6940. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6941. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6942. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6943. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6944. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6945. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6946. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6947. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6948. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6949. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6950. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6951. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6952. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6953. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6954. 0x61,0x16
  6955. };
  6956. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6957. {
  6958. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6959. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6960. };
  6961. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6962. {
  6963. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6964. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6965. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6966. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6967. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6968. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6969. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6970. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6971. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6972. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6973. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6974. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6975. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6976. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6977. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6978. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6979. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6980. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6981. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6982. 0x61,0x16
  6983. };
  6984. byte additional[] =
  6985. {
  6986. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6987. 0xc4,0xc5,0xc6,0xc7
  6988. };
  6989. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6990. {
  6991. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6992. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6993. };
  6994. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6995. {
  6996. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6997. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6998. };
  6999. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  7000. {
  7001. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  7002. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  7003. };
  7004. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  7005. {
  7006. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  7007. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  7008. };
  7009. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  7010. {
  7011. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  7012. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  7013. };
  7014. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  7015. {
  7016. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7017. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7018. };
  7019. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  7020. {
  7021. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  7022. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  7023. };
  7024. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7025. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  7026. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  7027. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7028. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7029. };
  7030. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7031. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  7032. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  7033. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  7034. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  7035. };
  7036. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  7037. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  7038. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  7039. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  7040. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  7041. };
  7042. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  7043. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7047. };
  7048. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  7049. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  7050. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  7051. const byte* keys[] = {key, key, key2, key2, key5, key};
  7052. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  7053. correct6};
  7054. int i;
  7055. wc_test_ret_t ret = 0;
  7056. WOLFSSL_ENTER("poly1305_test");
  7057. for (i = 0; i < 6; i++) {
  7058. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  7059. if (ret != 0)
  7060. return WC_TEST_RET_ENC_I(i);
  7061. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  7062. if (ret != 0)
  7063. return WC_TEST_RET_ENC_I(i);
  7064. ret = wc_Poly1305Final(&enc, tag);
  7065. if (ret != 0)
  7066. return WC_TEST_RET_ENC_I(i);
  7067. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  7068. return WC_TEST_RET_ENC_I(i);
  7069. }
  7070. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  7071. XMEMSET(tag, 0, sizeof(tag));
  7072. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  7073. if (ret != 0)
  7074. return WC_TEST_RET_ENC_EC(ret);
  7075. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7076. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7077. if (ret != 0)
  7078. return WC_TEST_RET_ENC_EC(ret);
  7079. if (XMEMCMP(tag, correct4, sizeof(tag)))
  7080. return WC_TEST_RET_ENC_NC;
  7081. /* Check fail of TLS MAC function if altering additional data */
  7082. XMEMSET(tag, 0, sizeof(tag));
  7083. additional[0]++;
  7084. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7085. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7086. if (ret != 0)
  7087. return WC_TEST_RET_ENC_EC(ret);
  7088. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  7089. return WC_TEST_RET_ENC_NC;
  7090. return 0;
  7091. }
  7092. #endif /* HAVE_POLY1305 */
  7093. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7094. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  7095. {
  7096. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  7097. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7098. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  7099. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  7100. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  7101. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  7102. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  7103. };
  7104. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  7105. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  7106. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  7107. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  7108. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  7109. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  7110. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  7111. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  7112. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  7113. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  7114. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  7115. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  7116. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  7117. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  7118. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  7119. 0x74, 0x2e
  7120. };
  7121. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  7122. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  7123. 0x44, 0x45, 0x46, 0x47
  7124. };
  7125. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  7126. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  7127. 0xc4, 0xc5, 0xc6, 0xc7
  7128. };
  7129. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  7130. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  7131. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  7132. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  7133. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  7134. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  7135. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  7136. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  7137. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  7138. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  7139. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  7140. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  7141. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  7142. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  7143. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  7144. 0x61, 0x16
  7145. };
  7146. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  7147. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  7148. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  7149. };
  7150. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  7151. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7152. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7153. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  7154. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  7155. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  7156. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  7157. };
  7158. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  7159. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  7160. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  7161. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  7162. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7163. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  7164. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  7165. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  7166. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  7167. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  7168. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  7169. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  7170. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  7171. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  7172. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  7173. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  7174. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7175. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  7176. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  7177. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  7178. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  7179. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  7180. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  7181. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  7182. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  7183. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  7184. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  7185. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  7186. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  7187. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  7188. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  7189. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  7190. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  7191. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  7192. 0x9d
  7193. };
  7194. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7195. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  7196. 0x05, 0x06, 0x07, 0x08
  7197. };
  7198. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  7199. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  7200. 0x00, 0x00, 0x4e, 0x91
  7201. };
  7202. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  7203. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  7204. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  7205. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  7206. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  7207. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  7208. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  7209. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  7210. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  7211. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  7212. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  7213. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  7214. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  7215. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  7216. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  7217. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  7218. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  7219. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  7220. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  7221. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  7222. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  7223. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  7224. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  7225. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  7226. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  7227. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  7228. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  7229. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  7230. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  7231. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  7232. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  7233. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  7234. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  7235. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  7236. 0x9b
  7237. };
  7238. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  7239. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  7240. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  7241. };
  7242. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  7243. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  7244. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  7245. ChaChaPoly_Aead aead;
  7246. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  7247. #define TEST_SMALL_CHACHA_CHUNKS 32
  7248. #else
  7249. #define TEST_SMALL_CHACHA_CHUNKS 64
  7250. #endif
  7251. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7252. word32 testLen;
  7253. #endif
  7254. wc_test_ret_t err;
  7255. WOLFSSL_ENTER("chacha20_poly1305_aead_test");
  7256. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7257. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7258. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7259. /* Parameter Validation testing */
  7260. /* Encrypt */
  7261. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  7262. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7263. if (err != BAD_FUNC_ARG)
  7264. return WC_TEST_RET_ENC_EC(err);
  7265. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  7266. plaintext1, sizeof(plaintext1), generatedCiphertext,
  7267. generatedAuthTag);
  7268. if (err != BAD_FUNC_ARG)
  7269. return WC_TEST_RET_ENC_EC(err);
  7270. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7271. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7272. if (err != BAD_FUNC_ARG)
  7273. return WC_TEST_RET_ENC_EC(err);
  7274. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7275. sizeof(plaintext1), NULL, generatedAuthTag);
  7276. if (err != BAD_FUNC_ARG)
  7277. return WC_TEST_RET_ENC_EC(err);
  7278. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7279. sizeof(plaintext1), generatedCiphertext, NULL);
  7280. if (err != BAD_FUNC_ARG)
  7281. return WC_TEST_RET_ENC_EC(err);
  7282. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7283. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7284. if (err != BAD_FUNC_ARG)
  7285. return WC_TEST_RET_ENC_EC(err);
  7286. /* Decrypt */
  7287. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  7288. sizeof(cipher2), authTag2, generatedPlaintext);
  7289. if (err != BAD_FUNC_ARG)
  7290. return WC_TEST_RET_ENC_EC(err);
  7291. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  7292. sizeof(cipher2), authTag2, generatedPlaintext);
  7293. if (err != BAD_FUNC_ARG)
  7294. return WC_TEST_RET_ENC_EC(err);
  7295. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7296. sizeof(cipher2), authTag2, generatedPlaintext);
  7297. if (err != BAD_FUNC_ARG)
  7298. return WC_TEST_RET_ENC_EC(err);
  7299. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7300. sizeof(cipher2), NULL, generatedPlaintext);
  7301. if (err != BAD_FUNC_ARG)
  7302. return WC_TEST_RET_ENC_EC(err);
  7303. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7304. sizeof(cipher2), authTag2, NULL);
  7305. if (err != BAD_FUNC_ARG)
  7306. return WC_TEST_RET_ENC_EC(err);
  7307. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7308. sizeof(cipher2), authTag2, generatedPlaintext);
  7309. if (err != BAD_FUNC_ARG)
  7310. return WC_TEST_RET_ENC_EC(err);
  7311. /* Test #1 */
  7312. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  7313. aad1, sizeof(aad1),
  7314. plaintext1, sizeof(plaintext1),
  7315. generatedCiphertext, generatedAuthTag);
  7316. if (err) {
  7317. return WC_TEST_RET_ENC_EC(err);
  7318. }
  7319. /* -- Check the ciphertext and authtag */
  7320. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7321. return WC_TEST_RET_ENC_NC;
  7322. }
  7323. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  7324. return WC_TEST_RET_ENC_NC;
  7325. }
  7326. /* -- Verify decryption works */
  7327. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  7328. aad1, sizeof(aad1),
  7329. cipher1, sizeof(cipher1),
  7330. authTag1, generatedPlaintext);
  7331. if (err) {
  7332. return err;
  7333. }
  7334. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7335. return WC_TEST_RET_ENC_NC;
  7336. }
  7337. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7338. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7339. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7340. /* Test #2 */
  7341. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  7342. aad2, sizeof(aad2),
  7343. plaintext2, sizeof(plaintext2),
  7344. generatedCiphertext, generatedAuthTag);
  7345. if (err) {
  7346. return err;
  7347. }
  7348. /* -- Check the ciphertext and authtag */
  7349. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7350. return WC_TEST_RET_ENC_NC;
  7351. }
  7352. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  7353. return WC_TEST_RET_ENC_NC;
  7354. }
  7355. /* -- Verify decryption works */
  7356. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  7357. aad2, sizeof(aad2),
  7358. cipher2, sizeof(cipher2),
  7359. authTag2, generatedPlaintext);
  7360. if (err) {
  7361. return err;
  7362. }
  7363. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7364. return WC_TEST_RET_ENC_NC;
  7365. }
  7366. /* AEAD init/update/final - bad argument tests */
  7367. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  7368. CHACHA20_POLY1305_AEAD_DECRYPT);
  7369. if (err != BAD_FUNC_ARG)
  7370. return WC_TEST_RET_ENC_EC(err);
  7371. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  7372. CHACHA20_POLY1305_AEAD_DECRYPT);
  7373. if (err != BAD_FUNC_ARG)
  7374. return WC_TEST_RET_ENC_EC(err);
  7375. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  7376. CHACHA20_POLY1305_AEAD_DECRYPT);
  7377. if (err != BAD_FUNC_ARG)
  7378. return WC_TEST_RET_ENC_EC(err);
  7379. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  7380. if (err != BAD_FUNC_ARG)
  7381. return WC_TEST_RET_ENC_EC(err);
  7382. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  7383. if (err != BAD_FUNC_ARG)
  7384. return WC_TEST_RET_ENC_EC(err);
  7385. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  7386. generatedPlaintext, sizeof(plaintext1));
  7387. if (err != BAD_FUNC_ARG)
  7388. return WC_TEST_RET_ENC_EC(err);
  7389. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  7390. sizeof(plaintext1));
  7391. if (err != BAD_FUNC_ARG)
  7392. return WC_TEST_RET_ENC_EC(err);
  7393. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  7394. sizeof(plaintext1));
  7395. if (err != BAD_FUNC_ARG)
  7396. return WC_TEST_RET_ENC_EC(err);
  7397. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  7398. if (err != BAD_FUNC_ARG)
  7399. return WC_TEST_RET_ENC_EC(err);
  7400. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  7401. if (err != BAD_FUNC_ARG)
  7402. return WC_TEST_RET_ENC_EC(err);
  7403. /* AEAD init/update/final - bad state tests */
  7404. /* clear struct - make valgrind happy to resolve
  7405. "Conditional jump or move depends on uninitialised value(s)".
  7406. The enum is "int" size and aead.state is "byte" */
  7407. /* The wc_ChaCha20Poly1305_Init function does this normally */
  7408. XMEMSET(&aead, 0, sizeof(aead));
  7409. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7410. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7411. if (err != BAD_STATE_E)
  7412. return WC_TEST_RET_ENC_EC(err);
  7413. aead.state = CHACHA20_POLY1305_STATE_DATA;
  7414. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7415. if (err != BAD_STATE_E)
  7416. return WC_TEST_RET_ENC_EC(err);
  7417. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7418. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  7419. generatedPlaintext, sizeof(plaintext1));
  7420. if (err != BAD_STATE_E)
  7421. return WC_TEST_RET_ENC_EC(err);
  7422. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7423. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7424. if (err != BAD_STATE_E)
  7425. return WC_TEST_RET_ENC_EC(err);
  7426. aead.state = CHACHA20_POLY1305_STATE_READY;
  7427. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7428. if (err != BAD_STATE_E)
  7429. return WC_TEST_RET_ENC_EC(err);
  7430. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7431. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7432. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7433. /* Test 1 - Encrypt */
  7434. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7435. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7436. if (err != 0)
  7437. return WC_TEST_RET_ENC_EC(err);
  7438. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7439. if (err != 0)
  7440. return WC_TEST_RET_ENC_EC(err);
  7441. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7442. /* test doing data in smaller chunks */
  7443. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7444. word32 dataLen = sizeof(plaintext1) - testLen;
  7445. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7446. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7447. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  7448. &generatedCiphertext[testLen], dataLen);
  7449. if (err != 0)
  7450. return WC_TEST_RET_ENC_EC(err);
  7451. testLen += dataLen;
  7452. }
  7453. #else
  7454. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  7455. generatedCiphertext, sizeof(plaintext1));
  7456. #endif
  7457. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7458. if (err != 0)
  7459. return WC_TEST_RET_ENC_EC(err);
  7460. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7461. if (err != 0)
  7462. return WC_TEST_RET_ENC_EC(err);
  7463. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7464. return WC_TEST_RET_ENC_NC;
  7465. }
  7466. /* Test 1 - Decrypt */
  7467. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7468. CHACHA20_POLY1305_AEAD_DECRYPT);
  7469. if (err != 0)
  7470. return WC_TEST_RET_ENC_EC(err);
  7471. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7472. if (err != 0)
  7473. return WC_TEST_RET_ENC_EC(err);
  7474. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7475. /* test doing data in smaller chunks */
  7476. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7477. word32 dataLen = sizeof(plaintext1) - testLen;
  7478. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7479. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7480. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7481. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7482. dataLen);
  7483. if (err != 0)
  7484. return WC_TEST_RET_ENC_EC(err);
  7485. testLen += dataLen;
  7486. }
  7487. #else
  7488. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7489. generatedPlaintext, sizeof(cipher1));
  7490. #endif
  7491. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7492. if (err != 0)
  7493. return WC_TEST_RET_ENC_EC(err);
  7494. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7495. if (err != 0)
  7496. return WC_TEST_RET_ENC_EC(err);
  7497. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7498. return WC_TEST_RET_ENC_NC;
  7499. }
  7500. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7501. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7502. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7503. /* Test 2 - Encrypt */
  7504. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7505. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7506. if (err != 0)
  7507. return WC_TEST_RET_ENC_EC(err);
  7508. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7509. if (err != 0)
  7510. return WC_TEST_RET_ENC_EC(err);
  7511. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7512. /* test doing data in smaller chunks */
  7513. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7514. word32 dataLen = sizeof(plaintext2) - testLen;
  7515. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7516. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7517. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  7518. &generatedCiphertext[testLen], dataLen);
  7519. if (err != 0)
  7520. return WC_TEST_RET_ENC_EC(err);
  7521. testLen += dataLen;
  7522. }
  7523. #else
  7524. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  7525. sizeof(plaintext2));
  7526. #endif
  7527. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7528. if (err != 0)
  7529. return WC_TEST_RET_ENC_EC(err);
  7530. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7531. if (err != 0)
  7532. return WC_TEST_RET_ENC_EC(err);
  7533. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7534. return WC_TEST_RET_ENC_NC;
  7535. }
  7536. /* Test 2 - Decrypt */
  7537. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7538. CHACHA20_POLY1305_AEAD_DECRYPT);
  7539. if (err != 0)
  7540. return WC_TEST_RET_ENC_EC(err);
  7541. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7542. if (err != 0)
  7543. return WC_TEST_RET_ENC_EC(err);
  7544. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7545. /* test doing data in smaller chunks */
  7546. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7547. word32 dataLen = sizeof(plaintext2) - testLen;
  7548. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7549. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7550. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7551. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7552. dataLen);
  7553. if (err != 0)
  7554. return WC_TEST_RET_ENC_EC(err);
  7555. testLen += dataLen;
  7556. }
  7557. #else
  7558. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7559. generatedPlaintext, sizeof(cipher2));
  7560. #endif
  7561. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7562. if (err != 0)
  7563. return WC_TEST_RET_ENC_EC(err);
  7564. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7565. if (err != 0)
  7566. return WC_TEST_RET_ENC_EC(err);
  7567. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7568. return WC_TEST_RET_ENC_NC;
  7569. }
  7570. return err;
  7571. }
  7572. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  7573. #ifndef NO_DES3
  7574. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  7575. {
  7576. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  7577. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7578. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7579. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7580. };
  7581. byte plain[24];
  7582. byte cipher[24];
  7583. Des enc;
  7584. Des dec;
  7585. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  7586. {
  7587. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  7588. };
  7589. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  7590. {
  7591. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  7592. };
  7593. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7594. {
  7595. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  7596. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  7597. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  7598. };
  7599. wc_test_ret_t ret;
  7600. WOLFSSL_ENTER("des_test");
  7601. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  7602. if (ret != 0)
  7603. return WC_TEST_RET_ENC_EC(ret);
  7604. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7605. if (ret != 0)
  7606. return WC_TEST_RET_ENC_EC(ret);
  7607. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  7608. if (ret != 0)
  7609. return WC_TEST_RET_ENC_EC(ret);
  7610. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7611. if (ret != 0)
  7612. return WC_TEST_RET_ENC_EC(ret);
  7613. if (XMEMCMP(plain, vector, sizeof(plain)))
  7614. return WC_TEST_RET_ENC_NC;
  7615. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  7616. return WC_TEST_RET_ENC_NC;
  7617. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  7618. if (ret != 0)
  7619. return WC_TEST_RET_ENC_EC(ret);
  7620. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7621. {
  7622. EncryptedInfo info;
  7623. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7624. XMEMCPY(info.iv, iv, sizeof(iv));
  7625. info.ivSz = sizeof(iv);
  7626. info.keySz = sizeof(key);
  7627. info.cipherType = WC_CIPHER_DES;
  7628. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7629. sizeof(key), WC_HASH_TYPE_SHA);
  7630. if (ret != 0)
  7631. return WC_TEST_RET_ENC_EC(ret);
  7632. /* Test invalid info ptr */
  7633. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  7634. sizeof(key), WC_HASH_TYPE_SHA);
  7635. if (ret != BAD_FUNC_ARG)
  7636. return WC_TEST_RET_ENC_EC(ret);
  7637. #ifndef NO_PWDBASED
  7638. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  7639. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7640. sizeof(key), WC_HASH_TYPE_NONE);
  7641. if (ret == 0)
  7642. return WC_TEST_RET_ENC_EC(ret);
  7643. #endif /* !NO_PWDBASED */
  7644. }
  7645. #endif
  7646. return 0;
  7647. }
  7648. #endif /* !NO_DES3 */
  7649. #ifndef NO_DES3
  7650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  7651. {
  7652. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  7653. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7654. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7655. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7656. };
  7657. byte plain[24];
  7658. byte cipher[24];
  7659. Des3 enc;
  7660. Des3 dec;
  7661. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7662. {
  7663. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  7664. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  7665. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  7666. };
  7667. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7668. {
  7669. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  7670. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  7671. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  7672. };
  7673. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  7674. {
  7675. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  7676. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  7677. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  7678. };
  7679. wc_test_ret_t ret;
  7680. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7681. size_t i;
  7682. #endif
  7683. WOLFSSL_ENTER("des3_test");
  7684. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  7685. if (ret != 0)
  7686. return WC_TEST_RET_ENC_EC(ret);
  7687. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  7688. if (ret != 0)
  7689. return WC_TEST_RET_ENC_EC(ret);
  7690. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  7691. if (ret != 0)
  7692. return WC_TEST_RET_ENC_EC(ret);
  7693. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  7694. if (ret != 0)
  7695. return WC_TEST_RET_ENC_EC(ret);
  7696. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7697. #if defined(WOLFSSL_ASYNC_CRYPT)
  7698. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7699. #endif
  7700. if (ret != 0)
  7701. return WC_TEST_RET_ENC_EC(ret);
  7702. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7703. #if defined(WOLFSSL_ASYNC_CRYPT)
  7704. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7705. #endif
  7706. if (ret != 0)
  7707. return WC_TEST_RET_ENC_EC(ret);
  7708. if (XMEMCMP(plain, vector, sizeof(plain)))
  7709. return WC_TEST_RET_ENC_NC;
  7710. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7711. return WC_TEST_RET_ENC_NC;
  7712. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7713. /* test the same vectors with using compatibility layer */
  7714. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  7715. DES_key_schedule ks1;
  7716. DES_key_schedule ks2;
  7717. DES_key_schedule ks3;
  7718. DES_cblock iv4;
  7719. byte tmp[sizeof(vector)];
  7720. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  7721. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  7722. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  7723. XMEMSET(plain, 0, sizeof(plain));
  7724. XMEMSET(cipher, 0, sizeof(cipher));
  7725. /* Test in-place encrypt/decrypt */
  7726. XMEMCPY(tmp, vector, sizeof(vector));
  7727. /* Use i as the splitter */
  7728. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7729. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7730. &iv4, DES_ENCRYPT);
  7731. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  7732. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  7733. XMEMCPY(cipher, tmp, sizeof(cipher));
  7734. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7735. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7736. &iv4, DES_DECRYPT);
  7737. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  7738. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  7739. XMEMCPY(plain, tmp, sizeof(plain));
  7740. if (XMEMCMP(plain, vector, sizeof(plain)))
  7741. return WC_TEST_RET_ENC_NC;
  7742. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7743. return WC_TEST_RET_ENC_NC;
  7744. }
  7745. #endif /* OPENSSL_EXTRA */
  7746. wc_Des3Free(&enc);
  7747. wc_Des3Free(&dec);
  7748. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7749. {
  7750. EncryptedInfo info;
  7751. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7752. XMEMCPY(info.iv, iv3, sizeof(iv3));
  7753. info.ivSz = sizeof(iv3);
  7754. info.keySz = sizeof(key3);
  7755. info.cipherType = WC_CIPHER_DES3;
  7756. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  7757. sizeof(key3), WC_HASH_TYPE_SHA);
  7758. if (ret != 0)
  7759. return WC_TEST_RET_ENC_EC(ret);
  7760. }
  7761. #endif
  7762. return 0;
  7763. }
  7764. #endif /* NO_DES3 */
  7765. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  7766. * see print_fiducials() below.
  7767. */
  7768. #ifndef NO_AES
  7769. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7770. defined(WOLFSSL_AES_XTS)
  7771. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7772. && !defined(HAVE_SELFTEST)
  7773. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  7774. /* pass in the function, key, iv, plain text and expected and this function
  7775. * tests that the encryption and decryption is successful */
  7776. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7777. const byte* iv, const byte* plain, int plainSz,
  7778. const byte* expected, int expectedSz)
  7779. {
  7780. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7781. EVP_CIPHER_CTX *ctx = NULL;
  7782. #else
  7783. EVP_CIPHER_CTX ctx[1];
  7784. #endif
  7785. int ctx_inited = 0;
  7786. int idx, cipherSz;
  7787. wc_test_ret_t ret = 0;
  7788. byte* cipher;
  7789. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7790. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7791. return MEMORY_E;
  7792. #endif
  7793. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7794. if (cipher == NULL) {
  7795. ret = WC_TEST_RET_ENC_ERRNO;
  7796. goto EVP_TEST_END;
  7797. }
  7798. /* test encrypt */
  7799. EVP_CIPHER_CTX_init(ctx);
  7800. ctx_inited = 1;
  7801. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7802. ret = WC_TEST_RET_ENC_NC;
  7803. goto EVP_TEST_END;
  7804. }
  7805. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7806. ret = WC_TEST_RET_ENC_NC;
  7807. goto EVP_TEST_END;
  7808. }
  7809. cipherSz = idx;
  7810. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7811. ret = WC_TEST_RET_ENC_NC;
  7812. goto EVP_TEST_END;
  7813. }
  7814. cipherSz += idx;
  7815. if (XMEMCMP(cipher, expected, plainSz)) {
  7816. ret = WC_TEST_RET_ENC_NC;
  7817. goto EVP_TEST_END;
  7818. }
  7819. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7820. ctx_inited = 0;
  7821. if (ret == WOLFSSL_SUCCESS)
  7822. ret = 0;
  7823. else {
  7824. ret = WC_TEST_RET_ENC_NC;
  7825. goto EVP_TEST_END;
  7826. }
  7827. /* test decrypt */
  7828. EVP_CIPHER_CTX_init(ctx);
  7829. ctx_inited = 1;
  7830. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7831. ret = WC_TEST_RET_ENC_NC;
  7832. goto EVP_TEST_END;
  7833. }
  7834. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7835. ret = WC_TEST_RET_ENC_NC;
  7836. goto EVP_TEST_END;
  7837. }
  7838. cipherSz = idx;
  7839. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7840. ret = WC_TEST_RET_ENC_NC;
  7841. goto EVP_TEST_END;
  7842. }
  7843. cipherSz += idx;
  7844. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7845. ret = WC_TEST_RET_ENC_NC;
  7846. goto EVP_TEST_END;
  7847. }
  7848. EVP_TEST_END:
  7849. if (cipher)
  7850. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7851. (void)cipherSz;
  7852. if (ctx_inited) {
  7853. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7854. if (cleanup_ret != WOLFSSL_SUCCESS)
  7855. ret = WC_TEST_RET_ENC_NC;
  7856. }
  7857. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7858. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7859. #endif
  7860. return ret;
  7861. }
  7862. #endif /* !HAVE_FIPS || FIPS_VERSION3_GE(6,0,0) */
  7863. #endif /* OPENSSL_EXTRA && !WOLFCRYPT_ONLY && !HAVE_SELFTEST */
  7864. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7865. #ifdef WOLFSSL_AES_OFB
  7866. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7867. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7868. {
  7869. #ifdef WOLFSSL_AES_256
  7870. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7871. {
  7872. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7873. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7874. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7875. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7876. };
  7877. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7878. {
  7879. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7880. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7881. };
  7882. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7883. {
  7884. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7885. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7886. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7887. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7888. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7889. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7890. };
  7891. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7892. {
  7893. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7894. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7895. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7896. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7897. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7898. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7899. };
  7900. #endif /* WOLFSSL_AES_256 */
  7901. #ifdef WOLFSSL_AES_128
  7902. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7903. {
  7904. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7905. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7906. };
  7907. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  7908. {
  7909. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7911. };
  7912. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  7913. {
  7914. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7915. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7916. };
  7917. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7918. {
  7919. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7920. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7921. };
  7922. #endif /* WOLFSSL_AES_128 */
  7923. #ifdef WOLFSSL_AES_192
  7924. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7925. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7926. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7927. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7928. };
  7929. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7930. {
  7931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7933. };
  7934. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7935. {
  7936. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7937. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7938. };
  7939. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7940. {
  7941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7943. };
  7944. #endif /* WOLFSSL_AES_192 */
  7945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7946. Aes *enc = NULL;
  7947. #else
  7948. Aes enc[1];
  7949. #endif
  7950. byte cipher[AES_BLOCK_SIZE * 4];
  7951. #ifdef HAVE_AES_DECRYPT
  7952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7953. Aes *dec = NULL;
  7954. #else
  7955. Aes dec[1];
  7956. #endif
  7957. byte plain [AES_BLOCK_SIZE * 4];
  7958. #endif
  7959. wc_test_ret_t ret = 0;
  7960. WOLFSSL_ENTER("aesofb_test");
  7961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7962. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7963. ERROR_OUT(-1, out);
  7964. #ifdef HAVE_AES_DECRYPT
  7965. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7966. ERROR_OUT(-1, out);
  7967. #endif
  7968. #endif
  7969. XMEMSET(enc, 0, sizeof *enc);
  7970. #ifdef HAVE_AES_DECRYPT
  7971. XMEMSET(dec, 0, sizeof *dec);
  7972. #endif
  7973. #ifdef WOLFSSL_AES_128
  7974. /* 128 key size test */
  7975. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7976. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7977. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7978. cipher2, sizeof(cipher2));
  7979. if (ret != 0) {
  7980. goto out;
  7981. }
  7982. #endif
  7983. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  7984. if (ret != 0)
  7985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7986. #ifdef HAVE_AES_DECRYPT
  7987. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  7988. if (ret != 0)
  7989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7990. #endif
  7991. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7992. if (ret != 0)
  7993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7994. #ifdef HAVE_AES_DECRYPT
  7995. /* decrypt uses AES_ENCRYPTION */
  7996. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7997. if (ret != 0)
  7998. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7999. #endif
  8000. XMEMSET(cipher, 0, sizeof(cipher));
  8001. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  8002. if (ret != 0)
  8003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8004. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  8005. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8006. #ifdef HAVE_AES_DECRYPT
  8007. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  8008. if (ret != 0)
  8009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8010. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  8011. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8012. #endif /* HAVE_AES_DECRYPT */
  8013. #endif /* WOLFSSL_AES_128 */
  8014. #ifdef WOLFSSL_AES_192
  8015. /* 192 key size test */
  8016. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8017. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8018. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  8019. cipher3, sizeof(cipher3));
  8020. if (ret != 0) {
  8021. goto out;
  8022. }
  8023. #endif
  8024. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8025. if (ret != 0)
  8026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8027. #ifdef HAVE_AES_DECRYPT
  8028. /* decrypt uses AES_ENCRYPTION */
  8029. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8030. if (ret != 0)
  8031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8032. #endif
  8033. XMEMSET(cipher, 0, sizeof(cipher));
  8034. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  8035. if (ret != 0)
  8036. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8037. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  8038. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8039. #ifdef HAVE_AES_DECRYPT
  8040. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  8041. if (ret != 0)
  8042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8043. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  8044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8045. #endif /* HAVE_AES_DECRYPT */
  8046. #endif /* WOLFSSL_AES_192 */
  8047. #ifdef WOLFSSL_AES_256
  8048. /* 256 key size test */
  8049. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8050. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8051. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  8052. cipher1, sizeof(cipher1));
  8053. if (ret != 0) {
  8054. goto out;
  8055. }
  8056. #endif
  8057. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8058. if (ret != 0)
  8059. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8060. #ifdef HAVE_AES_DECRYPT
  8061. /* decrypt uses AES_ENCRYPTION */
  8062. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8063. if (ret != 0)
  8064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8065. #endif
  8066. XMEMSET(cipher, 0, sizeof(cipher));
  8067. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  8068. if (ret != 0)
  8069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8070. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  8071. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8072. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8073. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8074. if (ret != 0)
  8075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8076. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  8077. AES_BLOCK_SIZE))
  8078. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8079. #ifdef HAVE_AES_DECRYPT
  8080. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  8081. if (ret != 0)
  8082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8083. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  8084. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8085. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  8086. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8087. if (ret != 0)
  8088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8089. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  8090. AES_BLOCK_SIZE))
  8091. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8092. #endif /* HAVE_AES_DECRYPT */
  8093. /* multiple blocks at once */
  8094. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8095. if (ret != 0)
  8096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8097. #ifdef HAVE_AES_DECRYPT
  8098. /* decrypt uses AES_ENCRYPTION */
  8099. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8100. if (ret != 0)
  8101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8102. #endif
  8103. XMEMSET(cipher, 0, sizeof(cipher));
  8104. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  8105. if (ret != 0)
  8106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8107. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  8108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8109. #ifdef HAVE_AES_DECRYPT
  8110. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  8111. if (ret != 0)
  8112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8113. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  8114. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8115. #endif /* HAVE_AES_DECRYPT */
  8116. /* inline decrypt/encrypt*/
  8117. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8118. if (ret != 0)
  8119. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8120. #ifdef HAVE_AES_DECRYPT
  8121. /* decrypt uses AES_ENCRYPTION */
  8122. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8123. if (ret != 0)
  8124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8125. #endif
  8126. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  8127. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  8128. if (ret != 0)
  8129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8130. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8132. #ifdef HAVE_AES_DECRYPT
  8133. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  8134. if (ret != 0)
  8135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8136. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  8137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8138. #endif /* HAVE_AES_DECRYPT */
  8139. /* 256 key size test leftover support */
  8140. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8141. if (ret != 0)
  8142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8143. #ifdef HAVE_AES_DECRYPT
  8144. /* decrypt uses AES_ENCRYPTION */
  8145. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8146. if (ret != 0)
  8147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8148. #endif
  8149. XMEMSET(cipher, 0, sizeof(cipher));
  8150. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  8151. if (ret != 0)
  8152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8153. if (XMEMCMP(cipher, cipher1, 3))
  8154. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8155. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  8156. if (ret != 0)
  8157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8158. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  8159. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8160. #ifdef HAVE_AES_DECRYPT
  8161. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  8162. if (ret != 0)
  8163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8164. if (XMEMCMP(plain, plain1, 6))
  8165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8166. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  8167. if (ret != 0)
  8168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8169. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  8170. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8171. #endif /* HAVE_AES_DECRYPT */
  8172. out:
  8173. wc_AesFree(enc);
  8174. #ifdef HAVE_AES_DECRYPT
  8175. wc_AesFree(dec);
  8176. #endif
  8177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8178. if (enc)
  8179. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8180. #ifdef HAVE_AES_DECRYPT
  8181. if (dec)
  8182. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8183. #endif
  8184. #endif
  8185. #endif /* WOLFSSL_AES_256 */
  8186. return ret;
  8187. }
  8188. #endif /* WOLFSSL_AES_OFB */
  8189. #if defined(WOLFSSL_AES_CFB)
  8190. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of
  8191. * Operation Methods and Techniques
  8192. */
  8193. static wc_test_ret_t aescfb_test_0(void)
  8194. {
  8195. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8196. Aes *enc = NULL;
  8197. #else
  8198. Aes enc[1];
  8199. #endif
  8200. int enc_inited = 0;
  8201. byte cipher[AES_BLOCK_SIZE * 4];
  8202. #ifdef HAVE_AES_DECRYPT
  8203. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8204. Aes *dec = NULL;
  8205. #else
  8206. Aes dec[1];
  8207. #endif
  8208. int dec_inited = 0;
  8209. byte plain [AES_BLOCK_SIZE * 4];
  8210. #endif
  8211. wc_test_ret_t ret = 0;
  8212. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8213. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8214. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  8215. };
  8216. #ifdef WOLFSSL_AES_128
  8217. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8218. {
  8219. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8220. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8221. };
  8222. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8223. {
  8224. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  8225. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  8226. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  8227. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  8228. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  8229. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  8230. };
  8231. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8232. {
  8233. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8234. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8235. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8236. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8237. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8238. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  8239. };
  8240. #endif /* WOLFSSL_AES_128 */
  8241. #ifdef WOLFSSL_AES_192
  8242. /* 192 size key test */
  8243. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8244. {
  8245. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8246. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8247. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8248. };
  8249. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8250. {
  8251. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  8252. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  8253. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  8254. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  8255. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  8256. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  8257. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  8258. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8261. {
  8262. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8263. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8264. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8265. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8266. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8267. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8268. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8269. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8270. };
  8271. #endif /* WOLFSSL_AES_192 */
  8272. #ifdef WOLFSSL_AES_256
  8273. /* 256 size key simple test */
  8274. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8275. {
  8276. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8277. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8278. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8279. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8280. };
  8281. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8282. {
  8283. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  8284. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  8285. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  8286. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  8287. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  8288. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  8289. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  8290. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  8291. };
  8292. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8293. {
  8294. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8295. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8296. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8297. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8298. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8299. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8300. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8301. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8302. };
  8303. #endif /* WOLFSSL_AES_256 */
  8304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8305. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8306. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8307. #ifdef HAVE_AES_DECRYPT
  8308. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8309. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8310. #endif
  8311. #endif
  8312. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8313. if (ret != 0)
  8314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8315. else
  8316. enc_inited = 1;
  8317. #ifdef HAVE_AES_DECRYPT
  8318. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8319. if (ret != 0)
  8320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8321. else
  8322. dec_inited = 1;
  8323. #endif
  8324. #ifdef WOLFSSL_AES_128
  8325. /* 128 key tests */
  8326. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8327. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8328. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  8329. cipher1, sizeof(cipher1));
  8330. if (ret != 0) {
  8331. return ret;
  8332. }
  8333. #endif
  8334. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8335. if (ret != 0)
  8336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8337. #ifdef HAVE_AES_DECRYPT
  8338. /* decrypt uses AES_ENCRYPTION */
  8339. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8340. if (ret != 0)
  8341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8342. #endif
  8343. XMEMSET(cipher, 0, sizeof(cipher));
  8344. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  8345. if (ret != 0)
  8346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8347. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8348. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8349. /* test restarting encryption process */
  8350. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  8351. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  8352. if (ret != 0)
  8353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8354. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  8355. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  8356. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8357. #ifdef HAVE_AES_DECRYPT
  8358. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  8359. if (ret != 0)
  8360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8361. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  8362. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8363. #endif /* HAVE_AES_DECRYPT */
  8364. #endif /* WOLFSSL_AES_128 */
  8365. #ifdef WOLFSSL_AES_192
  8366. /* 192 key size test */
  8367. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8368. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8369. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  8370. cipher2, sizeof(cipher2));
  8371. if (ret != 0) {
  8372. return ret;
  8373. }
  8374. #endif
  8375. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8376. if (ret != 0)
  8377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8378. #ifdef HAVE_AES_DECRYPT
  8379. /* decrypt uses AES_ENCRYPTION */
  8380. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8381. if (ret != 0)
  8382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8383. #endif
  8384. XMEMSET(cipher, 0, sizeof(cipher));
  8385. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  8386. if (ret != 0)
  8387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8388. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  8389. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8390. #ifdef HAVE_AES_DECRYPT
  8391. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  8392. if (ret != 0)
  8393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8394. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  8395. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8396. #endif /* HAVE_AES_DECRYPT */
  8397. #endif /* WOLFSSL_AES_192 */
  8398. #ifdef WOLFSSL_AES_256
  8399. /* 256 key size test */
  8400. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8401. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8402. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  8403. cipher3, sizeof(cipher3));
  8404. if (ret != 0) {
  8405. return ret;
  8406. }
  8407. #endif
  8408. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8409. if (ret != 0)
  8410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8411. #ifdef HAVE_AES_DECRYPT
  8412. /* decrypt uses AES_ENCRYPTION */
  8413. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8414. if (ret != 0)
  8415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8416. #endif
  8417. /* test with data left overs, magic lengths are checking near edges */
  8418. XMEMSET(cipher, 0, sizeof(cipher));
  8419. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  8420. if (ret != 0)
  8421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8422. if (XMEMCMP(cipher, cipher3, 4))
  8423. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8424. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  8425. if (ret != 0)
  8426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8427. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  8428. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8429. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  8430. (AES_BLOCK_SIZE * 4) - 31);
  8431. if (ret != 0)
  8432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8433. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  8434. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8435. #ifdef HAVE_AES_DECRYPT
  8436. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  8437. if (ret != 0)
  8438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8439. if (XMEMCMP(plain, msg3, 4))
  8440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8441. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  8442. if (ret != 0)
  8443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8444. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  8445. if (ret != 0)
  8446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8447. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  8448. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8449. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  8450. (AES_BLOCK_SIZE * 4) - 31);
  8451. if (ret != 0)
  8452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8453. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  8454. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8455. #endif /* HAVE_AES_DECRYPT */
  8456. #endif /* WOLFSSL_AES_256 */
  8457. out:
  8458. if (enc_inited)
  8459. wc_AesFree(enc);
  8460. #ifdef HAVE_AES_DECRYPT
  8461. if (dec_inited)
  8462. wc_AesFree(dec);
  8463. #endif
  8464. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8465. if (enc)
  8466. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8467. #ifdef HAVE_AES_DECRYPT
  8468. if (dec)
  8469. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8470. #endif
  8471. #endif
  8472. return ret;
  8473. }
  8474. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0))
  8475. static wc_test_ret_t aescfb1_test(void)
  8476. {
  8477. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8478. Aes *enc = NULL;
  8479. #else
  8480. Aes enc[1];
  8481. #endif
  8482. int enc_inited = 0;
  8483. byte cipher[AES_BLOCK_SIZE];
  8484. #ifdef HAVE_AES_DECRYPT
  8485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8486. Aes *dec = NULL;
  8487. #else
  8488. Aes dec[1];
  8489. #endif
  8490. int dec_inited = 0;
  8491. byte plain [AES_BLOCK_SIZE];
  8492. #endif
  8493. wc_test_ret_t ret = 0;
  8494. #ifdef WOLFSSL_AES_128
  8495. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8496. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  8497. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  8498. };
  8499. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8500. {
  8501. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  8502. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  8503. };
  8504. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8505. {
  8506. 0x00
  8507. };
  8508. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8509. {
  8510. 0xC0
  8511. };
  8512. WOLFSSL_SMALL_STACK_STATIC const byte cipher1_7bit[] =
  8513. {
  8514. 0x1C
  8515. };
  8516. #endif /* WOLFSSL_AES_128 */
  8517. #ifdef WOLFSSL_AES_192
  8518. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8519. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  8520. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  8521. };
  8522. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8523. {
  8524. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  8525. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  8526. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  8527. };
  8528. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8529. {
  8530. 0x30
  8531. };
  8532. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8533. {
  8534. 0x80
  8535. };
  8536. #endif /* WOLFSSL_AES_192 */
  8537. #ifdef WOLFSSL_AES_256
  8538. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8539. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  8540. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  8541. };
  8542. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8543. {
  8544. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  8545. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  8546. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  8547. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  8548. };
  8549. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8550. {
  8551. 0xF7, 0x00
  8552. };
  8553. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8554. {
  8555. 0x41, 0xC0
  8556. };
  8557. #endif /* WOLFSSL_AES_256 */
  8558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8559. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8560. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8561. #ifdef HAVE_AES_DECRYPT
  8562. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8563. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8564. #endif
  8565. #endif
  8566. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8567. if (ret != 0)
  8568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8569. else
  8570. enc_inited = 1;
  8571. #ifdef HAVE_AES_DECRYPT
  8572. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8573. if (ret != 0)
  8574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8575. else
  8576. dec_inited = 1;
  8577. #endif
  8578. #ifdef WOLFSSL_AES_128
  8579. /* 128 key tests */
  8580. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8581. if (ret != 0)
  8582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8583. #ifdef HAVE_AES_DECRYPT
  8584. /* decrypt uses AES_ENCRYPTION */
  8585. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8586. if (ret != 0)
  8587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8588. #endif
  8589. XMEMSET(cipher, 0, sizeof(cipher));
  8590. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  8591. if (ret != 0)
  8592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8593. if (cipher[0] != cipher1[0])
  8594. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8595. #ifdef HAVE_AES_DECRYPT
  8596. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  8597. if (ret != 0)
  8598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8599. if (plain[0] != msg1[0])
  8600. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8601. #endif /* HAVE_AES_DECRYPT */
  8602. XMEMSET(cipher, 0, sizeof(cipher));
  8603. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 7);
  8604. if (ret != 0)
  8605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8606. if (cipher[0] != cipher1_7bit[0])
  8607. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8608. #ifdef OPENSSL_EXTRA
  8609. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8610. if (ret != 0)
  8611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8612. XMEMSET(cipher, 0, sizeof(cipher));
  8613. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  8614. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  8615. if (ret != 0)
  8616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8617. #ifndef WOLFCRYPT_ONLY
  8618. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  8619. cipher, sizeof(msg1));
  8620. if (ret != 0) {
  8621. goto out;
  8622. }
  8623. #endif
  8624. #endif
  8625. #endif /* WOLFSSL_AES_128 */
  8626. #ifdef WOLFSSL_AES_192
  8627. /* 192 key tests */
  8628. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8629. if (ret != 0)
  8630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8631. XMEMSET(cipher, 0, sizeof(cipher));
  8632. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  8633. if (ret != 0)
  8634. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8635. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  8636. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8637. #ifdef OPENSSL_EXTRA
  8638. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8639. if (ret != 0)
  8640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8641. XMEMSET(cipher, 0, sizeof(cipher));
  8642. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  8643. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  8644. if (ret != 0)
  8645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8646. #ifndef WOLFCRYPT_ONLY
  8647. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  8648. cipher, sizeof(msg2));
  8649. if (ret != 0) {
  8650. goto out;
  8651. }
  8652. #endif
  8653. #endif
  8654. #endif /* WOLFSSL_AES_192 */
  8655. #ifdef WOLFSSL_AES_256
  8656. /* 256 key tests */
  8657. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8658. if (ret != 0)
  8659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8660. XMEMSET(cipher, 0, sizeof(cipher));
  8661. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  8662. if (ret != 0)
  8663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8664. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8665. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8666. #ifdef OPENSSL_EXTRA
  8667. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8668. if (ret != 0)
  8669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8670. XMEMSET(cipher, 0, sizeof(cipher));
  8671. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  8672. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  8673. if (ret != 0)
  8674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8675. #ifndef WOLFCRYPT_ONLY
  8676. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  8677. cipher, sizeof(msg3));
  8678. if (ret != 0) {
  8679. goto out;
  8680. }
  8681. #endif
  8682. #endif
  8683. #endif /* WOLFSSL_AES_256 */
  8684. out:
  8685. if (enc_inited)
  8686. wc_AesFree(enc);
  8687. #ifdef HAVE_AES_DECRYPT
  8688. if (dec_inited)
  8689. wc_AesFree(dec);
  8690. #endif
  8691. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8692. if (enc)
  8693. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8694. #ifdef HAVE_AES_DECRYPT
  8695. if (dec)
  8696. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8697. #endif
  8698. #endif
  8699. return ret;
  8700. }
  8701. static wc_test_ret_t aescfb8_test(void)
  8702. {
  8703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8704. Aes *enc = NULL;
  8705. #else
  8706. Aes enc[1];
  8707. #endif
  8708. int enc_inited = 0;
  8709. byte cipher[AES_BLOCK_SIZE];
  8710. #ifdef HAVE_AES_DECRYPT
  8711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8712. Aes *dec = NULL;
  8713. #else
  8714. Aes dec[1];
  8715. #endif
  8716. int dec_inited = 0;
  8717. byte plain [AES_BLOCK_SIZE];
  8718. #endif
  8719. wc_test_ret_t ret = 0;
  8720. #ifdef WOLFSSL_AES_128
  8721. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8722. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  8723. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  8724. };
  8725. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8726. {
  8727. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  8728. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  8729. };
  8730. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8731. {
  8732. 0xd2,0x76,0x91
  8733. };
  8734. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8735. {
  8736. 0xc9,0x06,0x35
  8737. };
  8738. #endif /* WOLFSSL_AES_128 */
  8739. #ifdef WOLFSSL_AES_192
  8740. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8741. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  8742. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  8743. };
  8744. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8745. {
  8746. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  8747. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  8748. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  8749. };
  8750. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8751. {
  8752. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  8753. 0xb5,0xe8
  8754. };
  8755. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8756. {
  8757. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  8758. 0x3a,0x60
  8759. };
  8760. #endif
  8761. #ifdef WOLFSSL_AES_256
  8762. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8763. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  8764. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  8765. };
  8766. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8767. {
  8768. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  8769. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  8770. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  8771. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  8772. };
  8773. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8774. {
  8775. 0x1c,0xff,0x95
  8776. };
  8777. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8778. {
  8779. 0xb9,0x74,0xfa
  8780. };
  8781. #endif
  8782. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8783. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8784. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8785. #ifdef HAVE_AES_DECRYPT
  8786. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8787. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8788. #endif
  8789. #endif
  8790. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8791. if (ret != 0)
  8792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8793. else
  8794. enc_inited = 1;
  8795. #ifdef HAVE_AES_DECRYPT
  8796. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8797. if (ret != 0)
  8798. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8799. else
  8800. dec_inited = 1;
  8801. #endif
  8802. #ifdef WOLFSSL_AES_128
  8803. /* 128 key tests */
  8804. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8805. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8806. cipher1, sizeof(cipher1));
  8807. if (ret != 0) {
  8808. return ret;
  8809. }
  8810. #endif
  8811. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8812. if (ret != 0)
  8813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8814. #ifdef HAVE_AES_DECRYPT
  8815. /* decrypt uses AES_ENCRYPTION */
  8816. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8817. if (ret != 0)
  8818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8819. #endif
  8820. XMEMSET(cipher, 0, sizeof(cipher));
  8821. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8822. if (ret != 0)
  8823. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8824. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8825. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8826. #ifdef HAVE_AES_DECRYPT
  8827. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8828. if (ret != 0)
  8829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8830. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  8831. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8832. #endif /* HAVE_AES_DECRYPT */
  8833. #endif /* WOLFSSL_AES_128 */
  8834. #ifdef WOLFSSL_AES_192
  8835. /* 192 key tests */
  8836. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8837. if (ret != 0)
  8838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8839. XMEMSET(cipher, 0, sizeof(cipher));
  8840. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8841. if (ret != 0)
  8842. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8843. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8844. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8845. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8846. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8847. cipher2, sizeof(msg2));
  8848. if (ret != 0) {
  8849. return ret;
  8850. }
  8851. #endif
  8852. #endif /* WOLFSSL_AES_192 */
  8853. #ifdef WOLFSSL_AES_256
  8854. /* 256 key tests */
  8855. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8856. if (ret != 0)
  8857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8858. XMEMSET(cipher, 0, sizeof(cipher));
  8859. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8860. if (ret != 0)
  8861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8862. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8863. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8864. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8865. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8866. cipher3, sizeof(msg3));
  8867. if (ret != 0) {
  8868. goto out;
  8869. }
  8870. #endif
  8871. #endif /* WOLFSSL_AES_256 */
  8872. out:
  8873. if (enc_inited)
  8874. wc_AesFree(enc);
  8875. #ifdef HAVE_AES_DECRYPT
  8876. if (dec_inited)
  8877. wc_AesFree(dec);
  8878. #endif
  8879. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8880. if (enc)
  8881. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8882. #ifdef HAVE_AES_DECRYPT
  8883. if (dec)
  8884. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8885. #endif
  8886. #endif
  8887. return ret;
  8888. }
  8889. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8890. #endif /* WOLFSSL_AES_CFB */
  8891. static wc_test_ret_t aes_key_size_test(void)
  8892. {
  8893. wc_test_ret_t ret;
  8894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8895. Aes *aes;
  8896. #else
  8897. Aes aes[1];
  8898. #endif
  8899. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8900. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8901. #ifndef WOLFSSL_CRYPTOCELL
  8902. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8903. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8904. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8905. #endif
  8906. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8907. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8908. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8909. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8910. byte iv[] = "1234567890abcdef";
  8911. #ifndef HAVE_FIPS
  8912. word32 keySize;
  8913. #endif
  8914. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8915. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8916. return WC_TEST_RET_ENC_ERRNO;
  8917. #endif
  8918. #if !defined(HAVE_FIPS) || \
  8919. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8920. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8921. * supported with that FIPS version */
  8922. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8923. if (ret != BAD_FUNC_ARG)
  8924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8925. #endif
  8926. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8927. /* 0 check OK for FIPSv1 */
  8928. if (ret != 0)
  8929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8930. #ifndef HAVE_FIPS
  8931. /* Parameter Validation testing. */
  8932. ret = wc_AesGetKeySize(NULL, NULL);
  8933. if (ret != BAD_FUNC_ARG)
  8934. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8935. ret = wc_AesGetKeySize(aes, NULL);
  8936. if (ret != BAD_FUNC_ARG)
  8937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8938. ret = wc_AesGetKeySize(NULL, &keySize);
  8939. if (ret != BAD_FUNC_ARG)
  8940. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8941. /* Crashes in FIPS */
  8942. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8943. if (ret != BAD_FUNC_ARG)
  8944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8945. #endif
  8946. /* NULL IV indicates to use all zeros IV. */
  8947. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8948. #ifdef WOLFSSL_AES_128
  8949. if (ret != 0)
  8950. #else
  8951. if (ret != BAD_FUNC_ARG)
  8952. #endif
  8953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8954. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8955. if (ret != BAD_FUNC_ARG)
  8956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8957. /* CryptoCell handles rounds internally */
  8958. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8959. /* PSA don't use aes->rounds */
  8960. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8961. /* Force invalid rounds */
  8962. aes->rounds = 16;
  8963. ret = wc_AesGetKeySize(aes, &keySize);
  8964. if (ret != BAD_FUNC_ARG)
  8965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8966. #endif
  8967. #endif
  8968. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8969. #ifdef WOLFSSL_AES_128
  8970. if (ret != 0)
  8971. #else
  8972. if (ret != BAD_FUNC_ARG)
  8973. #endif
  8974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8975. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8976. ret = wc_AesGetKeySize(aes, &keySize);
  8977. if (ret != 0 || keySize != sizeof(key16))
  8978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8979. #endif
  8980. #ifndef WOLFSSL_CRYPTOCELL
  8981. /* Cryptocell only supports AES-128 key size */
  8982. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8983. #ifdef WOLFSSL_AES_192
  8984. if (ret != 0)
  8985. #else
  8986. if (ret != BAD_FUNC_ARG)
  8987. #endif
  8988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8989. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8990. ret = wc_AesGetKeySize(aes, &keySize);
  8991. if (ret != 0 || keySize != sizeof(key24))
  8992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8993. #endif
  8994. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8995. #ifdef WOLFSSL_AES_256
  8996. if (ret != 0)
  8997. #else
  8998. if (ret != BAD_FUNC_ARG)
  8999. #endif
  9000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9001. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  9002. ret = wc_AesGetKeySize(aes, &keySize);
  9003. if (ret != 0 || keySize != sizeof(key32))
  9004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9005. #endif
  9006. #endif /* !WOLFSSL_CRYPTOCELL */
  9007. ret = 0; /* success */
  9008. out:
  9009. wc_AesFree(aes);
  9010. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9011. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9012. #endif
  9013. return ret;
  9014. }
  9015. #if defined(WOLFSSL_AES_XTS) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  9016. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  9017. #ifdef WOLFSSL_AES_128
  9018. static wc_test_ret_t aes_xts_128_test(void)
  9019. {
  9020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9021. XtsAes *aes = NULL;
  9022. #else
  9023. XtsAes aes[1];
  9024. #endif
  9025. int aes_inited = 0;
  9026. wc_test_ret_t ret = 0;
  9027. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9028. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9029. #ifdef WOLFSSL_AESXTS_STREAM
  9030. struct XtsAesStreamData stream;
  9031. #endif
  9032. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9033. !defined(WOLFSSL_AFALG)
  9034. #define LARGE_XTS_SZ 1024
  9035. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9036. byte* large_input = NULL;
  9037. #else
  9038. byte large_input[LARGE_XTS_SZ];
  9039. #endif
  9040. #endif
  9041. /* 128 key tests */
  9042. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9043. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  9044. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  9045. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  9046. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  9047. };
  9048. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9049. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9050. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9051. };
  9052. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9053. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9054. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9055. };
  9056. /* plain text test of partial block is not from NIST test vector list */
  9057. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9058. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9059. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9060. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9061. };
  9062. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9063. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  9064. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  9065. };
  9066. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9067. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  9068. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  9069. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  9070. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  9071. };
  9072. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9073. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9074. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9075. };
  9076. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9077. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9078. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9079. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9080. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9081. };
  9082. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9083. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  9084. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  9085. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  9086. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  9087. };
  9088. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9089. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  9090. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  9091. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  9092. };
  9093. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9094. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9095. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9096. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9097. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9098. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9099. };
  9100. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9101. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9102. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9103. };
  9104. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9105. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9106. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9107. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9108. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9109. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9110. };
  9111. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9112. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  9113. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  9114. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  9115. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  9116. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  9117. };
  9118. #endif /* HAVE_FIPS */
  9119. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9120. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9121. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9122. #endif
  9123. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  9124. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9125. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  9126. if (ret != 0) {
  9127. printf("EVP_aes_128_xts failed!\n");
  9128. goto out;
  9129. }
  9130. #endif
  9131. XMEMSET(buf, 0, sizeof(buf));
  9132. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9133. if (ret != 0)
  9134. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9135. else
  9136. aes_inited = 1;
  9137. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9138. if (ret != 0)
  9139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9140. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9141. #if defined(WOLFSSL_ASYNC_CRYPT)
  9142. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9143. #endif
  9144. if (ret != 0)
  9145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9146. if (XMEMCMP(c2, buf, sizeof(c2)))
  9147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9148. #ifdef WOLFSSL_AESXTS_STREAM
  9149. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9150. #if defined(WOLFSSL_ASYNC_CRYPT)
  9151. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9152. #endif
  9153. if (ret != 0)
  9154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9155. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9156. #if defined(WOLFSSL_ASYNC_CRYPT)
  9157. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9158. #endif
  9159. if (ret != 0)
  9160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9161. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9162. #if defined(WOLFSSL_ASYNC_CRYPT)
  9163. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9164. #endif
  9165. if (ret != 0)
  9166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9167. if (XMEMCMP(c2, buf, sizeof(c2)))
  9168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9169. #endif /* WOLFSSL_AESXTS_STREAM */
  9170. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9171. defined(WC_C_DYNAMIC_FALLBACK)
  9172. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9173. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9174. #if defined(WOLFSSL_ASYNC_CRYPT)
  9175. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9176. #endif
  9177. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9178. if (ret != 0)
  9179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9180. if (XMEMCMP(c2, buf, sizeof(c2)))
  9181. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9182. #endif
  9183. XMEMSET(buf, 0, sizeof(buf));
  9184. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9185. if (ret != 0)
  9186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9187. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9188. #if defined(WOLFSSL_ASYNC_CRYPT)
  9189. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9190. #endif
  9191. if (ret != 0)
  9192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9193. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9194. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9195. #ifdef WOLFSSL_AESXTS_STREAM
  9196. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i2), &stream);
  9197. #if defined(WOLFSSL_ASYNC_CRYPT)
  9198. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9199. #endif
  9200. if (ret != 0)
  9201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9202. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9203. #if defined(WOLFSSL_ASYNC_CRYPT)
  9204. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9205. #endif
  9206. if (ret != 0)
  9207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9208. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9209. #if defined(WOLFSSL_ASYNC_CRYPT)
  9210. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9211. #endif
  9212. if (ret != 0)
  9213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9214. if (XMEMCMP(c1, buf, sizeof(c1)))
  9215. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9216. #endif /* WOLFSSL_AESXTS_STREAM */
  9217. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9218. defined(WC_C_DYNAMIC_FALLBACK)
  9219. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9220. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9221. #if defined(WOLFSSL_ASYNC_CRYPT)
  9222. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9223. #endif
  9224. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9225. if (ret != 0)
  9226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9227. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9228. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9229. #endif
  9230. /* partial block encryption test */
  9231. XMEMSET(cipher, 0, sizeof(cipher));
  9232. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9233. #if defined(WOLFSSL_ASYNC_CRYPT)
  9234. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9235. #endif
  9236. if (ret != 0)
  9237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9238. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9239. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9240. #ifdef WOLFSSL_AESXTS_STREAM
  9241. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9242. #if defined(WOLFSSL_ASYNC_CRYPT)
  9243. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9244. #endif
  9245. if (ret != 0)
  9246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9247. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9248. #if defined(WOLFSSL_ASYNC_CRYPT)
  9249. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9250. #endif
  9251. if (ret != 0)
  9252. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9253. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9254. #if defined(WOLFSSL_ASYNC_CRYPT)
  9255. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9256. #endif
  9257. if (ret != 0)
  9258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9259. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9260. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9261. #endif /* WOLFSSL_AESXTS_STREAM */
  9262. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9263. defined(WC_C_DYNAMIC_FALLBACK)
  9264. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9265. XMEMSET(cipher, 0, sizeof(cipher));
  9266. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9267. #if defined(WOLFSSL_ASYNC_CRYPT)
  9268. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9269. #endif
  9270. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9271. if (ret != 0)
  9272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9273. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9274. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9275. #endif
  9276. /* partial block decrypt test */
  9277. XMEMSET(buf, 0, sizeof(buf));
  9278. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9279. if (ret != 0)
  9280. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9281. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9282. #if defined(WOLFSSL_ASYNC_CRYPT)
  9283. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9284. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9285. #else
  9286. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9287. #endif
  9288. #endif
  9289. if (ret != 0)
  9290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9291. if (XMEMCMP(pp, buf, sizeof(pp)))
  9292. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9293. #ifdef WOLFSSL_AESXTS_STREAM
  9294. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9295. #if defined(WOLFSSL_ASYNC_CRYPT)
  9296. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9297. #endif
  9298. if (ret != 0)
  9299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9300. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9301. #if defined(WOLFSSL_ASYNC_CRYPT)
  9302. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9303. #endif
  9304. if (ret != 0)
  9305. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9306. if (XMEMCMP(pp, buf, sizeof(pp)))
  9307. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9308. #endif /* WOLFSSL_AESXTS_STREAM */
  9309. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9310. defined(WC_C_DYNAMIC_FALLBACK)
  9311. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9312. XMEMSET(buf, 0, sizeof(buf));
  9313. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9314. #if defined(WOLFSSL_ASYNC_CRYPT)
  9315. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9316. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9317. #else
  9318. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9319. #endif
  9320. #endif
  9321. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9322. if (ret != 0)
  9323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9324. if (XMEMCMP(pp, buf, sizeof(pp)))
  9325. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9326. #endif
  9327. /* NIST decrypt test vector */
  9328. XMEMSET(buf, 0, sizeof(buf));
  9329. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9330. #if defined(WOLFSSL_ASYNC_CRYPT)
  9331. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9332. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9333. #else
  9334. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9335. #endif
  9336. #endif
  9337. if (ret != 0)
  9338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9339. if (XMEMCMP(p1, buf, sizeof(p1)))
  9340. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9341. #ifdef WOLFSSL_AESXTS_STREAM
  9342. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9343. #if defined(WOLFSSL_ASYNC_CRYPT)
  9344. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9345. #endif
  9346. if (ret != 0)
  9347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9348. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9349. #if defined(WOLFSSL_ASYNC_CRYPT)
  9350. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9351. #endif
  9352. if (ret != 0)
  9353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9354. if (XMEMCMP(p1, buf, sizeof(p1)))
  9355. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9356. #endif /* WOLFSSL_AESXTS_STREAM */
  9357. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9358. defined(WC_C_DYNAMIC_FALLBACK)
  9359. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9360. XMEMSET(buf, 0, sizeof(buf));
  9361. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9362. #if defined(WOLFSSL_ASYNC_CRYPT)
  9363. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9364. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9365. #else
  9366. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9367. #endif
  9368. #endif
  9369. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9370. if (ret != 0)
  9371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9372. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9373. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9374. #endif
  9375. /* fail case with decrypting using wrong key */
  9376. XMEMSET(buf, 0, sizeof(buf));
  9377. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9378. #if defined(WOLFSSL_ASYNC_CRYPT)
  9379. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9380. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9381. #else
  9382. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9383. #endif
  9384. #endif
  9385. if (ret != 0)
  9386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9387. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  9388. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9389. /* set correct key and retest */
  9390. XMEMSET(buf, 0, sizeof(buf));
  9391. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9392. if (ret != 0)
  9393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9394. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9395. #if defined(WOLFSSL_ASYNC_CRYPT)
  9396. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9397. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9398. #else
  9399. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9400. #endif
  9401. #endif
  9402. if (ret != 0)
  9403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9404. if (XMEMCMP(p2, buf, sizeof(p2)))
  9405. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9406. #ifndef HAVE_FIPS
  9407. /* Test ciphertext stealing in-place. */
  9408. XMEMCPY(buf, p3, sizeof(p3));
  9409. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  9410. if (ret != 0)
  9411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9412. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  9413. #if defined(WOLFSSL_ASYNC_CRYPT)
  9414. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9415. #endif
  9416. if (ret != 0)
  9417. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9418. if (XMEMCMP(c3, buf, sizeof(c3)))
  9419. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9420. #ifdef WOLFSSL_AESXTS_STREAM
  9421. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  9422. #if defined(WOLFSSL_ASYNC_CRYPT)
  9423. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9424. #endif
  9425. if (ret != 0)
  9426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9427. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  9428. #if defined(WOLFSSL_ASYNC_CRYPT)
  9429. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9430. #endif
  9431. if (ret != 0)
  9432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9433. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  9434. #if defined(WOLFSSL_ASYNC_CRYPT)
  9435. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9436. #endif
  9437. if (ret != 0)
  9438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9439. if (XMEMCMP(c3, buf, sizeof(c3)))
  9440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9441. #endif /* WOLFSSL_AESXTS_STREAM */
  9442. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  9443. if (ret != 0)
  9444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9445. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  9446. #if defined(WOLFSSL_ASYNC_CRYPT)
  9447. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9448. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9449. #else
  9450. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9451. #endif
  9452. #endif
  9453. if (ret != 0)
  9454. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9455. if (XMEMCMP(p3, buf, sizeof(p3)))
  9456. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9457. #ifdef WOLFSSL_AESXTS_STREAM
  9458. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  9459. #if defined(WOLFSSL_ASYNC_CRYPT)
  9460. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9461. #endif
  9462. if (ret != 0)
  9463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9464. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  9465. #if defined(WOLFSSL_ASYNC_CRYPT)
  9466. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9467. #endif
  9468. if (ret != 0)
  9469. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9470. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  9471. #if defined(WOLFSSL_ASYNC_CRYPT)
  9472. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9473. #endif
  9474. if (ret != 0)
  9475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9476. if (XMEMCMP(p3, buf, sizeof(p3)))
  9477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9478. #endif /* WOLFSSL_AESXTS_STREAM */
  9479. #endif /* !HAVE_FIPS */
  9480. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9481. !defined(WOLFSSL_AFALG)
  9482. {
  9483. int i;
  9484. int j;
  9485. #ifdef WOLFSSL_AESXTS_STREAM
  9486. int k;
  9487. #endif
  9488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9489. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  9490. DYNAMIC_TYPE_TMP_BUFFER);
  9491. if (large_input == NULL)
  9492. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  9493. #endif
  9494. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9495. large_input[i] = (byte)i;
  9496. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9497. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9498. if (ret != 0)
  9499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9500. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9501. sizeof(i1));
  9502. #if defined(WOLFSSL_ASYNC_CRYPT)
  9503. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9504. #endif
  9505. if (ret != 0)
  9506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9507. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9508. if (ret != 0)
  9509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9510. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9511. sizeof(i1));
  9512. #if defined(WOLFSSL_ASYNC_CRYPT)
  9513. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9514. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9515. WC_ASYNC_FLAG_NONE);
  9516. #else
  9517. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9518. #endif
  9519. #endif
  9520. if (ret != 0)
  9521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9522. for (i = 0; i < j; i++) {
  9523. if (large_input[i] != (byte)i) {
  9524. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9525. }
  9526. }
  9527. }
  9528. #ifdef WOLFSSL_AESXTS_STREAM
  9529. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9530. large_input[i] = (byte)i;
  9531. /* first, encrypt block by block then decrypt with a one-shot call. */
  9532. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9533. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9534. if (ret != 0)
  9535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9536. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9537. #if defined(WOLFSSL_ASYNC_CRYPT)
  9538. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9539. #endif
  9540. if (ret != 0)
  9541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9542. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9543. if ((j - k) < AES_BLOCK_SIZE*2)
  9544. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9545. else
  9546. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9547. #if defined(WOLFSSL_ASYNC_CRYPT)
  9548. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9549. #endif
  9550. if (ret != 0)
  9551. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9552. if ((j - k) < AES_BLOCK_SIZE*2)
  9553. break;
  9554. }
  9555. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9556. if (ret != 0)
  9557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9558. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9559. sizeof(i1));
  9560. #if defined(WOLFSSL_ASYNC_CRYPT)
  9561. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9562. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9563. WC_ASYNC_FLAG_NONE);
  9564. #else
  9565. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9566. #endif
  9567. #endif
  9568. if (ret != 0)
  9569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9570. for (i = 0; i < j; i++) {
  9571. if (large_input[i] != (byte)i) {
  9572. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9573. }
  9574. }
  9575. }
  9576. /* second, encrypt with a one-shot call then decrypt block by block. */
  9577. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9578. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9579. if (ret != 0)
  9580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9581. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9582. sizeof(i1));
  9583. #if defined(WOLFSSL_ASYNC_CRYPT)
  9584. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9585. #endif
  9586. if (ret != 0)
  9587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9588. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9589. if (ret != 0)
  9590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9591. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9592. #if defined(WOLFSSL_ASYNC_CRYPT)
  9593. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9594. #endif
  9595. if (ret != 0)
  9596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9597. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9598. if ((j - k) < AES_BLOCK_SIZE*2)
  9599. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9600. else
  9601. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9602. #if defined(WOLFSSL_ASYNC_CRYPT)
  9603. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9604. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9605. WC_ASYNC_FLAG_NONE);
  9606. #else
  9607. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9608. #endif
  9609. #endif
  9610. if (ret != 0)
  9611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9612. if ((j - k) < AES_BLOCK_SIZE*2)
  9613. break;
  9614. }
  9615. for (i = 0; i < j; i++) {
  9616. if (large_input[i] != (byte)i) {
  9617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9618. }
  9619. }
  9620. }
  9621. #endif /* WOLFSSL_AESXTS_STREAM */
  9622. }
  9623. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  9624. * !WOLFSSL_AFALG
  9625. */
  9626. out:
  9627. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9628. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  9629. !defined(WOLFSSL_NO_MALLOC)
  9630. if (large_input)
  9631. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9632. #endif
  9633. if (aes_inited)
  9634. wc_AesXtsFree(aes);
  9635. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9636. if (aes)
  9637. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9638. #endif
  9639. return ret;
  9640. }
  9641. #endif /* WOLFSSL_AES_128 */
  9642. #ifdef WOLFSSL_AES_192
  9643. static wc_test_ret_t aes_xts_192_test(void)
  9644. {
  9645. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9646. XtsAes *aes = NULL;
  9647. #else
  9648. XtsAes aes[1];
  9649. #endif
  9650. int aes_inited = 0;
  9651. wc_test_ret_t ret = 0;
  9652. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9653. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9654. #ifdef WOLFSSL_AESXTS_STREAM
  9655. struct XtsAesStreamData stream;
  9656. #endif
  9657. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9658. !defined(WOLFSSL_AFALG)
  9659. #define LARGE_XTS_SZ 1024
  9660. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9661. byte* large_input = NULL;
  9662. #else
  9663. byte large_input[LARGE_XTS_SZ];
  9664. #endif
  9665. #endif
  9666. /* 192 bit key tests */
  9667. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9668. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  9669. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  9670. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  9671. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  9672. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  9673. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3
  9674. };
  9675. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9676. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9677. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9678. };
  9679. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9680. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9681. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9682. };
  9683. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9684. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9685. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9686. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9687. };
  9688. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9689. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4,
  9690. 0xdb, 0x4e, 0xd3, 0x69, 0xdf, 0x8e, 0x3a, 0xe0
  9691. };
  9692. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9693. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  9694. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  9695. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  9696. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  9697. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  9698. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a
  9699. };
  9700. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9701. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9702. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9703. };
  9704. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9705. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9706. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9707. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9708. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9709. };
  9710. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9711. 0x6c, 0xa6, 0xb5, 0x73, 0x48, 0xf1, 0x89, 0xfa,
  9712. 0xdd, 0x80, 0x72, 0x1f, 0xb8, 0x56, 0x0c, 0xa2,
  9713. 0x35, 0xd4, 0x08, 0xbf, 0x24, 0xcb, 0xec, 0xdb,
  9714. 0x81, 0xe0, 0xe6, 0x4f, 0x3d, 0x1c, 0x5c, 0x46
  9715. };
  9716. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9717. 0xe9, 0x58, 0xfe, 0xab, 0x66, 0xb4, 0xf1, 0x79,
  9718. 0x91, 0x3f, 0x91, 0xdc, 0x6f, 0xdf, 0xd6, 0xac,
  9719. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4
  9720. };
  9721. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9722. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9723. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9724. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9725. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9726. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9727. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9728. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9729. };
  9730. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9731. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9732. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9733. };
  9734. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9735. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9736. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9737. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9738. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9739. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9740. };
  9741. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9742. 0xa4, 0xf2, 0x71, 0x5d, 0x80, 0x60, 0x68, 0xa0,
  9743. 0x80, 0x61, 0xd7, 0xc1, 0x55, 0xc8, 0x3a, 0x2e,
  9744. 0xd7, 0xf4, 0x62, 0xaf, 0xbd, 0x2d, 0xf9, 0x5f,
  9745. 0xe8, 0xc5, 0x99, 0x3d, 0x58, 0x3c, 0xeb, 0xba,
  9746. 0x86, 0xea, 0x2c, 0x7e, 0x1f, 0xba, 0x81, 0xde
  9747. };
  9748. #endif /* HAVE_FIPS */
  9749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9750. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9751. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9752. #endif
  9753. XMEMSET(buf, 0, sizeof(buf));
  9754. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9755. if (ret != 0)
  9756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9757. else
  9758. aes_inited = 1;
  9759. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9760. if (ret != 0)
  9761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9762. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9763. #if defined(WOLFSSL_ASYNC_CRYPT)
  9764. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9765. #endif
  9766. if (ret != 0)
  9767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9768. if (XMEMCMP(c2, buf, sizeof(c2)))
  9769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9770. #ifdef WOLFSSL_AESXTS_STREAM
  9771. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9772. #if defined(WOLFSSL_ASYNC_CRYPT)
  9773. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9774. #endif
  9775. if (ret != 0)
  9776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9777. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9778. #if defined(WOLFSSL_ASYNC_CRYPT)
  9779. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9780. #endif
  9781. if (ret != 0)
  9782. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9783. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9784. #if defined(WOLFSSL_ASYNC_CRYPT)
  9785. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9786. #endif
  9787. if (ret != 0)
  9788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9789. if (XMEMCMP(c2, buf, sizeof(c2)))
  9790. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9791. #endif /* WOLFSSL_AESXTS_STREAM */
  9792. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9793. defined(WC_C_DYNAMIC_FALLBACK)
  9794. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9795. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9796. #if defined(WOLFSSL_ASYNC_CRYPT)
  9797. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9798. #endif
  9799. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9800. if (ret != 0)
  9801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9802. if (XMEMCMP(c2, buf, sizeof(c2)))
  9803. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9804. #endif
  9805. XMEMSET(buf, 0, sizeof(buf));
  9806. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9807. if (ret != 0)
  9808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9809. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9810. #if defined(WOLFSSL_ASYNC_CRYPT)
  9811. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9812. #endif
  9813. if (ret != 0)
  9814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9815. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9816. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9817. #ifdef WOLFSSL_AESXTS_STREAM
  9818. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9819. #if defined(WOLFSSL_ASYNC_CRYPT)
  9820. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9821. #endif
  9822. if (ret != 0)
  9823. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9824. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9825. #if defined(WOLFSSL_ASYNC_CRYPT)
  9826. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9827. #endif
  9828. if (ret != 0)
  9829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9830. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9831. #if defined(WOLFSSL_ASYNC_CRYPT)
  9832. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9833. #endif
  9834. if (ret != 0)
  9835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9836. if (XMEMCMP(c1, buf, sizeof(c1)))
  9837. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9838. #endif /* WOLFSSL_AESXTS_STREAM */
  9839. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9840. defined(WC_C_DYNAMIC_FALLBACK)
  9841. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9842. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9843. #if defined(WOLFSSL_ASYNC_CRYPT)
  9844. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9845. #endif
  9846. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9847. if (ret != 0)
  9848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9849. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9850. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9851. #endif
  9852. /* partial block encryption test */
  9853. XMEMSET(cipher, 0, sizeof(cipher));
  9854. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9855. #if defined(WOLFSSL_ASYNC_CRYPT)
  9856. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9857. #endif
  9858. if (ret != 0)
  9859. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9860. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9861. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9862. #ifdef WOLFSSL_AESXTS_STREAM
  9863. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9864. #if defined(WOLFSSL_ASYNC_CRYPT)
  9865. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9866. #endif
  9867. if (ret != 0)
  9868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9869. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9870. #if defined(WOLFSSL_ASYNC_CRYPT)
  9871. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9872. #endif
  9873. if (ret != 0)
  9874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9875. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9876. #if defined(WOLFSSL_ASYNC_CRYPT)
  9877. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9878. #endif
  9879. if (ret != 0)
  9880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9881. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9882. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9883. #endif /* WOLFSSL_AESXTS_STREAM */
  9884. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9885. defined(WC_C_DYNAMIC_FALLBACK)
  9886. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9887. XMEMSET(cipher, 0, sizeof(cipher));
  9888. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9889. #if defined(WOLFSSL_ASYNC_CRYPT)
  9890. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9891. #endif
  9892. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9893. if (ret != 0)
  9894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9895. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9896. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9897. #endif
  9898. /* partial block decrypt test */
  9899. XMEMSET(buf, 0, sizeof(buf));
  9900. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9901. if (ret != 0)
  9902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9903. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9904. #if defined(WOLFSSL_ASYNC_CRYPT)
  9905. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9906. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9907. #else
  9908. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9909. #endif
  9910. #endif
  9911. if (ret != 0)
  9912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9913. if (XMEMCMP(pp, buf, sizeof(pp)))
  9914. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9915. #ifdef WOLFSSL_AESXTS_STREAM
  9916. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9917. #if defined(WOLFSSL_ASYNC_CRYPT)
  9918. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9919. #endif
  9920. if (ret != 0)
  9921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9922. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9923. #if defined(WOLFSSL_ASYNC_CRYPT)
  9924. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9925. #endif
  9926. if (ret != 0)
  9927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9928. if (XMEMCMP(pp, buf, sizeof(pp)))
  9929. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9930. #endif /* WOLFSSL_AESXTS_STREAM */
  9931. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9932. defined(WC_C_DYNAMIC_FALLBACK)
  9933. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9934. XMEMSET(buf, 0, sizeof(buf));
  9935. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9936. #if defined(WOLFSSL_ASYNC_CRYPT)
  9937. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9938. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9939. #else
  9940. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9941. #endif
  9942. #endif
  9943. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9944. if (ret != 0)
  9945. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9946. if (XMEMCMP(pp, buf, sizeof(pp)))
  9947. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9948. #endif
  9949. /* NIST decrypt test vector */
  9950. XMEMSET(buf, 0, sizeof(buf));
  9951. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9952. #if defined(WOLFSSL_ASYNC_CRYPT)
  9953. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9954. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9955. #else
  9956. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9957. #endif
  9958. #endif
  9959. if (ret != 0)
  9960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9961. if (XMEMCMP(p1, buf, sizeof(p1)))
  9962. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9963. #ifdef WOLFSSL_AESXTS_STREAM
  9964. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9965. #if defined(WOLFSSL_ASYNC_CRYPT)
  9966. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9967. #endif
  9968. if (ret != 0)
  9969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9970. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9971. #if defined(WOLFSSL_ASYNC_CRYPT)
  9972. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9973. #endif
  9974. if (ret != 0)
  9975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9976. if (XMEMCMP(p1, buf, sizeof(p1)))
  9977. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9978. #endif /* WOLFSSL_AESXTS_STREAM */
  9979. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9980. defined(WC_C_DYNAMIC_FALLBACK)
  9981. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9982. XMEMSET(buf, 0, sizeof(buf));
  9983. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9984. #if defined(WOLFSSL_ASYNC_CRYPT)
  9985. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9986. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9987. #else
  9988. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9989. #endif
  9990. #endif
  9991. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9992. if (ret != 0)
  9993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9994. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9995. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9996. #endif
  9997. /* fail case with decrypting using wrong key */
  9998. XMEMSET(buf, 0, sizeof(buf));
  9999. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10000. #if defined(WOLFSSL_ASYNC_CRYPT)
  10001. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10002. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10003. #else
  10004. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10005. #endif
  10006. #endif
  10007. if (ret != 0)
  10008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10009. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  10010. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10011. /* set correct key and retest */
  10012. XMEMSET(buf, 0, sizeof(buf));
  10013. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10014. if (ret != 0)
  10015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10016. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10017. #if defined(WOLFSSL_ASYNC_CRYPT)
  10018. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10019. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10020. #else
  10021. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10022. #endif
  10023. #endif
  10024. if (ret != 0)
  10025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10026. if (XMEMCMP(p2, buf, sizeof(p2)))
  10027. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10028. #ifndef HAVE_FIPS
  10029. /* Test ciphertext stealing in-place. */
  10030. XMEMCPY(buf, p3, sizeof(p3));
  10031. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10032. if (ret != 0)
  10033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10034. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  10035. #if defined(WOLFSSL_ASYNC_CRYPT)
  10036. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10037. #endif
  10038. if (ret != 0)
  10039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10040. if (XMEMCMP(c3, buf, sizeof(c3)))
  10041. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10042. #ifdef WOLFSSL_AESXTS_STREAM
  10043. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  10044. #if defined(WOLFSSL_ASYNC_CRYPT)
  10045. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10046. #endif
  10047. if (ret != 0)
  10048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10049. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  10050. #if defined(WOLFSSL_ASYNC_CRYPT)
  10051. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10052. #endif
  10053. if (ret != 0)
  10054. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10055. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  10056. #if defined(WOLFSSL_ASYNC_CRYPT)
  10057. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10058. #endif
  10059. if (ret != 0)
  10060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10061. if (XMEMCMP(c3, buf, sizeof(c3)))
  10062. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10063. #endif /* WOLFSSL_AESXTS_STREAM */
  10064. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10065. if (ret != 0)
  10066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10067. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  10068. #if defined(WOLFSSL_ASYNC_CRYPT)
  10069. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10070. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10071. #else
  10072. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10073. #endif
  10074. #endif
  10075. if (ret != 0)
  10076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10077. if (XMEMCMP(p3, buf, sizeof(p3)))
  10078. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10079. #ifdef WOLFSSL_AESXTS_STREAM
  10080. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  10081. #if defined(WOLFSSL_ASYNC_CRYPT)
  10082. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10083. #endif
  10084. if (ret != 0)
  10085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10086. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  10087. #if defined(WOLFSSL_ASYNC_CRYPT)
  10088. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10089. #endif
  10090. if (ret != 0)
  10091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10092. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  10093. #if defined(WOLFSSL_ASYNC_CRYPT)
  10094. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10095. #endif
  10096. if (ret != 0)
  10097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10098. if (XMEMCMP(p3, buf, sizeof(p3)))
  10099. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10100. #endif /* WOLFSSL_AESXTS_STREAM */
  10101. #endif /* !HAVE_FIPS */
  10102. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10103. !defined(WOLFSSL_AFALG)
  10104. {
  10105. int i;
  10106. int j;
  10107. #ifdef WOLFSSL_AESXTS_STREAM
  10108. int k;
  10109. #endif
  10110. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10111. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10112. DYNAMIC_TYPE_TMP_BUFFER);
  10113. if (large_input == NULL)
  10114. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10115. #endif
  10116. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10117. large_input[i] = (byte)i;
  10118. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10119. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10120. if (ret != 0)
  10121. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10122. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10123. sizeof(i1));
  10124. #if defined(WOLFSSL_ASYNC_CRYPT)
  10125. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10126. #endif
  10127. if (ret != 0)
  10128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10129. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10130. if (ret != 0)
  10131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10132. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10133. sizeof(i1));
  10134. #if defined(WOLFSSL_ASYNC_CRYPT)
  10135. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10136. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10137. WC_ASYNC_FLAG_NONE);
  10138. #else
  10139. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10140. #endif
  10141. #endif
  10142. if (ret != 0)
  10143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10144. for (i = 0; i < j; i++) {
  10145. if (large_input[i] != (byte)i) {
  10146. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10147. }
  10148. }
  10149. }
  10150. #ifdef WOLFSSL_AESXTS_STREAM
  10151. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10152. large_input[i] = (byte)i;
  10153. /* first, encrypt block by block then decrypt with a one-shot call. */
  10154. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10155. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10156. if (ret != 0)
  10157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10158. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10159. #if defined(WOLFSSL_ASYNC_CRYPT)
  10160. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10161. #endif
  10162. if (ret != 0)
  10163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10164. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10165. if ((j - k) < AES_BLOCK_SIZE*2)
  10166. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10167. else
  10168. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10169. #if defined(WOLFSSL_ASYNC_CRYPT)
  10170. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10171. #endif
  10172. if (ret != 0)
  10173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10174. if ((j - k) < AES_BLOCK_SIZE*2)
  10175. break;
  10176. }
  10177. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10178. if (ret != 0)
  10179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10180. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10181. sizeof(i1));
  10182. #if defined(WOLFSSL_ASYNC_CRYPT)
  10183. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10184. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10185. WC_ASYNC_FLAG_NONE);
  10186. #else
  10187. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10188. #endif
  10189. #endif
  10190. if (ret != 0)
  10191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10192. for (i = 0; i < j; i++) {
  10193. if (large_input[i] != (byte)i) {
  10194. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10195. }
  10196. }
  10197. }
  10198. /* second, encrypt with a one-shot call then decrypt block by block. */
  10199. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10200. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10201. if (ret != 0)
  10202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10203. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10204. sizeof(i1));
  10205. #if defined(WOLFSSL_ASYNC_CRYPT)
  10206. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10207. #endif
  10208. if (ret != 0)
  10209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10210. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10211. if (ret != 0)
  10212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10213. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10214. #if defined(WOLFSSL_ASYNC_CRYPT)
  10215. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10216. #endif
  10217. if (ret != 0)
  10218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10219. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10220. if ((j - k) < AES_BLOCK_SIZE*2)
  10221. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10222. else
  10223. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10224. #if defined(WOLFSSL_ASYNC_CRYPT)
  10225. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10226. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10227. WC_ASYNC_FLAG_NONE);
  10228. #else
  10229. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10230. #endif
  10231. #endif
  10232. if (ret != 0)
  10233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10234. if ((j - k) < AES_BLOCK_SIZE*2)
  10235. break;
  10236. }
  10237. for (i = 0; i < j; i++) {
  10238. if (large_input[i] != (byte)i) {
  10239. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10240. }
  10241. }
  10242. }
  10243. #endif /* WOLFSSL_AESXTS_STREAM */
  10244. }
  10245. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10246. * !WOLFSSL_AFALG
  10247. */
  10248. out:
  10249. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10250. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10251. !defined(WOLFSSL_NO_MALLOC)
  10252. if (large_input)
  10253. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10254. #endif
  10255. if (aes_inited)
  10256. wc_AesXtsFree(aes);
  10257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10258. if (aes)
  10259. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10260. #endif
  10261. return ret;
  10262. }
  10263. #endif /* WOLFSSL_AES_192 */
  10264. #ifdef WOLFSSL_AES_256
  10265. static wc_test_ret_t aes_xts_256_test(void)
  10266. {
  10267. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10268. XtsAes *aes = NULL;
  10269. #else
  10270. XtsAes aes[1];
  10271. #endif
  10272. int aes_inited = 0;
  10273. wc_test_ret_t ret = 0;
  10274. unsigned char buf[AES_BLOCK_SIZE * 3];
  10275. unsigned char cipher[AES_BLOCK_SIZE * 3];
  10276. #ifdef WOLFSSL_AESXTS_STREAM
  10277. struct XtsAesStreamData stream;
  10278. #endif
  10279. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10280. !defined(WOLFSSL_AFALG)
  10281. #define LARGE_XTS_SZ 1024
  10282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10283. byte* large_input = NULL;
  10284. #else
  10285. byte large_input[LARGE_XTS_SZ];
  10286. #endif
  10287. #endif
  10288. /* 256 key tests */
  10289. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10290. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  10291. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  10292. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  10293. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  10294. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  10295. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  10296. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  10297. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  10298. };
  10299. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  10300. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  10301. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  10302. };
  10303. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10304. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  10305. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  10306. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  10307. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  10308. };
  10309. /* plain text test of partial block is not from NIST test vector list */
  10310. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  10311. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  10312. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  10313. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10316. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  10317. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  10318. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  10319. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  10320. };
  10321. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10322. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  10323. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  10324. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  10325. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  10326. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  10327. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  10328. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  10329. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  10330. };
  10331. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  10332. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  10333. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  10334. };
  10335. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10336. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  10337. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  10338. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  10339. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  10340. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  10341. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  10342. };
  10343. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10344. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  10345. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  10346. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  10347. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  10348. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  10349. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  10350. };
  10351. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10352. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10353. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10354. #endif
  10355. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  10356. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10357. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  10358. if (ret != 0) {
  10359. printf("EVP_aes_256_xts failed\n");
  10360. goto out;
  10361. }
  10362. #endif
  10363. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10364. if (ret != 0)
  10365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10366. else
  10367. aes_inited = 1;
  10368. XMEMSET(buf, 0, sizeof(buf));
  10369. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10370. if (ret != 0)
  10371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10372. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  10373. #if defined(WOLFSSL_ASYNC_CRYPT)
  10374. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10375. #endif
  10376. if (ret != 0)
  10377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10378. if (XMEMCMP(c2, buf, sizeof(c2)))
  10379. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10380. #ifdef WOLFSSL_AESXTS_STREAM
  10381. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  10382. #if defined(WOLFSSL_ASYNC_CRYPT)
  10383. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10384. #endif
  10385. if (ret != 0)
  10386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10387. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  10388. #if defined(WOLFSSL_ASYNC_CRYPT)
  10389. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10390. #endif
  10391. if (ret != 0)
  10392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10393. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  10394. #if defined(WOLFSSL_ASYNC_CRYPT)
  10395. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10396. #endif
  10397. if (ret != 0)
  10398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10399. if (XMEMCMP(c2, buf, sizeof(c2)))
  10400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10401. #endif /* WOLFSSL_AESXTS_STREAM */
  10402. XMEMSET(buf, 0, sizeof(buf));
  10403. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10404. if (ret != 0)
  10405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10406. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  10407. #if defined(WOLFSSL_ASYNC_CRYPT)
  10408. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10409. #endif
  10410. if (ret != 0)
  10411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10412. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10413. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10414. #ifdef WOLFSSL_AESXTS_STREAM
  10415. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10416. #if defined(WOLFSSL_ASYNC_CRYPT)
  10417. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10418. #endif
  10419. if (ret != 0)
  10420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10421. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  10422. #if defined(WOLFSSL_ASYNC_CRYPT)
  10423. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10424. #endif
  10425. if (ret != 0)
  10426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10427. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  10428. #if defined(WOLFSSL_ASYNC_CRYPT)
  10429. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10430. #endif
  10431. if (ret != 0)
  10432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10433. if (XMEMCMP(c1, buf, sizeof(c1)))
  10434. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10435. #endif /* WOLFSSL_AESXTS_STREAM */
  10436. /* partial block encryption test */
  10437. XMEMSET(cipher, 0, sizeof(cipher));
  10438. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  10439. #if defined(WOLFSSL_ASYNC_CRYPT)
  10440. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10441. #endif
  10442. if (ret != 0)
  10443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10444. /* partial block decrypt test */
  10445. XMEMSET(buf, 0, sizeof(buf));
  10446. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10447. if (ret != 0)
  10448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10449. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  10450. #if defined(WOLFSSL_ASYNC_CRYPT)
  10451. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10452. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10453. #else
  10454. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10455. #endif
  10456. #endif
  10457. if (ret != 0)
  10458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10459. if (XMEMCMP(pp, buf, sizeof(pp)))
  10460. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10461. /* NIST decrypt test vector */
  10462. XMEMSET(buf, 0, sizeof(buf));
  10463. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  10464. #if defined(WOLFSSL_ASYNC_CRYPT)
  10465. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10466. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10467. #else
  10468. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10469. #endif
  10470. #endif
  10471. if (ret != 0)
  10472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10473. if (XMEMCMP(p1, buf, sizeof(p1)))
  10474. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10475. #ifdef WOLFSSL_AESXTS_STREAM
  10476. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10477. #if defined(WOLFSSL_ASYNC_CRYPT)
  10478. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10479. #endif
  10480. if (ret != 0)
  10481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10482. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  10483. #if defined(WOLFSSL_ASYNC_CRYPT)
  10484. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10485. #endif
  10486. if (ret != 0)
  10487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10488. if (XMEMCMP(p1, buf, sizeof(p1)))
  10489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10490. #endif /* WOLFSSL_AESXTS_STREAM */
  10491. XMEMSET(buf, 0, sizeof(buf));
  10492. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10493. if (ret != 0)
  10494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10495. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10496. #if defined(WOLFSSL_ASYNC_CRYPT)
  10497. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10498. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10499. #else
  10500. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10501. #endif
  10502. #endif
  10503. if (ret != 0)
  10504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10505. if (XMEMCMP(p2, buf, sizeof(p2)))
  10506. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10507. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10508. !defined(WOLFSSL_AFALG)
  10509. {
  10510. int i;
  10511. int j;
  10512. #ifdef WOLFSSL_AESXTS_STREAM
  10513. int k;
  10514. #endif
  10515. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10516. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10517. DYNAMIC_TYPE_TMP_BUFFER);
  10518. if (large_input == NULL)
  10519. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10520. #endif
  10521. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10522. large_input[i] = (byte)i;
  10523. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10524. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10525. if (ret != 0)
  10526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10527. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10528. sizeof(i1));
  10529. #if defined(WOLFSSL_ASYNC_CRYPT)
  10530. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10531. #endif
  10532. if (ret != 0)
  10533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10534. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10535. if (ret != 0)
  10536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10537. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10538. sizeof(i1));
  10539. #if defined(WOLFSSL_ASYNC_CRYPT)
  10540. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10541. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10542. WC_ASYNC_FLAG_NONE);
  10543. #else
  10544. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10545. #endif
  10546. #endif
  10547. if (ret != 0)
  10548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10549. for (i = 0; i < j; i++) {
  10550. if (large_input[i] != (byte)i) {
  10551. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10552. }
  10553. }
  10554. }
  10555. #ifdef WOLFSSL_AESXTS_STREAM
  10556. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10557. large_input[i] = (byte)i;
  10558. /* first, encrypt block by block then decrypt with a one-shot call. */
  10559. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10560. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10561. if (ret != 0)
  10562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10563. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10564. #if defined(WOLFSSL_ASYNC_CRYPT)
  10565. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10566. #endif
  10567. if (ret != 0)
  10568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10569. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10570. if ((j - k) < AES_BLOCK_SIZE*2)
  10571. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10572. else
  10573. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10574. #if defined(WOLFSSL_ASYNC_CRYPT)
  10575. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10576. #endif
  10577. if (ret != 0)
  10578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10579. if ((j - k) < AES_BLOCK_SIZE*2)
  10580. break;
  10581. }
  10582. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10583. if (ret != 0)
  10584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10585. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10586. sizeof(i1));
  10587. #if defined(WOLFSSL_ASYNC_CRYPT)
  10588. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10589. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10590. WC_ASYNC_FLAG_NONE);
  10591. #else
  10592. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10593. #endif
  10594. #endif
  10595. if (ret != 0)
  10596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10597. for (i = 0; i < j; i++) {
  10598. if (large_input[i] != (byte)i) {
  10599. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10600. }
  10601. }
  10602. }
  10603. /* second, encrypt with a one-shot call then decrypt block by block. */
  10604. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10605. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10606. if (ret != 0)
  10607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10608. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10609. sizeof(i1));
  10610. #if defined(WOLFSSL_ASYNC_CRYPT)
  10611. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10612. #endif
  10613. if (ret != 0)
  10614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10615. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10616. if (ret != 0)
  10617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10618. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10619. #if defined(WOLFSSL_ASYNC_CRYPT)
  10620. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10621. #endif
  10622. if (ret != 0)
  10623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10624. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10625. if ((j - k) < AES_BLOCK_SIZE*2)
  10626. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10627. else
  10628. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10629. #if defined(WOLFSSL_ASYNC_CRYPT)
  10630. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10631. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10632. WC_ASYNC_FLAG_NONE);
  10633. #else
  10634. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10635. #endif
  10636. #endif
  10637. if (ret != 0)
  10638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10639. if ((j - k) < AES_BLOCK_SIZE*2)
  10640. break;
  10641. }
  10642. for (i = 0; i < j; i++) {
  10643. if (large_input[i] != (byte)i) {
  10644. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10645. }
  10646. }
  10647. }
  10648. #endif /* WOLFSSL_AESXTS_STREAM */
  10649. }
  10650. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10651. * !WOLFSSL_AFALG
  10652. */
  10653. out:
  10654. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10655. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10656. !defined(WOLFSSL_NO_MALLOC)
  10657. if (large_input)
  10658. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10659. #endif
  10660. if (aes_inited)
  10661. wc_AesXtsFree(aes);
  10662. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10663. if (aes)
  10664. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10665. #endif
  10666. return ret;
  10667. }
  10668. #endif /* WOLFSSL_AES_256 */
  10669. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10670. /* both 128 and 256 bit key test */
  10671. static wc_test_ret_t aes_xts_sector_test(void)
  10672. {
  10673. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10674. XtsAes *aes = NULL;
  10675. #else
  10676. XtsAes aes[1];
  10677. #endif
  10678. int aes_inited = 0;
  10679. wc_test_ret_t ret = 0;
  10680. unsigned char buf[AES_BLOCK_SIZE * 2];
  10681. /* 128 key tests */
  10682. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10683. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10684. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10685. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10686. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10687. };
  10688. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10689. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10690. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10691. };
  10692. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10693. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10694. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10695. };
  10696. word64 s1 = 141;
  10697. /* 256 key tests */
  10698. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10699. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  10700. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  10701. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  10702. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  10703. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  10704. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  10705. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  10706. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  10707. };
  10708. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10709. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  10710. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  10711. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  10712. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  10713. };
  10714. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10715. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  10716. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  10717. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  10718. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  10719. };
  10720. word64 s2 = 187;
  10721. #if !defined(BENCH_EMBEDDED) && \
  10722. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10723. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  10724. word32 sectorSz = 512;
  10725. unsigned char data[550];
  10726. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  10727. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10728. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10729. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  10730. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  10731. };
  10732. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  10733. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10734. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10735. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10736. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10737. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10738. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10739. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10740. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10741. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10742. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10743. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10744. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10745. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10746. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10747. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10748. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10749. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10750. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10751. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10752. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10753. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10754. 0xfc, 0xfd, 0xfe, 0xff,
  10755. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10756. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10757. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10758. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10759. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10760. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10761. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10762. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10763. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10764. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10765. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10766. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10767. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10768. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10769. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10770. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10771. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10772. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10773. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10774. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10775. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10776. 0xfc, 0xfd, 0xfe, 0xff,
  10777. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10778. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10779. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  10780. };
  10781. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  10782. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  10783. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  10784. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  10785. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  10786. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  10787. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  10788. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  10789. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  10790. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  10791. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  10792. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  10793. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  10794. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  10795. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  10796. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  10797. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  10798. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  10799. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  10800. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  10801. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  10802. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  10803. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  10804. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  10805. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  10806. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  10807. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  10808. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  10809. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  10810. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  10811. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  10812. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  10813. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  10814. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  10815. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  10816. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  10817. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  10818. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  10819. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  10820. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  10821. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  10822. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  10823. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  10824. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  10825. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  10826. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  10827. 0x24, 0xe7, 0x3d, 0x6f
  10828. };
  10829. word64 s3 = 0x000000ffffffffff;
  10830. #endif
  10831. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10832. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10833. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10834. #endif
  10835. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10836. if (ret != 0)
  10837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10838. else
  10839. aes_inited = 1;
  10840. XMEMSET(buf, 0, sizeof(buf));
  10841. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10842. if (ret != 0)
  10843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10844. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  10845. #if defined(WOLFSSL_ASYNC_CRYPT)
  10846. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10847. #endif
  10848. if (ret != 0)
  10849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10850. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10851. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10852. /* decrypt test */
  10853. XMEMSET(buf, 0, sizeof(buf));
  10854. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10855. if (ret != 0)
  10856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10857. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  10858. #if defined(WOLFSSL_ASYNC_CRYPT)
  10859. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10860. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10861. #else
  10862. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10863. #endif
  10864. #endif
  10865. if (ret != 0)
  10866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10867. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10868. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10869. /* 256 bit key tests */
  10870. XMEMSET(buf, 0, sizeof(buf));
  10871. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10872. if (ret != 0)
  10873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10874. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  10875. #if defined(WOLFSSL_ASYNC_CRYPT)
  10876. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10877. #endif
  10878. if (ret != 0)
  10879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10880. if (XMEMCMP(c2, buf, sizeof(c2)))
  10881. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10882. /* decrypt test */
  10883. XMEMSET(buf, 0, sizeof(buf));
  10884. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10885. if (ret != 0)
  10886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10887. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  10888. #if defined(WOLFSSL_ASYNC_CRYPT)
  10889. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10890. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10891. #else
  10892. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10893. #endif
  10894. #endif
  10895. if (ret != 0)
  10896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10897. if (XMEMCMP(p2, buf, sizeof(p2)))
  10898. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10899. #if !defined(BENCH_EMBEDDED) && \
  10900. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10901. /* encrypt consecutive sectors test */
  10902. XMEMSET(data, 0, sizeof(buf));
  10903. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10904. if (ret != 0)
  10905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10906. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  10907. sizeof(p3), s3, sectorSz);
  10908. #if defined(WOLFSSL_ASYNC_CRYPT)
  10909. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10910. #endif
  10911. if (ret != 0)
  10912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10913. if (XMEMCMP(c3, data, sizeof(c3)))
  10914. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10915. /* decrypt consecutive sectors test */
  10916. XMEMSET(data, 0, sizeof(buf));
  10917. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10918. if (ret != 0)
  10919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10920. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  10921. sizeof(c3), s3, sectorSz);
  10922. #if defined(WOLFSSL_ASYNC_CRYPT)
  10923. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10924. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10925. #else
  10926. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10927. #endif
  10928. #endif
  10929. if (ret != 0)
  10930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10931. if (XMEMCMP(p3, data, sizeof(p3)))
  10932. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10933. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  10934. out:
  10935. if (aes_inited)
  10936. wc_AesXtsFree(aes);
  10937. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10938. if (aes)
  10939. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10940. #endif
  10941. return ret;
  10942. }
  10943. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  10944. #ifdef WOLFSSL_AES_128
  10945. /* testing of bad arguments */
  10946. static wc_test_ret_t aes_xts_args_test(void)
  10947. {
  10948. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10949. XtsAes *aes = NULL;
  10950. #else
  10951. XtsAes aes[1];
  10952. #endif
  10953. int aes_inited = 0;
  10954. wc_test_ret_t ret;
  10955. unsigned char buf[AES_BLOCK_SIZE * 2];
  10956. /* 128 key tests */
  10957. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10958. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10959. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10960. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10961. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10962. };
  10963. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10964. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10965. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10966. };
  10967. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10968. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10969. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10970. };
  10971. word64 s1 = 141;
  10972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10973. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10974. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10975. #endif
  10976. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10977. if (ret != 0)
  10978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10979. else
  10980. aes_inited = 1;
  10981. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  10982. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10983. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  10984. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10985. /* encryption operations */
  10986. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10987. if (ret != 0)
  10988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10989. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  10990. #if defined(WOLFSSL_ASYNC_CRYPT)
  10991. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10992. #endif
  10993. if (ret == 0)
  10994. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10995. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  10996. #if defined(WOLFSSL_ASYNC_CRYPT)
  10997. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10998. #endif
  10999. if (ret == 0)
  11000. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11001. /* decryption operations */
  11002. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  11003. if (ret != 0)
  11004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11005. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  11006. #if defined(WOLFSSL_ASYNC_CRYPT)
  11007. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11008. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11009. #else
  11010. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11011. #endif
  11012. #endif
  11013. if (ret == 0)
  11014. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11015. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  11016. #if defined(WOLFSSL_ASYNC_CRYPT)
  11017. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11018. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11019. #else
  11020. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11021. #endif
  11022. #endif
  11023. if (ret == 0)
  11024. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11025. ret = 0;
  11026. out:
  11027. if (aes_inited)
  11028. wc_AesXtsFree(aes);
  11029. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11030. if (aes)
  11031. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11032. #endif
  11033. return ret;
  11034. }
  11035. #endif /* WOLFSSL_AES_128 */
  11036. #endif /* WOLFSSL_AES_XTS && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  11037. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  11038. static wc_test_ret_t aes_cbc_test(void)
  11039. {
  11040. byte cipher[AES_BLOCK_SIZE];
  11041. byte plain[AES_BLOCK_SIZE];
  11042. wc_test_ret_t ret;
  11043. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11044. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11045. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11046. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11047. };
  11048. byte key[] = "0123456789abcdef "; /* align */
  11049. byte iv[] = "1234567890abcdef "; /* align */
  11050. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11051. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11052. /* Parameter Validation testing. */
  11053. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  11054. if (ret != BAD_FUNC_ARG)
  11055. return WC_TEST_RET_ENC_EC(ret);
  11056. #ifdef HAVE_AES_DECRYPT
  11057. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  11058. if (ret != BAD_FUNC_ARG)
  11059. return WC_TEST_RET_ENC_EC(ret);
  11060. #endif
  11061. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  11062. AES_BLOCK_SIZE, iv);
  11063. if (ret != 0)
  11064. return WC_TEST_RET_ENC_EC(ret);
  11065. #ifdef HAVE_AES_DECRYPT
  11066. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  11067. AES_BLOCK_SIZE, iv);
  11068. if (ret != 0)
  11069. return WC_TEST_RET_ENC_EC(ret);
  11070. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  11071. return WC_TEST_RET_ENC_NC;
  11072. #endif /* HAVE_AES_DECRYPT */
  11073. (void)plain;
  11074. return 0;
  11075. }
  11076. #endif
  11077. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11078. static wc_test_ret_t aesecb_test(void)
  11079. {
  11080. wc_test_ret_t ret = 0;
  11081. #if defined(WOLFSSL_AES_256)
  11082. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11083. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11084. #else
  11085. Aes enc[1];
  11086. #endif
  11087. int enc_inited = 0;
  11088. byte cipher[AES_BLOCK_SIZE * 4];
  11089. #ifdef HAVE_AES_DECRYPT
  11090. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11091. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11092. #else
  11093. Aes dec[1];
  11094. #endif
  11095. int dec_inited = 0;
  11096. byte plain[AES_BLOCK_SIZE * 4];
  11097. #endif /* HAVE_AES_DECRYPT */
  11098. {
  11099. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  11100. {
  11101. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11102. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11103. };
  11104. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  11105. {
  11106. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11107. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11108. };
  11109. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  11110. {
  11111. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11112. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11113. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11114. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11115. };
  11116. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11117. if (ret != 0)
  11118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11119. enc_inited = 1;
  11120. #if defined(HAVE_AES_DECRYPT)
  11121. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11122. if (ret != 0)
  11123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11124. dec_inited = 1;
  11125. #endif
  11126. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11127. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11128. if (ret != 0)
  11129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11130. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  11131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11132. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11133. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11134. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11135. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11136. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11137. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11138. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11139. if (ret != 0)
  11140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11141. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11142. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11143. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11144. if (ret != 0)
  11145. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11146. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11148. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11149. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11150. if (ret != 0)
  11151. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11152. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11153. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11154. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11155. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11156. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11157. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11158. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11159. if (ret != 0)
  11160. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11161. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11162. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11163. #endif
  11164. #ifdef HAVE_AES_DECRYPT
  11165. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11166. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11167. if (ret != 0)
  11168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11169. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  11170. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11171. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11172. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11173. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11174. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11175. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11176. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11177. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11178. if (ret != 0)
  11179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11180. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11181. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11182. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11183. if (ret != 0)
  11184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11185. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11186. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11187. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11188. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11189. if (ret != 0)
  11190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11191. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11192. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11193. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11194. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11195. if (ret != 0)
  11196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11197. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11198. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11199. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11200. if (ret != 0)
  11201. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11202. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11204. #endif
  11205. #endif /* HAVE_AES_DECRYPT */
  11206. }
  11207. out:
  11208. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11209. if (enc) {
  11210. if (enc_inited)
  11211. wc_AesFree(enc);
  11212. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11213. }
  11214. #ifdef HAVE_AES_DECRYPT
  11215. if (dec) {
  11216. if (dec_inited)
  11217. wc_AesFree(dec);
  11218. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11219. }
  11220. #endif
  11221. #else
  11222. if (enc_inited)
  11223. wc_AesFree(enc);
  11224. #ifdef HAVE_AES_DECRYPT
  11225. if (dec_inited)
  11226. wc_AesFree(dec);
  11227. #endif
  11228. #endif
  11229. #endif /* WOLFSSL_AES_256 */
  11230. return ret;
  11231. }
  11232. #endif /* HAVE_AES_ECB */
  11233. #ifdef WOLFSSL_AES_COUNTER
  11234. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11235. {
  11236. wc_test_ret_t ret = 0;
  11237. /* test vectors from "Recommendation for Block Cipher Modes of
  11238. * Operation" NIST Special Publication 800-38A */
  11239. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  11240. {
  11241. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11242. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11243. };
  11244. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  11245. {
  11246. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11247. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11248. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11249. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11250. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11251. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11252. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11253. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11254. };
  11255. #ifdef WOLFSSL_ARMASM
  11256. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  11257. {
  11258. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  11259. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11260. };
  11261. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  11262. {
  11263. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11264. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  11265. };
  11266. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  11267. {
  11268. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11269. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11270. };
  11271. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  11272. {
  11273. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  11274. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11275. };
  11276. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  11277. {
  11278. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11279. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  11280. };
  11281. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  11282. {
  11283. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  11284. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11285. };
  11286. #endif
  11287. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  11288. {
  11289. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11290. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11291. };
  11292. #ifdef WOLFSSL_ARMASM
  11293. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  11294. {
  11295. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11296. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11297. };
  11298. #endif
  11299. #ifdef WOLFSSL_AES_128
  11300. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  11301. {
  11302. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  11303. 0xc2
  11304. };
  11305. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  11306. {
  11307. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  11308. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  11309. };
  11310. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  11311. {
  11312. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  11313. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  11314. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  11315. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  11316. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  11317. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  11318. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  11319. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  11320. };
  11321. #ifdef WOLFSSL_ARMASM
  11322. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  11323. {
  11324. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11325. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11326. 0x7e,0xad,0xea,0x5c,0xd1
  11327. };
  11328. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  11329. {
  11330. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11331. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11332. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  11333. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  11334. };
  11335. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  11336. {
  11337. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  11338. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  11339. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  11340. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  11341. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  11342. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  11343. };
  11344. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  11345. {
  11346. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11347. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11348. 0xae,0x74,0x94,0x72,0x8e
  11349. };
  11350. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  11351. {
  11352. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11353. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11354. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  11355. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  11356. };
  11357. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  11358. {
  11359. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  11360. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  11361. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  11362. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  11363. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  11364. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  11365. };
  11366. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  11367. {
  11368. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11369. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11370. 0x9d,0x77,0xc9,0x5f,0x38
  11371. };
  11372. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  11373. {
  11374. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11375. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11376. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  11377. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  11378. };
  11379. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  11380. {
  11381. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  11382. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  11383. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  11384. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  11385. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  11386. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  11387. };
  11388. #endif
  11389. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  11390. {
  11391. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11392. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11393. 0xd3,0xda,0xe1,0x5b,0x04
  11394. };
  11395. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  11396. {
  11397. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11398. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11399. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  11400. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  11401. };
  11402. #ifdef WOLFSSL_ARMASM
  11403. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  11404. {
  11405. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  11406. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  11407. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  11408. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  11409. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  11410. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  11411. };
  11412. #endif
  11413. #endif /* WOLFSSL_AES_128 */
  11414. #ifdef WOLFSSL_AES_192
  11415. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  11416. {
  11417. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  11418. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  11419. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  11420. };
  11421. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  11422. {
  11423. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  11424. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  11425. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  11426. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  11427. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  11428. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  11429. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  11430. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  11431. };
  11432. #ifdef WOLFSSL_ARMASM
  11433. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  11434. {
  11435. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11436. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11437. 0x0c,0x33,0x97,0x06,0xc0
  11438. };
  11439. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  11440. {
  11441. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11442. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11443. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  11444. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  11445. };
  11446. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  11447. {
  11448. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  11449. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  11450. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  11451. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  11452. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  11453. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  11454. };
  11455. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  11456. {
  11457. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11458. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11459. 0x3d,0xf7,0xb4,0xfd,0x8c
  11460. };
  11461. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  11462. {
  11463. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11464. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11465. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  11466. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  11467. };
  11468. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  11469. {
  11470. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  11471. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  11472. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  11473. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  11474. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  11475. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  11476. };
  11477. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  11478. {
  11479. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11480. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11481. 0x47,0xb5,0xf3,0x30,0x3b
  11482. };
  11483. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  11484. {
  11485. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11486. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11487. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  11488. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  11489. };
  11490. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  11491. {
  11492. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  11493. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  11494. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  11495. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  11496. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  11497. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  11498. };
  11499. #endif
  11500. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  11501. {
  11502. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11503. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11504. 0x8c,0x68,0xa7,0xd9,0x57
  11505. };
  11506. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  11507. {
  11508. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11509. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11510. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  11511. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  11512. };
  11513. #ifdef WOLFSSL_ARMASM
  11514. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  11515. {
  11516. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  11517. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  11518. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  11519. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  11520. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  11521. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  11522. };
  11523. #endif
  11524. #endif
  11525. #ifdef WOLFSSL_AES_256
  11526. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  11527. {
  11528. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11529. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11530. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11531. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11532. };
  11533. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  11534. {
  11535. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  11536. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  11537. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  11538. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  11539. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  11540. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  11541. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  11542. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  11543. };
  11544. #ifdef WOLFSSL_ARMASM
  11545. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  11546. {
  11547. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11548. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11549. 0xa2,0x13,0x47,0x74,0xda
  11550. };
  11551. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  11552. {
  11553. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11554. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11555. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  11556. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  11557. };
  11558. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  11559. {
  11560. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  11561. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  11562. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  11563. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  11564. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  11565. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  11566. };
  11567. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  11568. {
  11569. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11570. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11571. 0xd4,0x94,0x4c,0xcd,0x4d
  11572. };
  11573. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  11574. {
  11575. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11576. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11577. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  11578. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  11579. };
  11580. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  11581. {
  11582. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  11583. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  11584. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  11585. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  11586. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  11587. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  11588. };
  11589. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  11590. {
  11591. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11592. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11593. 0xb8,0x69,0xa1,0x48,0x45
  11594. };
  11595. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  11596. {
  11597. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11598. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11599. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  11600. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  11601. };
  11602. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  11603. {
  11604. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  11605. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  11606. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  11607. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  11608. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  11609. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  11610. };
  11611. #endif
  11612. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  11613. {
  11614. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11615. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11616. 0x4b,0x45,0x7c,0xd6,0x8a
  11617. };
  11618. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  11619. {
  11620. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11621. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11622. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  11623. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  11624. };
  11625. #ifdef WOLFSSL_ARMASM
  11626. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  11627. {
  11628. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  11629. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  11630. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  11631. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  11632. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  11633. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  11634. };
  11635. #endif
  11636. #endif
  11637. int i;
  11638. struct {
  11639. const byte* key;
  11640. int keySz;
  11641. const byte* iv;
  11642. const byte* plain;
  11643. int len;
  11644. const byte* cipher;
  11645. } testVec[] = {
  11646. #ifdef WOLFSSL_AES_128
  11647. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11648. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  11649. /* let's try with just 9 bytes, non block size test */
  11650. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11651. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  11652. /* and an additional 9 bytes to reuse tmp left buffer */
  11653. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  11654. /* Counter wrapping */
  11655. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11656. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  11657. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11658. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  11659. ctr128Wrap128CipherLong },
  11660. #ifdef WOLFSSL_ARMASM
  11661. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  11662. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  11663. ctr128Wrap128_2CipherLong },
  11664. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11665. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  11666. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11667. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  11668. ctr128Wrap96CipherLong },
  11669. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  11670. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  11671. ctr128Wrap96_2CipherLong },
  11672. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11673. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  11674. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11675. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  11676. ctr128Wrap64CipherLong },
  11677. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  11678. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  11679. ctr128Wrap64_2CipherLong },
  11680. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11681. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  11682. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11683. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  11684. ctr128Wrap32CipherLong },
  11685. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  11686. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  11687. ctr128Wrap32_2CipherLong },
  11688. #endif
  11689. #endif
  11690. #ifdef WOLFSSL_AES_192
  11691. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11692. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  11693. /* let's try with just 9 bytes, non block size test */
  11694. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11695. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  11696. /* Counter wrapping */
  11697. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11698. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  11699. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11700. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  11701. ctr192Wrap128CipherLong },
  11702. #ifdef WOLFSSL_ARMASM
  11703. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  11704. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  11705. ctr192Wrap128_2CipherLong },
  11706. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11707. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  11708. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11709. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  11710. ctr192Wrap96CipherLong },
  11711. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  11712. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  11713. ctr192Wrap96_2CipherLong },
  11714. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11715. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  11716. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11717. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  11718. ctr192Wrap64CipherLong },
  11719. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  11720. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  11721. ctr192Wrap64_2CipherLong },
  11722. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11723. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  11724. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11725. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  11726. ctr192Wrap32CipherLong },
  11727. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  11728. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  11729. ctr192Wrap32_2CipherLong },
  11730. #endif
  11731. #endif
  11732. #ifdef WOLFSSL_AES_256
  11733. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11734. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  11735. /* let's try with just 9 bytes, non block size test */
  11736. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11737. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  11738. /* Counter wrapping */
  11739. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11740. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  11741. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11742. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  11743. ctr256Wrap128CipherLong },
  11744. #ifdef WOLFSSL_ARMASM
  11745. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  11746. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  11747. ctr256Wrap128_2CipherLong },
  11748. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11749. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  11750. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11751. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  11752. ctr256Wrap96CipherLong },
  11753. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  11754. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  11755. ctr256Wrap96_2CipherLong },
  11756. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11757. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  11758. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11759. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  11760. ctr256Wrap64CipherLong },
  11761. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  11762. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  11763. ctr256Wrap64_2CipherLong },
  11764. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11765. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  11766. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11767. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  11768. ctr256Wrap32CipherLong },
  11769. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  11770. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  11771. ctr256Wrap32_2CipherLong },
  11772. #endif
  11773. #endif
  11774. };
  11775. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11776. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11777. if (testVec[i].key != NULL) {
  11778. ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,
  11779. testVec[i].iv, AES_ENCRYPTION);
  11780. if (ret != 0) {
  11781. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11782. }
  11783. /* Ctr only uses encrypt, even on key setup */
  11784. ret = wc_AesSetKeyDirect(dec, testVec[i].key, (word32)testVec[i].keySz,
  11785. testVec[i].iv, AES_ENCRYPTION);
  11786. if (ret != 0) {
  11787. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11788. }
  11789. }
  11790. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, (word32)testVec[i].len);
  11791. if (ret != 0) {
  11792. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11793. }
  11794. ret = wc_AesCtrEncrypt(dec, plain, cipher, (word32)testVec[i].len);
  11795. if (ret != 0) {
  11796. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11797. }
  11798. if (XMEMCMP(plain, ctrPlain, (size_t)testVec[i].len)) {
  11799. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11800. }
  11801. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11802. if (XMEMCMP(cipher, testVec[i].cipher, (size_t)testVec[i].len)) {
  11803. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11804. }
  11805. #endif
  11806. }
  11807. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11808. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11809. if (testVec[i].key != NULL) {
  11810. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11811. testVec[i].iv, AES_ENCRYPTION);
  11812. if (ret != 0) {
  11813. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11814. }
  11815. /* Ctr only uses encrypt, even on key setup */
  11816. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11817. testVec[i].iv, AES_ENCRYPTION);
  11818. if (ret != 0) {
  11819. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11820. }
  11821. }
  11822. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11823. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11824. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11825. if (ret != 0) {
  11826. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11827. }
  11828. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11829. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11830. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11831. if (ret != 0) {
  11832. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11833. }
  11834. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11835. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11836. }
  11837. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11838. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11839. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11840. }
  11841. #endif
  11842. }
  11843. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11844. if (testVec[i].key != NULL) {
  11845. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11846. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11847. testVec[i].iv, AES_ENCRYPTION);
  11848. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11849. if (ret != 0) {
  11850. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11851. }
  11852. /* Ctr only uses encrypt, even on key setup */
  11853. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11854. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11855. testVec[i].iv, AES_ENCRYPTION);
  11856. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11857. if (ret != 0) {
  11858. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11859. }
  11860. }
  11861. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11862. if (ret != 0) {
  11863. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11864. }
  11865. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11866. if (ret != 0) {
  11867. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11868. }
  11869. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11870. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11871. }
  11872. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11873. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11874. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11875. }
  11876. #endif
  11877. }
  11878. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  11879. out:
  11880. return ret;
  11881. }
  11882. #endif /* WOLFSSL_AES_COUNTER */
  11883. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  11884. {
  11885. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  11886. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11887. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11888. #else
  11889. Aes enc[1];
  11890. #endif
  11891. int enc_inited = 0;
  11892. byte cipher[AES_BLOCK_SIZE * 4];
  11893. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11895. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11896. #else
  11897. Aes dec[1];
  11898. #endif
  11899. int dec_inited = 0;
  11900. byte plain [AES_BLOCK_SIZE * 4];
  11901. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  11902. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  11903. wc_test_ret_t ret = 0;
  11904. #ifdef HAVE_AES_CBC
  11905. #ifdef WOLFSSL_AES_128
  11906. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11907. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11908. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11909. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11910. };
  11911. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  11912. {
  11913. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  11914. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  11915. };
  11916. #ifdef HAVE_RENESAS_SYNC
  11917. const byte *key =
  11918. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  11919. #else
  11920. WOLFSSL_SMALL_STACK_STATIC const
  11921. byte key[] = "0123456789abcdef "; /* align */
  11922. #endif
  11923. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  11924. WOLFSSL_ENTER("aes_test");
  11925. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11926. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  11927. if (enc == NULL)
  11928. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11929. #endif
  11930. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11931. if (dec == NULL)
  11932. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11933. #endif
  11934. #endif
  11935. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11936. if (ret != 0)
  11937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11938. enc_inited = 1;
  11939. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11940. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11941. if (ret != 0)
  11942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11943. dec_inited = 1;
  11944. #endif
  11945. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  11946. if (ret != 0)
  11947. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11948. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11949. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  11950. if (ret != 0)
  11951. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11952. #endif
  11953. #ifdef HAVE_AES_ECB
  11954. {
  11955. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb[AES_BLOCK_SIZE] = {
  11956. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  11957. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  11958. };
  11959. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  11960. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  11961. #if defined(WOLFSSL_ASYNC_CRYPT)
  11962. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11963. #endif
  11964. if (ret != 0)
  11965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11966. if (XMEMCMP(cipher, verify_ecb, AES_BLOCK_SIZE)) {
  11967. WOLFSSL_MSG("aes_test cipher vs verify_ecb mismatch!");
  11968. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11969. }
  11970. #ifdef HAVE_AES_DECRYPT
  11971. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  11972. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  11973. #if defined(WOLFSSL_ASYNC_CRYPT)
  11974. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11975. #endif
  11976. if (ret != 0)
  11977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11978. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11979. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11980. #endif /* HAVE_AES_DECRYPT */
  11981. }
  11982. #endif
  11983. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  11984. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  11985. #if defined(WOLFSSL_ASYNC_CRYPT)
  11986. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11987. #endif
  11988. if (ret != 0)
  11989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11990. #ifdef HAVE_AES_DECRYPT
  11991. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  11992. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  11993. #if defined(WOLFSSL_ASYNC_CRYPT)
  11994. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11995. #endif
  11996. if (ret != 0) {
  11997. WOLFSSL_MSG("failed wc_AesCbcDecrypt");
  11998. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11999. }
  12000. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) {
  12001. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain compare");
  12002. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12003. }
  12004. #endif /* HAVE_AES_DECRYPT */
  12005. /* skipped because wrapped key use in case of renesas sm */
  12006. #ifndef HAVE_RENESAS_SYNC
  12007. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) {
  12008. WOLFSSL_MSG("wc_AesCbcDecrypt failed cipher-verify compare");
  12009. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12010. }
  12011. #endif
  12012. #endif /* WOLFSSL_AES_128 */
  12013. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  12014. {
  12015. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  12016. /* "All work and no play makes Jack a dull boy. " */
  12017. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12018. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12019. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12020. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12021. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12022. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12023. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12024. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12025. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12026. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12027. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12028. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12029. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12030. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12031. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12032. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12033. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12034. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12035. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12036. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12037. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12038. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12039. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12040. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12041. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12042. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12043. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12044. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12045. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12046. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12047. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12048. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12049. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12050. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12051. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12052. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12053. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12054. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12055. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12056. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12057. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12058. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12059. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12060. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12061. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12062. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12063. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12064. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  12065. };
  12066. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  12067. word32 keySz, msgSz;
  12068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12069. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12070. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12071. if ((bigCipher == NULL) ||
  12072. (bigPlain == NULL)) {
  12073. if (bigCipher != NULL)
  12074. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12075. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12076. }
  12077. #else
  12078. byte bigCipher[sizeof(bigMsg)];
  12079. byte bigPlain[sizeof(bigMsg)];
  12080. #endif
  12081. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12082. * message by AES_BLOCK_SIZE for each size of AES key. */
  12083. for (keySz = 16; keySz <= 32; keySz += 8) {
  12084. for (msgSz = AES_BLOCK_SIZE;
  12085. msgSz <= sizeof(bigMsg);
  12086. msgSz += AES_BLOCK_SIZE) {
  12087. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12088. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12089. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12090. if (ret != 0) {
  12091. ret = WC_TEST_RET_ENC_EC(ret);
  12092. break;
  12093. }
  12094. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12095. if (ret != 0) {
  12096. ret = WC_TEST_RET_ENC_EC(ret);
  12097. break;
  12098. }
  12099. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12100. #if defined(WOLFSSL_ASYNC_CRYPT)
  12101. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12102. #endif
  12103. if (ret != 0) {
  12104. ret = WC_TEST_RET_ENC_EC(ret);
  12105. break;
  12106. }
  12107. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12108. #if defined(WOLFSSL_ASYNC_CRYPT)
  12109. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12110. #endif
  12111. if (ret != 0) {
  12112. ret = WC_TEST_RET_ENC_EC(ret);
  12113. break;
  12114. }
  12115. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12116. ret = WC_TEST_RET_ENC_NC;
  12117. break;
  12118. }
  12119. }
  12120. if (ret != 0)
  12121. break;
  12122. }
  12123. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12124. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12125. * message by AES_BLOCK_SIZE for each size of AES key. */
  12126. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12127. for (keySz = 16; keySz <= 32; keySz += 8) {
  12128. for (msgSz = AES_BLOCK_SIZE;
  12129. msgSz <= sizeof(bigMsg);
  12130. msgSz += AES_BLOCK_SIZE) {
  12131. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12132. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12133. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12134. if (ret != 0) {
  12135. ret = WC_TEST_RET_ENC_EC(ret);
  12136. break;
  12137. }
  12138. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12139. if (ret != 0) {
  12140. ret = WC_TEST_RET_ENC_EC(ret);
  12141. break;
  12142. }
  12143. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12144. #if defined(WOLFSSL_ASYNC_CRYPT)
  12145. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12146. #endif
  12147. if (ret != 0) {
  12148. ret = WC_TEST_RET_ENC_EC(ret);
  12149. break;
  12150. }
  12151. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12152. #if defined(WOLFSSL_ASYNC_CRYPT)
  12153. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12154. #endif
  12155. if (ret != 0) {
  12156. ret = WC_TEST_RET_ENC_EC(ret);
  12157. break;
  12158. }
  12159. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12160. ret = WC_TEST_RET_ENC_NC;
  12161. break;
  12162. }
  12163. }
  12164. if (ret != 0)
  12165. break;
  12166. }
  12167. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12168. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12169. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12170. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12171. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12172. #endif
  12173. if (ret != 0)
  12174. goto out;
  12175. }
  12176. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  12177. /* Test of AES IV state with encrypt/decrypt */
  12178. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  12179. {
  12180. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  12181. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  12182. */
  12183. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  12184. {
  12185. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  12186. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  12187. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  12188. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  12189. };
  12190. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  12191. {
  12192. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  12193. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  12194. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  12195. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  12196. };
  12197. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  12198. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  12199. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  12200. };
  12201. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  12202. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12203. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  12204. };
  12205. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  12206. if (ret != 0)
  12207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12208. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  12209. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  12210. #if defined(WOLFSSL_ASYNC_CRYPT)
  12211. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12212. #endif
  12213. if (ret != 0)
  12214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12215. #ifndef HAVE_RENESAS_SYNC
  12216. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  12217. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12218. #endif
  12219. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  12220. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12221. #if defined(WOLFSSL_ASYNC_CRYPT)
  12222. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12223. #endif
  12224. if (ret != 0)
  12225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12226. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  12227. AES_BLOCK_SIZE))
  12228. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12229. #if defined(HAVE_AES_DECRYPT)
  12230. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  12231. if (ret != 0)
  12232. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12233. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  12234. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  12235. #if defined(WOLFSSL_ASYNC_CRYPT)
  12236. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12237. #endif
  12238. if (ret != 0)
  12239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12240. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  12241. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12242. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  12243. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12244. #if defined(WOLFSSL_ASYNC_CRYPT)
  12245. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12246. #endif
  12247. if (ret != 0)
  12248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12249. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  12250. AES_BLOCK_SIZE)) {
  12251. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain-msg2 compare");
  12252. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12253. }
  12254. #endif /* HAVE_AES_DECRYPT */
  12255. }
  12256. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  12257. #endif /* HAVE_AES_CBC */
  12258. #ifdef WOLFSSL_AES_COUNTER
  12259. ret = aesctr_test(enc, dec, cipher, plain);
  12260. if (ret != 0)
  12261. return ret;
  12262. #endif
  12263. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  12264. {
  12265. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  12266. {
  12267. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12268. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12269. };
  12270. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  12271. {
  12272. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  12273. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  12274. };
  12275. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  12276. {
  12277. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12278. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12279. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12280. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12281. };
  12282. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12283. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  12284. if (ret != 0)
  12285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12286. #if !defined(HAVE_SELFTEST) && \
  12287. (defined(WOLFSSL_LINUXKM) || \
  12288. !defined(HAVE_FIPS) || \
  12289. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12290. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  12291. if (ret != 0)
  12292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12293. #else
  12294. wc_AesEncryptDirect(enc, cipher, niPlain);
  12295. #endif
  12296. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  12297. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12298. #ifdef HAVE_AES_DECRYPT
  12299. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12300. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  12301. if (ret != 0)
  12302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12303. #if !defined(HAVE_SELFTEST) && \
  12304. (defined(WOLFSSL_LINUXKM) || \
  12305. !defined(HAVE_FIPS) || \
  12306. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12307. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  12308. if (ret != 0)
  12309. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12310. #else
  12311. wc_AesDecryptDirect(dec, plain, niCipher);
  12312. #endif
  12313. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  12314. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12315. #endif
  12316. }
  12317. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  12318. #ifndef HAVE_RENESAS_SYNC
  12319. ret = aes_key_size_test();
  12320. if (ret != 0)
  12321. goto out;
  12322. #endif
  12323. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  12324. !defined(HAVE_RENESAS_SYNC)
  12325. ret = aes_cbc_test();
  12326. if (ret != 0)
  12327. goto out;
  12328. #endif
  12329. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12330. ret = aesecb_test();
  12331. if (ret != 0)
  12332. goto out;
  12333. #endif
  12334. out:
  12335. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  12336. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12337. if (enc) {
  12338. if (enc_inited)
  12339. wc_AesFree(enc);
  12340. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12341. }
  12342. #else
  12343. if (enc_inited)
  12344. wc_AesFree(enc);
  12345. #endif
  12346. (void)cipher;
  12347. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12348. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12349. if (dec) {
  12350. if (dec_inited)
  12351. wc_AesFree(dec);
  12352. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12353. }
  12354. #else
  12355. if (dec_inited)
  12356. wc_AesFree(dec);
  12357. #endif
  12358. (void)plain;
  12359. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  12360. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  12361. return ret;
  12362. }
  12363. #if defined(WOLFSSL_AES_CFB)
  12364. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void)
  12365. {
  12366. int ret;
  12367. WOLFSSL_ENTER("aes_cfb_test");
  12368. ret = aescfb_test_0();
  12369. if (ret != 0)
  12370. return ret;
  12371. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  12372. ret = aescfb1_test();
  12373. if (ret != 0)
  12374. return ret;
  12375. ret = aescfb8_test();
  12376. if (ret != 0)
  12377. return ret;
  12378. #endif
  12379. return 0;
  12380. }
  12381. #endif
  12382. #if defined(WOLFSSL_AES_XTS)
  12383. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void)
  12384. {
  12385. int ret = 0;
  12386. WOLFSSL_ENTER("aes_xts_test");
  12387. #ifdef WOLFSSL_AES_128
  12388. ret = aes_xts_128_test();
  12389. if (ret != 0)
  12390. return ret;
  12391. #endif
  12392. #ifdef WOLFSSL_AES_192
  12393. ret = aes_xts_192_test();
  12394. if (ret != 0)
  12395. return ret;
  12396. #endif
  12397. #ifdef WOLFSSL_AES_256
  12398. ret = aes_xts_256_test();
  12399. if (ret != 0)
  12400. return ret;
  12401. #endif
  12402. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  12403. ret = aes_xts_sector_test();
  12404. if (ret != 0)
  12405. return ret;
  12406. #endif
  12407. #ifdef WOLFSSL_AES_128
  12408. ret = aes_xts_args_test();
  12409. if (ret != 0)
  12410. return ret;
  12411. #endif
  12412. return 0;
  12413. }
  12414. #endif
  12415. #ifdef WOLFSSL_AES_192
  12416. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  12417. {
  12418. #ifdef HAVE_AES_CBC
  12419. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12420. Aes *enc = NULL;
  12421. #else
  12422. Aes enc[1];
  12423. #endif
  12424. int enc_inited = 0;
  12425. byte cipher[AES_BLOCK_SIZE];
  12426. #ifdef HAVE_AES_DECRYPT
  12427. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12428. Aes *dec = NULL;
  12429. #else
  12430. Aes dec[1];
  12431. #endif
  12432. int dec_inited = 0;
  12433. byte plain[AES_BLOCK_SIZE];
  12434. #endif
  12435. #endif /* HAVE_AES_CBC */
  12436. wc_test_ret_t ret = 0;
  12437. #ifdef HAVE_AES_CBC
  12438. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  12439. * Appendix F.2.3 */
  12440. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12441. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12442. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12443. };
  12444. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12445. {
  12446. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  12447. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  12448. };
  12449. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12450. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  12451. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  12452. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  12453. };
  12454. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12455. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12456. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12457. };
  12458. WOLFSSL_ENTER("aes192_test");
  12459. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12460. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12461. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12462. #ifdef HAVE_AES_DECRYPT
  12463. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12464. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12465. #endif
  12466. #endif
  12467. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12468. if (ret != 0)
  12469. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12470. enc_inited = 1;
  12471. #ifdef HAVE_AES_DECRYPT
  12472. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12473. if (ret != 0)
  12474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12475. dec_inited = 1;
  12476. #endif
  12477. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  12478. if (ret != 0)
  12479. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12480. #ifdef HAVE_AES_DECRYPT
  12481. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  12482. if (ret != 0)
  12483. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12484. #endif
  12485. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12486. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12487. #if defined(WOLFSSL_ASYNC_CRYPT)
  12488. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12489. #endif
  12490. if (ret != 0)
  12491. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12492. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12493. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12494. #ifdef HAVE_AES_DECRYPT
  12495. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12496. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12497. #if defined(WOLFSSL_ASYNC_CRYPT)
  12498. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12499. #endif
  12500. if (ret != 0)
  12501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12502. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12503. WOLFSSL_MSG("failed wc_AesCbcDecrypt plain-msg compare");
  12504. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12505. }
  12506. #endif
  12507. out:
  12508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12509. if (enc) {
  12510. if (enc_inited)
  12511. wc_AesFree(enc);
  12512. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12513. }
  12514. #ifdef HAVE_AES_DECRYPT
  12515. if (dec) {
  12516. if (dec_inited)
  12517. wc_AesFree(dec);
  12518. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12519. }
  12520. #endif
  12521. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12522. if (enc_inited)
  12523. wc_AesFree(enc);
  12524. #ifdef HAVE_AES_DECRYPT
  12525. if (dec_inited)
  12526. wc_AesFree(dec);
  12527. #endif
  12528. #endif
  12529. #endif /* HAVE_AES_CBC */
  12530. return ret;
  12531. }
  12532. #endif /* WOLFSSL_AES_192 */
  12533. #ifdef WOLFSSL_AES_256
  12534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  12535. {
  12536. #ifdef HAVE_AES_CBC
  12537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12538. Aes *enc = NULL;
  12539. #else
  12540. Aes enc[1];
  12541. #endif
  12542. int enc_inited = 0;
  12543. byte cipher[AES_BLOCK_SIZE];
  12544. #ifdef HAVE_AES_DECRYPT
  12545. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12546. Aes *dec = NULL;
  12547. #else
  12548. Aes dec[1];
  12549. #endif
  12550. int dec_inited = 0;
  12551. byte plain[AES_BLOCK_SIZE];
  12552. #endif
  12553. #endif /* HAVE_AES_CBC */
  12554. wc_test_ret_t ret = 0;
  12555. #ifdef HAVE_AES_CBC
  12556. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  12557. * Appendix F.2.5 */
  12558. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12559. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12560. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12561. };
  12562. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12563. {
  12564. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  12565. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  12566. };
  12567. #ifdef HAVE_RENESAS_SYNC
  12568. byte *key =
  12569. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  12570. int keySz = (256/8);
  12571. #else
  12572. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12573. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12574. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12575. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12576. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12577. };
  12578. int keySz = (int)sizeof(key);
  12579. #endif
  12580. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12581. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12582. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12583. };
  12584. WOLFSSL_ENTER("aes256_test");
  12585. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12586. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12587. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12588. #ifdef HAVE_AES_DECRYPT
  12589. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12590. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12591. #endif
  12592. #endif
  12593. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12594. if (ret != 0)
  12595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12596. enc_inited = 1;
  12597. #ifdef HAVE_AES_DECRYPT
  12598. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12599. if (ret != 0)
  12600. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12601. dec_inited = 1;
  12602. #endif
  12603. ret = wc_AesSetKey(enc, key, (word32)keySz, iv, AES_ENCRYPTION);
  12604. if (ret != 0)
  12605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12606. #ifdef HAVE_AES_DECRYPT
  12607. ret = wc_AesSetKey(dec, key, (word32)keySz, iv, AES_DECRYPTION);
  12608. if (ret != 0)
  12609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12610. #endif
  12611. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12612. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12613. #if defined(WOLFSSL_ASYNC_CRYPT)
  12614. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12615. #endif
  12616. if (ret != 0)
  12617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12618. #ifdef HAVE_AES_DECRYPT
  12619. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12620. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12621. #if defined(WOLFSSL_ASYNC_CRYPT)
  12622. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12623. #endif
  12624. if (ret != 0)
  12625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12626. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12627. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12628. }
  12629. #endif
  12630. #ifndef HAVE_RENESAS_SYNC
  12631. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12632. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12633. #endif
  12634. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12635. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12636. if (ret != 0)
  12637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12638. #ifdef HAVE_AES_DECRYPT
  12639. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12640. if (ret != 0)
  12641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12642. #endif
  12643. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12644. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12645. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12646. #if defined(WOLFSSL_ASYNC_CRYPT)
  12647. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12648. #endif
  12649. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12650. if (ret != 0)
  12651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12652. #ifdef HAVE_AES_DECRYPT
  12653. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12654. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12655. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12656. #if defined(WOLFSSL_ASYNC_CRYPT)
  12657. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12658. #endif
  12659. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12660. if (ret != 0)
  12661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12662. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12663. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12664. }
  12665. #endif
  12666. #ifndef HAVE_RENESAS_SYNC
  12667. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12668. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12669. #endif
  12670. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12671. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12672. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12673. if (ret != 0)
  12674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12675. #ifdef HAVE_AES_DECRYPT
  12676. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12677. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12678. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12679. if (ret != 0)
  12680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12681. #endif
  12682. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12683. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12684. #if defined(WOLFSSL_ASYNC_CRYPT)
  12685. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12686. #endif
  12687. if (ret != 0)
  12688. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12689. #ifdef HAVE_AES_DECRYPT
  12690. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12691. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12692. #if defined(WOLFSSL_ASYNC_CRYPT)
  12693. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12694. #endif
  12695. if (ret != 0)
  12696. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12697. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12698. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12699. }
  12700. #endif
  12701. #ifndef HAVE_RENESAS_SYNC
  12702. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12703. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12704. #endif
  12705. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12706. out:
  12707. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12708. if (enc) {
  12709. if (enc_inited)
  12710. wc_AesFree(enc);
  12711. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12712. }
  12713. #ifdef HAVE_AES_DECRYPT
  12714. if (dec) {
  12715. if (dec_inited)
  12716. wc_AesFree(dec);
  12717. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12718. }
  12719. #endif
  12720. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12721. if (enc_inited)
  12722. wc_AesFree(enc);
  12723. #ifdef HAVE_AES_DECRYPT
  12724. if (dec_inited)
  12725. wc_AesFree(dec);
  12726. #endif
  12727. #endif
  12728. #endif /* HAVE_AES_CBC */
  12729. return ret;
  12730. }
  12731. #endif /* WOLFSSL_AES_256 */
  12732. #ifdef HAVE_AESGCM
  12733. #ifdef WOLFSSL_AES_128
  12734. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  12735. byte* plain, int plainSz, byte* cipher, int cipherSz,
  12736. byte* aad, int aadSz, byte* tag, int tagSz)
  12737. {
  12738. wc_test_ret_t ret;
  12739. int enc_inited = 0, dec_inited = 0;
  12740. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12741. Aes *enc = NULL;
  12742. Aes *dec = NULL;
  12743. #else
  12744. Aes enc[1];
  12745. Aes dec[1];
  12746. #endif
  12747. byte resultT[AES_BLOCK_SIZE];
  12748. byte resultP[AES_BLOCK_SIZE * 3];
  12749. byte resultC[AES_BLOCK_SIZE * 3];
  12750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12751. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12752. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12753. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12754. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12755. #endif
  12756. XMEMSET(resultT, 0, sizeof(resultT));
  12757. XMEMSET(resultC, 0, sizeof(resultC));
  12758. XMEMSET(resultP, 0, sizeof(resultP));
  12759. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12760. if (ret != 0)
  12761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12762. else
  12763. enc_inited = 1;
  12764. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12765. if (ret != 0)
  12766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12767. else
  12768. dec_inited = 1;
  12769. ret = wc_AesGcmSetKey(enc, key, (word32)keySz);
  12770. if (ret != 0)
  12771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12772. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  12773. ret = wc_AesGcmEncrypt(enc, resultC, plain, (word32)plainSz, iv, ivSz,
  12774. resultT, (word32)tagSz, aad, aadSz);
  12775. #if defined(WOLFSSL_ASYNC_CRYPT)
  12776. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12777. #endif
  12778. if (ret != 0)
  12779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12780. if (cipher != NULL) {
  12781. if (XMEMCMP(cipher, resultC, cipherSz))
  12782. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12783. }
  12784. if (XMEMCMP(tag, resultT, tagSz))
  12785. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12786. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12787. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12788. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  12789. resultT, tagSz, aad, aadSz);
  12790. #if defined(WOLFSSL_ASYNC_CRYPT)
  12791. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12792. #endif
  12793. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12794. if (ret != 0)
  12795. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12796. if (cipher != NULL) {
  12797. if (XMEMCMP(cipher, resultC, cipherSz))
  12798. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12799. }
  12800. if (XMEMCMP(tag, resultT, tagSz))
  12801. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12802. #endif
  12803. #ifdef HAVE_AES_DECRYPT
  12804. ret = wc_AesGcmSetKey(dec, key, (word32)keySz);
  12805. if (ret != 0)
  12806. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12807. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)cipherSz,
  12808. iv, (word32)ivSz, resultT, tagSz, aad, aadSz);
  12809. #if defined(WOLFSSL_ASYNC_CRYPT)
  12810. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12811. #endif
  12812. if (ret != 0)
  12813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12814. if (plain != NULL) {
  12815. if (XMEMCMP(plain, resultP, plainSz))
  12816. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12817. }
  12818. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12819. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12820. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  12821. iv, ivSz, resultT, tagSz, aad, aadSz);
  12822. #if defined(WOLFSSL_ASYNC_CRYPT)
  12823. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12824. #endif
  12825. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12826. if (ret != 0)
  12827. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12828. if (plain != NULL) {
  12829. if (XMEMCMP(plain, resultP, plainSz))
  12830. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12831. }
  12832. #endif
  12833. #endif /* HAVE_AES_DECRYPT */
  12834. ret = 0;
  12835. out:
  12836. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12837. if (enc) {
  12838. if (enc_inited)
  12839. wc_AesFree(enc);
  12840. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12841. }
  12842. if (dec) {
  12843. if (dec_inited)
  12844. wc_AesFree(dec);
  12845. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12846. }
  12847. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12848. if (enc_inited)
  12849. wc_AesFree(enc);
  12850. if (dec_inited)
  12851. wc_AesFree(dec);
  12852. #endif
  12853. return ret;
  12854. }
  12855. #endif
  12856. /* tests that only use 12 byte IV and 16 or less byte AAD
  12857. * test vectors are from NIST SP 800-38D
  12858. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  12859. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  12860. {
  12861. #ifdef WOLFSSL_AES_128
  12862. byte key1[] = {
  12863. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  12864. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  12865. };
  12866. byte iv1[] = {
  12867. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  12868. 0xe4, 0xed, 0x2f, 0x6d
  12869. };
  12870. ALIGN64 byte plain1[] = {
  12871. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  12872. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  12873. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  12874. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  12875. };
  12876. byte aad1[] = {
  12877. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  12878. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  12879. };
  12880. ALIGN64 byte cipher1[] = {
  12881. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  12882. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  12883. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  12884. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  12885. };
  12886. byte tag1[] = {
  12887. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  12888. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  12889. };
  12890. byte key2[] = {
  12891. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  12892. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  12893. };
  12894. byte iv2[] = {
  12895. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  12896. 0xc9, 0x8a, 0xff, 0xe3
  12897. };
  12898. ALIGN64 byte plain2[] = {
  12899. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  12900. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  12901. };
  12902. ALIGN64 byte cipher2[] = {
  12903. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  12904. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  12905. };
  12906. byte tag2[] = {
  12907. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  12908. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  12909. };
  12910. byte key3[] = {
  12911. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  12912. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  12913. };
  12914. byte iv3[] = {
  12915. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  12916. 0x6a, 0x4b, 0xd5, 0xe1
  12917. };
  12918. byte tag3[] = {
  12919. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  12920. 0x11, 0x64, 0xb2, 0xff
  12921. };
  12922. wc_test_ret_t ret;
  12923. WOLFSSL_ENTER("aesgcm_default_test");
  12924. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  12925. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  12926. aad1, sizeof(aad1), tag1, sizeof(tag1));
  12927. if (ret != 0) {
  12928. return ret;
  12929. }
  12930. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  12931. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  12932. NULL, 0, tag2, sizeof(tag2));
  12933. if (ret != 0) {
  12934. return ret;
  12935. }
  12936. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  12937. NULL, 0, NULL, 0,
  12938. NULL, 0, tag3, sizeof(tag3));
  12939. if (ret != 0) {
  12940. return ret;
  12941. }
  12942. #endif
  12943. return 0;
  12944. }
  12945. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  12946. {
  12947. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12948. Aes *enc = NULL;
  12949. Aes *dec = NULL;
  12950. #else
  12951. Aes enc[1];
  12952. Aes dec[1];
  12953. #endif
  12954. /*
  12955. * This is Test Case 16 from the document Galois/
  12956. * Counter Mode of Operation (GCM) by McGrew and
  12957. * Viega.
  12958. */
  12959. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  12960. {
  12961. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  12962. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  12963. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  12964. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  12965. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  12966. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  12967. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  12968. 0xba, 0x63, 0x7b, 0x39
  12969. };
  12970. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  12971. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  12972. {
  12973. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  12974. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  12975. 0xab, 0xad, 0xda, 0xd2
  12976. };
  12977. #endif
  12978. #ifdef WOLFSSL_AES_256
  12979. #ifdef HAVE_RENESAS_SYNC
  12980. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  12981. int k1Sz = (int)(256/8);
  12982. #else
  12983. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  12984. {
  12985. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  12986. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  12987. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  12988. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  12989. };
  12990. int k1Sz = (int)sizeof(k1);
  12991. #endif
  12992. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  12993. {
  12994. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  12995. 0xde, 0xca, 0xf8, 0x88
  12996. };
  12997. #endif /* WOLFSSL_AES_256 */
  12998. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  12999. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  13000. {
  13001. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  13002. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  13003. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  13004. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  13005. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  13006. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  13007. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  13008. 0xbc, 0xc9, 0xf6, 0x62
  13009. };
  13010. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  13011. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13012. {
  13013. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  13014. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  13015. };
  13016. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  13017. #if !defined(HAVE_FIPS) && \
  13018. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  13019. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  13020. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13021. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  13022. !(defined(WOLF_CRYPTO_CB) && \
  13023. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  13024. #define ENABLE_NON_12BYTE_IV_TEST
  13025. #ifdef WOLFSSL_AES_192
  13026. /* Test Case 12, uses same plaintext and AAD data. */
  13027. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13028. {
  13029. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13030. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13031. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  13032. };
  13033. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13034. {
  13035. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  13036. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  13037. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  13038. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  13039. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  13040. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  13041. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  13042. 0xa6, 0x37, 0xb3, 0x9b
  13043. };
  13044. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  13045. {
  13046. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  13047. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  13048. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  13049. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  13050. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  13051. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  13052. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  13053. 0xe9, 0xb7, 0x37, 0x3b
  13054. };
  13055. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13056. {
  13057. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  13058. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  13059. };
  13060. #endif /* WOLFSSL_AES_192 */
  13061. #ifdef WOLFSSL_AES_128
  13062. /* The following is an interesting test case from the example
  13063. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  13064. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  13065. {
  13066. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  13067. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  13068. };
  13069. #ifdef HAVE_RENESAS_SYNC
  13070. const byte *k3 =
  13071. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  13072. int k3Sz = (int)(128/8);
  13073. #else
  13074. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  13075. {
  13076. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  13077. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  13078. };
  13079. int k3Sz = (int)sizeof(k3);
  13080. #endif
  13081. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  13082. {
  13083. 0xca
  13084. };
  13085. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  13086. {
  13087. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  13088. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  13089. };
  13090. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  13091. {
  13092. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  13093. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  13094. };
  13095. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  13096. {
  13097. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  13098. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  13099. };
  13100. #endif /* WOLFSSL_AES_128 */
  13101. #ifdef WOLFSSL_AES_256
  13102. int ivlen;
  13103. #endif
  13104. #endif
  13105. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  13106. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  13107. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  13108. wc_test_ret_t ret = 0;
  13109. #ifdef WOLFSSL_AES_256
  13110. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  13111. int alen;
  13112. #endif
  13113. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13114. int plen;
  13115. #endif
  13116. #endif
  13117. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13118. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  13119. byte bufA[sizeof(a) + 1];
  13120. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13121. #endif
  13122. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  13123. #if !defined(BENCH_AESGCM_LARGE)
  13124. #define BENCH_AESGCM_LARGE 1024
  13125. #endif
  13126. #ifndef WOLFSSL_NO_MALLOC
  13127. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13128. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13129. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13130. if ((! large_input) || (! large_output) || (! large_outdec))
  13131. ERROR_OUT(MEMORY_E, out);
  13132. #else
  13133. byte large_input[BENCH_AESGCM_LARGE];
  13134. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  13135. byte large_outdec[BENCH_AESGCM_LARGE];
  13136. #endif
  13137. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  13138. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  13139. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  13140. #endif
  13141. WOLFSSL_ENTER("aesgcm_test");
  13142. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13143. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13144. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13145. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13146. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13147. #endif
  13148. XMEMSET(resultT, 0, sizeof(resultT));
  13149. XMEMSET(resultC, 0, sizeof(resultC));
  13150. XMEMSET(resultP, 0, sizeof(resultP));
  13151. ret = wc_AesInit(enc, HEAP_HINT, devId);
  13152. if (ret != 0)
  13153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13154. ret = wc_AesInit(dec, HEAP_HINT, devId);
  13155. if (ret != 0)
  13156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13157. #ifdef WOLFSSL_AES_256
  13158. ret = wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13159. if (ret != 0)
  13160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13161. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13162. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13163. resultT, sizeof(t1), a, sizeof(a));
  13164. #if defined(WOLFSSL_ASYNC_CRYPT)
  13165. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13166. #endif
  13167. if (ret != 0)
  13168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13169. #ifndef HAVE_RENESAS_SYNC
  13170. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13171. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13172. if (XMEMCMP(t1, resultT, sizeof(t1)))
  13173. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13174. #endif
  13175. #ifdef HAVE_AES_DECRYPT
  13176. ret = wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13177. if (ret != 0)
  13178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13179. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  13180. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13181. #if defined(WOLFSSL_ASYNC_CRYPT)
  13182. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13183. #endif
  13184. if (ret != 0)
  13185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13186. if (XMEMCMP(p, resultP, sizeof(p)))
  13187. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13188. #endif /* HAVE_AES_DECRYPT */
  13189. /* Large buffer test */
  13190. #ifdef BENCH_AESGCM_LARGE
  13191. /* setup test buffer */
  13192. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13193. large_input[alen] = (byte)alen;
  13194. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13195. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13196. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13197. resultT, sizeof(t1), a, sizeof(a));
  13198. #if defined(WOLFSSL_ASYNC_CRYPT)
  13199. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13200. #endif
  13201. if (ret != 0)
  13202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13203. #ifdef HAVE_AES_DECRYPT
  13204. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13205. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13206. sizeof(t1), a, sizeof(a));
  13207. #if defined(WOLFSSL_ASYNC_CRYPT)
  13208. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13209. #endif
  13210. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13211. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13212. if (ret != 0)
  13213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13214. #endif /* HAVE_AES_DECRYPT */
  13215. #endif /* BENCH_AESGCM_LARGE */
  13216. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  13217. /* Variable IV length test */
  13218. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  13219. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13220. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  13221. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13222. #if defined(WOLFSSL_ASYNC_CRYPT)
  13223. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13224. #endif
  13225. if (ret != 0)
  13226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13227. #ifdef HAVE_AES_DECRYPT
  13228. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  13229. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13230. #if defined(WOLFSSL_ASYNC_CRYPT)
  13231. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13232. #endif
  13233. if (ret != 0)
  13234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13235. #endif /* HAVE_AES_DECRYPT */
  13236. }
  13237. #endif
  13238. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  13239. /* Variable authenticated data length test */
  13240. for (alen=0; alen<(int)sizeof(p); alen++) {
  13241. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13242. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13243. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13244. #if defined(WOLFSSL_ASYNC_CRYPT)
  13245. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13246. #endif
  13247. if (ret != 0)
  13248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13249. #ifdef HAVE_AES_DECRYPT
  13250. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13251. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13252. #if defined(WOLFSSL_ASYNC_CRYPT)
  13253. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13254. #endif
  13255. if (ret != 0)
  13256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13257. #endif /* HAVE_AES_DECRYPT */
  13258. }
  13259. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13260. if (! large_aad)
  13261. ERROR_OUT(MEMORY_E, out);
  13262. XMEMSET(large_aad, 0, 1024+16);
  13263. /* Variable authenticated data length test */
  13264. for (alen=0; alen<=1024; alen+=16) {
  13265. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13266. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13267. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13268. if (ret != 0)
  13269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13270. #ifdef HAVE_AES_DECRYPT
  13271. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13272. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13273. if (ret != 0)
  13274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13275. #endif /* HAVE_AES_DECRYPT */
  13276. }
  13277. /* Test unaligned memory of all potential arguments */
  13278. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  13279. if (ret != 0)
  13280. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13281. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13282. XMEMCPY(&buf[1], p, sizeof(p));
  13283. XMEMCPY(&bufA[1], a, sizeof(a));
  13284. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  13285. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13286. if (ret != 0)
  13287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13288. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  13289. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13290. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  13291. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13292. #ifdef HAVE_AES_DECRYPT
  13293. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  13294. if (ret != 0)
  13295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13296. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  13297. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13298. if (ret != 0)
  13299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13300. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  13301. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13302. #endif /* HAVE_AES_DECRYPT */
  13303. #endif /* Xilinx Versal */
  13304. #endif
  13305. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13306. #ifdef BENCH_AESGCM_LARGE
  13307. /* Variable plain text length test */
  13308. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  13309. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13310. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13311. (word32)plen, iv1, sizeof(iv1), resultT,
  13312. sizeof(t1), a, sizeof(a));
  13313. #if defined(WOLFSSL_ASYNC_CRYPT)
  13314. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13315. #endif
  13316. if (ret != 0)
  13317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13318. #ifdef HAVE_AES_DECRYPT
  13319. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13320. (word32)plen, iv1, sizeof(iv1), resultT,
  13321. sizeof(t1), a, sizeof(a));
  13322. #if defined(WOLFSSL_ASYNC_CRYPT)
  13323. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13324. #endif
  13325. if (ret != 0)
  13326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13327. #endif /* HAVE_AES_DECRYPT */
  13328. }
  13329. #else /* BENCH_AESGCM_LARGE */
  13330. /* Variable plain text length test */
  13331. for (plen=1; plen<(int)sizeof(p); plen++) {
  13332. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13333. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  13334. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13335. #if defined(WOLFSSL_ASYNC_CRYPT)
  13336. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13337. #endif
  13338. if (ret != 0)
  13339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13340. #ifdef HAVE_AES_DECRYPT
  13341. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  13342. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13343. #if defined(WOLFSSL_ASYNC_CRYPT)
  13344. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13345. #endif
  13346. if (ret != 0)
  13347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13348. #endif /* HAVE_AES_DECRYPT */
  13349. }
  13350. #endif /* BENCH_AESGCM_LARGE */
  13351. #endif
  13352. #endif /* WOLFSSL_AES_256 */
  13353. /* test with IV != 12 bytes */
  13354. #ifdef ENABLE_NON_12BYTE_IV_TEST
  13355. XMEMSET(resultT, 0, sizeof(resultT));
  13356. XMEMSET(resultC, 0, sizeof(resultC));
  13357. XMEMSET(resultP, 0, sizeof(resultP));
  13358. #ifdef WOLFSSL_AES_192
  13359. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  13360. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13361. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  13362. resultT, sizeof(t1), a, sizeof(a));
  13363. #if defined(WOLFSSL_ASYNC_CRYPT)
  13364. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13365. #endif
  13366. if (ret != 0)
  13367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13368. if (XMEMCMP(c2, resultC, sizeof(c2)))
  13369. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13370. if (XMEMCMP(t2, resultT, sizeof(t1)))
  13371. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13372. #ifdef HAVE_AES_DECRYPT
  13373. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  13374. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  13375. #if defined(WOLFSSL_ASYNC_CRYPT)
  13376. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13377. #endif
  13378. if (ret != 0)
  13379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13380. if (XMEMCMP(p, resultP, sizeof(p)))
  13381. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13382. #endif /* HAVE_AES_DECRYPT */
  13383. /* Large buffer test */
  13384. #ifdef BENCH_AESGCM_LARGE
  13385. wc_AesGcmSetKey(enc, k2, (word32)k3Sz);
  13386. wc_AesGcmSetKey(dec, k2, (word32)k3Sz);
  13387. /* setup test buffer */
  13388. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13389. large_input[alen] = (byte)alen;
  13390. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13391. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13392. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13393. resultT, sizeof(t1), a, sizeof(a));
  13394. #if defined(WOLFSSL_ASYNC_CRYPT)
  13395. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13396. #endif
  13397. if (ret != 0)
  13398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13399. #ifdef HAVE_AES_DECRYPT
  13400. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13401. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13402. sizeof(t1), a, sizeof(a));
  13403. #if defined(WOLFSSL_ASYNC_CRYPT)
  13404. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13405. #endif
  13406. if (ret != 0)
  13407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13408. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13409. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13410. #endif /* HAVE_AES_DECRYPT */
  13411. #endif /* BENCH_AESGCM_LARGE */
  13412. XMEMSET(resultT, 0, sizeof(resultT));
  13413. XMEMSET(resultC, 0, sizeof(resultC));
  13414. XMEMSET(resultP, 0, sizeof(resultP));
  13415. #endif /* WOLFSSL_AES_192 */
  13416. #ifdef WOLFSSL_AES_128
  13417. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13418. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13419. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  13420. resultT, sizeof(t3), a3, sizeof(a3));
  13421. #if defined(WOLFSSL_ASYNC_CRYPT)
  13422. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13423. #endif
  13424. if (ret != 0)
  13425. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13426. #ifndef HAVE_RENESAS_SYNC
  13427. if (XMEMCMP(c3, resultC, sizeof(c3)))
  13428. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13429. if (XMEMCMP(t3, resultT, sizeof(t3)))
  13430. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13431. #endif
  13432. #ifdef HAVE_AES_DECRYPT
  13433. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  13434. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  13435. #if defined(WOLFSSL_ASYNC_CRYPT)
  13436. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13437. #endif
  13438. if (ret != 0)
  13439. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13440. if (XMEMCMP(p3, resultP, sizeof(p3)))
  13441. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13442. #endif /* HAVE_AES_DECRYPT */
  13443. /* Large buffer test */
  13444. #ifdef BENCH_AESGCM_LARGE
  13445. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13446. wc_AesGcmSetKey(dec, k3, (word32)k3Sz);
  13447. /* setup test buffer */
  13448. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13449. large_input[alen] = (byte)alen;
  13450. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13451. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13452. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13453. resultT, sizeof(t1), a, sizeof(a));
  13454. #if defined(WOLFSSL_ASYNC_CRYPT)
  13455. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13456. #endif
  13457. if (ret != 0)
  13458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13459. #ifdef HAVE_AES_DECRYPT
  13460. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13461. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13462. sizeof(t1), a, sizeof(a));
  13463. #if defined(WOLFSSL_ASYNC_CRYPT)
  13464. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13465. #endif
  13466. if (ret != 0)
  13467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13468. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13469. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13470. #endif /* HAVE_AES_DECRYPT */
  13471. #endif /* BENCH_AESGCM_LARGE */
  13472. #endif /* WOLFSSL_AES_128 */
  13473. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  13474. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13475. !defined(WOLFSSL_XILINX_CRYPT) && \
  13476. !(defined(WOLF_CRYPTO_CB) && \
  13477. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  13478. XMEMSET(resultT, 0, sizeof(resultT));
  13479. XMEMSET(resultC, 0, sizeof(resultC));
  13480. XMEMSET(resultP, 0, sizeof(resultP));
  13481. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13482. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13483. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13484. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13485. #if defined(WOLFSSL_ASYNC_CRYPT)
  13486. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13487. #endif
  13488. if (ret != 0)
  13489. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13490. #ifndef HAVE_RENESAS_SYNC
  13491. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13492. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13493. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  13494. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13495. #endif
  13496. #ifdef HAVE_AES_DECRYPT
  13497. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  13498. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13499. #if defined(WOLFSSL_ASYNC_CRYPT)
  13500. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13501. #endif
  13502. if (ret != 0)
  13503. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13504. if (XMEMCMP(p, resultP, sizeof(p)))
  13505. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13506. #endif /* HAVE_AES_DECRYPT */
  13507. #endif /* WOLFSSL_AES_256 */
  13508. #if !defined(HAVE_FIPS) || \
  13509. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13510. /* Test encrypt with internally generated IV */
  13511. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  13512. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  13513. {
  13514. WC_RNG rng;
  13515. byte randIV[12];
  13516. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13517. if (ret != 0)
  13518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13519. XMEMSET(randIV, 0, sizeof(randIV));
  13520. XMEMSET(resultT, 0, sizeof(resultT));
  13521. XMEMSET(resultC, 0, sizeof(resultC));
  13522. XMEMSET(resultP, 0, sizeof(resultP));
  13523. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13524. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  13525. if (ret != 0)
  13526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13527. ret = wc_AesGcmEncrypt_ex(enc,
  13528. resultC, p, sizeof(p),
  13529. randIV, sizeof(randIV),
  13530. resultT, sizeof(t1),
  13531. a, sizeof(a));
  13532. #if defined(WOLFSSL_ASYNC_CRYPT)
  13533. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13534. #endif
  13535. if (ret != 0)
  13536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13537. /* Check the IV has been set. */
  13538. {
  13539. word32 i, ivSum = 0;
  13540. for (i = 0; i < sizeof(randIV); i++)
  13541. ivSum += randIV[i];
  13542. if (ivSum == 0)
  13543. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13544. }
  13545. #ifdef HAVE_AES_DECRYPT
  13546. wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13547. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  13548. if (ret != 0)
  13549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13550. ret = wc_AesGcmDecrypt(dec,
  13551. resultP, resultC, sizeof(c1),
  13552. randIV, sizeof(randIV),
  13553. resultT, sizeof(t1),
  13554. a, sizeof(a));
  13555. #if defined(WOLFSSL_ASYNC_CRYPT)
  13556. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13557. #endif
  13558. if (ret != 0)
  13559. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13560. if (XMEMCMP(p, resultP, sizeof(p)))
  13561. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13562. #endif /* HAVE_AES_DECRYPT */
  13563. wc_FreeRng(&rng);
  13564. }
  13565. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  13566. #endif /* HAVE_FIPS_VERSION >= 2 */
  13567. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13568. #ifdef WOLFSSL_AES_256
  13569. #ifdef WOLFSSL_AESGCM_STREAM
  13570. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13571. if (ret != 0)
  13572. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13573. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  13574. if (ret != 0)
  13575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13576. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13577. if (ret != 0)
  13578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13579. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13580. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13581. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13582. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13583. #ifdef HAVE_AES_DECRYPT
  13584. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13585. if (ret != 0)
  13586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13587. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  13588. if (ret != 0)
  13589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13590. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13591. if (ret != 0)
  13592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13593. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13594. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13595. #endif
  13596. /* alen is the size to pass in with each update. */
  13597. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13598. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13599. if (ret != 0)
  13600. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13601. /* plen is the offset into AAD to update with. */
  13602. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13603. int len = sizeof(a) - plen;
  13604. if (len > alen) len = alen;
  13605. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13606. if (ret != 0)
  13607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13608. }
  13609. /* plen is the offset into plaintext to update with. */
  13610. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  13611. int len = sizeof(p) - plen;
  13612. if (len > alen) len = alen;
  13613. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, (word32)len,
  13614. NULL, 0);
  13615. if (ret != 0)
  13616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13617. }
  13618. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13619. if (ret != 0)
  13620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13621. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13622. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13623. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13624. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13625. }
  13626. #ifdef HAVE_AES_DECRYPT
  13627. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13628. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13629. if (ret != 0)
  13630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13631. /* plen is the offset into AAD to update with. */
  13632. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13633. int len = sizeof(a) - plen;
  13634. if (len > alen) len = alen;
  13635. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13636. if (ret != 0)
  13637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13638. }
  13639. /* plen is the offset into cipher text to update with. */
  13640. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  13641. int len = sizeof(c1) - plen;
  13642. if (len > alen) len = alen;
  13643. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, (word32)len,
  13644. NULL, 0);
  13645. if (ret != 0)
  13646. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13647. }
  13648. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13649. if (ret != 0)
  13650. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13651. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13652. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13653. }
  13654. #endif /* HAVE_AES_DECRYPT */
  13655. #ifdef BENCH_AESGCM_LARGE
  13656. /* setup test buffer */
  13657. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13658. if (ret != 0)
  13659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13660. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  13661. BENCH_AESGCM_LARGE, a, sizeof(a));
  13662. if (ret != 0)
  13663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13664. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13665. if (ret != 0)
  13666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13667. #ifdef HAVE_AES_DECRYPT
  13668. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13669. if (ret != 0)
  13670. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13671. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  13672. BENCH_AESGCM_LARGE, a, sizeof(a));
  13673. if (ret != 0)
  13674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13675. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  13676. if (ret != 0)
  13677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13678. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13679. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13680. #endif /* HAVE_AES_DECRYPT */
  13681. #endif /* BENCH_AESGCM_LARGE */
  13682. #endif /* WOLFSSL_AESGCM_STREAM */
  13683. #endif /* WOLFSSL_AES_256 */
  13684. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  13685. wc_AesFree(enc);
  13686. wc_AesFree(dec);
  13687. ret = 0;
  13688. out:
  13689. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  13690. !defined(WOLFSSL_NO_MALLOC)
  13691. if (large_input)
  13692. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13693. if (large_output)
  13694. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13695. if (large_outdec)
  13696. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13697. #endif
  13698. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13699. if (enc)
  13700. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  13701. if (dec)
  13702. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  13703. #endif
  13704. return ret;
  13705. }
  13706. #ifdef WOLFSSL_AES_128
  13707. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  13708. {
  13709. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13710. Gmac *gmac;
  13711. #else
  13712. Gmac gmac[1];
  13713. #endif
  13714. wc_test_ret_t ret;
  13715. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13716. {
  13717. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  13718. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  13719. };
  13720. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13721. {
  13722. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  13723. 0xe2, 0x8c, 0x8f, 0x16
  13724. };
  13725. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  13726. {
  13727. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  13728. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  13729. };
  13730. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13731. {
  13732. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  13733. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13734. };
  13735. #if (!defined(HAVE_FIPS) || \
  13736. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13737. /* FIPS builds only allow 16-byte auth tags. */
  13738. /* This sample uses a 15-byte auth tag. */
  13739. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13740. {
  13741. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  13742. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  13743. };
  13744. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13745. {
  13746. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  13747. 0x1a, 0x60, 0x24, 0xa7
  13748. };
  13749. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  13750. {
  13751. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  13752. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  13753. };
  13754. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13755. {
  13756. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  13757. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  13758. };
  13759. #endif
  13760. byte tag[16];
  13761. WOLFSSL_ENTER("gmac_test");
  13762. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13763. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13764. return WC_TEST_RET_ENC_ERRNO;
  13765. #endif
  13766. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  13767. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  13768. XMEMSET(tag, 0, sizeof(tag));
  13769. wc_GmacSetKey(gmac, k1, sizeof(k1));
  13770. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  13771. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  13772. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13773. #if (!defined(HAVE_FIPS) || \
  13774. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  13775. XMEMSET(tag, 0, sizeof(tag));
  13776. wc_GmacSetKey(gmac, k2, sizeof(k2));
  13777. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  13778. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  13779. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13780. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  13781. {
  13782. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  13783. {
  13784. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  13785. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13786. };
  13787. WC_RNG rng;
  13788. byte iv[12];
  13789. #ifndef HAVE_FIPS
  13790. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13791. if (ret != 0)
  13792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13793. #else
  13794. ret = wc_InitRng(&rng);
  13795. if (ret != 0)
  13796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13797. #endif
  13798. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13799. t1, sizeof(t1));
  13800. if (ret != 0)
  13801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13802. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13803. badT, sizeof(badT));
  13804. if (ret != AES_GCM_AUTH_E)
  13805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13806. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  13807. t2, sizeof(t2));
  13808. if (ret != 0)
  13809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13810. XMEMSET(tag, 0, sizeof(tag));
  13811. XMEMSET(iv, 0, sizeof(iv));
  13812. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13813. tag, sizeof(tag), &rng);
  13814. if (ret != 0)
  13815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13816. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13817. tag, sizeof(tag));
  13818. if (ret != 0)
  13819. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13820. wc_FreeRng(&rng);
  13821. }
  13822. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  13823. #endif /* HAVE_FIPS */
  13824. ret = 0;
  13825. out:
  13826. wc_AesFree(&gmac->aes);
  13827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13828. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  13829. #endif
  13830. return ret;
  13831. }
  13832. #endif /* WOLFSSL_AES_128 */
  13833. #endif /* HAVE_AESGCM */
  13834. #if defined(HAVE_AESCCM)
  13835. #if defined(WOLFSSL_AES_256)
  13836. static wc_test_ret_t aesccm_256_test(void)
  13837. {
  13838. wc_test_ret_t ret;
  13839. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  13840. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  13841. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13842. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  13843. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  13844. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  13845. };
  13846. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  13847. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  13848. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  13849. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  13850. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  13851. 0x20, 0x21, 0x22, 0x23};
  13852. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  13853. 0x8A, 0xB1, 0xA8, 0x74};
  13854. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  13855. 0x95, 0xFC, 0x08, 0x20};
  13856. byte output[sizeof(in_plaintext)];
  13857. byte atag[sizeof(exp_tag)];
  13858. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13859. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  13860. if (aes == NULL) {
  13861. return MEMORY_E;
  13862. }
  13863. #else
  13864. Aes aes[1];
  13865. #endif
  13866. ret = wc_AesInit(aes, HEAP_HINT, devId);
  13867. if (ret == 0) {
  13868. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  13869. }
  13870. if (ret == 0) {
  13871. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  13872. in_nonce, sizeof(in_nonce),
  13873. atag, sizeof(atag),
  13874. in_auth, sizeof(in_auth));
  13875. }
  13876. /* Verify we produce the proper ciphertext and tag */
  13877. if (ret == 0 &&
  13878. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  13879. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  13880. ret = WC_TEST_RET_ENC_NC;
  13881. }
  13882. #ifdef HAVE_AES_DECRYPT
  13883. if (ret == 0) {
  13884. /* decrypt inline */
  13885. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  13886. in_nonce, sizeof(in_nonce),
  13887. atag, sizeof(atag),
  13888. in_auth, sizeof(in_auth));
  13889. }
  13890. /* Verify decryption was successful */
  13891. if (ret == 0 &&
  13892. XMEMCMP(output, in_plaintext, sizeof(output))) {
  13893. ret = WC_TEST_RET_ENC_NC;
  13894. }
  13895. #endif
  13896. wc_AesFree(aes);
  13897. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13898. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  13899. #endif
  13900. return ret;
  13901. }
  13902. #endif /* WOLFSSL_AES_256 */
  13903. #if defined(WOLFSSL_AES_128)
  13904. static wc_test_ret_t aesccm_128_test(void)
  13905. {
  13906. wc_test_ret_t ret;
  13907. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13908. Aes *enc;
  13909. #else
  13910. Aes enc[1];
  13911. #endif
  13912. /* key */
  13913. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  13914. {
  13915. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  13916. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  13917. };
  13918. /* nonce */
  13919. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  13920. {
  13921. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  13922. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  13923. };
  13924. /* plaintext */
  13925. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13926. {
  13927. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13928. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13929. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  13930. };
  13931. /* plaintext - long */
  13932. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  13933. {
  13934. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13935. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13936. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  13937. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  13938. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  13939. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13940. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  13941. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13942. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  13943. 0x50
  13944. };
  13945. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  13946. {
  13947. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  13948. };
  13949. /* ciphertext */
  13950. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  13951. {
  13952. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  13953. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  13954. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  13955. };
  13956. /* tag - authentication */
  13957. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  13958. {
  13959. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  13960. };
  13961. /* ciphertext - long */
  13962. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  13963. {
  13964. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  13965. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  13966. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  13967. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  13968. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  13969. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  13970. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  13971. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  13972. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  13973. 0x0b
  13974. };
  13975. /* tag - authentication - long */
  13976. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  13977. {
  13978. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  13979. };
  13980. /* tag - authentication - empty plaintext */
  13981. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  13982. {
  13983. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  13984. };
  13985. byte t2[sizeof(t)];
  13986. byte p2[sizeof(p)];
  13987. byte c2[sizeof(c)];
  13988. byte iv2[sizeof(iv)];
  13989. byte pl2[sizeof(pl)];
  13990. byte cl2[sizeof(cl)];
  13991. byte tl2[sizeof(tl)];
  13992. byte t_empty2[sizeof(t_empty)];
  13993. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13994. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13995. return WC_TEST_RET_ENC_ERRNO;
  13996. #endif
  13997. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  13998. XMEMSET(t2, 0, sizeof(t2));
  13999. XMEMSET(c2, 0, sizeof(c2));
  14000. XMEMSET(p2, 0, sizeof(p2));
  14001. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14002. if (ret != 0)
  14003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14004. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14005. if (ret != 0)
  14006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14007. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14008. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14009. t2, sizeof(t2), a, sizeof(a));
  14010. if (ret != 0)
  14011. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14012. if (XMEMCMP(c, c2, sizeof(c2)))
  14013. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14014. if (XMEMCMP(t, t2, sizeof(t2)))
  14015. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14016. #ifdef HAVE_AES_DECRYPT
  14017. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  14018. t2, sizeof(t2), a, sizeof(a));
  14019. if (ret != 0)
  14020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14021. if (XMEMCMP(p, p2, sizeof(p2)))
  14022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14023. /* Test the authentication failure */
  14024. t2[0]++; /* Corrupt the authentication tag. */
  14025. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  14026. t2, sizeof(t2), a, sizeof(a));
  14027. if (ret == 0)
  14028. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14029. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  14030. * authentication fail. */
  14031. XMEMSET(c2, 0, sizeof(c2));
  14032. if (XMEMCMP(p2, c2, sizeof(p2)))
  14033. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14034. #endif
  14035. wc_AesFree(enc);
  14036. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  14037. XMEMSET(t2, 0, sizeof(t2));
  14038. XMEMSET(c2, 0, sizeof(c2));
  14039. XMEMSET(p2, 0, sizeof(p2));
  14040. XMEMSET(iv2, 0, sizeof(iv2));
  14041. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14042. if (ret != 0)
  14043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14044. #ifndef HAVE_SELFTEST
  14045. /* selftest build does not have wc_AesCcmSetNonce() or
  14046. * wc_AesCcmEncrypt_ex() */
  14047. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14048. if (ret != 0)
  14049. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14050. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  14051. if (ret != 0)
  14052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14053. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  14054. t2, sizeof(t2), a, sizeof(a));
  14055. if (ret != 0)
  14056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14057. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  14058. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14059. if (XMEMCMP(c, c2, sizeof(c2)))
  14060. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14061. if (XMEMCMP(t, t2, sizeof(t2)))
  14062. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14063. #endif
  14064. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14065. /* test fail on invalid IV sizes */
  14066. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14067. if (ret != 0)
  14068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14069. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14070. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14071. t2, 1, a, sizeof(a));
  14072. if (ret == 0) {
  14073. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14074. }
  14075. #endif
  14076. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14077. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  14078. tl2, sizeof(tl2), a, sizeof(a));
  14079. if (ret != 0)
  14080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14081. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  14082. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14083. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  14084. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14085. #ifdef HAVE_AES_DECRYPT
  14086. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  14087. tl2, sizeof(tl2), a, sizeof(a));
  14088. if (ret != 0)
  14089. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14090. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  14091. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14092. #endif
  14093. /* test empty message as null input or output with nonzero inSz. */
  14094. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  14095. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14096. a, sizeof(a));
  14097. if (ret != BAD_FUNC_ARG)
  14098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14099. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14100. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14101. a, sizeof(a));
  14102. if (ret != BAD_FUNC_ARG)
  14103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14104. #ifdef HAVE_AES_DECRYPT
  14105. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  14106. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14107. sizeof(a));
  14108. if (ret != BAD_FUNC_ARG)
  14109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14110. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14111. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14112. sizeof(a));
  14113. if (ret != BAD_FUNC_ARG)
  14114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14115. #endif
  14116. /* test empty message as null input and output with zero inSz --
  14117. * must either succeed, or fail early with BAD_FUNC_ARG.
  14118. */
  14119. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  14120. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14121. a, sizeof(a));
  14122. if (ret != BAD_FUNC_ARG) {
  14123. if (ret != 0)
  14124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14125. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14126. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14127. #ifdef HAVE_AES_DECRYPT
  14128. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  14129. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  14130. sizeof(t_empty2), a, sizeof(a));
  14131. if (ret != 0)
  14132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14133. #endif
  14134. }
  14135. /* test empty message as zero-length string -- must work. */
  14136. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  14137. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14138. sizeof(a));
  14139. if (ret != 0)
  14140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14141. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14142. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14143. #ifdef HAVE_AES_DECRYPT
  14144. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  14145. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14146. sizeof(a));
  14147. if (ret != 0)
  14148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14149. #endif
  14150. wc_AesFree(enc);
  14151. ret = 0;
  14152. out:
  14153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14154. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  14155. #endif
  14156. return ret;
  14157. }
  14158. #endif /* WOLFSSL_AES_128 */
  14159. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  14160. {
  14161. wc_test_ret_t ret = 0;
  14162. WOLFSSL_ENTER("aesccm_test");
  14163. #ifdef WOLFSSL_AES_128
  14164. if (ret == 0)
  14165. ret = aesccm_128_test();
  14166. #endif
  14167. #ifdef WOLFSSL_AES_256
  14168. if (ret == 0)
  14169. ret = aesccm_256_test();
  14170. #endif
  14171. return ret;
  14172. }
  14173. #endif /* HAVE_AESCCM */
  14174. #if defined(WOLFSSL_AES_EAX) && \
  14175. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14176. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  14177. {
  14178. typedef struct {
  14179. byte key[AES_256_KEY_SIZE];
  14180. int key_length;
  14181. byte iv[AES_BLOCK_SIZE];
  14182. int iv_length;
  14183. byte aad[AES_BLOCK_SIZE * 2];
  14184. int aad_length;
  14185. byte msg[AES_BLOCK_SIZE * 2];
  14186. int msg_length;
  14187. byte ct[AES_BLOCK_SIZE * 2];
  14188. int ct_length;
  14189. byte tag[AES_BLOCK_SIZE];
  14190. int tag_length;
  14191. int valid;
  14192. } AadVector;
  14193. /* A small selection of Google wycheproof vectors that use vectors
  14194. * from the original paper: eprint.iacr.org/2003/069
  14195. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  14196. */
  14197. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  14198. /* Vector from paper - empty message with auth data */
  14199. {
  14200. /* key, key length */
  14201. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  14202. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  14203. /* iv, iv length */
  14204. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  14205. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  14206. /* aad, aad length */
  14207. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  14208. /* msg, msg length */
  14209. {0}, 0,
  14210. /* ct, ct length */
  14211. {0}, 0,
  14212. /* tag, tag length */
  14213. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  14214. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  14215. /* valid */
  14216. 1,
  14217. },
  14218. /* Vector from paper - no auth data, valid auth tag */
  14219. {
  14220. /* key, key length */
  14221. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14222. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14223. /* iv , iv length */
  14224. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  14225. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  14226. /* aad, aad length */
  14227. {0}, 0,
  14228. /* msg, msg length */
  14229. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14230. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14231. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  14232. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  14233. /* ct, ct length */
  14234. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  14235. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  14236. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  14237. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  14238. /* tag, tag length */
  14239. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  14240. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  14241. /* valid */
  14242. 1,
  14243. },
  14244. /* Vector from paper - no auth data with invalid auth tag */
  14245. {
  14246. /* key, key length */
  14247. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14248. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14249. /* iv, iv length */
  14250. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  14251. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  14252. /* aad, aad length */
  14253. {0}, 0,
  14254. /* msg, msg length */
  14255. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  14256. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  14257. /* ct , ct length */
  14258. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  14259. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  14260. /* tag, tag length */
  14261. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  14262. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  14263. /* valid */
  14264. 0,
  14265. },
  14266. };
  14267. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  14268. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  14269. int i;
  14270. int len;
  14271. wc_test_ret_t ret;
  14272. WOLFSSL_ENTER("aes_eax_test");
  14273. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  14274. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14275. len = sizeof(authtag);
  14276. ret = wc_AesEaxEncryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14277. ciphertext,
  14278. vectors[i].msg, (word32)vectors[i].msg_length,
  14279. vectors[i].iv, (word32)vectors[i].iv_length,
  14280. authtag, (word32)len,
  14281. vectors[i].aad, (word32)vectors[i].aad_length);
  14282. if (ret != 0) {
  14283. return WC_TEST_RET_ENC_EC(ret);
  14284. }
  14285. /* check ciphertext matches vector */
  14286. if (XMEMCMP(ciphertext, vectors[i].ct, (size_t)vectors[i].ct_length)) {
  14287. return WC_TEST_RET_ENC_NC;
  14288. }
  14289. /* check that tag matches vector only for vectors marked as valid */
  14290. ret = XMEMCMP(authtag, vectors[i].tag, len);
  14291. if (vectors[i].valid == 1 && ret != 0 ) {
  14292. return WC_TEST_RET_ENC_NC;
  14293. }
  14294. else if (vectors[i].valid == 0 && ret == 0) {
  14295. return WC_TEST_RET_ENC_NC;
  14296. }
  14297. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14298. ret = wc_AesEaxDecryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14299. ciphertext,
  14300. vectors[i].ct, (word32)vectors[i].ct_length,
  14301. vectors[i].iv, (word32)vectors[i].iv_length,
  14302. authtag, (word32)len,
  14303. vectors[i].aad, (word32)vectors[i].aad_length);
  14304. if (ret != 0) {
  14305. return WC_TEST_RET_ENC_EC(ret);
  14306. }
  14307. /* check decrypted ciphertext matches vector plaintext */
  14308. if (XMEMCMP(ciphertext, vectors[i].msg, (size_t)vectors[i].msg_length)) {
  14309. return WC_TEST_RET_ENC_NC;
  14310. }
  14311. }
  14312. return 0;
  14313. }
  14314. #endif /* WOLFSSL_AES_EAX */
  14315. #ifdef HAVE_AES_KEYWRAP
  14316. #define MAX_KEYWRAP_TEST_OUTLEN 40
  14317. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  14318. typedef struct keywrapVector {
  14319. const byte* kek;
  14320. const byte* data;
  14321. const byte* verify;
  14322. word32 kekLen;
  14323. word32 dataLen;
  14324. word32 verifyLen;
  14325. } keywrapVector;
  14326. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  14327. {
  14328. int wrapSz, plainSz, testSz, i;
  14329. /* test vectors from RFC 3394 (kek, data, verify) */
  14330. #ifdef WOLFSSL_AES_128
  14331. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  14332. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14333. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14334. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14335. };
  14336. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  14337. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14338. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14339. };
  14340. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  14341. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  14342. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  14343. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  14344. };
  14345. #endif /* WOLFSSL_AES_128 */
  14346. #ifdef WOLFSSL_AES_192
  14347. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  14348. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  14349. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14350. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14351. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14352. };
  14353. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  14354. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14355. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14356. };
  14357. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  14358. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  14359. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  14360. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  14361. };
  14362. #endif
  14363. #ifdef WOLFSSL_AES_256
  14364. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  14365. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  14366. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14367. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14368. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14369. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14370. };
  14371. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  14372. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14373. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14374. };
  14375. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  14376. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  14377. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  14378. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  14379. };
  14380. #endif
  14381. #ifdef WOLFSSL_AES_192
  14382. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  14383. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  14384. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14385. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14386. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14387. };
  14388. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  14389. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14390. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14391. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14392. };
  14393. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  14394. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  14395. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  14396. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  14397. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  14398. };
  14399. #endif
  14400. #ifdef WOLFSSL_AES_256
  14401. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  14402. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  14403. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14404. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14405. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14406. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14407. };
  14408. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  14409. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14410. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14411. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14412. };
  14413. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  14414. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  14415. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  14416. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  14417. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  14418. };
  14419. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  14420. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  14421. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14422. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14423. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14424. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14425. };
  14426. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  14427. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14428. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14429. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14430. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14431. };
  14432. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  14433. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  14434. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  14435. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  14436. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  14437. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  14438. };
  14439. #endif /* WOLFSSL_AES_256 */
  14440. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  14441. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  14442. const keywrapVector test_wrap[] =
  14443. {
  14444. #ifdef WOLFSSL_AES_128
  14445. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  14446. #endif
  14447. #ifdef WOLFSSL_AES_192
  14448. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  14449. #endif
  14450. #ifdef WOLFSSL_AES_256
  14451. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  14452. #endif
  14453. #ifdef WOLFSSL_AES_192
  14454. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  14455. #endif
  14456. #ifdef WOLFSSL_AES_256
  14457. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  14458. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  14459. #endif
  14460. };
  14461. WOLFSSL_ENTER("aeskeywrap_test");
  14462. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  14463. XMEMSET(output, 0, sizeof(output));
  14464. XMEMSET(plain, 0, sizeof(plain));
  14465. for (i = 0; i < testSz; i++) {
  14466. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  14467. test_wrap[i].data, test_wrap[i].dataLen,
  14468. output, sizeof(output), NULL);
  14469. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  14470. return WC_TEST_RET_ENC_NC;
  14471. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  14472. return WC_TEST_RET_ENC_NC;
  14473. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  14474. output, (word32)wrapSz,
  14475. plain, sizeof(plain), NULL);
  14476. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  14477. return WC_TEST_RET_ENC_NC;
  14478. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  14479. return WC_TEST_RET_ENC_I(i);
  14480. }
  14481. return 0;
  14482. }
  14483. #endif /* HAVE_AES_KEYWRAP */
  14484. #endif /* NO_AES */
  14485. #ifdef HAVE_ARIA
  14486. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  14487. {
  14488. #ifndef DEBUG_WOLFSSL
  14489. (void)strName;
  14490. (void)data;
  14491. (void)dataSz;
  14492. #else
  14493. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  14494. WOLFSSL_BUFFER(data,dataSz);
  14495. #endif
  14496. }
  14497. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  14498. {
  14499. byte data[] = TEST_STRING;
  14500. word32 dataSz = TEST_STRING_SZ;
  14501. /* Arbitrarily random long key that we will truncate to the right size */
  14502. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  14503. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  14504. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  14505. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  14506. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  14507. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  14508. wc_Aria aria;
  14509. int ret = 0;
  14510. WOLFSSL_ENTER("ariagcm_test");
  14511. XMEMSET((void *)&aria, 0, sizeof(aria));
  14512. ret = wc_AriaInitCrypt(&aria, algo);
  14513. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14514. ret = wc_AriaSetKey(&aria, key);
  14515. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14516. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  14517. printOutput("Key", key, keySz);
  14518. WC_RNG rng;
  14519. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14520. if (ret != 0)
  14521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14522. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  14523. if (ret != 0)
  14524. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14525. wc_FreeRng(&rng);
  14526. printOutput("Plaintext", data, sizeof(data));
  14527. XMEMSET(cipher, 0, sizeof(cipher));
  14528. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  14529. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14530. authTag, authTagSz);
  14531. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14532. printOutput("Ciphertext", cipher, sizeof(cipher));
  14533. printOutput("AuthTag", authTag, sizeof(authTag));
  14534. XMEMSET(plain, 0, sizeof(plain));
  14535. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  14536. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14537. authTag, authTagSz);
  14538. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14539. printOutput("Plaintext", plain, sizeof(plain));
  14540. if (XMEMCMP(plain, data, dataSz) != 0)
  14541. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  14542. out:
  14543. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  14544. else { ret = wc_AriaFreeCrypt(&aria); }
  14545. return ret;
  14546. }
  14547. #endif /* HAVE_ARIA */
  14548. #ifdef HAVE_CAMELLIA
  14549. enum {
  14550. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  14551. };
  14552. typedef struct {
  14553. int type;
  14554. const byte* plaintext;
  14555. const byte* iv;
  14556. const byte* ciphertext;
  14557. const byte* key;
  14558. word32 keySz;
  14559. int errorCode;
  14560. } test_vector_t;
  14561. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  14562. {
  14563. /* Camellia ECB Test Plaintext */
  14564. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  14565. {
  14566. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14567. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14568. };
  14569. /* Camellia ECB Test Initialization Vector */
  14570. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  14571. /* Test 1: Camellia ECB 128-bit key */
  14572. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  14573. {
  14574. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14575. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14576. };
  14577. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  14578. {
  14579. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  14580. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  14581. };
  14582. /* Test 2: Camellia ECB 192-bit key */
  14583. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  14584. {
  14585. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14586. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14587. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14588. };
  14589. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  14590. {
  14591. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  14592. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  14593. };
  14594. /* Test 3: Camellia ECB 256-bit key */
  14595. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  14596. {
  14597. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14598. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14599. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14600. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  14601. };
  14602. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  14603. {
  14604. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  14605. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  14606. };
  14607. /* Camellia CBC Test Plaintext */
  14608. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  14609. {
  14610. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  14611. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  14612. };
  14613. /* Camellia CBC Test Initialization Vector */
  14614. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  14615. {
  14616. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14617. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14618. };
  14619. /* Test 4: Camellia-CBC 128-bit key */
  14620. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  14621. {
  14622. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  14623. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  14624. };
  14625. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  14626. {
  14627. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  14628. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  14629. };
  14630. /* Test 5: Camellia-CBC 192-bit key */
  14631. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  14632. {
  14633. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  14634. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  14635. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  14636. };
  14637. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  14638. {
  14639. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  14640. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  14641. };
  14642. /* Test 6: CBC 256-bit key */
  14643. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  14644. {
  14645. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  14646. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  14647. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  14648. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  14649. };
  14650. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  14651. {
  14652. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  14653. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  14654. };
  14655. byte out[CAMELLIA_BLOCK_SIZE];
  14656. Camellia cam;
  14657. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  14658. {
  14659. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  14660. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  14661. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  14662. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  14663. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  14664. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  14665. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  14666. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  14667. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  14668. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  14669. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  14670. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  14671. };
  14672. int i, testsSz;
  14673. int ret;
  14674. WOLFSSL_ENTER("camellia_test");
  14675. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  14676. for (i = 0; i < testsSz; i++) {
  14677. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  14678. testVectors[i].iv) != 0)
  14679. return testVectors[i].errorCode;
  14680. switch (testVectors[i].type) {
  14681. case CAM_ECB_ENC:
  14682. ret = wc_CamelliaEncryptDirect(&cam, out,
  14683. testVectors[i].plaintext);
  14684. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14685. CAMELLIA_BLOCK_SIZE))
  14686. return testVectors[i].errorCode;
  14687. break;
  14688. case CAM_ECB_DEC:
  14689. ret = wc_CamelliaDecryptDirect(&cam, out,
  14690. testVectors[i].ciphertext);
  14691. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14692. CAMELLIA_BLOCK_SIZE))
  14693. return testVectors[i].errorCode;
  14694. break;
  14695. case CAM_CBC_ENC:
  14696. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  14697. CAMELLIA_BLOCK_SIZE);
  14698. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14699. CAMELLIA_BLOCK_SIZE))
  14700. return testVectors[i].errorCode;
  14701. break;
  14702. case CAM_CBC_DEC:
  14703. ret = wc_CamelliaCbcDecrypt(&cam, out,
  14704. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  14705. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14706. CAMELLIA_BLOCK_SIZE))
  14707. return testVectors[i].errorCode;
  14708. break;
  14709. default:
  14710. break;
  14711. }
  14712. }
  14713. /* Setting the IV and checking it was actually set. */
  14714. ret = wc_CamelliaSetIV(&cam, ivc);
  14715. if (ret != 0)
  14716. return WC_TEST_RET_ENC_EC(ret);
  14717. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  14718. return WC_TEST_RET_ENC_NC;
  14719. /* Setting the IV to NULL should be same as all zeros IV */
  14720. ret = wc_CamelliaSetIV(&cam, NULL);
  14721. if (ret != 0)
  14722. return WC_TEST_RET_ENC_EC(ret);
  14723. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  14724. return WC_TEST_RET_ENC_NC;
  14725. /* First parameter should never be null */
  14726. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  14727. return WC_TEST_RET_ENC_NC;
  14728. /* First parameter should never be null, check it fails */
  14729. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  14730. return WC_TEST_RET_ENC_NC;
  14731. /* Key should have a size of 16, 24, or 32 */
  14732. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  14733. return WC_TEST_RET_ENC_NC;
  14734. return 0;
  14735. }
  14736. #endif /* HAVE_CAMELLIA */
  14737. #ifdef WOLFSSL_SM4
  14738. #ifdef WOLFSSL_SM4_ECB
  14739. static int sm4_ecb_test(void)
  14740. {
  14741. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  14742. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14743. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14744. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14745. };
  14746. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14747. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14748. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14749. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14750. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14751. };
  14752. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  14753. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  14754. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  14755. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  14756. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  14757. };
  14758. wc_Sm4 sm4;
  14759. byte enc[SM4_BLOCK_SIZE * 4];
  14760. byte dec[SM4_BLOCK_SIZE * 4];
  14761. int ret;
  14762. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14763. if (ret != 0)
  14764. return WC_TEST_RET_ENC_EC(ret);
  14765. /* Encrypt and decrypt with ECB. */
  14766. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14767. if (ret != 0)
  14768. return WC_TEST_RET_ENC_EC(ret);
  14769. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  14770. if (ret != 0)
  14771. return WC_TEST_RET_ENC_EC(ret);
  14772. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  14773. return WC_TEST_RET_ENC_NC;
  14774. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  14775. if (ret != 0)
  14776. return WC_TEST_RET_ENC_EC(ret);
  14777. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14778. return WC_TEST_RET_ENC_NC;
  14779. wc_Sm4Free(&sm4);
  14780. return 0;
  14781. }
  14782. #endif
  14783. #ifdef WOLFSSL_SM4_CBC
  14784. static int sm4_cbc_test(void)
  14785. {
  14786. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  14787. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14788. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14789. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14790. };
  14791. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14792. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14793. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14794. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14795. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14796. };
  14797. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14798. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14799. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14800. };
  14801. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  14802. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  14803. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  14804. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  14805. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  14806. };
  14807. wc_Sm4 sm4;
  14808. byte enc[SM4_BLOCK_SIZE * 4];
  14809. byte dec[SM4_BLOCK_SIZE * 4];
  14810. int ret;
  14811. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14812. if (ret != 0)
  14813. return WC_TEST_RET_ENC_EC(ret);
  14814. /* Encrypt and decrypt with CBC. */
  14815. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14816. if (ret != 0)
  14817. return WC_TEST_RET_ENC_EC(ret);
  14818. ret = wc_Sm4SetIV(&sm4, i1);
  14819. if (ret != 0)
  14820. return WC_TEST_RET_ENC_EC(ret);
  14821. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  14822. if (ret != 0)
  14823. return WC_TEST_RET_ENC_EC(ret);
  14824. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14825. return WC_TEST_RET_ENC_NC;
  14826. ret = wc_Sm4SetIV(&sm4, i1);
  14827. if (ret != 0)
  14828. return WC_TEST_RET_ENC_EC(ret);
  14829. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  14830. if (ret != 0)
  14831. return WC_TEST_RET_ENC_EC(ret);
  14832. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14833. return WC_TEST_RET_ENC_NC;
  14834. /* Encrypt and decrypt in-place with CBC. */
  14835. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14836. if (ret != 0)
  14837. return WC_TEST_RET_ENC_EC(ret);
  14838. ret = wc_Sm4SetIV(&sm4, i1);
  14839. if (ret != 0)
  14840. return WC_TEST_RET_ENC_EC(ret);
  14841. XMEMCPY(enc, p1, sizeof(p1));
  14842. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  14843. if (ret != 0)
  14844. return WC_TEST_RET_ENC_EC(ret);
  14845. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14846. return WC_TEST_RET_ENC_NC;
  14847. ret = wc_Sm4SetIV(&sm4, i1);
  14848. if (ret != 0)
  14849. return WC_TEST_RET_ENC_EC(ret);
  14850. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  14851. if (ret != 0)
  14852. return WC_TEST_RET_ENC_EC(ret);
  14853. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  14854. return WC_TEST_RET_ENC_NC;
  14855. wc_Sm4Free(&sm4);
  14856. return 0;
  14857. }
  14858. #endif
  14859. #ifdef WOLFSSL_SM4_CTR
  14860. static int sm4_ctr_test(void)
  14861. {
  14862. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  14863. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14864. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14865. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14866. };
  14867. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14868. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14869. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14870. };
  14871. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  14872. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14873. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  14874. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  14875. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  14876. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14877. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  14878. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14879. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  14880. };
  14881. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  14882. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  14883. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  14884. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  14885. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  14886. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  14887. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  14888. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  14889. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  14890. };
  14891. wc_Sm4 sm4;
  14892. byte enc[SM4_BLOCK_SIZE * 4];
  14893. byte dec[SM4_BLOCK_SIZE * 4];
  14894. int chunk;
  14895. int i;
  14896. int ret;
  14897. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14898. if (ret != 0)
  14899. return WC_TEST_RET_ENC_EC(ret);
  14900. /* Encrypt and decrypt using encrypt with CTR. */
  14901. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14902. if (ret != 0)
  14903. return WC_TEST_RET_ENC_EC(ret);
  14904. ret = wc_Sm4SetIV(&sm4, i1);
  14905. if (ret != 0)
  14906. return WC_TEST_RET_ENC_EC(ret);
  14907. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  14908. if (ret != 0)
  14909. return WC_TEST_RET_ENC_EC(ret);
  14910. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14911. return WC_TEST_RET_ENC_NC;
  14912. ret = wc_Sm4SetIV(&sm4, i1);
  14913. if (ret != 0)
  14914. return WC_TEST_RET_ENC_EC(ret);
  14915. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  14916. if (ret != 0)
  14917. return WC_TEST_RET_ENC_EC(ret);
  14918. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  14919. return WC_TEST_RET_ENC_NC;
  14920. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14921. ret = wc_Sm4SetIV(&sm4, i1);
  14922. if (ret != 0)
  14923. return WC_TEST_RET_ENC_I(chunk);
  14924. XMEMSET(enc, 0, sizeof(enc));
  14925. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  14926. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  14927. if (ret != 0)
  14928. return WC_TEST_RET_ENC_I(i);
  14929. }
  14930. if (i < (int)sizeof(p2)) {
  14931. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  14932. if (ret != 0)
  14933. return WC_TEST_RET_ENC_I(chunk);
  14934. }
  14935. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14936. return WC_TEST_RET_ENC_I(chunk);
  14937. }
  14938. wc_Sm4Free(&sm4);
  14939. return 0;
  14940. }
  14941. #endif
  14942. #ifdef WOLFSSL_SM4_GCM
  14943. static int sm4_gcm_test(void)
  14944. {
  14945. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14946. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14947. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14948. };
  14949. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14950. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14951. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14952. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14953. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14954. };
  14955. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14956. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14957. 0x08, 0x09, 0x0A, 0x0B
  14958. };
  14959. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  14960. 0xFF, 0xEE, 0xDD
  14961. };
  14962. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  14963. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  14964. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  14965. };
  14966. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  14967. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  14968. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  14969. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  14970. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  14971. };
  14972. /* RFC8998 A.1. */
  14973. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  14974. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  14975. 0x00, 0x00, 0xAB, 0xCD
  14976. };
  14977. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  14978. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14979. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14980. };
  14981. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  14982. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14983. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  14984. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  14985. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  14986. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14987. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  14988. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14989. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  14990. };
  14991. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  14992. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  14993. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  14994. 0xAB, 0xAD, 0xDA, 0xD2
  14995. };
  14996. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  14997. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  14998. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  14999. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  15000. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  15001. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  15002. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  15003. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  15004. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  15005. };
  15006. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15007. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  15008. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  15009. };
  15010. wc_Sm4 sm4;
  15011. byte enc[SM4_BLOCK_SIZE * 4];
  15012. byte dec[SM4_BLOCK_SIZE * 4];
  15013. byte tag[SM4_BLOCK_SIZE];
  15014. int ret;
  15015. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15016. if (ret != 0)
  15017. return WC_TEST_RET_ENC_EC(ret);
  15018. /* Encrypt and decrypt using encrypt with GCM. */
  15019. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  15020. if (ret != 0)
  15021. return WC_TEST_RET_ENC_EC(ret);
  15022. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15023. sizeof(tag), a1, sizeof(a1));
  15024. if (ret != 0)
  15025. return WC_TEST_RET_ENC_EC(ret);
  15026. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15027. return WC_TEST_RET_ENC_NC;
  15028. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15029. return WC_TEST_RET_ENC_NC;
  15030. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15031. sizeof(tag), a1, sizeof(a1));
  15032. if (ret != 0)
  15033. return WC_TEST_RET_ENC_EC(ret);
  15034. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15035. return WC_TEST_RET_ENC_NC;
  15036. /* RFC8998 test vector. */
  15037. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  15038. if (ret != 0)
  15039. return WC_TEST_RET_ENC_EC(ret);
  15040. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15041. sizeof(tag), a2, sizeof(a2));
  15042. if (ret != 0)
  15043. return WC_TEST_RET_ENC_EC(ret);
  15044. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15045. return WC_TEST_RET_ENC_NC;
  15046. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15047. return WC_TEST_RET_ENC_NC;
  15048. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15049. sizeof(tag), a2, sizeof(a2));
  15050. if (ret != 0)
  15051. return WC_TEST_RET_ENC_EC(ret);
  15052. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15053. return WC_TEST_RET_ENC_NC;
  15054. wc_Sm4Free(&sm4);
  15055. return 0;
  15056. }
  15057. #endif
  15058. #ifdef WOLFSSL_SM4_CCM
  15059. static int sm4_ccm_test(void)
  15060. {
  15061. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15062. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15063. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15064. };
  15065. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15066. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15067. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15068. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15069. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15070. };
  15071. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15072. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15073. 0x08, 0x09, 0x0A, 0x0B
  15074. };
  15075. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15076. 0xFF, 0xEE, 0xDD
  15077. };
  15078. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15079. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  15080. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  15081. };
  15082. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15083. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  15084. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  15085. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  15086. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  15087. };
  15088. /* RFC8998 A.1. */
  15089. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15090. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15091. 0x00, 0x00, 0xAB, 0xCD
  15092. };
  15093. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15094. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15095. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15096. };
  15097. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15098. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15099. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15100. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15101. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15102. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15103. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15104. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15105. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15106. };
  15107. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15108. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15109. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15110. 0xAB, 0xAD, 0xDA, 0xD2
  15111. };
  15112. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15113. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  15114. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  15115. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  15116. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  15117. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  15118. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  15119. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  15120. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  15121. };
  15122. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15123. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  15124. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  15125. };
  15126. wc_Sm4 sm4;
  15127. byte enc[SM4_BLOCK_SIZE * 4];
  15128. byte dec[SM4_BLOCK_SIZE * 4];
  15129. byte tag[SM4_BLOCK_SIZE];
  15130. int ret;
  15131. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15132. if (ret != 0)
  15133. return WC_TEST_RET_ENC_EC(ret);
  15134. /* Encrypt and decrypt using encrypt with CCM. */
  15135. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  15136. if (ret != 0)
  15137. return WC_TEST_RET_ENC_EC(ret);
  15138. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15139. sizeof(tag), a1, sizeof(a1));
  15140. if (ret != 0)
  15141. return WC_TEST_RET_ENC_EC(ret);
  15142. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15143. return WC_TEST_RET_ENC_NC;
  15144. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15145. return WC_TEST_RET_ENC_NC;
  15146. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15147. sizeof(tag), a1, sizeof(a1));
  15148. if (ret != 0)
  15149. return WC_TEST_RET_ENC_EC(ret);
  15150. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15151. return WC_TEST_RET_ENC_NC;
  15152. /* RFC8998 test vector. */
  15153. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  15154. if (ret != 0)
  15155. return WC_TEST_RET_ENC_EC(ret);
  15156. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15157. sizeof(tag), a2, sizeof(a2));
  15158. if (ret != 0)
  15159. return WC_TEST_RET_ENC_EC(ret);
  15160. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15161. return WC_TEST_RET_ENC_NC;
  15162. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15163. return WC_TEST_RET_ENC_NC;
  15164. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15165. sizeof(tag), a2, sizeof(a2));
  15166. if (ret != 0)
  15167. return WC_TEST_RET_ENC_EC(ret);
  15168. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15169. return WC_TEST_RET_ENC_NC;
  15170. wc_Sm4Free(&sm4);
  15171. return 0;
  15172. }
  15173. #endif
  15174. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  15175. {
  15176. wc_test_ret_t ret;
  15177. WOLFSSL_ENTER("sm4_test");
  15178. #ifdef WOLFSSL_SM4_ECB
  15179. ret = sm4_ecb_test();
  15180. if (ret != 0)
  15181. return ret;
  15182. #endif
  15183. #ifdef WOLFSSL_SM4_CBC
  15184. ret = sm4_cbc_test();
  15185. if (ret != 0)
  15186. return ret;
  15187. #endif
  15188. #ifdef WOLFSSL_SM4_CTR
  15189. ret = sm4_ctr_test();
  15190. if (ret != 0)
  15191. return ret;
  15192. #endif
  15193. #ifdef WOLFSSL_SM4_GCM
  15194. ret = sm4_gcm_test();
  15195. if (ret != 0)
  15196. return ret;
  15197. #endif
  15198. #ifdef WOLFSSL_SM4_CCM
  15199. ret = sm4_ccm_test();
  15200. if (ret != 0)
  15201. return ret;
  15202. #endif
  15203. return 0;
  15204. }
  15205. #endif
  15206. #ifdef HAVE_XCHACHA
  15207. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  15208. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15209. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  15210. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  15211. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  15212. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  15213. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  15214. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  15215. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  15216. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  15217. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  15218. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  15219. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  15220. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  15221. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  15222. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  15223. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  15224. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  15225. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  15226. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  15227. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  15228. };
  15229. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15230. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15231. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15232. };
  15233. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15234. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15235. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  15236. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15237. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  15238. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  15239. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  15240. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  15241. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  15242. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  15243. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  15244. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  15245. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  15246. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  15247. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  15248. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  15249. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  15250. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  15251. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  15252. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  15253. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  15254. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  15255. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  15256. };
  15257. wc_test_ret_t ret;
  15258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15259. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15260. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15261. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15262. WOLFSSL_ENTER("XChaCha_test");
  15263. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  15264. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15265. #else
  15266. struct ChaCha chacha[1];
  15267. byte buf1[sizeof Plaintext];
  15268. byte buf2[sizeof Plaintext];
  15269. WOLFSSL_ENTER("XChaCha_test");
  15270. #endif
  15271. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15272. if (ret < 0)
  15273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15274. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  15275. if (ret < 0)
  15276. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15277. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  15278. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15279. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15280. if (ret < 0)
  15281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15282. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  15283. if (ret < 0)
  15284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15285. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15286. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15287. out:
  15288. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15289. if (chacha)
  15290. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15291. if (buf1)
  15292. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15293. if (buf2)
  15294. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15295. #endif
  15296. return ret;
  15297. }
  15298. #endif /* HAVE_XCHACHA */
  15299. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  15300. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  15301. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15302. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  15303. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  15304. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  15305. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  15306. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  15307. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  15308. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  15309. 0x74, 0x2e }; /* t. */
  15310. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  15311. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15312. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15313. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15314. };
  15315. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15316. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15317. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  15318. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15319. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  15320. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  15321. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  15322. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  15323. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  15324. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  15325. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  15326. 0xb5, 0x2e };
  15327. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  15328. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  15329. };
  15330. wc_test_ret_t ret;
  15331. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15332. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15333. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15334. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15335. if ((buf1 == NULL) || (buf2 == NULL))
  15336. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15337. #else
  15338. byte buf1[sizeof Ciphertext + sizeof Tag];
  15339. byte buf2[sizeof Plaintext];
  15340. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15341. #endif
  15342. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  15343. Plaintext, sizeof Plaintext,
  15344. AAD, sizeof AAD,
  15345. IV, sizeof IV,
  15346. Key, sizeof Key);
  15347. if (ret < 0)
  15348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15349. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  15350. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15351. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  15352. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15353. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  15354. buf1, sizeof Ciphertext + sizeof Tag,
  15355. AAD, sizeof AAD,
  15356. IV, sizeof IV,
  15357. Key, sizeof Key);
  15358. if (ret < 0)
  15359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15360. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15361. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15362. out:
  15363. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15364. if (buf1 != NULL)
  15365. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15366. if (buf2 != NULL)
  15367. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15368. #endif
  15369. return ret;
  15370. }
  15371. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  15372. #ifndef WC_NO_RNG
  15373. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  15374. {
  15375. byte block[32];
  15376. wc_test_ret_t ret;
  15377. int i;
  15378. XMEMSET(block, 0, sizeof(block));
  15379. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  15380. if (ret != 0) {
  15381. ret = 1;
  15382. goto exit;
  15383. }
  15384. /* Check for 0's */
  15385. for (i=0; i<(int)sizeof(block); i++) {
  15386. if (block[i] == 0) {
  15387. ret++;
  15388. }
  15389. }
  15390. /* All zeros count check */
  15391. if (ret >= (int)sizeof(block)) {
  15392. ret = 2;
  15393. goto exit;
  15394. }
  15395. ret = wc_RNG_GenerateByte(rng, block);
  15396. if (ret != 0) {
  15397. ret = 3;
  15398. goto exit;
  15399. }
  15400. /* Parameter validation testing. */
  15401. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  15402. if (ret != BAD_FUNC_ARG) {
  15403. ret = 4;
  15404. goto exit;
  15405. }
  15406. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  15407. if (ret != BAD_FUNC_ARG) {
  15408. ret = 5;
  15409. goto exit;
  15410. }
  15411. ret = wc_RNG_GenerateByte(NULL, block);
  15412. if (ret != BAD_FUNC_ARG) {
  15413. ret = 6;
  15414. goto exit;
  15415. }
  15416. ret = wc_RNG_GenerateByte(rng, NULL);
  15417. if (ret != BAD_FUNC_ARG) {
  15418. ret = 7;
  15419. goto exit;
  15420. }
  15421. ret = 0;
  15422. exit:
  15423. if (ret != 0)
  15424. ret = errorOffset - (ret * 1000000);
  15425. return ret;
  15426. }
  15427. static wc_test_ret_t random_rng_test(void)
  15428. {
  15429. WC_RNG localRng;
  15430. WC_RNG* rng;
  15431. wc_test_ret_t ret;
  15432. rng = &localRng;
  15433. /* Test stack based RNG. */
  15434. #ifndef HAVE_FIPS
  15435. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  15436. #else
  15437. ret = wc_InitRng(rng);
  15438. #endif
  15439. if (ret != 0)
  15440. return WC_TEST_RET_ENC_EC(ret);
  15441. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15442. /* Make sure and free RNG */
  15443. wc_FreeRng(rng);
  15444. if (ret != 0)
  15445. return ret;
  15446. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  15447. {
  15448. byte nonce[8] = { 0 };
  15449. /* Test dynamic RNG */
  15450. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  15451. if (rng == NULL)
  15452. return WC_TEST_RET_ENC_ERRNO;
  15453. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15454. wc_rng_free(rng);
  15455. rng = NULL;
  15456. if (ret != 0)
  15457. return ret;
  15458. /* Test dynamic RNG using extended API */
  15459. ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
  15460. HEAP_HINT, devId);
  15461. if (ret != 0)
  15462. return WC_TEST_RET_ENC_EC(ret);
  15463. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15464. wc_rng_free(rng);
  15465. if (ret != 0)
  15466. return ret;
  15467. }
  15468. #endif
  15469. return ret;
  15470. }
  15471. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  15472. #ifdef WC_RNG_SEED_CB
  15473. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  15474. {
  15475. word32 i;
  15476. (void)os;
  15477. /* Known answer test. Set the seed to the same value every time. */
  15478. for (i = 0; i < sz; i++)
  15479. output[i] = (byte)i;
  15480. return 0;
  15481. }
  15482. static wc_test_ret_t rng_seed_test(void)
  15483. {
  15484. #ifndef HAVE_FIPS
  15485. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15486. {
  15487. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  15488. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  15489. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  15490. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  15491. };
  15492. #else
  15493. /* FIPS uses a longer seed, so different check value. */
  15494. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15495. {
  15496. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  15497. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  15498. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  15499. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  15500. };
  15501. #endif
  15502. byte output[WC_SHA256_DIGEST_SIZE];
  15503. WC_RNG rng;
  15504. wc_test_ret_t ret;
  15505. ret = wc_SetSeed_Cb(seed_cb);
  15506. if (ret != 0) {
  15507. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15508. }
  15509. ret = wc_InitRng(&rng);
  15510. if (ret != 0) {
  15511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15512. }
  15513. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  15514. if (ret != 0) {
  15515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15516. }
  15517. ret = XMEMCMP(output, check, sizeof(output));
  15518. if (ret != 0) {
  15519. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15520. }
  15521. ret = wc_FreeRng(&rng);
  15522. if (ret != 0) {
  15523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15524. }
  15525. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  15526. if (ret != 0) {
  15527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15528. }
  15529. out:
  15530. return ret;
  15531. }
  15532. #endif
  15533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15534. {
  15535. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  15536. {
  15537. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  15538. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  15539. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  15540. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  15541. };
  15542. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  15543. {
  15544. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  15545. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  15546. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  15547. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  15548. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  15549. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  15550. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  15551. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  15552. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  15553. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  15554. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  15555. };
  15556. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  15557. {
  15558. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  15559. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  15560. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  15561. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  15562. };
  15563. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  15564. {
  15565. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  15566. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  15567. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  15568. };
  15569. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  15570. {
  15571. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  15572. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  15573. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  15574. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  15575. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  15576. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  15577. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  15578. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  15579. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  15580. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  15581. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  15582. };
  15583. byte output[WC_SHA256_DIGEST_SIZE * 4];
  15584. wc_test_ret_t ret;
  15585. WOLFSSL_ENTER("random_test");
  15586. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  15587. output, sizeof(output));
  15588. if (ret != 0)
  15589. return WC_TEST_RET_ENC_EC(ret);
  15590. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  15591. return WC_TEST_RET_ENC_NC;
  15592. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  15593. test2EntropyB, sizeof(test2EntropyB),
  15594. output, sizeof(output));
  15595. if (ret != 0)
  15596. return WC_TEST_RET_ENC_EC(ret);
  15597. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  15598. return WC_TEST_RET_ENC_NC;
  15599. /* Basic RNG generate block test */
  15600. if ((ret = random_rng_test()) != 0)
  15601. return ret;
  15602. /* Test the seed check function. */
  15603. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  15604. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  15605. {
  15606. word32 i, outputSz;
  15607. /* Repeat the same byte over and over. Should fail. */
  15608. outputSz = sizeof(output);
  15609. XMEMSET(output, 1, outputSz);
  15610. ret = wc_RNG_TestSeed(output, outputSz);
  15611. if (ret == 0)
  15612. return WC_TEST_RET_ENC_NC;
  15613. /* Every byte of the entropy scratch is different,
  15614. * entropy is a single byte that shouldn't match. */
  15615. outputSz = (sizeof(output) / 2) + 1;
  15616. for (i = 0; i < outputSz; i++)
  15617. output[i] = (byte)i;
  15618. ret = wc_RNG_TestSeed(output, outputSz);
  15619. if (ret != 0)
  15620. return WC_TEST_RET_ENC_EC(ret);
  15621. outputSz = sizeof(output);
  15622. for (i = 0; i < outputSz; i++)
  15623. output[i] = (byte)i;
  15624. ret = wc_RNG_TestSeed(output, outputSz);
  15625. if (ret != 0)
  15626. return WC_TEST_RET_ENC_EC(ret);
  15627. }
  15628. #endif
  15629. /* Test the seed callback. */
  15630. #ifdef WC_RNG_SEED_CB
  15631. if ((ret = rng_seed_test()) != 0)
  15632. return ret;
  15633. #endif
  15634. return 0;
  15635. }
  15636. #else
  15637. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15638. {
  15639. WOLFSSL_ENTER("random_test");
  15640. /* Basic RNG generate block test */
  15641. return random_rng_test();
  15642. }
  15643. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  15644. #endif /* WC_NO_RNG */
  15645. #ifndef MEM_TEST_SZ
  15646. #define MEM_TEST_SZ 1024
  15647. #endif
  15648. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15649. static int simple_mem_test(int sz)
  15650. {
  15651. int ret = 0;
  15652. byte* b;
  15653. int i;
  15654. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15655. if (b == NULL) {
  15656. return WC_TEST_RET_ENC_NC;
  15657. }
  15658. /* utilize memory */
  15659. for (i = 0; i < sz; i++) {
  15660. b[i] = (byte)i;
  15661. }
  15662. /* read back and verify */
  15663. for (i = 0; i < sz; i++) {
  15664. if (b[i] != (byte)i) {
  15665. ret = WC_TEST_RET_ENC_NC;
  15666. break;
  15667. }
  15668. }
  15669. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15670. return ret;
  15671. }
  15672. #endif
  15673. /* If successful, returns the first letter of the byte array `in`.
  15674. **
  15675. ** This is a deceptively simple test of a read-only embedded Linux file system.
  15676. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  15677. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  15678. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  15679. **
  15680. ** Edit with caution. See PR #6523. */
  15681. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  15682. {
  15683. wc_test_ret_t ret = 0;
  15684. volatile word32 j = (word32)-1; /* must be volatile to properly detect error */
  15685. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  15686. (void)ret;
  15687. j = *outJ; /* Found index to use in const array. */
  15688. if (j == 0) {
  15689. #ifdef WOLFSSL_DEBUG
  15690. printf("Testing const byte ptr reference...\n");
  15691. #endif
  15692. /* although j is zero, in[0] does not detect the Illegal instruction */
  15693. ret = in[j]; /* The big test: can we actually access the `in` data? */
  15694. }
  15695. else {
  15696. ret = WC_TEST_RET_ENC_I(j);
  15697. }
  15698. return ret;
  15699. }
  15700. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  15701. {
  15702. wc_test_ret_t ret = 0;
  15703. word32 j = 0; /* used in embedded const pointer test */
  15704. WOLFSSL_ENTER("memory_test");
  15705. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  15706. int i;
  15707. #endif
  15708. #ifdef WOLFSSL_STATIC_MEMORY
  15709. word32 size[] = { WOLFMEM_BUCKETS };
  15710. word32 dist[] = { WOLFMEM_DIST };
  15711. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  15712. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  15713. /* pad to account for if head of buffer is not at set memory
  15714. * alignment when tests are ran */
  15715. #endif
  15716. #ifdef WOLFSSL_STATIC_MEMORY
  15717. /* check macro settings */
  15718. if (sizeof(size)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15719. return WC_TEST_RET_ENC_NC;
  15720. }
  15721. if (sizeof(dist)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15722. return WC_TEST_RET_ENC_NC;
  15723. }
  15724. if (WOLFMEM_DEF_BUCKETS > WOLFMEM_MAX_BUCKETS) {
  15725. return WC_TEST_RET_ENC_NC;
  15726. }
  15727. for (i = 0; i < WOLFMEM_DEF_BUCKETS; i++) {
  15728. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  15729. /* each element in array should be divisible by alignment size */
  15730. return WC_TEST_RET_ENC_NC;
  15731. }
  15732. }
  15733. for (i = 1; i < WOLFMEM_DEF_BUCKETS; i++) {
  15734. if (size[i - 1] >= size[i]) {
  15735. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  15736. }
  15737. }
  15738. #ifndef WOLFSSL_STATIC_MEMORY_LEAN
  15739. /* check that padding size returned is possible */
  15740. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  15741. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  15742. }
  15743. ret = wolfSSL_MemoryPaddingSz();
  15744. if (ret < 0) {
  15745. return WC_TEST_RET_ENC_EC(ret);
  15746. }
  15747. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  15748. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15749. }
  15750. /* check function to return optimum buffer size (rounded down) */
  15751. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  15752. if (ret < 0) {
  15753. return WC_TEST_RET_ENC_EC(ret);
  15754. }
  15755. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  15756. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15757. }
  15758. if ((unsigned int)ret > sizeof(buffer)) {
  15759. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  15760. }
  15761. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  15762. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  15763. * value
  15764. */
  15765. }
  15766. ret = wolfSSL_MemoryPaddingSz();
  15767. ret += pad; /* add space that is going to be needed if buffer not aligned */
  15768. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  15769. (ret + (int)size[0])) {
  15770. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  15771. * value
  15772. */
  15773. }
  15774. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  15775. if ((ret - pad) < 0) {
  15776. return WC_TEST_RET_ENC_NC;
  15777. }
  15778. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  15779. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  15780. }
  15781. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  15782. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  15783. }
  15784. /* check for passing bad or unknown arguments to functions */
  15785. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  15786. return WC_TEST_RET_ENC_NC;
  15787. }
  15788. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  15789. return WC_TEST_RET_ENC_NC; /* should round to 0
  15790. since struct + bucket will not fit */
  15791. }
  15792. #endif
  15793. (void)pad;
  15794. (void)dist; /* avoid static analysis warning of variable not used */
  15795. #endif
  15796. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15797. /* simple test */
  15798. ret = simple_mem_test(MEM_TEST_SZ);
  15799. if (ret != 0)
  15800. return ret;
  15801. #endif
  15802. #ifdef COMPLEX_MEM_TEST
  15803. /* test various size blocks */
  15804. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  15805. ret = simple_mem_test(i);
  15806. if (ret != 0)
  15807. return ret;
  15808. }
  15809. #endif
  15810. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  15811. /* realloc test */
  15812. {
  15813. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  15814. DYNAMIC_TYPE_TMP_BUFFER);
  15815. #ifndef WOLFSSL_NO_REALLOC
  15816. byte *c = NULL;
  15817. if (b) {
  15818. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  15819. DYNAMIC_TYPE_TMP_BUFFER);
  15820. if (c)
  15821. b = c;
  15822. }
  15823. #endif
  15824. if (b)
  15825. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15826. if ((b == NULL)
  15827. #ifndef WOLFSSL_NO_REALLOC
  15828. || (c == NULL)
  15829. #endif
  15830. ) {
  15831. return WC_TEST_RET_ENC_ERRNO;
  15832. }
  15833. }
  15834. #endif
  15835. if (ret == 0) {
  15836. /* This test is only interesting on embedded R/O Flash systems */
  15837. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  15838. ret = 1;
  15839. }
  15840. }
  15841. return ret;
  15842. }
  15843. #ifndef NO_FILESYSTEM
  15844. /* Cert Paths */
  15845. #ifdef FREESCALE_MQX
  15846. #define CERT_PREFIX "a:\\"
  15847. #define CERT_PATH_SEP "\\"
  15848. #elif defined(WOLFSSL_uTKERNEL2)
  15849. #define CERT_PREFIX "/uda/"
  15850. #define CERT_PATH_SEP "/"
  15851. #elif defined(_WIN32_WCE)
  15852. #define CERT_PREFIX "\\windows\\"
  15853. #define CERT_PATH_SEP "\\"
  15854. #endif
  15855. #ifndef CERT_PREFIX
  15856. #define CERT_PREFIX "./"
  15857. #endif
  15858. #ifndef CERT_PATH_SEP
  15859. #define CERT_PATH_SEP "/"
  15860. #endif
  15861. #ifndef CERT_WRITE_TEMP_DIR
  15862. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  15863. #endif
  15864. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  15865. /* Generated Test Certs */
  15866. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15867. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15868. #if !defined(NO_RSA) && !defined(NO_ASN)
  15869. static const char* clientKey = CERT_ROOT "client-key.der";
  15870. static const char* clientCert = CERT_ROOT "client-cert.der";
  15871. #ifdef WOLFSSL_CERT_EXT
  15872. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  15873. #endif
  15874. #endif /* !NO_RSA && !NO_ASN */
  15875. #endif
  15876. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15877. #if !defined(NO_RSA) && !defined(NO_ASN)
  15878. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  15879. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  15880. #ifdef WOLFSSL_CERT_GEN
  15881. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  15882. #endif
  15883. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  15884. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  15885. #endif
  15886. #ifdef HAVE_PKCS7
  15887. static const char* rsaServerCertDerFile =
  15888. CERT_ROOT "server-cert.der";
  15889. static const char* rsaServerKeyDerFile =
  15890. CERT_ROOT "server-key.der";
  15891. #endif
  15892. #endif
  15893. #endif /* !NO_RSA && !NO_ASN */
  15894. #endif /* !USE_CERT_BUFFER_* */
  15895. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15896. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15897. #if !defined(NO_ASN) && !defined(NO_DH)
  15898. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  15899. #endif
  15900. #endif
  15901. #if !defined(NO_ASN) && !defined(NO_DH)
  15902. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  15903. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15904. #if !defined(USE_CERT_BUFFERS_2048)
  15905. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  15906. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  15907. #endif
  15908. #endif
  15909. #endif
  15910. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15911. #ifndef NO_DSA
  15912. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  15913. #endif
  15914. #endif /* !USE_CERT_BUFFER_* */
  15915. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  15916. #ifdef HAVE_ECC
  15917. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  15918. #ifdef HAVE_ECC_KEY_IMPORT
  15919. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  15920. #endif
  15921. #endif
  15922. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  15923. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  15924. !defined(NO_ECC_SECP)
  15925. #ifndef NO_RSA
  15926. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  15927. #endif
  15928. #ifndef NO_ASN_TIME
  15929. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  15930. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  15931. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  15932. static const char* eccCaKey384File =
  15933. CERT_ROOT "ca-ecc384-key.der";
  15934. static const char* eccCaCert384File =
  15935. CERT_ROOT "ca-ecc384-cert.pem";
  15936. #endif
  15937. #endif
  15938. #endif
  15939. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  15940. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  15941. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  15942. #endif
  15943. #endif /* HAVE_ECC */
  15944. #ifdef HAVE_ED25519
  15945. #ifdef WOLFSSL_TEST_CERT
  15946. static const char* serverEd25519Cert =
  15947. CERT_ROOT "ed25519/server-ed25519.der";
  15948. static const char* caEd25519Cert =
  15949. CERT_ROOT "ed25519/ca-ed25519.der";
  15950. #endif
  15951. #endif
  15952. #ifdef HAVE_ED448
  15953. #ifdef WOLFSSL_TEST_CERT
  15954. static const char* serverEd448Cert =
  15955. CERT_ROOT "ed448/server-ed448.der";
  15956. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  15957. #endif
  15958. #endif
  15959. #endif /* !USE_CERT_BUFFER_* */
  15960. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  15961. !defined(NO_FILESYSTEM)
  15962. static const char* certExtNc =
  15963. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  15964. static const char* certExtIa =
  15965. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  15966. static const char* certExtNct =
  15967. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  15968. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  15969. static const char* certBadNegInt =
  15970. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-neg-int.der";
  15971. #endif
  15972. static const char* certBadOid =
  15973. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
  15974. #ifndef WOLFSSL_NO_ASN_STRICT
  15975. static const char* certBadUtf8 =
  15976. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
  15977. #endif
  15978. #endif
  15979. #ifndef NO_WRITE_TEMP_FILES
  15980. #ifdef HAVE_ECC
  15981. #ifndef NO_ECC_SECP
  15982. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  15983. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  15984. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  15985. #endif
  15986. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  15987. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  15988. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  15989. #endif
  15990. #endif
  15991. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  15992. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  15993. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  15994. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  15995. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  15996. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  15997. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  15998. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  15999. #endif
  16000. #endif /* HAVE_ECC_KEY_EXPORT */
  16001. #endif /* HAVE_ECC */
  16002. #ifndef NO_RSA
  16003. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16004. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  16005. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  16006. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  16007. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  16008. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16009. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  16010. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  16011. #endif
  16012. #endif
  16013. #endif /* !NO_RSA */
  16014. #if !defined(NO_RSA) || !defined(NO_DSA)
  16015. #ifdef WOLFSSL_KEY_GEN
  16016. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  16017. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  16018. #endif
  16019. #endif
  16020. #endif /* !NO_WRITE_TEMP_FILES */
  16021. #endif /* !NO_FILESYSTEM */
  16022. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  16023. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  16024. static CertName certDefaultName;
  16025. static void initDefaultName(void)
  16026. {
  16027. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16028. NameAttrib* n;
  16029. #endif
  16030. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  16031. certDefaultName.countryEnc = CTC_PRINTABLE;
  16032. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  16033. certDefaultName.stateEnc = CTC_UTF8;
  16034. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  16035. certDefaultName.streetEnc = CTC_UTF8;
  16036. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  16037. certDefaultName.localityEnc = CTC_UTF8;
  16038. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  16039. certDefaultName.surEnc = CTC_UTF8;
  16040. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  16041. certDefaultName.orgEnc = CTC_UTF8;
  16042. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  16043. certDefaultName.unitEnc = CTC_UTF8;
  16044. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  16045. certDefaultName.commonNameEnc = CTC_UTF8;
  16046. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  16047. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  16048. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  16049. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  16050. #ifdef WOLFSSL_CERT_EXT
  16051. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  16052. certDefaultName.busCatEnc = CTC_UTF8;
  16053. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  16054. certDefaultName.joiStEnc = CTC_PRINTABLE;
  16055. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  16056. certDefaultName.joiCEnc = CTC_PRINTABLE;
  16057. #endif
  16058. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  16059. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  16060. certDefaultName.userIdEnc = CTC_PRINTABLE;
  16061. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16062. /* test having additional OUs and setting DC */
  16063. n = &certDefaultName.name[0];
  16064. n->id = ASN_ORGUNIT_NAME;
  16065. n->type = CTC_UTF8;
  16066. n->sz = XSTRLEN("Development-2");
  16067. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  16068. #if CTC_MAX_ATTRIB > 3
  16069. n = &certDefaultName.name[1];
  16070. n->id = ASN_DOMAIN_COMPONENT;
  16071. n->type = CTC_UTF8;
  16072. n->sz = XSTRLEN("com");
  16073. XMEMCPY(n->value, "com", sizeof("com"));
  16074. n = &certDefaultName.name[2];
  16075. n->id = ASN_DOMAIN_COMPONENT;
  16076. n->type = CTC_UTF8;
  16077. n->sz = XSTRLEN("wolfssl");
  16078. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  16079. #endif
  16080. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  16081. #ifdef WOLFSSL_CUSTOM_OID
  16082. /* TODO: Add test case for custom OID's */
  16083. #endif
  16084. }
  16085. #ifdef WOLFSSL_CERT_EXT
  16086. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  16087. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  16088. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  16089. "digitalSignature,nonRepudiation";
  16090. #endif
  16091. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  16092. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  16093. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  16094. #endif
  16095. #endif /* WOLFSSL_CERT_EXT */
  16096. #endif /* WOLFSSL_CERT_GEN */
  16097. #ifndef NO_RSA
  16098. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16099. !defined(NO_FILESYSTEM)
  16100. static const byte minSerial[] = { 0x02, 0x01, 0x01 };
  16101. static const byte minName[] = { 0x30, 0x00 };
  16102. static const byte nameBad[] = {
  16103. 0x30, 0x08,
  16104. 0x31, 0x06,
  16105. 0x30, 0x04,
  16106. 0x06, 0x02,
  16107. 0x55, 0x04,
  16108. };
  16109. static const byte minDates[] = {
  16110. 0x30, 0x1e,
  16111. 0x17, 0x0d,
  16112. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  16113. 0x32, 0x33, 0x31, 0x30, 0x5a,
  16114. 0x17, 0x0d,
  16115. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  16116. 0x32, 0x33, 0x31, 0x30, 0x5a
  16117. };
  16118. static const byte minPubKey[] = {
  16119. 0x30, 0x1c,
  16120. 0x30, 0x0d,
  16121. 0x06, 0x09,
  16122. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16123. 0x01,
  16124. 0x05, 0x00,
  16125. 0x03, 0x0b,
  16126. 0x00, 0x30, 0x08,
  16127. 0x02, 0x01,
  16128. 0x03,
  16129. 0x02, 0x03,
  16130. 0x01, 0x00, 0x01
  16131. };
  16132. static const byte minSigAlg[] = {
  16133. 0x30, 0x0d,
  16134. 0x06, 0x09,
  16135. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16136. 0x0b,
  16137. 0x05, 0x00
  16138. };
  16139. static const byte minSig[] = {
  16140. 0x03, 0x01,
  16141. 0x00
  16142. };
  16143. static int add_seq(byte* certData, int offset, byte* data, byte length)
  16144. {
  16145. XMEMMOVE(certData + offset + 2, data, length);
  16146. certData[offset++] = 0x30;
  16147. certData[offset++] = length;
  16148. return offset + length;
  16149. }
  16150. static int add_data(byte* certData, int offset, const byte* data, byte length)
  16151. {
  16152. XMEMCPY(certData + offset, data, length);
  16153. return offset + length;
  16154. }
  16155. static wc_test_ret_t cert_asn1_test(void)
  16156. {
  16157. wc_test_ret_t ret;
  16158. int len[3];
  16159. DecodedCert cert;
  16160. byte certData[114];
  16161. byte* badCert = NULL;
  16162. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16163. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16164. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16165. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16166. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16167. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16168. len[1] = add_seq(certData, 0, certData, len[2]);
  16169. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16170. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16171. len[0] = add_seq(certData, 0, certData, len[1]);
  16172. /* Minimal good certificate */
  16173. InitDecodedCert(&cert, certData, len[0], 0);
  16174. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16175. FreeDecodedCert(&cert);
  16176. if (ret != 0)
  16177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16178. /* Bad issuer name */
  16179. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16180. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16181. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  16182. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16183. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16184. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16185. len[1] = add_seq(certData, 0, certData, len[2]);
  16186. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16187. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16188. len[0] = add_seq(certData, 0, certData, len[1]);
  16189. /* Put data into allocated buffer to allow access error checking. */
  16190. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16191. XMEMCPY(badCert, certData, len[0]);
  16192. InitDecodedCert(&cert, badCert, len[0], 0);
  16193. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16194. FreeDecodedCert(&cert);
  16195. if (ret != ASN_PARSE_E) {
  16196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16197. }
  16198. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16199. badCert = NULL;
  16200. ret = 0;
  16201. done:
  16202. if (badCert != NULL)
  16203. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16204. return ret;
  16205. }
  16206. static wc_test_ret_t cert_load_bad(const char* fname, byte* tmp, int err)
  16207. {
  16208. wc_test_ret_t ret;
  16209. DecodedCert cert;
  16210. XFILE file;
  16211. size_t bytes;
  16212. if ((fname == NULL) || (tmp == NULL)) {
  16213. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16214. }
  16215. file = XFOPEN(fname, "rb");
  16216. if (!file) {
  16217. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16218. }
  16219. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16220. XFCLOSE(file);
  16221. if (bytes == 0) {
  16222. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16223. }
  16224. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16225. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16226. FreeDecodedCert(&cert);
  16227. if (ret != err) {
  16228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16229. }
  16230. ret = 0;
  16231. done:
  16232. return ret;
  16233. }
  16234. static wc_test_ret_t cert_bad_asn1_test(void)
  16235. {
  16236. wc_test_ret_t ret = 0;
  16237. byte* tmp;
  16238. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16239. if (tmp == NULL) {
  16240. ret = WC_TEST_RET_ENC_ERRNO;
  16241. }
  16242. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16243. if (ret == 0) {
  16244. /* Serial number: 0xff 0xa8. 0xff and top bit set on next byte invalid.
  16245. */
  16246. ret = cert_load_bad(certBadNegInt, tmp, ASN_EXPECT_0_E);
  16247. }
  16248. #endif
  16249. if (ret == 0) {
  16250. /* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
  16251. ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
  16252. }
  16253. #ifndef WOLFSSL_NO_ASN_STRICT
  16254. if (ret == 0) {
  16255. /* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
  16256. */
  16257. ret = cert_load_bad(certBadUtf8, tmp, ASN_PARSE_E);
  16258. }
  16259. #endif
  16260. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16261. return ret;
  16262. }
  16263. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  16264. {
  16265. #if !defined(NO_FILESYSTEM)
  16266. DecodedCert cert;
  16267. byte* tmp;
  16268. size_t bytes;
  16269. XFILE file;
  16270. wc_test_ret_t ret;
  16271. WOLFSSL_ENTER("cert_test");
  16272. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16273. if (tmp == NULL)
  16274. return WC_TEST_RET_ENC_ERRNO;
  16275. /* Certificate with Name Constraints extension. */
  16276. file = XFOPEN(certExtNc, "rb");
  16277. if (!file) {
  16278. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16279. }
  16280. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16281. XFCLOSE(file);
  16282. if (bytes == 0)
  16283. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16284. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16285. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16286. if (ret != 0)
  16287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16288. FreeDecodedCert(&cert);
  16289. /* Certificate with Inhibit Any Policy extension. */
  16290. file = XFOPEN(certExtIa, "rb");
  16291. if (!file) {
  16292. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16293. }
  16294. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16295. XFCLOSE(file);
  16296. if (bytes == 0)
  16297. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16298. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16299. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16300. if (ret != 0)
  16301. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16302. FreeDecodedCert(&cert);
  16303. /* Certificate with Netscape Certificate Type extension. */
  16304. file = XFOPEN(certExtNct, "rb");
  16305. if (!file) {
  16306. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16307. }
  16308. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16309. XFCLOSE(file);
  16310. if (bytes == 0)
  16311. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16312. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16313. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16314. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  16315. if (ret != 0)
  16316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16317. #else
  16318. if (ret != ASN_CRIT_EXT_E) {
  16319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16320. }
  16321. ret = 0;
  16322. #endif
  16323. done:
  16324. FreeDecodedCert(&cert);
  16325. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16326. #endif /* !NO_FILESYSTEM */
  16327. if (ret == 0)
  16328. ret = cert_asn1_test();
  16329. if (ret == 0)
  16330. ret = cert_bad_asn1_test();
  16331. return ret;
  16332. }
  16333. #endif /* WOLFSSL_TEST_CERT */
  16334. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  16335. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  16336. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  16337. {
  16338. DecodedCert cert;
  16339. byte* tmp;
  16340. size_t bytes;
  16341. XFILE file;
  16342. wc_test_ret_t ret;
  16343. /* created from rsa_test : othercert.der */
  16344. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16345. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16346. /* created from rsa_test : othercert.der */
  16347. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  16348. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  16349. #ifdef HAVE_ECC
  16350. /* created from ecc_test_cert_gen : certecc.der */
  16351. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  16352. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  16353. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  16354. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  16355. #else
  16356. /* Authority key id from ./certs/ca-ecc-cert.pem */
  16357. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  16358. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  16359. #endif
  16360. #endif /* HAVE_ECC */
  16361. /* created from rsa_test : cert.der */
  16362. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16363. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16364. WOLFSSL_ENTER("certext_test");
  16365. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16366. if (tmp == NULL)
  16367. return WC_TEST_RET_ENC_ERRNO;
  16368. /* load othercert.der (Cert signed by an authority) */
  16369. file = XFOPEN(otherCertDerFile, "rb");
  16370. if (!file) {
  16371. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16372. return WC_TEST_RET_ENC_ERRNO;
  16373. }
  16374. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16375. XFCLOSE(file);
  16376. if (bytes == 0)
  16377. return WC_TEST_RET_ENC_ERRNO;
  16378. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16379. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16380. if (ret != 0)
  16381. return WC_TEST_RET_ENC_EC(ret);
  16382. /* check the SKID from a RSA certificate */
  16383. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16384. return WC_TEST_RET_ENC_NC;
  16385. /* check the AKID from an RSA certificate */
  16386. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16387. return WC_TEST_RET_ENC_NC;
  16388. /* check the Key Usage from an RSA certificate */
  16389. if (!cert.extKeyUsageSet)
  16390. return WC_TEST_RET_ENC_NC;
  16391. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  16392. return WC_TEST_RET_ENC_NC;
  16393. /* check the CA Basic Constraints from an RSA certificate */
  16394. if (cert.isCA)
  16395. return WC_TEST_RET_ENC_NC;
  16396. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16397. /* check the Certificate Policies Id */
  16398. if (cert.extCertPoliciesNb != 1)
  16399. return WC_TEST_RET_ENC_NC;
  16400. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16401. return WC_TEST_RET_ENC_NC;
  16402. #endif
  16403. FreeDecodedCert(&cert);
  16404. #ifdef HAVE_ECC
  16405. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  16406. file = XFOPEN(certEccDerFile, "rb");
  16407. if (!file) {
  16408. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16409. return WC_TEST_RET_ENC_ERRNO;
  16410. }
  16411. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16412. XFCLOSE(file);
  16413. if (bytes == 0)
  16414. return WC_TEST_RET_ENC_ERRNO;
  16415. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16416. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16417. if (ret != 0)
  16418. return WC_TEST_RET_ENC_EC(ret);
  16419. /* check the SKID from a ECC certificate - generated dynamically */
  16420. /* check the AKID from an ECC certificate */
  16421. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16422. return WC_TEST_RET_ENC_NC;
  16423. /* check the Key Usage from an ECC certificate */
  16424. if (!cert.extKeyUsageSet)
  16425. return WC_TEST_RET_ENC_NC;
  16426. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  16427. return WC_TEST_RET_ENC_NC;
  16428. /* check the CA Basic Constraints from an ECC certificate */
  16429. if (cert.isCA)
  16430. return WC_TEST_RET_ENC_NC;
  16431. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16432. /* check the Certificate Policies Id */
  16433. if (cert.extCertPoliciesNb != 2)
  16434. return WC_TEST_RET_ENC_NC;
  16435. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  16436. return WC_TEST_RET_ENC_NC;
  16437. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  16438. return WC_TEST_RET_ENC_NC;
  16439. #endif
  16440. FreeDecodedCert(&cert);
  16441. #endif /* HAVE_ECC */
  16442. /* load cert.der (self signed certificate) */
  16443. file = XFOPEN(certDerFile, "rb");
  16444. if (!file) {
  16445. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16446. return WC_TEST_RET_ENC_ERRNO;
  16447. }
  16448. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16449. XFCLOSE(file);
  16450. if (bytes == 0)
  16451. return WC_TEST_RET_ENC_ERRNO;
  16452. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16453. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16454. if (ret != 0)
  16455. return WC_TEST_RET_ENC_EC(ret);
  16456. /* check the SKID from a CA certificate */
  16457. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16458. return WC_TEST_RET_ENC_NC;
  16459. /* check the AKID from an CA certificate */
  16460. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16461. return WC_TEST_RET_ENC_NC;
  16462. /* check the Key Usage from CA certificate */
  16463. if (!cert.extKeyUsageSet)
  16464. return WC_TEST_RET_ENC_NC;
  16465. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  16466. return WC_TEST_RET_ENC_NC;
  16467. /* check the CA Basic Constraints CA certificate */
  16468. if (!cert.isCA)
  16469. return WC_TEST_RET_ENC_NC;
  16470. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16471. /* check the Certificate Policies Id */
  16472. if (cert.extCertPoliciesNb != 2)
  16473. return WC_TEST_RET_ENC_NC;
  16474. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16475. return WC_TEST_RET_ENC_NC;
  16476. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  16477. return WC_TEST_RET_ENC_NC;
  16478. #endif
  16479. FreeDecodedCert(&cert);
  16480. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16481. return 0;
  16482. }
  16483. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  16484. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  16485. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  16486. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  16487. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  16488. {
  16489. wc_test_ret_t ret = 0;
  16490. Cert cert;
  16491. FILE* file;
  16492. byte* der;
  16493. word32 derSz;
  16494. WOLFSSL_ENTER("decodedCertCache_test");
  16495. derSz = FOURK_BUF;
  16496. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16497. if (der == NULL)
  16498. ret = WC_TEST_RET_ENC_NC;
  16499. if (ret == 0) {
  16500. /* load cert.der */
  16501. file = XFOPEN(certDerFile, "rb");
  16502. if (file != NULL) {
  16503. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  16504. XFCLOSE(file);
  16505. if (derSz == 0)
  16506. ret = WC_TEST_RET_ENC_ERRNO;
  16507. }
  16508. else
  16509. ret = WC_TEST_RET_ENC_ERRNO;
  16510. }
  16511. if (ret == 0) {
  16512. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  16513. if (ret != 0)
  16514. ret = WC_TEST_RET_ENC_EC(ret);
  16515. }
  16516. if (ret == 0) {
  16517. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  16518. if (ret != 0)
  16519. ret = WC_TEST_RET_ENC_EC(ret);
  16520. }
  16521. if (ret == 0) {
  16522. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  16523. if (ret == BAD_FUNC_ARG)
  16524. ret = 0;
  16525. else
  16526. ret = WC_TEST_RET_ENC_EC(ret);
  16527. }
  16528. if (ret == 0) {
  16529. ret = wc_SetSubjectRaw(&cert, der, derSz);
  16530. if (ret != 0)
  16531. ret = WC_TEST_RET_ENC_EC(ret);
  16532. }
  16533. if (ret == 0) {
  16534. ret = wc_SetSubjectRaw(NULL, der, derSz);
  16535. if (ret == BAD_FUNC_ARG)
  16536. ret = 0;
  16537. else
  16538. ret = WC_TEST_RET_ENC_EC(ret);
  16539. }
  16540. if (ret == 0) {
  16541. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  16542. if (ret != 0)
  16543. ret = WC_TEST_RET_ENC_EC(ret);
  16544. }
  16545. if (ret == 0) {
  16546. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  16547. if (ret == BAD_FUNC_ARG)
  16548. ret = 0;
  16549. else
  16550. ret = WC_TEST_RET_ENC_EC(ret);
  16551. }
  16552. if (ret == 0) {
  16553. ret = wc_SetIssuerRaw(&cert, der, derSz);
  16554. if (ret != 0)
  16555. ret = WC_TEST_RET_ENC_EC(ret);
  16556. }
  16557. if (ret == 0) {
  16558. ret = wc_SetIssuerRaw(NULL, der, derSz);
  16559. if (ret == BAD_FUNC_ARG)
  16560. ret = 0;
  16561. else
  16562. ret = WC_TEST_RET_ENC_EC(ret);
  16563. }
  16564. #ifdef WOLFSSL_ALT_NAMES
  16565. if (ret == 0) {
  16566. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  16567. if (ret != 0)
  16568. ret = WC_TEST_RET_ENC_EC(ret);
  16569. }
  16570. if (ret == 0) {
  16571. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  16572. if (ret == BAD_FUNC_ARG)
  16573. ret = 0;
  16574. else
  16575. ret = WC_TEST_RET_ENC_EC(ret);
  16576. }
  16577. if (ret == 0) {
  16578. ret = wc_SetDatesBuffer(&cert, der, derSz);
  16579. if (ret != 0)
  16580. ret = WC_TEST_RET_ENC_EC(ret);
  16581. }
  16582. if (ret == 0) {
  16583. ret = wc_SetDatesBuffer(NULL, der, derSz);
  16584. if (ret == BAD_FUNC_ARG)
  16585. ret = 0;
  16586. else
  16587. ret = WC_TEST_RET_ENC_EC(ret);
  16588. }
  16589. #endif
  16590. if (ret == 0) {
  16591. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  16592. if (ret != 0)
  16593. ret = WC_TEST_RET_ENC_EC(ret);
  16594. }
  16595. if (ret == 0) {
  16596. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  16597. if (ret == BAD_FUNC_ARG)
  16598. ret = 0;
  16599. else
  16600. ret = WC_TEST_RET_ENC_NC;
  16601. }
  16602. wc_SetCert_Free(&cert);
  16603. if (ret == 0) {
  16604. if(cert.decodedCert != NULL)
  16605. ret = WC_TEST_RET_ENC_NC;
  16606. }
  16607. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16608. return ret;
  16609. }
  16610. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  16611. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  16612. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  16613. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16614. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16615. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  16616. {
  16617. wc_test_ret_t ret;
  16618. byte e[RSA_TEST_BYTES];
  16619. byte n[RSA_TEST_BYTES];
  16620. word32 eSz = sizeof(e);
  16621. word32 nSz = sizeof(n);
  16622. /* Parameter Validation testing. */
  16623. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16624. if (ret != BAD_FUNC_ARG)
  16625. return WC_TEST_RET_ENC_EC(ret);
  16626. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  16627. if (ret != BAD_FUNC_ARG)
  16628. return WC_TEST_RET_ENC_EC(ret);
  16629. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  16630. if (ret != BAD_FUNC_ARG)
  16631. return WC_TEST_RET_ENC_EC(ret);
  16632. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  16633. if (ret != BAD_FUNC_ARG)
  16634. return WC_TEST_RET_ENC_EC(ret);
  16635. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  16636. if (ret != BAD_FUNC_ARG)
  16637. return WC_TEST_RET_ENC_EC(ret);
  16638. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16639. if (ret != 0)
  16640. return WC_TEST_RET_ENC_EC(ret);
  16641. eSz = 0;
  16642. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16643. if (ret != RSA_BUFFER_E)
  16644. return WC_TEST_RET_ENC_EC(ret);
  16645. eSz = sizeof(e);
  16646. nSz = 0;
  16647. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16648. if (ret != RSA_BUFFER_E)
  16649. return WC_TEST_RET_ENC_EC(ret);
  16650. return 0;
  16651. }
  16652. #endif /* NO_ASN */
  16653. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  16654. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16655. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  16656. {
  16657. wc_test_ret_t ret;
  16658. byte e[3];
  16659. word32 eSz = sizeof(e);
  16660. byte n[RSA_TEST_BYTES];
  16661. word32 nSz = sizeof(n);
  16662. byte d[RSA_TEST_BYTES];
  16663. word32 dSz = sizeof(d);
  16664. byte p[RSA_TEST_BYTES/2];
  16665. word32 pSz = sizeof(p);
  16666. byte q[RSA_TEST_BYTES/2];
  16667. word32 qSz = sizeof(q);
  16668. word32 zero = 0;
  16669. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16670. if (ret != BAD_FUNC_ARG)
  16671. return WC_TEST_RET_ENC_EC(ret);
  16672. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16673. if (ret != BAD_FUNC_ARG)
  16674. return WC_TEST_RET_ENC_EC(ret);
  16675. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16676. if (ret != BAD_FUNC_ARG)
  16677. return WC_TEST_RET_ENC_EC(ret);
  16678. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16679. if (ret != BAD_FUNC_ARG)
  16680. return WC_TEST_RET_ENC_EC(ret);
  16681. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  16682. if (ret != BAD_FUNC_ARG)
  16683. return WC_TEST_RET_ENC_EC(ret);
  16684. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  16685. if (ret != BAD_FUNC_ARG)
  16686. return WC_TEST_RET_ENC_EC(ret);
  16687. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  16688. if (ret != BAD_FUNC_ARG)
  16689. return WC_TEST_RET_ENC_EC(ret);
  16690. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  16691. if (ret != BAD_FUNC_ARG)
  16692. return WC_TEST_RET_ENC_EC(ret);
  16693. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  16694. if (ret != BAD_FUNC_ARG)
  16695. return WC_TEST_RET_ENC_EC(ret);
  16696. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  16697. if (ret != BAD_FUNC_ARG)
  16698. return WC_TEST_RET_ENC_EC(ret);
  16699. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  16700. if (ret != BAD_FUNC_ARG)
  16701. return WC_TEST_RET_ENC_EC(ret);
  16702. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16703. if (ret != RSA_BUFFER_E)
  16704. return WC_TEST_RET_ENC_EC(ret);
  16705. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  16706. if (ret != RSA_BUFFER_E)
  16707. return WC_TEST_RET_ENC_EC(ret);
  16708. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16709. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  16710. if (ret != RSA_BUFFER_E)
  16711. return WC_TEST_RET_ENC_EC(ret);
  16712. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  16713. if (ret != RSA_BUFFER_E)
  16714. return WC_TEST_RET_ENC_EC(ret);
  16715. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  16716. if (ret != RSA_BUFFER_E)
  16717. return WC_TEST_RET_ENC_EC(ret);
  16718. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16719. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16720. if (ret != 0)
  16721. return WC_TEST_RET_ENC_EC(ret);
  16722. return 0;
  16723. }
  16724. #endif /* !HAVE_FIPS && !NO_ASN && !WOLFSSL_RSA_VERIFY_ONLY */
  16725. #ifndef NO_SIG_WRAPPER
  16726. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  16727. {
  16728. wc_test_ret_t ret;
  16729. word32 sigSz;
  16730. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  16731. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  16732. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16733. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16734. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16735. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16736. };
  16737. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  16738. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  16739. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  16740. 0x00, 0x04, 0x20,
  16741. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16742. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16743. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16744. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16745. };
  16746. word32 inLen = (word32)XSTRLEN((char*)in);
  16747. byte out[RSA_TEST_BYTES];
  16748. /* Parameter Validation testing. */
  16749. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  16750. if (ret != BAD_FUNC_ARG)
  16751. return WC_TEST_RET_ENC_EC(ret);
  16752. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  16753. if (ret != BAD_FUNC_ARG)
  16754. return WC_TEST_RET_ENC_EC(ret);
  16755. sigSz = (word32)modLen;
  16756. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16757. inLen, out, &sigSz, key, keyLen, rng);
  16758. if (ret != BAD_FUNC_ARG)
  16759. return WC_TEST_RET_ENC_EC(ret);
  16760. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16761. 0, out, &sigSz, key, keyLen, rng);
  16762. if (ret != BAD_FUNC_ARG)
  16763. return WC_TEST_RET_ENC_EC(ret);
  16764. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16765. inLen, NULL, &sigSz, key, keyLen, rng);
  16766. if (ret != BAD_FUNC_ARG)
  16767. return WC_TEST_RET_ENC_EC(ret);
  16768. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16769. inLen, out, NULL, key, keyLen, rng);
  16770. if (ret != BAD_FUNC_ARG)
  16771. return WC_TEST_RET_ENC_EC(ret);
  16772. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16773. inLen, out, &sigSz, NULL, keyLen, rng);
  16774. if (ret != BAD_FUNC_ARG)
  16775. return WC_TEST_RET_ENC_EC(ret);
  16776. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16777. inLen, out, &sigSz, key, 0, rng);
  16778. if (ret != BAD_FUNC_ARG)
  16779. return WC_TEST_RET_ENC_EC(ret);
  16780. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16781. inLen, out, &sigSz, key, keyLen, NULL);
  16782. #if defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  16783. /* blinding / rng handled with hardware acceleration */
  16784. if (ret != 0)
  16785. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  16786. /* async may not require RNG */
  16787. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16788. if (ret != NO_VALID_DEVID)
  16789. #else
  16790. if (ret != 0 && ret != MISSING_RNG_E)
  16791. #endif
  16792. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  16793. /* FIPS140 implementation does not do blinding */
  16794. if (ret != 0)
  16795. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  16796. if (ret != SIG_TYPE_E)
  16797. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  16798. /* RNG is handled by hardware */
  16799. if (ret != 0)
  16800. #else
  16801. if (ret != MISSING_RNG_E)
  16802. #endif
  16803. return WC_TEST_RET_ENC_EC(ret);
  16804. sigSz = 0;
  16805. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16806. inLen, out, &sigSz, key, keyLen, rng);
  16807. if (ret != BAD_FUNC_ARG)
  16808. return WC_TEST_RET_ENC_EC(ret);
  16809. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16810. inLen, out, (word32)modLen, key, keyLen);
  16811. if (ret != BAD_FUNC_ARG)
  16812. return WC_TEST_RET_ENC_EC(ret);
  16813. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16814. 0, out, (word32)modLen, key, keyLen);
  16815. if (ret != BAD_FUNC_ARG)
  16816. return WC_TEST_RET_ENC_EC(ret);
  16817. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16818. inLen, NULL, (word32)modLen, key, keyLen);
  16819. if (ret != BAD_FUNC_ARG)
  16820. return WC_TEST_RET_ENC_EC(ret);
  16821. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16822. inLen, out, 0, key, keyLen);
  16823. if (ret != BAD_FUNC_ARG)
  16824. return WC_TEST_RET_ENC_EC(ret);
  16825. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16826. inLen, out, (word32)modLen, NULL, keyLen);
  16827. if (ret != BAD_FUNC_ARG)
  16828. return WC_TEST_RET_ENC_EC(ret);
  16829. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16830. inLen, out, (word32)modLen, key, 0);
  16831. if (ret != BAD_FUNC_ARG)
  16832. return WC_TEST_RET_ENC_EC(ret);
  16833. #ifndef HAVE_ECC
  16834. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  16835. if (ret != SIG_TYPE_E)
  16836. return WC_TEST_RET_ENC_EC(ret);
  16837. #endif
  16838. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16839. return 0;
  16840. #endif
  16841. /* Use APIs. */
  16842. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  16843. if (ret != modLen)
  16844. return WC_TEST_RET_ENC_EC(ret);
  16845. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  16846. if (ret != modLen)
  16847. return WC_TEST_RET_ENC_EC(ret);
  16848. sigSz = (word32)ret;
  16849. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16850. XMEMSET(out, 0, sizeof(out));
  16851. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16852. inLen, out, &sigSz, key, keyLen, rng);
  16853. if (ret != 0)
  16854. return WC_TEST_RET_ENC_EC(ret);
  16855. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16856. inLen, out, (word32)modLen, key, keyLen);
  16857. if (ret != 0)
  16858. return WC_TEST_RET_ENC_EC(ret);
  16859. sigSz = (word32)sizeof(out);
  16860. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16861. in, inLen, out, &sigSz, key, keyLen, rng);
  16862. if (ret != 0)
  16863. return WC_TEST_RET_ENC_EC(ret);
  16864. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16865. in, inLen, out, (word32)modLen, key, keyLen);
  16866. if (ret != 0)
  16867. return WC_TEST_RET_ENC_EC(ret);
  16868. /* Wrong signature type. */
  16869. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16870. inLen, out, (word32)modLen, key, keyLen);
  16871. if (ret == 0)
  16872. return WC_TEST_RET_ENC_EC(ret);
  16873. /* check hash functions */
  16874. sigSz = (word32)sizeof(out);
  16875. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16876. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  16877. if (ret != 0)
  16878. return WC_TEST_RET_ENC_EC(ret);
  16879. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16880. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  16881. if (ret != 0)
  16882. return WC_TEST_RET_ENC_EC(ret);
  16883. sigSz = (word32)sizeof(out);
  16884. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16885. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  16886. if (ret != 0)
  16887. return WC_TEST_RET_ENC_EC(ret);
  16888. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16889. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  16890. if (ret != 0)
  16891. return WC_TEST_RET_ENC_EC(ret);
  16892. #else
  16893. (void)hash;
  16894. (void)hashEnc;
  16895. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  16896. return 0;
  16897. }
  16898. #endif /* !NO_SIG_WRAPPER */
  16899. #ifdef WC_RSA_NONBLOCK
  16900. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  16901. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  16902. {
  16903. wc_test_ret_t ret = 0;
  16904. int count;
  16905. int signSz = 0;
  16906. RsaNb nb;
  16907. byte* inlinePlain = NULL;
  16908. /* Enable non-blocking RSA mode - provide context */
  16909. ret = wc_RsaSetNonBlock(key, &nb);
  16910. if (ret != 0)
  16911. return ret;
  16912. #ifdef WC_RSA_NONBLOCK_TIME
  16913. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  16914. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  16915. if (ret != 0)
  16916. return ret;
  16917. #endif
  16918. count = 0;
  16919. do {
  16920. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  16921. count++; /* track number of would blocks */
  16922. if (ret == FP_WOULDBLOCK) {
  16923. /* do "other" work here */
  16924. }
  16925. } while (ret == FP_WOULDBLOCK);
  16926. if (ret < 0) {
  16927. return ret;
  16928. }
  16929. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16930. printf("RSA non-block sign: %d times\n", count);
  16931. #endif
  16932. signSz = ret;
  16933. /* Test non-blocking verify */
  16934. XMEMSET(plain, 0, plainSz);
  16935. count = 0;
  16936. do {
  16937. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  16938. count++; /* track number of would blocks */
  16939. if (ret == FP_WOULDBLOCK) {
  16940. /* do "other" work here */
  16941. }
  16942. } while (ret == FP_WOULDBLOCK);
  16943. if (ret < 0) {
  16944. return ret;
  16945. }
  16946. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16947. printf("RSA non-block verify: %d times\n", count);
  16948. #endif
  16949. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  16950. return SIG_VERIFY_E;
  16951. }
  16952. /* Test inline non-blocking verify */
  16953. count = 0;
  16954. do {
  16955. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  16956. count++; /* track number of would blocks */
  16957. if (ret == FP_WOULDBLOCK) {
  16958. /* do "other" work here */
  16959. }
  16960. } while (ret == FP_WOULDBLOCK);
  16961. if (ret < 0) {
  16962. return ret;
  16963. }
  16964. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16965. printf("RSA non-block inline verify: %d times\n", count);
  16966. #endif
  16967. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  16968. return SIG_VERIFY_E;
  16969. }
  16970. /* Disabling non-block RSA mode */
  16971. ret = wc_RsaSetNonBlock(key, NULL);
  16972. (void)count;
  16973. return 0;
  16974. }
  16975. #endif
  16976. #if !defined(NO_ASN)
  16977. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  16978. {
  16979. wc_test_ret_t ret;
  16980. word32 inSz;
  16981. word32 inOutIdx;
  16982. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  16983. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  16984. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  16985. 0x03 };
  16986. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  16987. 0x30, 0x18, 0x30, 0x16,
  16988. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  16989. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  16990. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  16991. 0x30, 0x1a, 0x30, 0x18,
  16992. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  16993. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  16994. 0x02, 0x1, 0x03 };
  16995. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  16996. 0x30, 0x1b, 0x30, 0x19,
  16997. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  16998. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  16999. 0x02, 0x1, 0x03 };
  17000. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  17001. 0x30, 0x18, 0x30, 0x16,
  17002. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17003. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17004. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  17005. 0x30, 0x18, 0x30, 0x16,
  17006. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17007. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17008. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  17009. 0x30, 0x16, 0x30, 0x14,
  17010. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17011. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17012. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  17013. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  17014. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17015. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  17016. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  17017. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  17018. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  17019. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  17020. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17021. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  17022. 0x30, 0x17, 0x30, 0x15,
  17023. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17024. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17025. ret = wc_InitRsaKey(keyPub, NULL);
  17026. if (ret != 0)
  17027. return WC_TEST_RET_ENC_EC(ret);
  17028. /* Parameter Validation testing. */
  17029. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  17030. if (ret != BAD_FUNC_ARG) {
  17031. ret = WC_TEST_RET_ENC_EC(ret);
  17032. goto done;
  17033. }
  17034. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  17035. if (ret != BAD_FUNC_ARG) {
  17036. ret = WC_TEST_RET_ENC_EC(ret);
  17037. goto done;
  17038. }
  17039. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  17040. if (ret != BAD_FUNC_ARG) {
  17041. ret = WC_TEST_RET_ENC_EC(ret);
  17042. goto done;
  17043. }
  17044. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  17045. #if defined(USE_INTEGER_HEAP_MATH)
  17046. if (ret != 0)
  17047. #else
  17048. if (ret != ASN_GETINT_E)
  17049. #endif
  17050. {
  17051. ret = WC_TEST_RET_ENC_EC(ret);
  17052. goto done;
  17053. }
  17054. wc_FreeRsaKey(keyPub);
  17055. ret = wc_InitRsaKey(keyPub, NULL);
  17056. if (ret != 0)
  17057. return WC_TEST_RET_ENC_EC(ret);
  17058. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  17059. #if defined(USE_INTEGER_HEAP_MATH)
  17060. if (ret != 0)
  17061. #else
  17062. if (ret != ASN_GETINT_E)
  17063. #endif
  17064. {
  17065. ret = WC_TEST_RET_ENC_EC(ret);
  17066. goto done;
  17067. }
  17068. wc_FreeRsaKey(keyPub);
  17069. ret = wc_InitRsaKey(keyPub, NULL);
  17070. if (ret != 0)
  17071. return WC_TEST_RET_ENC_EC(ret);
  17072. /* Use API. */
  17073. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  17074. if (ret != 0) {
  17075. ret = WC_TEST_RET_ENC_EC(ret);
  17076. goto done;
  17077. }
  17078. wc_FreeRsaKey(keyPub);
  17079. ret = wc_InitRsaKey(keyPub, NULL);
  17080. if (ret != 0)
  17081. return WC_TEST_RET_ENC_EC(ret);
  17082. /* Parameter Validation testing. */
  17083. inSz = sizeof(good);
  17084. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  17085. if (ret != BAD_FUNC_ARG) {
  17086. ret = WC_TEST_RET_ENC_EC(ret);
  17087. goto done;
  17088. }
  17089. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  17090. if (ret != BAD_FUNC_ARG) {
  17091. ret = WC_TEST_RET_ENC_EC(ret);
  17092. goto done;
  17093. }
  17094. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  17095. if (ret != BAD_FUNC_ARG) {
  17096. ret = WC_TEST_RET_ENC_EC(ret);
  17097. goto done;
  17098. }
  17099. /* Use good data and offset to bad data. */
  17100. inOutIdx = 2;
  17101. inSz = sizeof(good) - inOutIdx;
  17102. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17103. if (ret != ASN_PARSE_E) {
  17104. ret = WC_TEST_RET_ENC_EC(ret);
  17105. goto done;
  17106. }
  17107. inOutIdx = 2;
  17108. inSz = sizeof(goodAlgId) - inOutIdx;
  17109. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17110. if (ret != ASN_PARSE_E) {
  17111. ret = WC_TEST_RET_ENC_EC(ret);
  17112. goto done;
  17113. }
  17114. inOutIdx = 2;
  17115. inSz = sizeof(goodAlgId);
  17116. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17117. #ifndef WOLFSSL_NO_DECODE_EXTRA
  17118. if (ret != ASN_PARSE_E)
  17119. #else
  17120. if (ret != ASN_RSA_KEY_E)
  17121. #endif
  17122. {
  17123. ret = WC_TEST_RET_ENC_EC(ret);
  17124. goto done;
  17125. }
  17126. /* Try different bad data. */
  17127. inSz = sizeof(badAlgIdNull);
  17128. inOutIdx = 0;
  17129. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  17130. if (ret != ASN_EXPECT_0_E) {
  17131. ret = WC_TEST_RET_ENC_EC(ret);
  17132. goto done;
  17133. }
  17134. inSz = sizeof(badNotBitString);
  17135. inOutIdx = 0;
  17136. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  17137. if (ret != ASN_BITSTR_E) {
  17138. ret = WC_TEST_RET_ENC_EC(ret);
  17139. goto done;
  17140. }
  17141. inSz = sizeof(badBitStringLen);
  17142. inOutIdx = 0;
  17143. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  17144. if (ret != ASN_PARSE_E) {
  17145. ret = WC_TEST_RET_ENC_EC(ret);
  17146. goto done;
  17147. }
  17148. inSz = sizeof(badNoSeq);
  17149. inOutIdx = 0;
  17150. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  17151. if (ret != ASN_PARSE_E) {
  17152. ret = WC_TEST_RET_ENC_EC(ret);
  17153. goto done;
  17154. }
  17155. inSz = sizeof(badNoObj);
  17156. inOutIdx = 0;
  17157. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  17158. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  17159. ret = WC_TEST_RET_ENC_EC(ret);
  17160. goto done;
  17161. }
  17162. inSz = sizeof(badIntN);
  17163. inOutIdx = 0;
  17164. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  17165. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17166. ret = WC_TEST_RET_ENC_EC(ret);
  17167. goto done;
  17168. }
  17169. inSz = sizeof(badNotIntE);
  17170. inOutIdx = 0;
  17171. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  17172. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17173. ret = WC_TEST_RET_ENC_EC(ret);
  17174. goto done;
  17175. }
  17176. /* TODO: Shouldn't pass as the sequence length is too small. */
  17177. inSz = sizeof(badLength);
  17178. inOutIdx = 0;
  17179. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  17180. #ifndef WOLFSSL_ASN_TEMPLATE
  17181. if (ret != 0)
  17182. #else
  17183. if (ret != ASN_PARSE_E)
  17184. #endif
  17185. {
  17186. ret = WC_TEST_RET_ENC_EC(ret);
  17187. goto done;
  17188. }
  17189. /* TODO: Shouldn't ignore object id's data. */
  17190. wc_FreeRsaKey(keyPub);
  17191. ret = wc_InitRsaKey(keyPub, NULL);
  17192. if (ret != 0)
  17193. return WC_TEST_RET_ENC_EC(ret);
  17194. inSz = sizeof(badBitStrNoZero);
  17195. inOutIdx = 0;
  17196. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  17197. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  17198. ret = WC_TEST_RET_ENC_EC(ret);
  17199. goto done;
  17200. }
  17201. wc_FreeRsaKey(keyPub);
  17202. ret = wc_InitRsaKey(keyPub, NULL);
  17203. if (ret != 0)
  17204. return WC_TEST_RET_ENC_EC(ret);
  17205. /* Valid data cases. */
  17206. inSz = sizeof(good);
  17207. inOutIdx = 0;
  17208. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17209. if (ret != 0) {
  17210. ret = WC_TEST_RET_ENC_EC(ret);
  17211. goto done;
  17212. }
  17213. if (inOutIdx != inSz) {
  17214. ret = WC_TEST_RET_ENC_NC;
  17215. goto done;
  17216. }
  17217. wc_FreeRsaKey(keyPub);
  17218. ret = wc_InitRsaKey(keyPub, NULL);
  17219. if (ret != 0)
  17220. return WC_TEST_RET_ENC_EC(ret);
  17221. inSz = sizeof(goodAlgId);
  17222. inOutIdx = 0;
  17223. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17224. if (ret != 0) {
  17225. ret = WC_TEST_RET_ENC_EC(ret);
  17226. goto done;
  17227. }
  17228. if (inOutIdx != inSz) {
  17229. ret = WC_TEST_RET_ENC_NC;
  17230. goto done;
  17231. }
  17232. wc_FreeRsaKey(keyPub);
  17233. ret = wc_InitRsaKey(keyPub, NULL);
  17234. if (ret != 0)
  17235. return WC_TEST_RET_ENC_EC(ret);
  17236. inSz = sizeof(goodAlgIdNull);
  17237. inOutIdx = 0;
  17238. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  17239. if (ret != 0) {
  17240. ret = WC_TEST_RET_ENC_EC(ret);
  17241. goto done;
  17242. }
  17243. if (inOutIdx != inSz) {
  17244. ret = WC_TEST_RET_ENC_NC;
  17245. goto done;
  17246. }
  17247. done:
  17248. wc_FreeRsaKey(keyPub);
  17249. return ret;
  17250. }
  17251. #endif
  17252. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17253. /* Need to create known good signatures to test with this. */
  17254. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17255. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17256. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  17257. {
  17258. byte digest[WC_MAX_DIGEST_SIZE];
  17259. wc_test_ret_t ret = 0;
  17260. const char inStr[] = TEST_STRING;
  17261. word32 inLen = (word32)TEST_STRING_SZ;
  17262. word32 outSz;
  17263. word32 plainSz;
  17264. word32 digestSz;
  17265. int i, j;
  17266. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17267. int k, l;
  17268. #endif
  17269. #ifndef WOLFSSL_SE050
  17270. int len;
  17271. #endif
  17272. byte* plain;
  17273. int mgf[] = {
  17274. #ifndef NO_SHA
  17275. WC_MGF1SHA1,
  17276. #endif
  17277. #ifdef WOLFSSL_SHA224
  17278. WC_MGF1SHA224,
  17279. #endif
  17280. WC_MGF1SHA256,
  17281. #ifdef WOLFSSL_SHA384
  17282. WC_MGF1SHA384,
  17283. #endif
  17284. #ifdef WOLFSSL_SHA512
  17285. WC_MGF1SHA512
  17286. #endif
  17287. };
  17288. enum wc_HashType hash[] = {
  17289. #ifndef NO_SHA
  17290. WC_HASH_TYPE_SHA,
  17291. #endif
  17292. #ifdef WOLFSSL_SHA224
  17293. WC_HASH_TYPE_SHA224,
  17294. #endif
  17295. WC_HASH_TYPE_SHA256,
  17296. #ifdef WOLFSSL_SHA384
  17297. WC_HASH_TYPE_SHA384,
  17298. #endif
  17299. #ifdef WOLFSSL_SHA512
  17300. WC_HASH_TYPE_SHA512,
  17301. #endif
  17302. };
  17303. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17304. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17305. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17306. WC_ALLOC_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17307. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17308. WC_ALLOC_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17309. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17310. if (in == NULL || out == NULL || sig == NULL)
  17311. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  17312. #endif
  17313. XMEMCPY(in, inStr, inLen);
  17314. /* Test all combinations of hash and MGF. */
  17315. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  17316. /* Calculate hash of message. */
  17317. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  17318. if (ret != 0)
  17319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17320. digestSz = (word32)wc_HashGetDigestSize(hash[j]);
  17321. #ifdef WOLFSSL_SE050
  17322. /* SE050 only supports MGF matched to same hash type */
  17323. i = j;
  17324. #else
  17325. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  17326. #endif
  17327. outSz = RSA_TEST_BYTES;
  17328. do {
  17329. #if defined(WOLFSSL_ASYNC_CRYPT)
  17330. ret = wc_AsyncWait(ret, &key->asyncDev,
  17331. WC_ASYNC_FLAG_CALL_AGAIN);
  17332. #endif
  17333. if (ret >= 0) {
  17334. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  17335. hash[j], mgf[i], -1, key, rng);
  17336. }
  17337. } while (ret == WC_PENDING_E);
  17338. if (ret <= 0)
  17339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17340. outSz = (word32)ret;
  17341. XMEMCPY(sig, out, outSz);
  17342. plain = NULL;
  17343. TEST_SLEEP();
  17344. do {
  17345. #if defined(WOLFSSL_ASYNC_CRYPT)
  17346. ret = wc_AsyncWait(ret, &key->asyncDev,
  17347. WC_ASYNC_FLAG_CALL_AGAIN);
  17348. #endif
  17349. if (ret >= 0) {
  17350. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  17351. mgf[i], -1, key);
  17352. }
  17353. } while (ret == WC_PENDING_E);
  17354. if (ret <= 0)
  17355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17356. plainSz = (word32)ret;
  17357. TEST_SLEEP();
  17358. #if defined(HAVE_SELFTEST) && \
  17359. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17360. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17361. hash[j], -1);
  17362. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17363. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17364. hash[j], -1, 0);
  17365. #else
  17366. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  17367. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  17368. #endif
  17369. if (ret != 0)
  17370. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17371. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17372. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  17373. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  17374. if (i == k && j == l)
  17375. continue;
  17376. XMEMCPY(sig, out, outSz);
  17377. do {
  17378. #if defined(WOLFSSL_ASYNC_CRYPT)
  17379. ret = wc_AsyncWait(ret, &key->asyncDev,
  17380. WC_ASYNC_FLAG_CALL_AGAIN);
  17381. #endif
  17382. if (ret >= 0) {
  17383. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  17384. (byte**)&plain, hash[l], mgf[k], -1, key);
  17385. }
  17386. } while (ret == WC_PENDING_E);
  17387. if (ret >= 0)
  17388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17389. }
  17390. }
  17391. #endif
  17392. #ifndef WOLFSSL_SE050
  17393. } /* end mgf for loop */
  17394. #endif
  17395. }
  17396. /* SE050 generates salts internally only of hash length */
  17397. #ifndef WOLFSSL_SE050
  17398. /* Test that a salt length of zero works. */
  17399. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17400. outSz = RSA_TEST_BYTES;
  17401. do {
  17402. #if defined(WOLFSSL_ASYNC_CRYPT)
  17403. ret = wc_AsyncWait(ret, &key->asyncDev,
  17404. WC_ASYNC_FLAG_CALL_AGAIN);
  17405. #endif
  17406. if (ret >= 0) {
  17407. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17408. mgf[0], 0, key, rng);
  17409. }
  17410. } while (ret == WC_PENDING_E);
  17411. if (ret <= 0)
  17412. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17413. outSz = (word32)ret;
  17414. TEST_SLEEP();
  17415. do {
  17416. #if defined(WOLFSSL_ASYNC_CRYPT)
  17417. ret = wc_AsyncWait(ret, &key->asyncDev,
  17418. WC_ASYNC_FLAG_CALL_AGAIN);
  17419. #endif
  17420. if (ret >= 0) {
  17421. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  17422. 0, key);
  17423. }
  17424. } while (ret == WC_PENDING_E);
  17425. if (ret <= 0)
  17426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17427. plainSz = (word32)ret;
  17428. TEST_SLEEP();
  17429. do {
  17430. #if defined(WOLFSSL_ASYNC_CRYPT)
  17431. ret = wc_AsyncWait(ret, &key->asyncDev,
  17432. WC_ASYNC_FLAG_CALL_AGAIN);
  17433. #endif
  17434. if (ret >= 0) {
  17435. #if defined(HAVE_SELFTEST) && \
  17436. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17437. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17438. hash[0], 0);
  17439. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17440. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17441. hash[0], 0, 0);
  17442. #else
  17443. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  17444. hash[0], 0, 0, HEAP_HINT);
  17445. #endif
  17446. }
  17447. } while (ret == WC_PENDING_E);
  17448. if (ret != 0)
  17449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17450. XMEMCPY(sig, out, outSz);
  17451. plain = NULL;
  17452. do {
  17453. #if defined(WOLFSSL_ASYNC_CRYPT)
  17454. ret = wc_AsyncWait(ret, &key->asyncDev,
  17455. WC_ASYNC_FLAG_CALL_AGAIN);
  17456. #endif
  17457. if (ret >= 0) {
  17458. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17459. 0, key);
  17460. }
  17461. } while (ret == WC_PENDING_E);
  17462. if (ret <= 0)
  17463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17464. plainSz = (word32)ret;
  17465. TEST_SLEEP();
  17466. #if defined(HAVE_SELFTEST) && \
  17467. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17468. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17469. hash[0], 0);
  17470. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17471. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17472. hash[0], 0, 0);
  17473. #else
  17474. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17475. 0, 0, HEAP_HINT);
  17476. #endif
  17477. if (ret != 0)
  17478. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17479. /* Test bad salt lengths in various APIs. */
  17480. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17481. outSz = RSA_TEST_BYTES;
  17482. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17483. len = -2;
  17484. #else
  17485. len = -3;
  17486. #endif
  17487. do {
  17488. #if defined(WOLFSSL_ASYNC_CRYPT)
  17489. ret = wc_AsyncWait(ret, &key->asyncDev,
  17490. WC_ASYNC_FLAG_CALL_AGAIN);
  17491. #endif
  17492. if (ret >= 0) {
  17493. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17494. mgf[0], len, key, rng);
  17495. }
  17496. } while (ret == WC_PENDING_E);
  17497. if (ret != PSS_SALTLEN_E)
  17498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17499. do {
  17500. #if defined(WOLFSSL_ASYNC_CRYPT)
  17501. ret = wc_AsyncWait(ret, &key->asyncDev,
  17502. WC_ASYNC_FLAG_CALL_AGAIN);
  17503. #endif
  17504. if (ret >= 0) {
  17505. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17506. mgf[0], digestSz + 1, key, rng);
  17507. }
  17508. } while (ret == WC_PENDING_E);
  17509. if (ret != PSS_SALTLEN_E)
  17510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17511. TEST_SLEEP();
  17512. do {
  17513. #if defined(WOLFSSL_ASYNC_CRYPT)
  17514. ret = wc_AsyncWait(ret, &key->asyncDev,
  17515. WC_ASYNC_FLAG_CALL_AGAIN);
  17516. #endif
  17517. if (ret >= 0) {
  17518. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  17519. mgf[0], -2, key);
  17520. }
  17521. } while (ret == WC_PENDING_E);
  17522. if (ret != PSS_SALTLEN_E)
  17523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17524. TEST_SLEEP();
  17525. do {
  17526. #if defined(WOLFSSL_ASYNC_CRYPT)
  17527. ret = wc_AsyncWait(ret, &key->asyncDev,
  17528. WC_ASYNC_FLAG_CALL_AGAIN);
  17529. #endif
  17530. if (ret >= 0) {
  17531. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17532. digestSz + 1, key);
  17533. }
  17534. } while (ret == WC_PENDING_E);
  17535. if (ret != PSS_SALTLEN_E)
  17536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17537. TEST_SLEEP();
  17538. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17539. len = -2;
  17540. #else
  17541. len = -3;
  17542. #endif
  17543. #if defined(HAVE_SELFTEST) && \
  17544. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17545. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17546. hash[0], len);
  17547. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17548. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17549. hash[0], len, 0);
  17550. #else
  17551. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17552. len, 0, HEAP_HINT);
  17553. #endif
  17554. if (ret != PSS_SALTLEN_E)
  17555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17556. #ifndef WOLFSSL_PSS_LONG_SALT
  17557. len = (int)(digestSz + 1);
  17558. #else
  17559. len = (int)(plainSz - digestSz - 1);
  17560. #endif
  17561. #if defined(HAVE_SELFTEST) && \
  17562. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17563. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17564. hash[0], len);
  17565. if (ret != PSS_SALTLEN_E)
  17566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17567. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17568. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17569. hash[0], len, 0);
  17570. if (ret != BAD_PADDING_E)
  17571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17572. #else
  17573. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17574. len, 0, HEAP_HINT);
  17575. if (ret != PSS_SALTLEN_E)
  17576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17577. #endif
  17578. ret = 0;
  17579. #endif /* WOLFSSL_SE050 */
  17580. exit_rsa_pss:
  17581. WC_FREE_VAR(sig, HEAP_HINT);
  17582. WC_FREE_VAR(in, HEAP_HINT);
  17583. WC_FREE_VAR(out, HEAP_HINT);
  17584. return ret;
  17585. }
  17586. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  17587. #endif
  17588. #ifdef WC_RSA_NO_PADDING
  17589. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  17590. {
  17591. WC_RNG rng;
  17592. byte* tmp = NULL;
  17593. size_t bytes;
  17594. wc_test_ret_t ret;
  17595. word32 inLen = 0;
  17596. word32 idx = 0;
  17597. word32 outSz = RSA_TEST_BYTES;
  17598. word32 plainSz = RSA_TEST_BYTES;
  17599. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17600. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  17601. !defined(NO_FILESYSTEM)
  17602. XFILE file;
  17603. #endif
  17604. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  17605. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17606. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17607. WC_ALLOC_VAR(key, RsaKey, 1, HEAP_HINT);
  17608. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17609. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17610. WOLFSSL_ENTER("rsa_no_pad_test");
  17611. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17612. if (key == NULL || out == NULL || plain == NULL)
  17613. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  17614. #endif
  17615. /* initialize stack structures */
  17616. XMEMSET(&rng, 0, sizeof(rng));
  17617. XMEMSET(key, 0, sizeof(RsaKey));
  17618. #ifdef USE_CERT_BUFFERS_1024
  17619. bytes = (size_t)sizeof_client_key_der_1024;
  17620. if (bytes < (size_t)sizeof_client_cert_der_1024)
  17621. bytes = (size_t)sizeof_client_cert_der_1024;
  17622. #elif defined(USE_CERT_BUFFERS_2048)
  17623. bytes = (size_t)sizeof_client_key_der_2048;
  17624. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17625. bytes = (size_t)sizeof_client_cert_der_2048;
  17626. #else
  17627. bytes = FOURK_BUF;
  17628. #endif
  17629. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17630. if (tmp == NULL
  17631. #ifdef WOLFSSL_ASYNC_CRYPT
  17632. || out == NULL || plain == NULL
  17633. #endif
  17634. ) {
  17635. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17636. }
  17637. #ifdef USE_CERT_BUFFERS_1024
  17638. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  17639. #elif defined(USE_CERT_BUFFERS_2048)
  17640. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17641. #elif defined(USE_CERT_BUFFERS_3072)
  17642. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17643. #elif defined(USE_CERT_BUFFERS_4096)
  17644. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17645. #elif !defined(NO_FILESYSTEM)
  17646. file = XFOPEN(clientKey, "rb");
  17647. if (!file) {
  17648. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  17649. WC_TEST_RET_ENC_ERRNO);
  17650. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17651. }
  17652. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17653. XFCLOSE(file);
  17654. if (bytes == 0)
  17655. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17656. #else
  17657. /* No key to use. */
  17658. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17659. #endif /* USE_CERT_BUFFERS */
  17660. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  17661. if (ret != 0) {
  17662. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17663. }
  17664. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17665. if (ret != 0) {
  17666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17667. }
  17668. /* after loading in key use tmp as the test buffer */
  17669. #ifndef HAVE_FIPS
  17670. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17671. #else
  17672. ret = wc_InitRng(&rng);
  17673. #endif
  17674. if (ret != 0) {
  17675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17676. }
  17677. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17678. inLen = (word32)wc_RsaEncryptSize(key);
  17679. outSz = inLen;
  17680. plainSz = inLen;
  17681. XMEMSET(tmp, 7, inLen);
  17682. do {
  17683. #if defined(WOLFSSL_ASYNC_CRYPT)
  17684. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17685. #endif
  17686. if (ret >= 0) {
  17687. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  17688. RSA_PRIVATE_ENCRYPT, &rng);
  17689. }
  17690. } while (ret == WC_PENDING_E);
  17691. if (ret <= 0) {
  17692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17693. }
  17694. /* encrypted result should not be the same as input */
  17695. if (XMEMCMP(out, tmp, inLen) == 0) {
  17696. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17697. }
  17698. TEST_SLEEP();
  17699. /* decrypt with public key and compare result */
  17700. do {
  17701. #if defined(WOLFSSL_ASYNC_CRYPT)
  17702. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17703. #endif
  17704. if (ret >= 0) {
  17705. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  17706. RSA_PUBLIC_DECRYPT, &rng);
  17707. }
  17708. } while (ret == WC_PENDING_E);
  17709. if (ret <= 0) {
  17710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17711. }
  17712. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17713. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17714. }
  17715. TEST_SLEEP();
  17716. #endif
  17717. #ifdef WC_RSA_BLINDING
  17718. ret = wc_RsaSetRNG(NULL, &rng);
  17719. if (ret != BAD_FUNC_ARG) {
  17720. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17721. }
  17722. ret = wc_RsaSetRNG(key, &rng);
  17723. if (ret < 0) {
  17724. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17725. }
  17726. #endif
  17727. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17728. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17729. do {
  17730. #if defined(WOLFSSL_ASYNC_CRYPT)
  17731. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17732. #endif
  17733. if (ret >= 0) {
  17734. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, outSz, key, &rng,
  17735. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17736. }
  17737. } while (ret == WC_PENDING_E);
  17738. if (ret < 0) {
  17739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17740. }
  17741. TEST_SLEEP();
  17742. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17743. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17744. do {
  17745. #if defined(WOLFSSL_ASYNC_CRYPT)
  17746. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17747. #endif
  17748. if (ret >= 0) {
  17749. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, plainSz, key,
  17750. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17751. }
  17752. } while (ret == WC_PENDING_E);
  17753. if (ret < 0) {
  17754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17755. }
  17756. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17757. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17758. }
  17759. TEST_SLEEP();
  17760. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17761. /* test some bad arguments */
  17762. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  17763. &rng);
  17764. if (ret != BAD_FUNC_ARG) {
  17765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17766. }
  17767. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  17768. &rng);
  17769. if (ret != BAD_FUNC_ARG) {
  17770. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17771. }
  17772. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  17773. &rng);
  17774. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  17775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17776. }
  17777. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  17778. RSA_PUBLIC_DECRYPT, &rng);
  17779. if (ret != BAD_FUNC_ARG) {
  17780. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17781. }
  17782. /* if making it to this point of code without hitting an ERROR_OUT then
  17783. * all tests have passed */
  17784. ret = 0;
  17785. exit_rsa_nopadding:
  17786. wc_FreeRsaKey(key);
  17787. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17788. WC_FREE_VAR(key, HEAP_HINT);
  17789. WC_FREE_VAR(out, HEAP_HINT);
  17790. WC_FREE_VAR(plain, HEAP_HINT);
  17791. wc_FreeRng(&rng);
  17792. return ret;
  17793. }
  17794. #endif /* WC_RSA_NO_PADDING */
  17795. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17796. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  17797. {
  17798. byte* tmp = NULL;
  17799. size_t bytes;
  17800. wc_test_ret_t ret;
  17801. word32 inLen = 0;
  17802. #ifndef NO_ASN
  17803. word32 idx = 0;
  17804. #endif
  17805. word32 outSz = RSA_TEST_BYTES;
  17806. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17807. word32 plainSz = RSA_TEST_BYTES;
  17808. #endif
  17809. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  17810. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  17811. XFILE file;
  17812. #endif
  17813. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17814. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17815. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17816. #endif
  17817. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17818. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17819. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17820. #endif
  17821. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17822. if (out == NULL
  17823. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17824. || plain == NULL
  17825. #endif
  17826. ) {
  17827. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  17828. }
  17829. #endif
  17830. #if defined(USE_CERT_BUFFERS_2048)
  17831. bytes = (size_t)sizeof_client_key_der_2048;
  17832. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17833. bytes = (size_t)sizeof_client_cert_der_2048;
  17834. #else
  17835. bytes = FOURK_BUF;
  17836. #endif
  17837. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17838. if (tmp == NULL
  17839. #ifdef WOLFSSL_ASYNC_CRYPT
  17840. || out == NULL || plain == NULL
  17841. #endif
  17842. ) {
  17843. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17844. }
  17845. #if defined(USE_CERT_BUFFERS_2048)
  17846. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17847. #elif defined(USE_CERT_BUFFERS_3072)
  17848. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17849. #elif defined(USE_CERT_BUFFERS_4096)
  17850. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17851. #elif !defined(NO_FILESYSTEM)
  17852. file = XFOPEN(clientKey, "rb");
  17853. if (!file) {
  17854. err_sys("can't open ./certs/client-key.der, "
  17855. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17856. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17857. }
  17858. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17859. XFCLOSE(file);
  17860. if (bytes == 0)
  17861. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17862. #else
  17863. /* No key to use. */
  17864. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17865. #endif /* USE_CERT_BUFFERS */
  17866. #ifndef NO_ASN
  17867. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17868. if (ret != 0) {
  17869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17870. }
  17871. #else
  17872. #ifdef USE_CERT_BUFFERS_2048
  17873. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  17874. if (ret != 0) {
  17875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17876. }
  17877. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  17878. if (ret != 0) {
  17879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17880. }
  17881. #ifndef NO_SIG_WRAPPER
  17882. modLen = 2048;
  17883. #endif
  17884. #else
  17885. #error Not supported yet!
  17886. #endif
  17887. #endif
  17888. key->n.dp[0] &= (mp_digit)-2;
  17889. if (ret != 0) {
  17890. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17891. }
  17892. /* after loading in key use tmp as the test buffer */
  17893. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17894. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  17895. !defined(WOLFSSL_XILINX_CRYPT)
  17896. /* The ARM64_ASM code that was FIPS validated did not return these expected
  17897. * failure codes. These tests cases were added after the assembly was
  17898. * in-lined in the module and validated, these tests will be available in
  17899. * the 140-3 module */
  17900. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17901. inLen = 32;
  17902. outSz = wc_RsaEncryptSize(key);
  17903. XMEMSET(tmp, 7, plainSz);
  17904. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  17905. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17907. }
  17908. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  17909. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17911. }
  17912. #endif
  17913. #ifdef WC_RSA_BLINDING
  17914. ret = wc_RsaSetRNG(key, rng);
  17915. if (ret < 0) {
  17916. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17917. }
  17918. #endif
  17919. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17920. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17921. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  17922. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17924. }
  17925. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17926. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17927. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  17928. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17930. }
  17931. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17932. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  17933. /* if making it to this point of code without hitting an ERROR_OUT then
  17934. * all tests have passed */
  17935. ret = 0;
  17936. exit_rsa_even_mod:
  17937. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17938. WC_FREE_VAR(out, HEAP_HINT);
  17939. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17940. WC_FREE_VAR(plain, HEAP_HINT);
  17941. #endif
  17942. (void)out;
  17943. (void)outSz;
  17944. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17945. (void)plain;
  17946. (void)plainSz;
  17947. #endif
  17948. (void)inLen;
  17949. (void)rng;
  17950. return ret;
  17951. }
  17952. #endif /* WOLFSSL_HAVE_SP_RSA */
  17953. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  17954. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  17955. {
  17956. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17957. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17958. #ifdef WOLFSSL_TEST_CERT
  17959. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17960. #endif
  17961. #else
  17962. RsaKey caKey[1];
  17963. #ifdef WOLFSSL_TEST_CERT
  17964. DecodedCert decode[1];
  17965. #endif
  17966. #endif
  17967. byte* der = NULL;
  17968. wc_test_ret_t ret;
  17969. Cert* myCert = NULL;
  17970. int certSz;
  17971. size_t bytes3;
  17972. word32 idx3 = 0;
  17973. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  17974. XFILE file3;
  17975. #endif
  17976. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  17977. struct tm beforeTime;
  17978. struct tm afterTime;
  17979. #endif
  17980. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  17981. (void)keypub;
  17982. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17983. if (caKey == NULL)
  17984. ERROR_OUT(MEMORY_E, exit_rsa);
  17985. #ifdef WOLFSSL_TEST_CERT
  17986. if (decode == NULL)
  17987. ERROR_OUT(MEMORY_E, exit_rsa);
  17988. #endif
  17989. #endif
  17990. XMEMSET(caKey, 0, sizeof *caKey);
  17991. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17992. if (der == NULL) {
  17993. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17994. }
  17995. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17996. if (myCert == NULL) {
  17997. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17998. }
  17999. /* self signed */
  18000. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18001. if (ret != 0) {
  18002. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18003. }
  18004. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18005. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  18006. myCert->serialSz = (int)sizeof(mySerial);
  18007. myCert->isCA = 1;
  18008. #ifndef NO_SHA256
  18009. myCert->sigType = CTC_SHA256wRSA;
  18010. #else
  18011. myCert->sigType = CTC_SHAwRSA;
  18012. #endif
  18013. #ifdef WOLFSSL_CERT_EXT
  18014. /* add Policies */
  18015. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18016. CTC_MAX_CERTPOL_SZ);
  18017. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  18018. CTC_MAX_CERTPOL_SZ);
  18019. myCert->certPoliciesNb = 2;
  18020. /* add SKID from the Public Key */
  18021. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  18022. if (ret != 0) {
  18023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18024. }
  18025. /* add AKID from the Public Key */
  18026. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  18027. if (ret != 0) {
  18028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18029. }
  18030. /* add Key Usage */
  18031. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  18032. if (ret != 0) {
  18033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18034. }
  18035. #ifdef WOLFSSL_EKU_OID
  18036. {
  18037. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  18038. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  18039. HEAP_HINT);
  18040. if (ret != 0) {
  18041. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18042. }
  18043. }
  18044. #endif /* WOLFSSL_EKU_OID */
  18045. #endif /* WOLFSSL_CERT_EXT */
  18046. do {
  18047. #if defined(WOLFSSL_ASYNC_CRYPT)
  18048. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18049. #endif
  18050. if (ret >= 0) {
  18051. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  18052. }
  18053. } while (ret == WC_PENDING_E);
  18054. if (ret < 0) {
  18055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18056. }
  18057. certSz = (int)ret;
  18058. #ifdef WOLFSSL_TEST_CERT
  18059. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18060. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18061. if (ret != 0) {
  18062. FreeDecodedCert(decode);
  18063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18064. }
  18065. FreeDecodedCert(decode);
  18066. #endif
  18067. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  18068. CERT_TYPE);
  18069. if (ret != 0) {
  18070. goto exit_rsa;
  18071. }
  18072. /* Setup Certificate */
  18073. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18074. if (ret < 0) {
  18075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18076. }
  18077. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  18078. /* Get CA Cert for testing */
  18079. #ifdef USE_CERT_BUFFERS_1024
  18080. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  18081. bytes3 = sizeof_ca_cert_der_1024;
  18082. #elif defined(USE_CERT_BUFFERS_2048)
  18083. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  18084. bytes3 = sizeof_ca_cert_der_2048;
  18085. #else
  18086. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  18087. if (!file3) {
  18088. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18089. }
  18090. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18091. XFCLOSE(file3);
  18092. if (bytes3 == 0)
  18093. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18094. #endif /* USE_CERT_BUFFERS */
  18095. #if defined(WOLFSSL_ALT_NAMES)
  18096. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  18097. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  18098. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  18099. if (ret != 0)
  18100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18101. #endif
  18102. /* get alt names from der */
  18103. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  18104. if (ret != 0)
  18105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18106. /* get dates from der */
  18107. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  18108. if (ret != 0)
  18109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18110. #ifndef NO_ASN_TIME
  18111. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  18112. if (ret < 0)
  18113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18114. #endif
  18115. #endif /* WOLFSSL_ALT_NAMES */
  18116. #endif /* WOLFSSL_ALT_NAMES || HAVE_PKCS7 */
  18117. /* Get CA Key */
  18118. #ifdef USE_CERT_BUFFERS_1024
  18119. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18120. bytes3 = sizeof_ca_key_der_1024;
  18121. #elif defined(USE_CERT_BUFFERS_2048)
  18122. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18123. bytes3 = sizeof_ca_key_der_2048;
  18124. #else
  18125. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18126. if (!file3) {
  18127. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18128. }
  18129. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18130. XFCLOSE(file3);
  18131. if (bytes3 == 0)
  18132. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18133. #endif /* USE_CERT_BUFFERS */
  18134. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18135. if (ret != 0)
  18136. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18137. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18138. if (ret != 0)
  18139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18140. #ifndef NO_SHA256
  18141. myCert->sigType = CTC_SHA256wRSA;
  18142. #else
  18143. myCert->sigType = CTC_SHAwRSA;
  18144. #endif
  18145. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18146. #ifdef WOLFSSL_CERT_EXT
  18147. /* add Policies */
  18148. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18149. CTC_MAX_CERTPOL_SZ);
  18150. myCert->certPoliciesNb =1;
  18151. /* add SKID from the Public Key */
  18152. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  18153. if (ret != 0)
  18154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18155. /* add AKID from the CA certificate */
  18156. #if defined(USE_CERT_BUFFERS_2048)
  18157. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18158. sizeof_ca_cert_der_2048);
  18159. #elif defined(USE_CERT_BUFFERS_1024)
  18160. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18161. sizeof_ca_cert_der_1024);
  18162. #else
  18163. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18164. #endif
  18165. if (ret != 0)
  18166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18167. /* add Key Usage */
  18168. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  18169. if (ret != 0)
  18170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18171. #endif /* WOLFSSL_CERT_EXT */
  18172. #if defined(USE_CERT_BUFFERS_2048)
  18173. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18174. sizeof_ca_cert_der_2048);
  18175. #elif defined(USE_CERT_BUFFERS_1024)
  18176. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18177. sizeof_ca_cert_der_1024);
  18178. #else
  18179. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18180. #endif
  18181. if (ret < 0)
  18182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18183. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  18184. if (certSz < 0) {
  18185. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18186. }
  18187. ret = 0;
  18188. do {
  18189. #if defined(WOLFSSL_ASYNC_CRYPT)
  18190. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18191. #endif
  18192. if (ret >= 0) {
  18193. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  18194. caKey, NULL, rng);
  18195. }
  18196. } while (ret == WC_PENDING_E);
  18197. if (ret < 0)
  18198. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18199. certSz = (int)ret;
  18200. #ifdef WOLFSSL_TEST_CERT
  18201. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18202. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18203. if (ret != 0) {
  18204. FreeDecodedCert(decode);
  18205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18206. }
  18207. FreeDecodedCert(decode);
  18208. #endif
  18209. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  18210. CERT_TYPE);
  18211. if (ret != 0) {
  18212. goto exit_rsa;
  18213. }
  18214. exit_rsa:
  18215. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18216. if (caKey != NULL) {
  18217. wc_FreeRsaKey(caKey);
  18218. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18219. }
  18220. #ifdef WOLFSSL_TEST_CERT
  18221. if (decode != NULL)
  18222. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18223. #endif
  18224. #else
  18225. wc_FreeRsaKey(caKey);
  18226. #endif
  18227. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18228. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18229. return ret;
  18230. }
  18231. #endif
  18232. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  18233. defined(WOLFSSL_CERT_GEN)
  18234. /* Make Cert / Sign example for ECC cert and RSA CA */
  18235. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  18236. {
  18237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18238. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18239. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18240. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18241. #ifdef WOLFSSL_TEST_CERT
  18242. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18243. #endif
  18244. #else
  18245. RsaKey caKey[1];
  18246. ecc_key caEccKey[1];
  18247. ecc_key caEccKeyPub[1];
  18248. #ifdef WOLFSSL_TEST_CERT
  18249. DecodedCert decode[1];
  18250. #endif
  18251. #endif
  18252. byte* der = NULL;
  18253. Cert* myCert = NULL;
  18254. int certSz;
  18255. size_t bytes3;
  18256. word32 idx3 = 0;
  18257. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  18258. || !defined(USE_CERT_BUFFERS_256)
  18259. #ifndef NO_FILESYSTEM
  18260. XFILE file3;
  18261. #endif
  18262. #endif
  18263. wc_test_ret_t ret;
  18264. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18265. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  18266. #ifdef WOLFSSL_TEST_CERT
  18267. || (decode == NULL)
  18268. #endif
  18269. )
  18270. ERROR_OUT(MEMORY_E, exit_rsa);
  18271. #endif
  18272. XMEMSET(caKey, 0, sizeof *caKey);
  18273. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  18274. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  18275. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18276. if (der == NULL) {
  18277. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18278. }
  18279. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18280. if (myCert == NULL) {
  18281. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18282. }
  18283. /* Get CA Key */
  18284. #ifdef USE_CERT_BUFFERS_1024
  18285. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18286. bytes3 = sizeof_ca_key_der_1024;
  18287. #elif defined(USE_CERT_BUFFERS_2048)
  18288. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18289. bytes3 = sizeof_ca_key_der_2048;
  18290. #else
  18291. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18292. if (!file3) {
  18293. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18294. }
  18295. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18296. XFCLOSE(file3);
  18297. if (bytes3 == 0)
  18298. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18299. #endif /* USE_CERT_BUFFERS */
  18300. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18301. if (ret != 0)
  18302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18303. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18304. if (ret != 0)
  18305. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18306. /* Get Cert Key */
  18307. #if defined(USE_CERT_BUFFERS_256)
  18308. {
  18309. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  18310. bytes3 = sizeof_ecc_key_pub_der_256;
  18311. }
  18312. #elif !defined(NO_FILESYSTEM)
  18313. {
  18314. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  18315. if (!file3) {
  18316. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18317. }
  18318. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18319. XFCLOSE(file3);
  18320. if (bytes3 == 0)
  18321. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18322. }
  18323. #else
  18324. {
  18325. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
  18326. ERROR_OUT(ASN_PARSE_E, exit_rsa);
  18327. }
  18328. #endif
  18329. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  18330. if (ret != 0)
  18331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18332. idx3 = 0;
  18333. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  18334. if (ret != 0)
  18335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18336. /* Setup Certificate */
  18337. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18338. if (ret != 0)
  18339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18340. #ifndef NO_SHA256
  18341. myCert->sigType = CTC_SHA256wRSA;
  18342. #else
  18343. myCert->sigType = CTC_SHAwRSA;
  18344. #endif
  18345. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18346. #ifdef WOLFSSL_CERT_EXT
  18347. /* add Policies */
  18348. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  18349. CTC_MAX_CERTPOL_SZ);
  18350. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  18351. CTC_MAX_CERTPOL_SZ);
  18352. myCert->certPoliciesNb = 2;
  18353. /* add SKID from the Public Key */
  18354. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  18355. if (ret != 0)
  18356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18357. /* add AKID from the CA certificate */
  18358. #if defined(USE_CERT_BUFFERS_2048)
  18359. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18360. sizeof_ca_cert_der_2048);
  18361. #elif defined(USE_CERT_BUFFERS_1024)
  18362. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18363. sizeof_ca_cert_der_1024);
  18364. #else
  18365. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18366. #endif
  18367. if (ret != 0)
  18368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18369. /* add Key Usage */
  18370. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  18371. if (ret != 0)
  18372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18373. #endif /* WOLFSSL_CERT_EXT */
  18374. #if defined(USE_CERT_BUFFERS_2048)
  18375. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18376. sizeof_ca_cert_der_2048);
  18377. #elif defined(USE_CERT_BUFFERS_1024)
  18378. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18379. sizeof_ca_cert_der_1024);
  18380. #else
  18381. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18382. #endif
  18383. if (ret < 0)
  18384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18385. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  18386. if (certSz < 0) {
  18387. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18388. }
  18389. ret = 0;
  18390. do {
  18391. #if defined(WOLFSSL_ASYNC_CRYPT)
  18392. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18393. #endif
  18394. if (ret >= 0) {
  18395. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  18396. FOURK_BUF, caKey, NULL, rng);
  18397. }
  18398. } while (ret == WC_PENDING_E);
  18399. if (ret < 0)
  18400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18401. certSz = (int)ret;
  18402. #ifdef WOLFSSL_TEST_CERT
  18403. InitDecodedCert(decode, der, certSz, 0);
  18404. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18405. if (ret != 0) {
  18406. FreeDecodedCert(decode);
  18407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18408. }
  18409. FreeDecodedCert(decode);
  18410. #endif
  18411. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  18412. CERT_TYPE);
  18413. if (ret != 0) {
  18414. goto exit_rsa;
  18415. }
  18416. exit_rsa:
  18417. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18418. if (caKey != NULL) {
  18419. wc_FreeRsaKey(caKey);
  18420. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18421. }
  18422. if (caEccKey != NULL) {
  18423. wc_ecc_free(caEccKey);
  18424. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18425. }
  18426. if (caEccKeyPub != NULL) {
  18427. wc_ecc_free(caEccKeyPub);
  18428. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18429. }
  18430. #ifdef WOLFSSL_TEST_CERT
  18431. if (decode != NULL)
  18432. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18433. #endif
  18434. #else
  18435. wc_FreeRsaKey(caKey);
  18436. wc_ecc_free(caEccKey);
  18437. wc_ecc_free(caEccKeyPub);
  18438. #endif
  18439. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18440. myCert = NULL;
  18441. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18442. der = NULL;
  18443. if (ret >= 0)
  18444. ret = 0;
  18445. return ret;
  18446. }
  18447. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  18448. #ifdef WOLFSSL_KEY_GEN
  18449. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  18450. {
  18451. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18452. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18453. #else
  18454. RsaKey genKey[1];
  18455. #endif
  18456. wc_test_ret_t ret;
  18457. #ifndef WOLFSSL_NO_MALLOC
  18458. byte* der = NULL;
  18459. #else
  18460. byte der[1024];
  18461. #endif
  18462. #ifndef WOLFSSL_CRYPTOCELL
  18463. word32 idx = 0;
  18464. #endif
  18465. int derSz = 0;
  18466. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  18467. int keySz = 1024;
  18468. #else
  18469. int keySz = 2048;
  18470. #endif
  18471. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18472. if (! genKey)
  18473. ERROR_OUT(MEMORY_E, exit_rsa);
  18474. #endif
  18475. XMEMSET(genKey, 0, sizeof *genKey);
  18476. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  18477. if (ret != 0)
  18478. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18479. #ifdef HAVE_FIPS
  18480. for (;;) {
  18481. #endif
  18482. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  18483. #if defined(WOLFSSL_ASYNC_CRYPT)
  18484. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18485. #endif
  18486. #ifdef HAVE_FIPS
  18487. if (ret == PRIME_GEN_E)
  18488. continue;
  18489. break;
  18490. }
  18491. #endif
  18492. if (ret != 0)
  18493. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18494. TEST_SLEEP();
  18495. #ifdef WOLFSSL_RSA_KEY_CHECK
  18496. ret = wc_CheckRsaKey(genKey);
  18497. if (ret != 0)
  18498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18499. #endif
  18500. #ifndef WOLFSSL_NO_MALLOC
  18501. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18502. if (der == NULL) {
  18503. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18504. }
  18505. #endif
  18506. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  18507. if (derSz < 0) {
  18508. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  18509. }
  18510. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18511. PRIVATEKEY_TYPE);
  18512. if (ret != 0) {
  18513. goto exit_rsa;
  18514. }
  18515. wc_FreeRsaKey(genKey);
  18516. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  18517. if (ret != 0)
  18518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18519. #ifndef WOLFSSL_CRYPTOCELL
  18520. idx = 0;
  18521. /* The private key part of the key gen pairs from cryptocell can't be exported */
  18522. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
  18523. if (ret != 0)
  18524. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18525. #endif /* WOLFSSL_CRYPTOCELL */
  18526. exit_rsa:
  18527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18528. if (genKey) {
  18529. wc_FreeRsaKey(genKey);
  18530. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18531. }
  18532. #else
  18533. wc_FreeRsaKey(genKey);
  18534. #endif
  18535. #ifndef WOLFSSL_NO_MALLOC
  18536. if (der != NULL) {
  18537. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18538. der = NULL;
  18539. }
  18540. #endif
  18541. return ret;
  18542. }
  18543. #endif
  18544. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  18545. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  18546. (!defined(HAVE_FIPS) || \
  18547. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  18548. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  18549. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  18550. {
  18551. wc_test_ret_t ret = 0;
  18552. word32 idx = 0;
  18553. const char inStr[] = TEST_STRING;
  18554. const word32 inLen = (word32)TEST_STRING_SZ;
  18555. const word32 outSz = RSA_TEST_BYTES;
  18556. const word32 plainSz = RSA_TEST_BYTES;
  18557. byte* res = NULL;
  18558. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18559. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18560. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18561. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18562. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18563. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18564. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18565. if (in == NULL || out == NULL || plain == NULL)
  18566. ERROR_OUT(MEMORY_E, exit_rsa);
  18567. #endif
  18568. XMEMCPY(in, inStr, inLen);
  18569. #ifndef NO_SHA
  18570. do {
  18571. #if defined(WOLFSSL_ASYNC_CRYPT)
  18572. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18573. #endif
  18574. if (ret >= 0) {
  18575. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18576. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18577. }
  18578. } while (ret == WC_PENDING_E);
  18579. if (ret < 0)
  18580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18581. TEST_SLEEP();
  18582. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18583. idx = (word32)ret;
  18584. do {
  18585. #if defined(WOLFSSL_ASYNC_CRYPT)
  18586. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18587. #endif
  18588. if (ret >= 0) {
  18589. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18590. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18591. }
  18592. } while (ret == WC_PENDING_E);
  18593. if (ret < 0)
  18594. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18595. if (XMEMCMP(plain, in, inLen)) {
  18596. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18597. }
  18598. TEST_SLEEP();
  18599. #endif /* NO_SHA */
  18600. #endif
  18601. #ifndef NO_SHA256
  18602. XMEMSET(plain, 0, plainSz);
  18603. do {
  18604. #if defined(WOLFSSL_ASYNC_CRYPT)
  18605. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18606. #endif
  18607. if (ret >= 0) {
  18608. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18609. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18610. }
  18611. } while (ret == WC_PENDING_E);
  18612. if (ret < 0)
  18613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18614. TEST_SLEEP();
  18615. idx = (word32)ret;
  18616. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18617. do {
  18618. #if defined(WOLFSSL_ASYNC_CRYPT)
  18619. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18620. #endif
  18621. if (ret >= 0) {
  18622. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18623. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18624. }
  18625. } while (ret == WC_PENDING_E);
  18626. if (ret < 0)
  18627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18628. if (XMEMCMP(plain, in, inLen)) {
  18629. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18630. }
  18631. TEST_SLEEP();
  18632. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18633. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18634. do {
  18635. #if defined(WOLFSSL_ASYNC_CRYPT)
  18636. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18637. #endif
  18638. if (ret >= 0) {
  18639. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  18640. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18641. }
  18642. } while (ret == WC_PENDING_E);
  18643. if (ret < 0)
  18644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18645. if (ret != (int)inLen) {
  18646. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18647. }
  18648. if (XMEMCMP(res, in, inLen)) {
  18649. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18650. }
  18651. TEST_SLEEP();
  18652. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18653. /* check fails if not using the same optional label */
  18654. XMEMSET(plain, 0, plainSz);
  18655. do {
  18656. #if defined(WOLFSSL_ASYNC_CRYPT)
  18657. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18658. #endif
  18659. if (ret >= 0) {
  18660. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18661. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18662. }
  18663. } while (ret == WC_PENDING_E);
  18664. if (ret < 0)
  18665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18666. TEST_SLEEP();
  18667. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18668. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18669. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18670. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  18671. * due to label */
  18672. idx = (word32)ret;
  18673. do {
  18674. #if defined(WOLFSSL_ASYNC_CRYPT)
  18675. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18676. #endif
  18677. if (ret >= 0) {
  18678. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18679. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18680. }
  18681. } while (ret == WC_PENDING_E);
  18682. if (ret > 0) { /* in this case decrypt should fail */
  18683. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18684. }
  18685. ret = 0;
  18686. TEST_SLEEP();
  18687. #endif /* !HAVE_CAVIUM */
  18688. /* check using optional label with encrypt/decrypt */
  18689. XMEMSET(plain, 0, plainSz);
  18690. do {
  18691. #if defined(WOLFSSL_ASYNC_CRYPT)
  18692. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18693. #endif
  18694. if (ret >= 0) {
  18695. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18696. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18697. }
  18698. } while (ret == WC_PENDING_E);
  18699. if (ret < 0)
  18700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18701. TEST_SLEEP();
  18702. idx = (word32)ret;
  18703. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18704. do {
  18705. #if defined(WOLFSSL_ASYNC_CRYPT)
  18706. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18707. #endif
  18708. if (ret >= 0) {
  18709. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18710. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18711. }
  18712. } while (ret == WC_PENDING_E);
  18713. if (ret < 0)
  18714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18715. if (XMEMCMP(plain, in, inLen)) {
  18716. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18717. }
  18718. TEST_SLEEP();
  18719. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18720. #ifndef NO_SHA
  18721. /* check fail using mismatch hash algorithms */
  18722. XMEMSET(plain, 0, plainSz);
  18723. do {
  18724. #if defined(WOLFSSL_ASYNC_CRYPT)
  18725. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18726. #endif
  18727. if (ret >= 0) {
  18728. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18729. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  18730. }
  18731. } while (ret == WC_PENDING_E);
  18732. if (ret < 0)
  18733. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18734. TEST_SLEEP();
  18735. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18736. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18737. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18738. idx = (word32)ret;
  18739. do {
  18740. #if defined(WOLFSSL_ASYNC_CRYPT)
  18741. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18742. #endif
  18743. if (ret >= 0) {
  18744. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18745. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  18746. in, inLen);
  18747. }
  18748. } while (ret == WC_PENDING_E);
  18749. if (ret > 0) { /* should fail */
  18750. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18751. }
  18752. ret = 0;
  18753. TEST_SLEEP();
  18754. #endif /* !HAVE_CAVIUM */
  18755. #endif /* NO_SHA */
  18756. #endif /* NO_SHA256 */
  18757. #ifdef WOLFSSL_SHA512
  18758. /* Check valid RSA key size is used while using hash length of SHA512
  18759. If key size is less than (hash length * 2) + 2 then is invalid use
  18760. and test, since OAEP padding requires this.
  18761. BAD_FUNC_ARG is returned when this case is not met */
  18762. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  18763. XMEMSET(plain, 0, plainSz);
  18764. do {
  18765. #if defined(WOLFSSL_ASYNC_CRYPT)
  18766. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18767. #endif
  18768. if (ret >= 0) {
  18769. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18770. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18771. }
  18772. } while (ret == WC_PENDING_E);
  18773. if (ret < 0)
  18774. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18775. TEST_SLEEP();
  18776. idx = (word32)ret;
  18777. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18778. do {
  18779. #if defined(WOLFSSL_ASYNC_CRYPT)
  18780. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18781. #endif
  18782. if (ret >= 0) {
  18783. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18784. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18785. }
  18786. } while (ret == WC_PENDING_E);
  18787. if (ret < 0)
  18788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18789. if (XMEMCMP(plain, in, inLen)) {
  18790. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18791. }
  18792. TEST_SLEEP();
  18793. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18794. }
  18795. #endif /* WOLFSSL_SHA512 */
  18796. /* check using pkcsv15 padding with _ex API */
  18797. XMEMSET(plain, 0, plainSz);
  18798. do {
  18799. #if defined(WOLFSSL_ASYNC_CRYPT)
  18800. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18801. #endif
  18802. if (ret >= 0) {
  18803. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18804. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18805. }
  18806. } while (ret == WC_PENDING_E);
  18807. if (ret < 0)
  18808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18809. TEST_SLEEP();
  18810. idx = (word32)ret;
  18811. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18812. do {
  18813. #if defined(WOLFSSL_ASYNC_CRYPT)
  18814. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18815. #endif
  18816. if (ret >= 0) {
  18817. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18818. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18819. }
  18820. } while (ret == WC_PENDING_E);
  18821. if (ret < 0)
  18822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18823. if (XMEMCMP(plain, in, inLen)) {
  18824. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18825. }
  18826. TEST_SLEEP();
  18827. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18828. exit_rsa:
  18829. WC_FREE_VAR(in, HEAP_HINT);
  18830. WC_FREE_VAR(out, HEAP_HINT);
  18831. WC_FREE_VAR(plain, HEAP_HINT);
  18832. (void)idx;
  18833. (void)inStr;
  18834. (void)res;
  18835. if (ret >= 0)
  18836. ret = 0;
  18837. return ret;
  18838. }
  18839. #endif
  18840. #endif
  18841. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  18842. {
  18843. wc_test_ret_t ret;
  18844. size_t bytes;
  18845. WC_RNG rng;
  18846. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18847. byte* tmp = NULL;
  18848. byte* der = NULL;
  18849. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18850. #else
  18851. RsaKey key[1];
  18852. byte tmp[FOURK_BUF];
  18853. #endif
  18854. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18855. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18856. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18857. #else
  18858. RsaKey keypub[1];
  18859. #endif
  18860. #endif
  18861. word32 idx = 0;
  18862. const char inStr[] = TEST_STRING;
  18863. const word32 inLen = (word32)TEST_STRING_SZ;
  18864. const word32 outSz = RSA_TEST_BYTES;
  18865. const word32 plainSz = RSA_TEST_BYTES;
  18866. byte* res = NULL;
  18867. #ifndef NO_SIG_WRAPPER
  18868. int modLen;
  18869. #endif
  18870. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  18871. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  18872. !defined(NO_FILESYSTEM)
  18873. XFILE file;
  18874. #ifdef WOLFSSL_TEST_CERT
  18875. XFILE file2;
  18876. #endif
  18877. #endif
  18878. #ifdef WOLFSSL_TEST_CERT
  18879. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18880. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18881. #else
  18882. DecodedCert cert[1];
  18883. #endif
  18884. #ifndef NO_ASN_TIME
  18885. struct tm timearg;
  18886. const byte* date;
  18887. byte dateFormat;
  18888. int dateLength;
  18889. #endif
  18890. #endif
  18891. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18892. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18893. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18894. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18895. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18896. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18897. WOLFSSL_ENTER("rsa_test");
  18898. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18899. if (in == NULL || out == NULL || plain == NULL)
  18900. ERROR_OUT(MEMORY_E, exit_rsa);
  18901. #endif
  18902. XMEMCPY(in, inStr, inLen);
  18903. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18904. if (key == NULL)
  18905. ERROR_OUT(MEMORY_E, exit_rsa);
  18906. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18907. if (keypub == NULL)
  18908. ERROR_OUT(MEMORY_E, exit_rsa);
  18909. #endif
  18910. #ifdef WOLFSSL_TEST_CERT
  18911. if (cert == NULL)
  18912. ERROR_OUT(MEMORY_E, exit_rsa);
  18913. #endif
  18914. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18915. /* initialize stack structures */
  18916. XMEMSET(&rng, 0, sizeof(rng));
  18917. XMEMSET(key, 0, sizeof *key);
  18918. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18919. XMEMSET(keypub, 0, sizeof *keypub);
  18920. #endif
  18921. #if !defined(NO_ASN)
  18922. ret = rsa_decode_test(key);
  18923. if (ret != 0)
  18924. ERROR_OUT(ret, exit_rsa);
  18925. #endif
  18926. #ifdef USE_CERT_BUFFERS_1024
  18927. bytes = (size_t)sizeof_client_key_der_1024;
  18928. if (bytes < (size_t)sizeof_client_cert_der_1024)
  18929. bytes = (size_t)sizeof_client_cert_der_1024;
  18930. #elif defined(USE_CERT_BUFFERS_2048)
  18931. bytes = (size_t)sizeof_client_key_der_2048;
  18932. if (bytes < (size_t)sizeof_client_cert_der_2048)
  18933. bytes = (size_t)sizeof_client_cert_der_2048;
  18934. #elif defined(USE_CERT_BUFFERS_3072)
  18935. bytes = (size_t)sizeof_client_key_der_3072;
  18936. if (bytes < (size_t)sizeof_client_cert_der_3072)
  18937. bytes = (size_t)sizeof_client_cert_der_3072;
  18938. #elif defined(USE_CERT_BUFFERS_4096)
  18939. bytes = (size_t)sizeof_client_key_der_4096;
  18940. if (bytes < (size_t)sizeof_client_cert_der_4096)
  18941. bytes = (size_t)sizeof_client_cert_der_4096;
  18942. #else
  18943. bytes = FOURK_BUF;
  18944. #endif
  18945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18946. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18947. if (tmp == NULL)
  18948. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18949. #endif
  18950. #ifdef USE_CERT_BUFFERS_1024
  18951. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  18952. #elif defined(USE_CERT_BUFFERS_2048)
  18953. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  18954. #elif defined(USE_CERT_BUFFERS_3072)
  18955. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  18956. #elif defined(USE_CERT_BUFFERS_4096)
  18957. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  18958. #elif !defined(NO_FILESYSTEM)
  18959. file = XFOPEN(clientKey, "rb");
  18960. if (!file) {
  18961. err_sys("can't open ./certs/client-key.der, "
  18962. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  18963. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18964. }
  18965. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  18966. XFCLOSE(file);
  18967. if (bytes == 0)
  18968. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18969. #else
  18970. /* No key to use. */
  18971. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18972. #endif /* USE_CERT_BUFFERS */
  18973. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  18974. if (ret != 0)
  18975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18976. #ifndef NO_ASN
  18977. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  18978. if (ret != 0)
  18979. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18980. #ifndef NO_SIG_WRAPPER
  18981. modLen = wc_RsaEncryptSize(key);
  18982. #endif
  18983. #else
  18984. #ifdef USE_CERT_BUFFERS_2048
  18985. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  18986. if (ret != 0)
  18987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18988. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  18989. if (ret != 0)
  18990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18991. #ifndef NO_SIG_WRAPPER
  18992. modLen = 2048;
  18993. #endif
  18994. #else
  18995. #error Not supported yet!
  18996. #endif
  18997. #endif
  18998. #ifndef WC_NO_RNG
  18999. #ifndef HAVE_FIPS
  19000. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19001. #else
  19002. ret = wc_InitRng(&rng);
  19003. #endif
  19004. if (ret != 0)
  19005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19006. #endif
  19007. #ifndef NO_SIG_WRAPPER
  19008. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  19009. if (ret != 0)
  19010. goto exit_rsa;
  19011. #endif
  19012. #ifdef WC_RSA_NONBLOCK
  19013. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  19014. if (ret != 0)
  19015. goto exit_rsa;
  19016. #endif
  19017. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19018. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19019. do {
  19020. #if defined(WOLFSSL_ASYNC_CRYPT)
  19021. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19022. #endif
  19023. if (ret >= 0) {
  19024. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  19025. }
  19026. } while (ret == WC_PENDING_E);
  19027. if (ret < 0)
  19028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19029. TEST_SLEEP();
  19030. #ifdef WC_RSA_BLINDING
  19031. {
  19032. wc_test_ret_t tmpret = ret;
  19033. ret = wc_RsaSetRNG(key, &rng);
  19034. if (ret < 0)
  19035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19036. ret = tmpret;
  19037. }
  19038. #endif
  19039. idx = (word32)ret; /* save off encrypted length */
  19040. do {
  19041. #if defined(WOLFSSL_ASYNC_CRYPT)
  19042. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19043. #endif
  19044. if (ret >= 0) {
  19045. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  19046. }
  19047. } while (ret == WC_PENDING_E);
  19048. if (ret < 0)
  19049. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19050. if (XMEMCMP(plain, in, inLen)) {
  19051. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19052. }
  19053. TEST_SLEEP();
  19054. do {
  19055. #if defined(WOLFSSL_ASYNC_CRYPT)
  19056. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19057. #endif
  19058. if (ret >= 0) {
  19059. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  19060. }
  19061. } while (ret == WC_PENDING_E);
  19062. if (ret < 0)
  19063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19064. if (ret != (int)inLen) {
  19065. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19066. }
  19067. if (XMEMCMP(res, in, inLen)) {
  19068. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19069. }
  19070. TEST_SLEEP();
  19071. do {
  19072. #if defined(WOLFSSL_ASYNC_CRYPT)
  19073. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19074. #endif
  19075. if (ret >= 0) {
  19076. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  19077. }
  19078. } while (ret == WC_PENDING_E);
  19079. if (ret < 0)
  19080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19081. TEST_SLEEP();
  19082. #elif defined(WOLFSSL_PUBLIC_MP)
  19083. {
  19084. static const byte signature_2048[] = {
  19085. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  19086. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  19087. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  19088. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  19089. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  19090. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  19091. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  19092. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  19093. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  19094. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  19095. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  19096. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  19097. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  19098. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  19099. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  19100. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  19101. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  19102. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  19103. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  19104. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  19105. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  19106. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  19107. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  19108. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  19109. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  19110. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  19111. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  19112. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  19113. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  19114. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  19115. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  19116. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  19117. };
  19118. ret = sizeof(signature_2048);
  19119. XMEMCPY(out, signature_2048, ret);
  19120. }
  19121. #endif
  19122. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  19123. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  19124. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19125. idx = (word32)ret;
  19126. XMEMSET(plain, 0, plainSz);
  19127. do {
  19128. #if defined(WOLFSSL_ASYNC_CRYPT)
  19129. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19130. #endif
  19131. if (ret >= 0) {
  19132. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  19133. #if defined(WOLFSSL_CRYPTOCELL)
  19134. /*
  19135. Cryptocell requires the input data and signature byte array to verify.
  19136. first argument must be the input data
  19137. second argument must be the length of input data
  19138. third argument must be the signature byte array or the output from
  19139. wc_RsaSSL_Sign()
  19140. fourth argument must be the length of the signature byte array
  19141. */
  19142. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  19143. #else
  19144. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  19145. #endif /* WOLFSSL_CRYPTOCELL */
  19146. #else
  19147. byte* dec = NULL;
  19148. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  19149. if (ret > 0) {
  19150. XMEMCPY(plain, dec, ret);
  19151. }
  19152. #endif
  19153. }
  19154. } while (ret == WC_PENDING_E);
  19155. if (ret < 0)
  19156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19157. if (XMEMCMP(plain, in, (size_t)ret)) {
  19158. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19159. }
  19160. TEST_SLEEP();
  19161. #endif
  19162. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  19163. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  19164. #if (!defined(HAVE_FIPS) || \
  19165. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  19166. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19167. ret = rsa_oaep_padding_test(key, &rng);
  19168. if (ret != 0)
  19169. return ret;
  19170. #endif /* !HAVE_FIPS */
  19171. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  19172. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  19173. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  19174. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19175. ret = rsa_export_key_test(key);
  19176. if (ret != 0)
  19177. return ret;
  19178. #endif
  19179. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19180. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19181. ret = rsa_flatten_test(key);
  19182. if (ret != 0)
  19183. return ret;
  19184. #endif
  19185. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  19186. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  19187. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  19188. (void)clientCert;
  19189. #endif
  19190. #ifdef WOLFSSL_TEST_CERT
  19191. #if defined(WOLFSSL_MDK_ARM)
  19192. #define sizeof(s) XSTRLEN((char *)(s))
  19193. #endif
  19194. #ifdef USE_CERT_BUFFERS_1024
  19195. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  19196. bytes = (size_t)sizeof_client_cert_der_1024;
  19197. #elif defined(USE_CERT_BUFFERS_2048)
  19198. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  19199. bytes = (size_t)sizeof_client_cert_der_2048;
  19200. #elif defined(USE_CERT_BUFFERS_3072)
  19201. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  19202. bytes = (size_t)sizeof_client_cert_der_3072;
  19203. #elif defined(USE_CERT_BUFFERS_4096)
  19204. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  19205. bytes = (size_t)sizeof_client_cert_der_4096;
  19206. #elif !defined(NO_FILESYSTEM)
  19207. file2 = XFOPEN(clientCert, "rb");
  19208. if (!file2) {
  19209. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19210. }
  19211. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  19212. XFCLOSE(file2);
  19213. if (bytes == 0)
  19214. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19215. #else
  19216. /* No certificate to use. */
  19217. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19218. #endif
  19219. #ifdef sizeof
  19220. #undef sizeof
  19221. #endif
  19222. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  19223. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  19224. if (ret != 0) {
  19225. FreeDecodedCert(cert);
  19226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19227. }
  19228. #ifndef NO_ASN_TIME
  19229. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  19230. &dateFormat, &dateLength);
  19231. if (ret != 0) {
  19232. FreeDecodedCert(cert);
  19233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19234. }
  19235. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  19236. if (ret != 0) {
  19237. FreeDecodedCert(cert);
  19238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19239. }
  19240. #endif
  19241. FreeDecodedCert(cert);
  19242. #endif /* WOLFSSL_TEST_CERT */
  19243. #ifdef WOLFSSL_CERT_EXT
  19244. #ifdef USE_CERT_BUFFERS_1024
  19245. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19246. bytes = sizeof_client_keypub_der_1024;
  19247. #elif defined(USE_CERT_BUFFERS_2048)
  19248. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19249. bytes = sizeof_client_keypub_der_2048;
  19250. #elif defined(USE_CERT_BUFFERS_3072)
  19251. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  19252. bytes = sizeof_client_keypub_der_3072;
  19253. #elif defined(USE_CERT_BUFFERS_4096)
  19254. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  19255. bytes = sizeof_client_keypub_der_4096;
  19256. #else
  19257. file = XFOPEN(clientKeyPub, "rb");
  19258. if (!file) {
  19259. err_sys("can't open ./certs/client-keyPub.der, "
  19260. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19261. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19262. }
  19263. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19264. XFCLOSE(file);
  19265. if (bytes == 0)
  19266. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19267. #endif /* USE_CERT_BUFFERS */
  19268. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  19269. if (ret != 0)
  19270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19271. idx = 0;
  19272. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  19273. if (ret != 0)
  19274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19275. #endif /* WOLFSSL_CERT_EXT */
  19276. #ifdef WOLFSSL_KEY_GEN
  19277. ret = rsa_keygen_test(&rng);
  19278. if (ret != 0)
  19279. goto exit_rsa;
  19280. #endif
  19281. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  19282. /* Make Cert / Sign example for RSA cert and RSA CA */
  19283. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  19284. if (ret != 0)
  19285. goto exit_rsa;
  19286. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  19287. ret = rsa_ecc_certgen_test(&rng, tmp);
  19288. if (ret != 0)
  19289. goto exit_rsa;
  19290. #endif
  19291. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  19292. {
  19293. Cert *req;
  19294. int derSz;
  19295. #ifndef WOLFSSL_SMALL_STACK
  19296. byte* der = NULL;
  19297. #endif
  19298. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19299. if (! req)
  19300. ERROR_OUT(MEMORY_E, exit_rsa);
  19301. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  19302. if (der == NULL) {
  19303. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19304. }
  19305. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  19306. if (ret != 0)
  19307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19308. req->version = 0;
  19309. req->isCA = 1;
  19310. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  19311. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  19312. #ifndef NO_SHA256
  19313. req->sigType = CTC_SHA256wRSA;
  19314. #else
  19315. req->sigType = CTC_SHAwRSA;
  19316. #endif
  19317. #ifdef WOLFSSL_CERT_EXT
  19318. /* add SKID from the Public Key */
  19319. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  19320. if (ret != 0)
  19321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19322. /* add Key Usage */
  19323. ret = wc_SetKeyUsage(req, certKeyUsage2);
  19324. if (ret != 0)
  19325. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19326. /* add Extended Key Usage */
  19327. ret = wc_SetExtKeyUsage(req,
  19328. "serverAuth,clientAuth,codeSigning,"
  19329. "emailProtection,timeStamping,OCSPSigning");
  19330. if (ret != 0)
  19331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19332. #ifdef WOLFSSL_EKU_OID
  19333. {
  19334. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  19335. "2.16.840.1.111111.100.1.10.1";
  19336. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  19337. HEAP_HINT);
  19338. if (ret != 0)
  19339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19340. }
  19341. #endif /* WOLFSSL_EKU_OID */
  19342. #endif /* WOLFSSL_CERT_EXT */
  19343. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19344. if (derSz < 0) {
  19345. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19346. }
  19347. #ifdef WOLFSSL_CERT_EXT
  19348. /* Try again with "any" flag set, will override all others */
  19349. ret = wc_SetExtKeyUsage(req, "any");
  19350. if (ret != 0)
  19351. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19352. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19353. if (derSz < 0) {
  19354. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19355. }
  19356. #endif /* WOLFSSL_CERT_EXT */
  19357. ret = 0;
  19358. do {
  19359. #if defined(WOLFSSL_ASYNC_CRYPT)
  19360. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19361. #endif
  19362. if (ret >= 0) {
  19363. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  19364. key, NULL, &rng);
  19365. }
  19366. } while (ret == WC_PENDING_E);
  19367. if (ret < 0)
  19368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19369. derSz = (int)ret;
  19370. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  19371. CERTREQ_TYPE);
  19372. if (ret != 0) {
  19373. goto exit_rsa;
  19374. }
  19375. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  19376. if (derSz < 0) {
  19377. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19378. }
  19379. /* Test getting the size of the buffer without providing the buffer.
  19380. * derSz is set to the "largest buffer" we are willing to allocate. */
  19381. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  19382. if (derSz < 0) {
  19383. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19384. }
  19385. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19386. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19387. der = NULL;
  19388. }
  19389. #endif /* WOLFSSL_CERT_REQ */
  19390. #endif /* WOLFSSL_CERT_GEN */
  19391. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  19392. /* Need to create known good signatures to test with this. */
  19393. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19394. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19395. ret = rsa_pss_test(&rng, key);
  19396. if (ret != 0)
  19397. goto exit_rsa;
  19398. #endif
  19399. #endif
  19400. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  19401. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19402. /* New key to be loaded in rsa_even_mod_test(). */
  19403. if (key != NULL)
  19404. #endif
  19405. wc_FreeRsaKey(key);
  19406. /* New key to be loaded in rsa_even_mod_test(). */
  19407. ret = rsa_even_mod_test(&rng, key);
  19408. #endif
  19409. exit_rsa:
  19410. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19411. if (key != NULL) {
  19412. wc_FreeRsaKey(key);
  19413. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19414. }
  19415. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19416. if (keypub != NULL) {
  19417. wc_FreeRsaKey(keypub);
  19418. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19419. }
  19420. #endif
  19421. #ifdef WOLFSSL_TEST_CERT
  19422. if (cert != NULL)
  19423. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19424. #endif
  19425. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19426. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19427. #else
  19428. wc_FreeRsaKey(key);
  19429. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19430. wc_FreeRsaKey(keypub);
  19431. #endif
  19432. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19433. wc_FreeRng(&rng);
  19434. WC_FREE_VAR(in, HEAP_HINT);
  19435. WC_FREE_VAR(out, HEAP_HINT);
  19436. WC_FREE_VAR(plain, HEAP_HINT);
  19437. (void)res;
  19438. (void)bytes;
  19439. (void)idx;
  19440. (void)in;
  19441. (void)out;
  19442. (void)plain;
  19443. (void)idx;
  19444. (void)inStr;
  19445. (void)inLen;
  19446. (void)outSz;
  19447. (void)plainSz;
  19448. /* ret can be greater then 0 with certgen but all negative values should
  19449. * be returned and treated as an error */
  19450. if (ret >= 0) {
  19451. return 0;
  19452. }
  19453. else {
  19454. return ret;
  19455. }
  19456. }
  19457. #endif /* !NO_RSA */
  19458. #ifndef NO_DH
  19459. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  19460. {
  19461. wc_test_ret_t ret = 0;
  19462. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19463. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19464. #else
  19465. DhKey key[1];
  19466. #endif
  19467. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  19468. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  19469. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  19470. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  19471. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  19472. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  19473. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  19474. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  19475. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  19476. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  19477. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  19478. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  19479. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  19480. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  19481. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  19482. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  19483. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  19484. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  19485. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  19486. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  19487. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  19488. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  19489. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  19490. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  19491. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  19492. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  19493. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  19494. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  19495. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  19496. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  19497. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  19498. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  19499. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  19500. };
  19501. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  19502. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  19503. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  19504. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  19505. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  19506. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  19507. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  19508. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  19509. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  19510. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  19511. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  19512. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  19513. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  19514. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  19515. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  19516. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  19517. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  19518. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  19519. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  19520. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  19521. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  19522. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  19523. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  19524. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  19525. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  19526. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  19527. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  19528. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  19529. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  19530. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  19531. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  19532. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  19533. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  19534. };
  19535. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  19536. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19537. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19538. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19539. 0x40, 0x52, 0xed, 0x41
  19540. };
  19541. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  19542. 0x00,
  19543. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19544. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19545. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19546. 0x40, 0x52, 0xed, 0x41
  19547. };
  19548. byte priv[256];
  19549. byte pub[256];
  19550. word32 privSz = sizeof(priv);
  19551. word32 pubSz = sizeof(pub);
  19552. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19553. if (key == NULL)
  19554. ERROR_OUT(MEMORY_E, exit_gen_test);
  19555. #endif
  19556. /* Parameter Validation testing. */
  19557. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  19558. if (ret != BAD_FUNC_ARG)
  19559. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19560. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  19561. if (ret != BAD_FUNC_ARG)
  19562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19563. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  19564. if (ret != BAD_FUNC_ARG)
  19565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19566. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  19567. if (ret != BAD_FUNC_ARG)
  19568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19569. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  19570. if (ret != BAD_FUNC_ARG)
  19571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19572. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  19573. if (ret != BAD_FUNC_ARG)
  19574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19575. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19576. if (ret != 0)
  19577. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19578. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  19579. if (ret != 0)
  19580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19581. wc_FreeDhKey(key);
  19582. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19583. if (ret != 0)
  19584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19585. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  19586. if (ret != 0)
  19587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19588. /* Use API. */
  19589. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19590. #if defined(WOLFSSL_ASYNC_CRYPT)
  19591. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19592. #endif
  19593. if (ret != 0)
  19594. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19595. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  19596. if (ret != 0)
  19597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19598. wc_FreeDhKey(key);
  19599. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19600. if (ret != 0)
  19601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19602. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  19603. if (ret != 0)
  19604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19605. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  19606. if (ret != 0)
  19607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19608. #ifndef HAVE_SELFTEST
  19609. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19610. if (ret != 0)
  19611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19612. /* Taint the public key so the check fails. */
  19613. pub[0]++;
  19614. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19615. if (ret != MP_CMP_E) {
  19616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19617. }
  19618. #ifdef WOLFSSL_KEY_GEN
  19619. wc_FreeDhKey(key);
  19620. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19621. if (ret != 0)
  19622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19623. ret = wc_DhGenerateParams(rng, 2048, key);
  19624. if (ret != 0)
  19625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19626. privSz = sizeof(priv);
  19627. pubSz = sizeof(pub);
  19628. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19629. #if defined(WOLFSSL_ASYNC_CRYPT)
  19630. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19631. #endif
  19632. if (ret != 0)
  19633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19634. #endif /* WOLFSSL_KEY_GEN */
  19635. #endif /* HAVE_SELFTEST */
  19636. ret = 0;
  19637. exit_gen_test:
  19638. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19639. if (key) {
  19640. wc_FreeDhKey(key);
  19641. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19642. }
  19643. #else
  19644. wc_FreeDhKey(key);
  19645. #endif
  19646. return ret;
  19647. }
  19648. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  19649. {
  19650. wc_test_ret_t ret = 0;
  19651. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19652. DhKey *smallKey = NULL;
  19653. #else
  19654. DhKey smallKey[1];
  19655. #endif
  19656. byte p[2] = { 1, 7 }; /* 263 in decimal */
  19657. byte g[2] = { 0, 2 };
  19658. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19659. #ifdef WOLFSSL_DH_CONST
  19660. /* the table for constant DH lookup will round to the lowest byte size 21 */
  19661. byte priv[21];
  19662. byte pub[21];
  19663. #else
  19664. byte priv[2];
  19665. byte pub[2];
  19666. #endif
  19667. word32 privSz = sizeof(priv);
  19668. word32 pubSz = sizeof(pub);
  19669. #endif
  19670. int smallKey_inited = 0;
  19671. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19672. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  19673. return WC_TEST_RET_ENC_ERRNO;
  19674. #endif
  19675. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19676. if (ret != 0)
  19677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19678. smallKey_inited = 1;
  19679. /* Parameter Validation testing. */
  19680. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  19681. if (ret != BAD_FUNC_ARG)
  19682. return WC_TEST_RET_ENC_EC(ret);
  19683. wc_FreeDhKey(NULL);
  19684. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  19685. if (ret != BAD_FUNC_ARG) {
  19686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19687. }
  19688. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  19689. if (ret != BAD_FUNC_ARG) {
  19690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19691. }
  19692. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  19693. if (ret != BAD_FUNC_ARG) {
  19694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19695. }
  19696. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  19697. if (ret != BAD_FUNC_ARG) {
  19698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19699. }
  19700. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  19701. if (ret != BAD_FUNC_ARG) {
  19702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19703. }
  19704. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  19705. if (ret != 0)
  19706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19707. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19708. /* Use API. */
  19709. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19710. #if defined(WOLFSSL_ASYNC_CRYPT)
  19711. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19712. #endif
  19713. if (ret != 0) {
  19714. ret = WC_TEST_RET_ENC_EC(ret);
  19715. }
  19716. #else
  19717. (void)rng;
  19718. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  19719. ret = 0;
  19720. #endif
  19721. #endif
  19722. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  19723. {
  19724. byte priv[260];
  19725. byte pub[260];
  19726. word32 privSz = sizeof(priv);
  19727. word32 pubSz = sizeof(pub);
  19728. /* test odd ball param generation with DH */
  19729. wc_FreeDhKey(smallKey);
  19730. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19731. if (ret != 0)
  19732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19733. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  19734. if (ret != 0)
  19735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19736. privSz = sizeof(priv);
  19737. pubSz = sizeof(pub);
  19738. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19739. #if defined(WOLFSSL_ASYNC_CRYPT)
  19740. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19741. #endif
  19742. if (ret != 0)
  19743. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19744. }
  19745. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  19746. exit_gen_test:
  19747. if (smallKey_inited)
  19748. wc_FreeDhKey(smallKey);
  19749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19750. if (smallKey != NULL)
  19751. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19752. #endif
  19753. return ret;
  19754. }
  19755. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19756. typedef struct dh_pubvalue_test {
  19757. const byte* data;
  19758. word32 len;
  19759. } dh_pubvalue_test;
  19760. static wc_test_ret_t dh_test_check_pubvalue(void)
  19761. {
  19762. wc_test_ret_t ret;
  19763. word32 i;
  19764. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  19765. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  19766. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  19767. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  19768. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  19769. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  19770. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  19771. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  19772. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  19773. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  19774. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  19775. const dh_pubvalue_test dh_pubval_fail[] = {
  19776. { prime, sizeof(prime) },
  19777. { pubValZero, sizeof(pubValZero) },
  19778. { pubValZeroLong, sizeof(pubValZeroLong) },
  19779. { pubValOne, sizeof(pubValOne) },
  19780. { pubValOneLong, sizeof(pubValOneLong) },
  19781. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  19782. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  19783. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  19784. { pubValTooBig0, sizeof(pubValTooBig0) },
  19785. { pubValTooBig1, sizeof(pubValTooBig1) },
  19786. { pubValTooLong, sizeof(pubValTooLong) },
  19787. };
  19788. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  19789. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  19790. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  19791. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  19792. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  19793. const dh_pubvalue_test dh_pubval_pass[] = {
  19794. { pubValTwo, sizeof(pubValTwo) },
  19795. { pubValTwoLong, sizeof(pubValTwoLong) },
  19796. { pubValGood, sizeof(pubValGood) },
  19797. { pubValGoodLen, sizeof(pubValGoodLen) },
  19798. { pubValGoodLong, sizeof(pubValGoodLong) },
  19799. };
  19800. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  19801. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  19802. dh_pubval_fail[i].len);
  19803. if (ret != MP_VAL)
  19804. return WC_TEST_RET_ENC_I(i);
  19805. }
  19806. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  19807. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  19808. dh_pubval_pass[i].len);
  19809. if (ret != 0)
  19810. return WC_TEST_RET_ENC_I(i);
  19811. }
  19812. return 0;
  19813. }
  19814. #endif
  19815. #if defined(HAVE_FFDHE)
  19816. #if defined(HAVE_FFDHE_4096)
  19817. #define MAX_DH_PRIV_SZ 39
  19818. #define MAX_DH_KEY_SZ 512
  19819. #elif defined(HAVE_FFDHE_3072)
  19820. #define MAX_DH_PRIV_SZ 34
  19821. #define MAX_DH_KEY_SZ 384
  19822. #else
  19823. #define MAX_DH_PRIV_SZ 29
  19824. #define MAX_DH_KEY_SZ 256
  19825. #endif
  19826. #ifndef WC_NO_RNG
  19827. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  19828. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  19829. #ifdef HAVE_PUBLIC_FFDHE
  19830. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  19831. #else
  19832. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  19833. #endif
  19834. {
  19835. wc_test_ret_t ret;
  19836. word32 privSz, pubSz, privSz2, pubSz2;
  19837. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19838. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19839. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19840. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19841. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19842. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19843. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19844. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19845. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19846. #else
  19847. byte priv[MAX_DH_PRIV_SZ];
  19848. byte pub[MAX_DH_KEY_SZ];
  19849. byte priv2[MAX_DH_PRIV_SZ];
  19850. byte pub2[MAX_DH_KEY_SZ];
  19851. byte agree[MAX_DH_KEY_SZ];
  19852. byte agree2[MAX_DH_KEY_SZ];
  19853. DhKey key[1];
  19854. DhKey key2[1];
  19855. #endif
  19856. word32 agreeSz = MAX_DH_KEY_SZ;
  19857. word32 agreeSz2 = MAX_DH_KEY_SZ;
  19858. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19859. if ((priv == NULL) ||
  19860. (pub == NULL) ||
  19861. (priv2 == NULL) ||
  19862. (pub2 == NULL) ||
  19863. (agree == NULL) ||
  19864. (agree2 == NULL) ||
  19865. (key == NULL) ||
  19866. (key2 == NULL))
  19867. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19868. #endif
  19869. pubSz = MAX_DH_KEY_SZ;
  19870. pubSz2 = MAX_DH_KEY_SZ;
  19871. #ifdef HAVE_PUBLIC_FFDHE
  19872. privSz = MAX_DH_PRIV_SZ;
  19873. privSz2 = MAX_DH_PRIV_SZ;
  19874. #else
  19875. privSz = wc_DhGetNamedKeyMinSize(name);
  19876. privSz2 = privSz;
  19877. #endif
  19878. XMEMSET(key, 0, sizeof(*key));
  19879. XMEMSET(key2, 0, sizeof(*key2));
  19880. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19881. if (ret != 0)
  19882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19883. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  19884. if (ret != 0)
  19885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19886. #ifdef HAVE_PUBLIC_FFDHE
  19887. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  19888. #else
  19889. ret = wc_DhSetNamedKey(key, name);
  19890. #endif
  19891. if (ret != 0)
  19892. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19893. #ifdef HAVE_PUBLIC_FFDHE
  19894. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  19895. params->g_len);
  19896. #else
  19897. ret = wc_DhSetNamedKey(key2, name);
  19898. #endif
  19899. if (ret != 0)
  19900. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19901. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19902. #if defined(WOLFSSL_ASYNC_CRYPT)
  19903. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19904. #endif
  19905. if (ret != 0)
  19906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19907. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  19908. #if defined(WOLFSSL_ASYNC_CRYPT)
  19909. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19910. #endif
  19911. if (ret != 0)
  19912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19913. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19914. #if defined(WOLFSSL_ASYNC_CRYPT)
  19915. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19916. #endif
  19917. if (ret != 0)
  19918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19919. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  19920. #if defined(WOLFSSL_ASYNC_CRYPT)
  19921. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19922. #endif
  19923. if (ret != 0)
  19924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19925. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  19926. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19927. }
  19928. #if (defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)) && \
  19929. !defined(HAVE_INTEL_QA)
  19930. /* Make p even */
  19931. key->p.dp[0] &= (mp_digit)-2;
  19932. if (ret != 0)
  19933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19934. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19935. #if defined(WOLFSSL_ASYNC_CRYPT)
  19936. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19937. #endif
  19938. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  19939. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19940. }
  19941. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19942. #if defined(WOLFSSL_ASYNC_CRYPT)
  19943. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19944. #endif
  19945. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  19946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19947. }
  19948. #ifndef HAVE_SELFTEST
  19949. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19950. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  19951. ret != ASYNC_OP_E) {
  19952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19953. }
  19954. #endif
  19955. /* Getting here means success - set ret to 0. */
  19956. ret = 0;
  19957. #endif /* (SP DH or Fast Math) and not Intel QuickAssist */
  19958. done:
  19959. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  19960. !defined(WC_NO_RNG)
  19961. if (priv)
  19962. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19963. if (pub)
  19964. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19965. if (priv2)
  19966. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19967. if (pub2)
  19968. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19969. if (agree)
  19970. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19971. if (agree2)
  19972. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19973. if (key) {
  19974. wc_FreeDhKey(key);
  19975. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19976. }
  19977. if (key2) {
  19978. wc_FreeDhKey(key2);
  19979. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19980. }
  19981. #else
  19982. wc_FreeDhKey(key);
  19983. wc_FreeDhKey(key2);
  19984. #endif
  19985. return ret;
  19986. }
  19987. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  19988. #endif /* !WC_NO_RNG */
  19989. #endif /* HAVE_FFDHE */
  19990. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  19991. {
  19992. wc_test_ret_t ret;
  19993. word32 bytes;
  19994. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  19995. #ifndef WC_NO_RNG
  19996. WC_RNG rng;
  19997. int rngInit = 0;
  19998. #endif
  19999. int keyInit = 0, key2Init = 0;
  20000. #define DH_TEST_TMP_SIZE 1024
  20001. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  20002. #define DH_TEST_BUF_SIZE 256
  20003. #else
  20004. #define DH_TEST_BUF_SIZE 512
  20005. #endif
  20006. #ifndef WC_NO_RNG
  20007. word32 agreeSz = DH_TEST_BUF_SIZE;
  20008. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  20009. #endif
  20010. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20011. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20012. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20013. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20014. #else
  20015. DhKey key[1];
  20016. DhKey key2[1];
  20017. byte tmp[DH_TEST_TMP_SIZE];
  20018. #endif
  20019. #ifndef WC_NO_RNG
  20020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20021. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20022. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20023. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20024. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20025. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20026. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20027. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  20028. agree == NULL || agree2 == NULL) {
  20029. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20030. }
  20031. #else
  20032. byte priv[DH_TEST_BUF_SIZE];
  20033. byte pub[DH_TEST_BUF_SIZE];
  20034. byte priv2[DH_TEST_BUF_SIZE];
  20035. byte pub2[DH_TEST_BUF_SIZE];
  20036. byte agree[DH_TEST_BUF_SIZE];
  20037. byte agree2[DH_TEST_BUF_SIZE];
  20038. #endif
  20039. #endif /* !WC_NO_RNG */
  20040. WOLFSSL_ENTER("dh_test");
  20041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20042. if (key == NULL || key2 == NULL || tmp == NULL) {
  20043. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20044. }
  20045. #endif
  20046. #ifdef USE_CERT_BUFFERS_1024
  20047. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  20048. bytes = (size_t)sizeof_dh_key_der_1024;
  20049. #elif defined(USE_CERT_BUFFERS_2048)
  20050. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  20051. bytes = (size_t)sizeof_dh_key_der_2048;
  20052. #elif defined(USE_CERT_BUFFERS_3072)
  20053. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  20054. bytes = (size_t)sizeof_dh_key_der_3072;
  20055. #elif defined(USE_CERT_BUFFERS_4096)
  20056. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  20057. bytes = (size_t)sizeof_dh_key_der_4096;
  20058. #elif defined(NO_ASN)
  20059. /* don't use file, no DER parsing */
  20060. #elif !defined(NO_FILESYSTEM)
  20061. {
  20062. XFILE file = XFOPEN(dhParamsFile, "rb");
  20063. if (! file)
  20064. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20065. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20066. XFCLOSE(file);
  20067. if (bytes == 0)
  20068. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20069. }
  20070. #else
  20071. /* No DH key to use. */
  20072. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20073. #endif /* USE_CERT_BUFFERS */
  20074. (void)idx;
  20075. (void)tmp;
  20076. (void)bytes;
  20077. pubSz = DH_TEST_BUF_SIZE;
  20078. pubSz2 = DH_TEST_BUF_SIZE;
  20079. privSz = DH_TEST_BUF_SIZE;
  20080. privSz2 = DH_TEST_BUF_SIZE;
  20081. #ifndef WC_NO_RNG
  20082. XMEMSET(&rng, 0, sizeof(rng));
  20083. #endif
  20084. /* Use API for coverage. */
  20085. ret = wc_InitDhKey(key);
  20086. if (ret != 0)
  20087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20088. wc_FreeDhKey(key);
  20089. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20090. if (ret != 0)
  20091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20092. keyInit = 1;
  20093. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  20094. if (ret != 0)
  20095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20096. key2Init = 1;
  20097. #ifdef NO_ASN
  20098. #ifndef WOLFSSL_SP_MATH
  20099. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20100. if (ret != 0)
  20101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20102. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20103. if (ret != 0)
  20104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20105. #else
  20106. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20107. sizeof(dh2048_g));
  20108. if (ret != 0)
  20109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20110. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20111. sizeof(dh2048_g));
  20112. if (ret != 0)
  20113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20114. #endif
  20115. #else
  20116. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20117. if (ret != 0)
  20118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20119. idx = 0;
  20120. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  20121. if (ret != 0)
  20122. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20123. #endif
  20124. #ifndef WC_NO_RNG
  20125. #ifndef HAVE_FIPS
  20126. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20127. #else
  20128. ret = wc_InitRng(&rng);
  20129. #endif
  20130. if (ret != 0)
  20131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20132. rngInit = 1;
  20133. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  20134. #if defined(WOLFSSL_ASYNC_CRYPT)
  20135. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20136. #endif
  20137. if (ret != 0)
  20138. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20139. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  20140. #if defined(WOLFSSL_ASYNC_CRYPT)
  20141. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20142. #endif
  20143. if (ret != 0)
  20144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20145. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20146. #if defined(WOLFSSL_ASYNC_CRYPT)
  20147. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20148. #endif
  20149. if (ret != 0)
  20150. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20151. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  20152. #if defined(WOLFSSL_ASYNC_CRYPT)
  20153. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20154. #endif
  20155. if (ret != 0)
  20156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20157. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  20158. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20159. }
  20160. #endif /* !WC_NO_RNG */
  20161. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20162. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  20163. if (ret != BAD_FUNC_ARG)
  20164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20165. ret = wc_DhCheckPrivKey(key, priv, privSz);
  20166. if (ret != 0)
  20167. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20168. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  20169. if (ret != BAD_FUNC_ARG)
  20170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20171. {
  20172. word32 pSz, qSz, gSz;
  20173. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  20174. if (ret != LENGTH_ONLY_E)
  20175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20176. }
  20177. #endif
  20178. /* Test DH key import / export */
  20179. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  20180. (!defined(HAVE_FIPS) || \
  20181. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  20182. wc_FreeDhKey(key);
  20183. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20184. if (ret != 0)
  20185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20186. #ifndef NO_ASN
  20187. {
  20188. /* DH Private - Key Export / Import */
  20189. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20190. byte *tmp2;
  20191. #else
  20192. byte tmp2[DH_TEST_TMP_SIZE];
  20193. #endif
  20194. #if defined(USE_CERT_BUFFERS_2048)
  20195. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  20196. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  20197. #else
  20198. XFILE file = XFOPEN(dhKeyFile, "rb");
  20199. if (!file)
  20200. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20201. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20202. XFCLOSE(file);
  20203. if (bytes == 0)
  20204. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20205. #endif
  20206. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20207. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20208. if (tmp2 == NULL)
  20209. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20210. #endif
  20211. idx = 0;
  20212. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20213. /* Import DH Private key as DER */
  20214. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20215. if (ret == 0) {
  20216. /* Export as DER */
  20217. idx = DH_TEST_TMP_SIZE;
  20218. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  20219. }
  20220. /* Verify export matches original */
  20221. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20222. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20223. }
  20224. /* DH Public Key - Export / Import */
  20225. #if defined(USE_CERT_BUFFERS_2048)
  20226. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  20227. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  20228. #else
  20229. file = XFOPEN(dhKeyPubFile, "rb");
  20230. if (!file)
  20231. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20232. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20233. XFCLOSE(file);
  20234. if (bytes == 0)
  20235. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20236. #endif
  20237. /* for HAVE_WOLF_BIGINT prevent leak */
  20238. wc_FreeDhKey(key);
  20239. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20240. idx = 0;
  20241. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20242. /* Import DH Public key as DER */
  20243. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20244. if (ret == 0) {
  20245. /* Export as DER */
  20246. idx = DH_TEST_TMP_SIZE;
  20247. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  20248. }
  20249. /* Verify export matches original */
  20250. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20251. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20252. }
  20253. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20254. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20255. #endif
  20256. }
  20257. #else
  20258. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20259. if (ret != 0)
  20260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20261. #endif /* !NO_ASN */
  20262. privSz = DH_TEST_BUF_SIZE;
  20263. pubSz = DH_TEST_BUF_SIZE;
  20264. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  20265. if (ret != 0)
  20266. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20267. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  20268. if (ret != 0)
  20269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20270. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  20271. #ifndef WC_NO_RNG
  20272. ret = dh_generate_test(&rng);
  20273. if (ret != 0)
  20274. ERROR_OUT(ret, done);
  20275. ret = dh_fips_generate_test(&rng);
  20276. if (ret != 0)
  20277. ERROR_OUT(ret, done);
  20278. #endif /* !WC_NO_RNG */
  20279. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20280. ret = dh_test_check_pubvalue();
  20281. if (ret != 0)
  20282. ERROR_OUT(ret, done);
  20283. #endif
  20284. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  20285. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  20286. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  20287. * this will be available for testing in the 140-3 module */
  20288. #ifndef WC_NO_RNG
  20289. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  20290. #ifdef HAVE_FFDHE_2048
  20291. #ifdef HAVE_PUBLIC_FFDHE
  20292. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  20293. #else
  20294. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  20295. #endif
  20296. if (ret != 0)
  20297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20298. #endif
  20299. #ifdef HAVE_FFDHE_3072
  20300. #ifdef HAVE_PUBLIC_FFDHE
  20301. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  20302. #else
  20303. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  20304. #endif
  20305. if (ret != 0)
  20306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20307. #endif
  20308. #ifdef HAVE_FFDHE_4096
  20309. #ifdef HAVE_PUBLIC_FFDHE
  20310. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  20311. #else
  20312. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  20313. #endif
  20314. if (ret != 0)
  20315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20316. #endif
  20317. #endif /* !WC_NO_RNG */
  20318. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  20319. wc_FreeDhKey(key);
  20320. keyInit = 0;
  20321. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  20322. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  20323. /* Test Check Key */
  20324. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  20325. NULL, 0, 0, &rng);
  20326. if (ret != 0)
  20327. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20328. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  20329. #endif
  20330. done:
  20331. #ifndef WC_NO_RNG
  20332. if (rngInit)
  20333. wc_FreeRng(&rng);
  20334. #endif
  20335. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20336. if (key) {
  20337. if (keyInit)
  20338. wc_FreeDhKey(key);
  20339. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20340. }
  20341. if (key2) {
  20342. if (key2Init)
  20343. wc_FreeDhKey(key2);
  20344. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20345. }
  20346. if (tmp)
  20347. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20348. if (priv)
  20349. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20350. if (pub)
  20351. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20352. if (priv2)
  20353. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20354. if (pub2)
  20355. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20356. if (agree)
  20357. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20358. if (agree2)
  20359. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20360. #else
  20361. if (keyInit)
  20362. wc_FreeDhKey(key);
  20363. if (key2Init)
  20364. wc_FreeDhKey(key2);
  20365. #endif
  20366. (void)privSz;
  20367. (void)pubSz;
  20368. (void)pubSz2;
  20369. (void)privSz2;
  20370. return ret;
  20371. #undef DH_TEST_BUF_SIZE
  20372. #undef DH_TEST_TMP_SIZE
  20373. }
  20374. #endif /* NO_DH */
  20375. #ifndef NO_DSA
  20376. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  20377. {
  20378. wc_test_ret_t ret = 0;
  20379. int answer;
  20380. word32 bytes;
  20381. word32 idx = 0;
  20382. WC_RNG rng;
  20383. int rng_inited = 0;
  20384. wc_Sha sha;
  20385. byte hash[WC_SHA_DIGEST_SIZE];
  20386. byte signature[40];
  20387. int key_inited = 0;
  20388. #ifdef WOLFSSL_KEY_GEN
  20389. byte* der = 0;
  20390. int derIn_inited = 0;
  20391. int genKey_inited = 0;
  20392. #endif
  20393. #define DSA_TEST_TMP_SIZE 1024
  20394. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20395. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20396. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20397. #ifdef WOLFSSL_KEY_GEN
  20398. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20399. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20400. #endif
  20401. WOLFSSL_ENTER("dsa_test");
  20402. if ((tmp == NULL) ||
  20403. (key == NULL)
  20404. #ifdef WOLFSSL_KEY_GEN
  20405. || (derIn == NULL)
  20406. || (genKey == NULL)
  20407. #endif
  20408. ) {
  20409. ret = WC_TEST_RET_ENC_NC;
  20410. goto out;
  20411. }
  20412. #else
  20413. byte tmp[1024];
  20414. DsaKey key[1];
  20415. #ifdef WOLFSSL_KEY_GEN
  20416. DsaKey derIn[1];
  20417. DsaKey genKey[1];
  20418. #endif
  20419. #endif
  20420. #ifdef USE_CERT_BUFFERS_1024
  20421. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20422. bytes = sizeof_dsa_key_der_1024;
  20423. #elif defined(USE_CERT_BUFFERS_2048)
  20424. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20425. bytes = sizeof_dsa_key_der_2048;
  20426. #else
  20427. {
  20428. XFILE file = XFOPEN(dsaKey, "rb");
  20429. if (!file)
  20430. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20431. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  20432. XFCLOSE(file);
  20433. if (bytes == 0)
  20434. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20435. }
  20436. #endif /* USE_CERT_BUFFERS */
  20437. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  20438. if (ret != 0)
  20439. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20440. wc_ShaUpdate(&sha, tmp, bytes);
  20441. wc_ShaFinal(&sha, hash);
  20442. wc_ShaFree(&sha);
  20443. ret = wc_InitDsaKey(key);
  20444. if (ret != 0)
  20445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20446. key_inited = 1;
  20447. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  20448. if (ret != 0)
  20449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20450. #ifndef HAVE_FIPS
  20451. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20452. #else
  20453. ret = wc_InitRng(&rng);
  20454. #endif
  20455. if (ret != 0)
  20456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20457. rng_inited = 1;
  20458. ret = wc_DsaSign(hash, signature, key, &rng);
  20459. if (ret != 0)
  20460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20461. ret = wc_DsaVerify(hash, signature, key, &answer);
  20462. if (ret != 0)
  20463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20464. if (answer != 1)
  20465. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20466. wc_FreeDsaKey(key);
  20467. key_inited = 0;
  20468. ret = wc_InitDsaKey_h(key, NULL);
  20469. if (ret != 0)
  20470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20471. key_inited = 1;
  20472. #ifdef WOLFSSL_KEY_GEN
  20473. {
  20474. int derSz = 0;
  20475. ret = wc_InitDsaKey(genKey);
  20476. if (ret != 0)
  20477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20478. genKey_inited = 1;
  20479. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  20480. if (ret != 0)
  20481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20482. ret = wc_MakeDsaKey(&rng, genKey);
  20483. if (ret != 0)
  20484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20485. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20486. if (der == NULL)
  20487. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20488. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  20489. if (derSz < 0)
  20490. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  20491. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  20492. DSA_PRIVATEKEY_TYPE);
  20493. if (ret != 0)
  20494. goto out;
  20495. ret = wc_InitDsaKey(derIn);
  20496. if (ret != 0)
  20497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20498. derIn_inited = 1;
  20499. idx = 0;
  20500. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, (word32)derSz);
  20501. if (ret != 0)
  20502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20503. }
  20504. #endif /* WOLFSSL_KEY_GEN */
  20505. out:
  20506. #ifdef WOLFSSL_KEY_GEN
  20507. if (der)
  20508. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20509. #endif
  20510. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20511. if (tmp)
  20512. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20513. if (key) {
  20514. if (key_inited)
  20515. wc_FreeDsaKey(key);
  20516. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20517. }
  20518. #ifdef WOLFSSL_KEY_GEN
  20519. if (derIn) {
  20520. if (derIn_inited)
  20521. wc_FreeDsaKey(derIn);
  20522. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20523. }
  20524. if (genKey) {
  20525. if (genKey_inited)
  20526. wc_FreeDsaKey(genKey);
  20527. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20528. }
  20529. #endif
  20530. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  20531. if (key_inited)
  20532. wc_FreeDsaKey(key);
  20533. #ifdef WOLFSSL_KEY_GEN
  20534. if (derIn_inited)
  20535. wc_FreeDsaKey(derIn);
  20536. if (genKey_inited)
  20537. wc_FreeDsaKey(genKey);
  20538. #endif
  20539. #endif
  20540. if (rng_inited)
  20541. wc_FreeRng(&rng);
  20542. return ret;
  20543. }
  20544. #endif /* NO_DSA */
  20545. #ifdef WOLFCRYPT_HAVE_SRP
  20546. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  20547. {
  20548. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  20549. WC_RNG rng;
  20550. if(NULL == buf || !size)
  20551. return WC_TEST_RET_ENC_NC;
  20552. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  20553. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  20554. wc_FreeRng(&rng);
  20555. }
  20556. return ret;
  20557. }
  20558. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  20559. {
  20560. wc_test_ret_t r;
  20561. byte clientPubKey[192]; /* A */
  20562. byte serverPubKey[192]; /* B */
  20563. word32 clientPubKeySz = 192;
  20564. word32 serverPubKeySz = 192;
  20565. byte username[] = "user";
  20566. word32 usernameSz = 4;
  20567. byte password[] = "password";
  20568. word32 passwordSz = 8;
  20569. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  20570. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  20571. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  20572. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  20573. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  20574. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  20575. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  20576. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  20577. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  20578. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  20579. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  20580. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  20581. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  20582. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  20583. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  20584. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  20585. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  20586. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  20587. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  20588. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  20589. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  20590. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  20591. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  20592. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  20593. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  20594. };
  20595. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  20596. 0x02
  20597. };
  20598. byte salt[10];
  20599. byte verifier[192];
  20600. word32 v_size = sizeof(verifier);
  20601. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  20602. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  20603. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20604. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20605. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20606. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20607. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  20608. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20609. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  20610. if ((cli == NULL) ||
  20611. (srv == NULL) ||
  20612. (clientProof == NULL) ||
  20613. (serverProof == NULL)) {
  20614. r = WC_TEST_RET_ENC_NC;
  20615. goto out;
  20616. }
  20617. #else
  20618. Srp cli[1], srv[1];
  20619. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  20620. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  20621. #endif
  20622. /* set as 0's so if second init on srv not called SrpTerm is not on
  20623. * garbage values */
  20624. XMEMSET(srv, 0, sizeof *srv);
  20625. XMEMSET(cli, 0, sizeof *cli);
  20626. /* generating random salt */
  20627. r = generate_random_salt(salt, sizeof(salt));
  20628. /* client knows username and password. */
  20629. /* server knows N, g, salt and verifier. */
  20630. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  20631. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  20632. /* loading N, g and salt in advance to generate the verifier. */
  20633. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  20634. g, sizeof(g),
  20635. salt, sizeof(salt));
  20636. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  20637. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  20638. /* client sends username to server */
  20639. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  20640. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  20641. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  20642. g, sizeof(g),
  20643. salt, sizeof(salt));
  20644. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  20645. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  20646. /* server sends N, g, salt and B to client */
  20647. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  20648. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  20649. serverPubKey, serverPubKeySz);
  20650. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  20651. /* client sends A and M1 to server */
  20652. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  20653. serverPubKey, serverPubKeySz);
  20654. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  20655. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  20656. /* server sends M2 to client */
  20657. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  20658. wc_SrpTerm(cli);
  20659. wc_SrpTerm(srv);
  20660. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20661. out:
  20662. if (cli)
  20663. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20664. if (srv)
  20665. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20666. if (clientProof)
  20667. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20668. if (serverProof)
  20669. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20670. #endif
  20671. return r;
  20672. }
  20673. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  20674. {
  20675. wc_test_ret_t ret;
  20676. WOLFSSL_ENTER("srp_test");
  20677. #ifndef NO_SHA
  20678. ret = srp_test_digest(SRP_TYPE_SHA);
  20679. if (ret != 0)
  20680. return ret;
  20681. #endif
  20682. #ifndef NO_SHA256
  20683. ret = srp_test_digest(SRP_TYPE_SHA256);
  20684. if (ret != 0)
  20685. return ret;
  20686. #endif
  20687. #ifdef WOLFSSL_SHA384
  20688. ret = srp_test_digest(SRP_TYPE_SHA384);
  20689. if (ret != 0)
  20690. return ret;
  20691. #endif
  20692. #ifdef WOLFSSL_SHA512
  20693. ret = srp_test_digest(SRP_TYPE_SHA512);
  20694. if (ret != 0)
  20695. return ret;
  20696. #endif
  20697. return ret;
  20698. }
  20699. #endif /* WOLFCRYPT_HAVE_SRP */
  20700. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  20701. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  20702. static wc_test_ret_t openssl_aes_test(void)
  20703. {
  20704. #ifdef HAVE_AES_CBC
  20705. #ifdef WOLFSSL_AES_128
  20706. {
  20707. /* EVP_CipherUpdate test */
  20708. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20709. {
  20710. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20711. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20712. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20713. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20714. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20715. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20716. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20717. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20718. };
  20719. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20720. "0123456789abcdef "; /* align */
  20721. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20722. "1234567890abcdef "; /* align */
  20723. byte cipher[AES_BLOCK_SIZE * 4];
  20724. byte plain [AES_BLOCK_SIZE * 4];
  20725. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20726. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20727. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20728. #else
  20729. EVP_CIPHER_CTX en[1];
  20730. EVP_CIPHER_CTX de[1];
  20731. #endif
  20732. int outlen ;
  20733. int total = 0;
  20734. int i;
  20735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20736. if ((en == NULL) || (de == NULL))
  20737. return MEMORY_E;
  20738. #endif
  20739. EVP_CIPHER_CTX_init(en);
  20740. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20741. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20742. return WC_TEST_RET_ENC_NC;
  20743. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20744. (byte*)cbcPlain, 9) == 0)
  20745. return WC_TEST_RET_ENC_NC;
  20746. if (outlen != 0)
  20747. return WC_TEST_RET_ENC_NC;
  20748. total += outlen;
  20749. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20750. (byte*)&cbcPlain[9] , 9) == 0)
  20751. return WC_TEST_RET_ENC_NC;
  20752. if (outlen != 16)
  20753. return WC_TEST_RET_ENC_NC;
  20754. total += outlen;
  20755. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20756. return WC_TEST_RET_ENC_NC;
  20757. if (outlen != 16)
  20758. return WC_TEST_RET_ENC_NC;
  20759. total += outlen;
  20760. if (total != 32)
  20761. return 3408;
  20762. EVP_CIPHER_CTX_cleanup(en);
  20763. total = 0;
  20764. EVP_CIPHER_CTX_init(de);
  20765. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20766. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20767. return WC_TEST_RET_ENC_NC;
  20768. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20769. return WC_TEST_RET_ENC_NC;
  20770. if (outlen != 0)
  20771. return WC_TEST_RET_ENC_NC;
  20772. total += outlen;
  20773. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20774. (byte*)&cipher[6], 12) == 0)
  20775. return WC_TEST_RET_ENC_NC;
  20776. if (outlen != 0)
  20777. total += outlen;
  20778. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20779. (byte*)&cipher[6+12], 14) == 0)
  20780. return WC_TEST_RET_ENC_NC;
  20781. if (outlen != 16)
  20782. return WC_TEST_RET_ENC_NC;
  20783. total += outlen;
  20784. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20785. return WC_TEST_RET_ENC_NC;
  20786. if (outlen != 2)
  20787. return WC_TEST_RET_ENC_NC;
  20788. total += outlen;
  20789. if (total != 18)
  20790. return 3427;
  20791. if (XMEMCMP(plain, cbcPlain, 18))
  20792. return WC_TEST_RET_ENC_NC;
  20793. EVP_CIPHER_CTX_cleanup(de);
  20794. /* test with encrypting/decrypting more than 16 bytes at once */
  20795. total = 0;
  20796. EVP_CIPHER_CTX_init(en);
  20797. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20798. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20799. return WC_TEST_RET_ENC_NC;
  20800. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20801. (byte*)cbcPlain, 17) == 0)
  20802. return WC_TEST_RET_ENC_NC;
  20803. if (outlen != 16)
  20804. return WC_TEST_RET_ENC_NC;
  20805. total += outlen;
  20806. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20807. (byte*)&cbcPlain[17] , 1) == 0)
  20808. return WC_TEST_RET_ENC_NC;
  20809. if (outlen != 0)
  20810. return WC_TEST_RET_ENC_NC;
  20811. total += outlen;
  20812. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20813. return WC_TEST_RET_ENC_NC;
  20814. if (outlen != 16)
  20815. return WC_TEST_RET_ENC_NC;
  20816. total += outlen;
  20817. if (total != 32)
  20818. return WC_TEST_RET_ENC_NC;
  20819. EVP_CIPHER_CTX_cleanup(en);
  20820. total = 0;
  20821. EVP_CIPHER_CTX_init(de);
  20822. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20823. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20824. return WC_TEST_RET_ENC_NC;
  20825. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  20826. return WC_TEST_RET_ENC_NC;
  20827. if (outlen != 16)
  20828. return WC_TEST_RET_ENC_NC;
  20829. total += outlen;
  20830. /* final call on non block size should fail */
  20831. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  20832. return WC_TEST_RET_ENC_NC;
  20833. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20834. (byte*)&cipher[17], 1) == 0)
  20835. return WC_TEST_RET_ENC_NC;
  20836. if (outlen != 0)
  20837. total += outlen;
  20838. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20839. (byte*)&cipher[17+1], 14) == 0)
  20840. return WC_TEST_RET_ENC_NC;
  20841. if (outlen != 0)
  20842. return WC_TEST_RET_ENC_NC;
  20843. total += outlen;
  20844. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20845. return WC_TEST_RET_ENC_NC;
  20846. if (outlen != 2)
  20847. return WC_TEST_RET_ENC_NC;
  20848. total += outlen;
  20849. if (total != 18)
  20850. return WC_TEST_RET_ENC_NC;
  20851. if (XMEMCMP(plain, cbcPlain, 18))
  20852. return WC_TEST_RET_ENC_NC;
  20853. /* test byte by byte decrypt */
  20854. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20855. plain[i] = i;
  20856. }
  20857. EVP_CIPHER_CTX_cleanup(de);
  20858. total = 0;
  20859. EVP_CIPHER_CTX_init(en);
  20860. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20861. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20862. return WC_TEST_RET_ENC_NC;
  20863. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20864. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  20865. return WC_TEST_RET_ENC_NC;
  20866. if (outlen != AES_BLOCK_SIZE * 3)
  20867. return WC_TEST_RET_ENC_NC;
  20868. total += outlen;
  20869. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20870. return WC_TEST_RET_ENC_NC;
  20871. if (outlen != AES_BLOCK_SIZE)
  20872. return WC_TEST_RET_ENC_NC;
  20873. total += outlen;
  20874. if (total != sizeof(plain))
  20875. return WC_TEST_RET_ENC_NC;
  20876. EVP_CIPHER_CTX_cleanup(en);
  20877. total = 0;
  20878. EVP_CIPHER_CTX_init(de);
  20879. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20880. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20881. return WC_TEST_RET_ENC_NC;
  20882. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  20883. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  20884. (byte*)cipher + i, 1) == 0)
  20885. return WC_TEST_RET_ENC_NC;
  20886. if (outlen > 0) {
  20887. int j;
  20888. total += outlen;
  20889. for (j = 0; j < total; j++) {
  20890. if (plain[j] != j) {
  20891. return WC_TEST_RET_ENC_NC;
  20892. }
  20893. }
  20894. }
  20895. }
  20896. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20897. return WC_TEST_RET_ENC_NC;
  20898. total += outlen;
  20899. if (total != AES_BLOCK_SIZE * 3) {
  20900. return WC_TEST_RET_ENC_NC;
  20901. }
  20902. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20903. if (plain[i] != i) {
  20904. return WC_TEST_RET_ENC_NC;
  20905. }
  20906. }
  20907. EVP_CIPHER_CTX_cleanup(de);
  20908. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20909. wolfSSL_EVP_CIPHER_CTX_free(en);
  20910. wolfSSL_EVP_CIPHER_CTX_free(de);
  20911. #endif
  20912. }
  20913. /* set buffers to be exact size to catch potential over read/write */
  20914. {
  20915. /* EVP_CipherUpdate test */
  20916. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20917. {
  20918. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20919. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20920. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20921. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20922. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20923. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20924. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20925. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20926. };
  20927. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20928. "0123456789abcdef "; /* align */
  20929. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20930. "1234567890abcdef "; /* align */
  20931. #define EVP_TEST_BUF_SZ 18
  20932. #define EVP_TEST_BUF_PAD 32
  20933. byte cipher[EVP_TEST_BUF_SZ];
  20934. byte plain [EVP_TEST_BUF_SZ];
  20935. byte padded[EVP_TEST_BUF_PAD];
  20936. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20937. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20938. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20939. #else
  20940. EVP_CIPHER_CTX en[1];
  20941. EVP_CIPHER_CTX de[1];
  20942. #endif
  20943. int outlen ;
  20944. int total = 0;
  20945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20946. if ((en == NULL) || (de == NULL))
  20947. return MEMORY_E;
  20948. #endif
  20949. EVP_CIPHER_CTX_init(en);
  20950. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20951. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20952. return WC_TEST_RET_ENC_NC;
  20953. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  20954. return WC_TEST_RET_ENC_NC;
  20955. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20956. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  20957. return WC_TEST_RET_ENC_NC;
  20958. if (outlen != 16)
  20959. return WC_TEST_RET_ENC_NC;
  20960. total += outlen;
  20961. /* should fail here */
  20962. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  20963. return WC_TEST_RET_ENC_NC;
  20964. EVP_CIPHER_CTX_cleanup(en);
  20965. /* turn padding back on and do successful encrypt */
  20966. total = 0;
  20967. EVP_CIPHER_CTX_init(en);
  20968. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20969. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20970. return WC_TEST_RET_ENC_NC;
  20971. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  20972. return WC_TEST_RET_ENC_NC;
  20973. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  20974. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  20975. return WC_TEST_RET_ENC_NC;
  20976. if (outlen != 16)
  20977. return WC_TEST_RET_ENC_NC;
  20978. total += outlen;
  20979. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  20980. return WC_TEST_RET_ENC_NC;
  20981. total += outlen;
  20982. if (total != 32)
  20983. return WC_TEST_RET_ENC_NC;
  20984. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  20985. EVP_CIPHER_CTX_cleanup(en);
  20986. /* test out of bounds read on buffers w/o padding during decryption */
  20987. total = 0;
  20988. EVP_CIPHER_CTX_init(de);
  20989. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20990. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20991. return WC_TEST_RET_ENC_NC;
  20992. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  20993. return WC_TEST_RET_ENC_NC;
  20994. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  20995. EVP_TEST_BUF_SZ) == 0)
  20996. return WC_TEST_RET_ENC_NC;
  20997. if (outlen != 16)
  20998. return WC_TEST_RET_ENC_NC;
  20999. total += outlen;
  21000. /* should fail since not using padding */
  21001. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  21002. return WC_TEST_RET_ENC_NC;
  21003. EVP_CIPHER_CTX_cleanup(de);
  21004. total = 0;
  21005. EVP_CIPHER_CTX_init(de);
  21006. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21007. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21008. return WC_TEST_RET_ENC_NC;
  21009. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  21010. return WC_TEST_RET_ENC_NC;
  21011. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  21012. EVP_TEST_BUF_PAD) == 0)
  21013. return WC_TEST_RET_ENC_NC;
  21014. if (outlen != 16)
  21015. return WC_TEST_RET_ENC_NC;
  21016. total += outlen;
  21017. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  21018. return WC_TEST_RET_ENC_NC;
  21019. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  21020. return WC_TEST_RET_ENC_NC;
  21021. EVP_CIPHER_CTX_cleanup(de);
  21022. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21023. wolfSSL_EVP_CIPHER_CTX_free(en);
  21024. wolfSSL_EVP_CIPHER_CTX_free(de);
  21025. #endif
  21026. }
  21027. { /* evp_cipher test: EVP_aes_128_cbc */
  21028. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21029. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21030. #else
  21031. EVP_CIPHER_CTX ctx[1];
  21032. #endif
  21033. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21034. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21035. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21036. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21037. };
  21038. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21039. {
  21040. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21041. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  21042. };
  21043. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21044. "0123456789abcdef "; /* align */
  21045. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21046. "1234567890abcdef "; /* align */
  21047. byte cipher[AES_BLOCK_SIZE * 4];
  21048. byte plain [AES_BLOCK_SIZE * 4];
  21049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21050. if (ctx == NULL)
  21051. return MEMORY_E;
  21052. #endif
  21053. EVP_CIPHER_CTX_init(ctx);
  21054. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  21055. return WC_TEST_RET_ENC_NC;
  21056. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21057. return WC_TEST_RET_ENC_NC;
  21058. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21059. return WC_TEST_RET_ENC_NC;
  21060. EVP_CIPHER_CTX_cleanup(ctx);
  21061. EVP_CIPHER_CTX_init(ctx);
  21062. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  21063. return WC_TEST_RET_ENC_NC;
  21064. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21065. return WC_TEST_RET_ENC_NC;
  21066. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21067. return WC_TEST_RET_ENC_NC;
  21068. EVP_CIPHER_CTX_cleanup(ctx);
  21069. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21070. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21071. #endif
  21072. } /* end evp_cipher test: EVP_aes_128_cbc*/
  21073. #endif /* WOLFSSL_AES_128 */
  21074. #endif /* HAVE_AES_CBC */
  21075. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21076. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  21077. { /* evp_cipher test: EVP_aes_256_ecb*/
  21078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21079. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21080. #else
  21081. EVP_CIPHER_CTX ctx[1];
  21082. #endif
  21083. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21084. {
  21085. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21086. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21087. };
  21088. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21089. {
  21090. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21091. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21092. };
  21093. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21094. {
  21095. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21096. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21097. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21098. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21099. };
  21100. byte cipher[AES_BLOCK_SIZE * 4];
  21101. byte plain [AES_BLOCK_SIZE * 4];
  21102. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21103. if (ctx == NULL)
  21104. return MEMORY_E;
  21105. #endif
  21106. EVP_CIPHER_CTX_init(ctx);
  21107. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  21108. return WC_TEST_RET_ENC_NC;
  21109. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21110. return WC_TEST_RET_ENC_NC;
  21111. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21112. return WC_TEST_RET_ENC_NC;
  21113. EVP_CIPHER_CTX_init(ctx);
  21114. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  21115. return WC_TEST_RET_ENC_NC;
  21116. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21117. return WC_TEST_RET_ENC_NC;
  21118. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21119. return WC_TEST_RET_ENC_NC;
  21120. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21121. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21122. #endif
  21123. } /* end evp_cipher test */
  21124. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  21125. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  21126. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  21127. {
  21128. /* Test: AES_encrypt/decrypt/set Key */
  21129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21130. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21131. #ifdef HAVE_AES_DECRYPT
  21132. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21133. #endif
  21134. #else
  21135. AES_KEY enc[1];
  21136. #ifdef HAVE_AES_DECRYPT
  21137. AES_KEY dec[1];
  21138. #endif
  21139. #endif
  21140. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21141. {
  21142. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21143. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21144. };
  21145. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21146. {
  21147. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21148. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21149. };
  21150. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21151. {
  21152. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21153. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21154. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21155. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21156. };
  21157. byte plain[sizeof(msg)];
  21158. byte cipher[sizeof(msg)];
  21159. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21160. if (enc == NULL)
  21161. return MEMORY_E;
  21162. #ifdef HAVE_AES_DECRYPT
  21163. if (dec == NULL)
  21164. return MEMORY_E;
  21165. #endif
  21166. #endif
  21167. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  21168. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  21169. AES_encrypt(msg, cipher, enc);
  21170. #ifdef HAVE_AES_DECRYPT
  21171. AES_decrypt(cipher, plain, dec);
  21172. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21173. return WC_TEST_RET_ENC_NC;
  21174. #endif /* HAVE_AES_DECRYPT */
  21175. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21176. return WC_TEST_RET_ENC_NC;
  21177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21178. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21179. #ifdef HAVE_AES_DECRYPT
  21180. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21181. #endif
  21182. #endif
  21183. }
  21184. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  21185. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21186. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  21187. #ifdef WOLFSSL_AES_COUNTER
  21188. {
  21189. byte plainBuff [64];
  21190. byte cipherBuff[64];
  21191. #ifdef WOLFSSL_AES_128
  21192. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  21193. {
  21194. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21195. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21196. };
  21197. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  21198. {
  21199. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21200. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21201. };
  21202. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  21203. {
  21204. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21205. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21206. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21207. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  21208. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  21209. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  21210. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  21211. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  21212. };
  21213. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  21214. {
  21215. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  21216. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  21217. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  21218. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  21219. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  21220. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  21221. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  21222. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  21223. };
  21224. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  21225. {
  21226. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  21227. 0xc2
  21228. };
  21229. #endif
  21230. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21231. * NIST Special Publication 800-38A */
  21232. #ifdef WOLFSSL_AES_192
  21233. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  21234. {
  21235. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  21236. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  21237. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  21238. };
  21239. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  21240. {
  21241. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21242. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21243. };
  21244. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  21245. {
  21246. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21247. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21248. };
  21249. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  21250. {
  21251. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  21252. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  21253. };
  21254. #endif /* WOLFSSL_AES_192 */
  21255. #ifdef WOLFSSL_AES_256
  21256. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21257. * NIST Special Publication 800-38A */
  21258. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  21259. {
  21260. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21261. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21262. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21263. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21264. };
  21265. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  21266. {
  21267. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21268. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21269. };
  21270. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  21271. {
  21272. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21273. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21274. };
  21275. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  21276. {
  21277. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  21278. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  21279. };
  21280. #endif /* WOLFSSL_AES_256 */
  21281. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21282. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  21283. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  21284. #else
  21285. EVP_CIPHER_CTX en[1];
  21286. EVP_CIPHER_CTX de[1];
  21287. #endif
  21288. #ifdef WOLFSSL_AES_128
  21289. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21290. EVP_CIPHER_CTX *p_en;
  21291. EVP_CIPHER_CTX *p_de;
  21292. #endif
  21293. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21294. if ((en == NULL) || (de == NULL))
  21295. return MEMORY_E;
  21296. #endif
  21297. EVP_CIPHER_CTX_init(en);
  21298. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21299. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21300. return WC_TEST_RET_ENC_NC;
  21301. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  21302. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21303. return WC_TEST_RET_ENC_NC;
  21304. EVP_CIPHER_CTX_init(de);
  21305. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21306. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21307. return WC_TEST_RET_ENC_NC;
  21308. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21309. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21310. return WC_TEST_RET_ENC_NC;
  21311. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21312. return WC_TEST_RET_ENC_NC;
  21313. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21314. return WC_TEST_RET_ENC_NC;
  21315. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21316. return WC_TEST_RET_ENC_NC;
  21317. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21318. return WC_TEST_RET_ENC_NC;
  21319. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21320. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  21321. if (p_en == NULL)
  21322. return WC_TEST_RET_ENC_ERRNO;
  21323. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  21324. if (p_de == NULL)
  21325. return WC_TEST_RET_ENC_ERRNO;
  21326. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  21327. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21328. return WC_TEST_RET_ENC_NC;
  21329. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  21330. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21331. return WC_TEST_RET_ENC_NC;
  21332. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  21333. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21334. return WC_TEST_RET_ENC_NC;
  21335. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  21336. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21337. return WC_TEST_RET_ENC_NC;
  21338. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21339. return WC_TEST_RET_ENC_NC;
  21340. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21341. return WC_TEST_RET_ENC_NC;
  21342. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  21343. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  21344. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21345. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21346. return WC_TEST_RET_ENC_NC;
  21347. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21348. return WC_TEST_RET_ENC_NC;
  21349. EVP_CIPHER_CTX_init(en);
  21350. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21351. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21352. return WC_TEST_RET_ENC_NC;
  21353. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21354. return WC_TEST_RET_ENC_NC;
  21355. EVP_CIPHER_CTX_init(de);
  21356. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21357. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21358. return WC_TEST_RET_ENC_NC;
  21359. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21360. return WC_TEST_RET_ENC_NC;
  21361. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21362. return WC_TEST_RET_ENC_NC;
  21363. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  21364. return WC_TEST_RET_ENC_NC;
  21365. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21366. return WC_TEST_RET_ENC_NC;
  21367. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21368. return WC_TEST_RET_ENC_NC;
  21369. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21370. return WC_TEST_RET_ENC_NC;
  21371. if (XMEMCMP(cipherBuff, oddCipher, 9))
  21372. return WC_TEST_RET_ENC_NC;
  21373. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21374. return WC_TEST_RET_ENC_NC;
  21375. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21376. return WC_TEST_RET_ENC_NC;
  21377. #endif /* WOLFSSL_AES_128 */
  21378. #ifdef WOLFSSL_AES_192
  21379. EVP_CIPHER_CTX_init(en);
  21380. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  21381. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21382. return WC_TEST_RET_ENC_NC;
  21383. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  21384. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21385. return WC_TEST_RET_ENC_NC;
  21386. EVP_CIPHER_CTX_init(de);
  21387. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  21388. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21389. return WC_TEST_RET_ENC_NC;
  21390. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21391. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21392. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21393. return WC_TEST_RET_ENC_NC;
  21394. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  21395. return WC_TEST_RET_ENC_NC;
  21396. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  21397. return WC_TEST_RET_ENC_NC;
  21398. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21399. return WC_TEST_RET_ENC_NC;
  21400. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21401. return WC_TEST_RET_ENC_NC;
  21402. #endif /* WOLFSSL_AES_192 */
  21403. #ifdef WOLFSSL_AES_256
  21404. EVP_CIPHER_CTX_init(en);
  21405. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  21406. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21407. return WC_TEST_RET_ENC_NC;
  21408. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  21409. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21410. return WC_TEST_RET_ENC_NC;
  21411. EVP_CIPHER_CTX_init(de);
  21412. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  21413. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21414. return WC_TEST_RET_ENC_NC;
  21415. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21416. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21417. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21418. return WC_TEST_RET_ENC_NC;
  21419. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  21420. return WC_TEST_RET_ENC_NC;
  21421. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  21422. return WC_TEST_RET_ENC_NC;
  21423. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21424. return WC_TEST_RET_ENC_NC;
  21425. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21426. return WC_TEST_RET_ENC_NC;
  21427. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21428. wolfSSL_EVP_CIPHER_CTX_free(en);
  21429. wolfSSL_EVP_CIPHER_CTX_free(de);
  21430. #endif
  21431. #endif /* WOLFSSL_AES_256 */
  21432. }
  21433. #endif /* HAVE_AES_COUNTER */
  21434. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21435. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  21436. {
  21437. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21438. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21439. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21440. #else
  21441. AES_KEY enc[1];
  21442. AES_KEY dec[1];
  21443. #endif
  21444. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  21445. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  21446. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  21447. };
  21448. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21449. {
  21450. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21451. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21452. };
  21453. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  21454. {
  21455. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  21456. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  21457. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  21458. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  21459. };
  21460. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21461. {
  21462. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21463. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21464. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21465. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  21466. };
  21467. byte cipher[AES_BLOCK_SIZE * 2];
  21468. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  21469. int num = 0;
  21470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21471. if ((enc == NULL) || (dec == NULL))
  21472. return MEMORY_E;
  21473. #endif
  21474. XMEMCPY(iv, setIv, sizeof(setIv));
  21475. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  21476. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  21477. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  21478. &num, AES_ENCRYPT);
  21479. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  21480. return WC_TEST_RET_ENC_NC;
  21481. if (num != 15) /* should have used 15 of the 16 bytes */
  21482. return WC_TEST_RET_ENC_NC;
  21483. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  21484. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  21485. &num, AES_ENCRYPT);
  21486. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  21487. return WC_TEST_RET_ENC_NC;
  21488. if (num != 0)
  21489. return WC_TEST_RET_ENC_NC;
  21490. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21491. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21492. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21493. #endif
  21494. }
  21495. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  21496. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21497. return 0;
  21498. }
  21499. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  21500. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  21501. {
  21502. wc_test_ret_t ret;
  21503. #ifdef WOLFSSL_SMALL_STACK
  21504. EVP_MD_CTX *md_ctx = (EVP_MD_CTX *)XMALLOC(sizeof(EVP_MD_CTX), NULL, DYNAMIC_TYPE_OPENSSL);
  21505. #else
  21506. EVP_MD_CTX md_ctx[1];
  21507. #endif
  21508. testVector a, b, c, d, e, f;
  21509. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  21510. #ifdef WOLFSSL_SMALL_STACK
  21511. if (md_ctx == NULL)
  21512. return WC_TEST_RET_ENC_EC(MEMORY_E);
  21513. #endif
  21514. WOLFSSL_ENTER("openssl_test");
  21515. a.inLen = 0;
  21516. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  21517. (void)a;
  21518. (void)b;
  21519. (void)c;
  21520. (void)d;
  21521. (void)e;
  21522. (void)f;
  21523. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  21524. {
  21525. byte* p;
  21526. p = (byte*)CRYPTO_malloc(10, "", 0);
  21527. if (p == NULL) {
  21528. return WC_TEST_RET_ENC_NC;
  21529. }
  21530. XMEMSET(p, 0, 10);
  21531. CRYPTO_free(p, "", 0);
  21532. }
  21533. #ifndef NO_MD5
  21534. a.input = "1234567890123456789012345678901234567890123456789012345678"
  21535. "9012345678901234567890";
  21536. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  21537. "\x7a";
  21538. a.inLen = XSTRLEN(a.input);
  21539. a.outLen = WC_MD5_DIGEST_SIZE;
  21540. EVP_MD_CTX_init(md_ctx);
  21541. ret = EVP_DigestInit(md_ctx, EVP_md5());
  21542. if (ret == WOLFSSL_SUCCESS) {
  21543. ret = EVP_DigestUpdate(md_ctx, a.input, (unsigned long)a.inLen);
  21544. }
  21545. if (ret == WOLFSSL_SUCCESS) {
  21546. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21547. }
  21548. EVP_MD_CTX_cleanup(md_ctx);
  21549. if (ret != WOLFSSL_SUCCESS)
  21550. return WC_TEST_RET_ENC_NC;
  21551. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  21552. return WC_TEST_RET_ENC_NC;
  21553. #endif /* NO_MD5 */
  21554. #ifndef NO_SHA
  21555. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21556. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21557. "aaaaaaaaaa";
  21558. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  21559. "\x53\x99\x5E\x26\xA0";
  21560. b.inLen = XSTRLEN(b.input);
  21561. b.outLen = WC_SHA_DIGEST_SIZE;
  21562. EVP_MD_CTX_init(md_ctx);
  21563. ret = EVP_DigestInit(md_ctx, EVP_sha1());
  21564. if (ret == WOLFSSL_SUCCESS) {
  21565. ret = EVP_DigestUpdate(md_ctx, b.input, (unsigned long)b.inLen);
  21566. if (ret == WOLFSSL_SUCCESS)
  21567. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21568. }
  21569. EVP_MD_CTX_cleanup(md_ctx);
  21570. if (ret != WOLFSSL_SUCCESS)
  21571. return WC_TEST_RET_ENC_NC;
  21572. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  21573. return WC_TEST_RET_ENC_NC;
  21574. #endif /* NO_SHA */
  21575. #ifdef WOLFSSL_SHA224
  21576. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21577. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21578. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  21579. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  21580. e.inLen = XSTRLEN(e.input);
  21581. e.outLen = WC_SHA224_DIGEST_SIZE;
  21582. EVP_MD_CTX_init(md_ctx);
  21583. ret = EVP_DigestInit(md_ctx, EVP_sha224());
  21584. if (ret == WOLFSSL_SUCCESS) {
  21585. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21586. if (ret == WOLFSSL_SUCCESS)
  21587. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21588. }
  21589. EVP_MD_CTX_cleanup(md_ctx);
  21590. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21591. return WC_TEST_RET_ENC_NC;
  21592. }
  21593. #endif /* WOLFSSL_SHA224 */
  21594. #ifndef NO_SHA256
  21595. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  21596. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  21597. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  21598. "\x06\xC1";
  21599. d.inLen = XSTRLEN(d.input);
  21600. d.outLen = WC_SHA256_DIGEST_SIZE;
  21601. EVP_MD_CTX_init(md_ctx);
  21602. ret = EVP_DigestInit(md_ctx, EVP_sha256());
  21603. if (ret == WOLFSSL_SUCCESS) {
  21604. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21605. if (ret == WOLFSSL_SUCCESS)
  21606. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21607. }
  21608. EVP_MD_CTX_cleanup(md_ctx);
  21609. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21610. return WC_TEST_RET_ENC_NC;
  21611. }
  21612. #endif /* !NO_SHA256 */
  21613. #ifdef WOLFSSL_SHA384
  21614. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21615. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21616. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  21617. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  21618. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  21619. "\x74\x60\x39";
  21620. e.inLen = XSTRLEN(e.input);
  21621. e.outLen = WC_SHA384_DIGEST_SIZE;
  21622. EVP_MD_CTX_init(md_ctx);
  21623. ret = EVP_DigestInit(md_ctx, EVP_sha384());
  21624. if (ret == WOLFSSL_SUCCESS) {
  21625. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21626. if (ret == WOLFSSL_SUCCESS)
  21627. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21628. }
  21629. EVP_MD_CTX_cleanup(md_ctx);
  21630. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21631. return WC_TEST_RET_ENC_NC;
  21632. }
  21633. #endif /* WOLFSSL_SHA384 */
  21634. #ifdef WOLFSSL_SHA512
  21635. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21636. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21637. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  21638. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  21639. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  21640. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  21641. "\x87\x4b\xe9\x09";
  21642. f.inLen = XSTRLEN(f.input);
  21643. f.outLen = WC_SHA512_DIGEST_SIZE;
  21644. EVP_MD_CTX_init(md_ctx);
  21645. ret = EVP_DigestInit(md_ctx, EVP_sha512());
  21646. if (ret == WOLFSSL_SUCCESS) {
  21647. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21648. if (ret == WOLFSSL_SUCCESS)
  21649. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21650. }
  21651. EVP_MD_CTX_cleanup(md_ctx);
  21652. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21653. return WC_TEST_RET_ENC_NC;
  21654. }
  21655. #if !defined(WOLFSSL_NOSHA512_224) && \
  21656. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21657. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21658. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21659. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  21660. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  21661. f.inLen = XSTRLEN(f.input);
  21662. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  21663. EVP_MD_CTX_init(md_ctx);
  21664. ret = EVP_DigestInit(md_ctx, EVP_sha512_224());
  21665. if (ret == WOLFSSL_SUCCESS) {
  21666. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21667. if (ret == WOLFSSL_SUCCESS)
  21668. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21669. }
  21670. EVP_MD_CTX_cleanup(md_ctx);
  21671. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21672. return WC_TEST_RET_ENC_NC;
  21673. }
  21674. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21675. #if !defined(WOLFSSL_NOSHA512_256) && \
  21676. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21677. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21678. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21679. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  21680. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  21681. "\x56\x3a";
  21682. f.inLen = XSTRLEN(f.input);
  21683. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  21684. EVP_MD_CTX_init(md_ctx);
  21685. ret = EVP_DigestInit(md_ctx, EVP_sha512_256());
  21686. if (ret == WOLFSSL_SUCCESS) {
  21687. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21688. if (ret == WOLFSSL_SUCCESS)
  21689. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21690. }
  21691. EVP_MD_CTX_cleanup(md_ctx);
  21692. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21693. return WC_TEST_RET_ENC_NC;
  21694. }
  21695. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21696. #endif /* WOLFSSL_SHA512 */
  21697. #ifdef WOLFSSL_SHA3
  21698. #ifndef WOLFSSL_NOSHA3_224
  21699. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21700. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21701. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  21702. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  21703. e.inLen = XSTRLEN(e.input);
  21704. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  21705. EVP_MD_CTX_init(md_ctx);
  21706. ret = EVP_DigestInit(md_ctx, EVP_sha3_224());
  21707. if (ret == WOLFSSL_SUCCESS) {
  21708. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21709. if (ret == WOLFSSL_SUCCESS)
  21710. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21711. }
  21712. EVP_MD_CTX_cleanup(md_ctx);
  21713. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21714. return WC_TEST_RET_ENC_NC;
  21715. }
  21716. #endif /* WOLFSSL_NOSHA3_224 */
  21717. #ifndef WOLFSSL_NOSHA3_256
  21718. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21719. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21720. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  21721. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  21722. "\x1d\x18";
  21723. d.inLen = XSTRLEN(d.input);
  21724. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  21725. EVP_MD_CTX_init(md_ctx);
  21726. ret = EVP_DigestInit(md_ctx, EVP_sha3_256());
  21727. if (ret == WOLFSSL_SUCCESS) {
  21728. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21729. if (ret == WOLFSSL_SUCCESS)
  21730. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21731. }
  21732. EVP_MD_CTX_cleanup(md_ctx);
  21733. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21734. return WC_TEST_RET_ENC_NC;
  21735. }
  21736. #endif /* WOLFSSL_NOSHA3_256 */
  21737. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21738. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21739. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  21740. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  21741. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  21742. "\xaa\x7f\xc7";
  21743. e.inLen = XSTRLEN(e.input);
  21744. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  21745. EVP_MD_CTX_init(md_ctx);
  21746. ret = EVP_DigestInit(md_ctx, EVP_sha3_384());
  21747. if (ret == WOLFSSL_SUCCESS) {
  21748. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21749. if (ret == WOLFSSL_SUCCESS)
  21750. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21751. }
  21752. EVP_MD_CTX_cleanup(md_ctx);
  21753. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21754. return WC_TEST_RET_ENC_NC;
  21755. }
  21756. #ifndef WOLFSSL_NOSHA3_512
  21757. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21758. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21759. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  21760. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  21761. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  21762. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  21763. "\xf2\x1d\xd1\x85";
  21764. f.inLen = XSTRLEN(f.input);
  21765. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  21766. EVP_MD_CTX_init(md_ctx);
  21767. ret = EVP_DigestInit(md_ctx, EVP_sha3_512());
  21768. if (ret == WOLFSSL_SUCCESS) {
  21769. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21770. if (ret == WOLFSSL_SUCCESS)
  21771. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21772. }
  21773. EVP_MD_CTX_cleanup(md_ctx);
  21774. if (ret != WOLFSSL_SUCCESS ||
  21775. XMEMCMP(hash, f.output, f.outLen) != 0) {
  21776. return WC_TEST_RET_ENC_NC;
  21777. }
  21778. #endif /* WOLFSSL_NOSHA3_512 */
  21779. #endif /* WOLFSSL_SHA3 */
  21780. #ifdef WOLFSSL_SMALL_STACK
  21781. XFREE(md_ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  21782. md_ctx = NULL;
  21783. #endif
  21784. #ifndef WC_NO_RNG
  21785. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  21786. return WC_TEST_RET_ENC_NC;
  21787. #endif
  21788. #ifndef NO_MD5
  21789. c.input = "what do ya want for nothing?";
  21790. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  21791. "\x76";
  21792. c.inLen = XSTRLEN(c.input);
  21793. c.outLen = WC_MD5_DIGEST_SIZE;
  21794. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  21795. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  21796. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21797. hash, 0) != NULL)
  21798. #else
  21799. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21800. hash, 0) == NULL ||
  21801. XMEMCMP(hash, c.output, c.outLen) != 0)
  21802. #endif
  21803. {
  21804. return WC_TEST_RET_ENC_NC;
  21805. }
  21806. #endif /* NO_MD5 */
  21807. #ifndef NO_DES3
  21808. { /* des test */
  21809. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  21810. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21811. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21812. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21813. };
  21814. byte plain[24];
  21815. byte cipher[24];
  21816. const_DES_cblock key = {
  21817. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  21818. };
  21819. DES_cblock iv = {
  21820. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  21821. };
  21822. DES_key_schedule sched;
  21823. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21824. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  21825. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  21826. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  21827. };
  21828. DES_key_sched(&key, &sched);
  21829. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  21830. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  21831. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  21832. return WC_TEST_RET_ENC_NC;
  21833. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21834. return WC_TEST_RET_ENC_NC;
  21835. /* test changing iv */
  21836. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  21837. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  21838. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21839. return WC_TEST_RET_ENC_NC;
  21840. } /* end des test */
  21841. #endif /* NO_DES3 */
  21842. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  21843. if ((ret = openssl_aes_test()) != 0) {
  21844. return ret;
  21845. }
  21846. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  21847. { /* evp_cipher test: EVP_aes_128_cbc */
  21848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21849. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21850. #else
  21851. EVP_CIPHER_CTX ctx[1];
  21852. #endif
  21853. int idx, cipherSz, plainSz;
  21854. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21855. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21856. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21857. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21858. };
  21859. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21860. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21861. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21862. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  21863. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  21864. };
  21865. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21866. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21867. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21868. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  21869. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  21870. };
  21871. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21872. "0123456789abcdef "; /* align */
  21873. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21874. "1234567890abcdef "; /* align */
  21875. byte cipher[AES_BLOCK_SIZE * 4];
  21876. byte plain [AES_BLOCK_SIZE * 4];
  21877. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21878. if (ctx == NULL)
  21879. return MEMORY_E;
  21880. #endif
  21881. cipherSz = 0;
  21882. EVP_CIPHER_CTX_init(ctx);
  21883. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21884. if (ret == WOLFSSL_SUCCESS) {
  21885. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  21886. if (ret == WOLFSSL_SUCCESS)
  21887. cipherSz += idx;
  21888. }
  21889. if (ret == WOLFSSL_SUCCESS) {
  21890. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21891. if (ret == WOLFSSL_SUCCESS)
  21892. cipherSz += idx;
  21893. }
  21894. EVP_CIPHER_CTX_cleanup(ctx);
  21895. if (ret != WOLFSSL_SUCCESS)
  21896. return WC_TEST_RET_ENC_NC;
  21897. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  21898. return WC_TEST_RET_ENC_NC;
  21899. /* check partial decrypt (not enough padding for full block) */
  21900. plainSz = 0;
  21901. EVP_CIPHER_CTX_init(ctx);
  21902. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21903. if (ret == WOLFSSL_SUCCESS) {
  21904. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  21905. if (ret == WOLFSSL_SUCCESS)
  21906. plainSz += idx;
  21907. }
  21908. if (ret == WOLFSSL_SUCCESS) {
  21909. /* this test should fail... not enough padding for full block */
  21910. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21911. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  21912. ret = WOLFSSL_SUCCESS;
  21913. else
  21914. ret = WC_TEST_RET_ENC_NC;
  21915. }
  21916. else
  21917. ret = WC_TEST_RET_ENC_NC;
  21918. EVP_CIPHER_CTX_cleanup(ctx);
  21919. if (ret != WOLFSSL_SUCCESS)
  21920. return ret;
  21921. plainSz = 0;
  21922. EVP_CIPHER_CTX_init(ctx);
  21923. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21924. if (ret == WOLFSSL_SUCCESS) {
  21925. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  21926. if (ret == WOLFSSL_SUCCESS)
  21927. plainSz += idx;
  21928. }
  21929. if (ret == WOLFSSL_SUCCESS) {
  21930. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21931. if (ret == WOLFSSL_SUCCESS)
  21932. plainSz += idx;
  21933. }
  21934. EVP_CIPHER_CTX_cleanup(ctx);
  21935. if (ret != WOLFSSL_SUCCESS)
  21936. return WC_TEST_RET_ENC_NC;
  21937. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  21938. return WC_TEST_RET_ENC_NC;
  21939. cipherSz = 0;
  21940. EVP_CIPHER_CTX_init(ctx);
  21941. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21942. if (ret == WOLFSSL_SUCCESS) {
  21943. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  21944. if (ret == WOLFSSL_SUCCESS)
  21945. cipherSz += idx;
  21946. }
  21947. if (ret == WOLFSSL_SUCCESS) {
  21948. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21949. if (ret == WOLFSSL_SUCCESS)
  21950. cipherSz += idx;
  21951. }
  21952. EVP_CIPHER_CTX_cleanup(ctx);
  21953. if (ret != WOLFSSL_SUCCESS)
  21954. return WC_TEST_RET_ENC_NC;
  21955. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  21956. return WC_TEST_RET_ENC_NC;
  21957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21958. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21959. #endif
  21960. } /* end evp_cipher test: EVP_aes_128_cbc*/
  21961. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  21962. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  21963. { /* evp_cipher test: EVP_aes_256_ecb*/
  21964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21965. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21966. #else
  21967. EVP_CIPHER_CTX ctx[1];
  21968. #endif
  21969. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  21970. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21971. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21972. };
  21973. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21974. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21975. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21976. };
  21977. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  21978. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21979. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21980. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21981. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21982. };
  21983. byte cipher[AES_BLOCK_SIZE * 4];
  21984. byte plain [AES_BLOCK_SIZE * 4];
  21985. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21986. if (ctx == NULL)
  21987. return MEMORY_E;
  21988. #endif
  21989. EVP_CIPHER_CTX_init(ctx);
  21990. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  21991. if (ret == WOLFSSL_SUCCESS)
  21992. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  21993. EVP_CIPHER_CTX_cleanup(ctx);
  21994. if (ret != 16)
  21995. return WC_TEST_RET_ENC_NC;
  21996. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21997. return WC_TEST_RET_ENC_NC;
  21998. EVP_CIPHER_CTX_init(ctx);
  21999. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  22000. if (ret == WOLFSSL_SUCCESS)
  22001. ret = EVP_Cipher(ctx, plain, cipher, 16);
  22002. EVP_CIPHER_CTX_cleanup(ctx);
  22003. if (ret != 16)
  22004. return WC_TEST_RET_ENC_NC;
  22005. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22006. return WC_TEST_RET_ENC_NC;
  22007. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22008. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22009. #endif
  22010. } /* end evp_cipher test */
  22011. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  22012. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  22013. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  22014. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  22015. {
  22016. /* Test: AES_encrypt/decrypt/set Key */
  22017. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22018. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22019. #ifdef HAVE_AES_DECRYPT
  22020. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22021. #endif
  22022. #else
  22023. AES_KEY enc[1];
  22024. #ifdef HAVE_AES_DECRYPT
  22025. AES_KEY dec[1];
  22026. #endif
  22027. #endif
  22028. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  22029. {
  22030. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22031. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22032. };
  22033. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  22034. {
  22035. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22036. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22037. };
  22038. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  22039. {
  22040. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22041. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22042. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22043. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22044. };
  22045. byte plain[sizeof(msg)];
  22046. byte cipher[sizeof(msg)];
  22047. printf("openSSL extra test\n") ;
  22048. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22049. if (enc == NULL)
  22050. return MEMORY_E;
  22051. #ifdef HAVE_AES_DECRYPT
  22052. if (dec == NULL)
  22053. return MEMORY_E;
  22054. #endif
  22055. #endif
  22056. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  22057. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  22058. AES_encrypt(msg, cipher, enc);
  22059. #ifdef HAVE_AES_DECRYPT
  22060. AES_decrypt(cipher, plain, dec);
  22061. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22062. return WC_TEST_RET_ENC_NC;
  22063. #endif /* HAVE_AES_DECRYPT */
  22064. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22065. return WC_TEST_RET_ENC_NC;
  22066. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22067. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22068. #ifdef HAVE_AES_DECRYPT
  22069. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22070. #endif
  22071. #endif
  22072. }
  22073. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  22074. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  22075. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  22076. #ifdef WOLFSSL_AES_COUNTER
  22077. {
  22078. byte plainBuff [64];
  22079. byte cipherBuff[64];
  22080. #ifdef WOLFSSL_AES_128
  22081. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  22082. {
  22083. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  22084. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  22085. };
  22086. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  22087. {
  22088. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22089. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22090. };
  22091. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  22092. {
  22093. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22094. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22095. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22096. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22097. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22098. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22099. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22100. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22101. };
  22102. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  22103. {
  22104. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  22105. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  22106. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  22107. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  22108. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  22109. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  22110. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  22111. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  22112. };
  22113. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  22114. {
  22115. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  22116. 0xc2
  22117. };
  22118. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22119. EVP_CIPHER_CTX *p_en;
  22120. EVP_CIPHER_CTX *p_de;
  22121. #endif
  22122. #endif /* WOLFSSL_AES_128 */
  22123. #ifdef WOLFSSL_AES_192
  22124. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22125. * NIST Special Publication 800-38A */
  22126. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  22127. {
  22128. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  22129. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  22130. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  22131. };
  22132. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  22133. {
  22134. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22135. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22136. };
  22137. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  22138. {
  22139. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22140. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22141. };
  22142. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  22143. {
  22144. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  22145. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  22146. };
  22147. #endif /* WOLFSSL_AES_192 */
  22148. #ifdef WOLFSSL_AES_256
  22149. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22150. * NIST Special Publication 800-38A */
  22151. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  22152. {
  22153. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22154. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22155. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22156. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22157. };
  22158. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  22159. {
  22160. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22161. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22162. };
  22163. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  22164. {
  22165. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22166. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22167. };
  22168. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  22169. {
  22170. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  22171. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  22172. };
  22173. #endif /* WOLFSSL_AES_256 */
  22174. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22175. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22176. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22177. if ((en == NULL) || (de == NULL))
  22178. return MEMORY_E;
  22179. #else
  22180. EVP_CIPHER_CTX en[1];
  22181. EVP_CIPHER_CTX de[1];
  22182. #endif
  22183. #ifdef WOLFSSL_AES_128
  22184. EVP_CIPHER_CTX_init(en);
  22185. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22186. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22187. return WC_TEST_RET_ENC_NC;
  22188. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  22189. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22190. return WC_TEST_RET_ENC_NC;
  22191. EVP_CIPHER_CTX_init(de);
  22192. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22193. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22194. return WC_TEST_RET_ENC_NC;
  22195. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22196. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22197. return WC_TEST_RET_ENC_NC;
  22198. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22199. return WC_TEST_RET_ENC_NC;
  22200. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22201. return WC_TEST_RET_ENC_NC;
  22202. EVP_CIPHER_CTX_cleanup(en);
  22203. EVP_CIPHER_CTX_cleanup(de);
  22204. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22205. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  22206. if (p_en == NULL)
  22207. return WC_TEST_RET_ENC_ERRNO;
  22208. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  22209. if (p_de == NULL)
  22210. return WC_TEST_RET_ENC_ERRNO;
  22211. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  22212. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22213. return WC_TEST_RET_ENC_NC;
  22214. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  22215. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22216. return WC_TEST_RET_ENC_NC;
  22217. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  22218. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22219. return WC_TEST_RET_ENC_NC;
  22220. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  22221. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22222. return WC_TEST_RET_ENC_NC;
  22223. EVP_CIPHER_CTX_cleanup(p_en);
  22224. EVP_CIPHER_CTX_cleanup(p_de);
  22225. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  22226. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  22227. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22228. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22229. return WC_TEST_RET_ENC_NC;
  22230. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22231. return WC_TEST_RET_ENC_NC;
  22232. EVP_CIPHER_CTX_init(en);
  22233. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22234. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22235. return WC_TEST_RET_ENC_NC;
  22236. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22237. return WC_TEST_RET_ENC_NC;
  22238. EVP_CIPHER_CTX_init(de);
  22239. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22240. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22241. return WC_TEST_RET_ENC_NC;
  22242. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22243. return WC_TEST_RET_ENC_NC;
  22244. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22245. return WC_TEST_RET_ENC_NC;
  22246. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  22247. return WC_TEST_RET_ENC_NC;
  22248. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22249. return WC_TEST_RET_ENC_NC;
  22250. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22251. return WC_TEST_RET_ENC_NC;
  22252. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22253. return WC_TEST_RET_ENC_NC;
  22254. if (XMEMCMP(cipherBuff, oddCipher, 9))
  22255. return WC_TEST_RET_ENC_NC;
  22256. EVP_CIPHER_CTX_cleanup(en);
  22257. EVP_CIPHER_CTX_cleanup(de);
  22258. #endif /* WOLFSSL_AES_128 */
  22259. #ifdef WOLFSSL_AES_192
  22260. EVP_CIPHER_CTX_init(en);
  22261. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  22262. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22263. return WC_TEST_RET_ENC_NC;
  22264. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  22265. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22266. return WC_TEST_RET_ENC_NC;
  22267. EVP_CIPHER_CTX_init(de);
  22268. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  22269. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22270. return WC_TEST_RET_ENC_NC;
  22271. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22272. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22273. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22274. return WC_TEST_RET_ENC_NC;
  22275. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  22276. return WC_TEST_RET_ENC_NC;
  22277. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  22278. return WC_TEST_RET_ENC_NC;
  22279. EVP_CIPHER_CTX_cleanup(en);
  22280. EVP_CIPHER_CTX_cleanup(de);
  22281. #endif /* WOLFSSL_AES_192 */
  22282. #ifdef WOLFSSL_AES_256
  22283. EVP_CIPHER_CTX_init(en);
  22284. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  22285. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22286. return WC_TEST_RET_ENC_NC;
  22287. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  22288. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22289. return WC_TEST_RET_ENC_NC;
  22290. EVP_CIPHER_CTX_init(de);
  22291. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  22292. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22293. return WC_TEST_RET_ENC_NC;
  22294. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22295. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22296. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22297. return WC_TEST_RET_ENC_NC;
  22298. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  22299. return WC_TEST_RET_ENC_NC;
  22300. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  22301. return WC_TEST_RET_ENC_NC;
  22302. EVP_CIPHER_CTX_cleanup(en);
  22303. EVP_CIPHER_CTX_cleanup(de);
  22304. #endif /* WOLFSSL_AES_256 */
  22305. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22306. wolfSSL_EVP_CIPHER_CTX_free(en);
  22307. wolfSSL_EVP_CIPHER_CTX_free(de);
  22308. #endif
  22309. }
  22310. #endif /* HAVE_AES_COUNTER */
  22311. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22312. {
  22313. /* EVP_CipherUpdate test */
  22314. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  22315. {
  22316. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22317. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22318. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22319. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22320. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22321. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22322. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22323. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22324. };
  22325. byte key[] = "0123456789abcdef "; /* align */
  22326. byte iv[] = "1234567890abcdef "; /* align */
  22327. byte cipher[AES_BLOCK_SIZE * 4];
  22328. byte plain [AES_BLOCK_SIZE * 4];
  22329. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22330. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22331. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22332. #else
  22333. EVP_CIPHER_CTX en[1];
  22334. EVP_CIPHER_CTX de[1];
  22335. #endif
  22336. int outlen ;
  22337. int total = 0;
  22338. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22339. if ((en == NULL) || (de == NULL))
  22340. return MEMORY_E;
  22341. #endif
  22342. EVP_CIPHER_CTX_init(en);
  22343. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22344. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22345. return WC_TEST_RET_ENC_NC;
  22346. /* openSSL compatibility, if(inlen == 0)return 1; */
  22347. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22348. (byte*)cbcPlain, 0) != 1)
  22349. return WC_TEST_RET_ENC_NC;
  22350. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22351. return WC_TEST_RET_ENC_NC;
  22352. EVP_CIPHER_CTX_init(en);
  22353. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22354. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22355. return WC_TEST_RET_ENC_NC;
  22356. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22357. (byte*)cbcPlain, 9) == 0)
  22358. return WC_TEST_RET_ENC_NC;
  22359. if(outlen != 0)
  22360. return WC_TEST_RET_ENC_NC;
  22361. total += outlen;
  22362. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  22363. (byte*)&cbcPlain[9] , 9) == 0)
  22364. return WC_TEST_RET_ENC_NC;
  22365. if(outlen != 16)
  22366. return WC_TEST_RET_ENC_NC;
  22367. total += outlen;
  22368. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22369. return WC_TEST_RET_ENC_NC;
  22370. if(outlen != 16)
  22371. return WC_TEST_RET_ENC_NC;
  22372. total += outlen;
  22373. if(total != 32)
  22374. return WC_TEST_RET_ENC_NC;
  22375. total = 0;
  22376. EVP_CIPHER_CTX_init(de);
  22377. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  22378. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22379. return WC_TEST_RET_ENC_NC;
  22380. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22381. return WC_TEST_RET_ENC_NC;
  22382. if(outlen != 0)
  22383. return WC_TEST_RET_ENC_NC;
  22384. total += outlen;
  22385. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22386. (byte*)&cipher[6], 12) == 0)
  22387. return WC_TEST_RET_ENC_NC;
  22388. if(outlen != 0)
  22389. total += outlen;
  22390. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22391. (byte*)&cipher[6+12], 14) == 0)
  22392. return WC_TEST_RET_ENC_NC;
  22393. if(outlen != 16)
  22394. return WC_TEST_RET_ENC_NC;
  22395. total += outlen;
  22396. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  22397. return WC_TEST_RET_ENC_NC;
  22398. if(outlen != 2)
  22399. return WC_TEST_RET_ENC_NC;
  22400. total += outlen;
  22401. if(total != 18)
  22402. return WC_TEST_RET_ENC_NC;
  22403. if (XMEMCMP(plain, cbcPlain, 18))
  22404. return WC_TEST_RET_ENC_NC;
  22405. total = 0;
  22406. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22407. return WC_TEST_RET_ENC_NC;
  22408. EVP_CIPHER_CTX_init(en);
  22409. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  22410. (unsigned char*)key, (unsigned char*)iv) == 0)
  22411. return WC_TEST_RET_ENC_NC;
  22412. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  22413. return WC_TEST_RET_ENC_NC;
  22414. if(outlen != 0)
  22415. return WC_TEST_RET_ENC_NC;
  22416. total += outlen;
  22417. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  22418. return WC_TEST_RET_ENC_NC;
  22419. if(outlen != 16)
  22420. return WC_TEST_RET_ENC_NC;
  22421. total += outlen;
  22422. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22423. return WC_TEST_RET_ENC_NC;
  22424. if(outlen != 16)
  22425. return WC_TEST_RET_ENC_NC;
  22426. total += outlen;
  22427. if(total != 32)
  22428. return 3438;
  22429. total = 0;
  22430. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22431. return WC_TEST_RET_ENC_NC;
  22432. EVP_CIPHER_CTX_init(de);
  22433. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  22434. (unsigned char*)key, (unsigned char*)iv) == 0)
  22435. return WC_TEST_RET_ENC_NC;
  22436. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22437. return WC_TEST_RET_ENC_NC;
  22438. if(outlen != 0)
  22439. return WC_TEST_RET_ENC_NC;
  22440. total += outlen;
  22441. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  22442. return WC_TEST_RET_ENC_NC;
  22443. if(outlen != 0)
  22444. total += outlen;
  22445. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  22446. return WC_TEST_RET_ENC_NC;
  22447. if(outlen != 16)
  22448. return WC_TEST_RET_ENC_NC;
  22449. total += outlen;
  22450. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  22451. return WC_TEST_RET_ENC_NC;
  22452. if(outlen != 2)
  22453. return WC_TEST_RET_ENC_NC;
  22454. total += outlen;
  22455. if(total != 18)
  22456. return 3447;
  22457. if (XMEMCMP(plain, cbcPlain, 18))
  22458. return WC_TEST_RET_ENC_NC;
  22459. if (EVP_CIPHER_key_length(NULL) != 0)
  22460. return WC_TEST_RET_ENC_NC;
  22461. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  22462. return WC_TEST_RET_ENC_NC;
  22463. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  22464. return WC_TEST_RET_ENC_NC;
  22465. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  22466. return WC_TEST_RET_ENC_NC;
  22467. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22468. return WC_TEST_RET_ENC_NC;
  22469. EVP_CIPHER_CTX_init(en);
  22470. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  22471. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22472. return WC_TEST_RET_ENC_NC;
  22473. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22474. return WC_TEST_RET_ENC_NC;
  22475. EVP_CIPHER_CTX_init(en);
  22476. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22477. (unsigned char*)key, (unsigned char*)iv) == 0)
  22478. return WC_TEST_RET_ENC_NC;
  22479. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22480. return WC_TEST_RET_ENC_NC;
  22481. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22482. return WC_TEST_RET_ENC_NC;
  22483. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22484. return WC_TEST_RET_ENC_NC;
  22485. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22486. return WC_TEST_RET_ENC_NC;
  22487. EVP_CIPHER_CTX_init(de);
  22488. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  22489. (unsigned char*)key, (unsigned char*)iv) == 0)
  22490. return WC_TEST_RET_ENC_NC;
  22491. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22492. return WC_TEST_RET_ENC_NC;
  22493. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22494. return WC_TEST_RET_ENC_NC;
  22495. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  22496. return WC_TEST_RET_ENC_NC;
  22497. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22498. return WC_TEST_RET_ENC_NC;
  22499. EVP_CIPHER_CTX_init(en);
  22500. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22501. (unsigned char*)key, (unsigned char*)iv);
  22502. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  22503. return WC_TEST_RET_ENC_NC;
  22504. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  22505. return WC_TEST_RET_ENC_NC;
  22506. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  22507. return WC_TEST_RET_ENC_NC;
  22508. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  22509. return WC_TEST_RET_ENC_NC;
  22510. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  22511. return WC_TEST_RET_ENC_NC;
  22512. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  22513. EVP_CIPHER_CTX_set_flags(en, 42);
  22514. if (en->flags != 42)
  22515. return WC_TEST_RET_ENC_NC;
  22516. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  22517. return WC_TEST_RET_ENC_NC;
  22518. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  22519. return WC_TEST_RET_ENC_NC;
  22520. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  22521. return WC_TEST_RET_ENC_NC;
  22522. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22523. return WC_TEST_RET_ENC_NC;
  22524. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22525. return WC_TEST_RET_ENC_NC;
  22526. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22527. wolfSSL_EVP_CIPHER_CTX_free(en);
  22528. wolfSSL_EVP_CIPHER_CTX_free(de);
  22529. #endif
  22530. }
  22531. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22532. #endif /* ifndef NO_AES */
  22533. return 0;
  22534. }
  22535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  22536. {
  22537. wc_test_ret_t ret = 0;
  22538. #if !defined(NO_SHA256) && !defined(NO_SHA)
  22539. WOLFSSL_EVP_MD_CTX* ctx;
  22540. WOLFSSL_EVP_MD_CTX* ctx2;
  22541. WOLFSSL_ENTER("openSSL_evpMD_test");
  22542. ctx = EVP_MD_CTX_create();
  22543. ctx2 = EVP_MD_CTX_create();
  22544. ret = EVP_DigestInit(ctx, EVP_sha256());
  22545. if (ret != SSL_SUCCESS) {
  22546. ret = WC_TEST_RET_ENC_NC;
  22547. goto openSSL_evpMD_test_done;
  22548. }
  22549. ret = EVP_MD_CTX_copy(ctx2, ctx);
  22550. if (ret != SSL_SUCCESS) {
  22551. ret = WC_TEST_RET_ENC_NC;
  22552. goto openSSL_evpMD_test_done;
  22553. }
  22554. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22555. ret = WC_TEST_RET_ENC_NC;
  22556. goto openSSL_evpMD_test_done;
  22557. }
  22558. ret = EVP_DigestInit(ctx, EVP_sha1());
  22559. if (ret != SSL_SUCCESS) {
  22560. ret = WC_TEST_RET_ENC_NC;
  22561. goto openSSL_evpMD_test_done;
  22562. }
  22563. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22564. ret = WC_TEST_RET_ENC_NC;
  22565. goto openSSL_evpMD_test_done;
  22566. }
  22567. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  22568. if (ret != SSL_SUCCESS) {
  22569. ret = WC_TEST_RET_ENC_NC;
  22570. goto openSSL_evpMD_test_done;
  22571. }
  22572. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  22573. ret = WC_TEST_RET_ENC_NC;
  22574. goto openSSL_evpMD_test_done;
  22575. }
  22576. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  22577. ret = WC_TEST_RET_ENC_NC;
  22578. goto openSSL_evpMD_test_done;
  22579. }
  22580. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  22581. ret = WC_TEST_RET_ENC_NC;
  22582. goto openSSL_evpMD_test_done;
  22583. }
  22584. if (EVP_add_digest(NULL) != 0) {
  22585. ret = WC_TEST_RET_ENC_NC;
  22586. goto openSSL_evpMD_test_done;
  22587. }
  22588. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  22589. ret = WC_TEST_RET_ENC_NC;
  22590. goto openSSL_evpMD_test_done;
  22591. }
  22592. ret = 0; /* got to success state without jumping to end with a fail */
  22593. openSSL_evpMD_test_done:
  22594. EVP_MD_CTX_destroy(ctx);
  22595. EVP_MD_CTX_destroy(ctx2);
  22596. #endif /* NO_SHA256 */
  22597. return ret;
  22598. }
  22599. #ifdef DEBUG_SIGN
  22600. static void show(const char *title, const char *p, unsigned int s) {
  22601. char* i;
  22602. printf("%s: ", title);
  22603. for (i = p;
  22604. i < p + s;
  22605. printf("%c", *i), i++);
  22606. printf("\n");
  22607. }
  22608. #else
  22609. #define show(a,b,c) WC_DO_NOTHING
  22610. #endif
  22611. #define FOURK_BUFF 4096
  22612. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  22613. {
  22614. wc_test_ret_t ret = 0;
  22615. #if !defined(NO_RSA) && !defined(NO_SHA)
  22616. byte* prvTmp;
  22617. byte* pubTmp;
  22618. int prvBytes;
  22619. int pubBytes;
  22620. RSA *prvRsa = NULL;
  22621. RSA *pubRsa = NULL;
  22622. EVP_PKEY *prvPkey = NULL;
  22623. EVP_PKEY *pubPkey = NULL;
  22624. EVP_PKEY_CTX *enc = NULL;
  22625. EVP_PKEY_CTX *dec = NULL;
  22626. byte in[] = TEST_STRING;
  22627. byte out[256];
  22628. size_t outlen;
  22629. size_t keySz;
  22630. byte plain[256];
  22631. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  22632. XFILE keyFile;
  22633. XFILE keypubFile;
  22634. char cliKey[] = "./certs/client-key.der";
  22635. char cliKeypub[] = "./certs/client-keyPub.der";
  22636. #endif
  22637. WOLFSSL_ENTER("openssl_pkey0_test");
  22638. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22639. if (prvTmp == NULL)
  22640. return WC_TEST_RET_ENC_ERRNO;
  22641. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22642. if (pubTmp == NULL) {
  22643. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22644. return WC_TEST_RET_ENC_NC;
  22645. }
  22646. #ifdef USE_CERT_BUFFERS_1024
  22647. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  22648. prvBytes = sizeof_client_key_der_1024;
  22649. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  22650. pubBytes = sizeof_client_keypub_der_1024;
  22651. #elif defined(USE_CERT_BUFFERS_2048)
  22652. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  22653. prvBytes = sizeof_client_key_der_2048;
  22654. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  22655. pubBytes = sizeof_client_keypub_der_2048;
  22656. #else
  22657. keyFile = XFOPEN(cliKey, "rb");
  22658. if (!keyFile) {
  22659. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22660. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22661. err_sys("can't open ./certs/client-key.der, "
  22662. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22663. return WC_TEST_RET_ENC_ERRNO;
  22664. }
  22665. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  22666. XFCLOSE(keyFile);
  22667. if (prvBytes == 0) {
  22668. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22669. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22670. return WC_TEST_RET_ENC_ERRNO;
  22671. }
  22672. keypubFile = XFOPEN(cliKeypub, "rb");
  22673. if (!keypubFile) {
  22674. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22675. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22676. err_sys("can't open ./certs/client-cert.der, "
  22677. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22678. return WC_TEST_RET_ENC_ERRNO;
  22679. }
  22680. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  22681. XFCLOSE(keypubFile);
  22682. if (pubBytes == 0) {
  22683. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22684. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22685. return WC_TEST_RET_ENC_ERRNO;
  22686. }
  22687. #endif /* USE_CERT_BUFFERS */
  22688. prvRsa = wolfSSL_RSA_new();
  22689. pubRsa = wolfSSL_RSA_new();
  22690. if((prvRsa == NULL) || (pubRsa == NULL)){
  22691. printf("error with RSA_new\n");
  22692. ret = WC_TEST_RET_ENC_ERRNO;
  22693. goto openssl_pkey0_test_done;
  22694. }
  22695. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  22696. if(ret != SSL_SUCCESS){
  22697. printf("error with RSA_LoadDer_ex\n");
  22698. ret = WC_TEST_RET_ENC_EC(ret);
  22699. goto openssl_pkey0_test_done;
  22700. }
  22701. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  22702. if(ret != SSL_SUCCESS){
  22703. printf("error with RSA_LoadDer_ex\n");
  22704. ret = WC_TEST_RET_ENC_EC(ret);
  22705. goto openssl_pkey0_test_done;
  22706. }
  22707. keySz = (size_t)RSA_size(pubRsa);
  22708. prvPkey = wolfSSL_EVP_PKEY_new();
  22709. pubPkey = wolfSSL_EVP_PKEY_new();
  22710. if((prvPkey == NULL) || (pubPkey == NULL)){
  22711. printf("error with PKEY_new\n");
  22712. ret = WC_TEST_RET_ENC_NC;
  22713. goto openssl_pkey0_test_done;
  22714. }
  22715. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  22716. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  22717. if(ret != 2){
  22718. printf("error with PKEY_set1_RSA\n");
  22719. ret = WC_TEST_RET_ENC_I(ret);
  22720. goto openssl_pkey0_test_done;
  22721. }
  22722. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  22723. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  22724. if((dec == NULL)||(enc==NULL)){
  22725. printf("error with EVP_PKEY_CTX_new\n");
  22726. ret = WC_TEST_RET_ENC_NC;
  22727. goto openssl_pkey0_test_done;
  22728. }
  22729. ret = EVP_PKEY_decrypt_init(dec);
  22730. if (ret != 1) {
  22731. printf("error with decrypt init\n");
  22732. ret = WC_TEST_RET_ENC_NC;
  22733. goto openssl_pkey0_test_done;
  22734. }
  22735. ret = EVP_PKEY_encrypt_init(enc);
  22736. if (ret != 1) {
  22737. printf("error with encrypt init\n");
  22738. ret = WC_TEST_RET_ENC_NC;
  22739. goto openssl_pkey0_test_done;
  22740. }
  22741. XMEMSET(out, 0, sizeof(out));
  22742. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22743. if (ret != 1) {
  22744. printf("error encrypting msg\n");
  22745. ret = WC_TEST_RET_ENC_NC;
  22746. goto openssl_pkey0_test_done;
  22747. }
  22748. show("encrypted msg", out, outlen);
  22749. XMEMSET(plain, 0, sizeof(plain));
  22750. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22751. if (ret != 1) {
  22752. printf("error decrypting msg\n");
  22753. ret = WC_TEST_RET_ENC_NC;
  22754. goto openssl_pkey0_test_done;
  22755. }
  22756. show("decrypted msg", plain, outlen);
  22757. /* RSA_PKCS1_OAEP_PADDING test */
  22758. ret = EVP_PKEY_decrypt_init(dec);
  22759. if (ret != 1) {
  22760. printf("error with decrypt init\n");
  22761. ret = WC_TEST_RET_ENC_NC;
  22762. goto openssl_pkey0_test_done;
  22763. }
  22764. ret = EVP_PKEY_encrypt_init(enc);
  22765. if (ret != 1) {
  22766. printf("error with encrypt init\n");
  22767. ret = WC_TEST_RET_ENC_NC;
  22768. goto openssl_pkey0_test_done;
  22769. }
  22770. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22771. printf("first set rsa padding error\n");
  22772. ret = WC_TEST_RET_ENC_EC(ret);
  22773. goto openssl_pkey0_test_done;
  22774. }
  22775. #ifndef HAVE_FIPS
  22776. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22777. printf("second set rsa padding error\n");
  22778. ret = WC_TEST_RET_ENC_EC(ret);
  22779. goto openssl_pkey0_test_done;
  22780. }
  22781. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22782. printf("third set rsa padding error\n");
  22783. ret = WC_TEST_RET_ENC_EC(ret);
  22784. goto openssl_pkey0_test_done;
  22785. }
  22786. #endif
  22787. XMEMSET(out, 0, sizeof(out));
  22788. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22789. if (ret != 1) {
  22790. printf("error encrypting msg\n");
  22791. ret = WC_TEST_RET_ENC_NC;
  22792. goto openssl_pkey0_test_done;
  22793. }
  22794. show("encrypted msg", out, outlen);
  22795. XMEMSET(plain, 0, sizeof(plain));
  22796. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22797. if (ret != 1) {
  22798. printf("error decrypting msg\n");
  22799. ret = WC_TEST_RET_ENC_NC;
  22800. goto openssl_pkey0_test_done;
  22801. }
  22802. show("decrypted msg", plain, outlen);
  22803. ret = 0; /* made it to this point without error then set success */
  22804. openssl_pkey0_test_done:
  22805. wolfSSL_RSA_free(prvRsa);
  22806. wolfSSL_RSA_free(pubRsa);
  22807. EVP_PKEY_free(pubPkey);
  22808. EVP_PKEY_free(prvPkey);
  22809. EVP_PKEY_CTX_free(dec);
  22810. EVP_PKEY_CTX_free(enc);
  22811. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22812. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22813. #endif /* NO_RSA */
  22814. return ret;
  22815. }
  22816. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  22817. {
  22818. wc_test_ret_t ret = 0;
  22819. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  22820. EVP_PKEY_CTX* dec = NULL;
  22821. EVP_PKEY_CTX* enc = NULL;
  22822. EVP_PKEY* pubKey = NULL;
  22823. EVP_PKEY* prvKey = NULL;
  22824. X509* x509 = NULL;
  22825. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  22826. const unsigned char* clikey;
  22827. long cliKeySz;
  22828. size_t outlen;
  22829. int keyLenBits = 2048;
  22830. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22831. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22832. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22833. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22834. if ((tmp == NULL) ||
  22835. (cipher == NULL) ||
  22836. (plain == NULL)) {
  22837. ret = WC_TEST_RET_ENC_NC;
  22838. goto openssl_pkey1_test_done;
  22839. }
  22840. #else
  22841. unsigned char tmp[FOURK_BUF];
  22842. unsigned char cipher[RSA_TEST_BYTES];
  22843. unsigned char plain[RSA_TEST_BYTES];
  22844. #endif
  22845. WOLFSSL_ENTER("openssl_pkey1_test");
  22846. #if defined(USE_CERT_BUFFERS_1024)
  22847. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  22848. cliKeySz = (long)sizeof_client_key_der_1024;
  22849. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  22850. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  22851. keyLenBits = 1024;
  22852. #elif defined(USE_CERT_BUFFERS_2048)
  22853. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  22854. cliKeySz = (long)sizeof_client_key_der_2048;
  22855. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  22856. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  22857. #elif defined(USE_CERT_BUFFERS_3072)
  22858. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  22859. cliKeySz = (long)sizeof_client_key_der_3072;
  22860. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  22861. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  22862. keyLenBits = 3072;
  22863. #elif defined(USE_CERT_BUFFERS_4096)
  22864. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  22865. cliKeySz = (long)sizeof_client_key_der_4096;
  22866. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  22867. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  22868. keyLenBits = 4096;
  22869. #else
  22870. {
  22871. XFILE f;
  22872. f = XFOPEN(clientKey, "rb");
  22873. if (!f) {
  22874. err_sys("can't open ./certs/client-key.der, "
  22875. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22876. ret = WC_TEST_RET_ENC_ERRNO;
  22877. goto openssl_pkey1_test_done;
  22878. }
  22879. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  22880. XFCLOSE(f);
  22881. if (cliKeySz == 0)
  22882. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  22883. }
  22884. /* using existing wolfSSL api to get public and private key */
  22885. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  22886. #endif /* USE_CERT_BUFFERS */
  22887. clikey = tmp;
  22888. if ((prvKey = EVP_PKEY_new()) == NULL) {
  22889. ret = WC_TEST_RET_ENC_ERRNO;
  22890. goto openssl_pkey1_test_done;
  22891. }
  22892. EVP_PKEY_free(prvKey);
  22893. prvKey = NULL;
  22894. if (x509 == NULL) {
  22895. ret = WC_TEST_RET_ENC_NC;
  22896. goto openssl_pkey1_test_done;
  22897. }
  22898. pubKey = X509_get_pubkey(x509);
  22899. if (pubKey == NULL) {
  22900. ret = WC_TEST_RET_ENC_NC;
  22901. goto openssl_pkey1_test_done;
  22902. }
  22903. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  22904. if (prvKey == NULL) {
  22905. ret = WC_TEST_RET_ENC_NC;
  22906. goto openssl_pkey1_test_done;
  22907. }
  22908. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  22909. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  22910. ret = WC_TEST_RET_ENC_NC;
  22911. goto openssl_pkey1_test_done;
  22912. }
  22913. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  22914. ret = WC_TEST_RET_ENC_NC;
  22915. goto openssl_pkey1_test_done;
  22916. }
  22917. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  22918. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  22919. if (dec == NULL || enc == NULL) {
  22920. ret = WC_TEST_RET_ENC_ERRNO;
  22921. goto openssl_pkey1_test_done;
  22922. }
  22923. if (EVP_PKEY_decrypt_init(dec) != 1) {
  22924. ret = WC_TEST_RET_ENC_NC;
  22925. goto openssl_pkey1_test_done;
  22926. }
  22927. if (EVP_PKEY_encrypt_init(enc) != 1) {
  22928. ret = WC_TEST_RET_ENC_NC;
  22929. goto openssl_pkey1_test_done;
  22930. }
  22931. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22932. ret = WC_TEST_RET_ENC_NC;
  22933. goto openssl_pkey1_test_done;
  22934. }
  22935. #ifndef HAVE_FIPS
  22936. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22937. ret = WC_TEST_RET_ENC_EC(ret);
  22938. goto openssl_pkey1_test_done;
  22939. }
  22940. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22941. ret = WC_TEST_RET_ENC_EC(ret);
  22942. goto openssl_pkey1_test_done;
  22943. }
  22944. #endif
  22945. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  22946. outlen = (size_t)(keyLenBits/8);
  22947. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  22948. ret = WC_TEST_RET_ENC_EC(ret);
  22949. goto openssl_pkey1_test_done;
  22950. }
  22951. XMEMSET(plain, 0, RSA_TEST_BYTES);
  22952. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  22953. ret = WC_TEST_RET_ENC_NC;
  22954. goto openssl_pkey1_test_done;
  22955. }
  22956. openssl_pkey1_test_done:
  22957. if (pubKey != NULL) {
  22958. EVP_PKEY_free(pubKey);
  22959. }
  22960. if (prvKey != NULL) {
  22961. EVP_PKEY_free(prvKey);
  22962. }
  22963. if (dec != NULL) {
  22964. EVP_PKEY_CTX_free(dec);
  22965. }
  22966. if (enc != NULL) {
  22967. EVP_PKEY_CTX_free(enc);
  22968. }
  22969. if (x509 != NULL) {
  22970. X509_free(x509);
  22971. }
  22972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22973. if (tmp != NULL)
  22974. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22975. if (cipher != NULL)
  22976. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22977. if (plain != NULL)
  22978. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22979. #endif
  22980. #endif
  22981. return ret;
  22982. }
  22983. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  22984. {
  22985. #if !defined(NO_RSA) && !defined(NO_SHA)
  22986. byte* prvTmp;
  22987. byte* pubTmp;
  22988. int prvBytes;
  22989. int pubBytes;
  22990. RSA *prvRsa;
  22991. RSA *pubRsa;
  22992. EVP_PKEY *prvPkey;
  22993. EVP_PKEY *pubPkey;
  22994. EVP_MD_CTX* sign;
  22995. EVP_MD_CTX* verf;
  22996. char msg[] = "see spot run";
  22997. unsigned char sig[256];
  22998. unsigned int sigSz;
  22999. const void* pt;
  23000. unsigned int count;
  23001. wc_test_ret_t ret, ret1, ret2;
  23002. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  23003. XFILE keyFile;
  23004. XFILE keypubFile;
  23005. char cliKey[] = "./certs/client-key.der";
  23006. char cliKeypub[] = "./certs/client-keyPub.der";
  23007. #endif
  23008. WOLFSSL_ENTER("openssl_evpSig_test");
  23009. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23010. if (prvTmp == NULL)
  23011. return WC_TEST_RET_ENC_ERRNO;
  23012. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23013. if (pubTmp == NULL) {
  23014. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23015. return WC_TEST_RET_ENC_NC;
  23016. }
  23017. #ifdef USE_CERT_BUFFERS_1024
  23018. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  23019. prvBytes = sizeof_client_key_der_1024;
  23020. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  23021. pubBytes = sizeof_client_keypub_der_1024;
  23022. #elif defined(USE_CERT_BUFFERS_2048)
  23023. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  23024. prvBytes = sizeof_client_key_der_2048;
  23025. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  23026. pubBytes = sizeof_client_keypub_der_2048;
  23027. #else
  23028. keyFile = XFOPEN(cliKey, "rb");
  23029. if (!keyFile) {
  23030. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23031. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23032. err_sys("can't open ./certs/client-key.der, "
  23033. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23034. return WC_TEST_RET_ENC_ERRNO;
  23035. }
  23036. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  23037. XFCLOSE(keyFile);
  23038. if (prvBytes == 0) {
  23039. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23040. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23041. return WC_TEST_RET_ENC_ERRNO;
  23042. }
  23043. keypubFile = XFOPEN(cliKeypub, "rb");
  23044. if (!keypubFile) {
  23045. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23046. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23047. err_sys("can't open ./certs/client-cert.der, "
  23048. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23049. return WC_TEST_RET_ENC_ERRNO;
  23050. }
  23051. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  23052. XFCLOSE(keypubFile);
  23053. if (pubBytes == 0) {
  23054. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23055. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23056. return WC_TEST_RET_ENC_ERRNO;
  23057. }
  23058. #endif /* USE_CERT_BUFFERS */
  23059. prvRsa = wolfSSL_RSA_new();
  23060. pubRsa = wolfSSL_RSA_new();
  23061. if((prvRsa == NULL) || (pubRsa == NULL)){
  23062. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23063. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23064. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  23065. return WC_TEST_RET_ENC_NC;
  23066. }
  23067. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  23068. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  23069. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23070. printf("error with RSA_LoadDer_ex\n");
  23071. return WC_TEST_RET_ENC_NC;
  23072. }
  23073. prvPkey = wolfSSL_EVP_PKEY_new();
  23074. pubPkey = wolfSSL_EVP_PKEY_new();
  23075. if((prvPkey == NULL) || (pubPkey == NULL)){
  23076. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23077. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23078. printf("error with KEY_new\n");
  23079. return WC_TEST_RET_ENC_NC;
  23080. }
  23081. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  23082. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  23083. if((ret1 != 1) || (ret2 != 1)){
  23084. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23085. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23086. printf("error with EVP_PKEY_set1_RSA\n");
  23087. return WC_TEST_RET_ENC_NC;
  23088. }
  23089. /****************** sign and verify *******************/
  23090. sign = EVP_MD_CTX_create();
  23091. verf = EVP_MD_CTX_create();
  23092. if((sign == NULL)||(verf == NULL)){
  23093. printf("error with EVP_MD_CTX_create\n");
  23094. EVP_MD_CTX_destroy(sign);
  23095. EVP_MD_CTX_destroy(verf);
  23096. return WC_TEST_RET_ENC_NC;
  23097. }
  23098. ret = EVP_SignInit(sign, EVP_sha1());
  23099. if (ret != SSL_SUCCESS){
  23100. printf("error with EVP_SignInit\n");
  23101. EVP_MD_CTX_destroy(sign);
  23102. EVP_MD_CTX_destroy(verf);
  23103. return WC_TEST_RET_ENC_NC;
  23104. }
  23105. count = sizeof(msg);
  23106. show("message = ", (char *)msg, count);
  23107. /* sign */
  23108. XMEMSET(sig, 0, sizeof(sig));
  23109. pt = (const void*)msg;
  23110. ret1 = EVP_SignUpdate(sign, pt, count);
  23111. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  23112. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23113. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23114. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23115. EVP_MD_CTX_destroy(sign);
  23116. EVP_MD_CTX_destroy(verf);
  23117. printf("error with EVP_MD_CTX_create\n");
  23118. return WC_TEST_RET_ENC_NC;
  23119. }
  23120. show("signature = ", (char *)sig, sigSz);
  23121. /* verify */
  23122. pt = (const void*)msg;
  23123. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  23124. ret2 = EVP_VerifyUpdate(verf, pt, count);
  23125. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23126. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23127. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23128. EVP_MD_CTX_destroy(sign);
  23129. EVP_MD_CTX_destroy(verf);
  23130. printf("error with EVP_Verify\n");
  23131. return WC_TEST_RET_ENC_NC;
  23132. }
  23133. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  23134. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23135. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23136. EVP_MD_CTX_destroy(sign);
  23137. EVP_MD_CTX_destroy(verf);
  23138. printf("error with EVP_VerifyFinal\n");
  23139. return WC_TEST_RET_ENC_NC;
  23140. }
  23141. /* expect fail without update */
  23142. EVP_VerifyInit(verf, EVP_sha1());
  23143. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  23144. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23145. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23146. EVP_MD_CTX_destroy(sign);
  23147. EVP_MD_CTX_destroy(verf);
  23148. printf("EVP_VerifyInit without update not detected\n");
  23149. return WC_TEST_RET_ENC_NC;
  23150. }
  23151. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23152. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23153. EVP_MD_CTX_destroy(sign);
  23154. EVP_MD_CTX_destroy(verf);
  23155. wolfSSL_RSA_free(prvRsa);
  23156. wolfSSL_RSA_free(pubRsa);
  23157. EVP_PKEY_free(pubPkey);
  23158. EVP_PKEY_free(prvPkey);
  23159. #endif /* NO_RSA */
  23160. return 0;
  23161. }
  23162. #endif /* OPENSSL_EXTRA */
  23163. #ifndef NO_PWDBASED
  23164. #ifdef HAVE_SCRYPT
  23165. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  23166. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  23167. {
  23168. #ifdef HAVE_FIPS
  23169. /* RFC 7914 test vector keys are too short for FIPS. */
  23170. #else
  23171. wc_test_ret_t ret;
  23172. byte derived[64];
  23173. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  23174. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  23175. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  23176. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  23177. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  23178. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  23179. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  23180. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  23181. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  23182. };
  23183. #if !defined(BENCH_EMBEDDED)
  23184. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23185. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  23186. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  23187. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  23188. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  23189. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  23190. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  23191. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  23192. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  23193. };
  23194. #endif
  23195. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23196. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  23197. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  23198. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  23199. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  23200. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  23201. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  23202. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  23203. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  23204. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  23205. };
  23206. #endif
  23207. #ifdef SCRYPT_TEST_ALL
  23208. /* Test case is very slow.
  23209. * Use for confirmation after code change or new platform.
  23210. */
  23211. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  23212. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  23213. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  23214. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  23215. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  23216. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  23217. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  23218. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  23219. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  23220. };
  23221. #endif
  23222. WOLFSSL_ENTER("scrypt_test");
  23223. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  23224. if (ret != 0)
  23225. return WC_TEST_RET_ENC_EC(ret);
  23226. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  23227. return WC_TEST_RET_ENC_NC;
  23228. #if !defined(BENCH_EMBEDDED)
  23229. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  23230. sizeof(verify2));
  23231. if (ret != 0)
  23232. return WC_TEST_RET_ENC_EC(ret);
  23233. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23234. return WC_TEST_RET_ENC_NC;
  23235. #endif
  23236. /* Test case with parallel overflowing */
  23237. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  23238. sizeof(verify1));
  23239. if (ret != BAD_FUNC_ARG)
  23240. return WC_TEST_RET_ENC_EC(ret);
  23241. /* Don't run these test on embedded, since they use large mallocs */
  23242. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23243. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23244. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  23245. if (ret != 0)
  23246. return WC_TEST_RET_ENC_EC(ret);
  23247. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  23248. return WC_TEST_RET_ENC_NC;
  23249. #ifdef SCRYPT_TEST_ALL
  23250. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23251. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  23252. if (ret != 0)
  23253. return WC_TEST_RET_ENC_EC(ret);
  23254. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  23255. return WC_TEST_RET_ENC_NC;
  23256. #endif
  23257. #else
  23258. #ifdef SCRYPT_TEST_ALL
  23259. (void)verify4;
  23260. #endif
  23261. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  23262. #if !defined(BENCH_EMBEDDED)
  23263. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  23264. 8, 16, sizeof(verify2));
  23265. if (ret != 0)
  23266. return WC_TEST_RET_ENC_EC(ret);
  23267. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23268. return WC_TEST_RET_ENC_NC;
  23269. #endif
  23270. #endif /* !HAVE_FIPS */
  23271. return 0;
  23272. }
  23273. #endif
  23274. #ifdef HAVE_PKCS12
  23275. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
  23276. {
  23277. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  23278. 0x00, 0x00 };
  23279. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  23280. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  23281. 0x00, 0x67, 0x00, 0x00 };
  23282. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  23283. byte derived[64];
  23284. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23285. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  23286. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  23287. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  23288. };
  23289. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23290. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  23291. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  23292. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  23293. };
  23294. int id = 1;
  23295. int kLen = 24;
  23296. int iterations = 1;
  23297. wc_test_ret_t ret;
  23298. WOLFSSL_ENTER("pkcs12_pbkdf_test");
  23299. ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  23300. iterations, kLen, WC_SHA256, id);
  23301. if (ret < 0)
  23302. return WC_TEST_RET_ENC_EC(ret);
  23303. if (XMEMCMP(derived, verify, kLen) != 0)
  23304. return WC_TEST_RET_ENC_NC;
  23305. iterations = 1000;
  23306. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  23307. iterations, kLen, WC_SHA256, id);
  23308. if (ret < 0)
  23309. return WC_TEST_RET_ENC_EC(ret);
  23310. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  23311. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  23312. if (ret < 0)
  23313. return WC_TEST_RET_ENC_EC(ret);
  23314. if (XMEMCMP(derived, verify2, 24) != 0)
  23315. return WC_TEST_RET_ENC_NC;
  23316. return 0;
  23317. }
  23318. #endif /* HAVE_PKCS12 */
  23319. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23320. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  23321. {
  23322. char passwd[] = "passwordpassword";
  23323. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a,
  23324. 0x5d, 0x63, 0xcb, 0x06 };
  23325. int iterations = 2048;
  23326. int kLen = 24;
  23327. byte derived[64];
  23328. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23329. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  23330. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  23331. };
  23332. wc_test_ret_t ret;
  23333. WOLFSSL_ENTER("pbkdf2_test");
  23334. ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd),
  23335. salt, (int)sizeof(salt), iterations,
  23336. kLen, WC_SHA256, HEAP_HINT, devId);
  23337. if (ret != 0)
  23338. return WC_TEST_RET_ENC_EC(ret);
  23339. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23340. return WC_TEST_RET_ENC_NC;
  23341. return 0;
  23342. }
  23343. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  23344. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23345. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  23346. {
  23347. char passwd[] = "password";
  23348. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  23349. int iterations = 1000;
  23350. int kLen = 16;
  23351. byte derived[16];
  23352. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23353. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  23354. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  23355. };
  23356. wc_test_ret_t ret;
  23357. WOLFSSL_ENTER("pbkdf1_test");
  23358. ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  23359. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  23360. HEAP_HINT);
  23361. if (ret != 0)
  23362. return ret;
  23363. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23364. return WC_TEST_RET_ENC_NC;
  23365. return 0;
  23366. }
  23367. #endif /* HAVE_PBKDF2 && !NO_SHA */
  23368. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  23369. {
  23370. wc_test_ret_t ret = 0;
  23371. WOLFSSL_ENTER("pwdbased_test");
  23372. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23373. ret = pbkdf1_test();
  23374. if (ret != 0)
  23375. return ret;
  23376. #endif
  23377. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23378. ret = pbkdf2_test();
  23379. if (ret != 0)
  23380. return ret;
  23381. #endif
  23382. #ifdef HAVE_PKCS12
  23383. ret = pkcs12_pbkdf_test();
  23384. if (ret != 0)
  23385. return ret;
  23386. #endif
  23387. #ifdef HAVE_SCRYPT
  23388. ret = scrypt_test();
  23389. if (ret != 0)
  23390. return ret;
  23391. #endif
  23392. return ret;
  23393. }
  23394. #endif /* NO_PWDBASED */
  23395. #if defined(USE_CERT_BUFFERS_2048) && \
  23396. defined(HAVE_PKCS12) && \
  23397. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  23398. !defined(NO_CERTS) && !defined(NO_DES3)
  23399. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  23400. {
  23401. wc_test_ret_t ret = 0;
  23402. WC_PKCS12* pkcs12 = NULL;
  23403. /* Gen vars */
  23404. byte* pkcs12der = NULL;
  23405. int pkcs12derSz = 0;
  23406. WC_DerCertList derCaList = {
  23407. (byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
  23408. };
  23409. char* pass = (char*)"wolfSSL test";
  23410. /* Parsing vars */
  23411. WC_DerCertList* derCaListOut = NULL;
  23412. byte* keyDer = NULL;
  23413. byte* certDer = NULL;
  23414. word32 keySz;
  23415. word32 certSz;
  23416. WOLFSSL_ENTER("pkcs12_test");
  23417. pkcs12 = wc_PKCS12_create(pass, (word32)XSTRLEN(pass),
  23418. (char*)"friendlyName" /* not used currently */,
  23419. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  23420. (byte*)server_cert_der_2048, sizeof_server_cert_der_2048,
  23421. &derCaList, PBE_SHA1_DES3, PBE_SHA1_DES3, 100, 100,
  23422. 0 /* not used currently */, HEAP_HINT);
  23423. if (pkcs12 == NULL) {
  23424. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23425. goto out;
  23426. }
  23427. ret = wc_i2d_PKCS12(pkcs12, NULL, &pkcs12derSz);
  23428. if (ret != LENGTH_ONLY_E) {
  23429. if (ret == 0)
  23430. ret = WC_TEST_RET_ENC_NC;
  23431. else
  23432. ret = WC_TEST_RET_ENC_I(ret);
  23433. goto out;
  23434. }
  23435. pkcs12der = (byte*)XMALLOC(pkcs12derSz, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23436. if (pkcs12der == NULL) {
  23437. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23438. goto out;
  23439. }
  23440. {
  23441. /* Use tmp pointer to avoid advancing pkcs12der */
  23442. byte* tmp = pkcs12der;
  23443. ret = wc_i2d_PKCS12(pkcs12, &tmp, &pkcs12derSz);
  23444. if (ret <= 0) {
  23445. if (ret == 0)
  23446. ret = WC_TEST_RET_ENC_NC;
  23447. else
  23448. ret = WC_TEST_RET_ENC_I(ret);
  23449. goto out;
  23450. }
  23451. }
  23452. wc_PKCS12_free(pkcs12);
  23453. pkcs12 = wc_PKCS12_new_ex(HEAP_HINT);
  23454. if (pkcs12 == NULL) {
  23455. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23456. goto out;
  23457. }
  23458. /* convert the DER file into an internal structure */
  23459. ret = wc_d2i_PKCS12(pkcs12der, pkcs12derSz, pkcs12);
  23460. if (ret != 0) {
  23461. ret = WC_TEST_RET_ENC_EC(ret);
  23462. goto out;
  23463. }
  23464. /* parse the internal structure into its parts */
  23465. ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
  23466. &certDer, &certSz, &derCaListOut);
  23467. if (ret != 0) {
  23468. ret = WC_TEST_RET_ENC_EC(ret);
  23469. goto out;
  23470. }
  23471. if (keyDer == NULL || certDer == NULL || derCaListOut == NULL) {
  23472. ret = WC_TEST_RET_ENC_NC;
  23473. goto out;
  23474. }
  23475. out:
  23476. if (derCaListOut)
  23477. wc_FreeCertList(derCaListOut, HEAP_HINT);
  23478. XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23479. XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23480. if (pkcs12)
  23481. wc_PKCS12_free(pkcs12);
  23482. XFREE(pkcs12der, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23483. return ret;
  23484. }
  23485. #endif
  23486. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23487. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  23488. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  23489. defined(WOLFSSL_AFALG_XILINX_RSA)
  23490. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  23491. static wc_test_ret_t hkdf_test(void)
  23492. #else
  23493. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  23494. #endif
  23495. {
  23496. wc_test_ret_t ret = 0;
  23497. #if !defined(NO_SHA) || !defined(NO_SHA256)
  23498. int L;
  23499. byte okm1[42];
  23500. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23501. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23502. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  23503. #ifndef HAVE_FIPS
  23504. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23505. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  23506. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  23507. 0xf8, 0xf9 };
  23508. #endif
  23509. #ifndef NO_SHA
  23510. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  23511. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  23512. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  23513. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  23514. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  23515. 0x49, 0x18 };
  23516. #ifndef HAVE_FIPS
  23517. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  23518. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  23519. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  23520. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  23521. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  23522. 0xf8, 0x96 };
  23523. #endif
  23524. #endif /* !NO_SHA */
  23525. #ifndef NO_SHA256
  23526. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  23527. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  23528. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  23529. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  23530. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  23531. 0x96, 0xc8 };
  23532. #ifndef HAVE_FIPS
  23533. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  23534. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  23535. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  23536. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  23537. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  23538. 0x58, 0x65 };
  23539. #endif
  23540. #endif /* !NO_SHA256 */
  23541. WOLFSSL_ENTER("hkdf_test");
  23542. XMEMSET(okm1, 0, sizeof(okm1));
  23543. L = (int)sizeof(okm1);
  23544. #ifndef NO_SHA
  23545. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23546. okm1, (word32)L);
  23547. if (ret != 0)
  23548. return WC_TEST_RET_ENC_EC(ret);
  23549. if (XMEMCMP(okm1, res1, L) != 0)
  23550. return WC_TEST_RET_ENC_NC;
  23551. #ifndef HAVE_FIPS
  23552. /* fips can't have key size under 14 bytes, salt is key too */
  23553. L = (int)sizeof(okm1);
  23554. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  23555. info1, (word32)sizeof(info1), okm1, (word32)L);
  23556. if (ret != 0)
  23557. return WC_TEST_RET_ENC_EC(ret);
  23558. if (XMEMCMP(okm1, res2, L) != 0)
  23559. return WC_TEST_RET_ENC_NC;
  23560. #endif /* HAVE_FIPS */
  23561. #endif /* !NO_SHA */
  23562. #ifndef NO_SHA256
  23563. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23564. okm1, (word32)L);
  23565. if (ret != 0)
  23566. return WC_TEST_RET_ENC_EC(ret);
  23567. if (XMEMCMP(okm1, res3, L) != 0)
  23568. return WC_TEST_RET_ENC_NC;
  23569. #ifndef HAVE_FIPS
  23570. /* fips can't have key size under 14 bytes, salt is key too */
  23571. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  23572. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, (word32)L);
  23573. if (ret != 0)
  23574. return WC_TEST_RET_ENC_EC(ret);
  23575. if (XMEMCMP(okm1, res4, L) != 0)
  23576. return WC_TEST_RET_ENC_NC;
  23577. #endif /* HAVE_FIPS */
  23578. #endif /* !NO_SHA256 */
  23579. #endif /* !NO_SHA || !NO_SHA256 */
  23580. return ret;
  23581. }
  23582. #endif /* HAVE_HKDF */
  23583. #ifdef WOLFSSL_WOLFSSH
  23584. typedef struct {
  23585. byte hashId;
  23586. byte keyId;
  23587. const byte* k;
  23588. word32 kSz;
  23589. const byte* h;
  23590. word32 hSz;
  23591. const byte* sessionId;
  23592. word32 sessionIdSz;
  23593. const byte* expectedKey;
  23594. word32 expectedKeySz;
  23595. } SshKdfTestVector;
  23596. /** Test Vector Set #3: SHA-256 **/
  23597. static const byte sshKdfTvSet3k[] = {
  23598. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  23599. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  23600. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  23601. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  23602. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  23603. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  23604. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  23605. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  23606. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  23607. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  23608. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  23609. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  23610. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  23611. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  23612. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  23613. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  23614. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  23615. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  23616. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  23617. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  23618. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  23619. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  23620. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  23621. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  23622. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  23623. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  23624. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  23625. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  23626. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  23627. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  23628. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  23629. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  23630. };
  23631. static const byte sshKdfTvSet3h[] = {
  23632. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23633. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23634. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23635. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23636. };
  23637. static const byte sshKdfTvSet3sid[] = {
  23638. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23639. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23640. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23641. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23642. };
  23643. static const byte sshKdfTvSet3a[] = {
  23644. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  23645. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  23646. };
  23647. static const byte sshKdfTvSet3b[] = {
  23648. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  23649. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  23650. };
  23651. static const byte sshKdfTvSet3c[] = {
  23652. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  23653. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  23654. };
  23655. static const byte sshKdfTvSet3d[] = {
  23656. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  23657. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  23658. };
  23659. static const byte sshKdfTvSet3e[] = {
  23660. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  23661. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  23662. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  23663. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  23664. };
  23665. static const byte sshKdfTvSet3f[] = {
  23666. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  23667. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  23668. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  23669. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  23670. };
  23671. static const SshKdfTestVector sshKdfTestVectors[] = {
  23672. {WC_HASH_TYPE_SHA256, 'A',
  23673. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23674. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23675. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23676. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  23677. {WC_HASH_TYPE_SHA256, 'B',
  23678. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23679. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23680. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23681. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  23682. {WC_HASH_TYPE_SHA256, 'C',
  23683. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23684. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23685. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23686. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  23687. {WC_HASH_TYPE_SHA256, 'D',
  23688. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23689. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23690. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23691. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  23692. {WC_HASH_TYPE_SHA256, 'E',
  23693. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23694. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23695. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23696. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  23697. {WC_HASH_TYPE_SHA256, 'F',
  23698. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23699. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23700. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23701. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  23702. };
  23703. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  23704. {
  23705. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  23706. word32 i;
  23707. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  23708. const SshKdfTestVector* tv = NULL;
  23709. wc_test_ret_t result = 0;
  23710. WOLFSSL_ENTER("sshkdf_test");
  23711. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  23712. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  23713. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  23714. cKey, tv->expectedKeySz,
  23715. tv->k, tv->kSz, tv->h, tv->hSz,
  23716. tv->sessionId, tv->sessionIdSz);
  23717. if (result != 0) {
  23718. printf("KDF: Could not derive key.\n");
  23719. result = WC_TEST_RET_ENC_EC(result);
  23720. }
  23721. else {
  23722. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  23723. printf("KDF: Calculated Key does not match Expected Key.\n");
  23724. result = WC_TEST_RET_ENC_EC(result);
  23725. }
  23726. }
  23727. if (result != 0) break;
  23728. }
  23729. return result;
  23730. }
  23731. #endif /* WOLFSSL_WOLFSSH */
  23732. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  23733. #define DIGL 12
  23734. #define SECL 48
  23735. #define LBSL 63
  23736. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void)
  23737. {
  23738. int ret;
  23739. byte dig[DIGL] = {0};
  23740. byte secret[SECL] = {
  23741. 0x10, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c,
  23742. 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f,
  23743. 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71,
  23744. 0xa3, 0x67, 0x76, 0x6b, 0xfa, 0x5d, 0x46, 0x4a, 0xe8,
  23745. 0x61, 0x18, 0x81, 0xc4, 0x66, 0xcc, 0x6f, 0x09, 0x99,
  23746. 0x9d, 0xfc, 0x47
  23747. };
  23748. byte lablSd[LBSL] = {
  23749. 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69,
  23750. 0x6e, 0x69, 0x73, 0x68, 0x65, 0x64, 0x0b, 0x46, 0xba,
  23751. 0x56, 0xbf, 0x1f, 0x5d, 0x99, 0xff, 0xe9, 0xbb, 0x43,
  23752. 0x01, 0xe7, 0xca, 0x2c, 0x00, 0xdf, 0x9a, 0x39, 0x6e,
  23753. 0xcf, 0x6d, 0x15, 0x27, 0x4d, 0xf2, 0x93, 0x96, 0x4a,
  23754. 0x91, 0xde, 0x5c, 0xc0, 0x47, 0x7c, 0xa8, 0xae, 0xcf,
  23755. 0x5d, 0x93, 0x5f, 0x4c, 0x92, 0xcc, 0x98, 0x5b, 0x43
  23756. };
  23757. byte expected[DIGL] = {
  23758. 0xee, 0xcb, 0xb1, 0x30, 0xf2, 0xcd, 0xb3, 0x4a,
  23759. 0xbe, 0xda, 0xc1, 0xf6
  23760. };
  23761. int digL = DIGL;
  23762. int secL = SECL;
  23763. int lblsdL = LBSL;
  23764. int hash_type = sha384_mac;
  23765. ret = wc_PRF(dig, (word32)digL, secret, secL, lablSd, lblsdL, hash_type,
  23766. HEAP_HINT, INVALID_DEVID);
  23767. if (ret != 0) {
  23768. printf("Failed w/ code: %d\n", ret);
  23769. return WC_TEST_RET_ENC_EC(ret);
  23770. }
  23771. if (XMEMCMP(expected, dig, DIGL) != 0) {
  23772. printf("Got unexpected digest\n");
  23773. return WC_TEST_RET_ENC_NC;
  23774. }
  23775. return 0;
  23776. }
  23777. #endif /* WOLFSSL_HAVE_PRF && !NO_HMAC */
  23778. #ifdef WOLFSSL_HAVE_PRF
  23779. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23780. #ifdef WOLFSSL_BASE16
  23781. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void)
  23782. {
  23783. const char* preMasterSecret = "D06F9C19BFF49B1E91E4EFE97345D089"
  23784. "4E6C2E6C34A165B24540E2970875D641"
  23785. "2AA6515871B389B4C199BB8389C71CED";
  23786. const char* helloRandom = "162B81EDFBEAE4F25240320B87E7651C"
  23787. "865564191DD782DB0B9ECA275FBA1BB9"
  23788. "5A1DA3DF436D68DA86C5E7B4B4A36E46"
  23789. "B977C61767983A31BE270D74517BD0F6";
  23790. const char* masterSecret = "EB38B8D89B98B1C266DE44BB3CA14E83"
  23791. "C32F009F9955B1D994E61D3C51EE8760"
  23792. "90B4EF89CC7AF42F46E72201BFCC7977";
  23793. const char* label = "master secret";
  23794. byte pms[48] = {0};
  23795. byte seed[64] = {0};
  23796. byte ms[48] = {0};
  23797. byte result[48] = {0};
  23798. word32 pmsSz = (word32)sizeof(pms);
  23799. word32 seedSz = (word32)sizeof(seed);
  23800. word32 msSz = (word32)sizeof(ms);
  23801. int ret;
  23802. ret = Base16_Decode((const byte*)preMasterSecret,
  23803. (word32)XSTRLEN(preMasterSecret), pms, &pmsSz);
  23804. if (ret != 0)
  23805. return ret;
  23806. ret = Base16_Decode((const byte*)helloRandom,
  23807. (word32)XSTRLEN(helloRandom), seed, &seedSz);
  23808. if (ret != 0)
  23809. return ret;
  23810. ret = Base16_Decode((const byte*)masterSecret,
  23811. (word32)XSTRLEN(masterSecret), ms, &msSz);
  23812. if (ret != 0)
  23813. return ret;
  23814. ret = wc_PRF_TLS(result, msSz, pms, pmsSz,
  23815. (const byte*)label, (word32)XSTRLEN(label), seed, seedSz,
  23816. 1, sha256_mac, NULL, INVALID_DEVID);
  23817. if (ret != 0) {
  23818. if (ret == FIPS_PRIVATE_KEY_LOCKED_E) {
  23819. printf(" wc_PRF_TLSv12: Private key locked.\n");
  23820. }
  23821. return WC_TEST_RET_ENC_NC;
  23822. }
  23823. if (XMEMCMP(result, ms, msSz) != 0)
  23824. return WC_TEST_RET_ENC_NC;
  23825. return 0;
  23826. }
  23827. #endif /* WOLFSSL_BASE16 */
  23828. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  23829. #endif /* WOLFSSL_HAVE_PRF */
  23830. #ifdef WOLFSSL_TLS13
  23831. #define TLSV13_PSK_DHE_SZ 40
  23832. typedef struct {
  23833. enum wc_HashType hashAlg;
  23834. word32 pskSz;
  23835. word32 dheSz;
  23836. byte psk[TLSV13_PSK_DHE_SZ];
  23837. byte dhe[TLSV13_PSK_DHE_SZ];
  23838. byte hashHello1[WC_MAX_DIGEST_SIZE];
  23839. byte hashHello2[WC_MAX_DIGEST_SIZE];
  23840. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  23841. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  23842. /* Expected */
  23843. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  23844. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23845. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23846. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23847. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23848. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23849. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23850. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  23851. } Tls13KdfTestVector;
  23852. /* The following tests come from the CAVP test vectors we used for
  23853. * our FIPS validation. The hash values used are the components from
  23854. * the test hashed together. hashHello1 is the hash of the
  23855. * clientHelloRandom value of the test vector. hashHello2 is the hash
  23856. * of the clientHelloRandom and serverHelloRandom values from the test
  23857. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  23858. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  23859. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  23860. * hashed together. */
  23861. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  23862. { /* 1 */
  23863. WC_HASH_TYPE_SHA256, 35, 35,
  23864. { /* PSK */
  23865. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  23866. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  23867. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  23868. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  23869. 0x7f, 0x6d, 0xbe
  23870. },
  23871. { /* DHE */
  23872. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  23873. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  23874. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  23875. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  23876. 0x81, 0x26, 0x40
  23877. },
  23878. { /* Hello 1 */
  23879. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  23880. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  23881. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  23882. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  23883. },
  23884. { /* Hello 2 */
  23885. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  23886. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  23887. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  23888. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  23889. },
  23890. { /* Finished 1 */
  23891. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  23892. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  23893. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  23894. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  23895. },
  23896. { /* Finished 2 */
  23897. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  23898. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  23899. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  23900. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  23901. },
  23902. { /* Client Early Traffic Secret */
  23903. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  23904. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  23905. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  23906. },
  23907. { /* Early Exporter Master Secret */
  23908. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  23909. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  23910. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  23911. },
  23912. { /* Client Handshake Traffic Secret */
  23913. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  23914. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  23915. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  23916. },
  23917. { /* Server Handshake Traffic Secret */
  23918. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  23919. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  23920. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  23921. },
  23922. { /* Client Application Traffic Secret */
  23923. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  23924. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  23925. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  23926. },
  23927. { /* Server Application Traffic Secret */
  23928. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  23929. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  23930. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  23931. },
  23932. { /* Exporter Master Secret */
  23933. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  23934. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  23935. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  23936. },
  23937. { /* Resumption Master Secret */
  23938. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  23939. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  23940. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  23941. }
  23942. },
  23943. { /* 6 */
  23944. WC_HASH_TYPE_SHA256, 0, 33,
  23945. { 0 }, /* PSK */
  23946. { /* DHE */
  23947. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  23948. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  23949. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  23950. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  23951. 0x18
  23952. },
  23953. { /* Hello 1 */
  23954. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  23955. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  23956. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  23957. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  23958. },
  23959. { /* Hello 2 */
  23960. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  23961. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  23962. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  23963. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  23964. },
  23965. { /* Finished 1 */
  23966. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  23967. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  23968. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  23969. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  23970. },
  23971. { /* Finished 2 */
  23972. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  23973. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  23974. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  23975. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  23976. },
  23977. {
  23978. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  23979. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  23980. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  23981. },
  23982. {
  23983. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  23984. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  23985. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  23986. },
  23987. {
  23988. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  23989. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  23990. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  23991. },
  23992. {
  23993. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  23994. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  23995. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  23996. },
  23997. {
  23998. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  23999. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  24000. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  24001. },
  24002. {
  24003. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  24004. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  24005. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  24006. },
  24007. {
  24008. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  24009. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  24010. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  24011. },
  24012. {
  24013. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  24014. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  24015. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  24016. }
  24017. },
  24018. { /* 11 */
  24019. WC_HASH_TYPE_SHA256, 33, 0,
  24020. { /* PSK */
  24021. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  24022. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  24023. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  24024. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  24025. 0x12
  24026. },
  24027. { 0 }, /* DHE */
  24028. { /* Hello 1 */
  24029. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  24030. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  24031. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  24032. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  24033. },
  24034. { /* Hello 2 */
  24035. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  24036. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  24037. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  24038. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  24039. },
  24040. { /* Finished 1 */
  24041. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  24042. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  24043. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  24044. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  24045. },
  24046. { /* Finished 2 */
  24047. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  24048. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  24049. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  24050. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  24051. },
  24052. {
  24053. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  24054. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  24055. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  24056. {
  24057. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  24058. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  24059. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  24060. {
  24061. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  24062. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  24063. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  24064. {
  24065. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  24066. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  24067. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  24068. {
  24069. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  24070. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  24071. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  24072. {
  24073. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  24074. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  24075. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  24076. {
  24077. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  24078. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  24079. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  24080. {
  24081. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  24082. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  24083. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  24084. },
  24085. #ifdef WOLFSSL_SHA384
  24086. { /* 26 */
  24087. WC_HASH_TYPE_SHA384, 35, 35,
  24088. { /* PSK */
  24089. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  24090. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  24091. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  24092. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  24093. 0x68, 0xba, 0xf9
  24094. },
  24095. { /* DHE */
  24096. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  24097. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  24098. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  24099. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  24100. 0xd8, 0x66, 0xac
  24101. },
  24102. { /* Hello 1 */
  24103. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  24104. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  24105. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  24106. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  24107. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  24108. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  24109. },
  24110. { /* Hello 2 */
  24111. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  24112. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  24113. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  24114. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  24115. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  24116. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  24117. },
  24118. { /* Finished 1 */
  24119. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  24120. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  24121. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  24122. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  24123. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  24124. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  24125. },
  24126. { /* Finished 2 */
  24127. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  24128. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  24129. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  24130. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  24131. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  24132. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  24133. },
  24134. {
  24135. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  24136. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  24137. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  24138. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  24139. },
  24140. {
  24141. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  24142. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  24143. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  24144. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  24145. },
  24146. {
  24147. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  24148. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  24149. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  24150. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  24151. },
  24152. {
  24153. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  24154. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  24155. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  24156. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  24157. },
  24158. {
  24159. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  24160. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  24161. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  24162. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  24163. },
  24164. {
  24165. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  24166. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  24167. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  24168. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  24169. },
  24170. {
  24171. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  24172. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  24173. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  24174. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  24175. },
  24176. {
  24177. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  24178. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  24179. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  24180. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  24181. }
  24182. },
  24183. { /* 36 */
  24184. WC_HASH_TYPE_SHA384, 0, 33,
  24185. { 0 }, /* PSK */
  24186. { /* DHE */
  24187. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  24188. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  24189. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  24190. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  24191. 0xfd
  24192. },
  24193. { /* Hello 1 */
  24194. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  24195. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  24196. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  24197. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  24198. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  24199. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  24200. },
  24201. { /* Hello 2 */
  24202. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  24203. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  24204. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  24205. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  24206. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  24207. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  24208. },
  24209. { /* Finished 1 */
  24210. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  24211. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  24212. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  24213. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  24214. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  24215. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  24216. },
  24217. { /* Finished 2 */
  24218. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  24219. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  24220. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  24221. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  24222. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  24223. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  24224. },
  24225. {
  24226. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  24227. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  24228. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  24229. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  24230. },
  24231. {
  24232. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  24233. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  24234. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  24235. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  24236. },
  24237. {
  24238. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  24239. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  24240. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  24241. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  24242. },
  24243. {
  24244. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  24245. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  24246. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  24247. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  24248. },
  24249. {
  24250. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  24251. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  24252. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  24253. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  24254. },
  24255. {
  24256. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  24257. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  24258. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  24259. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  24260. },
  24261. {
  24262. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  24263. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  24264. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  24265. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  24266. },
  24267. {
  24268. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  24269. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  24270. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  24271. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  24272. }
  24273. },
  24274. { /* 41 */
  24275. WC_HASH_TYPE_SHA384, 33, 0,
  24276. { /* PSK */
  24277. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  24278. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  24279. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  24280. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  24281. 0x12
  24282. },
  24283. { 0 }, /* DHE */
  24284. { /* Hello 1 */
  24285. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  24286. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  24287. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  24288. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  24289. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  24290. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  24291. },
  24292. { /* Hello 2 */
  24293. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  24294. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  24295. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  24296. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  24297. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  24298. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  24299. },
  24300. { /* Finished 1 */
  24301. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  24302. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  24303. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  24304. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  24305. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  24306. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  24307. },
  24308. { /* Finished 2 */
  24309. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  24310. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  24311. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  24312. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  24313. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  24314. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  24315. },
  24316. {
  24317. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  24318. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  24319. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  24320. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  24321. },
  24322. {
  24323. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  24324. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  24325. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  24326. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  24327. },
  24328. {
  24329. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  24330. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  24331. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  24332. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  24333. },
  24334. {
  24335. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  24336. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  24337. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  24338. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  24339. },
  24340. {
  24341. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  24342. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  24343. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  24344. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  24345. },
  24346. {
  24347. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  24348. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  24349. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  24350. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  24351. },
  24352. {
  24353. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  24354. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  24355. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  24356. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  24357. },
  24358. {
  24359. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  24360. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  24361. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  24362. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  24363. }
  24364. },
  24365. #endif /* WOLFSSL_SHA384 */
  24366. };
  24367. static const char protocolLabel[] = "tls13 ";
  24368. static const char ceTrafficLabel[] = "c e traffic";
  24369. static const char eExpMasterLabel[] = "e exp master";
  24370. static const char cHsTrafficLabel[] = "c hs traffic";
  24371. static const char sHsTrafficLabel[] = "s hs traffic";
  24372. static const char cAppTrafficLabel[] = "c ap traffic";
  24373. static const char sAppTrafficLabel[] = "s ap traffic";
  24374. static const char expMasterLabel[] = "exp master";
  24375. static const char resMasterLabel[] = "res master";
  24376. static const char derivedLabel[] = "derived";
  24377. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  24378. {
  24379. wc_test_ret_t ret = 0;
  24380. word32 i;
  24381. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  24382. const Tls13KdfTestVector* tv = NULL;
  24383. WOLFSSL_ENTER("tls13_kdf_test");
  24384. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  24385. byte output[WC_MAX_DIGEST_SIZE];
  24386. byte secret[WC_MAX_DIGEST_SIZE];
  24387. byte salt[WC_MAX_DIGEST_SIZE];
  24388. byte zeroes[WC_MAX_DIGEST_SIZE];
  24389. byte hashZero[WC_MAX_DIGEST_SIZE];
  24390. int hashAlgSz;
  24391. XMEMSET(zeroes, 0, sizeof zeroes);
  24392. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  24393. if (hashAlgSz == BAD_FUNC_ARG) break;
  24394. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, (word32)hashAlgSz);
  24395. if (ret != 0) break;
  24396. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  24397. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  24398. tv->pskSz, tv->hashAlg);
  24399. if (ret != 0) break;
  24400. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24401. secret, (word32)hashAlgSz,
  24402. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24403. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  24404. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24405. if (ret != 0) break;
  24406. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  24407. if (ret != 0) break;
  24408. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24409. secret, (word32)hashAlgSz,
  24410. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24411. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  24412. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24413. if (ret != 0) break;
  24414. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  24415. if (ret != 0) break;
  24416. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24417. secret, (word32)hashAlgSz,
  24418. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24419. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24420. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24421. if (ret != 0) break;
  24422. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24423. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  24424. tv->dheSz, tv->hashAlg);
  24425. if (ret != 0) break;
  24426. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24427. secret, (word32)hashAlgSz,
  24428. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24429. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  24430. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24431. if (ret != 0) break;
  24432. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  24433. output, hashAlgSz);
  24434. if (ret != 0) break;
  24435. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24436. secret, (word32)hashAlgSz,
  24437. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24438. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  24439. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24440. if (ret != 0) break;
  24441. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  24442. if (ret != 0) break;
  24443. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24444. secret, (word32)hashAlgSz,
  24445. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24446. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24447. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24448. if (ret != 0) break;
  24449. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24450. zeroes, (word32)(word32)hashAlgSz, tv->hashAlg);
  24451. if (ret != 0) break;
  24452. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24453. secret, (word32)hashAlgSz,
  24454. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24455. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  24456. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24457. if (ret != 0) break;
  24458. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  24459. if (ret != 0) break;
  24460. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24461. secret, (word32)hashAlgSz,
  24462. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24463. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  24464. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24465. if (ret != 0) break;
  24466. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  24467. if (ret != 0) break;
  24468. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24469. secret, (word32)hashAlgSz,
  24470. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24471. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  24472. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24473. if (ret != 0) break;
  24474. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  24475. if (ret != 0) break;
  24476. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24477. secret, (word32)hashAlgSz,
  24478. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24479. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  24480. tv->hashFinished2, (word32)hashAlgSz, tv->hashAlg);
  24481. if (ret != 0) break;
  24482. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  24483. if (ret != 0) break;
  24484. }
  24485. return ret;
  24486. }
  24487. #endif /* WOLFSSL_TLS13 */
  24488. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  24489. * see print_fiducials() below.
  24490. */
  24491. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  24492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  24493. {
  24494. wc_test_ret_t ret;
  24495. byte kek[128];
  24496. #ifndef NO_SHA
  24497. /* SHA-1, COUNT = 0
  24498. * shared secret length: 192
  24499. * SharedInfo length: 0
  24500. * key data length: 128
  24501. */
  24502. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  24503. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  24504. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  24505. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  24506. };
  24507. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  24508. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  24509. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  24510. };
  24511. #endif
  24512. #ifndef NO_SHA256
  24513. /* SHA-256, COUNT = 3
  24514. * shared secret length: 192
  24515. * SharedInfo length: 0
  24516. * key data length: 128
  24517. */
  24518. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  24519. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  24520. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  24521. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  24522. };
  24523. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  24524. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  24525. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  24526. };
  24527. #endif
  24528. #ifdef WOLFSSL_SHA512
  24529. /* SHA-512, COUNT = 0
  24530. * shared secret length: 192
  24531. * SharedInfo length: 0
  24532. * key data length: 128
  24533. */
  24534. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  24535. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  24536. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  24537. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  24538. };
  24539. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  24540. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  24541. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  24542. };
  24543. /* SHA-512, COUNT = 0
  24544. * shared secret length: 521
  24545. * SharedInfo length: 128
  24546. * key data length: 1024
  24547. */
  24548. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  24549. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  24550. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  24551. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  24552. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  24553. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  24554. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  24555. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  24556. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  24557. 0x5a, 0x2d
  24558. };
  24559. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  24560. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  24561. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  24562. };
  24563. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  24564. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  24565. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  24566. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  24567. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  24568. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  24569. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  24570. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  24571. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  24572. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  24573. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  24574. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  24575. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  24576. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  24577. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  24578. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  24579. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  24580. };
  24581. #endif
  24582. WOLFSSL_ENTER("x963kdf_test");
  24583. #ifndef NO_SHA
  24584. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  24585. kek, sizeof(verify));
  24586. if (ret != 0)
  24587. return WC_TEST_RET_ENC_EC(ret);
  24588. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  24589. return WC_TEST_RET_ENC_NC;
  24590. #endif
  24591. #ifndef NO_SHA256
  24592. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  24593. kek, sizeof(verify2));
  24594. if (ret != 0)
  24595. return WC_TEST_RET_ENC_EC(ret);
  24596. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  24597. return WC_TEST_RET_ENC_NC;
  24598. #endif
  24599. #ifdef WOLFSSL_SHA512
  24600. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  24601. kek, sizeof(verify3));
  24602. if (ret != 0)
  24603. return WC_TEST_RET_ENC_EC(ret);
  24604. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  24605. return WC_TEST_RET_ENC_NC;
  24606. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  24607. sizeof(info4), kek, sizeof(verify4));
  24608. if (ret != 0)
  24609. return WC_TEST_RET_ENC_EC(ret);
  24610. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  24611. return WC_TEST_RET_ENC_NC;
  24612. #endif
  24613. return 0;
  24614. }
  24615. #endif /* HAVE_X963_KDF */
  24616. #if defined(HAVE_HPKE) && \
  24617. (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \
  24618. defined(HAVE_AESGCM)
  24619. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  24620. {
  24621. wc_test_ret_t ret = 0;
  24622. int rngRet = 0;
  24623. WC_RNG rng[1];
  24624. const char* start_text = "this is a test";
  24625. const char* info_text = "info";
  24626. const char* aad_text = "aad";
  24627. byte ciphertext[MAX_HPKE_LABEL_SZ];
  24628. byte plaintext[MAX_HPKE_LABEL_SZ];
  24629. void* receiverKey = NULL;
  24630. void* ephemeralKey = NULL;
  24631. #ifdef WOLFSSL_SMALL_STACK
  24632. byte *pubKey = NULL; /* public key */
  24633. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  24634. #else
  24635. byte pubKey[HPKE_Npk_MAX]; /* public key */
  24636. word16 pubKeySz = (word16)sizeof(pubKey);
  24637. #endif
  24638. rngRet = ret = wc_InitRng(rng);
  24639. if (ret != 0)
  24640. return WC_TEST_RET_ENC_EC(ret);
  24641. #ifdef WOLFSSL_SMALL_STACK
  24642. if (ret == 0) {
  24643. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  24644. DYNAMIC_TYPE_TMP_BUFFER);
  24645. if (pubKey == NULL)
  24646. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  24647. }
  24648. #endif
  24649. /* generate the keys */
  24650. if (ret == 0) {
  24651. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  24652. if (ret != 0)
  24653. ret = WC_TEST_RET_ENC_EC(ret);
  24654. }
  24655. if (ret == 0) {
  24656. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  24657. if (ret != 0)
  24658. ret = WC_TEST_RET_ENC_EC(ret);
  24659. }
  24660. /* seal */
  24661. if (ret == 0) {
  24662. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  24663. (byte*)info_text, (word32)XSTRLEN(info_text),
  24664. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24665. (byte*)start_text, (word32)XSTRLEN(start_text),
  24666. ciphertext);
  24667. if (ret != 0)
  24668. ret = WC_TEST_RET_ENC_EC(ret);
  24669. }
  24670. /* export ephemeral key */
  24671. if (ret == 0) {
  24672. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  24673. if (ret != 0)
  24674. ret = WC_TEST_RET_ENC_EC(ret);
  24675. }
  24676. /* open with exported ephemeral key */
  24677. if (ret == 0) {
  24678. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  24679. (byte*)info_text, (word32)XSTRLEN(info_text),
  24680. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24681. ciphertext, (word32)XSTRLEN(start_text),
  24682. plaintext);
  24683. if (ret != 0)
  24684. ret = WC_TEST_RET_ENC_EC(ret);
  24685. }
  24686. if (ret == 0) {
  24687. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  24688. if (ret != 0)
  24689. ret = WC_TEST_RET_ENC_NC;
  24690. }
  24691. if (ephemeralKey != NULL)
  24692. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  24693. if (receiverKey != NULL)
  24694. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  24695. #ifdef WOLFSSL_SMALL_STACK
  24696. if (pubKey != NULL)
  24697. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24698. #endif
  24699. if (rngRet == 0)
  24700. wc_FreeRng(rng);
  24701. return ret;
  24702. }
  24703. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  24704. {
  24705. wc_test_ret_t ret = 0;
  24706. Hpke hpke[1];
  24707. WOLFSSL_ENTER("hpke_test");
  24708. #if defined(HAVE_ECC)
  24709. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  24710. /* p256 */
  24711. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  24712. HPKE_AES_128_GCM, NULL);
  24713. if (ret != 0)
  24714. return WC_TEST_RET_ENC_EC(ret);
  24715. ret = hpke_test_single(hpke);
  24716. if (ret != 0)
  24717. return ret;
  24718. #endif
  24719. #if defined(WOLFSSL_SHA384) && \
  24720. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  24721. /* p384 */
  24722. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  24723. HPKE_AES_128_GCM, NULL);
  24724. if (ret != 0)
  24725. return WC_TEST_RET_ENC_EC(ret);
  24726. ret = hpke_test_single(hpke);
  24727. if (ret != 0)
  24728. return ret;
  24729. #endif
  24730. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  24731. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  24732. /* p521 */
  24733. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  24734. HPKE_AES_128_GCM, NULL);
  24735. if (ret != 0)
  24736. return WC_TEST_RET_ENC_EC(ret);
  24737. ret = hpke_test_single(hpke);
  24738. if (ret != 0)
  24739. return ret;
  24740. #endif
  24741. #endif
  24742. #if defined(HAVE_CURVE25519)
  24743. /* test with curve25519 and aes256 */
  24744. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  24745. HPKE_AES_256_GCM, NULL);
  24746. if (ret != 0)
  24747. return WC_TEST_RET_ENC_EC(ret);
  24748. ret = hpke_test_single(hpke);
  24749. if (ret != 0)
  24750. return ret;
  24751. #endif
  24752. #if defined(HAVE_CURVE448) && \
  24753. (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
  24754. /* test with curve448 and aes256 */
  24755. ret = wc_HpkeInit(hpke, DHKEM_X448_HKDF_SHA512, HKDF_SHA512,
  24756. HPKE_AES_256_GCM, NULL);
  24757. /* HPKE does not support X448 yet, so expect failure */
  24758. if (ret != BAD_FUNC_ARG)
  24759. return WC_TEST_RET_ENC_EC(ret);
  24760. ret = hpke_test_single(hpke);
  24761. /* HPKE does not support X448 yet, so expect failure */
  24762. if (WC_TEST_RET_DEC_EC(ret) != BAD_FUNC_ARG)
  24763. return ret;
  24764. ret = 0; /* reset error code */
  24765. #endif
  24766. /* TODO: HPKE chacha20 is not implemented */
  24767. return ret;
  24768. }
  24769. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  24770. #if defined(WC_SRTP_KDF)
  24771. typedef struct Srtp_Kdf_Tv {
  24772. const unsigned char* key;
  24773. word32 keySz;
  24774. const unsigned char* salt;
  24775. word32 saltSz;
  24776. int kdfIdx;
  24777. const unsigned char* index;
  24778. const unsigned char* ke;
  24779. const unsigned char* ka;
  24780. const unsigned char* ks;
  24781. const unsigned char* index_c;
  24782. const unsigned char* ke_c;
  24783. const unsigned char* ka_c;
  24784. const unsigned char* ks_c;
  24785. word32 keSz;
  24786. word32 kaSz;
  24787. word32 ksSz;
  24788. } Srtp_Kdf_Tv;
  24789. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  24790. {
  24791. wc_test_ret_t ret = 0;
  24792. /* 128-bit key, kdrIdx = -1 */
  24793. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  24794. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  24795. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  24796. };
  24797. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  24798. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  24799. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  24800. };
  24801. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  24802. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  24803. };
  24804. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  24805. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  24806. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  24807. };
  24808. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  24809. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  24810. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  24811. 0xbd, 0x32, 0x6e, 0xe4
  24812. };
  24813. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  24814. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  24815. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  24816. };
  24817. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  24818. 0x56, 0xf3, 0xf1, 0x97
  24819. };
  24820. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  24821. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  24822. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  24823. };
  24824. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  24825. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  24826. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  24827. 0xc3, 0xd8, 0x77, 0x0e
  24828. };
  24829. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  24830. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  24831. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  24832. };
  24833. /* 192-bit key, kdrIdx = 0 */
  24834. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  24835. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  24836. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  24837. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  24838. };
  24839. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  24840. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  24841. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  24842. };
  24843. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  24844. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  24845. };
  24846. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  24847. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  24848. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  24849. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  24850. };
  24851. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  24852. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  24853. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  24854. 0xd1, 0xd1, 0x47, 0x08
  24855. };
  24856. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  24857. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  24858. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  24859. };
  24860. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  24861. 0x40, 0xbf, 0xd4, 0xa9
  24862. };
  24863. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  24864. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  24865. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  24866. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  24867. };
  24868. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  24869. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  24870. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  24871. 0xf3, 0x3b, 0x8e, 0x29
  24872. };
  24873. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  24874. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  24875. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  24876. };
  24877. /* 256-bit key, kdrIdx = 1 */
  24878. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  24879. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  24880. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  24881. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  24882. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  24883. };
  24884. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  24885. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  24886. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  24887. };
  24888. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  24889. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  24890. };
  24891. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  24892. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  24893. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  24894. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  24895. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  24896. };
  24897. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  24898. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  24899. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  24900. 0x65, 0xc7, 0x50, 0xc1
  24901. };
  24902. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  24903. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  24904. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  24905. };
  24906. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  24907. 0x31, 0x2d, 0x58, 0x15
  24908. };
  24909. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  24910. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  24911. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  24912. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  24913. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  24914. };
  24915. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  24916. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  24917. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  24918. 0x35, 0x78, 0xdf, 0xa4
  24919. };
  24920. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  24921. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  24922. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  24923. };
  24924. /* 128-bit key, kdrIdx = 8 */
  24925. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  24926. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  24927. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  24928. };
  24929. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  24930. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  24931. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  24932. };
  24933. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  24934. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  24935. };
  24936. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  24937. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  24938. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  24939. };
  24940. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  24941. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  24942. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  24943. 0xea, 0x31, 0x24, 0xe6
  24944. };
  24945. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  24946. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  24947. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  24948. };
  24949. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  24950. 0x4a, 0x7d, 0xaa, 0x85
  24951. };
  24952. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  24953. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  24954. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  24955. };
  24956. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  24957. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  24958. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  24959. 0x05, 0x18, 0xe3, 0xed
  24960. };
  24961. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  24962. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  24963. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  24964. };
  24965. /* SRTCP w/ 48-bit idx - KDR 0 (-1) */
  24966. WOLFSSL_SMALL_STACK_STATIC const byte mk48_1[] = {
  24967. 0xFF, 0xB6, 0xCB, 0x09, 0x71, 0x3F, 0x63, 0x4D,
  24968. 0x7F, 0x42, 0xED, 0xA8, 0x12, 0x81, 0x50, 0xE6
  24969. };
  24970. WOLFSSL_SMALL_STACK_STATIC const byte ms48_1[] = {
  24971. 0x1F, 0x04, 0x76, 0xC8, 0x7F, 0x58, 0x23, 0xEF,
  24972. 0xD3, 0x57, 0xB2, 0xBD, 0xF1, 0x32
  24973. };
  24974. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_1[] = {
  24975. 0x00, 0x00, 0x08, 0x56, 0xBC, 0x39
  24976. };
  24977. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_1[] = {
  24978. 0xD2, 0xC3, 0xF3, 0x49, 0x00, 0x1A, 0x18, 0x0F,
  24979. 0xB6, 0x05, 0x5A, 0x5A, 0x67, 0x8E, 0xE5, 0xB2
  24980. };
  24981. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_1[] = {
  24982. 0x8D, 0x54, 0xBE, 0xB5, 0x7B, 0x7F, 0x7A, 0xAB,
  24983. 0xF5, 0x46, 0xCE, 0x5B, 0x45, 0x69, 0x4A, 0x75,
  24984. 0x81, 0x2A, 0xE2, 0xCB
  24985. };
  24986. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_1[] = {
  24987. 0x76, 0x3C, 0x97, 0x6A, 0x45, 0x31, 0xA7, 0x79,
  24988. 0x3C, 0x28, 0x4A, 0xA6, 0x82, 0x03
  24989. };
  24990. /* SRTCP w/ 48-bit idx - KDR 19 */
  24991. WOLFSSL_SMALL_STACK_STATIC const byte mk48_2[] = {
  24992. 0xBD, 0x1D, 0x71, 0x6B, 0xDA, 0x28, 0xE3, 0xFC,
  24993. 0xA5, 0xA0, 0x66, 0x3F, 0x2E, 0x34, 0xA8, 0x58
  24994. };
  24995. WOLFSSL_SMALL_STACK_STATIC const byte ms48_2[] = {
  24996. 0x79, 0x06, 0xE5, 0xAB, 0x5C, 0x2B, 0x1B, 0x69,
  24997. 0xFA, 0xEE, 0xD2, 0x29, 0x57, 0x3C
  24998. };
  24999. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_2[] = {
  25000. 0x00, 0x00, 0x59, 0xD0, 0xC2, 0xE8
  25001. };
  25002. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_2[] = {
  25003. 0xB9, 0xD7, 0xAD, 0xD8, 0x90, 0x94, 0xC2, 0x92,
  25004. 0xA5, 0x04, 0x87, 0xC4, 0x8C, 0xEF, 0xE2, 0xA3
  25005. };
  25006. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_2[] = {
  25007. 0x07, 0xD5, 0xC4, 0xD2, 0x06, 0xFB, 0x63, 0x15,
  25008. 0xC2, 0x9C, 0x7F, 0x55, 0xD1, 0x16, 0x5C, 0xB5,
  25009. 0xB7, 0x44, 0x54, 0xBD
  25010. };
  25011. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_2[] = {
  25012. 0x0C, 0x5E, 0x53, 0xC1, 0xD0, 0x75, 0xAD, 0x65,
  25013. 0xBF, 0x51, 0x74, 0x50, 0x89, 0xD7
  25014. };
  25015. int kdr_48_1 = -1;
  25016. int kdr_48_2 = 19;
  25017. #define SRTP_TV_CNT 4
  25018. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  25019. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  25020. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  25021. 16, 20, 14 },
  25022. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  25023. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  25024. 24, 20, 14 },
  25025. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  25026. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  25027. 32, 20, 14 },
  25028. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  25029. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  25030. 16, 20, 14 },
  25031. };
  25032. int i;
  25033. int idx;
  25034. unsigned char keyE[32];
  25035. unsigned char keyA[20];
  25036. unsigned char keyS[14];
  25037. WOLFSSL_ENTER("srtpkdf_test");
  25038. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  25039. #ifndef WOLFSSL_AES_128
  25040. if (tv[i].keySz == AES_128_KEY_SIZE) {
  25041. continue;
  25042. }
  25043. #endif
  25044. #ifndef WOLFSSL_AES_192
  25045. if (tv[i].keySz == AES_192_KEY_SIZE) {
  25046. continue;
  25047. }
  25048. #endif
  25049. #ifndef WOLFSSL_AES_256
  25050. if (tv[i].keySz == AES_256_KEY_SIZE) {
  25051. continue;
  25052. }
  25053. #endif
  25054. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25055. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25056. keyS, tv[i].ksSz);
  25057. if (ret != 0)
  25058. return WC_TEST_RET_ENC_EC(ret);
  25059. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25060. return WC_TEST_RET_ENC_NC;
  25061. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25062. return WC_TEST_RET_ENC_NC;
  25063. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25064. return WC_TEST_RET_ENC_NC;
  25065. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25066. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_ENCRYPTION,
  25067. keyE, tv[i].keSz);
  25068. if (ret != 0)
  25069. return WC_TEST_RET_ENC_EC(ret);
  25070. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25071. return WC_TEST_RET_ENC_NC;
  25072. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25073. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_MSG_AUTH,
  25074. keyA, tv[i].kaSz);
  25075. if (ret != 0)
  25076. return WC_TEST_RET_ENC_EC(ret);
  25077. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25078. return WC_TEST_RET_ENC_NC;
  25079. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25080. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_SALT, keyS,
  25081. tv[i].ksSz);
  25082. if (ret != 0)
  25083. return WC_TEST_RET_ENC_EC(ret);
  25084. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25085. return WC_TEST_RET_ENC_NC;
  25086. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25087. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25088. keyS, tv[i].ksSz);
  25089. if (ret != 0)
  25090. return WC_TEST_RET_ENC_EC(ret);
  25091. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25092. return WC_TEST_RET_ENC_NC;
  25093. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25094. return WC_TEST_RET_ENC_NC;
  25095. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25096. return WC_TEST_RET_ENC_NC;
  25097. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25098. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c,
  25099. WC_SRTCP_LABEL_ENCRYPTION, keyE, tv[i].keSz);
  25100. if (ret != 0)
  25101. return WC_TEST_RET_ENC_EC(ret);
  25102. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25103. return WC_TEST_RET_ENC_NC;
  25104. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25105. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_MSG_AUTH,
  25106. keyA, tv[i].kaSz);
  25107. if (ret != 0)
  25108. return WC_TEST_RET_ENC_EC(ret);
  25109. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25110. return WC_TEST_RET_ENC_NC;
  25111. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25112. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_SALT,
  25113. keyS, tv[i].ksSz);
  25114. if (ret != 0)
  25115. return WC_TEST_RET_ENC_EC(ret);
  25116. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25117. return WC_TEST_RET_ENC_NC;
  25118. }
  25119. #ifdef WOLFSSL_AES_128
  25120. i = 0;
  25121. #elif defined(WOLFSSL_AES_192)
  25122. i = 1;
  25123. #else
  25124. i = 2;
  25125. #endif
  25126. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25127. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25128. keyS, tv[i].ksSz);
  25129. if (ret != BAD_FUNC_ARG)
  25130. return WC_TEST_RET_ENC_EC(ret);
  25131. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25132. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25133. keyS, tv[i].ksSz);
  25134. if (ret != BAD_FUNC_ARG)
  25135. return WC_TEST_RET_ENC_EC(ret);
  25136. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25137. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25138. keyS, tv[i].ksSz);
  25139. if (ret != BAD_FUNC_ARG)
  25140. return WC_TEST_RET_ENC_EC(ret);
  25141. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25142. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25143. keyS, tv[i].ksSz);
  25144. if (ret != BAD_FUNC_ARG)
  25145. return WC_TEST_RET_ENC_EC(ret);
  25146. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25147. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25148. keyS, tv[i].ksSz);
  25149. if (ret != BAD_FUNC_ARG)
  25150. return WC_TEST_RET_ENC_EC(ret);
  25151. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25152. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25153. keyS, tv[i].ksSz);
  25154. if (ret != BAD_FUNC_ARG)
  25155. return WC_TEST_RET_ENC_EC(ret);
  25156. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25157. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25158. keyS, tv[i].ksSz);
  25159. if (ret != BAD_FUNC_ARG)
  25160. return WC_TEST_RET_ENC_EC(ret);
  25161. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25162. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25163. keyS, tv[i].ksSz);
  25164. if (ret != BAD_FUNC_ARG)
  25165. return WC_TEST_RET_ENC_EC(ret);
  25166. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25167. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25168. keyS, tv[i].ksSz);
  25169. if (ret != BAD_FUNC_ARG)
  25170. return WC_TEST_RET_ENC_EC(ret);
  25171. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25172. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25173. keyS, tv[i].ksSz);
  25174. if (ret != BAD_FUNC_ARG)
  25175. return WC_TEST_RET_ENC_EC(ret);
  25176. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25177. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25178. keyS, tv[i].ksSz);
  25179. if (ret != BAD_FUNC_ARG)
  25180. return WC_TEST_RET_ENC_EC(ret);
  25181. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25182. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25183. keyS, tv[i].ksSz);
  25184. if (ret != BAD_FUNC_ARG)
  25185. return WC_TEST_RET_ENC_EC(ret);
  25186. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25187. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25188. keyS, tv[i].ksSz);
  25189. if (ret != BAD_FUNC_ARG)
  25190. return WC_TEST_RET_ENC_EC(ret);
  25191. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25192. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25193. keyS, tv[i].ksSz);
  25194. if (ret != BAD_FUNC_ARG)
  25195. return WC_TEST_RET_ENC_EC(ret);
  25196. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25197. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25198. keyS, tv[i].ksSz);
  25199. if (ret != 0)
  25200. return WC_TEST_RET_ENC_EC(ret);
  25201. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25202. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25203. keyS, tv[i].ksSz);
  25204. if (ret != 0)
  25205. return WC_TEST_RET_ENC_EC(ret);
  25206. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25207. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25208. keyS, tv[i].ksSz);
  25209. if (ret != 0)
  25210. return WC_TEST_RET_ENC_EC(ret);
  25211. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25212. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25213. keyS, tv[i].ksSz);
  25214. if (ret != 0)
  25215. return WC_TEST_RET_ENC_EC(ret);
  25216. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25217. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25218. NULL, tv[i].ksSz);
  25219. if (ret != 0)
  25220. return WC_TEST_RET_ENC_EC(ret);
  25221. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25222. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25223. NULL, tv[i].ksSz);
  25224. if (ret != 0)
  25225. return WC_TEST_RET_ENC_EC(ret);
  25226. idx = wc_SRTP_KDF_kdr_to_idx(0);
  25227. if (idx != -1)
  25228. return WC_TEST_RET_ENC_NC;
  25229. for (i = 0; i < 32; i++) {
  25230. word32 kdr = 1U << i;
  25231. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  25232. if (idx != i)
  25233. return WC_TEST_RET_ENC_NC;
  25234. }
  25235. /* SRTCP w/ 48-bit IDX, 128-bit key test */
  25236. if (i == 0) {
  25237. ret = wc_SRTCP_KDF_ex(mk48_1, (word32)sizeof(mk48_1),
  25238. ms48_1, (word32)sizeof(ms48_1),
  25239. kdr_48_1, srtcp48idx_1, keyE, tv[i].keSz,
  25240. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25241. WC_SRTCP_48BIT_IDX);
  25242. if (ret != 0)
  25243. return WC_TEST_RET_ENC_EC(ret);
  25244. if (XMEMCMP(keyE, srtcpKe_48_1, tv[i].keSz) != 0)
  25245. return WC_TEST_RET_ENC_NC;
  25246. if (XMEMCMP(keyA, srtcpKa_48_1, tv[i].kaSz) != 0)
  25247. return WC_TEST_RET_ENC_NC;
  25248. if (XMEMCMP(keyS, srtcpKs_48_1, tv[i].ksSz) != 0)
  25249. return WC_TEST_RET_ENC_NC;
  25250. ret = wc_SRTCP_KDF_ex(mk48_2, (word32)sizeof(mk48_2),
  25251. ms48_2, (word32)sizeof(ms48_2),
  25252. kdr_48_2, srtcp48idx_2, keyE, tv[i].keSz,
  25253. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25254. WC_SRTCP_48BIT_IDX);
  25255. if (ret != 0)
  25256. return WC_TEST_RET_ENC_EC(ret);
  25257. if (XMEMCMP(keyE, srtcpKe_48_2, tv[i].keSz) != 0)
  25258. return WC_TEST_RET_ENC_NC;
  25259. if (XMEMCMP(keyA, srtcpKa_48_2, tv[i].kaSz) != 0)
  25260. return WC_TEST_RET_ENC_NC;
  25261. if (XMEMCMP(keyS, srtcpKs_48_2, tv[i].ksSz) != 0)
  25262. return WC_TEST_RET_ENC_NC;
  25263. }
  25264. return 0;
  25265. }
  25266. #endif
  25267. #ifdef HAVE_ECC
  25268. /* size to use for ECC key gen tests */
  25269. #ifndef ECC_KEYGEN_SIZE
  25270. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  25271. #define ECC_KEYGEN_SIZE 32
  25272. #elif defined(HAVE_ECC384)
  25273. #define ECC_KEYGEN_SIZE 48
  25274. #elif defined(HAVE_ECC224)
  25275. #define ECC_KEYGEN_SIZE 28
  25276. #elif defined(HAVE_ECC521)
  25277. #define ECC_KEYGEN_SIZE 66
  25278. #else
  25279. #error No ECC keygen size defined for test
  25280. #endif
  25281. #endif
  25282. #ifdef BENCH_EMBEDDED
  25283. #define ECC_SHARED_SIZE 128
  25284. #else
  25285. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  25286. #endif
  25287. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  25288. #define HAVE_ECC_DETERMINISTIC_K
  25289. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  25290. #else
  25291. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  25292. #endif
  25293. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  25294. #ifdef NO_ECC_SECP
  25295. #define NO_ECC_VECTOR_TEST
  25296. #endif
  25297. #ifndef NO_ECC_VECTOR_TEST
  25298. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  25299. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  25300. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  25301. #define HAVE_ECC_VECTOR_TEST
  25302. #endif
  25303. #endif
  25304. #ifdef HAVE_ECC_VECTOR_TEST
  25305. typedef struct eccVector {
  25306. const char* msg; /* SHA-1 Encoded Message */
  25307. const char* Qx;
  25308. const char* Qy;
  25309. const char* d; /* Private Key */
  25310. const char* R;
  25311. const char* S;
  25312. const char* curveName;
  25313. word32 msgLen;
  25314. word32 keySize;
  25315. #ifndef NO_ASN
  25316. const byte* r;
  25317. word32 rSz;
  25318. const byte* s;
  25319. word32 sSz;
  25320. #endif
  25321. } eccVector;
  25322. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25323. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  25324. {
  25325. wc_test_ret_t ret = 0;
  25326. int verify = 0;
  25327. word32 sigSz;
  25328. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25329. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25330. #else
  25331. ecc_key userA[1];
  25332. #endif
  25333. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25334. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25335. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  25336. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25337. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25338. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25339. #endif
  25340. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25341. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25342. WC_ALLOC_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25343. WC_ALLOC_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25344. WC_ALLOC_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25345. #endif
  25346. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  25347. if (sig == NULL)
  25348. ERROR_OUT(MEMORY_E, done);
  25349. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25350. if (sigRaw == NULL || r == NULL || s == NULL)
  25351. ERROR_OUT(MEMORY_E, done);
  25352. #endif
  25353. #endif
  25354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25355. if (userA == NULL)
  25356. ERROR_OUT(MEMORY_E, done);
  25357. #endif
  25358. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25359. if (ret != 0)
  25360. goto done;
  25361. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  25362. vector->d, vector->curveName);
  25363. if (ret != 0)
  25364. goto done;
  25365. XMEMSET(sig, 0, ECC_SIG_SIZE);
  25366. sigSz = ECC_SIG_SIZE;
  25367. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  25368. if (ret != 0)
  25369. goto done;
  25370. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25371. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  25372. sigRawSz = ECC_SIG_SIZE;
  25373. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  25374. sigRaw, &sigRawSz);
  25375. if (ret != 0)
  25376. goto done;
  25377. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  25378. ret = WC_TEST_RET_ENC_NC;
  25379. goto done;
  25380. }
  25381. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  25382. if (ret != 0)
  25383. goto done;
  25384. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  25385. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  25386. ret = WC_TEST_RET_ENC_NC;
  25387. goto done;
  25388. }
  25389. #endif
  25390. #ifdef HAVE_ECC_VERIFY
  25391. do {
  25392. #if defined(WOLFSSL_ASYNC_CRYPT)
  25393. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25394. #endif
  25395. if (ret == 0)
  25396. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  25397. vector->msgLen, &verify, userA);
  25398. } while (ret == WC_PENDING_E);
  25399. if (ret != 0)
  25400. goto done;
  25401. TEST_SLEEP();
  25402. if (verify != 1)
  25403. ret = WC_TEST_RET_ENC_NC;
  25404. #endif
  25405. done:
  25406. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25407. if (userA != NULL) {
  25408. wc_ecc_free(userA);
  25409. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25410. }
  25411. #else
  25412. wc_ecc_free(userA);
  25413. #endif
  25414. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25415. WC_FREE_VAR(sigRaw, HEAP_HINT);
  25416. WC_FREE_VAR(r, HEAP_HINT);
  25417. WC_FREE_VAR(s, HEAP_HINT);
  25418. #endif
  25419. WC_FREE_VAR(sig, HEAP_HINT);
  25420. return ret;
  25421. }
  25422. static wc_test_ret_t ecc_test_vector(int keySize)
  25423. {
  25424. wc_test_ret_t ret;
  25425. eccVector vec;
  25426. XMEMSET(&vec, 0, sizeof(vec));
  25427. vec.keySize = (word32)keySize;
  25428. switch(keySize) {
  25429. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  25430. case 14:
  25431. return 0;
  25432. #endif /* HAVE_ECC112 */
  25433. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  25434. case 16:
  25435. return 0;
  25436. #endif /* HAVE_ECC128 */
  25437. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  25438. case 20:
  25439. return 0;
  25440. #endif /* HAVE_ECC160 */
  25441. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  25442. case 24:
  25443. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  25444. #if 1
  25445. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  25446. vec.msgLen = 20;
  25447. #else
  25448. /* This is the raw message prior to SHA-1 */
  25449. vec.msg =
  25450. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  25451. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  25452. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  25453. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  25454. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  25455. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  25456. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  25457. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  25458. vec.msgLen = 128;
  25459. #endif
  25460. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  25461. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  25462. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  25463. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  25464. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  25465. vec.curveName = "SECP192R1";
  25466. #ifndef NO_ASN
  25467. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  25468. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  25469. vec.rSz = 24;
  25470. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  25471. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  25472. vec.sSz = 24;
  25473. #endif
  25474. break;
  25475. #endif /* HAVE_ECC192 */
  25476. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  25477. case 28:
  25478. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  25479. #if 1
  25480. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  25481. vec.msgLen = 20;
  25482. #else
  25483. /* This is the raw message prior to SHA-1 */
  25484. vec.msg =
  25485. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  25486. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  25487. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  25488. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  25489. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  25490. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  25491. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  25492. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  25493. vec.msgLen = 128;
  25494. #endif
  25495. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  25496. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  25497. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  25498. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  25499. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  25500. vec.curveName = "SECP224R1";
  25501. #ifndef NO_ASN
  25502. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  25503. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  25504. "\xbc\x16\x71\xa7";
  25505. vec.rSz = 28;
  25506. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  25507. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  25508. "\x6a\xf3\xad\x5b";
  25509. vec.sSz = 28;
  25510. #endif
  25511. break;
  25512. #endif /* HAVE_ECC224 */
  25513. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  25514. case 30:
  25515. return 0;
  25516. #endif /* HAVE_ECC239 */
  25517. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  25518. case 32:
  25519. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  25520. #if 1
  25521. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  25522. vec.msgLen = 20;
  25523. #else
  25524. /* This is the raw message prior to SHA-1 */
  25525. vec.msg =
  25526. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  25527. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  25528. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  25529. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  25530. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  25531. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  25532. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  25533. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  25534. vec.msgLen = 128;
  25535. #endif
  25536. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25537. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25538. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25539. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  25540. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  25541. #ifndef NO_ASN
  25542. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  25543. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  25544. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  25545. vec.rSz = 32;
  25546. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  25547. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  25548. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  25549. vec.sSz = 32;
  25550. #endif
  25551. vec.curveName = "SECP256R1";
  25552. break;
  25553. #endif /* !NO_ECC256 */
  25554. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  25555. case 40:
  25556. return 0;
  25557. #endif /* HAVE_ECC320 */
  25558. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  25559. case 48:
  25560. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  25561. #if 1
  25562. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  25563. vec.msgLen = 20;
  25564. #else
  25565. /* This is the raw message prior to SHA-1 */
  25566. vec.msg =
  25567. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  25568. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  25569. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  25570. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  25571. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  25572. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  25573. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  25574. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  25575. vec.msgLen = 128;
  25576. #endif
  25577. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  25578. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  25579. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  25580. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  25581. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  25582. vec.curveName = "SECP384R1";
  25583. #ifndef NO_ASN
  25584. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  25585. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  25586. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  25587. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  25588. vec.rSz = 48;
  25589. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  25590. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  25591. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  25592. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  25593. vec.sSz = 48;
  25594. #endif
  25595. break;
  25596. #endif /* HAVE_ECC384 */
  25597. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  25598. case 64:
  25599. return 0;
  25600. #endif /* HAVE_ECC512 */
  25601. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  25602. case 66:
  25603. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  25604. #if 1
  25605. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  25606. vec.msgLen = 20;
  25607. #else
  25608. /* This is the raw message prior to SHA-1 */
  25609. vec.msg =
  25610. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  25611. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  25612. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  25613. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  25614. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  25615. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  25616. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  25617. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  25618. vec.msgLen = 128;
  25619. #endif
  25620. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  25621. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  25622. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  25623. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  25624. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  25625. vec.curveName = "SECP521R1";
  25626. #ifndef NO_ASN
  25627. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  25628. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  25629. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  25630. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  25631. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  25632. "\xa3\xbb\x15\x21\xbe";
  25633. vec.rSz = 65;
  25634. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  25635. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  25636. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  25637. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  25638. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  25639. "\x3d\x22\xf2\x48\x0c";
  25640. vec.sSz = 65;
  25641. #endif
  25642. break;
  25643. #endif /* HAVE_ECC521 */
  25644. default:
  25645. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  25646. }; /* Switch */
  25647. ret = ecc_test_vector_item(&vec);
  25648. if (ret < 0) {
  25649. return ret;
  25650. }
  25651. return 0;
  25652. }
  25653. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25654. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25655. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  25656. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  25657. #if defined(HAVE_ECC256)
  25658. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  25659. {
  25660. wc_test_ret_t ret;
  25661. #ifdef WOLFSSL_SMALL_STACK
  25662. ecc_key *key = NULL;
  25663. #else
  25664. ecc_key key[1];
  25665. #endif
  25666. int key_inited = 0;
  25667. byte sig[72];
  25668. word32 sigSz;
  25669. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25670. unsigned char hash[32];
  25671. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25672. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  25673. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25674. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  25675. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25676. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  25677. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  25678. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  25679. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  25680. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  25681. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  25682. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  25683. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  25684. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  25685. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  25686. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  25687. };
  25688. #ifdef WOLFSSL_SMALL_STACK
  25689. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25690. if (key == NULL)
  25691. return MEMORY_E;
  25692. #endif
  25693. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25694. if (ret != 0) {
  25695. goto done;
  25696. }
  25697. key_inited = 1;
  25698. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  25699. if (ret != 0) {
  25700. goto done;
  25701. }
  25702. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25703. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25704. if (ret != 0) {
  25705. goto done;
  25706. }
  25707. ret = wc_ecc_set_deterministic(key, 1);
  25708. if (ret != 0) {
  25709. goto done;
  25710. }
  25711. sigSz = sizeof(sig);
  25712. do {
  25713. #if defined(WOLFSSL_ASYNC_CRYPT)
  25714. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25715. #endif
  25716. if (ret == 0)
  25717. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25718. } while (ret == WC_PENDING_E);
  25719. if (ret != 0) {
  25720. goto done;
  25721. }
  25722. TEST_SLEEP();
  25723. if (sigSz != sizeof(expSig)) {
  25724. ret = WC_TEST_RET_ENC_NC;
  25725. goto done;
  25726. }
  25727. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  25728. ret = WC_TEST_RET_ENC_NC;
  25729. goto done;
  25730. }
  25731. sigSz = sizeof(sig);
  25732. do {
  25733. #if defined(WOLFSSL_ASYNC_CRYPT)
  25734. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25735. #endif
  25736. if (ret == 0)
  25737. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25738. } while (ret == WC_PENDING_E);
  25739. if (ret != 0) {
  25740. goto done;
  25741. }
  25742. TEST_SLEEP();
  25743. done:
  25744. if (key_inited)
  25745. wc_ecc_free(key);
  25746. #ifdef WOLFSSL_SMALL_STACK
  25747. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25748. #endif
  25749. return ret;
  25750. }
  25751. #endif
  25752. #ifdef WOLFSSL_PUBLIC_MP
  25753. #if defined(HAVE_ECC384)
  25754. /* KAT from RFC6979 */
  25755. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  25756. {
  25757. wc_test_ret_t ret;
  25758. #ifdef WOLFSSL_SMALL_STACK
  25759. ecc_key *key;
  25760. mp_int *r, *s, *expR, *expS;
  25761. #else
  25762. ecc_key key[1];
  25763. mp_int r[1], s[1], expR[1], expS[1];
  25764. #endif
  25765. int key_inited = 0;
  25766. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25767. unsigned char hash[32];
  25768. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25769. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  25770. "96D5724E4C70A825F872C9EA60D2EDF5";
  25771. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25772. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  25773. "DEF8F0EA9055866064A254515480BC13";
  25774. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25775. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  25776. "288B231C3AE0D4FE7344FD2533264720";
  25777. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25778. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  25779. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  25780. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25781. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  25782. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  25783. #ifdef WOLFSSL_SMALL_STACK
  25784. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25785. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25786. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25787. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25788. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25789. if ((key == NULL) ||
  25790. (r == NULL) ||
  25791. (s == NULL) ||
  25792. (expR == NULL) ||
  25793. (expS == NULL))
  25794. {
  25795. ret = MEMORY_E;
  25796. goto done;
  25797. }
  25798. #endif
  25799. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25800. if (ret != MP_OKAY) {
  25801. goto done;
  25802. }
  25803. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25804. if (ret != 0) {
  25805. goto done;
  25806. }
  25807. key_inited = 1;
  25808. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  25809. if (ret != 0) {
  25810. goto done;
  25811. }
  25812. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25813. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25814. if (ret != 0) {
  25815. goto done;
  25816. }
  25817. ret = wc_ecc_set_deterministic(key, 1);
  25818. if (ret != 0) {
  25819. goto done;
  25820. }
  25821. do {
  25822. #if defined(WOLFSSL_ASYNC_CRYPT)
  25823. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25824. #endif
  25825. if (ret == 0)
  25826. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  25827. } while (ret == WC_PENDING_E);
  25828. if (ret != 0) {
  25829. goto done;
  25830. }
  25831. TEST_SLEEP();
  25832. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  25833. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  25834. if (mp_cmp(r, expR) != MP_EQ) {
  25835. ret = WC_TEST_RET_ENC_NC;
  25836. }
  25837. done:
  25838. if (key_inited)
  25839. wc_ecc_free(key);
  25840. #ifdef WOLFSSL_SMALL_STACK
  25841. if (key != NULL)
  25842. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25843. if (r != NULL)
  25844. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25845. if (s != NULL)
  25846. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25847. if (expR != NULL)
  25848. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25849. if (expS != NULL)
  25850. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25851. #endif
  25852. return ret;
  25853. }
  25854. #endif /* HAVE_ECC384 */
  25855. #if defined(HAVE_ECC521)
  25856. /* KAT from RFC6979 */
  25857. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  25858. {
  25859. wc_test_ret_t ret;
  25860. #ifdef WOLFSSL_SMALL_STACK
  25861. ecc_key *key;
  25862. mp_int *r, *s, *expR, *expS;
  25863. #else
  25864. ecc_key key[1];
  25865. mp_int r[1], s[1], expR[1], expS[1];
  25866. #endif
  25867. int key_inited = 0;
  25868. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25869. unsigned char hash[32];
  25870. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25871. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  25872. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  25873. "538";
  25874. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25875. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  25876. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  25877. "3A4";
  25878. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25879. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  25880. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  25881. "CF5";
  25882. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25883. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  25884. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  25885. "1A7";
  25886. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25887. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  25888. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  25889. "CFC";
  25890. #ifdef WOLFSSL_SMALL_STACK
  25891. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25892. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25893. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25894. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25895. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25896. if ((key == NULL) ||
  25897. (r == NULL) ||
  25898. (s == NULL) ||
  25899. (expR == NULL) ||
  25900. (expS == NULL))
  25901. {
  25902. ret = MEMORY_E;
  25903. goto done;
  25904. }
  25905. #endif
  25906. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25907. if (ret != MP_OKAY) {
  25908. goto done;
  25909. }
  25910. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25911. if (ret != 0) {
  25912. return WC_TEST_RET_ENC_EC(ret);
  25913. }
  25914. key_inited = 1;
  25915. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  25916. if (ret != 0) {
  25917. goto done;
  25918. }
  25919. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25920. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25921. if (ret != 0) {
  25922. goto done;
  25923. }
  25924. ret = wc_ecc_set_deterministic(key, 1);
  25925. if (ret != 0) {
  25926. goto done;
  25927. }
  25928. do {
  25929. #if defined(WOLFSSL_ASYNC_CRYPT)
  25930. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25931. #endif
  25932. if (ret == 0)
  25933. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  25934. } while (ret == WC_PENDING_E);
  25935. if (ret != 0) {
  25936. goto done;
  25937. }
  25938. TEST_SLEEP();
  25939. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  25940. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  25941. if (mp_cmp(r, expR) != MP_EQ) {
  25942. ret = WC_TEST_RET_ENC_NC;
  25943. }
  25944. done:
  25945. if (key_inited)
  25946. wc_ecc_free(key);
  25947. #ifdef WOLFSSL_SMALL_STACK
  25948. if (key != NULL)
  25949. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25950. if (r != NULL)
  25951. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25952. if (s != NULL)
  25953. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25954. if (expR != NULL)
  25955. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25956. if (expS != NULL)
  25957. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25958. #endif
  25959. return ret;
  25960. }
  25961. #endif /* HAVE_ECC521 */
  25962. #endif /* WOLFSSL_PUBLIC_MP */
  25963. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  25964. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  25965. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  25966. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  25967. !defined(WOLFSSL_KCAPI_ECC)
  25968. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  25969. {
  25970. wc_test_ret_t ret;
  25971. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25972. ecc_key *key = NULL;
  25973. #else
  25974. ecc_key key[1];
  25975. #endif
  25976. int key_inited = 0;
  25977. byte sig[72];
  25978. word32 sigSz;
  25979. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  25980. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  25981. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  25982. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  25983. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  25984. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  25985. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  25986. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  25987. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  25988. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  25989. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  25990. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  25991. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  25992. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  25993. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  25994. };
  25995. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25996. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  25997. return MEMORY_E;
  25998. #endif
  25999. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26000. if (ret != 0) {
  26001. goto done;
  26002. }
  26003. key_inited = 1;
  26004. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26005. if (ret != 0) {
  26006. goto done;
  26007. }
  26008. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  26009. && (HAVE_FIPS_VERSION > 2)))
  26010. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  26011. #endif
  26012. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  26013. if (ret != 0) {
  26014. goto done;
  26015. }
  26016. sigSz = sizeof(sig);
  26017. do {
  26018. #if defined(WOLFSSL_ASYNC_CRYPT)
  26019. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26020. #endif
  26021. if (ret == 0)
  26022. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26023. } while (ret == WC_PENDING_E);
  26024. if (ret != 0) {
  26025. goto done;
  26026. }
  26027. TEST_SLEEP();
  26028. if (sigSz != sizeof(expSig)) {
  26029. ret = WC_TEST_RET_ENC_NC;
  26030. goto done;
  26031. }
  26032. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  26033. ret = WC_TEST_RET_ENC_NC;
  26034. goto done;
  26035. }
  26036. sigSz = sizeof(sig);
  26037. do {
  26038. #if defined(WOLFSSL_ASYNC_CRYPT)
  26039. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26040. #endif
  26041. if (ret == 0)
  26042. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26043. } while (ret == WC_PENDING_E);
  26044. if (ret != 0) {
  26045. goto done;
  26046. }
  26047. TEST_SLEEP();
  26048. done:
  26049. if (key_inited)
  26050. wc_ecc_free(key);
  26051. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26052. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26053. #endif
  26054. return ret;
  26055. }
  26056. #endif
  26057. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  26058. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  26059. {
  26060. wc_test_ret_t ret;
  26061. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26062. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26063. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26064. #else
  26065. ecc_key pub_key[1], priv_key[1];
  26066. #endif
  26067. byte sharedA[32] = {0}, sharedB[32] = {0};
  26068. word32 x, z;
  26069. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  26070. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  26071. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26072. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26073. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26074. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  26075. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26076. if ((pub_key == NULL) ||
  26077. (priv_key == NULL)) {
  26078. ret = MEMORY_E;
  26079. goto done;
  26080. }
  26081. #endif
  26082. XMEMSET(pub_key, 0, sizeof *pub_key);
  26083. XMEMSET(priv_key, 0, sizeof *priv_key);
  26084. /* setup private and public keys */
  26085. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  26086. if (ret != 0)
  26087. goto done;
  26088. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  26089. if (ret != 0)
  26090. goto done;
  26091. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  26092. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  26093. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  26094. if (ret != 0)
  26095. goto done;
  26096. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26097. if (ret != 0)
  26098. goto done;
  26099. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26100. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26101. !defined(HAVE_SELFTEST)
  26102. ret = wc_ecc_set_rng(priv_key, rng);
  26103. if (ret != 0)
  26104. goto done;
  26105. #else
  26106. (void)rng;
  26107. #endif
  26108. /* compute ECC Cofactor shared secret */
  26109. x = sizeof(sharedA);
  26110. do {
  26111. #if defined(WOLFSSL_ASYNC_CRYPT)
  26112. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26113. #endif
  26114. if (ret == 0)
  26115. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  26116. } while (ret == WC_PENDING_E);
  26117. if (ret != 0) {
  26118. goto done;
  26119. }
  26120. TEST_SLEEP();
  26121. /* read in expected Z */
  26122. z = sizeof(sharedB);
  26123. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  26124. if (ret != 0)
  26125. goto done;
  26126. /* compare results */
  26127. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  26128. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26129. }
  26130. done:
  26131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26132. if (priv_key) {
  26133. wc_ecc_free(priv_key);
  26134. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26135. }
  26136. if (pub_key) {
  26137. wc_ecc_free(pub_key);
  26138. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26139. }
  26140. #else
  26141. wc_ecc_free(priv_key);
  26142. wc_ecc_free(pub_key);
  26143. #endif
  26144. return ret;
  26145. }
  26146. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  26147. #endif /* HAVE_ECC_VECTOR_TEST */
  26148. #ifdef HAVE_ECC_KEY_IMPORT
  26149. /* returns 0 on success */
  26150. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  26151. {
  26152. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26153. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26154. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26155. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26156. #endif
  26157. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26158. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26159. #else
  26160. ecc_key key[1];
  26161. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26162. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26163. ecc_key pub[1];
  26164. #endif
  26165. byte exportBuf[ECC_BUFSIZE];
  26166. byte tmp[ECC_BUFSIZE];
  26167. #endif
  26168. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  26169. word32 x;
  26170. word32 tmpSz;
  26171. wc_test_ret_t ret = 0;
  26172. ecc_point* pubPoint = NULL;
  26173. #ifdef HAVE_ECC_VERIFY
  26174. int verify = 0;
  26175. #endif
  26176. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26177. if ((key == NULL) ||
  26178. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26179. (pub == NULL) ||
  26180. #endif
  26181. (exportBuf == NULL) ||
  26182. (tmp == NULL))
  26183. ERROR_OUT(MEMORY_E, done);
  26184. #endif
  26185. (void)msg;
  26186. (void)verify;
  26187. (void)exportBuf;
  26188. (void)rng;
  26189. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26190. #ifndef NO_ECC256
  26191. #if defined(USE_CERT_BUFFERS_256)
  26192. {
  26193. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  26194. tmpSz = (size_t)sizeof_ecc_key_der_256;
  26195. }
  26196. #elif !defined(NO_FILESYSTEM)
  26197. {
  26198. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  26199. if (!file) {
  26200. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26201. }
  26202. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  26203. XFCLOSE(file);
  26204. if (tmpSz == 0)
  26205. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26206. }
  26207. #else
  26208. {
  26209. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
  26210. ERROR_OUT(ASN_PARSE_E, done);
  26211. }
  26212. #endif /* USE_CERT_BUFFERS_256 */
  26213. /* import private only then test with */
  26214. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  26215. if (ret == 0) {
  26216. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26217. }
  26218. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  26219. if (ret == 0) {
  26220. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26221. }
  26222. x = 0;
  26223. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  26224. if (ret != 0)
  26225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26226. #ifdef HAVE_ECC_KEY_EXPORT
  26227. x = ECC_BUFSIZE;
  26228. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26229. if (ret != 0)
  26230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26231. /* make private only key */
  26232. wc_ecc_free(key);
  26233. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26234. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26235. if (ret != 0)
  26236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26237. x = ECC_BUFSIZE;
  26238. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26239. if (ret == 0) {
  26240. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26241. }
  26242. #endif /* HAVE_ECC_KEY_EXPORT */
  26243. ret = wc_ecc_make_pub(NULL, NULL);
  26244. if (ret == 0) {
  26245. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26246. }
  26247. TEST_SLEEP();
  26248. #ifndef WOLFSSL_NO_MALLOC
  26249. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  26250. if (pubPoint == NULL) {
  26251. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26252. }
  26253. #if !defined(WOLFSSL_CRYPTOCELL)
  26254. ret = wc_ecc_make_pub(key, pubPoint);
  26255. #if defined(WOLFSSL_ASYNC_CRYPT)
  26256. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26257. #endif
  26258. if (ret != 0)
  26259. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26260. #endif
  26261. TEST_SLEEP();
  26262. #ifdef HAVE_ECC_KEY_EXPORT
  26263. /* export should still fail, is private only key */
  26264. x = ECC_BUFSIZE;
  26265. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26266. if (ret == 0) {
  26267. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26268. }
  26269. #endif /* HAVE_ECC_KEY_EXPORT */
  26270. #endif /* !WOLFSSL_NO_MALLOC */
  26271. #endif /* !NO_ECC256 */
  26272. /* create a new key since above test for loading key is not supported */
  26273. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  26274. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  26275. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  26276. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26277. if (ret != 0)
  26278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26279. #endif
  26280. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  26281. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  26282. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  26283. tmpSz = ECC_BUFSIZE;
  26284. ret = 0;
  26285. do {
  26286. #if defined(WOLFSSL_ASYNC_CRYPT)
  26287. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26288. #endif
  26289. if (ret == 0) {
  26290. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  26291. &tmpSz, rng, key);
  26292. }
  26293. } while (ret == WC_PENDING_E);
  26294. if (ret != 0)
  26295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26296. TEST_SLEEP();
  26297. #ifdef HAVE_ECC_VERIFY
  26298. /* try verify with private only key */
  26299. ret = 0;
  26300. do {
  26301. #if defined(WOLFSSL_ASYNC_CRYPT)
  26302. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26303. #endif
  26304. if (ret == 0) {
  26305. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  26306. (word32)XSTRLEN((const char*)msg), &verify, key);
  26307. }
  26308. } while (ret == WC_PENDING_E);
  26309. if (ret != 0)
  26310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26311. if (verify != 1) {
  26312. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26313. }
  26314. TEST_SLEEP();
  26315. #ifdef HAVE_ECC_KEY_EXPORT
  26316. /* exporting the public part should now work */
  26317. x = ECC_BUFSIZE;
  26318. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26319. if (ret != 0)
  26320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26321. #endif /* HAVE_ECC_KEY_EXPORT */
  26322. #endif /* HAVE_ECC_VERIFY */
  26323. #endif /* HAVE_ECC_SIGN */
  26324. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  26325. /* now test private only key with creating a shared secret */
  26326. x = ECC_BUFSIZE;
  26327. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26328. if (ret != 0)
  26329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26330. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  26331. /* make private only key */
  26332. wc_ecc_free(key);
  26333. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26334. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26335. if (ret != 0)
  26336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26337. /* check that public export fails with private only key */
  26338. x = ECC_BUFSIZE;
  26339. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26340. if (ret == 0) {
  26341. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26342. }
  26343. #endif /* WOLFSSL_QNX_CAAM */
  26344. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  26345. /* make public key for shared secret */
  26346. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  26347. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  26348. #ifdef HAVE_ECC_CDH
  26349. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  26350. #endif
  26351. #if defined(WOLFSSL_ASYNC_CRYPT)
  26352. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  26353. #endif
  26354. if (ret != 0)
  26355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26356. TEST_SLEEP();
  26357. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26358. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26359. !defined(HAVE_SELFTEST)
  26360. ret = wc_ecc_set_rng(key, rng);
  26361. if (ret != 0)
  26362. goto done;
  26363. #endif
  26364. x = ECC_BUFSIZE;
  26365. do {
  26366. #if defined(WOLFSSL_ASYNC_CRYPT)
  26367. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26368. #endif
  26369. if (ret == 0) {
  26370. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  26371. }
  26372. } while (ret == WC_PENDING_E);
  26373. wc_ecc_free(pub);
  26374. if (ret != 0)
  26375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26376. TEST_SLEEP();
  26377. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  26378. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  26379. ret = 0;
  26380. done:
  26381. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  26382. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26383. if (key != NULL) {
  26384. wc_ecc_free(key);
  26385. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26386. }
  26387. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26388. if (pub != NULL)
  26389. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26390. #endif
  26391. if (exportBuf != NULL)
  26392. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26393. if (tmp != NULL)
  26394. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26395. #else
  26396. wc_ecc_free(key);
  26397. #endif
  26398. return ret;
  26399. }
  26400. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26401. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26402. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  26403. {
  26404. wc_test_ret_t ret;
  26405. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26406. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26407. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26408. #else
  26409. ecc_key eccKey[1];
  26410. byte tmpBuf[ECC_BUFSIZE];
  26411. #endif
  26412. word32 tmpSz;
  26413. word32 idx;
  26414. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26415. if ((eccKey == NULL) || (tmpBuf == NULL))
  26416. ERROR_OUT(MEMORY_E, done);
  26417. #endif
  26418. ret = wc_ecc_init(eccKey);
  26419. if (ret != 0) {
  26420. goto done;
  26421. }
  26422. ret = wc_ecc_make_key(rng, keySize, eccKey);
  26423. #if defined(WOLFSSL_ASYNC_CRYPT)
  26424. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  26425. #endif
  26426. if (ret != 0) {
  26427. goto done;
  26428. }
  26429. tmpSz = ECC_BUFSIZE;
  26430. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  26431. wc_ecc_free(eccKey);
  26432. if (ret < 0) {
  26433. goto done;
  26434. }
  26435. tmpSz = (word32)ret;
  26436. ret = wc_ecc_init(eccKey);
  26437. if (ret != 0) {
  26438. goto done;
  26439. }
  26440. idx = 0;
  26441. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26442. if (ret != 0) {
  26443. goto done;
  26444. }
  26445. wc_ecc_free(eccKey);
  26446. ret = wc_ecc_init(eccKey);
  26447. if (ret != 0) {
  26448. goto done;
  26449. }
  26450. idx = 0;
  26451. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26452. if (ret != 0) {
  26453. goto done;
  26454. }
  26455. ret = 0;
  26456. done:
  26457. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26458. if (eccKey != NULL) {
  26459. wc_ecc_free(eccKey);
  26460. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26461. }
  26462. if (tmpBuf != NULL)
  26463. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26464. #else
  26465. wc_ecc_free(eccKey);
  26466. #endif
  26467. return ret;
  26468. }
  26469. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26470. #endif /* HAVE_ECC_KEY_IMPORT */
  26471. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26472. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26473. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  26474. {
  26475. wc_test_ret_t ret = 0;
  26476. int derSz;
  26477. #ifdef HAVE_PKCS8
  26478. word32 pkcs8Sz;
  26479. #endif
  26480. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26481. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26482. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26483. #else
  26484. byte der[ECC_BUFSIZE];
  26485. ecc_key userA[1];
  26486. #endif
  26487. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26488. if ((der == NULL) || (userA == NULL))
  26489. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  26490. #endif
  26491. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26492. if (ret != 0)
  26493. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26494. ret = wc_ecc_make_key(rng, keySize, userA);
  26495. #if defined(WOLFSSL_ASYNC_CRYPT)
  26496. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26497. #endif
  26498. if (ret != 0)
  26499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26500. TEST_SLEEP();
  26501. ret = wc_ecc_check_key(userA);
  26502. if (ret != 0)
  26503. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26504. TEST_SLEEP();
  26505. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  26506. if (derSz < 0) {
  26507. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26508. }
  26509. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  26510. ECC_PRIVATEKEY_TYPE);
  26511. if (ret != 0)
  26512. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26513. /* test export of public key */
  26514. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  26515. if (derSz < 0) {
  26516. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26517. }
  26518. if (derSz == 0) {
  26519. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26520. }
  26521. #ifdef HAVE_COMP_KEY
  26522. /* test export of compressed public key */
  26523. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  26524. if (derSz < 0) {
  26525. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26526. }
  26527. if (derSz == 0) {
  26528. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26529. }
  26530. #endif
  26531. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  26532. if (ret != 0)
  26533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26534. #ifdef HAVE_PKCS8
  26535. /* test export of PKCS#8 unencrypted private key */
  26536. pkcs8Sz = FOURK_BUF;
  26537. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  26538. if (derSz < 0) {
  26539. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26540. }
  26541. if (derSz == 0) {
  26542. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26543. }
  26544. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  26545. if (ret != 0) {
  26546. goto done;
  26547. }
  26548. #endif /* HAVE_PKCS8 */
  26549. done:
  26550. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26551. if (der != NULL)
  26552. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26553. if (userA != NULL) {
  26554. wc_ecc_free(userA);
  26555. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26556. }
  26557. #else
  26558. wc_ecc_free(userA);
  26559. #endif
  26560. return ret;
  26561. }
  26562. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26563. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  26564. int curve_id, const ecc_set_type* dp)
  26565. {
  26566. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26567. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26568. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26569. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26570. word32 y;
  26571. #endif
  26572. #ifdef HAVE_ECC_KEY_EXPORT
  26573. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  26574. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26575. #endif
  26576. word32 x = 0;
  26577. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26578. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26579. defined(HAVE_ECC_SIGN)
  26580. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26581. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26582. int i;
  26583. #ifdef HAVE_ECC_VERIFY
  26584. int verify;
  26585. #endif /* HAVE_ECC_VERIFY */
  26586. #endif /* HAVE_ECC_SIGN */
  26587. wc_test_ret_t ret;
  26588. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26589. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26590. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26591. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26592. #else
  26593. ecc_key userA[1];
  26594. ecc_key userB[1];
  26595. ecc_key pubKey[1];
  26596. #endif
  26597. #ifndef WC_NO_RNG
  26598. int curveSize;
  26599. #endif
  26600. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26601. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26602. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26603. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26604. #endif
  26605. #ifdef HAVE_ECC_KEY_EXPORT
  26606. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26607. #endif
  26608. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26609. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26610. defined(HAVE_ECC_SIGN)
  26611. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26612. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26613. #endif
  26614. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  26615. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  26616. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26617. if (sharedA == NULL || sharedB == NULL)
  26618. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26619. #endif
  26620. #ifdef HAVE_ECC_KEY_EXPORT
  26621. if (exportBuf == NULL)
  26622. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26623. #endif
  26624. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26625. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26626. defined(HAVE_ECC_SIGN)
  26627. if (sig == NULL || digest == NULL)
  26628. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26629. #endif
  26630. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  26631. (void)testVerifyCount;
  26632. (void)dp;
  26633. (void)x;
  26634. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26635. if ((userA == NULL) ||
  26636. (userB == NULL) ||
  26637. (pubKey == NULL))
  26638. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26639. #endif
  26640. XMEMSET(userA, 0, sizeof *userA);
  26641. XMEMSET(userB, 0, sizeof *userB);
  26642. XMEMSET(pubKey, 0, sizeof *pubKey);
  26643. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26644. if (ret != 0)
  26645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26646. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  26647. if (ret != 0)
  26648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26649. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26650. if (ret != 0)
  26651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26652. #ifdef WOLFSSL_CUSTOM_CURVES
  26653. if (dp != NULL) {
  26654. ret = wc_ecc_set_custom_curve(userA, dp);
  26655. if (ret != 0)
  26656. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26657. ret = wc_ecc_set_custom_curve(userB, dp);
  26658. if (ret != 0)
  26659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26660. }
  26661. #endif
  26662. #ifndef WC_NO_RNG
  26663. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  26664. #if defined(WOLFSSL_ASYNC_CRYPT)
  26665. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26666. #endif
  26667. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  26668. if (ret == NO_VALID_DEVID) {
  26669. ret = 0;
  26670. goto done; /* no software case */
  26671. }
  26672. #endif
  26673. if (ret == ECC_CURVE_OID_E)
  26674. goto done; /* catch case, where curve is not supported */
  26675. if (ret != 0)
  26676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26677. TEST_SLEEP();
  26678. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  26679. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  26680. if (curveSize != userA->dp->size)
  26681. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26682. }
  26683. ret = wc_ecc_check_key(userA);
  26684. if (ret != 0)
  26685. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26686. TEST_SLEEP();
  26687. /* ATECC508/608 configuration may not support more than one ECDH key */
  26688. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26689. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  26690. #if defined(WOLFSSL_ASYNC_CRYPT)
  26691. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  26692. #endif
  26693. if (ret != 0)
  26694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26695. TEST_SLEEP();
  26696. /* only perform the below tests if the key size matches */
  26697. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  26698. /* Not an error, just not a key size match */
  26699. WARNING_OUT(ECC_CURVE_OID_E, done);
  26700. #ifdef HAVE_ECC_DHE
  26701. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26702. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26703. !defined(HAVE_SELFTEST)
  26704. ret = wc_ecc_set_rng(userA, rng);
  26705. if (ret != 0)
  26706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26707. ret = wc_ecc_set_rng(userB, rng);
  26708. if (ret != 0)
  26709. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26710. #endif
  26711. x = ECC_SHARED_SIZE;
  26712. do {
  26713. #if defined(WOLFSSL_ASYNC_CRYPT)
  26714. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26715. #endif
  26716. if (ret == 0)
  26717. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26718. } while (ret == WC_PENDING_E);
  26719. if (ret != 0)
  26720. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26721. TEST_SLEEP();
  26722. y = ECC_SHARED_SIZE;
  26723. do {
  26724. #if defined(WOLFSSL_ASYNC_CRYPT)
  26725. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26726. #endif
  26727. if (ret == 0)
  26728. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26729. } while (ret == WC_PENDING_E);
  26730. if (ret != 0)
  26731. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26732. if (y != x)
  26733. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26734. if (XMEMCMP(sharedA, sharedB, x))
  26735. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26736. TEST_SLEEP();
  26737. #ifdef HAVE_ECC_CDH
  26738. /* add cofactor flag */
  26739. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  26740. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  26741. x = ECC_SHARED_SIZE;
  26742. do {
  26743. #if defined(WOLFSSL_ASYNC_CRYPT)
  26744. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26745. #endif
  26746. if (ret == 0)
  26747. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26748. } while (ret == WC_PENDING_E);
  26749. if (ret != 0)
  26750. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26751. TEST_SLEEP();
  26752. y = ECC_SHARED_SIZE;
  26753. do {
  26754. #if defined(WOLFSSL_ASYNC_CRYPT)
  26755. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26756. #endif
  26757. if (ret == 0)
  26758. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26759. } while (ret == WC_PENDING_E);
  26760. if (ret != 0)
  26761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26762. if (y != x)
  26763. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26764. if (XMEMCMP(sharedA, sharedB, x))
  26765. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26766. TEST_SLEEP();
  26767. /* remove cofactor flag */
  26768. wc_ecc_set_flags(userA, 0);
  26769. wc_ecc_set_flags(userB, 0);
  26770. #endif /* HAVE_ECC_CDH */
  26771. #endif /* HAVE_ECC_DHE */
  26772. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  26773. #ifdef HAVE_ECC_KEY_EXPORT
  26774. x = ECC_KEY_EXPORT_BUF_SIZE;
  26775. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  26776. if (ret != 0)
  26777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26778. #ifdef HAVE_ECC_KEY_IMPORT
  26779. #ifdef WOLFSSL_CUSTOM_CURVES
  26780. if (dp != NULL) {
  26781. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26782. if (ret != 0)
  26783. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26784. }
  26785. #endif
  26786. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26787. if (ret != 0)
  26788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26789. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26790. #ifdef HAVE_ECC_DHE
  26791. y = ECC_SHARED_SIZE;
  26792. do {
  26793. #if defined(WOLFSSL_ASYNC_CRYPT)
  26794. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26795. #endif
  26796. if (ret == 0)
  26797. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26798. } while (ret == WC_PENDING_E);
  26799. if (ret != 0)
  26800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26801. if (XMEMCMP(sharedA, sharedB, y))
  26802. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26803. TEST_SLEEP();
  26804. #endif /* HAVE_ECC_DHE */
  26805. #ifdef HAVE_COMP_KEY
  26806. /* try compressed export / import too */
  26807. x = ECC_KEY_EXPORT_BUF_SIZE;
  26808. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  26809. if (ret != 0)
  26810. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26811. wc_ecc_free(pubKey);
  26812. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26813. if (ret != 0)
  26814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26815. #ifdef WOLFSSL_CUSTOM_CURVES
  26816. if (dp != NULL) {
  26817. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26818. if (ret != 0)
  26819. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26820. }
  26821. #endif
  26822. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26823. if (ret != 0)
  26824. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26825. #ifdef HAVE_ECC_DHE
  26826. y = ECC_SHARED_SIZE;
  26827. do {
  26828. #if defined(WOLFSSL_ASYNC_CRYPT)
  26829. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26830. #endif
  26831. if (ret == 0)
  26832. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26833. } while (ret == WC_PENDING_E);
  26834. if (ret != 0)
  26835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26836. if (XMEMCMP(sharedA, sharedB, y))
  26837. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26838. TEST_SLEEP();
  26839. #endif /* HAVE_ECC_DHE */
  26840. #endif /* HAVE_COMP_KEY */
  26841. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  26842. #endif /* !WC_NO_RNG */
  26843. #endif /* HAVE_ECC_KEY_IMPORT */
  26844. #endif /* HAVE_ECC_KEY_EXPORT */
  26845. /* For KCAPI cannot sign using generated ECDH key */
  26846. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26847. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  26848. #ifdef HAVE_ECC_SIGN
  26849. /* ECC w/out Shamir has issue with all 0 digest */
  26850. /* WC_BIGINT doesn't have 0 len well on hardware */
  26851. /* Cryptocell has issues with all 0 digest */
  26852. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  26853. !defined(WOLFSSL_CRYPTOCELL)
  26854. /* test DSA sign hash with zeros */
  26855. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26856. digest[i] = 0;
  26857. }
  26858. x = ECC_SIG_SIZE;
  26859. do {
  26860. #if defined(WOLFSSL_ASYNC_CRYPT)
  26861. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26862. #endif
  26863. if (ret == 0)
  26864. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  26865. userA);
  26866. } while (ret == WC_PENDING_E);
  26867. if (ret != 0)
  26868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26869. TEST_SLEEP();
  26870. #ifdef HAVE_ECC_VERIFY
  26871. for (i=0; i<testVerifyCount; i++) {
  26872. verify = 0;
  26873. do {
  26874. #if defined(WOLFSSL_ASYNC_CRYPT)
  26875. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26876. #endif
  26877. if (ret == 0)
  26878. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  26879. &verify, userA);
  26880. } while (ret == WC_PENDING_E);
  26881. if (ret != 0)
  26882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26883. if (verify != 1)
  26884. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26885. TEST_SLEEP();
  26886. }
  26887. #endif /* HAVE_ECC_VERIFY */
  26888. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  26889. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  26890. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26891. digest[i] = (byte)i;
  26892. }
  26893. x = ECC_SIG_SIZE;
  26894. do {
  26895. #if defined(WOLFSSL_ASYNC_CRYPT)
  26896. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26897. #endif
  26898. if (ret == 0)
  26899. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  26900. } while (ret == WC_PENDING_E);
  26901. if (ret != 0)
  26902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26903. TEST_SLEEP();
  26904. #ifdef HAVE_ECC_VERIFY
  26905. for (i=0; i<testVerifyCount; i++) {
  26906. verify = 0;
  26907. do {
  26908. #if defined(WOLFSSL_ASYNC_CRYPT)
  26909. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26910. #endif
  26911. if (ret == 0)
  26912. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  26913. } while (ret == WC_PENDING_E);
  26914. if (ret != 0)
  26915. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26916. if (verify != 1)
  26917. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26918. TEST_SLEEP();
  26919. }
  26920. #endif /* HAVE_ECC_VERIFY */
  26921. #endif /* HAVE_ECC_SIGN */
  26922. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  26923. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  26924. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26925. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  26926. !defined(WOLFSSL_KCAPI_ECC)
  26927. x = ECC_KEY_EXPORT_BUF_SIZE;
  26928. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  26929. if (ret != 0)
  26930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26931. #elif defined(HAVE_ECC_KEY_EXPORT)
  26932. (void)exportBuf;
  26933. #endif /* HAVE_ECC_KEY_EXPORT */
  26934. done:
  26935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26936. if (userA != NULL) {
  26937. wc_ecc_free(userA);
  26938. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26939. }
  26940. if (userB != NULL) {
  26941. wc_ecc_free(userB);
  26942. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26943. }
  26944. if (pubKey != NULL) {
  26945. wc_ecc_free(pubKey);
  26946. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26947. }
  26948. #else
  26949. wc_ecc_free(pubKey);
  26950. wc_ecc_free(userB);
  26951. wc_ecc_free(userA);
  26952. #endif
  26953. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  26954. WC_FREE_VAR(sharedA, HEAP_HINT);
  26955. WC_FREE_VAR(sharedB, HEAP_HINT);
  26956. #endif
  26957. #ifdef HAVE_ECC_KEY_EXPORT
  26958. WC_FREE_VAR(exportBuf, HEAP_HINT);
  26959. #endif
  26960. #ifdef HAVE_ECC_SIGN
  26961. WC_FREE_VAR(sig, HEAP_HINT);
  26962. WC_FREE_VAR(digest, HEAP_HINT);
  26963. #endif
  26964. (void)keySize;
  26965. (void)curve_id;
  26966. (void)rng;
  26967. return ret;
  26968. }
  26969. #undef ECC_TEST_VERIFY_COUNT
  26970. #define ECC_TEST_VERIFY_COUNT 2
  26971. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  26972. {
  26973. wc_test_ret_t ret;
  26974. WOLFSSL_MSG_EX("ecc_test_curve keySize = %d", keySize);
  26975. #if FIPS_VERSION3_GE(6,0,0)
  26976. #ifdef DEBUG_WOLFSSL
  26977. printf("keySize is %d\n", keySize);
  26978. #endif
  26979. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  26980. goto skip_A;
  26981. }
  26982. #endif
  26983. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  26984. NULL);
  26985. if (ret < 0) {
  26986. if (ret == ECC_CURVE_OID_E) {
  26987. /* ignore error for curves not found */
  26988. /* some curve sizes are only available with:
  26989. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  26990. and HAVE_ECC_KOBLITZ */
  26991. }
  26992. else {
  26993. printf("ecc_test_curve_size %d failed!\n", keySize);
  26994. return ret;
  26995. }
  26996. }
  26997. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  26998. #if FIPS_VERSION3_GE(6,0,0)
  26999. skip_A:
  27000. #endif
  27001. #ifdef HAVE_ECC_VECTOR_TEST
  27002. ret = ecc_test_vector(keySize);
  27003. if (ret < 0) {
  27004. printf("ecc_test_vector %d failed!\n", keySize);
  27005. return ret;
  27006. }
  27007. #endif
  27008. #if FIPS_VERSION3_GE(6,0,0)
  27009. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27010. goto skip_B;
  27011. }
  27012. #endif
  27013. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27014. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27015. ret = ecc_test_key_decode(rng, keySize);
  27016. if (ret < 0) {
  27017. if (ret == ECC_CURVE_OID_E) {
  27018. /* ignore error for curves not found */
  27019. }
  27020. else {
  27021. printf("ecc_test_key_decode %d failed!\n", keySize);
  27022. return ret;
  27023. }
  27024. }
  27025. #endif
  27026. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27027. ret = ecc_test_key_gen(rng, keySize);
  27028. if (ret < 0) {
  27029. if (ret == ECC_CURVE_OID_E) {
  27030. /* ignore error for curves not found */
  27031. }
  27032. else {
  27033. printf("ecc_test_key_gen %d failed!\n", keySize);
  27034. return ret;
  27035. }
  27036. }
  27037. #endif
  27038. #if FIPS_VERSION3_GE(6,0,0)
  27039. skip_B:
  27040. #endif
  27041. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27042. return 0;
  27043. }
  27044. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27045. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27046. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27047. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27048. static wc_test_ret_t ecc_point_test(void)
  27049. {
  27050. wc_test_ret_t ret;
  27051. ecc_point* point;
  27052. ecc_point* point2;
  27053. #ifdef HAVE_COMP_KEY
  27054. ecc_point* point3;
  27055. ecc_point* point4;
  27056. #endif
  27057. word32 outLen;
  27058. byte out[65];
  27059. byte der[] = { 0x04, /* = Uncompressed */
  27060. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27061. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27062. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27063. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27064. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27065. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27066. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27067. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27068. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27069. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27070. byte derComp0[] = { 0x02, /* = Compressed, y even */
  27071. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27072. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27073. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27074. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27075. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  27076. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27077. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27078. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27079. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27080. #endif
  27081. byte altDer[] = { 0x04, /* = Uncompressed */
  27082. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27083. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27084. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27085. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27086. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27087. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27088. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27089. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  27090. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  27091. /* if curve P256 is not enabled then test should not fail */
  27092. if (curve_idx == ECC_CURVE_INVALID)
  27093. return 0;
  27094. outLen = sizeof(out);
  27095. point = wc_ecc_new_point();
  27096. if (point == NULL)
  27097. return WC_TEST_RET_ENC_ERRNO;
  27098. point2 = wc_ecc_new_point();
  27099. if (point2 == NULL) {
  27100. wc_ecc_del_point(point);
  27101. return WC_TEST_RET_ENC_NC;
  27102. }
  27103. #ifdef HAVE_COMP_KEY
  27104. point3 = wc_ecc_new_point();
  27105. if (point3 == NULL) {
  27106. wc_ecc_del_point(point2);
  27107. wc_ecc_del_point(point);
  27108. return WC_TEST_RET_ENC_NC;
  27109. }
  27110. point4 = wc_ecc_new_point();
  27111. if (point4 == NULL) {
  27112. wc_ecc_del_point(point3);
  27113. wc_ecc_del_point(point2);
  27114. wc_ecc_del_point(point);
  27115. return WC_TEST_RET_ENC_NC;
  27116. }
  27117. #endif
  27118. /* Parameter Validation testing. */
  27119. wc_ecc_del_point(NULL);
  27120. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  27121. if (ret != ECC_BAD_ARG_E) {
  27122. ret = WC_TEST_RET_ENC_EC(ret);
  27123. goto done;
  27124. }
  27125. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  27126. if (ret != ECC_BAD_ARG_E) {
  27127. ret = WC_TEST_RET_ENC_EC(ret);
  27128. goto done;
  27129. }
  27130. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  27131. if (ret != ECC_BAD_ARG_E) {
  27132. ret = WC_TEST_RET_ENC_EC(ret);
  27133. goto done;
  27134. }
  27135. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  27136. if (ret != ECC_BAD_ARG_E) {
  27137. ret = WC_TEST_RET_ENC_EC(ret);
  27138. goto done;
  27139. }
  27140. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  27141. if (ret != ECC_BAD_ARG_E) {
  27142. ret = WC_TEST_RET_ENC_EC(ret);
  27143. goto done;
  27144. }
  27145. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  27146. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  27147. ret = WC_TEST_RET_ENC_EC(ret);
  27148. goto done;
  27149. }
  27150. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  27151. if (ret != ECC_BAD_ARG_E) {
  27152. ret = WC_TEST_RET_ENC_EC(ret);
  27153. goto done;
  27154. }
  27155. outLen = 0;
  27156. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27157. if (ret != BUFFER_E) {
  27158. ret = WC_TEST_RET_ENC_EC(ret);
  27159. goto done;
  27160. }
  27161. ret = wc_ecc_copy_point(NULL, NULL);
  27162. if (ret != ECC_BAD_ARG_E) {
  27163. ret = WC_TEST_RET_ENC_EC(ret);
  27164. goto done;
  27165. }
  27166. ret = wc_ecc_copy_point(NULL, point2);
  27167. if (ret != ECC_BAD_ARG_E) {
  27168. ret = WC_TEST_RET_ENC_EC(ret);
  27169. goto done;
  27170. }
  27171. ret = wc_ecc_copy_point(point, NULL);
  27172. if (ret != ECC_BAD_ARG_E) {
  27173. ret = WC_TEST_RET_ENC_EC(ret);
  27174. goto done;
  27175. }
  27176. ret = wc_ecc_cmp_point(NULL, NULL);
  27177. if (ret != BAD_FUNC_ARG) {
  27178. ret = WC_TEST_RET_ENC_EC(ret);
  27179. goto done;
  27180. }
  27181. ret = wc_ecc_cmp_point(NULL, point2);
  27182. if (ret != BAD_FUNC_ARG) {
  27183. ret = WC_TEST_RET_ENC_EC(ret);
  27184. goto done;
  27185. }
  27186. ret = wc_ecc_cmp_point(point, NULL);
  27187. if (ret != BAD_FUNC_ARG) {
  27188. ret = WC_TEST_RET_ENC_EC(ret);
  27189. goto done;
  27190. }
  27191. /* Use API. */
  27192. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  27193. if (ret != 0) {
  27194. ret = WC_TEST_RET_ENC_EC(ret);
  27195. goto done;
  27196. }
  27197. outLen = sizeof(out);
  27198. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27199. if (ret != 0) {
  27200. ret = WC_TEST_RET_ENC_EC(ret);
  27201. goto done;
  27202. }
  27203. if (outLen != sizeof(der)) {
  27204. ret = WC_TEST_RET_ENC_NC;
  27205. goto done;
  27206. }
  27207. if (XMEMCMP(out, der, outLen) != 0) {
  27208. ret = WC_TEST_RET_ENC_NC;
  27209. goto done;
  27210. }
  27211. ret = wc_ecc_copy_point(point2, point);
  27212. if (ret != MP_OKAY) {
  27213. ret = WC_TEST_RET_ENC_EC(ret);
  27214. goto done;
  27215. }
  27216. ret = wc_ecc_cmp_point(point2, point);
  27217. if (ret != MP_EQ) {
  27218. ret = WC_TEST_RET_ENC_EC(ret);
  27219. goto done;
  27220. }
  27221. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  27222. if (ret != 0) {
  27223. ret = WC_TEST_RET_ENC_EC(ret);
  27224. goto done;
  27225. }
  27226. ret = wc_ecc_cmp_point(point2, point);
  27227. if (ret != MP_GT) {
  27228. ret = WC_TEST_RET_ENC_EC(ret);
  27229. goto done;
  27230. }
  27231. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27232. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27233. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  27234. if (ret != 0) {
  27235. ret = WC_TEST_RET_ENC_EC(ret);
  27236. goto done;
  27237. }
  27238. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  27239. if (ret != 0) {
  27240. ret = WC_TEST_RET_ENC_EC(ret);
  27241. goto done;
  27242. }
  27243. ret = wc_ecc_cmp_point(point3, point4);
  27244. if (ret != MP_EQ) {
  27245. ret = WC_TEST_RET_ENC_EC(ret);
  27246. goto done;
  27247. }
  27248. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  27249. if (ret != 0) {
  27250. ret = WC_TEST_RET_ENC_EC(ret);
  27251. goto done;
  27252. }
  27253. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  27254. if (ret != 0) {
  27255. ret = WC_TEST_RET_ENC_EC(ret);
  27256. goto done;
  27257. }
  27258. ret = wc_ecc_cmp_point(point3, point4);
  27259. if (ret != MP_EQ) {
  27260. ret = WC_TEST_RET_ENC_EC(ret);
  27261. goto done;
  27262. }
  27263. #endif
  27264. done:
  27265. #ifdef HAVE_COMP_KEY
  27266. wc_ecc_del_point(point4);
  27267. wc_ecc_del_point(point3);
  27268. #endif
  27269. wc_ecc_del_point(point2);
  27270. wc_ecc_del_point(point);
  27271. return ret;
  27272. }
  27273. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27274. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27275. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  27276. {
  27277. wc_test_ret_t ret;
  27278. word32 sigSz;
  27279. int size;
  27280. byte out[ECC_MAX_SIG_SIZE];
  27281. byte in[] = TEST_STRING;
  27282. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  27283. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  27284. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  27285. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  27286. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  27287. };
  27288. word32 inLen = (word32)XSTRLEN((char*)in);
  27289. size = wc_ecc_sig_size(key);
  27290. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  27291. if (ret != size)
  27292. return WC_TEST_RET_ENC_NC;
  27293. sigSz = (word32)ret;
  27294. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27295. inLen, out, &sigSz, key, sizeof(*key), rng);
  27296. if (ret != 0)
  27297. return WC_TEST_RET_ENC_EC(ret);
  27298. TEST_SLEEP();
  27299. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27300. inLen, out, sigSz, key, sizeof(*key));
  27301. if (ret != 0)
  27302. return WC_TEST_RET_ENC_EC(ret);
  27303. TEST_SLEEP();
  27304. sigSz = (word32)sizeof(out);
  27305. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27306. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  27307. if (ret != 0)
  27308. return WC_TEST_RET_ENC_EC(ret);
  27309. TEST_SLEEP();
  27310. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27311. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  27312. if (ret != 0)
  27313. return WC_TEST_RET_ENC_EC(ret);
  27314. TEST_SLEEP();
  27315. return 0;
  27316. }
  27317. #endif
  27318. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27319. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27320. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  27321. {
  27322. wc_test_ret_t ret;
  27323. int curve_id;
  27324. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27325. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27326. #else
  27327. ecc_key keyImp[1];
  27328. #endif
  27329. byte priv[32];
  27330. word32 privLen;
  27331. byte pub[65*2];
  27332. word32 pubLen, pubLenX, pubLenY;
  27333. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  27334. "ce46cbc947616d0cbaa82323818a793d";
  27335. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  27336. "922f8b30ea6e8811742ac7238fe87308";
  27337. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  27338. "2e7a493ec1153b3a95feb8a4873f8d08";
  27339. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27340. if (keyImp == NULL)
  27341. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27342. #endif
  27343. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27344. privLen = sizeof(priv);
  27345. ret = wc_ecc_export_private_only(key, priv, &privLen);
  27346. if (ret != 0) {
  27347. ret = WC_TEST_RET_ENC_EC(ret);
  27348. goto done;
  27349. }
  27350. pubLen = sizeof(pub);
  27351. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  27352. if (ret != 0) {
  27353. ret = WC_TEST_RET_ENC_EC(ret);
  27354. goto done;
  27355. }
  27356. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  27357. if (ret != 0) {
  27358. ret = WC_TEST_RET_ENC_EC(ret);
  27359. goto done;
  27360. }
  27361. wc_ecc_free(keyImp);
  27362. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27363. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  27364. if (ret != 0) {
  27365. ret = WC_TEST_RET_ENC_EC(ret);
  27366. goto done;
  27367. }
  27368. wc_ecc_free(keyImp);
  27369. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27370. curve_id = wc_ecc_get_curve_id(key->idx);
  27371. if (curve_id < 0) {
  27372. ret = WC_TEST_RET_ENC_EC(curve_id);
  27373. goto done;
  27374. }
  27375. /* test import private only */
  27376. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  27377. curve_id);
  27378. if (ret != 0) {
  27379. ret = WC_TEST_RET_ENC_EC(ret);
  27380. goto done;
  27381. }
  27382. wc_ecc_free(keyImp);
  27383. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27384. /* test export public raw */
  27385. pubLenX = pubLenY = 32;
  27386. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  27387. if (ret != 0) {
  27388. ret = WC_TEST_RET_ENC_EC(ret);
  27389. goto done;
  27390. }
  27391. #ifndef HAVE_SELFTEST
  27392. /* test import of public */
  27393. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  27394. if (ret != 0) {
  27395. ret = WC_TEST_RET_ENC_EC(ret);
  27396. goto done;
  27397. }
  27398. #endif
  27399. wc_ecc_free(keyImp);
  27400. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27401. /* test export private and public raw */
  27402. pubLenX = pubLenY = privLen = 32;
  27403. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  27404. priv, &privLen);
  27405. if (ret != 0) {
  27406. ret = WC_TEST_RET_ENC_EC(ret);
  27407. goto done;
  27408. }
  27409. #ifndef HAVE_SELFTEST
  27410. /* test import of private and public */
  27411. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  27412. if (ret != 0) {
  27413. ret = WC_TEST_RET_ENC_EC(ret);
  27414. goto done;
  27415. }
  27416. #endif
  27417. done:
  27418. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27419. if (keyImp != NULL) {
  27420. wc_ecc_free(keyImp);
  27421. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27422. }
  27423. #else
  27424. wc_ecc_free(keyImp);
  27425. #endif
  27426. return ret;
  27427. }
  27428. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27429. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27430. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27431. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  27432. {
  27433. wc_test_ret_t ret;
  27434. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27435. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27436. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27437. #else
  27438. ecc_key key2[1];
  27439. ecc_key key3[1];
  27440. #endif
  27441. #ifdef WOLFSSL_PUBLIC_MP
  27442. mp_int* priv;
  27443. #endif
  27444. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27445. if ((key2 == NULL) || (key3 == NULL))
  27446. ERROR_OUT(MEMORY_E, done);
  27447. #endif
  27448. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  27449. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  27450. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  27451. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  27452. * private key in key2.
  27453. */
  27454. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  27455. ECC_SECP256R1);
  27456. if (ret != 0)
  27457. goto done;
  27458. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  27459. * in key3.
  27460. */
  27461. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  27462. key1->dp->prime, ECC_SECP256R1);
  27463. if (ret != 0)
  27464. goto done;
  27465. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27466. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27467. 1);
  27468. if (ret != 0) {
  27469. ret = WC_TEST_RET_ENC_EC(ret);
  27470. goto done;
  27471. }
  27472. #ifdef WOLFSSL_PUBLIC_MP
  27473. priv = wc_ecc_key_get_priv(key1);
  27474. mp_zero(priv);
  27475. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27476. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27477. 1);
  27478. if (ret != 0) {
  27479. ret = WC_TEST_RET_ENC_EC(ret);
  27480. goto done;
  27481. }
  27482. if (!wc_ecc_point_is_at_infinity(&key3->pubkey)) {
  27483. ret = WC_TEST_RET_ENC_EC(ret);
  27484. goto done;
  27485. }
  27486. #endif
  27487. done:
  27488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27489. if (key2 != NULL) {
  27490. wc_ecc_free(key2);
  27491. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27492. }
  27493. if (key3 != NULL) {
  27494. wc_ecc_free(key3);
  27495. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27496. }
  27497. #else
  27498. wc_ecc_free(key3);
  27499. wc_ecc_free(key2);
  27500. #endif
  27501. return ret;
  27502. }
  27503. #endif
  27504. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  27505. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_ATECC508A) && \
  27506. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  27507. !defined(WOLFSSL_CRYPTOCELL)
  27508. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  27509. {
  27510. wc_test_ret_t ret;
  27511. byte out[128];
  27512. word32 outLen = sizeof(out);
  27513. /* Parameter Validation testing. */
  27514. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  27515. if (ret != BAD_FUNC_ARG)
  27516. return WC_TEST_RET_ENC_EC(ret);
  27517. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  27518. if (ret != BAD_FUNC_ARG)
  27519. return WC_TEST_RET_ENC_EC(ret);
  27520. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  27521. if (ret != BAD_FUNC_ARG)
  27522. return WC_TEST_RET_ENC_EC(ret);
  27523. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  27524. if (ret != BAD_FUNC_ARG)
  27525. return WC_TEST_RET_ENC_EC(ret);
  27526. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27527. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27528. !defined(HAVE_SELFTEST)
  27529. ret = wc_ecc_set_rng(key, rng);
  27530. if (ret != 0)
  27531. return WC_TEST_RET_ENC_EC(ret);
  27532. #else
  27533. (void)rng;
  27534. #endif
  27535. /* Use API. */
  27536. ret = 0;
  27537. do {
  27538. #if defined(WOLFSSL_ASYNC_CRYPT)
  27539. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27540. #endif
  27541. if (ret == 0)
  27542. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  27543. } while (ret == WC_PENDING_E);
  27544. if (ret != 0)
  27545. return WC_TEST_RET_ENC_EC(ret);
  27546. TEST_SLEEP();
  27547. return 0;
  27548. }
  27549. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  27550. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  27551. {
  27552. wc_test_ret_t ret;
  27553. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27554. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27555. #else
  27556. ecc_key key[1];
  27557. #endif
  27558. #if !defined(NO_ECC_SECP) && \
  27559. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27560. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27561. word32 idx = 0;
  27562. #endif
  27563. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27564. if (key == NULL)
  27565. ERROR_OUT(MEMORY_E, done);
  27566. #endif
  27567. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27568. /* Use API */
  27569. ret = wc_ecc_set_flags(NULL, 0);
  27570. if (ret != BAD_FUNC_ARG) {
  27571. ret = WC_TEST_RET_ENC_EC(ret);
  27572. goto done;
  27573. }
  27574. ret = wc_ecc_set_flags(key, 0);
  27575. if (ret != 0) {
  27576. ret = WC_TEST_RET_ENC_EC(ret);
  27577. goto done;
  27578. }
  27579. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27580. #ifndef WC_NO_RNG
  27581. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  27582. #if defined(WOLFSSL_ASYNC_CRYPT)
  27583. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  27584. #endif
  27585. if (ret != 0) {
  27586. goto done;
  27587. }
  27588. #ifndef NO_SIG_WRAPPER
  27589. ret = ecc_sig_test(rng, key);
  27590. if (ret < 0)
  27591. goto done;
  27592. #endif
  27593. TEST_SLEEP();
  27594. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  27595. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27596. ret = ecc_ssh_test(key, rng);
  27597. if (ret < 0)
  27598. goto done;
  27599. #endif
  27600. wc_ecc_free(key);
  27601. #else
  27602. (void)rng;
  27603. #endif /* !WC_NO_RNG */
  27604. #if !defined(NO_ECC_SECP) && \
  27605. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27606. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27607. /* Use test ECC key - ensure real private "d" exists */
  27608. #if defined(USE_CERT_BUFFERS_256)
  27609. {
  27610. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  27611. sizeof_ecc_key_der_256);
  27612. }
  27613. #elif !defined(NO_FILESYSTEM)
  27614. {
  27615. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  27616. byte der[128];
  27617. word32 derSz;
  27618. if (!file) {
  27619. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27620. }
  27621. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  27622. XFCLOSE(file);
  27623. if (derSz == 0)
  27624. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27625. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  27626. }
  27627. #else
  27628. {
  27629. (void)idx;
  27630. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
  27631. ERROR_OUT(ASN_PARSE_E, done);
  27632. }
  27633. #endif
  27634. if (ret != 0) {
  27635. goto done;
  27636. }
  27637. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  27638. ret = ecc_exp_imp_test(key);
  27639. if (ret < 0)
  27640. goto done;
  27641. #endif
  27642. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27643. !defined(WOLFSSL_CRYPTOCELL)
  27644. ret = ecc_mulmod_test(key);
  27645. if (ret < 0)
  27646. goto done;
  27647. #endif
  27648. #endif
  27649. #else
  27650. (void)rng;
  27651. (void)idx;
  27652. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27653. done:
  27654. wc_ecc_free(key);
  27655. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27656. if (key != NULL) {
  27657. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27658. }
  27659. #endif
  27660. return ret;
  27661. }
  27662. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  27663. #if defined(WOLFSSL_CERT_EXT) && \
  27664. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27665. static wc_test_ret_t ecc_decode_test(void)
  27666. {
  27667. wc_test_ret_t ret;
  27668. word32 inSz;
  27669. word32 inOutIdx;
  27670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27671. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27672. #else
  27673. ecc_key key[1];
  27674. #endif
  27675. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  27676. /* This is ecc_clikeypub_der_256. */
  27677. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  27678. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  27679. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  27680. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  27681. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  27682. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  27683. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  27684. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  27685. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  27686. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  27687. 0xb4 };
  27688. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  27689. 0x00, 0x04, 0x01, 0x01 };
  27690. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  27691. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27692. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  27693. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27694. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  27695. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27696. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27697. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27698. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27699. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27700. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27701. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  27702. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  27703. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27704. 0x03, 0x03, 0x04, 0x01, 0x01 };
  27705. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  27706. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27707. 0x03, 0x03, 0x00, 0x04, 0x01 };
  27708. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27709. if (key == NULL)
  27710. ERROR_OUT(MEMORY_E, done);
  27711. #endif
  27712. XMEMSET(key, 0, sizeof *key);
  27713. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27714. inSz = sizeof(good);
  27715. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  27716. if (ret != BAD_FUNC_ARG) {
  27717. ret = WC_TEST_RET_ENC_EC(ret);
  27718. goto done;
  27719. }
  27720. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  27721. if (ret != BAD_FUNC_ARG) {
  27722. ret = WC_TEST_RET_ENC_EC(ret);
  27723. goto done;
  27724. }
  27725. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  27726. if (ret != BAD_FUNC_ARG) {
  27727. ret = WC_TEST_RET_ENC_EC(ret);
  27728. goto done;
  27729. }
  27730. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  27731. if (ret != BAD_FUNC_ARG) {
  27732. ret = WC_TEST_RET_ENC_EC(ret);
  27733. goto done;
  27734. }
  27735. /* Change offset to produce bad input data. */
  27736. inOutIdx = 2;
  27737. inSz = sizeof(good) - inOutIdx;
  27738. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27739. if (ret != ASN_PARSE_E) {
  27740. ret = WC_TEST_RET_ENC_EC(ret);
  27741. goto done;
  27742. }
  27743. inOutIdx = 4;
  27744. inSz = sizeof(good) - inOutIdx;
  27745. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27746. if (ret != ASN_PARSE_E) {
  27747. ret = WC_TEST_RET_ENC_EC(ret);
  27748. goto done;
  27749. }
  27750. /* Bad data. */
  27751. inSz = sizeof(badNoObjId);
  27752. inOutIdx = 0;
  27753. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  27754. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27755. ret = WC_TEST_RET_ENC_EC(ret);
  27756. goto done;
  27757. }
  27758. inSz = sizeof(badOneObjId);
  27759. inOutIdx = 0;
  27760. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  27761. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27762. ret = WC_TEST_RET_ENC_EC(ret);
  27763. goto done;
  27764. }
  27765. inSz = sizeof(badObjId1Len);
  27766. inOutIdx = 0;
  27767. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  27768. if (ret != ASN_PARSE_E) {
  27769. ret = WC_TEST_RET_ENC_EC(ret);
  27770. goto done;
  27771. }
  27772. inSz = sizeof(badObj2d1Len);
  27773. inOutIdx = 0;
  27774. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  27775. if (ret != ASN_PARSE_E) {
  27776. ret = WC_TEST_RET_ENC_EC(ret);
  27777. goto done;
  27778. }
  27779. inSz = sizeof(badNotBitStr);
  27780. inOutIdx = 0;
  27781. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  27782. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  27783. ret = WC_TEST_RET_ENC_EC(ret);
  27784. goto done;
  27785. }
  27786. inSz = sizeof(badBitStrLen);
  27787. inOutIdx = 0;
  27788. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  27789. if (ret != ASN_PARSE_E) {
  27790. ret = WC_TEST_RET_ENC_EC(ret);
  27791. goto done;
  27792. }
  27793. inSz = sizeof(badNoBitStrZero);
  27794. inOutIdx = 0;
  27795. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  27796. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  27797. ret = WC_TEST_RET_ENC_EC(ret);
  27798. goto done;
  27799. }
  27800. inSz = sizeof(badPoint);
  27801. inOutIdx = 0;
  27802. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  27803. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  27804. ret = WC_TEST_RET_ENC_EC(ret);
  27805. goto done;
  27806. }
  27807. inSz = sizeof(good);
  27808. inOutIdx = 0;
  27809. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27810. if (ret != 0) {
  27811. ret = WC_TEST_RET_ENC_EC(ret);
  27812. goto done;
  27813. }
  27814. done:
  27815. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27816. if (key != NULL) {
  27817. wc_ecc_free(key);
  27818. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27819. }
  27820. #else
  27821. wc_ecc_free(key);
  27822. #endif
  27823. return ret;
  27824. }
  27825. #endif /* WOLFSSL_CERT_EXT */
  27826. #ifdef WOLFSSL_CUSTOM_CURVES
  27827. static const byte eccKeyExplicitCurve[] = {
  27828. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  27829. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  27830. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  27831. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  27832. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  27833. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27834. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27835. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  27836. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  27837. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  27838. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  27839. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  27840. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  27841. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  27842. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  27843. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  27844. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  27845. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  27846. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27847. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27848. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  27849. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  27850. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  27851. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  27852. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  27853. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  27854. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  27855. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  27856. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  27857. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  27858. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  27859. };
  27860. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  27861. {
  27862. wc_test_ret_t ret;
  27863. word32 inOutIdx;
  27864. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27865. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27866. #else
  27867. ecc_key key[1];
  27868. #endif
  27869. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  27870. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27871. #ifndef WOLFSSL_ECC_CURVE_STATIC
  27872. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  27873. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  27874. };
  27875. #define ecc_oid_brainpoolp256r1_sz \
  27876. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  27877. #else
  27878. #define ecc_oid_brainpoolp256r1 { \
  27879. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  27880. }
  27881. #define ecc_oid_brainpoolp256r1_sz 9
  27882. #endif
  27883. #define ecc_oid_brainpoolp256r1_sum 104
  27884. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  27885. 32, /* size/bytes */
  27886. ECC_CURVE_CUSTOM, /* ID */
  27887. "BRAINPOOLP256R1", /* curve name */
  27888. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  27889. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  27890. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  27891. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  27892. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  27893. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  27894. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  27895. ecc_oid_brainpoolp256r1_sz,
  27896. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  27897. 1, /* cofactor */
  27898. };
  27899. #endif /* HAVE_ECC_BRAINPOOL */
  27900. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27901. if (! key) {
  27902. ret = MEMORY_E;
  27903. goto done;
  27904. }
  27905. #endif
  27906. XMEMSET(key, 0, sizeof *key);
  27907. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27908. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  27909. &ecc_dp_brainpool256r1);
  27910. if (ret != 0) {
  27911. printf("ECC test for custom curve failed!\n");
  27912. goto done;
  27913. }
  27914. #endif
  27915. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  27916. {
  27917. int curve_id;
  27918. #ifdef HAVE_ECC_BRAINPOOL
  27919. curve_id = ECC_BRAINPOOLP256R1;
  27920. #else
  27921. curve_id = ECC_SECP256K1;
  27922. #endif
  27923. /* Test and demonstrate use of non-SECP curve */
  27924. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  27925. if (ret < 0) {
  27926. printf("ECC test for curve_id %d failed!\n", curve_id);
  27927. goto done;
  27928. }
  27929. }
  27930. #endif
  27931. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  27932. if (ret != 0) {
  27933. ret = WC_TEST_RET_ENC_EC(ret);
  27934. goto done;
  27935. }
  27936. inOutIdx = 0;
  27937. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  27938. sizeof(eccKeyExplicitCurve));
  27939. if (ret != 0)
  27940. ret = WC_TEST_RET_ENC_EC(ret);
  27941. done:
  27942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27943. if (key) {
  27944. wc_ecc_free(key);
  27945. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27946. }
  27947. #else
  27948. wc_ecc_free(key);
  27949. #endif
  27950. (void)rng;
  27951. return ret;
  27952. }
  27953. #endif /* WOLFSSL_CUSTOM_CURVES */
  27954. #ifdef WOLFSSL_SM2
  27955. #ifdef HAVE_ECC_VERIFY
  27956. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  27957. #ifdef HAVE_OID_ENCODING
  27958. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  27959. #define CODED_SM2P256V1_SZ 6
  27960. #else
  27961. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  27962. #define CODED_SM2P256V1_SZ 10
  27963. #endif
  27964. #ifndef WOLFSSL_ECC_CURVE_STATIC
  27965. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  27966. #else
  27967. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  27968. #endif
  27969. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  27970. #define ECC_SM2P256V1_TEST 102
  27971. static int test_sm2_verify_caseA2(void)
  27972. {
  27973. ecc_key key;
  27974. int ret, res;
  27975. mp_int r,s;
  27976. /* test key values */
  27977. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  27978. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  27979. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  27980. const ecc_set_type ecc_sm2_A2 = {
  27981. 32, /* size/bytes */
  27982. ECC_SM2P256V1_TEST, /* ID */
  27983. "SM2P256V1_TEST", /* curve name */
  27984. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  27985. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  27986. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  27987. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  27988. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  27989. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  27990. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  27991. ecc_oid_sm2p256v1, /* oid/oidSz */
  27992. ecc_oid_sm2p256v1_sz,
  27993. ECC_SM2P256V1_OID, /* oid sum */
  27994. 1, /* cofactor */
  27995. };
  27996. /* use canned hash value hash = H(ZA||M) */
  27997. const byte hash[] = {
  27998. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  27999. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  28000. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  28001. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  28002. };
  28003. /* canned r and s */
  28004. const byte rCan[] = {
  28005. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  28006. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  28007. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  28008. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  28009. };
  28010. const byte sCan[] = {
  28011. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  28012. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  28013. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  28014. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  28015. };
  28016. mp_init(&r);
  28017. mp_init(&s);
  28018. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28019. if (ret != 0)
  28020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28021. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  28022. if (ret != 0)
  28023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28024. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  28025. if (ret != 0)
  28026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28027. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  28028. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  28029. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  28030. if (ret != 0)
  28031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28032. if (res != 1)
  28033. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28034. done:
  28035. mp_free(&r);
  28036. mp_free(&s);
  28037. wc_ecc_free(&key);
  28038. return ret;
  28039. }
  28040. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  28041. static int test_sm2_verify_case(void)
  28042. {
  28043. ecc_key key;
  28044. int ret, res;
  28045. /* test key values */
  28046. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  28047. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  28048. /* use canned hash value hash = H(ZA||M) */
  28049. const byte hash[] = {
  28050. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  28051. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  28052. 0x50,0x69,0x5B,0x20
  28053. };
  28054. const byte sig[] = {
  28055. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28056. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28057. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28058. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28059. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28060. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  28061. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28062. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28063. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28064. };
  28065. const byte badSig[] = {
  28066. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28067. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28068. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28069. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28070. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28071. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  28072. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28073. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28074. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28075. };
  28076. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28077. if (ret != 0)
  28078. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28079. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  28080. if (ret != 0)
  28081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28082. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  28083. &key);
  28084. if (ret != 0)
  28085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28086. if (res != 1)
  28087. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28088. /* now test a case that should fail */
  28089. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  28090. &res, &key);
  28091. if (ret != 0)
  28092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28093. if (res == 1)
  28094. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28095. done:
  28096. wc_ecc_free(&key);
  28097. return ret;
  28098. }
  28099. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  28100. {
  28101. const ecc_set_type* dp = wc_ecc_get_curve_params(
  28102. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  28103. int keySize = 32;
  28104. int curve_id = ECC_SM2P256V1;
  28105. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28106. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28107. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28108. #endif
  28109. #ifdef HAVE_ECC_KEY_EXPORT
  28110. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  28111. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28112. #endif
  28113. word32 x = 0;
  28114. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28115. word32 y;
  28116. #endif
  28117. #ifdef HAVE_ECC_SIGN
  28118. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28119. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28120. int i;
  28121. int verify;
  28122. #endif /* HAVE_ECC_SIGN */
  28123. int ret;
  28124. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28125. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28126. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28127. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28128. #else
  28129. ecc_key userA[1];
  28130. ecc_key userB[1];
  28131. ecc_key pubKey[1];
  28132. #endif
  28133. #ifndef WC_NO_RNG
  28134. int curveSize;
  28135. #endif
  28136. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28137. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28138. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28139. #endif
  28140. #ifdef HAVE_ECC_KEY_EXPORT
  28141. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28142. #endif
  28143. #ifdef HAVE_ECC_SIGN
  28144. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28145. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28146. #endif
  28147. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  28148. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28149. if (sharedA == NULL || sharedB == NULL)
  28150. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28151. #endif
  28152. #ifdef HAVE_ECC_KEY_EXPORT
  28153. if (exportBuf == NULL)
  28154. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28155. #endif
  28156. #ifdef HAVE_ECC_SIGN
  28157. if (sig == NULL || digest == NULL)
  28158. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28159. #endif
  28160. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  28161. (void)testVerifyCount;
  28162. (void)dp;
  28163. (void)x;
  28164. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28165. if ((userA == NULL) ||
  28166. (userB == NULL) ||
  28167. (pubKey == NULL))
  28168. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28169. #endif
  28170. XMEMSET(userA, 0, sizeof *userA);
  28171. XMEMSET(userB, 0, sizeof *userB);
  28172. XMEMSET(pubKey, 0, sizeof *pubKey);
  28173. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  28174. if (ret != 0)
  28175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28176. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  28177. if (ret != 0)
  28178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28179. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28180. if (ret != 0)
  28181. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28182. #ifndef WC_NO_RNG
  28183. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  28184. if (ret == ECC_CURVE_OID_E)
  28185. goto done; /* catch case, where curve is not supported */
  28186. if (ret != 0)
  28187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28188. TEST_SLEEP();
  28189. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  28190. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  28191. if (curveSize != userA->dp->size) {
  28192. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28193. }
  28194. }
  28195. ret = wc_ecc_check_key(userA);
  28196. if (ret != 0)
  28197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28198. TEST_SLEEP();
  28199. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  28200. if (ret != 0)
  28201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28202. /* only perform the below tests if the key size matches */
  28203. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  28204. if (ret != 0) {
  28205. ret = ECC_CURVE_OID_E;
  28206. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28207. }
  28208. #ifdef HAVE_ECC_DHE
  28209. #if defined(ECC_TIMING_RESISTANT)
  28210. ret = wc_ecc_set_rng(userA, rng);
  28211. if (ret != 0)
  28212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28213. ret = wc_ecc_set_rng(userB, rng);
  28214. if (ret != 0)
  28215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28216. #endif
  28217. x = ECC_SHARED_SIZE;
  28218. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  28219. if (ret != 0)
  28220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28221. y = ECC_SHARED_SIZE;
  28222. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  28223. if (ret != 0)
  28224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28225. if (y != x)
  28226. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28227. if (XMEMCMP(sharedA, sharedB, x))
  28228. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28229. #endif /* HAVE_ECC_DHE */
  28230. #ifdef HAVE_ECC_KEY_EXPORT
  28231. x = ECC_KEY_EXPORT_BUF_SIZE;
  28232. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  28233. if (ret != 0)
  28234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28235. #ifdef HAVE_ECC_KEY_IMPORT
  28236. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28237. if (ret != 0)
  28238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28239. #ifdef HAVE_ECC_DHE
  28240. y = ECC_SHARED_SIZE;
  28241. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28242. if (ret != 0)
  28243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28244. if (XMEMCMP(sharedA, sharedB, y))
  28245. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28246. #endif /* HAVE_ECC_DHE */
  28247. #ifdef HAVE_COMP_KEY
  28248. /* try compressed export / import too */
  28249. x = ECC_KEY_EXPORT_BUF_SIZE;
  28250. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  28251. if (ret != 0)
  28252. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28253. wc_ecc_free(pubKey);
  28254. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28255. if (ret != 0)
  28256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28257. #endif
  28258. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28259. if (ret != 0)
  28260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28261. #ifdef HAVE_ECC_DHE
  28262. y = ECC_SHARED_SIZE;
  28263. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28264. if (ret != 0)
  28265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28266. if (XMEMCMP(sharedA, sharedB, y))
  28267. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28268. #endif /* HAVE_ECC_DHE */
  28269. #endif /* HAVE_ECC_KEY_IMPORT */
  28270. #endif /* HAVE_ECC_KEY_EXPORT */
  28271. #endif /* !WC_NO_RNG */
  28272. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  28273. !defined(WC_NO_RNG))
  28274. #ifdef HAVE_ECC_SIGN
  28275. /* ECC w/out Shamir has issue with all 0 digest */
  28276. /* WC_BIGINT doesn't have 0 len well on hardware */
  28277. /* Cryptocell has issues with all 0 digest */
  28278. #if defined(ECC_SHAMIR)
  28279. /* test DSA sign hash with zeros */
  28280. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28281. digest[i] = 0;
  28282. }
  28283. x = ECC_SIG_SIZE;
  28284. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28285. if (ret != 0)
  28286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28287. for (i = 0; i < testVerifyCount; i++) {
  28288. verify = 0;
  28289. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28290. userA);
  28291. if (ret != 0)
  28292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28293. if (verify != 1)
  28294. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28295. }
  28296. #endif /* ECC_SHAMIR */
  28297. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  28298. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28299. digest[i] = (byte)i;
  28300. }
  28301. x = ECC_SIG_SIZE;
  28302. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28303. if (ret != 0)
  28304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28305. for (i = 0; i < testVerifyCount; i++) {
  28306. verify = 0;
  28307. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28308. userA);
  28309. if (ret != 0)
  28310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28311. if (verify != 1)
  28312. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28313. }
  28314. #endif /* HAVE_ECC_SIGN */
  28315. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  28316. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  28317. x = ECC_KEY_EXPORT_BUF_SIZE;
  28318. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  28319. if (ret != 0)
  28320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28321. #elif defined(HAVE_ECC_KEY_EXPORT)
  28322. (void)exportBuf;
  28323. #endif /* HAVE_ECC_KEY_EXPORT */
  28324. done:
  28325. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28326. if (userA != NULL) {
  28327. wc_ecc_free(userA);
  28328. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28329. }
  28330. if (userB != NULL) {
  28331. wc_ecc_free(userB);
  28332. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28333. }
  28334. if (pubKey != NULL) {
  28335. wc_ecc_free(pubKey);
  28336. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28337. }
  28338. #else
  28339. wc_ecc_free(pubKey);
  28340. wc_ecc_free(userB);
  28341. wc_ecc_free(userA);
  28342. #endif
  28343. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  28344. WC_FREE_VAR(sharedA, HEAP_HINT);
  28345. WC_FREE_VAR(sharedB, HEAP_HINT);
  28346. #endif
  28347. #ifdef HAVE_ECC_KEY_EXPORT
  28348. WC_FREE_VAR(exportBuf, HEAP_HINT);
  28349. #endif
  28350. #ifdef HAVE_ECC_SIGN
  28351. WC_FREE_VAR(sig, HEAP_HINT);
  28352. WC_FREE_VAR(digest, HEAP_HINT);
  28353. #endif
  28354. (void)keySize;
  28355. (void)curve_id;
  28356. (void)rng;
  28357. return ret;
  28358. }
  28359. #endif /* HAVE_ECC_VERIFY */
  28360. static int test_sm2_create_digest(void)
  28361. {
  28362. const byte msg[] = "message to sign";
  28363. const byte id[] = "0123456789";
  28364. const byte badId[] = "0123556789";
  28365. byte expected[] = {
  28366. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  28367. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  28368. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  28369. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  28370. };
  28371. ecc_key key;
  28372. int ret;
  28373. /* test key values */
  28374. const char qx[] =
  28375. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  28376. const char qy[] =
  28377. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  28378. const char d[] =
  28379. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  28380. byte digest[WC_SHA256_DIGEST_SIZE];
  28381. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28382. if (ret != 0)
  28383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28384. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  28385. if (ret != 0)
  28386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28387. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  28388. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  28389. WC_SHA256_DIGEST_SIZE, &key);
  28390. if (ret != 0)
  28391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28392. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  28393. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28394. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  28395. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  28396. WC_SHA256_DIGEST_SIZE, &key);
  28397. if (ret != 0)
  28398. goto done;
  28399. /* should be different than the previous ID used */
  28400. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  28401. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28402. done:
  28403. wc_ecc_free(&key);
  28404. return ret;
  28405. }
  28406. static int test_sm2_verify(void)
  28407. {
  28408. int ret = 0;
  28409. #ifdef HAVE_ECC_VERIFY
  28410. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28411. ret = test_sm2_verify_caseA2();
  28412. if (ret != 0)
  28413. return ret;
  28414. #endif
  28415. ret = test_sm2_verify_case();
  28416. if (ret != 0)
  28417. return ret;
  28418. #endif /* HAVE_ECC_VERIFY */
  28419. ret = test_sm2_create_digest();
  28420. return ret;
  28421. }
  28422. #endif /* WOLFSSL_SM2 */
  28423. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  28424. /* Make Cert / Sign example for ECC cert and ECC CA */
  28425. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  28426. {
  28427. wc_test_ret_t ret;
  28428. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28429. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28430. #ifdef WOLFSSL_TEST_CERT
  28431. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28432. #endif
  28433. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28434. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28435. #else
  28436. Cert myCert[1];
  28437. #ifdef WOLFSSL_TEST_CERT
  28438. DecodedCert decode[1];
  28439. #endif
  28440. ecc_key caEccKey[1];
  28441. ecc_key certPubKey[1];
  28442. #endif
  28443. int certSz;
  28444. size_t bytes = 0;
  28445. word32 idx = 0;
  28446. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
  28447. XFILE file;
  28448. #endif
  28449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28450. byte* der = NULL;
  28451. #else
  28452. byte der[FOURK_BUF];
  28453. #endif
  28454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28455. if ((myCert == NULL)
  28456. #ifdef WOLFSSL_TEST_CERT
  28457. || (decode == NULL)
  28458. #endif
  28459. || (caEccKey == NULL) || (certPubKey == NULL))
  28460. ERROR_OUT(MEMORY_E, exit);
  28461. #endif
  28462. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  28463. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  28464. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28465. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28466. if (der == NULL) {
  28467. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28468. }
  28469. #endif
  28470. /* Get cert private key */
  28471. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28472. /* Get Cert Key 384 */
  28473. #ifdef USE_CERT_BUFFERS_256
  28474. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  28475. bytes = sizeof_ca_ecc_key_der_384;
  28476. #elif !defined(NO_FILESYSTEM)
  28477. file = XFOPEN(eccCaKey384File, "rb");
  28478. if (!file) {
  28479. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28480. }
  28481. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28482. XFCLOSE(file);
  28483. if (bytes == 0)
  28484. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28485. (void)eccCaKeyFile;
  28486. #else
  28487. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
  28488. ERROR_OUT(ASN_PARSE_E, exit);
  28489. #endif /* USE_CERT_BUFFERS_256 */
  28490. /* end if ENABLE_ECC384_CERT_GEN_TEST */
  28491. #else
  28492. /* !ENABLE_ECC384_CERT_GEN_TEST */
  28493. #ifdef USE_CERT_BUFFERS_256
  28494. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  28495. bytes = sizeof_ca_ecc_key_der_256;
  28496. #else
  28497. file = XFOPEN(eccCaKeyFile, "rb");
  28498. if (!file) {
  28499. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28500. }
  28501. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28502. XFCLOSE(file);
  28503. if (bytes == 0)
  28504. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28505. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28506. (void)eccCaKey384File;
  28507. #endif
  28508. #endif /* USE_CERT_BUFFERS_256 */
  28509. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28510. /* Get CA Key */
  28511. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  28512. if (ret != 0)
  28513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28514. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  28515. if (ret != 0)
  28516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28517. /* Make a public key */
  28518. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  28519. if (ret != 0)
  28520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28521. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  28522. #if defined(WOLFSSL_ASYNC_CRYPT)
  28523. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  28524. #endif
  28525. if (ret != 0)
  28526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28527. TEST_SLEEP();
  28528. /* Setup Certificate */
  28529. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  28530. if (ret != 0)
  28531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28532. #ifndef NO_SHA256
  28533. myCert->sigType = CTC_SHA256wECDSA;
  28534. #else
  28535. myCert->sigType = CTC_SHAwECDSA;
  28536. #endif
  28537. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  28538. #ifdef WOLFSSL_CERT_EXT
  28539. /* add Policies */
  28540. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  28541. CTC_MAX_CERTPOL_SZ);
  28542. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  28543. CTC_MAX_CERTPOL_SZ);
  28544. myCert->certPoliciesNb = 2;
  28545. /* add SKID from the Public Key */
  28546. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  28547. if (ret != 0)
  28548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28549. /* add AKID from the Public Key */
  28550. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  28551. if (ret != 0)
  28552. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28553. /* add Key Usage */
  28554. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  28555. if (ret != 0)
  28556. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28557. #endif /* WOLFSSL_CERT_EXT */
  28558. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28559. #if defined(USE_CERT_BUFFERS_256)
  28560. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  28561. sizeof_ca_ecc_cert_der_384);
  28562. #elif !defined(NO_FILESYSTEM)
  28563. ret = wc_SetIssuer(myCert, eccCaCert384File);
  28564. (void)eccCaCertFile;
  28565. #else
  28566. /* not testing with embedded, no file system target */
  28567. ERROR_OUT(ASN_PARSE_E, exit);
  28568. #endif /* USE_CERT_BUFFERS_256 */
  28569. #else
  28570. /* not ENABLE_ECC384_CERT_GEN_TEST */
  28571. #if defined(USE_CERT_BUFFERS_256)
  28572. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  28573. sizeof_ca_ecc_cert_der_256);
  28574. #else
  28575. ret = wc_SetIssuer(myCert, eccCaCertFile);
  28576. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28577. (void)eccCaCert384File;
  28578. #endif
  28579. #endif
  28580. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28581. if (ret < 0)
  28582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28583. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  28584. if (certSz < 0) {
  28585. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  28586. }
  28587. ret = 0;
  28588. do {
  28589. #if defined(WOLFSSL_ASYNC_CRYPT)
  28590. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28591. #endif
  28592. if (ret >= 0) {
  28593. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  28594. FOURK_BUF, NULL, caEccKey, rng);
  28595. }
  28596. } while (ret == WC_PENDING_E);
  28597. if (ret < 0)
  28598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28599. certSz = (int)ret;
  28600. TEST_SLEEP();
  28601. #ifdef WOLFSSL_TEST_CERT
  28602. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  28603. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  28604. if (ret != 0) {
  28605. FreeDecodedCert(decode);
  28606. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28607. }
  28608. FreeDecodedCert(decode);
  28609. #endif
  28610. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  28611. CERT_TYPE);
  28612. if (ret != 0) {
  28613. goto exit;
  28614. }
  28615. exit:
  28616. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28617. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28618. #endif
  28619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28620. if (myCert != NULL)
  28621. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28622. #ifdef WOLFSSL_TEST_CERT
  28623. if (decode != NULL)
  28624. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28625. #endif
  28626. if (caEccKey != NULL) {
  28627. wc_ecc_free(caEccKey);
  28628. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28629. }
  28630. if (certPubKey != NULL) {
  28631. wc_ecc_free(certPubKey);
  28632. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28633. }
  28634. #else
  28635. wc_ecc_free(certPubKey);
  28636. wc_ecc_free(caEccKey);
  28637. #endif
  28638. return ret;
  28639. }
  28640. #endif /* WOLFSSL_CERT_GEN */
  28641. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  28642. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  28643. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  28644. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  28645. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  28646. {
  28647. wc_test_ret_t ret = 0;
  28648. ecc_key* key;
  28649. #ifdef WC_NO_RNG
  28650. word32 idx = 0;
  28651. #endif
  28652. key = wc_ecc_key_new(HEAP_HINT);
  28653. if (key == NULL) {
  28654. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28655. }
  28656. #ifndef WC_NO_RNG
  28657. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  28658. #if defined(WOLFSSL_ASYNC_CRYPT)
  28659. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  28660. #endif
  28661. if (ret != 0)
  28662. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28663. #else
  28664. /* use test ECC key */
  28665. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  28666. (word32)sizeof_ecc_key_der_256);
  28667. (void)rng;
  28668. #endif
  28669. exit:
  28670. wc_ecc_key_free(key);
  28671. return ret;
  28672. }
  28673. #endif
  28674. /* ECC Non-blocking tests for Sign and Verify */
  28675. /* Requires SP math and supports P384 or P256 */
  28676. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  28677. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  28678. defined(WOLFSSL_PUBLIC_MP)
  28679. /* ECC Private Key "d" */
  28680. static const byte p256PrivKey[] = {
  28681. /* SECP256R1 */
  28682. /* d */
  28683. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  28684. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  28685. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  28686. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  28687. };
  28688. #ifdef HAVE_ECC384
  28689. static const byte p384PrivKey[] = {
  28690. /* SECP384R1 */
  28691. /* d */
  28692. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  28693. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  28694. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  28695. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  28696. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  28697. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  28698. };
  28699. #endif /* HAVE_ECC384 */
  28700. #ifdef HAVE_ECC521
  28701. static const byte p521PrivKey[] = {
  28702. /* SECP521R1 */
  28703. /* d */
  28704. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  28705. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  28706. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  28707. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  28708. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  28709. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  28710. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  28711. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  28712. 0xa4, 0x36
  28713. };
  28714. #endif /* HAVE_ECC521 */
  28715. /* ECC public key Qx/Qy */
  28716. static const byte p256PubKey[] = {
  28717. /* SECP256R1 */
  28718. /* Qx */
  28719. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  28720. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  28721. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  28722. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  28723. /* Qy */
  28724. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  28725. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  28726. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  28727. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  28728. };
  28729. #ifdef HAVE_ECC384
  28730. static const byte p384PubKey[] = {
  28731. /* SECP384R1 */
  28732. /* Qx */
  28733. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  28734. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  28735. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  28736. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  28737. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  28738. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  28739. /* Qy */
  28740. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  28741. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  28742. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  28743. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  28744. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  28745. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  28746. };
  28747. #endif
  28748. #ifdef HAVE_ECC521
  28749. static const byte p521PubKey[] = {
  28750. /* SECP521R1 */
  28751. /* Qx */
  28752. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  28753. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  28754. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  28755. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  28756. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  28757. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  28758. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  28759. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  28760. 0x8b, 0xbc,
  28761. /* Qy */
  28762. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  28763. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  28764. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  28765. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  28766. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  28767. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  28768. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  28769. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  28770. 0x91, 0xd9
  28771. };
  28772. /* perform verify of signature and hash using public key */
  28773. /* key is public Qx + public Qy */
  28774. /* sig is r + s */
  28775. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  28776. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  28777. uint32_t curveSz, int curveId)
  28778. {
  28779. wc_test_ret_t ret;
  28780. int verify_res = 0, count = 0;
  28781. mp_int r, s;
  28782. ecc_key ecc;
  28783. ecc_nb_ctx_t nb_ctx;
  28784. /* validate arguments */
  28785. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  28786. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  28787. {
  28788. return WC_TEST_RET_ENC_NC;
  28789. }
  28790. /* Setup the ECC key */
  28791. ret = wc_ecc_init(&ecc);
  28792. if (ret < 0) {
  28793. return WC_TEST_RET_ENC_EC(ret);
  28794. }
  28795. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28796. if (ret != MP_OKAY) {
  28797. wc_ecc_free(&ecc);
  28798. return WC_TEST_RET_ENC_EC(ret);
  28799. }
  28800. /* Setup the signature r/s variables */
  28801. ret = mp_init(&r);
  28802. if (ret != MP_OKAY) {
  28803. wc_ecc_free(&ecc);
  28804. return WC_TEST_RET_ENC_EC(ret);
  28805. }
  28806. ret = mp_init(&s);
  28807. if (ret != MP_OKAY) {
  28808. mp_clear(&r);
  28809. wc_ecc_free(&ecc);
  28810. return WC_TEST_RET_ENC_EC(ret);
  28811. }
  28812. /* Import public key x/y */
  28813. ret = wc_ecc_import_unsigned(
  28814. &ecc,
  28815. (byte*)key, /* Public "x" Coordinate */
  28816. (byte*)(key + curveSz), /* Public "y" Coordinate */
  28817. NULL, /* Private "d" (optional) */
  28818. curveId /* ECC Curve Id */
  28819. );
  28820. /* Make sure it was a public key imported */
  28821. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  28822. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  28823. }
  28824. /* Import signature r/s */
  28825. if (ret == 0) {
  28826. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  28827. if (ret < 0)
  28828. ret = WC_TEST_RET_ENC_EC(ret);
  28829. }
  28830. if (ret == 0) {
  28831. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  28832. if (ret < 0)
  28833. ret = WC_TEST_RET_ENC_EC(ret);
  28834. }
  28835. /* Verify ECC Signature */
  28836. if (ret == 0) {
  28837. do {
  28838. ret = wc_ecc_verify_hash_ex(
  28839. &r, &s, /* r/s as mp_int */
  28840. hash, hashSz, /* computed hash digest */
  28841. &verify_res, /* verification result 1=success */
  28842. &ecc
  28843. );
  28844. count++;
  28845. /* This is where real-time work could be called */
  28846. } while (ret == FP_WOULDBLOCK);
  28847. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28848. printf("ECC non-block verify: %d times\n", count);
  28849. #endif
  28850. if (ret < 0)
  28851. ret = WC_TEST_RET_ENC_EC(ret);
  28852. }
  28853. /* check verify result */
  28854. if (ret == 0 && verify_res == 0) {
  28855. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  28856. }
  28857. mp_clear(&r);
  28858. mp_clear(&s);
  28859. wc_ecc_free(&ecc);
  28860. (void)count;
  28861. return ret;
  28862. }
  28863. /* perform signature operation against hash using private key */
  28864. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  28865. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  28866. uint32_t curveSz, int curveId, WC_RNG* rng)
  28867. {
  28868. wc_test_ret_t ret;
  28869. int count = 0;
  28870. mp_int r, s;
  28871. ecc_key ecc;
  28872. ecc_nb_ctx_t nb_ctx;
  28873. /* validate arguments */
  28874. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  28875. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  28876. {
  28877. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  28878. }
  28879. /* Initialize signature result */
  28880. memset(sig, 0, curveSz*2);
  28881. /* Setup the ECC key */
  28882. ret = wc_ecc_init(&ecc);
  28883. if (ret < 0) {
  28884. return WC_TEST_RET_ENC_EC(ret);
  28885. }
  28886. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28887. if (ret != MP_OKAY) {
  28888. wc_ecc_free(&ecc);
  28889. return WC_TEST_RET_ENC_EC(ret);
  28890. }
  28891. /* Setup the signature r/s variables */
  28892. ret = mp_init(&r);
  28893. if (ret != MP_OKAY) {
  28894. wc_ecc_free(&ecc);
  28895. return WC_TEST_RET_ENC_EC(ret);
  28896. }
  28897. ret = mp_init(&s);
  28898. if (ret != MP_OKAY) {
  28899. mp_clear(&r);
  28900. wc_ecc_free(&ecc);
  28901. return WC_TEST_RET_ENC_EC(ret);
  28902. }
  28903. /* Import private key "k" */
  28904. ret = wc_ecc_import_private_key_ex(
  28905. key, keySz, /* private key "d" */
  28906. NULL, 0, /* public (optional) */
  28907. &ecc,
  28908. curveId /* ECC Curve Id */
  28909. );
  28910. if (ret < 0)
  28911. ret = WC_TEST_RET_ENC_EC(ret);
  28912. if (ret == 0) {
  28913. do {
  28914. /* Verify ECC Signature */
  28915. ret = wc_ecc_sign_hash_ex(
  28916. hash, hashSz, /* computed hash digest */
  28917. rng, &ecc, /* random and key context */
  28918. &r, &s /* r/s as mp_int */
  28919. );
  28920. count++;
  28921. /* This is where real-time work could be called */
  28922. } while (ret == FP_WOULDBLOCK);
  28923. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28924. printf("ECC non-block sign: %d times\n", count);
  28925. #endif
  28926. if (ret < 0)
  28927. ret = WC_TEST_RET_ENC_EC(ret);
  28928. }
  28929. if (ret == 0) {
  28930. /* export r/s */
  28931. mp_to_unsigned_bin_len(&r, sig, curveSz);
  28932. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  28933. }
  28934. mp_clear(&r);
  28935. mp_clear(&s);
  28936. wc_ecc_free(&ecc);
  28937. (void)count;
  28938. return ret;
  28939. }
  28940. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  28941. /*
  28942. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  28943. * don't have non-blocking versions of the key checking functions, yet.
  28944. */
  28945. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  28946. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  28947. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  28948. {
  28949. wc_test_ret_t ret;
  28950. ecc_key keyA;
  28951. ecc_key keyB;
  28952. ecc_nb_ctx_t nbCtxA;
  28953. ecc_nb_ctx_t nbCtxB;
  28954. byte secretA[ECC_SHARED_SIZE];
  28955. byte secretB[ECC_SHARED_SIZE];
  28956. word32 secretSzA = ECC_SHARED_SIZE;
  28957. word32 secretSzB = ECC_SHARED_SIZE;
  28958. int count = 0;
  28959. ret = wc_ecc_init(&keyA);
  28960. if (ret == 0) {
  28961. ret = wc_ecc_init(&keyB);
  28962. if (ret < 0)
  28963. ret = WC_TEST_RET_ENC_EC(ret);
  28964. }
  28965. if (ret == 0) {
  28966. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  28967. if (ret < 0)
  28968. ret = WC_TEST_RET_ENC_EC(ret);
  28969. }
  28970. if (ret == 0) {
  28971. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  28972. if (ret < 0)
  28973. ret = WC_TEST_RET_ENC_EC(ret);
  28974. }
  28975. if (ret == 0) {
  28976. do {
  28977. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  28978. count++;
  28979. } while (ret == FP_WOULDBLOCK);
  28980. if (ret < 0)
  28981. ret = WC_TEST_RET_ENC_EC(ret);
  28982. }
  28983. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28984. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  28985. #endif
  28986. if (ret == 0) {
  28987. ret = wc_ecc_check_key(&keyA);
  28988. if (ret < 0)
  28989. ret = WC_TEST_RET_ENC_EC(ret);
  28990. }
  28991. if (ret == 0) {
  28992. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  28993. privKey, curveId);
  28994. if (ret < 0)
  28995. ret = WC_TEST_RET_ENC_EC(ret);
  28996. }
  28997. count = 0;
  28998. if (ret == 0) {
  28999. do {
  29000. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  29001. count++;
  29002. } while (ret == FP_WOULDBLOCK);
  29003. if (ret < 0)
  29004. ret = WC_TEST_RET_ENC_EC(ret);
  29005. }
  29006. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29007. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  29008. #endif
  29009. if (ret == 0) {
  29010. do {
  29011. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  29012. } while (ret == FP_WOULDBLOCK);
  29013. if (ret < 0)
  29014. ret = WC_TEST_RET_ENC_EC(ret);
  29015. }
  29016. if (ret == 0) {
  29017. if (secretSzA != secretSzB ||
  29018. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  29019. ret = WC_TEST_RET_ENC_NC;
  29020. }
  29021. }
  29022. wc_ecc_free(&keyA);
  29023. wc_ecc_free(&keyB);
  29024. return ret;
  29025. }
  29026. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29027. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29028. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  29029. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  29030. WC_RNG* rng)
  29031. {
  29032. wc_test_ret_t ret = 0;
  29033. byte* sig = NULL;
  29034. word32 sigSz = curveSz * 2;
  29035. static const byte hash[] = {
  29036. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  29037. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  29038. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  29039. };
  29040. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29041. if (sig == NULL) {
  29042. ret = WC_TEST_RET_ENC_ERRNO;
  29043. }
  29044. if (ret == 0) {
  29045. /* Sign hash using private key */
  29046. /* Note: result of an ECC sign varies for each call even with same
  29047. private key and hash. This is because a new random public key is
  29048. used for each operation. */
  29049. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  29050. &sigSz, curveSz, curveId, rng);
  29051. }
  29052. if (ret == 0) {
  29053. /* Verify generated signature is valid */
  29054. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  29055. sigSz, curveSz, curveId);
  29056. }
  29057. if (sig != NULL) {
  29058. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29059. }
  29060. return ret;
  29061. }
  29062. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29063. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  29064. {
  29065. wc_test_ret_t ret = 0;
  29066. word32 i;
  29067. int curveIds[3] = {0, 0, 0};
  29068. word32 curveSzs[3] = {0, 0, 0};
  29069. const byte* privKeys[3] = {NULL, NULL, NULL};
  29070. word32 privKeySzs[3] = {0, 0, 0};
  29071. const byte* pubKeys[3] = {NULL, NULL, NULL};
  29072. word32 pubKeySzs[3] = {0, 0, 0};
  29073. curveIds[0] = ECC_SECP256R1;
  29074. curveSzs[0] = 32;
  29075. privKeys[0] = p256PrivKey;
  29076. privKeySzs[0] = sizeof(p256PrivKey);
  29077. pubKeys[0] = p256PubKey;
  29078. pubKeySzs[0] = sizeof(p256PubKey);
  29079. #ifdef HAVE_ECC384
  29080. curveIds[1] = ECC_SECP384R1;
  29081. curveSzs[1] = 48;
  29082. privKeys[1] = p384PrivKey;
  29083. privKeySzs[1] = sizeof(p384PrivKey);
  29084. pubKeys[1] = p384PubKey;
  29085. pubKeySzs[1] = sizeof(p384PubKey);
  29086. #endif
  29087. #ifdef HAVE_ECC521
  29088. curveIds[2] = ECC_SECP521R1;
  29089. curveSzs[2] = 66;
  29090. privKeys[2] = p521PrivKey;
  29091. privKeySzs[2] = sizeof(p521PrivKey);
  29092. pubKeys[2] = p521PubKey;
  29093. pubKeySzs[2] = sizeof(p521PubKey);
  29094. #endif
  29095. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  29096. if (curveIds[i] == 0) {
  29097. continue;
  29098. }
  29099. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29100. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  29101. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  29102. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29103. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29104. if (ret == 0) {
  29105. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  29106. pubKeys[i], rng);
  29107. }
  29108. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29109. }
  29110. return ret;
  29111. }
  29112. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  29113. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29114. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29115. (HAVE_FIPS_VERSION > 2)))
  29116. static int ecc_test_raw_enc_dec(void)
  29117. {
  29118. int ret;
  29119. unsigned char r[1];
  29120. word32 rSz;
  29121. unsigned char s[1];
  29122. word32 sSz;
  29123. unsigned char rZero[] = { 0, 0, 0, 0 };
  29124. unsigned char sOne[] = { 0, 0, 1 };
  29125. unsigned char sigRaw[32];
  29126. word32 sigRawSz;
  29127. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  29128. sigRawSz = sizeof(sigRaw);
  29129. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  29130. sigRaw, &sigRawSz);
  29131. if (ret != 0) {
  29132. return WC_TEST_RET_ENC_EC(ret);
  29133. }
  29134. if (sigRawSz != sizeof(expSig)) {
  29135. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  29136. }
  29137. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  29138. return WC_TEST_RET_ENC_NC;
  29139. }
  29140. rSz = sizeof(r);
  29141. sSz = sizeof(s);
  29142. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  29143. if (ret != 0) {
  29144. return WC_TEST_RET_ENC_EC(ret);
  29145. }
  29146. if (rSz != 1) {
  29147. return WC_TEST_RET_ENC_EC((int)rSz);
  29148. }
  29149. if (sSz != 1) {
  29150. return WC_TEST_RET_ENC_EC((int)sSz);
  29151. }
  29152. if (r[0] != 0) {
  29153. return WC_TEST_RET_ENC_EC(r[0]);
  29154. }
  29155. if (s[0] != 1) {
  29156. return WC_TEST_RET_ENC_EC(s[0]);
  29157. }
  29158. return ret;
  29159. }
  29160. #endif
  29161. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  29162. {
  29163. wc_test_ret_t ret;
  29164. WC_RNG rng;
  29165. WOLFSSL_ENTER("ecc_test");
  29166. #if defined(ECC_MIN_KEY_SZ)
  29167. WOLFSSL_MSG_EX("ecc_test ECC_MIN_KEY_SZ = %d\n", ECC_MIN_KEY_SZ);
  29168. #else
  29169. WOLFSSL_MSG("ecc_test ECC_MIN_KEY_SZ not defined.");
  29170. #endif
  29171. #if defined(WOLFSSL_CERT_EXT) && \
  29172. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29173. ret = ecc_decode_test();
  29174. if (ret < 0)
  29175. return ret;
  29176. #endif
  29177. #ifndef HAVE_FIPS
  29178. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29179. #else
  29180. ret = wc_InitRng(&rng);
  29181. #endif
  29182. #ifndef WC_NO_RNG
  29183. if (ret != 0)
  29184. return WC_TEST_RET_ENC_EC(ret);
  29185. #else
  29186. (void)ret;
  29187. #endif
  29188. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  29189. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  29190. if (ret < 0) {
  29191. printf("keySize=14, Default\n");
  29192. goto done;
  29193. }
  29194. #endif /* HAVE_ECC112 */
  29195. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  29196. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  29197. if (ret < 0) {
  29198. printf("keySize=16, Default\n");
  29199. goto done;
  29200. }
  29201. #endif /* HAVE_ECC128 */
  29202. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  29203. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  29204. if (ret < 0) {
  29205. printf("keySize=20, Default\n");
  29206. goto done;
  29207. }
  29208. #endif /* HAVE_ECC160 */
  29209. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  29210. #if !FIPS_VERSION3_GE(6,0,0)
  29211. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  29212. if (ret < 0) {
  29213. printf("keySize=24, Default\n");
  29214. goto done;
  29215. }
  29216. #endif
  29217. #endif /* HAVE_ECC192 */
  29218. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  29219. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  29220. if (ret < 0) {
  29221. printf("keySize=28, Default\n");
  29222. goto done;
  29223. }
  29224. #endif /* HAVE_ECC224 */
  29225. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  29226. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  29227. if (ret < 0) {
  29228. printf("keySize=30, Default\n");
  29229. goto done;
  29230. }
  29231. #endif /* HAVE_ECC239 */
  29232. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29233. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  29234. if (ret < 0) {
  29235. printf("keySize=32, Default\n");
  29236. goto done;
  29237. }
  29238. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29239. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  29240. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  29241. ret = ecc_point_test();
  29242. if (ret < 0) {
  29243. goto done;
  29244. }
  29245. #endif
  29246. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  29247. ret = ecc_def_curve_test(&rng);
  29248. if (ret < 0) {
  29249. printf("Default\n");
  29250. goto done;
  29251. }
  29252. #endif
  29253. #endif /* !NO_ECC256 */
  29254. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  29255. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  29256. if (ret < 0) {
  29257. printf("keySize=40, Default\n");
  29258. goto done;
  29259. }
  29260. #endif /* HAVE_ECC320 */
  29261. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  29262. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  29263. if (ret < 0) {
  29264. printf("keySize=48, Default\n");
  29265. goto done;
  29266. }
  29267. #endif /* HAVE_ECC384 */
  29268. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  29269. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  29270. if (ret < 0) {
  29271. printf("keySize=64, Default\n");
  29272. goto done;
  29273. }
  29274. #endif /* HAVE_ECC512 */
  29275. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  29276. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  29277. if (ret < 0) {
  29278. printf("keySize=66, Default\n");
  29279. goto done;
  29280. }
  29281. #endif /* HAVE_ECC521 */
  29282. #ifdef WOLFSSL_SM2
  29283. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  29284. if (ret < 0) {
  29285. printf("SM2\n");
  29286. goto done;
  29287. }
  29288. #endif
  29289. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29290. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29291. (HAVE_FIPS_VERSION > 2)))
  29292. ret = ecc_test_raw_enc_dec();
  29293. if (ret != 0) {
  29294. printf("raw sig encode/decode\n");
  29295. goto done;
  29296. }
  29297. #endif
  29298. #if defined(WOLFSSL_CUSTOM_CURVES)
  29299. ret = ecc_test_custom_curves(&rng);
  29300. if (ret != 0) {
  29301. printf("Custom\n");
  29302. goto done;
  29303. }
  29304. #endif
  29305. #if defined(WOLFSSL_SM2)
  29306. ret = test_sm2_verify();
  29307. if (ret != 0) {
  29308. printf("SM2 Verify\n");
  29309. goto done;
  29310. }
  29311. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  29312. if (ret != 0) {
  29313. printf("SM2 test\n");
  29314. goto done;
  29315. }
  29316. #endif
  29317. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  29318. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  29319. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  29320. #ifdef HAVE_ECC256
  29321. ret = ecc_test_deterministic_k(&rng);
  29322. if (ret != 0) {
  29323. printf("ecc_test_deterministic_k failed!\n");
  29324. goto done;
  29325. }
  29326. #endif
  29327. #ifdef WOLFSSL_PUBLIC_MP
  29328. #if defined(HAVE_ECC384)
  29329. ret = ecc384_test_deterministic_k(&rng);
  29330. if (ret != 0) {
  29331. printf("ecc384_test_deterministic_k failed!\n");
  29332. goto done;
  29333. }
  29334. #endif
  29335. #if defined(HAVE_ECC521)
  29336. ret = ecc521_test_deterministic_k(&rng);
  29337. if (ret != 0) {
  29338. printf("ecc512_test_deterministic_k failed!\n");
  29339. goto done;
  29340. }
  29341. #endif
  29342. #endif
  29343. #endif
  29344. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  29345. !defined(WOLFSSL_KCAPI_ECC)
  29346. ret = ecc_test_sign_vectors(&rng);
  29347. if (ret != 0) {
  29348. printf("ecc_test_sign_vectors failed!\n");
  29349. goto done;
  29350. }
  29351. #endif
  29352. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  29353. defined(HAVE_ECC_DHE)
  29354. ret = ecc_test_cdh_vectors(&rng);
  29355. if (ret != 0) {
  29356. printf("ecc_test_cdh_vectors failed!\n");
  29357. goto done;
  29358. }
  29359. #endif
  29360. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29361. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  29362. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  29363. ret = ecc_test_make_pub(&rng);
  29364. if (ret != 0) {
  29365. printf("ecc_test_make_pub failed!\n");
  29366. goto done;
  29367. }
  29368. #elif defined(HAVE_ECC_KEY_IMPORT)
  29369. (void)ecc_test_make_pub; /* for compiler warning */
  29370. #endif
  29371. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  29372. ret = ecc_test_cert_gen(&rng);
  29373. if (ret != 0) {
  29374. printf("ecc_test_cert_gen failed!\n");
  29375. goto done;
  29376. }
  29377. #endif
  29378. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  29379. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  29380. defined(WOLFSSL_CUSTOM_CURVES))
  29381. ret = ecc_test_allocator(&rng);
  29382. if (ret != 0) {
  29383. printf("ecc_test_allocator failed!\n");
  29384. goto done;
  29385. }
  29386. #endif
  29387. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  29388. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29389. ret = ecc_test_nonblock(&rng);
  29390. if (ret != 0) {
  29391. printf("ecc_test_nonblock failed!\n");
  29392. goto done;
  29393. }
  29394. #endif
  29395. done:
  29396. wc_FreeRng(&rng);
  29397. return ret;
  29398. }
  29399. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  29400. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  29401. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  29402. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  29403. {
  29404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29405. byte* plaintext;
  29406. byte* encrypted;
  29407. byte* decrypted;
  29408. #else
  29409. byte plaintext[128];
  29410. byte encrypted[128];
  29411. byte decrypted[128];
  29412. #endif
  29413. ecEncCtx* aCtx = NULL;
  29414. ecEncCtx* bCtx = NULL;
  29415. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  29416. 14, 15};
  29417. wc_test_ret_t ret = 0;
  29418. static const char message[] = "Hello wolfSSL!";
  29419. word32 plaintextLen;
  29420. word32 encryptLen = 128;
  29421. word32 decryptLen = 128;
  29422. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29423. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29424. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29425. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29426. #endif
  29427. wc_ecc_free(a);
  29428. wc_ecc_free(b);
  29429. ret = wc_ecc_init(a);
  29430. if (ret != 0)
  29431. ret = WC_TEST_RET_ENC_EC(ret);
  29432. if (ret == 0) {
  29433. ret = wc_ecc_init(b);
  29434. if (ret != 0)
  29435. ret = WC_TEST_RET_ENC_EC(ret);
  29436. }
  29437. if (ret == 0)
  29438. ret = wc_ecc_make_key(rng, 32, a);
  29439. if (ret == 0)
  29440. ret = wc_ecc_make_key(rng, 32, b);
  29441. /* create context */
  29442. if (ret == 0) {
  29443. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29444. if (aCtx == NULL)
  29445. ret = WC_TEST_RET_ENC_NC;
  29446. }
  29447. if (ret == 0) {
  29448. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29449. if (bCtx == NULL)
  29450. ret = WC_TEST_RET_ENC_NC;
  29451. }
  29452. /* set salt */
  29453. if (ret == 0) {
  29454. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  29455. if (ret != 0)
  29456. ret = 10472;
  29457. }
  29458. if (ret == 0) {
  29459. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  29460. if (ret != 0)
  29461. ret = 10473;
  29462. }
  29463. XMEMSET(plaintext, 0, 128);
  29464. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  29465. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  29466. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  29467. /* encrypt */
  29468. if (ret == 0) {
  29469. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  29470. &encryptLen, aCtx);
  29471. if (ret != 0)
  29472. ret = WC_TEST_RET_ENC_EC(ret);
  29473. }
  29474. /* decrypt */
  29475. if (ret == 0) {
  29476. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  29477. &decryptLen, bCtx);
  29478. if (ret != 0)
  29479. ret = WC_TEST_RET_ENC_EC(ret);
  29480. }
  29481. /* compare */
  29482. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  29483. ret = WC_TEST_RET_ENC_NC;
  29484. wc_ecc_free(a);
  29485. wc_ecc_free(b);
  29486. wc_ecc_ctx_free(aCtx);
  29487. wc_ecc_ctx_free(bCtx);
  29488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29489. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29490. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29491. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29492. #endif
  29493. return ret;
  29494. }
  29495. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  29496. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  29497. * wolfFIPS 5.3.
  29498. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  29499. */
  29500. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  29501. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  29502. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  29503. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  29504. {
  29505. wc_test_ret_t ret = 0;
  29506. #ifdef WOLFSSL_ECIES_OLD
  29507. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29508. ecc_key* userA = NULL;
  29509. #else
  29510. ecc_key userA[1];
  29511. #endif
  29512. int userAInit = 0;
  29513. #endif
  29514. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29515. ecc_key* userB = NULL;
  29516. #else
  29517. ecc_key userB[1];
  29518. #endif
  29519. int userBInit = 0;
  29520. ecc_key* tmpKey;
  29521. byte plain[48];
  29522. word32 plainSz = sizeof(plain);
  29523. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  29524. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  29525. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  29526. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  29527. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  29528. };
  29529. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  29530. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  29531. 0x04,
  29532. /* X */
  29533. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29534. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29535. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29536. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29537. /* X */
  29538. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29539. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29540. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29541. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  29542. };
  29543. #endif
  29544. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  29545. #ifdef WOLFSSL_ECIES_OLD
  29546. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  29547. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  29548. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  29549. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  29550. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  29551. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  29552. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  29553. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  29554. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  29555. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  29556. #elif defined(WOLFSSL_ECIES_ISO18033)
  29557. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29558. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29559. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29560. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29561. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29562. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29563. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29564. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29565. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  29566. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  29567. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  29568. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  29569. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  29570. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  29571. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  29572. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  29573. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  29574. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  29575. 0x7f
  29576. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29577. /* EC P-256 point */
  29578. 0x04,
  29579. /* X */
  29580. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29581. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29582. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29583. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29584. /* Y */
  29585. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29586. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29587. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29588. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  29589. /* IV */
  29590. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29591. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29592. /* Encrypted Msg */
  29593. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  29594. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  29595. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  29596. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  29597. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  29598. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  29599. /* HMAC */
  29600. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  29601. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  29602. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  29603. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  29604. #else
  29605. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29606. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29607. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29608. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29609. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29610. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29611. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29612. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29613. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  29614. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  29615. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  29616. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  29617. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  29618. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  29619. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  29620. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  29621. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  29622. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  29623. 0x55
  29624. #endif
  29625. };
  29626. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  29627. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29628. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  29629. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  29630. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  29631. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  29632. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  29633. };
  29634. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29635. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  29636. DYNAMIC_TYPE_TMP_BUFFER);
  29637. if (userB == NULL) {
  29638. ret = WC_TEST_RET_ENC_ERRNO;
  29639. }
  29640. #ifdef WOLFSSL_ECIES_OLD
  29641. if (ret == 0) {
  29642. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  29643. DYNAMIC_TYPE_TMP_BUFFER);
  29644. if (userA == NULL) {
  29645. ret = WC_TEST_RET_ENC_ERRNO;
  29646. }
  29647. }
  29648. #endif
  29649. #endif
  29650. if (ret == 0) {
  29651. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  29652. if (ret != 0)
  29653. ret = WC_TEST_RET_ENC_EC(ret);
  29654. }
  29655. if (ret == 0) {
  29656. userBInit = 1;
  29657. #ifdef WOLFSSL_ECIES_OLD
  29658. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  29659. if (ret != 0)
  29660. ret = WC_TEST_RET_ENC_EC(ret);
  29661. }
  29662. if (ret == 0) {
  29663. userAInit = 1;
  29664. tmpKey = userA;
  29665. #else
  29666. tmpKey = NULL;
  29667. #endif
  29668. }
  29669. if (ret == 0) {
  29670. #ifdef WOLFSSL_QNX_CAAM
  29671. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  29672. sizeof(pubKey), userB, ECC_SECP256R1);
  29673. #else
  29674. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  29675. userB, ECC_SECP256R1);
  29676. #endif
  29677. if (ret != 0)
  29678. ret = WC_TEST_RET_ENC_EC(ret);
  29679. }
  29680. #ifdef WOLFSSL_ECIES_OLD
  29681. if (ret == 0) {
  29682. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  29683. ECC_SECP256R1);
  29684. if (ret != 0)
  29685. ret = WC_TEST_RET_ENC_EC(ret);
  29686. }
  29687. #endif
  29688. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  29689. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  29690. !defined(HAVE_SELFTEST)
  29691. if (ret == 0) {
  29692. ret = wc_ecc_set_rng(userB, rng);
  29693. if (ret != 0) {
  29694. ret = WC_TEST_RET_ENC_EC(ret);
  29695. }
  29696. }
  29697. #else
  29698. (void)rng;
  29699. #endif
  29700. if (ret == 0) {
  29701. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  29702. &plainSz, NULL);
  29703. if (ret != 0)
  29704. ret = WC_TEST_RET_ENC_EC(ret);
  29705. }
  29706. if (ret == 0) {
  29707. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29708. ret = WC_TEST_RET_ENC_NC;
  29709. }
  29710. }
  29711. if (userBInit)
  29712. wc_ecc_free(userB);
  29713. #ifdef WOLFSSL_ECIES_OLD
  29714. if (userAInit)
  29715. wc_ecc_free(userA);
  29716. #endif
  29717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29718. if (userB != NULL) {
  29719. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29720. }
  29721. #ifdef WOLFSSL_ECIES_OLD
  29722. if (userA != NULL) {
  29723. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29724. }
  29725. #endif
  29726. #endif
  29727. return ret;
  29728. }
  29729. #endif
  29730. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  29731. byte encAlgo, byte kdfAlgo, byte macAlgo)
  29732. {
  29733. wc_test_ret_t ret = 0;
  29734. byte msg[48];
  29735. byte plain[48];
  29736. #ifdef WOLFSSL_ECIES_OLD
  29737. byte out[80];
  29738. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29739. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29740. #else
  29741. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29742. #endif
  29743. word32 outSz = sizeof(out);
  29744. word32 plainSz = sizeof(plain);
  29745. int i;
  29746. ecEncCtx* cliCtx = NULL;
  29747. ecEncCtx* srvCtx = NULL;
  29748. byte cliSalt[EXCHANGE_SALT_SZ];
  29749. byte srvSalt[EXCHANGE_SALT_SZ];
  29750. const byte* tmpSalt;
  29751. byte msg2[48];
  29752. byte plain2[48];
  29753. #ifdef WOLFSSL_ECIES_OLD
  29754. byte out2[80];
  29755. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29756. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29757. #else
  29758. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29759. #endif
  29760. word32 outSz2 = sizeof(out2);
  29761. word32 plainSz2 = sizeof(plain2);
  29762. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29763. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  29764. DYNAMIC_TYPE_TMP_BUFFER);
  29765. #else
  29766. ecc_key tmpKey[1];
  29767. #endif
  29768. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29769. if (tmpKey == NULL) {
  29770. ERROR_OUT(MEMORY_E, done);
  29771. }
  29772. #endif
  29773. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  29774. if (ret != 0)
  29775. goto done;
  29776. /* set message to incrementing 0,1,2,etc... */
  29777. for (i = 0; i < (int)sizeof(msg); i++)
  29778. msg[i] = i;
  29779. /* encrypt msg to B */
  29780. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  29781. if (ret != 0) {
  29782. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29783. }
  29784. #ifdef WOLFSSL_ECIES_OLD
  29785. tmpKey->dp = userA->dp;
  29786. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  29787. if (ret != 0) {
  29788. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29789. }
  29790. #endif
  29791. /* decrypt msg from A */
  29792. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  29793. if (ret != 0) {
  29794. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29795. }
  29796. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29797. ret = WC_TEST_RET_ENC_NC; goto done;
  29798. }
  29799. #ifndef WOLFSSL_ECIES_OLD
  29800. /* A decrypts msg (response) from B */
  29801. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  29802. if (ret != 0)
  29803. goto done;
  29804. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29805. ret = WC_TEST_RET_ENC_NC; goto done;
  29806. }
  29807. #endif
  29808. /* let's verify message exchange works, A is client, B is server */
  29809. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29810. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29811. if (cliCtx == NULL || srvCtx == NULL) {
  29812. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  29813. }
  29814. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  29815. if (ret != 0)
  29816. goto done;
  29817. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  29818. if (ret != 0)
  29819. goto done;
  29820. /* get salt to send to peer */
  29821. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  29822. if (tmpSalt == NULL) {
  29823. ret = WC_TEST_RET_ENC_NC; goto done;
  29824. }
  29825. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29826. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  29827. if (tmpSalt == NULL) {
  29828. ret = WC_TEST_RET_ENC_NC; goto done;
  29829. }
  29830. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29831. /* in actual use, we'd get the peer's salt over the transport */
  29832. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  29833. if (ret != 0)
  29834. goto done;
  29835. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  29836. if (ret != 0)
  29837. goto done;
  29838. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  29839. if (ret != 0)
  29840. goto done;
  29841. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  29842. if (ret != 0)
  29843. goto done;
  29844. /* get encrypted msg (request) to send to B */
  29845. outSz = sizeof(out);
  29846. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  29847. if (ret != 0)
  29848. goto done;
  29849. #ifndef WOLFSSL_ECIES_OLD
  29850. wc_ecc_free(tmpKey);
  29851. #endif
  29852. /* B decrypts msg (request) from A */
  29853. plainSz = sizeof(plain);
  29854. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  29855. if (ret != 0)
  29856. goto done;
  29857. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29858. ret = WC_TEST_RET_ENC_NC; goto done;
  29859. }
  29860. /* msg2 (response) from B to A */
  29861. for (i = 0; i < (int)sizeof(msg2); i++)
  29862. msg2[i] = i + sizeof(msg2);
  29863. /* get encrypted msg (response) to send to B */
  29864. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  29865. &outSz2, srvCtx);
  29866. if (ret != 0)
  29867. goto done;
  29868. #ifdef WOLFSSL_ECIES_OLD
  29869. tmpKey->dp = userB->dp;
  29870. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  29871. if (ret != 0) {
  29872. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29873. }
  29874. #else
  29875. wc_ecc_free(tmpKey);
  29876. #endif
  29877. /* A decrypts msg (response) from B */
  29878. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  29879. cliCtx);
  29880. if (ret != 0)
  29881. goto done;
  29882. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  29883. ret = WC_TEST_RET_ENC_NC; goto done;
  29884. }
  29885. #if defined(HAVE_COMP_KEY) && \
  29886. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  29887. /* Create new client and server contexts. */
  29888. wc_ecc_ctx_free(srvCtx);
  29889. wc_ecc_ctx_free(cliCtx);
  29890. /* let's verify message exchange works, A is client, B is server */
  29891. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29892. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29893. if (cliCtx == NULL || srvCtx == NULL) {
  29894. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  29895. }
  29896. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  29897. if (ret != 0)
  29898. goto done;
  29899. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  29900. if (ret != 0)
  29901. goto done;
  29902. /* get salt to send to peer */
  29903. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  29904. if (tmpSalt == NULL) {
  29905. ret = WC_TEST_RET_ENC_NC; goto done;
  29906. }
  29907. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29908. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  29909. if (tmpSalt == NULL) {
  29910. ret = WC_TEST_RET_ENC_NC; goto done;
  29911. }
  29912. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29913. /* in actual use, we'd get the peer's salt over the transport */
  29914. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  29915. if (ret != 0)
  29916. goto done;
  29917. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  29918. if (ret != 0)
  29919. goto done;
  29920. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  29921. if (ret != 0)
  29922. goto done;
  29923. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  29924. if (ret != 0)
  29925. goto done;
  29926. /* get encrypted msg (request) to send to B - compressed public key */
  29927. outSz = sizeof(out);
  29928. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  29929. 1);
  29930. if (ret != 0)
  29931. goto done;
  29932. #ifndef WOLFSSL_ECIES_OLD
  29933. wc_ecc_free(tmpKey);
  29934. #endif
  29935. /* B decrypts msg (request) from A - out has a compressed public key */
  29936. plainSz = sizeof(plain);
  29937. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  29938. if (ret != 0)
  29939. goto done;
  29940. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29941. ret = WC_TEST_RET_ENC_NC; goto done;
  29942. }
  29943. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  29944. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  29945. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  29946. ret = ecc_encrypt_kat(rng);
  29947. #endif
  29948. done:
  29949. /* cleanup */
  29950. wc_ecc_ctx_free(srvCtx);
  29951. wc_ecc_ctx_free(cliCtx);
  29952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29953. if (tmpKey != NULL) {
  29954. wc_ecc_free(tmpKey);
  29955. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29956. }
  29957. #else
  29958. wc_ecc_free(tmpKey);
  29959. #endif
  29960. return ret;
  29961. }
  29962. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  29963. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  29964. {
  29965. WC_RNG rng;
  29966. wc_test_ret_t ret;
  29967. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29968. ecc_key *userA;
  29969. ecc_key *userB;
  29970. #else
  29971. ecc_key userA[1];
  29972. ecc_key userB[1];
  29973. #endif
  29974. WOLFSSL_ENTER("ecc_encrypt_test");
  29975. #ifndef HAVE_FIPS
  29976. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29977. #else
  29978. ret = wc_InitRng(&rng);
  29979. #endif
  29980. if (ret != 0)
  29981. return WC_TEST_RET_ENC_EC(ret);
  29982. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29983. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  29984. DYNAMIC_TYPE_TMP_BUFFER);
  29985. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  29986. DYNAMIC_TYPE_TMP_BUFFER);
  29987. if ((userA == NULL) || (userB == NULL)) {
  29988. ERROR_OUT(MEMORY_E, done);
  29989. }
  29990. #endif
  29991. XMEMSET(userA, 0, sizeof *userA);
  29992. XMEMSET(userB, 0, sizeof *userB);
  29993. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  29994. if (ret != 0)
  29995. goto done;
  29996. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  29997. if (ret != 0)
  29998. goto done;
  29999. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  30000. #if defined(WOLFSSL_ASYNC_CRYPT)
  30001. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  30002. #endif
  30003. if (ret != 0){
  30004. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30005. }
  30006. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  30007. #if defined(WOLFSSL_ASYNC_CRYPT)
  30008. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  30009. #endif
  30010. if (ret != 0){
  30011. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30012. }
  30013. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30014. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30015. !defined(HAVE_SELFTEST)
  30016. ret = wc_ecc_set_rng(userA, &rng);
  30017. if (ret != 0) {
  30018. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30019. }
  30020. ret = wc_ecc_set_rng(userB, &rng);
  30021. if (ret != 0) {
  30022. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30023. }
  30024. #endif
  30025. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  30026. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30027. #ifdef WOLFSSL_AES_128
  30028. if (ret == 0) {
  30029. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30030. ecHKDF_SHA256, ecHMAC_SHA256);
  30031. if (ret != 0) {
  30032. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30033. }
  30034. }
  30035. #ifdef HAVE_X963_KDF
  30036. if (ret == 0) {
  30037. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30038. ecKDF_X963_SHA256, ecHMAC_SHA256);
  30039. if (ret != 0) {
  30040. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  30041. }
  30042. }
  30043. if (ret == 0) {
  30044. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30045. ecKDF_SHA256, ecHMAC_SHA256);
  30046. if (ret != 0) {
  30047. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  30048. }
  30049. }
  30050. #endif
  30051. #endif
  30052. #ifdef WOLFSSL_AES_256
  30053. if (ret == 0) {
  30054. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  30055. ecHKDF_SHA256, ecHMAC_SHA256);
  30056. if (ret != 0) {
  30057. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30058. }
  30059. }
  30060. #endif
  30061. #endif
  30062. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  30063. #ifdef WOLFSSL_AES_128
  30064. if (ret == 0) {
  30065. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  30066. ecHKDF_SHA256, ecHMAC_SHA256);
  30067. if (ret != 0) {
  30068. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30069. }
  30070. }
  30071. #endif
  30072. #ifdef WOLFSSL_AES_256
  30073. if (ret == 0) {
  30074. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  30075. ecHKDF_SHA256, ecHMAC_SHA256);
  30076. if (ret != 0) {
  30077. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30078. }
  30079. }
  30080. #endif
  30081. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  30082. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30083. if (ret == 0) {
  30084. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  30085. }
  30086. #endif
  30087. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30088. done:
  30089. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30090. if (userA != NULL) {
  30091. wc_ecc_free(userA);
  30092. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30093. }
  30094. if (userB != NULL) {
  30095. wc_ecc_free(userB);
  30096. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30097. }
  30098. #else
  30099. wc_ecc_free(userB);
  30100. wc_ecc_free(userA);
  30101. #endif
  30102. wc_FreeRng(&rng);
  30103. return ret;
  30104. }
  30105. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  30106. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  30107. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  30108. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  30109. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  30110. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  30111. {
  30112. size_t bytes;
  30113. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30114. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30115. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30116. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30117. #else
  30118. ecc_key cliKey[1];
  30119. ecc_key servKey[1];
  30120. ecc_key tmpKey[1];
  30121. #endif
  30122. WC_RNG rng;
  30123. word32 idx = 0;
  30124. wc_test_ret_t ret;
  30125. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  30126. byte in[] = "Everyone gets Friday off. ecc p";
  30127. word32 inLen = (word32)XSTRLEN((char*)in);
  30128. byte out[256];
  30129. byte plain[256];
  30130. int verify = 0;
  30131. word32 x;
  30132. WOLFSSL_ENTER("ecc_test_buffers");
  30133. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30134. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  30135. ERROR_OUT(MEMORY_E, done);
  30136. #endif
  30137. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  30138. if (ret != 0)
  30139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30140. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  30141. if (ret != 0)
  30142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30143. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  30144. if (ret != 0)
  30145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30146. bytes = (size_t)sizeof_ecc_clikey_der_256;
  30147. /* place client key into ecc_key struct cliKey */
  30148. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  30149. (word32)bytes);
  30150. if (ret != 0)
  30151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30152. idx = 0;
  30153. bytes = (size_t)sizeof_ecc_key_der_256;
  30154. /* place server key into ecc_key struct servKey */
  30155. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  30156. (word32)bytes);
  30157. if (ret != 0)
  30158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30159. #ifndef WC_NO_RNG
  30160. #ifndef HAVE_FIPS
  30161. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30162. #else
  30163. ret = wc_InitRng(&rng);
  30164. #endif
  30165. if (ret != 0)
  30166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30167. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30168. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30169. !defined(HAVE_SELFTEST)
  30170. ret = wc_ecc_set_rng(cliKey, &rng);
  30171. if (ret != 0)
  30172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30173. ret = wc_ecc_set_rng(servKey, &rng);
  30174. if (ret != 0)
  30175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30176. #endif
  30177. #endif /* !WC_NO_RNG */
  30178. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  30179. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30180. {
  30181. word32 y;
  30182. /* test encrypt and decrypt if they're available */
  30183. x = sizeof(out);
  30184. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  30185. if (ret < 0)
  30186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30187. #ifdef WOLFSSL_ECIES_OLD
  30188. tmpKey->dp = cliKey->dp;
  30189. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  30190. if (ret != 0) {
  30191. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30192. }
  30193. #endif
  30194. y = sizeof(plain);
  30195. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  30196. if (ret < 0)
  30197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30198. if (XMEMCMP(plain, in, inLen))
  30199. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30200. }
  30201. #endif
  30202. x = sizeof(out);
  30203. do {
  30204. #if defined(WOLFSSL_ASYNC_CRYPT)
  30205. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30206. #endif
  30207. if (ret == 0)
  30208. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  30209. } while (ret == WC_PENDING_E);
  30210. if (ret < 0)
  30211. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30212. TEST_SLEEP();
  30213. XMEMSET(plain, 0, sizeof(plain));
  30214. do {
  30215. #if defined(WOLFSSL_ASYNC_CRYPT)
  30216. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30217. #endif
  30218. if (ret == 0)
  30219. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  30220. cliKey);
  30221. } while (ret == WC_PENDING_E);
  30222. if (ret < 0)
  30223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30224. if (verify != 1)
  30225. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30226. TEST_SLEEP();
  30227. #ifdef WOLFSSL_CERT_EXT
  30228. idx = 0;
  30229. bytes = sizeof_ecc_clikeypub_der_256;
  30230. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  30231. (word32) bytes);
  30232. if (ret != 0)
  30233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30234. #endif
  30235. ret = 0;
  30236. done:
  30237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30238. if (cliKey != NULL) {
  30239. wc_ecc_free(cliKey);
  30240. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30241. }
  30242. if (servKey != NULL) {
  30243. wc_ecc_free(servKey);
  30244. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30245. }
  30246. if (tmpKey != NULL) {
  30247. wc_ecc_free(tmpKey);
  30248. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30249. }
  30250. #else
  30251. wc_ecc_free(cliKey);
  30252. wc_ecc_free(servKey);
  30253. wc_ecc_free(tmpKey);
  30254. #endif
  30255. wc_FreeRng(&rng);
  30256. return ret;
  30257. }
  30258. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  30259. #endif /* HAVE_ECC */
  30260. #ifdef HAVE_CURVE25519
  30261. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30262. defined(HAVE_CURVE25519_KEY_IMPORT)
  30263. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30264. #define X25519_TEST_CNT 5
  30265. #else
  30266. #define X25519_TEST_CNT 1
  30267. #endif
  30268. static wc_test_ret_t curve25519_overflow_test(void)
  30269. {
  30270. /* secret key for party a */
  30271. byte sa[X25519_TEST_CNT][32] = {
  30272. {
  30273. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  30274. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  30275. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  30276. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  30277. },
  30278. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30279. {
  30280. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  30281. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  30282. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  30283. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  30284. },
  30285. {
  30286. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30287. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30288. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30289. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30290. },
  30291. {
  30292. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30293. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30294. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30295. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30296. },
  30297. {
  30298. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30299. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30300. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30301. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30302. }
  30303. #endif
  30304. };
  30305. /* public key for party b */
  30306. byte pb[X25519_TEST_CNT][32] = {
  30307. {
  30308. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30309. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30310. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30311. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30312. },
  30313. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30314. {
  30315. /* 0xff first byte in original - invalid! */
  30316. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30317. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30318. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30319. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30320. },
  30321. {
  30322. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  30323. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  30324. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  30325. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  30326. },
  30327. {
  30328. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  30329. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  30330. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  30331. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  30332. },
  30333. {
  30334. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  30335. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  30336. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  30337. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  30338. }
  30339. #endif
  30340. };
  30341. /* expected shared key */
  30342. byte ss[X25519_TEST_CNT][32] = {
  30343. {
  30344. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  30345. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  30346. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  30347. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  30348. },
  30349. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30350. {
  30351. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  30352. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  30353. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  30354. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  30355. },
  30356. {
  30357. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30358. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30359. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30360. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  30361. },
  30362. {
  30363. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30364. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30365. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30366. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  30367. },
  30368. {
  30369. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30370. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30371. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30372. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  30373. }
  30374. #endif
  30375. };
  30376. wc_test_ret_t ret = 0;
  30377. int i;
  30378. word32 y;
  30379. byte shared[32];
  30380. curve25519_key userA;
  30381. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30382. for (i = 0; i < X25519_TEST_CNT; i++) {
  30383. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  30384. sizeof(pb[i]), &userA) != 0) {
  30385. ret = WC_TEST_RET_ENC_I(i); break;
  30386. }
  30387. /* test against known test vector */
  30388. XMEMSET(shared, 0, sizeof(shared));
  30389. y = sizeof(shared);
  30390. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  30391. ret = WC_TEST_RET_ENC_I(i); break;
  30392. }
  30393. if (XMEMCMP(ss[i], shared, y)) {
  30394. ret = WC_TEST_RET_ENC_I(i); break;
  30395. }
  30396. }
  30397. wc_curve25519_free(&userA);
  30398. return ret;
  30399. }
  30400. /* Test the wc_curve25519_check_public API.
  30401. *
  30402. * returns 0 on success and -ve on failure.
  30403. */
  30404. static wc_test_ret_t curve25519_check_public_test(void)
  30405. {
  30406. wc_test_ret_t ret;
  30407. /* Little-endian values that will fail */
  30408. byte fail_le[][CURVE25519_KEYSIZE] = {
  30409. {
  30410. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30411. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30412. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30413. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30414. },
  30415. {
  30416. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30417. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30420. },
  30421. {
  30422. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30423. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30424. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30425. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  30426. },
  30427. };
  30428. /* Big-endian values that will fail */
  30429. byte fail_be[][CURVE25519_KEYSIZE] = {
  30430. {
  30431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30433. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30435. },
  30436. {
  30437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30441. },
  30442. {
  30443. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30444. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30445. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30446. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30447. },
  30448. };
  30449. /* Good or valid public value */
  30450. byte good[CURVE25519_KEYSIZE] = {
  30451. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30452. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30453. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30454. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30455. };
  30456. int i;
  30457. /* Parameter checks */
  30458. /* NULL pointer */
  30459. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  30460. if (ret != BAD_FUNC_ARG) {
  30461. return WC_TEST_RET_ENC_EC(ret);
  30462. }
  30463. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  30464. if (ret != BAD_FUNC_ARG) {
  30465. return WC_TEST_RET_ENC_EC(ret);
  30466. }
  30467. /* Length of 0 treated differently to other invalid lengths for TLS */
  30468. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  30469. if (ret != BUFFER_E)
  30470. return WC_TEST_RET_ENC_EC(ret);
  30471. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  30472. if (ret != BUFFER_E)
  30473. return WC_TEST_RET_ENC_EC(ret);
  30474. /* Length not CURVE25519_KEYSIZE */
  30475. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  30476. if (i == CURVE25519_KEYSIZE)
  30477. continue;
  30478. if (wc_curve25519_check_public(good, (word32)i, EC25519_LITTLE_ENDIAN) !=
  30479. ECC_BAD_ARG_E) {
  30480. return WC_TEST_RET_ENC_I(i);
  30481. }
  30482. if (wc_curve25519_check_public(good, (word32)i, EC25519_BIG_ENDIAN) !=
  30483. ECC_BAD_ARG_E) {
  30484. return WC_TEST_RET_ENC_I(i);
  30485. }
  30486. }
  30487. /* Little-endian fail cases */
  30488. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  30489. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  30490. EC25519_LITTLE_ENDIAN) == 0) {
  30491. return WC_TEST_RET_ENC_I(i);
  30492. }
  30493. }
  30494. /* Big-endian fail cases */
  30495. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  30496. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  30497. EC25519_BIG_ENDIAN) == 0) {
  30498. return WC_TEST_RET_ENC_I(i);
  30499. }
  30500. }
  30501. /* Check a valid public value works! */
  30502. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30503. EC25519_LITTLE_ENDIAN);
  30504. if (ret != 0) {
  30505. return WC_TEST_RET_ENC_EC(ret);
  30506. }
  30507. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30508. EC25519_BIG_ENDIAN);
  30509. if (ret != 0) {
  30510. return WC_TEST_RET_ENC_EC(ret);
  30511. }
  30512. return 0;
  30513. }
  30514. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30515. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30516. defined(HAVE_CURVE25519_KEY_IMPORT)
  30517. static wc_test_ret_t curve255519_der_test(void)
  30518. {
  30519. wc_test_ret_t ret = 0;
  30520. /* certs/statickeys/x25519.der */
  30521. const byte kCurve25519PrivDer[] = {
  30522. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  30523. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  30524. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  30525. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  30526. };
  30527. /* certs/statickeys/x25519-pub.der */
  30528. const byte kCurve25519PubDer[] = {
  30529. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  30530. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  30531. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  30532. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  30533. };
  30534. curve25519_key key;
  30535. byte output[128];
  30536. word32 outputSz = 128;
  30537. word32 idx;
  30538. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  30539. if (ret != 0)
  30540. return WC_TEST_RET_ENC_EC(ret);
  30541. /* Test decode / encode of Curve25519 private key only */
  30542. if (ret == 0) {
  30543. idx = 0;
  30544. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  30545. (word32)sizeof(kCurve25519PrivDer));
  30546. if (ret < 0)
  30547. ret = WC_TEST_RET_ENC_EC(ret);
  30548. }
  30549. if (ret == 0) {
  30550. outputSz = (word32)sizeof(output);
  30551. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  30552. if (ret >= 0) {
  30553. outputSz = (word32)ret;
  30554. ret = 0;
  30555. }
  30556. else {
  30557. ret = WC_TEST_RET_ENC_EC(ret);
  30558. }
  30559. }
  30560. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  30561. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  30562. ret = WC_TEST_RET_ENC_NC;
  30563. }
  30564. /* Test decode / encode of Curve25519 public key only */
  30565. if (ret == 0) {
  30566. idx = 0;
  30567. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  30568. (word32)sizeof(kCurve25519PubDer));
  30569. if (ret < 0)
  30570. ret = WC_TEST_RET_ENC_EC(ret);
  30571. }
  30572. if (ret == 0) {
  30573. outputSz = (word32)sizeof(output);
  30574. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  30575. if (ret >= 0) {
  30576. outputSz = (word32)ret;
  30577. ret = 0;
  30578. }
  30579. else {
  30580. ret = WC_TEST_RET_ENC_EC(ret);
  30581. }
  30582. }
  30583. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  30584. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  30585. ret = WC_TEST_RET_ENC_NC;
  30586. }
  30587. wc_curve25519_free(&key);
  30588. return ret;
  30589. }
  30590. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  30591. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  30592. {
  30593. WC_RNG rng;
  30594. wc_test_ret_t ret;
  30595. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30596. byte sharedA[32];
  30597. byte sharedB[32];
  30598. word32 y;
  30599. #endif
  30600. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30601. byte exportBuf[32];
  30602. #endif
  30603. word32 x = 0;
  30604. curve25519_key userA, userB, pubKey;
  30605. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30606. defined(HAVE_CURVE25519_KEY_IMPORT)
  30607. /* test vectors from
  30608. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  30609. */
  30610. /* secret key for party a */
  30611. byte sa[] = {
  30612. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  30613. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  30614. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  30615. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  30616. };
  30617. /* public key for party a */
  30618. byte pa[] = {
  30619. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  30620. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  30621. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  30622. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  30623. };
  30624. /* secret key for party b */
  30625. byte sb[] = {
  30626. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  30627. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  30628. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  30629. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  30630. };
  30631. /* public key for party b */
  30632. byte pb[] = {
  30633. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  30634. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  30635. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  30636. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  30637. };
  30638. /* expected shared key */
  30639. byte ss[] = {
  30640. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  30641. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  30642. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  30643. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  30644. };
  30645. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  30646. (void)x;
  30647. WOLFSSL_ENTER("curve25519_test");
  30648. #ifndef HAVE_FIPS
  30649. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30650. #else
  30651. ret = wc_InitRng(&rng);
  30652. #endif
  30653. if (ret != 0)
  30654. return WC_TEST_RET_ENC_EC(ret);
  30655. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30656. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30657. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  30658. /* make curve25519 keys */
  30659. ret = wc_curve25519_make_key(&rng, 32, &userA);
  30660. if (ret != 0)
  30661. return WC_TEST_RET_ENC_EC(ret);
  30662. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30663. if (ret != 0)
  30664. return WC_TEST_RET_ENC_EC(ret);
  30665. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30666. /* find shared secret key */
  30667. x = sizeof(sharedA);
  30668. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  30669. printf("wc_curve25519_shared_secret 1 failed\n");
  30670. return WC_TEST_RET_ENC_EC(ret);
  30671. }
  30672. y = sizeof(sharedB);
  30673. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  30674. printf("wc_curve25519_shared_secret 2 failed\n");
  30675. return WC_TEST_RET_ENC_EC(ret);
  30676. }
  30677. /* compare shared secret keys to test they are the same */
  30678. if (y != x)
  30679. return WC_TEST_RET_ENC_NC;
  30680. if (XMEMCMP(sharedA, sharedB, x))
  30681. return WC_TEST_RET_ENC_NC;
  30682. #endif
  30683. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30684. /* export a public key and import it for another user */
  30685. x = sizeof(exportBuf);
  30686. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  30687. if (ret != 0)
  30688. return WC_TEST_RET_ENC_EC(ret);
  30689. #ifdef HAVE_CURVE25519_KEY_IMPORT
  30690. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  30691. if (ret != 0)
  30692. return WC_TEST_RET_ENC_EC(ret);
  30693. #endif
  30694. #endif
  30695. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30696. defined(HAVE_CURVE25519_KEY_IMPORT)
  30697. /* test shared key after importing a public key */
  30698. XMEMSET(sharedB, 0, sizeof(sharedB));
  30699. y = sizeof(sharedB);
  30700. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  30701. return WC_TEST_RET_ENC_NC;
  30702. }
  30703. if (XMEMCMP(sharedA, sharedB, y))
  30704. return WC_TEST_RET_ENC_NC;
  30705. /* import RFC test vectors and compare shared key */
  30706. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30707. &userA);
  30708. if (ret != 0)
  30709. return WC_TEST_RET_ENC_EC(ret);
  30710. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  30711. &userB);
  30712. if (ret != 0)
  30713. return WC_TEST_RET_ENC_EC(ret);
  30714. /* test against known test vector */
  30715. XMEMSET(sharedB, 0, sizeof(sharedB));
  30716. y = sizeof(sharedB);
  30717. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  30718. if (ret != 0)
  30719. return WC_TEST_RET_ENC_EC(ret);
  30720. if (XMEMCMP(ss, sharedB, y))
  30721. return WC_TEST_RET_ENC_NC;
  30722. /* test swapping roles of keys and generating same shared key */
  30723. XMEMSET(sharedB, 0, sizeof(sharedB));
  30724. y = sizeof(sharedB);
  30725. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30726. if (ret != 0)
  30727. return WC_TEST_RET_ENC_EC(ret);
  30728. if (XMEMCMP(ss, sharedB, y))
  30729. return WC_TEST_RET_ENC_NC;
  30730. /* test with 1 generated key and 1 from known test vector */
  30731. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30732. &userA);
  30733. if (ret != 0)
  30734. return WC_TEST_RET_ENC_EC(ret);
  30735. wc_curve25519_free(&userB);
  30736. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30737. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30738. if (ret != 0)
  30739. return WC_TEST_RET_ENC_EC(ret);
  30740. x = sizeof(sharedA);
  30741. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  30742. if (ret != 0)
  30743. return WC_TEST_RET_ENC_EC(ret);
  30744. y = sizeof(sharedB);
  30745. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30746. if (ret != 0)
  30747. return WC_TEST_RET_ENC_EC(ret);
  30748. /* compare shared secret keys to test they are the same */
  30749. if (y != x)
  30750. return WC_TEST_RET_ENC_NC;
  30751. if (XMEMCMP(sharedA, sharedB, x))
  30752. return WC_TEST_RET_ENC_NC;
  30753. ret = curve25519_overflow_test();
  30754. if (ret != 0)
  30755. return ret;
  30756. ret = curve25519_check_public_test();
  30757. if (ret != 0)
  30758. return ret;
  30759. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30760. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30761. defined(HAVE_CURVE25519_KEY_IMPORT)
  30762. ret = curve255519_der_test();
  30763. if (ret != 0)
  30764. return ret;
  30765. #endif
  30766. /* clean up keys when done */
  30767. wc_curve25519_free(&pubKey);
  30768. wc_curve25519_free(&userB);
  30769. wc_curve25519_free(&userA);
  30770. wc_FreeRng(&rng);
  30771. return 0;
  30772. }
  30773. #endif /* HAVE_CURVE25519 */
  30774. #ifdef HAVE_ED25519
  30775. #ifdef WOLFSSL_TEST_CERT
  30776. static wc_test_ret_t ed25519_test_cert(void)
  30777. {
  30778. DecodedCert cert[2];
  30779. DecodedCert* serverCert = NULL;
  30780. DecodedCert* caCert = NULL;
  30781. #ifdef HAVE_ED25519_VERIFY
  30782. ed25519_key key;
  30783. ed25519_key* pubKey = NULL;
  30784. int verify;
  30785. #endif /* HAVE_ED25519_VERIFY */
  30786. wc_test_ret_t ret;
  30787. byte* tmp;
  30788. size_t bytes;
  30789. XFILE file;
  30790. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30791. if (tmp == NULL) {
  30792. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30793. }
  30794. #ifdef USE_CERT_BUFFERS_256
  30795. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  30796. bytes = sizeof_ca_ed25519_cert;
  30797. #elif !defined(NO_FILESYSTEM)
  30798. file = XFOPEN(caEd25519Cert, "rb");
  30799. if (file == NULL) {
  30800. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30801. }
  30802. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30803. XFCLOSE(file);
  30804. if (bytes == 0)
  30805. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30806. #else
  30807. /* No certificate to use. */
  30808. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30809. #endif
  30810. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  30811. caCert = &cert[0];
  30812. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  30813. if (ret != 0)
  30814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30815. #ifdef USE_CERT_BUFFERS_256
  30816. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  30817. bytes = sizeof_server_ed25519_cert;
  30818. #elif !defined(NO_FILESYSTEM)
  30819. file = XFOPEN(serverEd25519Cert, "rb");
  30820. if (file == NULL) {
  30821. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30822. }
  30823. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30824. XFCLOSE(file);
  30825. if (bytes == 0)
  30826. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30827. #else
  30828. /* No certificate to use. */
  30829. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30830. #endif
  30831. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  30832. serverCert = &cert[1];
  30833. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  30834. if (ret != 0)
  30835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30836. #ifdef HAVE_ED25519_VERIFY
  30837. ret = wc_ed25519_init(&key);
  30838. if (ret < 0)
  30839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30840. pubKey = &key;
  30841. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  30842. pubKey);
  30843. if (ret < 0)
  30844. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30845. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  30846. serverCert->source + serverCert->certBegin,
  30847. serverCert->sigIndex - serverCert->certBegin,
  30848. &verify, pubKey);
  30849. if (ret < 0 || verify != 1)
  30850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30851. #endif /* HAVE_ED25519_VERIFY */
  30852. done:
  30853. if (tmp != NULL)
  30854. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30855. #ifdef HAVE_ED25519_VERIFY
  30856. wc_ed25519_free(pubKey);
  30857. #endif /* HAVE_ED25519_VERIFY */
  30858. if (caCert != NULL)
  30859. FreeDecodedCert(caCert);
  30860. if (serverCert != NULL)
  30861. FreeDecodedCert(serverCert);
  30862. return ret;
  30863. }
  30864. static wc_test_ret_t ed25519_test_make_cert(void)
  30865. {
  30866. WC_RNG rng;
  30867. Cert cert;
  30868. DecodedCert decode;
  30869. ed25519_key key;
  30870. ed25519_key* privKey = NULL;
  30871. wc_test_ret_t ret = 0;
  30872. byte* tmp = NULL;
  30873. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  30874. #ifndef HAVE_FIPS
  30875. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30876. #else
  30877. ret = wc_InitRng(&rng);
  30878. #endif
  30879. if (ret != 0)
  30880. return WC_TEST_RET_ENC_EC(ret);
  30881. wc_ed25519_init(&key);
  30882. privKey = &key;
  30883. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  30884. cert.daysValid = 365 * 2;
  30885. cert.selfSigned = 1;
  30886. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  30887. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  30888. cert.isCA = 0;
  30889. #ifdef WOLFSSL_CERT_EXT
  30890. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  30891. if (ret < 0)
  30892. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30893. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  30894. if (ret < 0)
  30895. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30896. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  30897. if (ret < 0)
  30898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30899. #endif
  30900. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30901. if (tmp == NULL) {
  30902. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30903. }
  30904. cert.sigType = CTC_ED25519;
  30905. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  30906. if (ret < 0)
  30907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30908. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  30909. ED25519_TYPE, privKey, &rng);
  30910. if (ret < 0)
  30911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30912. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  30913. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  30914. FreeDecodedCert(&decode);
  30915. if (ret != 0)
  30916. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30917. done:
  30918. if (tmp != NULL)
  30919. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30920. wc_ed25519_free(privKey);
  30921. wc_FreeRng(&rng);
  30922. return ret;
  30923. }
  30924. #endif /* WOLFSSL_TEST_CERT */
  30925. #if defined(HAVE_ED25519_KEY_IMPORT)
  30926. static wc_test_ret_t ed25519_test_check_key(void)
  30927. {
  30928. /* Fails to find x-ordinate from this y-ordinate. */
  30929. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  30930. 0x40,
  30931. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30935. };
  30936. /* Y-ordinate value larger than prime. */
  30937. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  30938. 0x40,
  30939. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30940. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30941. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30942. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  30943. };
  30944. /* Y-ordinate value equal to prime. */
  30945. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  30946. 0x40,
  30947. 0xed,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30948. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30949. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30950. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  30951. };
  30952. /* Y-ordinate value equal to prime - 1. */
  30953. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  30954. 0x40,
  30955. 0xec,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30956. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30957. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30958. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  30959. };
  30960. ed25519_key key;
  30961. int ret;
  30962. int res = 0;
  30963. /* Initialize key for use. */
  30964. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  30965. if (ret != 0) {
  30966. return WC_TEST_RET_ENC_NC;
  30967. }
  30968. /* Load bad public key only and perform checks. */
  30969. ret = wc_ed25519_import_public(key_bad_y, ED25519_PUB_KEY_SIZE + 1, &key);
  30970. if (ret != PUBLIC_KEY_E) {
  30971. res = WC_TEST_RET_ENC_NC;
  30972. }
  30973. if (res == 0) {
  30974. /* Load bad public key only and perform checks. */
  30975. ret = wc_ed25519_import_public(key_bad_y_max, ED25519_PUB_KEY_SIZE + 1,
  30976. &key);
  30977. if (ret != PUBLIC_KEY_E) {
  30978. res = WC_TEST_RET_ENC_NC;
  30979. }
  30980. }
  30981. if (res == 0) {
  30982. /* Load bad public key only and perform checks. */
  30983. ret = wc_ed25519_import_public(key_bad_y_is_p, ED25519_PUB_KEY_SIZE + 1,
  30984. &key);
  30985. if (ret != PUBLIC_KEY_E) {
  30986. res = WC_TEST_RET_ENC_NC;
  30987. }
  30988. }
  30989. if (res == 0) {
  30990. /* Load good public key only and perform checks. */
  30991. ret = wc_ed25519_import_public(key_y_is_p_minus_1,
  30992. ED25519_PUB_KEY_SIZE + 1, &key);
  30993. if (ret != 0) {
  30994. res = WC_TEST_RET_ENC_NC;
  30995. }
  30996. }
  30997. /* Dispose of key. */
  30998. wc_ed25519_free(&key);
  30999. return res;
  31000. }
  31001. #endif
  31002. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31003. defined(HAVE_ED25519_KEY_IMPORT)
  31004. static wc_test_ret_t ed25519ctx_test(void)
  31005. {
  31006. wc_test_ret_t ret;
  31007. byte out[ED25519_SIG_SIZE];
  31008. word32 outlen;
  31009. #ifdef HAVE_ED25519_VERIFY
  31010. int verify = 0;
  31011. #endif /* HAVE_ED25519_VERIFY */
  31012. ed25519_key key;
  31013. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  31014. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  31015. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  31016. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  31017. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  31018. };
  31019. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  31020. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  31021. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  31022. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  31023. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  31024. };
  31025. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  31026. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  31027. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  31028. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  31029. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  31030. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  31031. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  31032. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  31033. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  31034. };
  31035. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  31036. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  31037. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  31038. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  31039. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  31040. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  31041. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  31042. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  31043. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  31044. };
  31045. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  31046. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  31047. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  31048. };
  31049. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  31050. 0x66,0x6f,0x6f
  31051. };
  31052. outlen = sizeof(out);
  31053. XMEMSET(out, 0, sizeof(out));
  31054. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31055. if (ret != 0)
  31056. return 10800;
  31057. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  31058. sizeof(pKeyCtx), &key);
  31059. if (ret == 0)
  31060. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31061. contextCtx, sizeof(contextCtx));
  31062. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  31063. ret = WC_TEST_RET_ENC_NC;
  31064. #if defined(HAVE_ED25519_VERIFY)
  31065. /* test verify on good msg */
  31066. if (ret == 0)
  31067. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31068. &verify, &key, contextCtx, sizeof(contextCtx));
  31069. if (ret == 0 && verify != 1)
  31070. ret = WC_TEST_RET_ENC_NC;
  31071. #endif
  31072. if (ret == 0)
  31073. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31074. NULL, 0);
  31075. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  31076. ret = WC_TEST_RET_ENC_NC;
  31077. #if defined(HAVE_ED25519_VERIFY)
  31078. /* test verify on good msg */
  31079. if (ret == 0)
  31080. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31081. &verify, &key, NULL, 0);
  31082. if (ret == 0 && verify != 1)
  31083. ret = WC_TEST_RET_ENC_NC;
  31084. #endif
  31085. wc_ed25519_free(&key);
  31086. return ret;
  31087. }
  31088. static wc_test_ret_t ed25519ph_test(void)
  31089. {
  31090. wc_test_ret_t ret = 0;
  31091. byte out[ED25519_SIG_SIZE];
  31092. word32 outlen;
  31093. #ifdef HAVE_ED25519_VERIFY
  31094. int verify = 0;
  31095. #endif /* HAVE_ED25519_VERIFY */
  31096. ed25519_key key;
  31097. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  31098. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  31099. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  31100. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  31101. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  31102. };
  31103. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  31104. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  31105. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  31106. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  31107. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  31108. };
  31109. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  31110. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  31111. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  31112. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  31113. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  31114. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  31115. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  31116. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  31117. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  31118. };
  31119. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  31120. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  31121. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  31122. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  31123. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  31124. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  31125. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  31126. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  31127. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  31128. };
  31129. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  31130. 0x61,0x62,0x63
  31131. };
  31132. /* SHA-512 hash of msgPh */
  31133. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  31134. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  31135. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  31136. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  31137. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  31138. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  31139. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  31140. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  31141. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  31142. };
  31143. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  31144. 0x66,0x6f,0x6f
  31145. };
  31146. outlen = sizeof(out);
  31147. XMEMSET(out, 0, sizeof(out));
  31148. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31149. if (ret != 0)
  31150. return WC_TEST_RET_ENC_EC(ret);
  31151. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  31152. sizeof(pKeyPh), &key);
  31153. if (ret == 0)
  31154. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31155. NULL, 0);
  31156. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31157. ret = WC_TEST_RET_ENC_NC;
  31158. #if defined(HAVE_ED25519_VERIFY)
  31159. /* test verify on good msg */
  31160. if (ret == 0)
  31161. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  31162. &verify, &key, NULL, 0);
  31163. if (ret == 0 && verify != 1)
  31164. ret = WC_TEST_RET_ENC_NC;
  31165. #endif
  31166. if (ret == 0)
  31167. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31168. contextPh2, sizeof(contextPh2));
  31169. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31170. ret = WC_TEST_RET_ENC_NC;
  31171. #if defined(HAVE_ED25519_VERIFY)
  31172. /* test verify on good msg */
  31173. if (ret == 0)
  31174. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  31175. &key, contextPh2, sizeof(contextPh2));
  31176. if (ret == 0 && verify != 1)
  31177. ret = WC_TEST_RET_ENC_NC;
  31178. #endif
  31179. if (ret == 0)
  31180. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31181. NULL, 0);
  31182. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31183. ret = WC_TEST_RET_ENC_NC;
  31184. #if defined(HAVE_ED25519_VERIFY)
  31185. if (ret == 0)
  31186. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  31187. &verify, &key, NULL, 0);
  31188. if (ret == 0 && verify != 1)
  31189. ret = WC_TEST_RET_ENC_NC;
  31190. #endif
  31191. if (ret == 0)
  31192. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31193. contextPh2, sizeof(contextPh2));
  31194. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31195. ret = WC_TEST_RET_ENC_NC;
  31196. #if defined(HAVE_ED25519_VERIFY)
  31197. if (ret == 0)
  31198. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  31199. &key, contextPh2, sizeof(contextPh2));
  31200. if (ret == 0 && verify != 1)
  31201. ret = WC_TEST_RET_ENC_NC;
  31202. #endif
  31203. wc_ed25519_free(&key);
  31204. return ret;
  31205. }
  31206. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31207. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  31208. {
  31209. wc_test_ret_t ret;
  31210. WC_RNG rng;
  31211. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31212. defined(HAVE_ED25519_KEY_IMPORT)
  31213. byte out[ED25519_SIG_SIZE];
  31214. byte exportPKey[ED25519_KEY_SIZE];
  31215. byte exportSKey[ED25519_KEY_SIZE];
  31216. word32 exportPSz;
  31217. word32 exportSSz;
  31218. int i;
  31219. word32 outlen;
  31220. #ifdef HAVE_ED25519_VERIFY
  31221. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31222. int j;
  31223. #endif
  31224. int verify;
  31225. #endif /* HAVE_ED25519_VERIFY */
  31226. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31227. word32 keySz, sigSz;
  31228. ed25519_key key;
  31229. ed25519_key key2;
  31230. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31231. defined(HAVE_ED25519_KEY_IMPORT)
  31232. /* test vectors from
  31233. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  31234. */
  31235. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  31236. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31237. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31238. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31239. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31240. };
  31241. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  31242. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  31243. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  31244. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  31245. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  31246. };
  31247. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  31248. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  31249. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  31250. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  31251. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  31252. };
  31253. /* uncompressed test */
  31254. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  31255. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31256. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31257. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31258. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31259. };
  31260. /* compressed prefix test */
  31261. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  31262. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31263. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31264. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31265. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31266. };
  31267. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  31268. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  31269. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  31270. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  31271. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  31272. };
  31273. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  31274. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  31275. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31276. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31277. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31278. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31279. };
  31280. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  31281. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  31282. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  31283. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  31284. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  31285. };
  31286. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  31287. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  31288. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  31289. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  31290. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  31291. };
  31292. /* uncompressed test */
  31293. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  31294. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  31295. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  31296. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  31297. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  31298. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  31299. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  31300. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  31301. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  31302. 0xd7
  31303. };
  31304. /* compressed prefix */
  31305. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  31306. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31307. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31308. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31309. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31310. };
  31311. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  31312. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  31313. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  31314. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  31315. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  31316. };
  31317. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  31318. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  31319. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  31320. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  31321. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31322. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31323. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31324. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31325. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31326. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31327. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31328. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31329. };
  31330. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  31331. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  31332. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  31333. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  31334. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  31335. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  31336. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  31337. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  31338. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  31339. };
  31340. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  31341. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  31342. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  31343. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  31344. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  31345. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  31346. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  31347. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  31348. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  31349. };
  31350. /* uncompressed test */
  31351. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  31352. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31353. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31354. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31355. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31356. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31357. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31358. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31359. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31360. };
  31361. /* compressed prefix */
  31362. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  31363. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31364. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31365. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31366. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31367. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31368. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31369. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31370. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31371. };
  31372. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  31373. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  31374. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  31375. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  31376. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  31377. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  31378. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  31379. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  31380. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  31381. };
  31382. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  31383. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  31384. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  31385. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  31386. /* test of a 1024 byte long message */
  31387. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  31388. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  31389. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  31390. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  31391. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  31392. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  31393. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  31394. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  31395. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  31396. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  31397. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  31398. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  31399. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  31400. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  31401. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  31402. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  31403. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  31404. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  31405. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  31406. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  31407. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  31408. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  31409. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  31410. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  31411. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  31412. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  31413. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  31414. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  31415. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  31416. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  31417. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  31418. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  31419. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  31420. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  31421. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  31422. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  31423. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  31424. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  31425. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  31426. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  31427. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  31428. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  31429. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  31430. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  31431. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  31432. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  31433. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  31434. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  31435. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  31436. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  31437. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  31438. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  31439. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  31440. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  31441. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  31442. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  31443. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  31444. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  31445. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  31446. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  31447. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  31448. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  31449. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  31450. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  31451. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  31452. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  31453. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  31454. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  31455. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  31456. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  31457. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  31458. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  31459. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  31460. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  31461. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  31462. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  31463. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  31464. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  31465. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  31466. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  31467. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  31468. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  31469. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  31470. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  31471. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  31472. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  31473. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  31474. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  31475. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  31476. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  31477. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  31478. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  31479. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  31480. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  31481. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  31482. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  31483. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  31484. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  31485. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  31486. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  31487. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  31488. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  31489. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  31490. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  31491. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  31492. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  31493. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  31494. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  31495. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  31496. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  31497. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  31498. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  31499. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  31500. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  31501. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  31502. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  31503. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  31504. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  31505. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  31506. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  31507. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  31508. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  31509. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  31510. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  31511. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  31512. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  31513. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  31514. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  31515. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  31516. };
  31517. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  31518. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  31519. sizeof(msg2),
  31520. sizeof(msg3),
  31521. 0 /*sizeof(msg1)*/,
  31522. 0 /*sizeof(msg1)*/,
  31523. sizeof(msg4)
  31524. };
  31525. #ifndef NO_ASN
  31526. static const byte privateEd25519[] = {
  31527. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  31528. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31529. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31530. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31531. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31532. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31533. };
  31534. static const byte badPrivateEd25519[] = {
  31535. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  31536. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31537. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31538. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31539. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31540. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31541. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  31542. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31543. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31544. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31545. 0xf7,0x07,0x51,0x1a,
  31546. 0x00 /* add additional bytes to make the pubkey bigger */
  31547. };
  31548. static const byte publicEd25519[] = {
  31549. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31550. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31551. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31552. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31553. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31554. 0xf7,0x07,0x51,0x1a
  31555. };
  31556. /* size has been altered to catch if sanity check is done */
  31557. static const byte badPublicEd25519[] = {
  31558. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31559. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31560. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31561. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31562. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31563. 0xf7,0x07,0x51,0x1a,
  31564. 0x00 /* add an additional byte to make the pubkey appear bigger */
  31565. };
  31566. static const byte privPubEd25519[] = {
  31567. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  31568. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31569. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31570. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31571. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31572. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31573. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  31574. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  31575. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  31576. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  31577. 0x51,0x1a
  31578. };
  31579. word32 idx;
  31580. #endif /* NO_ASN */
  31581. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31582. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31583. ed25519_key key3;
  31584. #endif
  31585. WOLFSSL_ENTER("ed25519_test");
  31586. /* create ed25519 keys */
  31587. #ifndef HAVE_FIPS
  31588. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31589. #else
  31590. ret = wc_InitRng(&rng);
  31591. #endif
  31592. if (ret != 0)
  31593. return WC_TEST_RET_ENC_EC(ret);
  31594. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31595. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  31596. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31597. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  31598. #endif
  31599. #ifdef HAVE_ED25519_MAKE_KEY
  31600. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  31601. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  31602. #endif
  31603. /* helper functions for signature and key size */
  31604. keySz = (word32)wc_ed25519_size(&key);
  31605. sigSz = (word32)wc_ed25519_sig_size(&key);
  31606. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31607. defined(HAVE_ED25519_KEY_IMPORT)
  31608. for (i = 0; i < 6; i++) {
  31609. outlen = sizeof(out);
  31610. XMEMSET(out, 0, sizeof(out));
  31611. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  31612. pKeySz[i], &key) != 0)
  31613. return WC_TEST_RET_ENC_I(i);
  31614. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  31615. return WC_TEST_RET_ENC_I(i);
  31616. if (XMEMCMP(out, sigs[i], 64))
  31617. return WC_TEST_RET_ENC_I(i);
  31618. #if defined(HAVE_ED25519_VERIFY)
  31619. /* test verify on good msg */
  31620. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31621. &key) != 0 || verify != 1)
  31622. return WC_TEST_RET_ENC_I(i);
  31623. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31624. /* test verify on good msg using streaming interface directly */
  31625. if (wc_ed25519_verify_msg_init(out, outlen,
  31626. &key, (byte)Ed25519, NULL, 0) != 0)
  31627. return WC_TEST_RET_ENC_I(i);
  31628. for (j = 0; j < msgSz[i]; j += i) {
  31629. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  31630. return WC_TEST_RET_ENC_I(i);
  31631. }
  31632. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  31633. &key) != 0 || verify != 1)
  31634. return WC_TEST_RET_ENC_I(i);
  31635. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  31636. /* test verify on bad msg */
  31637. out[outlen-1] = out[outlen-1] + 1;
  31638. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31639. &key) == 0 || verify == 1)
  31640. return WC_TEST_RET_ENC_I(i);
  31641. #endif /* HAVE_ED25519_VERIFY */
  31642. /* test api for import/exporting keys */
  31643. exportPSz = sizeof(exportPKey);
  31644. exportSSz = sizeof(exportSKey);
  31645. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  31646. return WC_TEST_RET_ENC_I(i);
  31647. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  31648. return WC_TEST_RET_ENC_I(i);
  31649. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  31650. return WC_TEST_RET_ENC_I(i);
  31651. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  31652. exportPKey, exportPSz, &key2) != 0)
  31653. return WC_TEST_RET_ENC_I(i);
  31654. /* clear "out" buffer and test sign with imported keys */
  31655. outlen = sizeof(out);
  31656. XMEMSET(out, 0, sizeof(out));
  31657. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  31658. return WC_TEST_RET_ENC_I(i);
  31659. #if defined(HAVE_ED25519_VERIFY)
  31660. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31661. &key2) != 0 || verify != 1)
  31662. return WC_TEST_RET_ENC_I(i);
  31663. if (XMEMCMP(out, sigs[i], 64))
  31664. return WC_TEST_RET_ENC_I(i);
  31665. #endif /* HAVE_ED25519_VERIFY */
  31666. }
  31667. {
  31668. /* Run tests for some rare code paths */
  31669. /* sig is exactly equal to the order */
  31670. static const byte rareEd1[] = {
  31671. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31672. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31673. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31674. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31675. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31676. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31677. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31678. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31679. };
  31680. /* sig is larger than the order before we get to the low part */
  31681. static const byte rareEd2[] = {
  31682. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31683. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31684. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31685. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31686. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31687. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31688. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31689. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10
  31690. };
  31691. /* sig is larger than the order in the low part */
  31692. static const byte rareEd3[] = {
  31693. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31695. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31697. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31698. 0xd6, 0x9c, 0xf9, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31699. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31700. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31701. };
  31702. /* sig is smaller than the order */
  31703. static const byte rareEd4[] = {
  31704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31708. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31709. 0xd6, 0x9c, 0xf1, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31712. };
  31713. ret = wc_ed25519_import_private_key(sKeys[0], ED25519_KEY_SIZE,
  31714. pKeys[0], pKeySz[0], &key);
  31715. if (ret != 0)
  31716. return ret;
  31717. ret = wc_ed25519_verify_msg(rareEd1, sizeof(rareEd1), msgs[0], msgSz[0],
  31718. &verify, &key);
  31719. if (ret != BAD_FUNC_ARG)
  31720. return ret;
  31721. ret = wc_ed25519_verify_msg(rareEd2, sizeof(rareEd2), msgs[0], msgSz[0],
  31722. &verify, &key);
  31723. if (ret != BAD_FUNC_ARG)
  31724. return ret;
  31725. ret = wc_ed25519_verify_msg(rareEd3, sizeof(rareEd3), msgs[0], msgSz[0],
  31726. &verify, &key);
  31727. if (ret != BAD_FUNC_ARG)
  31728. return ret;
  31729. ret = wc_ed25519_verify_msg(rareEd4, sizeof(rareEd4), msgs[0], msgSz[0],
  31730. &verify, &key);
  31731. if (ret != SIG_VERIFY_E)
  31732. return ret;
  31733. }
  31734. ret = ed25519ctx_test();
  31735. if (ret != 0)
  31736. return ret;
  31737. ret = ed25519ph_test();
  31738. if (ret != 0)
  31739. return ret;
  31740. #ifndef NO_ASN
  31741. /* Try ASN.1 encoded private-only key and public key. */
  31742. idx = 0;
  31743. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  31744. sizeof(privateEd25519));
  31745. if (ret != 0)
  31746. return WC_TEST_RET_ENC_EC(ret);
  31747. idx = 0;
  31748. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  31749. sizeof(badPrivateEd25519)) == 0)
  31750. return WC_TEST_RET_ENC_NC;
  31751. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31752. if (ret != BAD_FUNC_ARG)
  31753. return WC_TEST_RET_ENC_EC(ret);
  31754. /* try with a buffer size that is too large */
  31755. idx = 0;
  31756. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  31757. sizeof(badPublicEd25519)) == 0)
  31758. return WC_TEST_RET_ENC_NC;
  31759. idx = 0;
  31760. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  31761. sizeof(publicEd25519));
  31762. if (ret != 0)
  31763. return WC_TEST_RET_ENC_EC(ret);
  31764. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31765. if (ret != 0)
  31766. return WC_TEST_RET_ENC_EC(ret);
  31767. if (XMEMCMP(out, sigs[0], 64))
  31768. return WC_TEST_RET_ENC_NC;
  31769. #if defined(HAVE_ED25519_VERIFY)
  31770. /* test verify on good msg */
  31771. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  31772. if (ret != 0 || verify != 1)
  31773. return WC_TEST_RET_ENC_EC(ret);
  31774. #endif /* HAVE_ED25519_VERIFY */
  31775. wc_ed25519_free(&key3);
  31776. wc_ed25519_init(&key3);
  31777. idx = 0;
  31778. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  31779. sizeof(privPubEd25519));
  31780. if (ret != 0)
  31781. return WC_TEST_RET_ENC_EC(ret);
  31782. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31783. if (ret != 0)
  31784. return WC_TEST_RET_ENC_EC(ret);
  31785. if (XMEMCMP(out, sigs[0], 64))
  31786. return WC_TEST_RET_ENC_NC;
  31787. wc_ed25519_free(&key3);
  31788. #endif /* NO_ASN */
  31789. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31790. /* clean up keys when done */
  31791. wc_ed25519_free(&key);
  31792. wc_ed25519_free(&key2);
  31793. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  31794. wc_FreeRng(&rng);
  31795. #endif
  31796. /* hush warnings of unused keySz and sigSz */
  31797. (void)keySz;
  31798. (void)sigSz;
  31799. ret = ed25519_test_check_key();
  31800. if (ret < 0)
  31801. return ret;
  31802. #ifdef WOLFSSL_TEST_CERT
  31803. ret = ed25519_test_cert();
  31804. if (ret < 0)
  31805. return ret;
  31806. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  31807. ret = ed25519_test_make_cert();
  31808. if (ret < 0)
  31809. return ret;
  31810. #endif /* WOLFSSL_CERT_GEN */
  31811. #endif /* WOLFSSL_TEST_CERT */
  31812. return 0;
  31813. }
  31814. #endif /* HAVE_ED25519 */
  31815. #ifdef HAVE_CURVE448
  31816. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  31817. defined(HAVE_CURVE448_KEY_IMPORT)
  31818. /* Test the wc_curve448_check_public API.
  31819. *
  31820. * returns 0 on success and -ve on failure.
  31821. */
  31822. static wc_test_ret_t curve448_check_public_test(void)
  31823. {
  31824. /* Little-endian values that will fail */
  31825. byte fail_le[][CURVE448_KEY_SIZE] = {
  31826. {
  31827. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31828. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31829. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31830. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31831. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31833. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31834. },
  31835. {
  31836. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31840. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31841. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31842. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31843. },
  31844. };
  31845. /* Big-endian values that will fail */
  31846. byte fail_be[][CURVE448_KEY_SIZE] = {
  31847. {
  31848. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31849. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31850. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31851. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31852. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31853. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31854. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31855. },
  31856. {
  31857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31860. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31861. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31862. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31863. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31864. },
  31865. };
  31866. /* Good or valid public value */
  31867. byte good[CURVE448_KEY_SIZE] = {
  31868. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31869. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31870. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31871. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31872. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31873. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31874. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31875. };
  31876. int i;
  31877. wc_test_ret_t ret;
  31878. /* Parameter checks */
  31879. /* NULL pointer */
  31880. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  31881. if (ret != BAD_FUNC_ARG)
  31882. return WC_TEST_RET_ENC_EC(ret);
  31883. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  31884. if (ret != BAD_FUNC_ARG)
  31885. return WC_TEST_RET_ENC_EC(ret);
  31886. /* Length of 0 treated differently to other invalid lengths for TLS */
  31887. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  31888. if (ret != BUFFER_E)
  31889. return WC_TEST_RET_ENC_EC(ret);
  31890. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  31891. if (ret != BUFFER_E)
  31892. return WC_TEST_RET_ENC_EC(ret);
  31893. /* Length not CURVE448_KEY_SIZE */
  31894. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  31895. if (i == CURVE448_KEY_SIZE)
  31896. continue;
  31897. if (wc_curve448_check_public(good, (word32)i, EC448_LITTLE_ENDIAN) !=
  31898. ECC_BAD_ARG_E) {
  31899. return WC_TEST_RET_ENC_I(i);
  31900. }
  31901. if (wc_curve448_check_public(good, (word32)i, EC448_BIG_ENDIAN) !=
  31902. ECC_BAD_ARG_E) {
  31903. return WC_TEST_RET_ENC_I(i);
  31904. }
  31905. }
  31906. /* Little-endian fail cases */
  31907. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  31908. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  31909. EC448_LITTLE_ENDIAN) == 0) {
  31910. return WC_TEST_RET_ENC_I(i);
  31911. }
  31912. }
  31913. /* Big-endian fail cases */
  31914. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  31915. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  31916. EC448_BIG_ENDIAN) == 0) {
  31917. return WC_TEST_RET_ENC_I(i);
  31918. }
  31919. }
  31920. /* Check a valid public value works! */
  31921. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  31922. EC448_LITTLE_ENDIAN);
  31923. if (ret != 0)
  31924. return WC_TEST_RET_ENC_EC(ret);
  31925. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  31926. if (ret != 0)
  31927. return WC_TEST_RET_ENC_EC(ret);
  31928. return 0;
  31929. }
  31930. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  31931. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  31932. {
  31933. WC_RNG rng;
  31934. wc_test_ret_t ret;
  31935. #ifdef HAVE_CURVE448_SHARED_SECRET
  31936. byte sharedA[CURVE448_KEY_SIZE];
  31937. byte sharedB[CURVE448_KEY_SIZE];
  31938. word32 y;
  31939. #endif
  31940. #ifdef HAVE_CURVE448_KEY_EXPORT
  31941. byte exportBuf[CURVE448_KEY_SIZE];
  31942. #endif
  31943. word32 x = 0;
  31944. curve448_key userA, userB, pubKey;
  31945. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  31946. defined(HAVE_CURVE448_KEY_IMPORT)
  31947. /* test vectors from
  31948. https://www.rfc-editor.org/rfc/rfc7748.html
  31949. */
  31950. /* secret key for party a */
  31951. byte sa[] = {
  31952. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  31953. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  31954. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  31955. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  31956. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  31957. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  31958. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  31959. };
  31960. /* public key for party a */
  31961. byte pa[] = {
  31962. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  31963. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  31964. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  31965. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  31966. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  31967. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  31968. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  31969. };
  31970. /* secret key for party b */
  31971. byte sb[] = {
  31972. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  31973. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  31974. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  31975. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  31976. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  31977. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  31978. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  31979. };
  31980. /* public key for party b */
  31981. byte pb[] = {
  31982. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  31983. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  31984. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  31985. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  31986. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  31987. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  31988. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  31989. };
  31990. /* expected shared key */
  31991. byte ss[] = {
  31992. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  31993. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  31994. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  31995. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  31996. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  31997. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  31998. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  31999. };
  32000. #endif /* HAVE_CURVE448_SHARED_SECRET */
  32001. (void)x;
  32002. WOLFSSL_ENTER("curve448_test");
  32003. #ifndef HAVE_FIPS
  32004. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32005. #else
  32006. ret = wc_InitRng(&rng);
  32007. #endif
  32008. if (ret != 0)
  32009. return WC_TEST_RET_ENC_EC(ret);
  32010. wc_curve448_init(&userA);
  32011. wc_curve448_init(&userB);
  32012. wc_curve448_init(&pubKey);
  32013. /* make curve448 keys */
  32014. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  32015. if (ret != 0)
  32016. return WC_TEST_RET_ENC_EC(ret);
  32017. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  32018. if (ret != 0)
  32019. return WC_TEST_RET_ENC_EC(ret);
  32020. #ifdef HAVE_CURVE448_SHARED_SECRET
  32021. /* find shared secret key */
  32022. x = sizeof(sharedA);
  32023. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32024. if (ret != 0)
  32025. return WC_TEST_RET_ENC_EC(ret);
  32026. y = sizeof(sharedB);
  32027. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32028. if (ret != 0)
  32029. return WC_TEST_RET_ENC_EC(ret);
  32030. /* compare shared secret keys to test they are the same */
  32031. if (y != x)
  32032. return WC_TEST_RET_ENC_NC;
  32033. if (XMEMCMP(sharedA, sharedB, x))
  32034. return WC_TEST_RET_ENC_NC;
  32035. #endif
  32036. #ifdef HAVE_CURVE448_KEY_EXPORT
  32037. /* export a public key and import it for another user */
  32038. x = sizeof(exportBuf);
  32039. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  32040. if (ret != 0)
  32041. return WC_TEST_RET_ENC_EC(ret);
  32042. #ifdef HAVE_CURVE448_KEY_IMPORT
  32043. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  32044. if (ret != 0)
  32045. return WC_TEST_RET_ENC_EC(ret);
  32046. #endif
  32047. #endif
  32048. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32049. defined(HAVE_CURVE448_KEY_IMPORT)
  32050. /* test shared key after importing a public key */
  32051. XMEMSET(sharedB, 0, sizeof(sharedB));
  32052. y = sizeof(sharedB);
  32053. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  32054. if (ret != 0)
  32055. return WC_TEST_RET_ENC_EC(ret);
  32056. if (XMEMCMP(sharedA, sharedB, y))
  32057. return WC_TEST_RET_ENC_NC;
  32058. /* import RFC test vectors and compare shared key */
  32059. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32060. if (ret != 0)
  32061. return WC_TEST_RET_ENC_EC(ret);
  32062. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  32063. if (ret != 0)
  32064. return WC_TEST_RET_ENC_EC(ret);
  32065. /* test against known test vector */
  32066. XMEMSET(sharedB, 0, sizeof(sharedB));
  32067. y = sizeof(sharedB);
  32068. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  32069. if (ret != 0)
  32070. return WC_TEST_RET_ENC_EC(ret);
  32071. if (XMEMCMP(ss, sharedB, y))
  32072. return WC_TEST_RET_ENC_NC;
  32073. /* test swapping roles of keys and generating same shared key */
  32074. XMEMSET(sharedB, 0, sizeof(sharedB));
  32075. y = sizeof(sharedB);
  32076. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32077. if (ret != 0)
  32078. return WC_TEST_RET_ENC_EC(ret);
  32079. if (XMEMCMP(ss, sharedB, y))
  32080. return WC_TEST_RET_ENC_NC;
  32081. /* test with 1 generated key and 1 from known test vector */
  32082. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32083. if (ret != 0)
  32084. return WC_TEST_RET_ENC_EC(ret);
  32085. ret = wc_curve448_make_key(&rng, 56, &userB);
  32086. if (ret != 0)
  32087. return WC_TEST_RET_ENC_EC(ret);
  32088. x = sizeof(sharedA);
  32089. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32090. if (ret != 0)
  32091. return WC_TEST_RET_ENC_EC(ret);
  32092. y = sizeof(sharedB);
  32093. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32094. if (ret != 0)
  32095. return WC_TEST_RET_ENC_EC(ret);
  32096. /* compare shared secret keys to test they are the same */
  32097. if (y != x)
  32098. return WC_TEST_RET_ENC_NC;
  32099. if (XMEMCMP(sharedA, sharedB, x))
  32100. return WC_TEST_RET_ENC_NC;
  32101. ret = curve448_check_public_test();
  32102. if (ret != 0)
  32103. return ret;
  32104. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32105. /* clean up keys when done */
  32106. wc_curve448_free(&pubKey);
  32107. wc_curve448_free(&userB);
  32108. wc_curve448_free(&userA);
  32109. wc_FreeRng(&rng);
  32110. return 0;
  32111. }
  32112. #endif /* HAVE_CURVE448 */
  32113. #ifdef HAVE_ED448
  32114. #ifdef WOLFSSL_TEST_CERT
  32115. static wc_test_ret_t ed448_test_cert(void)
  32116. {
  32117. DecodedCert cert[2];
  32118. DecodedCert* serverCert = NULL;
  32119. DecodedCert* caCert = NULL;
  32120. #ifdef HAVE_ED448_VERIFY
  32121. ed448_key key;
  32122. ed448_key* pubKey = NULL;
  32123. int verify;
  32124. #endif /* HAVE_ED448_VERIFY */
  32125. wc_test_ret_t ret;
  32126. byte* tmp;
  32127. size_t bytes;
  32128. XFILE file;
  32129. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32130. if (tmp == NULL) {
  32131. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32132. }
  32133. #ifdef USE_CERT_BUFFERS_256
  32134. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  32135. bytes = sizeof_ca_ed448_cert;
  32136. #elif !defined(NO_FILESYSTEM)
  32137. file = XFOPEN(caEd448Cert, "rb");
  32138. if (file == NULL) {
  32139. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32140. }
  32141. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32142. XFCLOSE(file);
  32143. if (bytes == 0)
  32144. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32145. #else
  32146. /* No certificate to use. */
  32147. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32148. #endif
  32149. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  32150. caCert = &cert[0];
  32151. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  32152. if (ret != 0)
  32153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32154. #ifdef USE_CERT_BUFFERS_256
  32155. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  32156. bytes = sizeof_server_ed448_cert;
  32157. #elif !defined(NO_FILESYSTEM)
  32158. file = XFOPEN(serverEd448Cert, "rb");
  32159. if (file == NULL) {
  32160. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32161. }
  32162. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32163. XFCLOSE(file);
  32164. if (bytes == 0)
  32165. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32166. #else
  32167. /* No certificate to use. */
  32168. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32169. #endif
  32170. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  32171. serverCert = &cert[1];
  32172. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  32173. if (ret != 0)
  32174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32175. #ifdef HAVE_ED448_VERIFY
  32176. ret = wc_ed448_init(&key);
  32177. if (ret < 0)
  32178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32179. pubKey = &key;
  32180. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  32181. if (ret < 0)
  32182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32183. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  32184. serverCert->source + serverCert->certBegin,
  32185. serverCert->sigIndex - serverCert->certBegin,
  32186. &verify, pubKey, NULL, 0);
  32187. if (ret < 0 || verify != 1) {
  32188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32189. }
  32190. #endif /* HAVE_ED448_VERIFY */
  32191. done:
  32192. if (tmp != NULL)
  32193. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32194. #ifdef HAVE_ED448_VERIFY
  32195. wc_ed448_free(pubKey);
  32196. #endif /* HAVE_ED448_VERIFY */
  32197. if (caCert != NULL)
  32198. FreeDecodedCert(caCert);
  32199. if (serverCert != NULL)
  32200. FreeDecodedCert(serverCert);
  32201. return ret;
  32202. }
  32203. static wc_test_ret_t ed448_test_make_cert(void)
  32204. {
  32205. WC_RNG rng;
  32206. Cert cert;
  32207. DecodedCert decode;
  32208. ed448_key key;
  32209. ed448_key* privKey = NULL;
  32210. wc_test_ret_t ret = 0;
  32211. byte* tmp = NULL;
  32212. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  32213. #ifndef HAVE_FIPS
  32214. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32215. #else
  32216. ret = wc_InitRng(&rng);
  32217. #endif
  32218. if (ret != 0)
  32219. return WC_TEST_RET_ENC_EC(ret);
  32220. wc_ed448_init(&key);
  32221. privKey = &key;
  32222. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  32223. cert.daysValid = 365 * 2;
  32224. cert.selfSigned = 1;
  32225. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  32226. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  32227. cert.isCA = 0;
  32228. #ifdef WOLFSSL_CERT_EXT
  32229. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  32230. if (ret < 0)
  32231. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32232. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32233. if (ret < 0)
  32234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32235. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32236. if (ret < 0)
  32237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32238. #endif
  32239. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32240. if (tmp == NULL) {
  32241. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32242. }
  32243. cert.sigType = CTC_ED448;
  32244. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  32245. if (ret < 0)
  32246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32247. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  32248. privKey, &rng);
  32249. if (ret < 0)
  32250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32251. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  32252. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  32253. FreeDecodedCert(&decode);
  32254. if (ret != 0)
  32255. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32256. done:
  32257. if (tmp != NULL)
  32258. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32259. wc_ed448_free(privKey);
  32260. wc_FreeRng(&rng);
  32261. return ret;
  32262. }
  32263. #endif /* WOLFSSL_TEST_CERT */
  32264. #if defined(HAVE_ED448_KEY_IMPORT)
  32265. static wc_test_ret_t ed448_test_check_key(void)
  32266. {
  32267. /* Fails to find x-ordinate from this y-ordinate. */
  32268. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  32269. 0x40,
  32270. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32271. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32272. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32273. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32274. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32275. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32276. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32277. 0x00
  32278. };
  32279. /* Y-ordinate value larger than prime. */
  32280. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  32281. 0x40,
  32282. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32283. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32284. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32285. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32286. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32287. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32288. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32289. 0xff
  32290. };
  32291. /* Y-ordinate value equal to prime. */
  32292. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  32293. 0x40,
  32294. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32295. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32296. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32297. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32298. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32299. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32300. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32301. 0xff
  32302. };
  32303. /* Y-ordinate value equal to prime - 1. */
  32304. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  32305. 0x40,
  32306. 0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32307. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32308. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32309. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32310. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32311. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32312. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32313. 0xff
  32314. };
  32315. ed448_key key;
  32316. int ret;
  32317. int res = 0;
  32318. /* Initialize key for use. */
  32319. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32320. if (ret != 0) {
  32321. return WC_TEST_RET_ENC_NC;
  32322. }
  32323. /* Load bad public key only and perform checks. */
  32324. ret = wc_ed448_import_public(key_bad_y, ED448_PUB_KEY_SIZE + 1, &key);
  32325. if (ret != PUBLIC_KEY_E) {
  32326. res = WC_TEST_RET_ENC_NC;
  32327. }
  32328. if (ret == 0) {
  32329. /* Load bad public key only and perform checks. */
  32330. ret = wc_ed448_import_public(key_bad_y_max, ED448_PUB_KEY_SIZE + 1,
  32331. &key);
  32332. if (ret != PUBLIC_KEY_E) {
  32333. res = WC_TEST_RET_ENC_NC;
  32334. }
  32335. }
  32336. if (res == 0) {
  32337. /* Load bad public key only and perform checks. */
  32338. ret = wc_ed448_import_public(key_bad_y_is_p, ED448_PUB_KEY_SIZE + 1,
  32339. &key);
  32340. if (ret != PUBLIC_KEY_E) {
  32341. res = WC_TEST_RET_ENC_NC;
  32342. }
  32343. }
  32344. if (res == 0) {
  32345. /* Load good public key only and perform checks. */
  32346. ret = wc_ed448_import_public(key_y_is_p_minus_1, ED448_PUB_KEY_SIZE + 1,
  32347. &key);
  32348. if (ret != 0) {
  32349. res = WC_TEST_RET_ENC_NC;
  32350. }
  32351. }
  32352. /* Dispose of key. */
  32353. wc_ed448_free(&key);
  32354. return res;
  32355. }
  32356. #endif
  32357. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32358. defined(HAVE_ED448_KEY_IMPORT)
  32359. static wc_test_ret_t ed448_ctx_test(void)
  32360. {
  32361. wc_test_ret_t ret;
  32362. byte out[ED448_SIG_SIZE];
  32363. word32 outlen;
  32364. #ifdef HAVE_ED448_VERIFY
  32365. int verify;
  32366. #endif /* HAVE_ED448_VERIFY */
  32367. ed448_key key;
  32368. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  32369. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32370. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32371. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32372. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32373. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32374. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32375. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32376. 0x4e
  32377. };
  32378. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  32379. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32380. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32381. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32382. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32383. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32384. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32385. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32386. 0x80
  32387. };
  32388. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  32389. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  32390. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  32391. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  32392. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  32393. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  32394. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  32395. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  32396. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  32397. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  32398. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  32399. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  32400. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  32401. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  32402. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  32403. 0x3c, 0x00
  32404. };
  32405. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  32406. 0x03
  32407. };
  32408. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  32409. 0x66,0x6f,0x6f
  32410. };
  32411. outlen = sizeof(out);
  32412. XMEMSET(out, 0, sizeof(out));
  32413. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32414. if (ret != 0)
  32415. return WC_TEST_RET_ENC_EC(ret);
  32416. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  32417. sizeof(pKeyCtx), &key);
  32418. if (ret != 0)
  32419. return WC_TEST_RET_ENC_EC(ret);
  32420. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  32421. contextCtx, sizeof(contextCtx));
  32422. if (ret != 0)
  32423. return WC_TEST_RET_ENC_EC(ret);
  32424. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  32425. return WC_TEST_RET_ENC_NC;
  32426. #if defined(HAVE_ED448_VERIFY)
  32427. /* test verify on good msg */
  32428. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  32429. contextCtx, sizeof(contextCtx));
  32430. if (ret != 0 || verify != 1)
  32431. return WC_TEST_RET_ENC_EC(ret);
  32432. #endif
  32433. wc_ed448_free(&key);
  32434. return 0;
  32435. }
  32436. static wc_test_ret_t ed448ph_test(void)
  32437. {
  32438. wc_test_ret_t ret;
  32439. byte out[ED448_SIG_SIZE];
  32440. word32 outlen;
  32441. #ifdef HAVE_ED448_VERIFY
  32442. int verify;
  32443. #endif /* HAVE_ED448_VERIFY */
  32444. ed448_key key;
  32445. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  32446. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  32447. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  32448. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  32449. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  32450. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  32451. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  32452. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  32453. 0x49
  32454. };
  32455. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  32456. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  32457. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  32458. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  32459. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  32460. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  32461. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  32462. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  32463. 0x80
  32464. };
  32465. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  32466. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  32467. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  32468. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  32469. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  32470. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  32471. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  32472. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  32473. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  32474. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  32475. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  32476. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  32477. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  32478. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  32479. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  32480. 0x0f, 0x00
  32481. };
  32482. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  32483. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  32484. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  32485. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  32486. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  32487. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  32488. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  32489. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  32490. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  32491. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  32492. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  32493. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  32494. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  32495. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  32496. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  32497. 0x21, 0x00
  32498. };
  32499. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  32500. 0x61,0x62,0x63
  32501. };
  32502. /* SHA-512 hash of msgPh */
  32503. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  32504. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  32505. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  32506. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  32507. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  32508. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  32509. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  32510. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  32511. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  32512. };
  32513. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  32514. 0x66,0x6f,0x6f
  32515. };
  32516. outlen = sizeof(out);
  32517. XMEMSET(out, 0, sizeof(out));
  32518. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32519. if (ret != 0)
  32520. return WC_TEST_RET_ENC_EC(ret);
  32521. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  32522. sizeof(pKeyPh), &key);
  32523. if (ret != 0)
  32524. return WC_TEST_RET_ENC_EC(ret);
  32525. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  32526. 0);
  32527. if (ret != 0)
  32528. return WC_TEST_RET_ENC_EC(ret);
  32529. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32530. return WC_TEST_RET_ENC_NC;
  32531. #if defined(HAVE_ED448_VERIFY)
  32532. /* test verify on good msg */
  32533. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32534. NULL, 0);
  32535. if (ret != 0 || verify != 1) {
  32536. return WC_TEST_RET_ENC_EC(ret);
  32537. }
  32538. #endif
  32539. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  32540. contextPh2, sizeof(contextPh2));
  32541. if (ret != 0)
  32542. return WC_TEST_RET_ENC_EC(ret);
  32543. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32544. return WC_TEST_RET_ENC_NC;
  32545. #if defined(HAVE_ED448_VERIFY)
  32546. /* test verify on good msg */
  32547. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32548. contextPh2, sizeof(contextPh2));
  32549. if (ret != 0 || verify != 1) {
  32550. return WC_TEST_RET_ENC_EC(ret);
  32551. }
  32552. #endif
  32553. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  32554. 0);
  32555. if (ret != 0)
  32556. return WC_TEST_RET_ENC_EC(ret);
  32557. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32558. return WC_TEST_RET_ENC_NC;
  32559. #if defined(HAVE_ED448_VERIFY)
  32560. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32561. &key, NULL, 0);
  32562. if (ret != 0 || verify != 1) {
  32563. return WC_TEST_RET_ENC_EC(ret);
  32564. }
  32565. #endif
  32566. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  32567. contextPh2, sizeof(contextPh2));
  32568. if (ret != 0)
  32569. return WC_TEST_RET_ENC_EC(ret);
  32570. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32571. return WC_TEST_RET_ENC_NC;
  32572. #if defined(HAVE_ED448_VERIFY)
  32573. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32574. &key, contextPh2, sizeof(contextPh2));
  32575. if (ret != 0 || verify != 1) {
  32576. return WC_TEST_RET_ENC_EC(ret);
  32577. }
  32578. #endif
  32579. wc_ed448_free(&key);
  32580. return 0;
  32581. }
  32582. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32583. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  32584. {
  32585. wc_test_ret_t ret;
  32586. WC_RNG rng;
  32587. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  32588. defined(HAVE_ED448_KEY_IMPORT)
  32589. byte out[ED448_SIG_SIZE];
  32590. int i;
  32591. word32 outlen;
  32592. #ifdef HAVE_ED448_VERIFY
  32593. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  32594. int j;
  32595. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  32596. int verify;
  32597. #endif /* HAVE_ED448_VERIFY */
  32598. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32599. word32 keySz, sigSz;
  32600. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32601. ed448_key *key = NULL;
  32602. ed448_key *key2 = NULL;
  32603. #else
  32604. ed448_key key[1];
  32605. ed448_key key2[1];
  32606. #endif
  32607. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32608. defined(HAVE_ED448_KEY_IMPORT)
  32609. /* test vectors from
  32610. https://tools.ietf.org/html/rfc8032
  32611. */
  32612. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  32613. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32614. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32615. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32616. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32617. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32618. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32619. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32620. 0x5b
  32621. };
  32622. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  32623. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32624. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32625. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32626. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32627. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32628. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32629. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32630. 0x4e
  32631. };
  32632. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  32633. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  32634. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  32635. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  32636. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  32637. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  32638. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  32639. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  32640. 0x1b
  32641. };
  32642. /* uncompressed test */
  32643. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  32644. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32645. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32646. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32647. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32648. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32649. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32650. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32651. 0x5b
  32652. };
  32653. /* compressed prefix test */
  32654. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  32655. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32656. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32657. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32658. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32659. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32660. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32661. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32662. 0x5b
  32663. };
  32664. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  32665. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  32666. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  32667. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  32668. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  32669. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  32670. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  32671. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  32672. 0xe8
  32673. };
  32674. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  32675. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  32676. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32677. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32678. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32679. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32680. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32681. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32682. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32683. 0x80
  32684. };
  32685. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  32686. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32687. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32688. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32689. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32690. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32691. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32692. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32693. 0x80
  32694. };
  32695. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  32696. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  32697. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  32698. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  32699. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  32700. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  32701. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  32702. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  32703. 0x80
  32704. };
  32705. /* uncompressed test */
  32706. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  32707. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32708. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32709. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32710. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32711. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32712. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32713. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32714. 0x80
  32715. };
  32716. /* compressed prefix */
  32717. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  32718. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32719. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32720. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32721. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32722. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32723. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32724. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32725. 0x80
  32726. };
  32727. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  32728. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  32729. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  32730. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  32731. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  32732. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  32733. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  32734. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  32735. 0x00
  32736. };
  32737. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  32738. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  32739. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  32740. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  32741. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32742. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32743. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32744. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32745. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32746. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32747. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32748. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32749. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32750. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32751. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32752. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32753. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32754. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32755. 0x26, 0x00
  32756. };
  32757. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  32758. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  32759. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  32760. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  32761. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  32762. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  32763. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  32764. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  32765. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  32766. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  32767. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  32768. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  32769. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  32770. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  32771. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  32772. 0x3a, 0x00
  32773. };
  32774. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  32775. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  32776. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  32777. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  32778. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  32779. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  32780. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  32781. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  32782. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  32783. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  32784. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  32785. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  32786. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  32787. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  32788. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  32789. 0x3c, 0x00
  32790. };
  32791. /* uncompressed test */
  32792. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  32793. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32794. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32795. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32796. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32797. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32798. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32799. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32800. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32801. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32802. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32803. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32804. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32805. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32806. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32807. 0x26, 0x00
  32808. };
  32809. /* compressed prefix */
  32810. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  32811. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32812. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32813. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32814. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32815. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32816. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32817. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32818. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32819. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32820. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32821. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32822. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32823. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32824. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32825. 0x26, 0x00
  32826. };
  32827. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  32828. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  32829. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  32830. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  32831. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  32832. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  32833. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  32834. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  32835. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  32836. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  32837. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  32838. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  32839. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  32840. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  32841. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  32842. 0x1a, 0x00
  32843. };
  32844. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  32845. #define SIGSZ sizeof(sig1)
  32846. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { 0 };
  32847. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  32848. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  32849. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  32850. /* test of a 1023 byte long message */
  32851. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  32852. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  32853. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  32854. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  32855. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  32856. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  32857. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  32858. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  32859. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  32860. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  32861. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  32862. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  32863. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  32864. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  32865. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  32866. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  32867. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  32868. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  32869. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  32870. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  32871. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  32872. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  32873. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  32874. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  32875. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  32876. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  32877. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  32878. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  32879. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  32880. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  32881. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  32882. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  32883. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  32884. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  32885. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  32886. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  32887. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  32888. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  32889. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  32890. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  32891. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  32892. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  32893. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  32894. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  32895. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  32896. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  32897. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  32898. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  32899. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  32900. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  32901. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  32902. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  32903. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  32904. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  32905. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  32906. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  32907. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  32908. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  32909. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  32910. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  32911. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  32912. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  32913. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  32914. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  32915. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  32916. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  32917. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  32918. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  32919. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  32920. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  32921. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  32922. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  32923. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  32924. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  32925. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  32926. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  32927. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  32928. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  32929. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  32930. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  32931. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  32932. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  32933. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  32934. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  32935. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  32936. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  32937. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  32938. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  32939. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  32940. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  32941. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  32942. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  32943. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  32944. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  32945. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  32946. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  32947. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  32948. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  32949. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  32950. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  32951. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  32952. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  32953. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  32954. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  32955. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  32956. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  32957. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  32958. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  32959. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  32960. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  32961. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  32962. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  32963. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  32964. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  32965. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  32966. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  32967. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  32968. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  32969. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  32970. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  32971. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  32972. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  32973. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  32974. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  32975. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  32976. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  32977. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  32978. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  32979. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  32980. };
  32981. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  32982. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  32983. sizeof(msg2),
  32984. sizeof(msg3),
  32985. 0 /*sizeof(msg1)*/,
  32986. 0 /*sizeof(msg1)*/,
  32987. sizeof(msg4)
  32988. };
  32989. #ifndef NO_ASN
  32990. static const byte privateEd448[] = {
  32991. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  32992. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  32993. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32994. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32995. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32996. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32997. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32998. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32999. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  33000. 0x5b
  33001. };
  33002. static const byte publicEd448[] = {
  33003. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  33004. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  33005. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33006. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33007. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33008. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33009. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33010. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33011. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33012. };
  33013. static const byte privPubEd448[] = {
  33014. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  33015. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  33016. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  33017. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  33018. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  33019. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  33020. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  33021. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  33022. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  33023. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  33024. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33025. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33026. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33027. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33028. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33029. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33030. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33031. };
  33032. word32 idx;
  33033. #endif /* NO_ASN */
  33034. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33035. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33036. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33037. ed448_key *key3 = NULL;
  33038. #else
  33039. ed448_key key3[1];
  33040. #endif
  33041. #endif
  33042. WOLFSSL_ENTER("ed448_test");
  33043. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33044. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33045. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33046. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33047. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33048. #endif
  33049. #endif
  33050. /* create ed448 keys */
  33051. #ifndef HAVE_FIPS
  33052. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33053. #else
  33054. ret = wc_InitRng(&rng);
  33055. #endif
  33056. if (ret != 0) {
  33057. XMEMSET(&rng, 0, sizeof(rng));
  33058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33059. }
  33060. ret = wc_ed448_init(key);
  33061. if (ret < 0)
  33062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33063. ret = wc_ed448_init(key2);
  33064. if (ret < 0)
  33065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33066. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33067. ret = wc_ed448_init(key3);
  33068. if (ret < 0)
  33069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33070. #endif
  33071. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  33072. if (ret < 0)
  33073. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33074. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  33075. if (ret < 0)
  33076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33077. /* helper functions for signature and key size */
  33078. keySz = (word32)wc_ed448_size(key);
  33079. sigSz = (word32)wc_ed448_sig_size(key);
  33080. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  33081. defined(HAVE_ED448_KEY_IMPORT)
  33082. for (i = 0; i < 6; i++) {
  33083. outlen = sizeof(out);
  33084. XMEMSET(out, 0, sizeof(out));
  33085. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  33086. pKeySz[i], key) != 0)
  33087. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33088. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  33089. 0) != 0)
  33090. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33091. if (XMEMCMP(out, sigs[i], 114))
  33092. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33093. #if defined(HAVE_ED448_VERIFY)
  33094. /* test verify on good msg */
  33095. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33096. NULL, 0) != 0 || verify != 1)
  33097. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33098. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  33099. /* test verify on good msg using streaming interface directly */
  33100. if (wc_ed448_verify_msg_init(out, outlen,
  33101. key, (byte)Ed448, NULL, 0) != 0)
  33102. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33103. for (j = 0; j < msgSz[i]; j += i) {
  33104. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  33105. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33106. }
  33107. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  33108. key) != 0 || verify != 1)
  33109. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33110. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  33111. /* test verify on bad msg */
  33112. out[outlen-2] = out[outlen-2] + 1;
  33113. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33114. NULL, 0) == 0 || verify == 1)
  33115. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33116. #endif /* HAVE_ED448_VERIFY */
  33117. /* test api for import/exporting keys */
  33118. {
  33119. byte *exportPKey = NULL;
  33120. byte *exportSKey = NULL;
  33121. word32 exportPSz = ED448_KEY_SIZE;
  33122. word32 exportSSz = ED448_KEY_SIZE;
  33123. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33124. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33125. if ((exportPKey == NULL) || (exportSKey == NULL))
  33126. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33127. ret = 0;
  33128. do {
  33129. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  33130. ret = WC_TEST_RET_ENC_I(i);
  33131. break;
  33132. }
  33133. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  33134. ret = WC_TEST_RET_ENC_I(i);
  33135. break;
  33136. }
  33137. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  33138. ret = WC_TEST_RET_ENC_I(i);
  33139. break;
  33140. }
  33141. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  33142. exportPKey, exportPSz, key2) != 0) {
  33143. ret = WC_TEST_RET_ENC_I(i);
  33144. break;
  33145. }
  33146. /* clear "out" buffer and test sign with imported keys */
  33147. outlen = sizeof(out);
  33148. XMEMSET(out, 0, sizeof(out));
  33149. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  33150. 0) != 0) {
  33151. ret = WC_TEST_RET_ENC_I(i);
  33152. break;
  33153. }
  33154. } while(0);
  33155. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33156. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33157. if (ret != 0)
  33158. goto out;
  33159. }
  33160. #if defined(HAVE_ED448_VERIFY)
  33161. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  33162. NULL, 0) != 0 || verify != 1)
  33163. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33164. if (XMEMCMP(out, sigs[i], SIGSZ))
  33165. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33166. #endif /* HAVE_ED448_VERIFY */
  33167. }
  33168. ret = ed448_ctx_test();
  33169. if (ret != 0)
  33170. goto out;
  33171. ret = ed448ph_test();
  33172. if (ret != 0)
  33173. goto out;
  33174. #ifndef NO_ASN
  33175. /* Try ASN.1 encoded private-only key and public key. */
  33176. idx = 0;
  33177. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  33178. sizeof(privateEd448));
  33179. if (ret != 0)
  33180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33181. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33182. if (ret != BAD_FUNC_ARG)
  33183. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33184. idx = 0;
  33185. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  33186. if (ret != 0)
  33187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33188. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33189. if (ret != 0)
  33190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33191. if (XMEMCMP(out, sigs[0], SIGSZ))
  33192. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33193. #if defined(HAVE_ED448_VERIFY)
  33194. /* test verify on good msg */
  33195. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  33196. NULL, 0);
  33197. if (ret != 0 || verify != 1)
  33198. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33199. #endif /* HAVE_ED448_VERIFY */
  33200. wc_ed448_free(key3);
  33201. ret = wc_ed448_init(key3);
  33202. if (ret < 0)
  33203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33204. idx = 0;
  33205. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  33206. sizeof(privPubEd448));
  33207. if (ret != 0)
  33208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33209. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33210. if (ret != 0)
  33211. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33212. if (XMEMCMP(out, sigs[0], SIGSZ))
  33213. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33214. #endif /* NO_ASN */
  33215. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33216. ret = 0;
  33217. out:
  33218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33219. if (key) {
  33220. wc_ed448_free(key);
  33221. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33222. }
  33223. if (key2) {
  33224. wc_ed448_free(key2);
  33225. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33226. }
  33227. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33228. if (key3) {
  33229. wc_ed448_free(key3);
  33230. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33231. }
  33232. #endif
  33233. #else
  33234. wc_ed448_free(key);
  33235. wc_ed448_free(key2);
  33236. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33237. wc_ed448_free(key3);
  33238. #endif
  33239. #endif
  33240. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  33241. wc_FreeRng(&rng);
  33242. #endif
  33243. if (ret < 0)
  33244. return ret;
  33245. /* hush warnings of unused keySz and sigSz */
  33246. (void)keySz;
  33247. (void)sigSz;
  33248. ret = ed448_test_check_key();
  33249. if (ret < 0)
  33250. return ret;
  33251. #ifdef WOLFSSL_TEST_CERT
  33252. ret = ed448_test_cert();
  33253. if (ret < 0)
  33254. return ret;
  33255. #ifdef WOLFSSL_CERT_GEN
  33256. ret = ed448_test_make_cert();
  33257. if (ret < 0)
  33258. return ret;
  33259. #endif /* WOLFSSL_CERT_GEN */
  33260. #endif /* WOLFSSL_TEST_CERT */
  33261. return 0;
  33262. }
  33263. #endif /* HAVE_ED448 */
  33264. #ifdef WOLFSSL_HAVE_KYBER
  33265. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  33266. #ifdef WOLFSSL_KYBER512
  33267. static wc_test_ret_t kyber512_kat(void)
  33268. {
  33269. wc_test_ret_t ret;
  33270. #ifdef WOLFSSL_SMALL_STACK
  33271. KyberKey *key = NULL;
  33272. byte *priv = NULL;
  33273. byte *pub = NULL;
  33274. byte *ct = NULL;
  33275. byte *ss = NULL;
  33276. byte *ss_dec = NULL;
  33277. #else
  33278. KyberKey key[1];
  33279. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  33280. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  33281. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  33282. byte ss[KYBER_SS_SZ];
  33283. byte ss_dec[KYBER_SS_SZ];
  33284. #endif
  33285. int key_inited = 0;
  33286. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_rand[] = {
  33287. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33288. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33289. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33290. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33291. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33292. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33293. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33294. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33295. };
  33296. WOLFSSL_SMALL_STACK_STATIC const byte kyber512enc_rand[] = {
  33297. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33298. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33299. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33300. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33301. };
  33302. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_pk[] = {
  33303. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33304. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33305. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33306. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33307. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33308. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33309. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33310. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33311. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33312. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33313. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33314. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33315. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33316. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33317. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33318. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33319. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33320. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33321. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33322. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33323. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33324. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33325. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33326. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33327. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33328. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33329. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33330. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33331. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33332. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33333. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33334. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33335. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33336. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33337. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33338. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33339. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33340. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33341. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33342. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33343. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33344. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33345. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33346. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33347. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33348. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33349. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33350. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33351. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33352. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33353. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33354. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33355. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33356. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33357. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33358. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33359. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33360. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33361. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33362. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33363. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33364. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33365. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33366. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33367. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33368. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33369. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33370. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33371. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33372. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33373. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33374. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33375. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33376. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33377. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33378. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33379. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33380. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33381. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33382. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33383. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33384. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33385. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33386. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33387. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33388. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33389. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33390. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33391. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33392. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33393. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33394. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33395. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33396. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33397. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33398. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33399. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33400. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33401. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33402. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  33403. };
  33404. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_sk[] = {
  33405. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  33406. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  33407. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  33408. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  33409. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  33410. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  33411. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  33412. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  33413. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  33414. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  33415. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  33416. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  33417. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  33418. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  33419. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  33420. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  33421. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  33422. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  33423. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  33424. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  33425. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  33426. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  33427. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  33428. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  33429. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  33430. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  33431. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  33432. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  33433. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  33434. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  33435. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  33436. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  33437. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  33438. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  33439. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  33440. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  33441. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  33442. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  33443. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  33444. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  33445. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  33446. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  33447. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  33448. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  33449. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  33450. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  33451. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  33452. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  33453. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  33454. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  33455. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  33456. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  33457. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  33458. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  33459. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  33460. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  33461. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  33462. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  33463. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  33464. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  33465. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  33466. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  33467. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  33468. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  33469. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  33470. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  33471. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  33472. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  33473. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  33474. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  33475. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  33476. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  33477. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  33478. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  33479. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  33480. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  33481. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  33482. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  33483. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  33484. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  33485. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  33486. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  33487. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  33488. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  33489. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  33490. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  33491. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  33492. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  33493. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  33494. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  33495. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  33496. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  33497. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  33498. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  33499. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  33500. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  33501. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33502. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33503. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33504. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33505. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33506. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33507. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33508. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33509. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33510. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33511. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33512. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33513. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33514. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33515. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33516. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33517. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33518. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33519. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33520. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33521. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33522. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33523. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33524. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33525. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33526. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33527. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33528. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33529. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33530. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33531. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33532. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33533. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33534. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33535. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33536. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33537. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33538. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33539. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33540. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33541. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33542. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33543. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33544. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33545. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33546. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33547. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33548. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33549. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33550. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33551. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33552. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33553. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33554. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33555. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33556. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33557. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33558. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33559. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33560. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33561. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33562. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33563. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33564. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33565. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33566. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33567. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33568. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33569. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33570. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33571. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33572. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33573. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33574. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33575. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33576. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33577. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33578. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33579. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33580. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33581. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33582. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33583. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33584. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33585. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33586. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33587. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33588. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33589. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33590. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33591. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33592. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33593. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33594. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33595. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33596. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33597. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33598. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33599. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33600. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  33601. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  33602. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  33603. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  33604. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  33605. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33606. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33607. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33608. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33609. };
  33610. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ct[] = {
  33611. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  33612. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  33613. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  33614. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  33615. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  33616. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  33617. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  33618. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  33619. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  33620. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  33621. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  33622. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  33623. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  33624. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  33625. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  33626. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  33627. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  33628. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  33629. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  33630. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  33631. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  33632. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  33633. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  33634. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  33635. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  33636. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  33637. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  33638. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  33639. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  33640. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  33641. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  33642. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  33643. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  33644. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  33645. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  33646. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  33647. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  33648. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  33649. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  33650. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  33651. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  33652. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  33653. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  33654. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  33655. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  33656. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  33657. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  33658. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  33659. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  33660. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  33661. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  33662. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  33663. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  33664. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  33665. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  33666. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  33667. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  33668. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  33669. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  33670. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  33671. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  33672. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  33673. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  33674. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  33675. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  33676. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  33677. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  33678. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  33679. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  33680. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  33681. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  33682. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  33683. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  33684. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  33685. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  33686. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  33687. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  33688. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  33689. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  33690. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  33691. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  33692. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  33693. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  33694. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  33695. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  33696. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  33697. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  33698. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  33699. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  33700. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  33701. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  33702. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  33703. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  33704. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  33705. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  33706. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  33707. };
  33708. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ss[] = {
  33709. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  33710. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  33711. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  33712. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  33713. };
  33714. #ifdef WOLFSSL_SMALL_STACK
  33715. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  33716. DYNAMIC_TYPE_TMP_BUFFER);
  33717. priv = (byte *)XMALLOC(KYBER512_PRIVATE_KEY_SIZE, HEAP_HINT,
  33718. DYNAMIC_TYPE_TMP_BUFFER);
  33719. pub = (byte *)XMALLOC(KYBER512_PUBLIC_KEY_SIZE, HEAP_HINT,
  33720. DYNAMIC_TYPE_TMP_BUFFER);
  33721. ct = (byte *)XMALLOC(KYBER512_CIPHER_TEXT_SIZE, HEAP_HINT,
  33722. DYNAMIC_TYPE_TMP_BUFFER);
  33723. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33724. DYNAMIC_TYPE_TMP_BUFFER);
  33725. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33726. DYNAMIC_TYPE_TMP_BUFFER);
  33727. if (! (key && priv && pub && ct && ss && ss_dec))
  33728. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  33729. #endif
  33730. ret = wc_KyberKey_Init(KYBER512, key, HEAP_HINT, INVALID_DEVID);
  33731. if (ret != 0)
  33732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33733. else
  33734. key_inited = 1;
  33735. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber512_rand,
  33736. sizeof(kyber512_rand));
  33737. if (ret != 0)
  33738. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33739. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER512_PUBLIC_KEY_SIZE);
  33740. if (ret != 0)
  33741. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33742. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER512_PRIVATE_KEY_SIZE);
  33743. if (ret != 0)
  33744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33745. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  33746. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33747. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  33748. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33749. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber512enc_rand,
  33750. sizeof(kyber512enc_rand));
  33751. if (ret != 0)
  33752. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33753. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  33754. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33755. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33756. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33757. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber512_ct));
  33758. if (ret != 0)
  33759. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33760. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33761. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33762. out:
  33763. if (key_inited)
  33764. wc_KyberKey_Free(key);
  33765. #ifdef WOLFSSL_SMALL_STACK
  33766. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33767. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33768. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33769. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33770. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33771. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33772. #endif
  33773. return ret;
  33774. }
  33775. #endif /* WOLFSSL_KYBER512 */
  33776. #ifdef WOLFSSL_KYBER768
  33777. static wc_test_ret_t kyber768_kat(void)
  33778. {
  33779. wc_test_ret_t ret;
  33780. #ifdef WOLFSSL_SMALL_STACK
  33781. KyberKey *key = NULL;
  33782. byte *priv = NULL;
  33783. byte *pub = NULL;
  33784. byte *ct = NULL;
  33785. byte *ss = NULL;
  33786. byte *ss_dec = NULL;
  33787. #else
  33788. KyberKey key[1];
  33789. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  33790. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  33791. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  33792. byte ss[KYBER_SS_SZ];
  33793. byte ss_dec[KYBER_SS_SZ];
  33794. #endif
  33795. int key_inited = 0;
  33796. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_rand[] = {
  33797. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33798. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33799. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33800. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33801. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33802. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33803. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33804. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33805. };
  33806. WOLFSSL_SMALL_STACK_STATIC const byte kyber768enc_rand[] = {
  33807. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33808. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33809. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33810. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33811. };
  33812. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_pk[] = {
  33813. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  33814. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  33815. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  33816. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  33817. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  33818. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  33819. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  33820. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  33821. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  33822. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  33823. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  33824. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  33825. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  33826. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  33827. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  33828. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  33829. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  33830. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  33831. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  33832. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  33833. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  33834. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  33835. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  33836. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  33837. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  33838. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  33839. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  33840. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  33841. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  33842. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  33843. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  33844. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  33845. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  33846. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  33847. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  33848. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  33849. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  33850. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  33851. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  33852. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  33853. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  33854. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  33855. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  33856. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  33857. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  33858. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  33859. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  33860. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  33861. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  33862. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  33863. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  33864. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  33865. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  33866. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  33867. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  33868. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  33869. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  33870. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  33871. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  33872. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  33873. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  33874. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  33875. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  33876. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  33877. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  33878. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  33879. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  33880. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  33881. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  33882. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  33883. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  33884. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  33885. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  33886. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  33887. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  33888. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  33889. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  33890. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  33891. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  33892. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  33893. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  33894. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  33895. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  33896. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  33897. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  33898. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  33899. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  33900. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  33901. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  33902. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  33903. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  33904. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  33905. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  33906. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  33907. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  33908. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  33909. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  33910. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  33911. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  33912. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  33913. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  33914. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  33915. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  33916. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  33917. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  33918. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  33919. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  33920. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  33921. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  33922. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  33923. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  33924. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  33925. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  33926. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  33927. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  33928. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  33929. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  33930. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  33931. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  33932. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  33933. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  33934. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  33935. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  33936. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  33937. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  33938. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  33939. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  33940. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  33941. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  33942. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  33943. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  33944. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  33945. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  33946. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  33947. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  33948. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  33949. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  33950. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  33951. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  33952. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  33953. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  33954. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  33955. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  33956. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  33957. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33958. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33959. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33960. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  33961. };
  33962. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_sk[] = {
  33963. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  33964. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  33965. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  33966. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  33967. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  33968. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  33969. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  33970. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  33971. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  33972. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  33973. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  33974. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  33975. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  33976. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  33977. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  33978. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  33979. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  33980. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  33981. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  33982. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  33983. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  33984. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  33985. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  33986. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  33987. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  33988. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  33989. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  33990. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  33991. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  33992. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  33993. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  33994. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  33995. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  33996. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  33997. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  33998. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  33999. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34000. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34001. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34002. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34003. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34004. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34005. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34006. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34007. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34008. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34009. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34010. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34011. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34012. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34013. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34014. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34015. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34016. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34017. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34018. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34019. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34020. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34021. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34022. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34023. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34024. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34025. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34026. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34027. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34028. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34029. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34030. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34031. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34032. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34033. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34034. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34035. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34036. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34037. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34038. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34039. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34040. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34041. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34042. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34043. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34044. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34045. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34046. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34047. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34048. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34049. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34050. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34051. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34052. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34053. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34054. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34055. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34056. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34057. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34058. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34059. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34060. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34061. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34062. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34063. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34064. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34065. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34066. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34067. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34068. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34069. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34070. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34071. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34072. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34073. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34074. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34075. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34076. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34077. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34078. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34079. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34080. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34081. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34082. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34083. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34084. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34085. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34086. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34087. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34088. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34089. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34090. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34091. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34092. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34093. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34094. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34095. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34096. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34097. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34098. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34099. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34100. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34101. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34102. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34103. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34104. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34105. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34106. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34107. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  34108. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  34109. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  34110. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  34111. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  34112. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  34113. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  34114. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  34115. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  34116. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  34117. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  34118. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  34119. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  34120. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  34121. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  34122. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  34123. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  34124. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  34125. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  34126. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  34127. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  34128. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  34129. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  34130. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  34131. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  34132. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  34133. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  34134. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  34135. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  34136. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  34137. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  34138. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  34139. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  34140. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  34141. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  34142. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  34143. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  34144. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  34145. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  34146. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  34147. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  34148. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  34149. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  34150. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  34151. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  34152. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  34153. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  34154. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  34155. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  34156. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  34157. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  34158. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  34159. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  34160. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  34161. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  34162. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  34163. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  34164. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  34165. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  34166. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  34167. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  34168. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  34169. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  34170. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  34171. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  34172. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  34173. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  34174. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  34175. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  34176. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  34177. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  34178. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  34179. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  34180. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  34181. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34182. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34183. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34184. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34185. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34186. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34187. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34188. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34189. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34190. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34191. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34192. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34193. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34194. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34195. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34196. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34197. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34198. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34199. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34200. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34201. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34202. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34203. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34204. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34205. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34206. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34207. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34208. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34209. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34210. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34211. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34212. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34213. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34214. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34215. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34216. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34217. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34218. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34219. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34220. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34221. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34222. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34223. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34224. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34225. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34226. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34227. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34228. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34229. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34230. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34231. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34232. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34233. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34234. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34235. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34236. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34237. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34238. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34239. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34240. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34241. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34242. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34243. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34244. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34245. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34246. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34247. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34248. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34249. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34250. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34251. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34252. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34253. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34254. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  34255. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  34256. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  34257. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  34258. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  34259. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34260. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34261. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34262. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34263. };
  34264. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ct[] = {
  34265. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  34266. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  34267. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  34268. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  34269. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  34270. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  34271. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  34272. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  34273. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  34274. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  34275. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  34276. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  34277. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  34278. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  34279. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  34280. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  34281. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  34282. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  34283. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  34284. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  34285. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  34286. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  34287. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  34288. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  34289. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  34290. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  34291. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  34292. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  34293. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  34294. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  34295. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  34296. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  34297. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  34298. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  34299. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  34300. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  34301. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  34302. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  34303. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  34304. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  34305. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  34306. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  34307. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  34308. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  34309. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  34310. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  34311. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  34312. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  34313. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  34314. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  34315. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  34316. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  34317. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  34318. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  34319. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  34320. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  34321. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  34322. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  34323. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  34324. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  34325. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  34326. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  34327. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  34328. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  34329. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  34330. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  34331. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  34332. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  34333. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  34334. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  34335. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  34336. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  34337. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  34338. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  34339. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  34340. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  34341. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  34342. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  34343. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  34344. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  34345. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  34346. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  34347. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  34348. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  34349. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  34350. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  34351. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  34352. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  34353. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  34354. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  34355. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  34356. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  34357. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  34358. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  34359. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  34360. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  34361. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  34362. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  34363. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  34364. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  34365. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  34366. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  34367. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  34368. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  34369. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  34370. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  34371. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  34372. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  34373. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  34374. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  34375. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  34376. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  34377. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  34378. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  34379. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  34380. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  34381. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  34382. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  34383. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  34384. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  34385. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  34386. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  34387. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  34388. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  34389. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  34390. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  34391. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  34392. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  34393. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  34394. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  34395. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  34396. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  34397. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  34398. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  34399. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  34400. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  34401. };
  34402. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ss[] = {
  34403. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  34404. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  34405. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  34406. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  34407. };
  34408. #ifdef WOLFSSL_SMALL_STACK
  34409. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  34410. DYNAMIC_TYPE_TMP_BUFFER);
  34411. priv = (byte *)XMALLOC(KYBER768_PRIVATE_KEY_SIZE, HEAP_HINT,
  34412. DYNAMIC_TYPE_TMP_BUFFER);
  34413. pub = (byte *)XMALLOC(KYBER768_PUBLIC_KEY_SIZE, HEAP_HINT,
  34414. DYNAMIC_TYPE_TMP_BUFFER);
  34415. ct = (byte *)XMALLOC(KYBER768_CIPHER_TEXT_SIZE, HEAP_HINT,
  34416. DYNAMIC_TYPE_TMP_BUFFER);
  34417. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34418. DYNAMIC_TYPE_TMP_BUFFER);
  34419. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34420. DYNAMIC_TYPE_TMP_BUFFER);
  34421. if (! (key && priv && pub && ct && ss && ss_dec))
  34422. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34423. #endif
  34424. ret = wc_KyberKey_Init(KYBER768, key, HEAP_HINT, INVALID_DEVID);
  34425. if (ret != 0)
  34426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34427. else
  34428. key_inited = 1;
  34429. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber768_rand,
  34430. sizeof(kyber768_rand));
  34431. if (ret != 0)
  34432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34433. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER768_PUBLIC_KEY_SIZE);
  34434. if (ret != 0)
  34435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34436. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER768_PRIVATE_KEY_SIZE);
  34437. if (ret != 0)
  34438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34439. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  34440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34441. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  34442. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34443. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber768enc_rand,
  34444. sizeof(kyber768enc_rand));
  34445. if (ret != 0)
  34446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34447. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  34448. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34449. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34450. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34451. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber768_ct));
  34452. if (ret != 0)
  34453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34454. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34455. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34456. out:
  34457. if (key_inited)
  34458. wc_KyberKey_Free(key);
  34459. #ifdef WOLFSSL_SMALL_STACK
  34460. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34461. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34462. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34463. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34464. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34465. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34466. #endif
  34467. return ret;
  34468. }
  34469. #endif /* WOLFSSL_KYBER768 */
  34470. #ifdef WOLFSSL_KYBER1024
  34471. static wc_test_ret_t kyber1024_kat(void)
  34472. {
  34473. wc_test_ret_t ret;
  34474. #ifdef WOLFSSL_SMALL_STACK
  34475. KyberKey *key = NULL;
  34476. byte *priv = NULL;
  34477. byte *pub = NULL;
  34478. byte *ct = NULL;
  34479. byte *ss = NULL;
  34480. byte *ss_dec = NULL;
  34481. #else
  34482. KyberKey key[1];
  34483. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  34484. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  34485. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  34486. byte ss[KYBER_SS_SZ];
  34487. byte ss_dec[KYBER_SS_SZ];
  34488. #endif
  34489. int key_inited = 0;
  34490. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_rand[] = {
  34491. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  34492. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  34493. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  34494. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  34495. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34496. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34497. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34498. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34499. };
  34500. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024enc_rand[] = {
  34501. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  34502. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  34503. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  34504. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  34505. };
  34506. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_pk[] = {
  34507. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  34508. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  34509. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  34510. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  34511. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  34512. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  34513. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  34514. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  34515. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  34516. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  34517. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  34518. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  34519. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  34520. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  34521. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  34522. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  34523. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  34524. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  34525. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  34526. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  34527. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  34528. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  34529. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  34530. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  34531. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  34532. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  34533. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  34534. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  34535. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  34536. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  34537. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  34538. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  34539. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  34540. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  34541. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  34542. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  34543. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  34544. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  34545. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  34546. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  34547. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  34548. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  34549. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  34550. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  34551. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  34552. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  34553. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  34554. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  34555. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  34556. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  34557. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  34558. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  34559. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  34560. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  34561. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  34562. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  34563. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  34564. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  34565. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  34566. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  34567. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  34568. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  34569. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  34570. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  34571. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  34572. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  34573. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  34574. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  34575. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  34576. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  34577. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  34578. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  34579. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  34580. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  34581. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  34582. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  34583. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  34584. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  34585. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  34586. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  34587. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  34588. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  34589. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  34590. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  34591. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  34592. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  34593. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  34594. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  34595. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  34596. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  34597. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  34598. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  34599. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  34600. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  34601. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  34602. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  34603. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  34604. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  34605. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  34606. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  34607. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  34608. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  34609. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  34610. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  34611. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  34612. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  34613. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  34614. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  34615. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  34616. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  34617. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  34618. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  34619. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  34620. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  34621. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  34622. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  34623. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  34624. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  34625. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  34626. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  34627. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  34628. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  34629. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  34630. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  34631. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  34632. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  34633. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  34634. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  34635. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  34636. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  34637. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  34638. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  34639. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  34640. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  34641. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  34642. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  34643. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  34644. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  34645. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  34646. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  34647. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  34648. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  34649. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  34650. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  34651. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  34652. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  34653. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  34654. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  34655. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  34656. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  34657. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  34658. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  34659. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  34660. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  34661. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  34662. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  34663. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  34664. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  34665. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  34666. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  34667. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  34668. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  34669. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  34670. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  34671. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  34672. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  34673. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  34674. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  34675. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  34676. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  34677. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  34678. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  34679. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  34680. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  34681. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  34682. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  34683. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  34684. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  34685. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  34686. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  34687. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  34688. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  34689. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  34690. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  34691. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  34692. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  34693. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  34694. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  34695. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  34696. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  34697. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  34698. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  34699. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34700. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34701. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34702. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34703. };
  34704. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_sk[] = {
  34705. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34706. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34707. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34708. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34709. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34710. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34711. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34712. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34713. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34714. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34715. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34716. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34717. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34718. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34719. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34720. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34721. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34722. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34723. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34724. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34725. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34726. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34727. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34728. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34729. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34730. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34731. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34732. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34733. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34734. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34735. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34736. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34737. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34738. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34739. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34740. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34741. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34742. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34743. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34744. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34745. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34746. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34747. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34748. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34749. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34750. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34751. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34752. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34753. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34754. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34755. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34756. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34757. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34758. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34759. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34760. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34761. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34762. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34763. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34764. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34765. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34766. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34767. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34768. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34769. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34770. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34771. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34772. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34773. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34774. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34775. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34776. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34777. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34778. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34779. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34780. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34781. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34782. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34783. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34784. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34785. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34786. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34787. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34788. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34789. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34790. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34791. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34792. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34793. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34794. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34795. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34796. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34797. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34798. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34799. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34800. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34801. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34802. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34803. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34804. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34805. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34806. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34807. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34808. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34809. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34810. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34811. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34812. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34813. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34814. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34815. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34816. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34817. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34818. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34819. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34820. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34821. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34822. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34823. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34824. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34825. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34826. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34827. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34828. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34829. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34830. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34831. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34832. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34833. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34834. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34835. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34836. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34837. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34838. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34839. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34840. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34841. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34842. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34843. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34844. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34845. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34846. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34847. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34848. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34849. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  34850. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  34851. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  34852. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  34853. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  34854. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  34855. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  34856. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  34857. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  34858. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  34859. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  34860. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  34861. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  34862. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  34863. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  34864. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  34865. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  34866. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  34867. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  34868. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  34869. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  34870. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  34871. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  34872. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  34873. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  34874. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  34875. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  34876. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  34877. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  34878. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  34879. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  34880. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  34881. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  34882. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  34883. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  34884. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  34885. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  34886. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  34887. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  34888. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  34889. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  34890. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  34891. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  34892. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  34893. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  34894. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  34895. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  34896. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  34897. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  34898. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  34899. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  34900. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  34901. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  34902. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  34903. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  34904. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  34905. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  34906. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  34907. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  34908. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  34909. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  34910. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  34911. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  34912. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  34913. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  34914. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  34915. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  34916. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  34917. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  34918. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  34919. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  34920. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  34921. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  34922. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  34923. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  34924. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  34925. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  34926. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  34927. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  34928. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  34929. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  34930. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  34931. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  34932. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  34933. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  34934. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  34935. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  34936. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  34937. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  34938. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  34939. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  34940. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  34941. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  34942. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  34943. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  34944. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  34945. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  34946. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  34947. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  34948. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  34949. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  34950. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  34951. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  34952. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  34953. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  34954. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  34955. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  34956. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  34957. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  34958. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  34959. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  34960. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  34961. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  34962. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  34963. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  34964. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  34965. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  34966. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  34967. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  34968. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  34969. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  34970. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  34971. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  34972. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  34973. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  34974. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  34975. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  34976. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  34977. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  34978. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  34979. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  34980. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  34981. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  34982. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  34983. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  34984. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  34985. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  34986. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  34987. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  34988. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  34989. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  34990. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  34991. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  34992. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  34993. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  34994. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  34995. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  34996. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  34997. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  34998. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  34999. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  35000. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  35001. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  35002. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  35003. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  35004. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  35005. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  35006. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  35007. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  35008. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  35009. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  35010. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  35011. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  35012. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  35013. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  35014. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  35015. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  35016. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  35017. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  35018. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  35019. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  35020. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  35021. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  35022. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  35023. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  35024. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  35025. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  35026. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  35027. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  35028. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  35029. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  35030. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  35031. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  35032. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  35033. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  35034. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  35035. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  35036. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  35037. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  35038. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  35039. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  35040. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  35041. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  35042. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  35043. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  35044. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  35045. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  35046. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  35047. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  35048. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  35049. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  35050. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  35051. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  35052. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  35053. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  35054. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  35055. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  35056. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  35057. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  35058. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  35059. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  35060. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  35061. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  35062. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  35063. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  35064. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  35065. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  35066. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  35067. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  35068. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  35069. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  35070. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  35071. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  35072. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  35073. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  35074. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  35075. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  35076. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  35077. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  35078. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  35079. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  35080. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  35081. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  35082. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  35083. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  35084. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  35085. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  35086. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  35087. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  35088. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  35089. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  35090. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  35091. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  35092. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  35093. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  35094. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  35095. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  35096. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  35097. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  35098. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  35099. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  35100. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  35101. };
  35102. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ct[] = {
  35103. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  35104. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  35105. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  35106. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  35107. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  35108. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  35109. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  35110. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  35111. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  35112. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  35113. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  35114. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  35115. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  35116. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  35117. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  35118. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  35119. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  35120. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  35121. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  35122. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  35123. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  35124. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  35125. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  35126. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  35127. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  35128. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  35129. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  35130. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  35131. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  35132. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  35133. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  35134. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  35135. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  35136. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  35137. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  35138. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  35139. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  35140. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  35141. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  35142. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  35143. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  35144. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  35145. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  35146. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  35147. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  35148. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  35149. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  35150. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  35151. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  35152. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  35153. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  35154. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  35155. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  35156. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  35157. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  35158. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  35159. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  35160. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  35161. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  35162. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  35163. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  35164. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  35165. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  35166. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  35167. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  35168. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  35169. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  35170. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  35171. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  35172. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  35173. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  35174. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  35175. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  35176. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  35177. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  35178. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  35179. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  35180. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  35181. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  35182. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  35183. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  35184. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  35185. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  35186. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  35187. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  35188. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  35189. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  35190. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  35191. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  35192. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  35193. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  35194. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  35195. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  35196. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  35197. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  35198. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  35199. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  35200. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  35201. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  35202. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  35203. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  35204. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  35205. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  35206. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  35207. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  35208. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  35209. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  35210. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  35211. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  35212. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  35213. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  35214. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  35215. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  35216. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  35217. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  35218. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  35219. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  35220. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  35221. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  35222. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  35223. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  35224. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  35225. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  35226. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  35227. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  35228. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  35229. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  35230. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  35231. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  35232. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  35233. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  35234. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  35235. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  35236. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  35237. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  35238. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  35239. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  35240. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  35241. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  35242. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  35243. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  35244. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  35245. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  35246. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  35247. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  35248. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  35249. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  35250. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  35251. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  35252. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  35253. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  35254. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  35255. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  35256. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  35257. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  35258. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  35259. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  35260. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  35261. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  35262. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  35263. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  35264. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  35265. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  35266. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  35267. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  35268. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  35269. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  35270. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  35271. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  35272. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  35273. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  35274. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  35275. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  35276. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  35277. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  35278. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  35279. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  35280. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  35281. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  35282. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  35283. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  35284. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  35285. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  35286. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  35287. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  35288. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  35289. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  35290. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  35291. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  35292. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  35293. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  35294. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  35295. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  35296. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  35297. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  35298. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  35299. };
  35300. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ss[] = {
  35301. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  35302. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  35303. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  35304. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  35305. };
  35306. #ifdef WOLFSSL_SMALL_STACK
  35307. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35308. DYNAMIC_TYPE_TMP_BUFFER);
  35309. priv = (byte *)XMALLOC(KYBER1024_PRIVATE_KEY_SIZE, HEAP_HINT,
  35310. DYNAMIC_TYPE_TMP_BUFFER);
  35311. pub = (byte *)XMALLOC(KYBER1024_PUBLIC_KEY_SIZE, HEAP_HINT,
  35312. DYNAMIC_TYPE_TMP_BUFFER);
  35313. ct = (byte *)XMALLOC(KYBER1024_CIPHER_TEXT_SIZE, HEAP_HINT,
  35314. DYNAMIC_TYPE_TMP_BUFFER);
  35315. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35316. DYNAMIC_TYPE_TMP_BUFFER);
  35317. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35318. DYNAMIC_TYPE_TMP_BUFFER);
  35319. if (! (key && priv && pub && ct && ss && ss_dec))
  35320. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35321. #endif
  35322. ret = wc_KyberKey_Init(KYBER1024, key, HEAP_HINT, INVALID_DEVID);
  35323. if (ret != 0)
  35324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35325. else
  35326. key_inited = 1;
  35327. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber1024_rand,
  35328. sizeof(kyber1024_rand));
  35329. if (ret != 0)
  35330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35331. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER_MAX_PUBLIC_KEY_SIZE);
  35332. if (ret != 0)
  35333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35334. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER_MAX_PRIVATE_KEY_SIZE);
  35335. if (ret != 0)
  35336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35337. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  35338. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35339. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  35340. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35341. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber1024enc_rand,
  35342. sizeof(kyber1024enc_rand));
  35343. if (ret != 0)
  35344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35345. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  35346. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35347. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35348. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35349. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber1024_ct));
  35350. if (ret != 0)
  35351. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35352. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35353. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35354. out:
  35355. if (key_inited)
  35356. wc_KyberKey_Free(key);
  35357. #ifdef WOLFSSL_SMALL_STACK
  35358. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35359. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35360. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35361. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35362. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35363. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35364. #endif
  35365. return ret;
  35366. }
  35367. #endif /* WOLFSSL_KYBER1024 */
  35368. #endif /* WOLFSSL_WC_KYBER */
  35369. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  35370. {
  35371. wc_test_ret_t ret;
  35372. WC_RNG rng;
  35373. int i;
  35374. #ifdef WOLFSSL_SMALL_STACK
  35375. KyberKey *key = NULL;
  35376. byte *priv = NULL;
  35377. byte *pub = NULL;
  35378. byte *priv2 = NULL;
  35379. byte *pub2 = NULL;
  35380. byte *ct = NULL;
  35381. byte *ss = NULL;
  35382. byte *ss_dec = NULL;
  35383. #else
  35384. KyberKey key[1];
  35385. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  35386. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  35387. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  35388. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  35389. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  35390. byte ss[KYBER_SS_SZ];
  35391. byte ss_dec[KYBER_SS_SZ];
  35392. #endif
  35393. int key_inited = 0;
  35394. static const int testData[][4] = {
  35395. #ifdef WOLFSSL_KYBER512
  35396. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  35397. KYBER512_CIPHER_TEXT_SIZE },
  35398. #endif
  35399. #ifdef WOLFSSL_KYBER768
  35400. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  35401. KYBER768_CIPHER_TEXT_SIZE },
  35402. #endif
  35403. #ifdef WOLFSSL_KYBER1024
  35404. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  35405. KYBER1024_CIPHER_TEXT_SIZE },
  35406. #endif
  35407. };
  35408. WOLFSSL_ENTER("kyber_test");
  35409. #ifdef WOLFSSL_SMALL_STACK
  35410. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35411. DYNAMIC_TYPE_TMP_BUFFER);
  35412. priv = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35413. DYNAMIC_TYPE_TMP_BUFFER);
  35414. pub = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35415. DYNAMIC_TYPE_TMP_BUFFER);
  35416. priv2 = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35417. DYNAMIC_TYPE_TMP_BUFFER);
  35418. pub2 = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35419. DYNAMIC_TYPE_TMP_BUFFER);
  35420. ct = (byte *)XMALLOC(KYBER_MAX_CIPHER_TEXT_SIZE, HEAP_HINT,
  35421. DYNAMIC_TYPE_TMP_BUFFER);
  35422. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35423. DYNAMIC_TYPE_TMP_BUFFER);
  35424. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35425. DYNAMIC_TYPE_TMP_BUFFER);
  35426. if (! (key && priv && pub && priv2 && pub2 && ct && ss && ss_dec))
  35427. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35428. #endif
  35429. #ifndef HAVE_FIPS
  35430. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  35431. #else
  35432. ret = wc_InitRng(&rng);
  35433. #endif
  35434. if (ret != 0)
  35435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35436. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  35437. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35438. if (ret != 0)
  35439. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35440. else
  35441. key_inited = 1;
  35442. ret = wc_KyberKey_MakeKey(key, &rng);
  35443. if (ret != 0)
  35444. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35445. ret = wc_KyberKey_EncodePublicKey(key, pub, testData[i][2]);
  35446. if (ret != 0)
  35447. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35448. ret = wc_KyberKey_EncodePrivateKey(key, priv, testData[i][1]);
  35449. if (ret != 0)
  35450. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35451. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35452. if (ret != 0)
  35453. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35454. ret = wc_KyberKey_DecodePublicKey(key, pub, testData[i][2]);
  35455. if (ret != 0)
  35456. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35457. ret = wc_KyberKey_Encapsulate(key, ct, ss, &rng);
  35458. if (ret != 0)
  35459. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35460. ret = wc_KyberKey_EncodePublicKey(key, pub2, testData[i][2]);
  35461. if (ret != 0)
  35462. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35463. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  35464. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35465. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35466. if (ret != 0)
  35467. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35468. ret = wc_KyberKey_DecodePrivateKey(key, priv, testData[i][1]);
  35469. if (ret != 0)
  35470. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35471. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, testData[i][3]);
  35472. if (ret != 0)
  35473. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35474. if (XMEMCMP(ss, ss_dec, KYBER_SS_SZ) != 0)
  35475. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35476. ret = wc_KyberKey_EncodePrivateKey(key, priv2, testData[i][1]);
  35477. if (ret != 0)
  35478. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35479. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  35480. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35481. }
  35482. wc_FreeRng(&rng);
  35483. #ifdef WOLFSSL_WC_KYBER
  35484. #ifdef WOLFSSL_KYBER512
  35485. ret = kyber512_kat();
  35486. if (ret != 0)
  35487. goto out;
  35488. #endif
  35489. #ifdef WOLFSSL_KYBER768
  35490. ret = kyber768_kat();
  35491. if (ret != 0)
  35492. goto out;
  35493. #endif
  35494. #ifdef WOLFSSL_KYBER1024
  35495. ret = kyber1024_kat();
  35496. if (ret != 0)
  35497. goto out;
  35498. #endif
  35499. #endif /* WOLFSSL_WC_KYBER */
  35500. out:
  35501. if (key_inited)
  35502. wc_KyberKey_Free(key);
  35503. #ifdef WOLFSSL_SMALL_STACK
  35504. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35505. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35506. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35507. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35508. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35509. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35510. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35511. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35512. #endif
  35513. return ret;
  35514. }
  35515. #endif /* WOLFSSL_HAVE_KYBER */
  35516. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  35517. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  35518. void *context)
  35519. {
  35520. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  35521. * BE USED FOR TESTING PURPOSES! Production applications should
  35522. * write only to non-volatile storage. */
  35523. XMEMCPY(context, priv, privSz);
  35524. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  35525. }
  35526. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  35527. void *context)
  35528. {
  35529. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  35530. * BE USED FOR TESTING PURPOSES! */
  35531. XMEMCPY(priv, context, privSz);
  35532. return WC_XMSS_RC_READ_TO_MEMORY;
  35533. }
  35534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  35535. {
  35536. int i = 0;
  35537. int j = 0;
  35538. XmssKey signingKey;
  35539. XmssKey verifyKey;
  35540. WC_RNG rng;
  35541. word32 pkSz = 0;
  35542. word32 skSz = 0;
  35543. word32 sigSz = 0;
  35544. word32 bufSz = 0;
  35545. unsigned char * sk = NULL;
  35546. unsigned char * old_sk = NULL;
  35547. const char * msg = "XMSS post quantum signature test";
  35548. word32 msgSz = (word32) XSTRLEN(msg);
  35549. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10
  35550. const char * param = "XMSS-SHA2_10_256";
  35551. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 20
  35552. const char * param = "XMSSMT-SHA2_20/4_256";
  35553. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 40
  35554. const char * param = "XMSSMT-SHA2_40/8_256";
  35555. #else
  35556. const char * param = "XMSSMT-SHA2_60/12_256";
  35557. #endif
  35558. byte * sig = NULL;
  35559. int ret2 = -1;
  35560. int ret = WC_TEST_RET_ENC_NC;
  35561. WOLFSSL_ENTER("xmss_test");
  35562. #ifndef HAVE_FIPS
  35563. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  35564. #else
  35565. ret = wc_InitRng(&rng);
  35566. #endif
  35567. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35568. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  35569. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35570. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  35571. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35572. /* Set the parameter string to the signing key, and
  35573. * get sizes for secret key, pub key, and signature. */
  35574. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  35575. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35576. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  35577. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35578. if (pkSz != XMSS_SHA256_PUBLEN) {
  35579. return WC_TEST_RET_ENC_EC(pkSz);
  35580. }
  35581. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  35582. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35583. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  35584. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35585. /* Allocate signature array. */
  35586. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35587. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35588. bufSz = sigSz;
  35589. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  35590. fprintf(stderr, "param: %s\n", param);
  35591. fprintf(stderr, "pkSz: %d\n", pkSz);
  35592. fprintf(stderr, "skSz: %d\n", skSz);
  35593. fprintf(stderr, "sigSz: %d\n", sigSz);
  35594. #endif
  35595. /* Allocate current and old secret keys.*/
  35596. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35597. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35598. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35599. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35600. XMEMSET(sk, 0, skSz);
  35601. XMEMSET(old_sk, 0, skSz);
  35602. XMEMSET(sig, 0, sigSz);
  35603. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  35604. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35605. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  35606. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35607. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  35608. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35609. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  35610. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35611. /* Export the pub to a verify key. */
  35612. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  35613. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35614. /* Repeat a few times to check that:
  35615. * 1. The secret key is mutated on each sign.
  35616. * 2. We can verify each new signature.
  35617. * Only do a few times, because the full signature space
  35618. * for this parameter set is huge. */
  35619. for (i = 0; i < 10; ++i) {
  35620. XMEMCPY(old_sk, sk, skSz);
  35621. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  35622. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  35623. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  35624. /* Old secret key and current secret key should not match. */
  35625. ret = XMEMCMP(old_sk, sk, skSz);
  35626. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  35627. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  35628. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  35629. /* Flip bits in a few places throughout the signature, stepping in multiple
  35630. * of hash size. These should all fail with -1. */
  35631. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  35632. sig[j] ^= 1;
  35633. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  35634. msgSz);
  35635. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  35636. /* Verify passed when it should have failed. */
  35637. return WC_TEST_RET_ENC_I(j);
  35638. }
  35639. /* Flip this spot back. */
  35640. sig[j] ^= 1;
  35641. }
  35642. }
  35643. /* Cleanup everything. */
  35644. if (sig != NULL) {
  35645. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35646. sig = NULL;
  35647. }
  35648. if (sk != NULL) {
  35649. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35650. sk = NULL;
  35651. }
  35652. if (old_sk != NULL) {
  35653. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35654. old_sk = NULL;
  35655. }
  35656. wc_XmssKey_Free(&signingKey);
  35657. wc_FreeRng(&rng);
  35658. return ret;
  35659. }
  35660. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  35661. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_SMALL_STACK) && \
  35662. WOLFSSL_XMSS_MIN_HEIGHT <= 10
  35663. /* A simple xmss verify only test using:
  35664. * XMSS-SHA2_10_256
  35665. * pub len: 68
  35666. * msg len: 32
  35667. * sig len: 2500
  35668. *
  35669. * These were generated with the test xmss_fast, from the unpatched
  35670. * xmss-reference repository:
  35671. * https://github.com/XMSS/xmss-reference
  35672. * */
  35673. static const byte xmss_pub[XMSS_SHA256_PUBLEN] =
  35674. {
  35675. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  35676. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  35677. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  35678. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  35679. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  35680. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  35681. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  35682. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  35683. 0xC9,0xB7,0x39,0x4E
  35684. };
  35685. static /* not const */ byte xmss_msg[32] =
  35686. {
  35687. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  35688. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  35689. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  35690. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  35691. };
  35692. /* This was actually the 5th signature produced from
  35693. * xmss_fast test in xmss-reference. */
  35694. static /* not const */ byte xmss_sig[2500] =
  35695. {
  35696. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  35697. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  35698. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  35699. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  35700. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  35701. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  35702. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  35703. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  35704. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  35705. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  35706. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  35707. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  35708. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  35709. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  35710. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  35711. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  35712. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  35713. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  35714. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  35715. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  35716. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  35717. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  35718. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  35719. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  35720. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  35721. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  35722. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  35723. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  35724. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  35725. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  35726. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  35727. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  35728. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  35729. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  35730. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  35731. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  35732. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  35733. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  35734. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  35735. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  35736. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  35737. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  35738. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  35739. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  35740. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  35741. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  35742. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  35743. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  35744. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  35745. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  35746. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  35747. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  35748. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  35749. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  35750. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  35751. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  35752. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  35753. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  35754. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  35755. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  35756. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  35757. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  35758. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  35759. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  35760. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  35761. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  35762. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  35763. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  35764. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  35765. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  35766. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  35767. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  35768. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  35769. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  35770. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  35771. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  35772. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  35773. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  35774. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  35775. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  35776. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  35777. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  35778. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  35779. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  35780. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  35781. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  35782. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  35783. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  35784. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  35785. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  35786. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  35787. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  35788. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  35789. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  35790. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  35791. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  35792. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  35793. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  35794. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  35795. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  35796. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  35797. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  35798. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  35799. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  35800. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  35801. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  35802. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  35803. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  35804. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  35805. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  35806. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  35807. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  35808. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  35809. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  35810. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  35811. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  35812. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  35813. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  35814. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  35815. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  35816. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  35817. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  35818. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  35819. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  35820. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  35821. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  35822. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  35823. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  35824. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  35825. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  35826. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  35827. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  35828. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  35829. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  35830. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  35831. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  35832. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  35833. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  35834. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  35835. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  35836. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  35837. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  35838. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  35839. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  35840. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  35841. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  35842. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  35843. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  35844. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  35845. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  35846. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  35847. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  35848. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  35849. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  35850. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  35851. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  35852. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  35853. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  35854. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  35855. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  35856. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  35857. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  35858. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  35859. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  35860. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  35861. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  35862. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  35863. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  35864. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  35865. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  35866. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  35867. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  35868. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  35869. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  35870. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  35871. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  35872. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  35873. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  35874. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  35875. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  35876. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  35877. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  35878. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  35879. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  35880. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  35881. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  35882. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  35883. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  35884. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  35885. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  35886. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  35887. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  35888. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  35889. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  35890. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  35891. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  35892. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  35893. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  35894. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  35895. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  35896. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  35897. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  35898. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  35899. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  35900. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  35901. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  35902. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  35903. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  35904. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  35905. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  35906. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  35907. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  35908. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  35909. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  35910. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  35911. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  35912. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  35913. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  35914. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  35915. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  35916. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  35917. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  35918. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  35919. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  35920. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  35921. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  35922. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  35923. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  35924. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  35925. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  35926. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  35927. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  35928. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  35929. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  35930. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  35931. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  35932. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  35933. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  35934. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  35935. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  35936. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  35937. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  35938. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  35939. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  35940. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  35941. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  35942. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  35943. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  35944. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  35945. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  35946. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  35947. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  35948. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  35949. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  35950. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  35951. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  35952. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  35953. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  35954. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  35955. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  35956. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  35957. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  35958. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  35959. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  35960. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  35961. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  35962. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  35963. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  35964. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  35965. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  35966. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  35967. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  35968. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  35969. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  35970. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  35971. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  35972. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  35973. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  35974. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  35975. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  35976. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  35977. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  35978. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  35979. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  35980. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  35981. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  35982. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  35983. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  35984. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  35985. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  35986. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  35987. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  35988. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  35989. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  35990. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  35991. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  35992. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  35993. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  35994. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  35995. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  35996. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  35997. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  35998. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  35999. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  36000. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  36001. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  36002. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  36003. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  36004. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  36005. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  36006. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  36007. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  36008. 0xB3,0x5D,0x73,0x52
  36009. };
  36010. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  36011. {
  36012. XmssKey verifyKey;
  36013. unsigned char pub_raw[XMSS_SHA256_PUBLEN];
  36014. word32 pub_len = sizeof(pub_raw);
  36015. word32 pkSz = 0;
  36016. word32 sigSz = 0;
  36017. const char * param = "XMSS-SHA2_10_256";
  36018. int j = 0;
  36019. int ret2 = WC_TEST_RET_ENC_NC;
  36020. int ret = WC_TEST_RET_ENC_NC;
  36021. int n_diff = 0;
  36022. WOLFSSL_ENTER("xmss_test_verify_only");
  36023. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  36024. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36025. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36026. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  36027. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36028. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  36029. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36030. if (pkSz != XMSS_SHA256_PUBLEN) {
  36031. return WC_TEST_RET_ENC_EC(pkSz);
  36032. }
  36033. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  36034. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36035. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  36036. fprintf(stderr, "param: %s\n", param);
  36037. fprintf(stderr, "pkSz: %d\n", pkSz);
  36038. fprintf(stderr, "sigSz: %d\n", sigSz);
  36039. #endif
  36040. if (sigSz != sizeof(xmss_sig)) {
  36041. return WC_TEST_RET_ENC_EC(sigSz);
  36042. }
  36043. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  36044. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36045. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36046. (byte *) xmss_msg, sizeof(xmss_msg));
  36047. if (ret != 0) {
  36048. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  36049. return WC_TEST_RET_ENC_EC(ret);
  36050. }
  36051. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  36052. ret = wc_XmssKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  36053. if (ret != 0) {
  36054. printf("error: wc_XmssKey_ExportPubRaw returned %d, expected 0\n", ret);
  36055. return WC_TEST_RET_ENC_EC(ret);
  36056. }
  36057. if (pub_len != XMSS_SHA256_PUBLEN) {
  36058. printf("error: xmss pub len %u, expected %d\n", pub_len,
  36059. XMSS_SHA256_PUBLEN);
  36060. return WC_TEST_RET_ENC_EC(pub_len);
  36061. }
  36062. n_diff = XMEMCMP(pub_raw, xmss_pub, sizeof(xmss_pub));
  36063. if (n_diff != 0) {
  36064. printf("error: exported and imported pub raw do not match: %d\n",
  36065. n_diff);
  36066. return WC_TEST_RET_ENC_EC(n_diff);
  36067. }
  36068. /* Flip bits in message. This should fail. */
  36069. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  36070. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36071. (byte *) xmss_msg, sizeof(xmss_msg));
  36072. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36073. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  36074. return WC_TEST_RET_ENC_EC(ret2);
  36075. }
  36076. /* Flip it back. This should pass again. */
  36077. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  36078. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36079. (byte *) xmss_msg, sizeof(xmss_msg));
  36080. if (ret != 0) {
  36081. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  36082. return WC_TEST_RET_ENC_EC(ret);
  36083. }
  36084. /* Flip bits in a few places throughout the signature, stepping in multiple
  36085. * of hash size. These should all fail with -1. */
  36086. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  36087. xmss_sig[j] ^= 1;
  36088. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36089. (byte *) xmss_msg, sizeof(xmss_msg));
  36090. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36091. /* Verify passed when it should have failed. */
  36092. return WC_TEST_RET_ENC_I(j);
  36093. }
  36094. /* Flip this spot back. */
  36095. xmss_sig[j] ^= 1;
  36096. }
  36097. /* Cleanup everything. */
  36098. wc_XmssKey_Free(&verifyKey);
  36099. return ret;
  36100. }
  36101. #endif /* WOLFSSL_HAVE_XMSS && !WOLFSSL_SMALL_STACK &&
  36102. * WOLFSSL_XMSS_MIN_HEIGHT <= 10 */
  36103. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  36104. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  36105. {
  36106. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  36107. * BE USED FOR TESTING PURPOSES! Production applications should
  36108. * write only to non-volatile storage. */
  36109. XMEMCPY(context, priv, privSz);
  36110. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  36111. }
  36112. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  36113. {
  36114. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  36115. * BE USED FOR TESTING PURPOSES! */
  36116. XMEMCPY(priv, context, privSz);
  36117. return WC_LMS_RC_READ_TO_MEMORY;
  36118. }
  36119. /* LMS signature sizes are a function of their parameters. This
  36120. * test has a signature of 8688 bytes. */
  36121. #define WC_TEST_LMS_SIG_LEN (8688)
  36122. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  36123. {
  36124. int i = 0;
  36125. int j = 0;
  36126. int ret = WC_TEST_RET_ENC_NC;
  36127. int ret2 = WC_TEST_RET_ENC_NC;
  36128. int sigsLeft = 0;
  36129. LmsKey signingKey;
  36130. LmsKey verifyKey;
  36131. WC_RNG rng;
  36132. word32 sigSz = 0;
  36133. const char * msg = "LMS HSS post quantum signature test";
  36134. word32 msgSz = (word32) XSTRLEN(msg);
  36135. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  36136. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  36137. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36138. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  36139. DYNAMIC_TYPE_TMP_BUFFER);
  36140. if (sig == NULL) {
  36141. return WC_TEST_RET_ENC_ERRNO;
  36142. }
  36143. #else
  36144. byte sig[WC_TEST_LMS_SIG_LEN];
  36145. #endif
  36146. WOLFSSL_ENTER("lms_test");
  36147. XMEMSET(priv, 0, sizeof(priv));
  36148. XMEMSET(old_priv, 0, sizeof(old_priv));
  36149. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  36150. #ifndef HAVE_FIPS
  36151. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  36152. #else
  36153. ret = wc_InitRng(&rng);
  36154. #endif
  36155. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36156. /* This test:
  36157. * levels: 1
  36158. * height: 5
  36159. * winternitz: 1
  36160. *
  36161. * max sigs: 2 ** (1 * 5) = 32
  36162. * signature length: 8688
  36163. */
  36164. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  36165. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36166. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36167. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36168. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  36169. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36170. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  36171. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36172. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  36173. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36174. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  36175. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36176. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  36177. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36178. XMEMCPY(old_priv, priv, sizeof(priv));
  36179. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  36180. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36181. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  36182. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36183. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  36184. printf("error: got %u, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  36185. return WC_TEST_RET_ENC_EC(sigSz);
  36186. }
  36187. /* 2 ** 5 should be the max number of signatures */
  36188. for (i = 0; i < 32; ++i) {
  36189. /* We should have remaining signstures. */
  36190. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  36191. if (sigsLeft == 0) {
  36192. return WC_TEST_RET_ENC_EC(sigsLeft);
  36193. }
  36194. /* Sign with key. The private key will be updated on every signature. */
  36195. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  36196. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  36197. /* The updated private key should not match the old one. */
  36198. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  36199. printf("error: current priv key should not match old: %d\n", i);
  36200. return WC_TEST_RET_ENC_I(i);
  36201. }
  36202. XMEMCPY(old_priv, priv, sizeof(priv));
  36203. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  36204. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  36205. /* Flip bits in a few places throughout the signature, stepping in multiple
  36206. * of hash size. These should all fail with -1. */
  36207. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  36208. sig[j] ^= 1;
  36209. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  36210. msgSz);
  36211. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36212. /* Verify passed when it should have failed. */
  36213. return WC_TEST_RET_ENC_I(j);
  36214. }
  36215. /* Flip this spot back. */
  36216. sig[j] ^= 1;
  36217. }
  36218. }
  36219. /* This should be the last signature. */
  36220. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  36221. if (sigsLeft != 0) {
  36222. return WC_TEST_RET_ENC_EC(sigsLeft);
  36223. }
  36224. wc_LmsKey_Free(&signingKey);
  36225. wc_LmsKey_Free(&verifyKey);
  36226. wc_FreeRng(&rng);
  36227. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36228. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36229. #endif
  36230. return ret;
  36231. }
  36232. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  36233. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK)
  36234. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  36235. defined(HAVE_LIBLMS)
  36236. /* A simple LMS verify only test.
  36237. *
  36238. * Note: LMS signature sizes are a function of their parameters. This
  36239. * test has a signature of 1456 bytes:
  36240. * levels: 1
  36241. * height: 10
  36242. * winternitz: 8
  36243. * max sigs: 2 ** (1 * 10) = 1024
  36244. * signature length: 1456
  36245. * */
  36246. /* "wolfSSL LMS example message!" without null terminator. */
  36247. static byte lms_msg[28] =
  36248. {
  36249. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  36250. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  36251. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  36252. 0x61,0x67,0x65,0x21
  36253. };
  36254. static const byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  36255. {
  36256. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  36257. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  36258. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  36259. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  36260. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  36261. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  36262. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  36263. 0xC5,0xB4,0x8F,0x23
  36264. };
  36265. #define LMS_L1H10W8_SIGLEN (1456)
  36266. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  36267. {
  36268. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  36269. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  36270. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  36271. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  36272. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  36273. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  36274. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  36275. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  36276. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  36277. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  36278. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  36279. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  36280. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  36281. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  36282. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  36283. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  36284. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  36285. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  36286. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  36287. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  36288. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  36289. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  36290. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  36291. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  36292. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  36293. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  36294. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  36295. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  36296. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  36297. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  36298. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  36299. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  36300. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  36301. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  36302. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  36303. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  36304. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  36305. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  36306. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  36307. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  36308. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  36309. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  36310. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  36311. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  36312. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  36313. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  36314. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  36315. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  36316. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  36317. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  36318. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  36319. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  36320. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  36321. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  36322. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  36323. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  36324. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  36325. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  36326. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  36327. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  36328. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  36329. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  36330. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  36331. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  36332. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  36333. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  36334. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  36335. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  36336. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  36337. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  36338. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  36339. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  36340. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  36341. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  36342. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  36343. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  36344. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  36345. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  36346. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  36347. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  36348. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  36349. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  36350. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  36351. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  36352. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  36353. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  36354. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  36355. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  36356. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  36357. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  36358. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  36359. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  36360. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  36361. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  36362. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  36363. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  36364. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  36365. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  36366. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  36367. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  36368. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  36369. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  36370. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  36371. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  36372. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  36373. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  36374. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  36375. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  36376. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  36377. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  36378. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  36379. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  36380. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  36381. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  36382. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  36383. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  36384. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  36385. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  36386. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  36387. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  36388. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  36389. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  36390. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  36391. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  36392. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  36393. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  36394. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  36395. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  36396. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  36397. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  36398. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  36399. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  36400. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  36401. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  36402. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  36403. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  36404. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  36405. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  36406. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  36407. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  36408. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  36409. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  36410. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  36411. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  36412. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  36413. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  36414. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  36415. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  36416. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  36417. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  36418. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  36419. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  36420. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  36421. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  36422. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  36423. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  36424. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  36425. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  36426. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  36427. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  36428. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  36429. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  36430. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  36431. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  36432. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  36433. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  36434. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  36435. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  36436. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  36437. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  36438. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  36439. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  36440. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  36441. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  36442. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  36443. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  36444. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  36445. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  36446. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  36447. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  36448. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  36449. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  36450. };
  36451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  36452. {
  36453. LmsKey verifyKey;
  36454. unsigned char pub_raw[HSS_MAX_PUBLIC_KEY_LEN];
  36455. word32 pub_len = sizeof(pub_raw);
  36456. word32 sigSz = 0;
  36457. word32 msgSz = sizeof(lms_msg);
  36458. word32 pubSz = 0;
  36459. int levels = 0;
  36460. int height = 0;
  36461. int winternitz = 0;
  36462. int ret = WC_TEST_RET_ENC_NC;
  36463. int ret2 = WC_TEST_RET_ENC_NC;
  36464. int j = 0;
  36465. int n_diff = 0;
  36466. WOLFSSL_ENTER("lms_test_verify_only");
  36467. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  36468. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36469. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36470. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  36471. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36472. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  36473. HSS_MAX_PUBLIC_KEY_LEN);
  36474. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36475. /* Verify parameters, pub length, and sig length are correct. */
  36476. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  36477. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36478. if (levels != 1 || height != 10 || winternitz != 8) {
  36479. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  36480. winternitz);
  36481. return WC_TEST_RET_ENC_NC;
  36482. }
  36483. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubSz);
  36484. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36485. if (pubSz != HSS_MAX_PUBLIC_KEY_LEN) {
  36486. printf("error: got %u, expected %d\n", pubSz, HSS_MAX_PUBLIC_KEY_LEN);
  36487. return WC_TEST_RET_ENC_EC(pubSz);
  36488. }
  36489. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  36490. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36491. if (sigSz != LMS_L1H10W8_SIGLEN) {
  36492. printf("error: got %u, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  36493. return WC_TEST_RET_ENC_EC(sigSz);
  36494. }
  36495. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36496. (byte *) lms_msg, msgSz);
  36497. if (ret != 0) {
  36498. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  36499. return WC_TEST_RET_ENC_EC(ret);
  36500. }
  36501. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  36502. ret = wc_LmsKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  36503. if (ret != 0) {
  36504. printf("error: wc_LmsKey_ExportPubRaw returned %d, expected 0\n", ret);
  36505. return WC_TEST_RET_ENC_EC(ret);
  36506. }
  36507. if (pub_len != HSS_MAX_PUBLIC_KEY_LEN) {
  36508. printf("error: LMS pub len %u, expected %d\n", pub_len,
  36509. HSS_MAX_PUBLIC_KEY_LEN);
  36510. return WC_TEST_RET_ENC_EC(pub_len);
  36511. }
  36512. n_diff = XMEMCMP(pub_raw, lms_L1H10W8_pub, sizeof(lms_L1H10W8_pub));
  36513. if (n_diff != 0) {
  36514. printf("error: exported and imported pub raw do not match: %d\n",
  36515. n_diff);
  36516. return WC_TEST_RET_ENC_EC(n_diff);
  36517. }
  36518. /* Flip bits in message. This should fail. */
  36519. lms_msg[msgSz / 2] ^= 1;
  36520. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36521. (byte *) lms_msg, msgSz);
  36522. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36523. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  36524. return WC_TEST_RET_ENC_EC(ret);
  36525. }
  36526. /* Flip it back. This should pass again. */
  36527. lms_msg[msgSz / 2] ^= 1;
  36528. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36529. (byte *) lms_msg, msgSz);
  36530. if (ret != 0) {
  36531. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  36532. return WC_TEST_RET_ENC_EC(ret);
  36533. }
  36534. /* Flip bits in a few places throughout the signature, stepping in multiple
  36535. * of hash size. These should all fail with -1. */
  36536. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  36537. lms_L1H10W8_sig[j] ^= 1;
  36538. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  36539. LMS_L1H10W8_SIGLEN,
  36540. (byte *) lms_msg, msgSz);
  36541. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36542. /* Verify passed when it should have failed. */
  36543. return WC_TEST_RET_ENC_I(j);
  36544. }
  36545. /* Flip this spot back. */
  36546. lms_L1H10W8_sig[j] ^= 1;
  36547. }
  36548. wc_LmsKey_Free(&verifyKey);
  36549. return ret;
  36550. }
  36551. #endif
  36552. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK) */
  36553. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  36554. * see print_fiducials() below.
  36555. */
  36556. #ifdef WOLFCRYPT_HAVE_ECCSI
  36557. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  36558. ecc_point* pvt)
  36559. {
  36560. wc_test_ret_t ret;
  36561. byte id[1] = { 0x00 };
  36562. int valid;
  36563. word32 sz;
  36564. byte data[256];
  36565. byte hash[WC_MAX_DIGEST_SIZE];
  36566. byte hashSz;
  36567. byte sig[257];
  36568. word32 sigSz;
  36569. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  36570. if (ret != BAD_FUNC_ARG)
  36571. return WC_TEST_RET_ENC_EC(ret);
  36572. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  36573. if (ret != BAD_FUNC_ARG)
  36574. return WC_TEST_RET_ENC_EC(ret);
  36575. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  36576. if (ret != BAD_FUNC_ARG)
  36577. return WC_TEST_RET_ENC_EC(ret);
  36578. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  36579. if (ret != BAD_FUNC_ARG)
  36580. return WC_TEST_RET_ENC_EC(ret);
  36581. wc_FreeEccsiKey(NULL);
  36582. /* Create a valid key. */
  36583. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  36584. if (ret != 0)
  36585. return WC_TEST_RET_ENC_EC(ret);
  36586. ret = wc_MakeEccsiKey(NULL, NULL);
  36587. if (ret != BAD_FUNC_ARG)
  36588. return WC_TEST_RET_ENC_EC(ret);
  36589. ret = wc_MakeEccsiKey(key, NULL);
  36590. if (ret != BAD_FUNC_ARG)
  36591. return WC_TEST_RET_ENC_EC(ret);
  36592. ret = wc_MakeEccsiKey(NULL, rng);
  36593. if (ret != BAD_FUNC_ARG)
  36594. return WC_TEST_RET_ENC_EC(ret);
  36595. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  36596. NULL);
  36597. if (ret != BAD_FUNC_ARG)
  36598. return WC_TEST_RET_ENC_EC(ret);
  36599. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  36600. if (ret != BAD_FUNC_ARG)
  36601. return WC_TEST_RET_ENC_EC(ret);
  36602. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  36603. if (ret != BAD_FUNC_ARG)
  36604. return WC_TEST_RET_ENC_EC(ret);
  36605. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  36606. if (ret != BAD_FUNC_ARG)
  36607. return WC_TEST_RET_ENC_EC(ret);
  36608. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36609. if (ret != BAD_FUNC_ARG)
  36610. return WC_TEST_RET_ENC_EC(ret);
  36611. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36612. if (ret != BAD_FUNC_ARG)
  36613. return WC_TEST_RET_ENC_EC(ret);
  36614. /* No key set */
  36615. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36616. if (ret != BAD_STATE_E)
  36617. return WC_TEST_RET_ENC_EC(ret);
  36618. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  36619. NULL);
  36620. if (ret != BAD_FUNC_ARG)
  36621. return WC_TEST_RET_ENC_EC(ret);
  36622. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36623. NULL);
  36624. if (ret != BAD_FUNC_ARG)
  36625. return WC_TEST_RET_ENC_EC(ret);
  36626. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  36627. &valid);
  36628. if (ret != BAD_FUNC_ARG)
  36629. return WC_TEST_RET_ENC_EC(ret);
  36630. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  36631. &valid);
  36632. if (ret != BAD_FUNC_ARG)
  36633. return WC_TEST_RET_ENC_EC(ret);
  36634. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  36635. &valid);
  36636. if (ret != BAD_FUNC_ARG)
  36637. return WC_TEST_RET_ENC_EC(ret);
  36638. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36639. &valid);
  36640. if (ret != BAD_FUNC_ARG)
  36641. return WC_TEST_RET_ENC_EC(ret);
  36642. /* No key set */
  36643. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36644. &valid);
  36645. if (ret != BAD_STATE_E)
  36646. return WC_TEST_RET_ENC_EC(ret);
  36647. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  36648. if (ret != BAD_FUNC_ARG)
  36649. return WC_TEST_RET_ENC_EC(ret);
  36650. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  36651. if (ret != BAD_FUNC_ARG)
  36652. return WC_TEST_RET_ENC_EC(ret);
  36653. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  36654. if (ret != BAD_FUNC_ARG)
  36655. return WC_TEST_RET_ENC_EC(ret);
  36656. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  36657. if (ret != BAD_FUNC_ARG)
  36658. return WC_TEST_RET_ENC_EC(ret);
  36659. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  36660. if (ret != BAD_FUNC_ARG)
  36661. return WC_TEST_RET_ENC_EC(ret);
  36662. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  36663. if (ret != BAD_FUNC_ARG)
  36664. return WC_TEST_RET_ENC_EC(ret);
  36665. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  36666. if (ret != BAD_FUNC_ARG)
  36667. return WC_TEST_RET_ENC_EC(ret);
  36668. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  36669. if (ret != BAD_FUNC_ARG)
  36670. return WC_TEST_RET_ENC_EC(ret);
  36671. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  36672. if (ret != BAD_FUNC_ARG)
  36673. return WC_TEST_RET_ENC_EC(ret);
  36674. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  36675. if (ret != BAD_FUNC_ARG)
  36676. return WC_TEST_RET_ENC_EC(ret);
  36677. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  36678. if (ret != BAD_FUNC_ARG)
  36679. return WC_TEST_RET_ENC_EC(ret);
  36680. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  36681. if (ret != BAD_FUNC_ARG)
  36682. return WC_TEST_RET_ENC_EC(ret);
  36683. /* No key created so no curve information. */
  36684. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  36685. if (ret != LENGTH_ONLY_E)
  36686. return WC_TEST_RET_ENC_EC(ret);
  36687. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  36688. if (ret != BAD_FUNC_ARG)
  36689. return WC_TEST_RET_ENC_EC(ret);
  36690. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  36691. if (ret != BAD_FUNC_ARG)
  36692. return WC_TEST_RET_ENC_EC(ret);
  36693. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  36694. if (ret != BAD_FUNC_ARG)
  36695. return WC_TEST_RET_ENC_EC(ret);
  36696. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  36697. if (ret != BAD_FUNC_ARG)
  36698. return WC_TEST_RET_ENC_EC(ret);
  36699. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  36700. if (ret != BAD_FUNC_ARG)
  36701. return WC_TEST_RET_ENC_EC(ret);
  36702. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  36703. if (ret != BAD_FUNC_ARG)
  36704. return WC_TEST_RET_ENC_EC(ret);
  36705. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  36706. if (ret != BAD_FUNC_ARG)
  36707. return WC_TEST_RET_ENC_EC(ret);
  36708. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  36709. if (ret != BAD_FUNC_ARG)
  36710. return WC_TEST_RET_ENC_EC(ret);
  36711. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  36712. if (ret != BAD_FUNC_ARG)
  36713. return WC_TEST_RET_ENC_EC(ret);
  36714. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  36715. if (ret != BAD_FUNC_ARG)
  36716. return WC_TEST_RET_ENC_EC(ret);
  36717. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  36718. if (ret != BAD_FUNC_ARG)
  36719. return WC_TEST_RET_ENC_EC(ret);
  36720. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  36721. if (ret != BAD_FUNC_ARG)
  36722. return WC_TEST_RET_ENC_EC(ret);
  36723. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  36724. if (ret != BAD_FUNC_ARG)
  36725. return WC_TEST_RET_ENC_EC(ret);
  36726. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  36727. if (ret != BAD_FUNC_ARG)
  36728. return WC_TEST_RET_ENC_EC(ret);
  36729. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  36730. if (ret != BAD_FUNC_ARG)
  36731. return WC_TEST_RET_ENC_EC(ret);
  36732. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  36733. if (ret != BAD_FUNC_ARG)
  36734. return WC_TEST_RET_ENC_EC(ret);
  36735. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  36736. if (ret != BAD_FUNC_ARG)
  36737. return WC_TEST_RET_ENC_EC(ret);
  36738. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  36739. if (ret != BAD_FUNC_ARG)
  36740. return WC_TEST_RET_ENC_EC(ret);
  36741. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  36742. if (ret != BAD_FUNC_ARG)
  36743. return WC_TEST_RET_ENC_EC(ret);
  36744. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  36745. if (ret != BAD_FUNC_ARG)
  36746. return WC_TEST_RET_ENC_EC(ret);
  36747. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  36748. if (ret != BAD_FUNC_ARG)
  36749. return WC_TEST_RET_ENC_EC(ret);
  36750. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  36751. if (ret != BAD_FUNC_ARG)
  36752. return WC_TEST_RET_ENC_EC(ret);
  36753. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  36754. if (ret != BAD_FUNC_ARG)
  36755. return WC_TEST_RET_ENC_EC(ret);
  36756. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  36757. if (ret != BAD_FUNC_ARG)
  36758. return WC_TEST_RET_ENC_EC(ret);
  36759. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  36760. if (ret != BAD_FUNC_ARG)
  36761. return WC_TEST_RET_ENC_EC(ret);
  36762. ret = wc_ExportEccsiKey(NULL, data, NULL);
  36763. if (ret != BAD_FUNC_ARG)
  36764. return WC_TEST_RET_ENC_EC(ret);
  36765. ret = wc_ExportEccsiKey(key, data, NULL);
  36766. if (ret != BAD_FUNC_ARG)
  36767. return WC_TEST_RET_ENC_EC(ret);
  36768. ret = wc_ExportEccsiKey(NULL, data, &sz);
  36769. if (ret != BAD_FUNC_ARG)
  36770. return WC_TEST_RET_ENC_EC(ret);
  36771. /* No key to export */
  36772. ret = wc_ExportEccsiKey(key, NULL, &sz);
  36773. if (ret != BAD_STATE_E)
  36774. return WC_TEST_RET_ENC_EC(ret);
  36775. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  36776. if (ret != BAD_FUNC_ARG)
  36777. return WC_TEST_RET_ENC_EC(ret);
  36778. ret = wc_ImportEccsiKey(key, NULL, 0);
  36779. if (ret != BAD_FUNC_ARG)
  36780. return WC_TEST_RET_ENC_EC(ret);
  36781. ret = wc_ImportEccsiKey(NULL, data, 0);
  36782. if (ret != BAD_FUNC_ARG)
  36783. return WC_TEST_RET_ENC_EC(ret);
  36784. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  36785. if (ret != BAD_FUNC_ARG)
  36786. return WC_TEST_RET_ENC_EC(ret);
  36787. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  36788. if (ret != BAD_FUNC_ARG)
  36789. return WC_TEST_RET_ENC_EC(ret);
  36790. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  36791. if (ret != BAD_FUNC_ARG)
  36792. return WC_TEST_RET_ENC_EC(ret);
  36793. /* No key to export */
  36794. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  36795. if (ret != BAD_STATE_E)
  36796. return WC_TEST_RET_ENC_EC(ret);
  36797. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  36798. if (ret != BAD_FUNC_ARG)
  36799. return WC_TEST_RET_ENC_EC(ret);
  36800. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  36801. if (ret != BAD_FUNC_ARG)
  36802. return WC_TEST_RET_ENC_EC(ret);
  36803. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  36804. if (ret != BAD_FUNC_ARG)
  36805. return WC_TEST_RET_ENC_EC(ret);
  36806. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  36807. if (ret != BAD_FUNC_ARG)
  36808. return WC_TEST_RET_ENC_EC(ret);
  36809. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  36810. if (ret != BAD_FUNC_ARG)
  36811. return WC_TEST_RET_ENC_EC(ret);
  36812. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  36813. if (ret != BAD_FUNC_ARG)
  36814. return WC_TEST_RET_ENC_EC(ret);
  36815. /* No key to export */
  36816. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  36817. if (ret != BAD_STATE_E)
  36818. return WC_TEST_RET_ENC_EC(ret);
  36819. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  36820. if (ret != BAD_FUNC_ARG)
  36821. return WC_TEST_RET_ENC_EC(ret);
  36822. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  36823. if (ret != BAD_FUNC_ARG)
  36824. return WC_TEST_RET_ENC_EC(ret);
  36825. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  36826. if (ret != BAD_FUNC_ARG)
  36827. return WC_TEST_RET_ENC_EC(ret);
  36828. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  36829. if (ret != BAD_FUNC_ARG)
  36830. return WC_TEST_RET_ENC_EC(ret);
  36831. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  36832. if (ret != BAD_FUNC_ARG)
  36833. return WC_TEST_RET_ENC_EC(ret);
  36834. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  36835. if (ret != BAD_FUNC_ARG)
  36836. return WC_TEST_RET_ENC_EC(ret);
  36837. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  36838. if (ret != BAD_FUNC_ARG)
  36839. return WC_TEST_RET_ENC_EC(ret);
  36840. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  36841. &hashSz);
  36842. if (ret != BAD_FUNC_ARG)
  36843. return WC_TEST_RET_ENC_EC(ret);
  36844. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  36845. if (ret != BAD_FUNC_ARG)
  36846. return WC_TEST_RET_ENC_EC(ret);
  36847. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  36848. if (ret != BAD_STATE_E)
  36849. return WC_TEST_RET_ENC_EC(ret);
  36850. ret = wc_SetEccsiHash(NULL, NULL, 1);
  36851. if (ret != BAD_FUNC_ARG)
  36852. return WC_TEST_RET_ENC_EC(ret);
  36853. ret = wc_SetEccsiHash(key, NULL, 1);
  36854. if (ret != BAD_FUNC_ARG)
  36855. return WC_TEST_RET_ENC_EC(ret);
  36856. ret = wc_SetEccsiHash(NULL, hash, 1);
  36857. if (ret != BAD_FUNC_ARG)
  36858. return WC_TEST_RET_ENC_EC(ret);
  36859. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  36860. if (ret != BAD_FUNC_ARG)
  36861. return WC_TEST_RET_ENC_EC(ret);
  36862. ret = wc_SetEccsiPair(key, NULL, NULL);
  36863. if (ret != BAD_FUNC_ARG)
  36864. return WC_TEST_RET_ENC_EC(ret);
  36865. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  36866. if (ret != BAD_FUNC_ARG)
  36867. return WC_TEST_RET_ENC_EC(ret);
  36868. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  36869. if (ret != BAD_FUNC_ARG)
  36870. return WC_TEST_RET_ENC_EC(ret);
  36871. ret = wc_SetEccsiPair(key, ssk, NULL);
  36872. if (ret != BAD_FUNC_ARG)
  36873. return WC_TEST_RET_ENC_EC(ret);
  36874. ret = wc_SetEccsiPair(key, NULL, pvt);
  36875. if (ret != BAD_FUNC_ARG)
  36876. return WC_TEST_RET_ENC_EC(ret);
  36877. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  36878. if (ret != BAD_FUNC_ARG)
  36879. return WC_TEST_RET_ENC_EC(ret);
  36880. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  36881. if (ret != BAD_FUNC_ARG)
  36882. return WC_TEST_RET_ENC_EC(ret);
  36883. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  36884. if (ret != BAD_FUNC_ARG)
  36885. return WC_TEST_RET_ENC_EC(ret);
  36886. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  36887. &sigSz);
  36888. if (ret != BAD_FUNC_ARG)
  36889. return WC_TEST_RET_ENC_EC(ret);
  36890. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  36891. &sigSz);
  36892. if (ret != BAD_FUNC_ARG)
  36893. return WC_TEST_RET_ENC_EC(ret);
  36894. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  36895. &sigSz);
  36896. if (ret != BAD_FUNC_ARG)
  36897. return WC_TEST_RET_ENC_EC(ret);
  36898. /* Key not set. */
  36899. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  36900. &sigSz);
  36901. if (ret != BAD_STATE_E)
  36902. return WC_TEST_RET_ENC_EC(ret);
  36903. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  36904. if (ret != BAD_FUNC_ARG)
  36905. return WC_TEST_RET_ENC_EC(ret);
  36906. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  36907. if (ret != BAD_FUNC_ARG)
  36908. return WC_TEST_RET_ENC_EC(ret);
  36909. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  36910. if (ret != BAD_FUNC_ARG)
  36911. return WC_TEST_RET_ENC_EC(ret);
  36912. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  36913. if (ret != BAD_FUNC_ARG)
  36914. return WC_TEST_RET_ENC_EC(ret);
  36915. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  36916. &valid);
  36917. if (ret != BAD_FUNC_ARG)
  36918. return WC_TEST_RET_ENC_EC(ret);
  36919. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  36920. if (ret != BAD_FUNC_ARG)
  36921. return WC_TEST_RET_ENC_EC(ret);
  36922. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  36923. &valid);
  36924. if (ret != BAD_FUNC_ARG)
  36925. return WC_TEST_RET_ENC_EC(ret);
  36926. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  36927. &valid);
  36928. if (ret != BAD_FUNC_ARG)
  36929. return WC_TEST_RET_ENC_EC(ret);
  36930. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  36931. &valid);
  36932. if (ret != BAD_FUNC_ARG)
  36933. return WC_TEST_RET_ENC_EC(ret);
  36934. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  36935. &valid);
  36936. if (ret != BAD_STATE_E)
  36937. return WC_TEST_RET_ENC_EC(ret);
  36938. ret = wc_SetEccsiPair(key, ssk, pvt);
  36939. if (ret != 0)
  36940. return WC_TEST_RET_ENC_EC(ret);
  36941. /* Identity hash not set. */
  36942. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  36943. &sigSz);
  36944. if (ret != BAD_STATE_E)
  36945. return WC_TEST_RET_ENC_EC(ret);
  36946. wc_FreeEccsiKey(key);
  36947. return 0;
  36948. }
  36949. /* RFC 6507: Appendix A */
  36950. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  36951. {
  36952. wc_test_ret_t ret;
  36953. int verified;
  36954. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  36955. word32 msgSz = sizeof(msg);
  36956. byte hash[WC_SHA256_DIGEST_SIZE];
  36957. byte hashSz = WC_SHA256_DIGEST_SIZE;
  36958. static const byte id[] = {
  36959. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  36960. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  36961. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  36962. 0x33, 0x00
  36963. };
  36964. word32 idSz = sizeof(id);
  36965. static const byte sig[] = {
  36966. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  36967. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  36968. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  36969. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  36970. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  36971. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  36972. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  36973. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  36974. 0x04,
  36975. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  36976. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  36977. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  36978. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  36979. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  36980. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  36981. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  36982. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  36983. };
  36984. word32 sigSz = sizeof(sig);
  36985. static const byte pubData[] = {
  36986. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  36987. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  36988. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  36989. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  36990. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  36991. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  36992. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  36993. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  36994. };
  36995. static const byte expHash[] = {
  36996. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  36997. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  36998. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  36999. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  37000. };
  37001. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  37002. if (ret != 0)
  37003. return WC_TEST_RET_ENC_EC(ret);
  37004. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  37005. if (ret != 0)
  37006. return WC_TEST_RET_ENC_EC(ret);
  37007. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  37008. &hashSz);
  37009. if (ret != 0)
  37010. return WC_TEST_RET_ENC_EC(ret);
  37011. if (hashSz != sizeof(expHash))
  37012. return WC_TEST_RET_ENC_NC;
  37013. if (XMEMCMP(hash, expHash, hashSz) != 0)
  37014. return WC_TEST_RET_ENC_NC;
  37015. ret = wc_SetEccsiHash(key, hash, hashSz);
  37016. if (ret != 0)
  37017. return WC_TEST_RET_ENC_EC(ret);
  37018. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37019. &verified);
  37020. if (ret != 0)
  37021. return WC_TEST_RET_ENC_EC(ret);
  37022. if (!verified)
  37023. return WC_TEST_RET_ENC_NC;
  37024. return 0;
  37025. }
  37026. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  37027. {
  37028. wc_test_ret_t ret;
  37029. byte data[32 * 3];
  37030. word32 sz;
  37031. ecc_point* decPvt = NULL;
  37032. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37033. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  37034. DYNAMIC_TYPE_TMP_BUFFER);
  37035. if (decSsk == NULL)
  37036. return WC_TEST_RET_ENC_ERRNO;
  37037. #else
  37038. mp_int decSsk[1];
  37039. #endif
  37040. ret = mp_init(decSsk);
  37041. if (ret != 0)
  37042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37043. decPvt = wc_ecc_new_point();
  37044. if (decPvt == NULL)
  37045. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37046. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  37047. if (ret != LENGTH_ONLY_E)
  37048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37049. if (sz != 32 * 3)
  37050. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37051. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  37052. if (ret != 0)
  37053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37054. if (sz != 32* 3)
  37055. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37056. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  37057. if (ret != 0)
  37058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37059. ret = mp_cmp(ssk, decSsk);
  37060. if (ret != MP_EQ)
  37061. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37062. ret = wc_ecc_cmp_point(pvt, decPvt);
  37063. if (ret != MP_EQ)
  37064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37065. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  37066. if (ret != LENGTH_ONLY_E)
  37067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37068. if (sz != 32)
  37069. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37070. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  37071. if (ret != 0)
  37072. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37073. if (sz != 32)
  37074. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37075. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  37076. if (ret != 0)
  37077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37078. ret = mp_cmp(ssk, decSsk);
  37079. if (ret != MP_EQ)
  37080. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37081. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  37082. if (ret != LENGTH_ONLY_E)
  37083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37084. if (sz != 32 * 2)
  37085. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37086. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  37087. if (ret != 0)
  37088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37089. if (sz != 32 * 2)
  37090. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37091. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  37092. if (ret != 0)
  37093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37094. ret = wc_ecc_cmp_point(pvt, decPvt);
  37095. if (ret != MP_EQ)
  37096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37097. sz = sizeof(data);
  37098. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  37099. if (ret != 0)
  37100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37101. if (sz != 32 * 2 + 1)
  37102. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37103. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  37104. if (ret != 0)
  37105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37106. ret = wc_ecc_cmp_point(pvt, decPvt);
  37107. if (ret != MP_EQ)
  37108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37109. wc_ecc_del_point(decPvt);
  37110. out:
  37111. mp_free(decSsk);
  37112. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37113. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37114. #endif
  37115. return ret;
  37116. }
  37117. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  37118. {
  37119. wc_test_ret_t ret;
  37120. byte data[32 * 3];
  37121. byte out[32 * 3];
  37122. word32 sz;
  37123. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  37124. if (ret != LENGTH_ONLY_E)
  37125. return WC_TEST_RET_ENC_EC(ret);
  37126. if (sz != 32 * 3)
  37127. return WC_TEST_RET_ENC_NC;
  37128. ret = wc_ExportEccsiKey(priv, data, &sz);
  37129. if (ret != 0)
  37130. return WC_TEST_RET_ENC_EC(ret);
  37131. ret = wc_ImportEccsiKey(priv, data, sz);
  37132. if (ret != 0)
  37133. return WC_TEST_RET_ENC_EC(ret);
  37134. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  37135. if (ret != LENGTH_ONLY_E)
  37136. return WC_TEST_RET_ENC_EC(ret);
  37137. if (sz != 32 * 3)
  37138. return WC_TEST_RET_ENC_NC;
  37139. ret = wc_ExportEccsiKey(priv, out, &sz);
  37140. if (ret != 0)
  37141. return WC_TEST_RET_ENC_EC(ret);
  37142. if (sz != 32 * 3)
  37143. return WC_TEST_RET_ENC_NC;
  37144. if (XMEMCMP(data, out, sz) != 0)
  37145. return WC_TEST_RET_ENC_NC;
  37146. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  37147. if (ret != LENGTH_ONLY_E)
  37148. return WC_TEST_RET_ENC_EC(ret);
  37149. if (sz != 32)
  37150. return WC_TEST_RET_ENC_NC;
  37151. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  37152. if (ret != 0)
  37153. return WC_TEST_RET_ENC_EC(ret);
  37154. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  37155. if (ret != 0)
  37156. return WC_TEST_RET_ENC_EC(ret);
  37157. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  37158. if (ret != LENGTH_ONLY_E)
  37159. return WC_TEST_RET_ENC_EC(ret);
  37160. if (sz != 32)
  37161. return WC_TEST_RET_ENC_NC;
  37162. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  37163. if (ret != 0)
  37164. return WC_TEST_RET_ENC_EC(ret);
  37165. if (sz != 32)
  37166. return WC_TEST_RET_ENC_NC;
  37167. if (XMEMCMP(data, out, sz) != 0)
  37168. return WC_TEST_RET_ENC_NC;
  37169. return 0;
  37170. }
  37171. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  37172. {
  37173. wc_test_ret_t ret;
  37174. byte data[32 * 2 + 1];
  37175. byte pubData[32 * 2 + 1];
  37176. word32 sz;
  37177. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  37178. if (ret != LENGTH_ONLY_E)
  37179. return WC_TEST_RET_ENC_EC(ret);
  37180. if (sz != 32 * 2)
  37181. return WC_TEST_RET_ENC_NC;
  37182. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  37183. if (ret != 0)
  37184. return WC_TEST_RET_ENC_EC(ret);
  37185. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  37186. if (ret != 0)
  37187. return WC_TEST_RET_ENC_EC(ret);
  37188. sz = sizeof(pubData);
  37189. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  37190. if (ret != 0)
  37191. return WC_TEST_RET_ENC_EC(ret);
  37192. if (sz != 32 * 2)
  37193. return WC_TEST_RET_ENC_NC;
  37194. if (XMEMCMP(data, pubData, sz) != 0)
  37195. return WC_TEST_RET_ENC_NC;
  37196. sz = sizeof(pubData);
  37197. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  37198. if (ret != 0)
  37199. return WC_TEST_RET_ENC_EC(ret);
  37200. if (sz != 32 * 2 + 1)
  37201. return WC_TEST_RET_ENC_NC;
  37202. if (pubData[0] != 0x04)
  37203. return WC_TEST_RET_ENC_NC;
  37204. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  37205. return WC_TEST_RET_ENC_NC;
  37206. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  37207. if (ret != 0)
  37208. return WC_TEST_RET_ENC_EC(ret);
  37209. return 0;
  37210. }
  37211. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  37212. mp_int* ssk, ecc_point* pvt)
  37213. {
  37214. wc_test_ret_t ret;
  37215. char mail[] = "test@wolfssl.com";
  37216. byte* id = (byte*)mail;
  37217. word32 idSz = (word32) XSTRLEN(mail);
  37218. int valid;
  37219. ret = wc_MakeEccsiKey(priv, rng);
  37220. if (ret != 0)
  37221. return WC_TEST_RET_ENC_EC(ret);
  37222. ret = eccsi_imp_exp_key_test(priv);
  37223. if (ret < 0)
  37224. return ret;
  37225. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  37226. if (ret < 0)
  37227. return ret;
  37228. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  37229. if (ret != 0)
  37230. return WC_TEST_RET_ENC_EC(ret);
  37231. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  37232. &valid);
  37233. if (ret != 0)
  37234. return WC_TEST_RET_ENC_EC(ret);
  37235. if (!valid)
  37236. return WC_TEST_RET_ENC_NC;
  37237. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  37238. if (ret != 0)
  37239. return ret;
  37240. return 0;
  37241. }
  37242. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  37243. mp_int* ssk, ecc_point* pvt)
  37244. {
  37245. wc_test_ret_t ret;
  37246. byte hashPriv[WC_MAX_DIGEST_SIZE];
  37247. byte hashPub[WC_MAX_DIGEST_SIZE];
  37248. byte hashSz;
  37249. byte sig[144];
  37250. word32 sigSz;
  37251. int verified, valid;
  37252. char mail[] = "test@wolfssl.com";
  37253. byte* id = (byte*)mail;
  37254. word32 idSz = (word32) XSTRLEN(mail);
  37255. byte msg[] = { 0x00 };
  37256. word32 msgSz = sizeof(msg);
  37257. #ifdef WOLFSSL_SHA384
  37258. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  37259. &hashSz);
  37260. if (ret != BAD_FUNC_ARG)
  37261. return WC_TEST_RET_ENC_EC(ret);
  37262. #endif
  37263. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  37264. &hashSz);
  37265. if (ret != 0)
  37266. return WC_TEST_RET_ENC_EC(ret);
  37267. if (hashSz != 32)
  37268. return WC_TEST_RET_ENC_NC;
  37269. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  37270. &hashSz);
  37271. if (ret != 0)
  37272. return WC_TEST_RET_ENC_EC(ret);
  37273. if (hashSz != 32)
  37274. return WC_TEST_RET_ENC_NC;
  37275. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  37276. return WC_TEST_RET_ENC_NC;
  37277. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  37278. if (ret != 0)
  37279. return WC_TEST_RET_ENC_EC(ret);
  37280. ret = wc_SetEccsiPair(priv, ssk, pvt);
  37281. if (ret != 0)
  37282. return WC_TEST_RET_ENC_EC(ret);
  37283. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  37284. &sigSz);
  37285. if (ret != LENGTH_ONLY_E)
  37286. return WC_TEST_RET_ENC_EC(ret);
  37287. if (sigSz != 129)
  37288. return WC_TEST_RET_ENC_NC;
  37289. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  37290. &sigSz);
  37291. if (ret != 0)
  37292. return WC_TEST_RET_ENC_EC(ret);
  37293. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37294. if (ret != 0)
  37295. return WC_TEST_RET_ENC_EC(ret);
  37296. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37297. &verified);
  37298. if (ret != 0)
  37299. return WC_TEST_RET_ENC_EC(ret);
  37300. if (!verified)
  37301. return WC_TEST_RET_ENC_NC;
  37302. /* Check that changing HS results in verification failure. */
  37303. hashPub[0] ^= 0x80;
  37304. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37305. if (ret != 0)
  37306. return WC_TEST_RET_ENC_EC(ret);
  37307. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37308. &verified);
  37309. if (ret != 0)
  37310. return WC_TEST_RET_ENC_EC(ret);
  37311. if (verified)
  37312. return WC_TEST_RET_ENC_NC;
  37313. hashPub[0] ^= 0x80;
  37314. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37315. if (ret != 0)
  37316. return WC_TEST_RET_ENC_EC(ret);
  37317. /* Check that changing msg results in verification failure. */
  37318. msg[0] ^= 0x80;
  37319. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37320. &verified);
  37321. if (ret != 0)
  37322. return WC_TEST_RET_ENC_EC(ret);
  37323. if (verified)
  37324. return WC_TEST_RET_ENC_NC;
  37325. msg[0] ^= 0x80;
  37326. /* Check that changing signature results in verification failure. */
  37327. sig[0] ^= 0x80;
  37328. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37329. &verified);
  37330. if (ret != 0)
  37331. return WC_TEST_RET_ENC_EC(ret);
  37332. if (verified)
  37333. return WC_TEST_RET_ENC_NC;
  37334. sig[0] ^= 0x80;
  37335. /* Check that key state hasn't been invalidated. */
  37336. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37337. &verified);
  37338. if (ret != 0)
  37339. return WC_TEST_RET_ENC_EC(ret);
  37340. if (!verified)
  37341. return WC_TEST_RET_ENC_NC;
  37342. /* Check that verifying with the private key works. */
  37343. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37344. &verified);
  37345. if (ret != 0)
  37346. return WC_TEST_RET_ENC_EC(ret);
  37347. if (!verified)
  37348. return WC_TEST_RET_ENC_NC;
  37349. /* Check that the KPAK is converted from montgomery form. */
  37350. ret = eccsi_imp_exp_key_test(priv);
  37351. if (ret != 0)
  37352. return ret;
  37353. /* Check that KPAK can converted to Montgomery form again. */
  37354. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37355. &verified);
  37356. if (ret != 0)
  37357. return WC_TEST_RET_ENC_EC(ret);
  37358. if (!verified)
  37359. return WC_TEST_RET_ENC_NC;
  37360. /* Check that the KPAK is converted from montgomery form. */
  37361. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  37362. &valid);
  37363. if (ret != 0)
  37364. return WC_TEST_RET_ENC_EC(ret);
  37365. if (!valid)
  37366. return WC_TEST_RET_ENC_NC;
  37367. /* Check that KPAK can converted to Montgomery form again. */
  37368. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37369. &verified);
  37370. if (ret != 0)
  37371. return WC_TEST_RET_ENC_EC(ret);
  37372. if (!verified)
  37373. return WC_TEST_RET_ENC_NC;
  37374. /* Check that the KPAK is converted from montgomery form. */
  37375. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  37376. if (ret != 0)
  37377. return ret;
  37378. return 0;
  37379. }
  37380. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  37381. {
  37382. wc_test_ret_t ret = 0;
  37383. WC_RNG rng;
  37384. int rng_inited = 0;
  37385. EccsiKey* priv = NULL;
  37386. EccsiKey* pub = NULL;
  37387. mp_int* ssk = NULL;
  37388. ecc_point* pvt = NULL;
  37389. WOLFSSL_ENTER("eccsi_test");
  37390. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  37391. DYNAMIC_TYPE_TMP_BUFFER);
  37392. if (priv == NULL)
  37393. ret = WC_TEST_RET_ENC_NC;
  37394. else
  37395. XMEMSET(priv, 0, sizeof(*priv));
  37396. if (ret == 0) {
  37397. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  37398. DYNAMIC_TYPE_TMP_BUFFER);
  37399. if (pub == NULL)
  37400. ret = WC_TEST_RET_ENC_NC;
  37401. else
  37402. XMEMSET(pub, 0, sizeof(*pub));
  37403. }
  37404. if (ret == 0) {
  37405. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  37406. DYNAMIC_TYPE_TMP_BUFFER);
  37407. if (ssk == NULL)
  37408. ret = WC_TEST_RET_ENC_NC;
  37409. else
  37410. XMEMSET(ssk, 0, sizeof(*ssk));
  37411. }
  37412. if (ret == 0) {
  37413. #ifndef HAVE_FIPS
  37414. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37415. #else
  37416. ret = wc_InitRng(&rng);
  37417. #endif
  37418. if (ret != 0)
  37419. ret = WC_TEST_RET_ENC_EC(ret);
  37420. else
  37421. rng_inited = 1;
  37422. }
  37423. if (ret == 0) {
  37424. pvt = wc_ecc_new_point();
  37425. if (pvt == NULL)
  37426. ret = WC_TEST_RET_ENC_NC;
  37427. }
  37428. if (ret == 0) {
  37429. ret = mp_init(ssk);
  37430. if (ret != 0)
  37431. ret = WC_TEST_RET_ENC_EC(ret);
  37432. }
  37433. if (ret == 0) {
  37434. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  37435. }
  37436. if (ret == 0) {
  37437. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  37438. if (ret != 0)
  37439. ret = WC_TEST_RET_ENC_EC(ret);
  37440. }
  37441. if (ret == 0) {
  37442. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  37443. if (ret != 0)
  37444. ret = WC_TEST_RET_ENC_EC(ret);
  37445. }
  37446. if (ret == 0) {
  37447. ret = eccsi_kat_verify_test(pub, pvt);
  37448. }
  37449. if (ret == 0) {
  37450. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  37451. }
  37452. if (ret == 0) {
  37453. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  37454. }
  37455. if (pvt != NULL)
  37456. wc_ecc_del_point(pvt);
  37457. if (rng_inited)
  37458. wc_FreeRng(&rng);
  37459. if (ssk != NULL) {
  37460. mp_free(ssk);
  37461. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37462. }
  37463. if (pub != NULL) {
  37464. wc_FreeEccsiKey(pub);
  37465. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37466. }
  37467. if (priv != NULL) {
  37468. wc_FreeEccsiKey(priv);
  37469. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37470. }
  37471. return ret;
  37472. }
  37473. #endif /* WOLFCRYPT_HAVE_ECCSI */
  37474. #ifdef WOLFCRYPT_HAVE_SAKKE
  37475. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  37476. {
  37477. wc_test_ret_t ret;
  37478. byte id[1] = { 0x00 };
  37479. int valid;
  37480. byte data[256];
  37481. word32 sz;
  37482. byte auth[257];
  37483. word16 authSz;
  37484. byte ssv[256];
  37485. word16 ssvSz;
  37486. word32 len;
  37487. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  37488. if (ret != BAD_FUNC_ARG)
  37489. return WC_TEST_RET_ENC_EC(ret);
  37490. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  37491. if (ret != BAD_FUNC_ARG)
  37492. return WC_TEST_RET_ENC_EC(ret);
  37493. wc_FreeSakkeKey(NULL);
  37494. XMEMSET(key, 0, sizeof(*key));
  37495. wc_FreeSakkeKey(key);
  37496. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  37497. if (ret != 0)
  37498. return WC_TEST_RET_ENC_EC(ret);
  37499. ret = wc_MakeSakkeKey(NULL, NULL);
  37500. if (ret != BAD_FUNC_ARG)
  37501. return WC_TEST_RET_ENC_EC(ret);
  37502. ret = wc_MakeSakkeKey(key, NULL);
  37503. if (ret != BAD_FUNC_ARG)
  37504. return WC_TEST_RET_ENC_EC(ret);
  37505. ret = wc_MakeSakkeKey(NULL, rng);
  37506. if (ret != BAD_FUNC_ARG)
  37507. return WC_TEST_RET_ENC_EC(ret);
  37508. ret = wc_MakeSakkePublicKey(NULL, NULL);
  37509. if (ret != BAD_FUNC_ARG)
  37510. return WC_TEST_RET_ENC_EC(ret);
  37511. ret = wc_MakeSakkePublicKey(key, NULL);
  37512. if (ret != BAD_FUNC_ARG)
  37513. return WC_TEST_RET_ENC_EC(ret);
  37514. ret = wc_MakeSakkePublicKey(NULL, rsk);
  37515. if (ret != BAD_FUNC_ARG)
  37516. return WC_TEST_RET_ENC_EC(ret);
  37517. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  37518. if (ret != BAD_FUNC_ARG)
  37519. return WC_TEST_RET_ENC_EC(ret);
  37520. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  37521. if (ret != BAD_FUNC_ARG)
  37522. return WC_TEST_RET_ENC_EC(ret);
  37523. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  37524. if (ret != BAD_FUNC_ARG)
  37525. return WC_TEST_RET_ENC_EC(ret);
  37526. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  37527. if (ret != BAD_FUNC_ARG)
  37528. return WC_TEST_RET_ENC_EC(ret);
  37529. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  37530. if (ret != BAD_FUNC_ARG)
  37531. return WC_TEST_RET_ENC_EC(ret);
  37532. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  37533. if (ret != BAD_FUNC_ARG)
  37534. return WC_TEST_RET_ENC_EC(ret);
  37535. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  37536. if (ret != BAD_FUNC_ARG)
  37537. return WC_TEST_RET_ENC_EC(ret);
  37538. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  37539. if (ret != BAD_FUNC_ARG)
  37540. return WC_TEST_RET_ENC_EC(ret);
  37541. ret = wc_ExportSakkeKey(key, data, NULL);
  37542. if (ret != BAD_FUNC_ARG)
  37543. return WC_TEST_RET_ENC_EC(ret);
  37544. ret = wc_ExportSakkeKey(NULL, data, &sz);
  37545. if (ret != BAD_FUNC_ARG)
  37546. return WC_TEST_RET_ENC_EC(ret);
  37547. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  37548. if (ret != BAD_FUNC_ARG)
  37549. return WC_TEST_RET_ENC_EC(ret);
  37550. ret = wc_ImportSakkeKey(key, NULL, 1);
  37551. if (ret != BAD_FUNC_ARG)
  37552. return WC_TEST_RET_ENC_EC(ret);
  37553. ret = wc_ImportSakkeKey(NULL, data, 1);
  37554. if (ret != BAD_FUNC_ARG)
  37555. return WC_TEST_RET_ENC_EC(ret);
  37556. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  37557. if (ret != BAD_FUNC_ARG)
  37558. return WC_TEST_RET_ENC_EC(ret);
  37559. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  37560. if (ret != BAD_FUNC_ARG)
  37561. return WC_TEST_RET_ENC_EC(ret);
  37562. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  37563. if (ret != BAD_FUNC_ARG)
  37564. return WC_TEST_RET_ENC_EC(ret);
  37565. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  37566. if (ret != BAD_FUNC_ARG)
  37567. return WC_TEST_RET_ENC_EC(ret);
  37568. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  37569. if (ret != BAD_FUNC_ARG)
  37570. return WC_TEST_RET_ENC_EC(ret);
  37571. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  37572. if (ret != BAD_FUNC_ARG)
  37573. return WC_TEST_RET_ENC_EC(ret);
  37574. sz = sizeof(data);
  37575. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  37576. if (ret != BAD_FUNC_ARG)
  37577. return WC_TEST_RET_ENC_EC(ret);
  37578. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  37579. if (ret != BAD_FUNC_ARG)
  37580. return WC_TEST_RET_ENC_EC(ret);
  37581. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  37582. if (ret != BAD_FUNC_ARG)
  37583. return WC_TEST_RET_ENC_EC(ret);
  37584. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  37585. if (ret != BAD_FUNC_ARG)
  37586. return WC_TEST_RET_ENC_EC(ret);
  37587. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  37588. if (ret != BAD_FUNC_ARG)
  37589. return WC_TEST_RET_ENC_EC(ret);
  37590. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  37591. if (ret != BAD_FUNC_ARG)
  37592. return WC_TEST_RET_ENC_EC(ret);
  37593. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  37594. if (ret != BAD_FUNC_ARG)
  37595. return WC_TEST_RET_ENC_EC(ret);
  37596. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  37597. if (ret != BAD_FUNC_ARG)
  37598. return WC_TEST_RET_ENC_EC(ret);
  37599. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  37600. if (ret != BAD_FUNC_ARG)
  37601. return WC_TEST_RET_ENC_EC(ret);
  37602. ret = wc_ImportSakkeRsk(key, NULL, sz);
  37603. if (ret != BAD_FUNC_ARG)
  37604. return WC_TEST_RET_ENC_EC(ret);
  37605. ret = wc_ImportSakkeRsk(NULL, data, sz);
  37606. if (ret != BAD_FUNC_ARG)
  37607. return WC_TEST_RET_ENC_EC(ret);
  37608. ret = wc_ImportSakkeRsk(key, data, 1);
  37609. if (ret != BUFFER_E)
  37610. return WC_TEST_RET_ENC_EC(ret);
  37611. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  37612. if (ret != BAD_FUNC_ARG)
  37613. return WC_TEST_RET_ENC_EC(ret);
  37614. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  37615. if (ret != BAD_FUNC_ARG)
  37616. return WC_TEST_RET_ENC_EC(ret);
  37617. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  37618. if (ret != BAD_FUNC_ARG)
  37619. return WC_TEST_RET_ENC_EC(ret);
  37620. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  37621. if (ret != BAD_FUNC_ARG)
  37622. return WC_TEST_RET_ENC_EC(ret);
  37623. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  37624. if (ret != BAD_FUNC_ARG)
  37625. return WC_TEST_RET_ENC_EC(ret);
  37626. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  37627. if (ret != BAD_FUNC_ARG)
  37628. return WC_TEST_RET_ENC_EC(ret);
  37629. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  37630. if (ret != BAD_FUNC_ARG)
  37631. return WC_TEST_RET_ENC_EC(ret);
  37632. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  37633. if (ret != LENGTH_ONLY_E)
  37634. return WC_TEST_RET_ENC_EC(ret);
  37635. len--;
  37636. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  37637. if (ret != BUFFER_E)
  37638. return WC_TEST_RET_ENC_EC(ret);
  37639. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  37640. if (ret != BAD_FUNC_ARG)
  37641. return WC_TEST_RET_ENC_EC(ret);
  37642. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  37643. if (ret != BAD_FUNC_ARG)
  37644. return WC_TEST_RET_ENC_EC(ret);
  37645. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  37646. if (ret != BAD_FUNC_ARG)
  37647. return WC_TEST_RET_ENC_EC(ret);
  37648. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  37649. if (ret != BAD_FUNC_ARG)
  37650. return WC_TEST_RET_ENC_EC(ret);
  37651. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  37652. if (ret != BAD_FUNC_ARG)
  37653. return WC_TEST_RET_ENC_EC(ret);
  37654. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  37655. if (ret != BAD_FUNC_ARG)
  37656. return WC_TEST_RET_ENC_EC(ret);
  37657. ret = wc_GetSakkeAuthSize(NULL, NULL);
  37658. if (ret != BAD_FUNC_ARG)
  37659. return WC_TEST_RET_ENC_EC(ret);
  37660. ret = wc_GetSakkeAuthSize(key, NULL);
  37661. if (ret != BAD_FUNC_ARG)
  37662. return WC_TEST_RET_ENC_EC(ret);
  37663. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  37664. if (ret != BAD_FUNC_ARG)
  37665. return WC_TEST_RET_ENC_EC(ret);
  37666. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  37667. if (ret != BAD_FUNC_ARG)
  37668. return WC_TEST_RET_ENC_EC(ret);
  37669. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  37670. if (ret != BAD_FUNC_ARG)
  37671. return WC_TEST_RET_ENC_EC(ret);
  37672. ret = wc_MakeSakkePointI(NULL, id, 1);
  37673. if (ret != BAD_FUNC_ARG)
  37674. return WC_TEST_RET_ENC_EC(ret);
  37675. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  37676. if (ret != BAD_FUNC_ARG)
  37677. return WC_TEST_RET_ENC_EC(ret);
  37678. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  37679. if (ret != BAD_FUNC_ARG)
  37680. return WC_TEST_RET_ENC_EC(ret);
  37681. ret = wc_MakeSakkePointI(key, NULL, 1);
  37682. if (ret != BAD_FUNC_ARG)
  37683. return WC_TEST_RET_ENC_EC(ret);
  37684. ret = wc_MakeSakkePointI(NULL, id, 1);
  37685. if (ret != BAD_FUNC_ARG)
  37686. return WC_TEST_RET_ENC_EC(ret);
  37687. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  37688. if (ret != BAD_FUNC_ARG)
  37689. return WC_TEST_RET_ENC_EC(ret);
  37690. ret = wc_GenerateSakkePointITable(key, data, NULL);
  37691. if (ret != BAD_FUNC_ARG)
  37692. return WC_TEST_RET_ENC_EC(ret);
  37693. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  37694. if (ret != BAD_FUNC_ARG)
  37695. return WC_TEST_RET_ENC_EC(ret);
  37696. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  37697. if (ret != LENGTH_ONLY_E)
  37698. return WC_TEST_RET_ENC_EC(ret);
  37699. len--;
  37700. ret = wc_GenerateSakkePointITable(key, data, &len);
  37701. if (ret != BUFFER_E)
  37702. return WC_TEST_RET_ENC_EC(ret);
  37703. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  37704. if (ret != BAD_FUNC_ARG)
  37705. return WC_TEST_RET_ENC_EC(ret);
  37706. ret = wc_SetSakkePointITable(key, NULL, 1);
  37707. if (ret != BAD_FUNC_ARG)
  37708. return WC_TEST_RET_ENC_EC(ret);
  37709. ret = wc_SetSakkePointITable(NULL, data, 1);
  37710. if (ret != BAD_FUNC_ARG)
  37711. return WC_TEST_RET_ENC_EC(ret);
  37712. ret = wc_SetSakkePointITable(key, data, 1);
  37713. if (ret != BUFFER_E)
  37714. return WC_TEST_RET_ENC_EC(ret);
  37715. ret = wc_ClearSakkePointITable(NULL);
  37716. if (ret != BAD_FUNC_ARG)
  37717. return WC_TEST_RET_ENC_EC(ret);
  37718. ret = wc_GetSakkePointI(NULL, data, NULL);
  37719. if (ret != BAD_FUNC_ARG)
  37720. return WC_TEST_RET_ENC_EC(ret);
  37721. ret = wc_GetSakkePointI(key, data, NULL);
  37722. if (ret != BAD_FUNC_ARG)
  37723. return WC_TEST_RET_ENC_EC(ret);
  37724. ret = wc_GetSakkePointI(NULL, data, &sz);
  37725. if (ret != BAD_FUNC_ARG)
  37726. return WC_TEST_RET_ENC_EC(ret);
  37727. sz = 1;
  37728. ret = wc_GetSakkePointI(key, data, &sz);
  37729. if (ret != BUFFER_E)
  37730. return WC_TEST_RET_ENC_EC(ret);
  37731. sz = 256;
  37732. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  37733. if (ret != BAD_FUNC_ARG)
  37734. return WC_TEST_RET_ENC_EC(ret);
  37735. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  37736. if (ret != BAD_FUNC_ARG)
  37737. return WC_TEST_RET_ENC_EC(ret);
  37738. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  37739. if (ret != BAD_FUNC_ARG)
  37740. return WC_TEST_RET_ENC_EC(ret);
  37741. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  37742. if (ret != BAD_FUNC_ARG)
  37743. return WC_TEST_RET_ENC_EC(ret);
  37744. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  37745. if (ret != BAD_FUNC_ARG)
  37746. return WC_TEST_RET_ENC_EC(ret);
  37747. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  37748. if (ret != BAD_FUNC_ARG)
  37749. return WC_TEST_RET_ENC_EC(ret);
  37750. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  37751. if (ret != BAD_FUNC_ARG)
  37752. return WC_TEST_RET_ENC_EC(ret);
  37753. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  37754. if (ret != BUFFER_E)
  37755. return WC_TEST_RET_ENC_EC(ret);
  37756. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  37757. if (ret != BUFFER_E)
  37758. return WC_TEST_RET_ENC_EC(ret);
  37759. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  37760. if (ret != BAD_FUNC_ARG)
  37761. return WC_TEST_RET_ENC_EC(ret);
  37762. ret = wc_SetSakkeIdentity(key, NULL, 1);
  37763. if (ret != BAD_FUNC_ARG)
  37764. return WC_TEST_RET_ENC_EC(ret);
  37765. ret = wc_SetSakkeIdentity(NULL, id, 1);
  37766. if (ret != BAD_FUNC_ARG)
  37767. return WC_TEST_RET_ENC_EC(ret);
  37768. ssvSz = sizeof(ssv);
  37769. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37770. auth, NULL);
  37771. if (ret != BAD_FUNC_ARG)
  37772. return WC_TEST_RET_ENC_EC(ret);
  37773. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37774. auth, NULL);
  37775. if (ret != BAD_FUNC_ARG)
  37776. return WC_TEST_RET_ENC_EC(ret);
  37777. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37778. auth, NULL);
  37779. if (ret != BAD_FUNC_ARG)
  37780. return WC_TEST_RET_ENC_EC(ret);
  37781. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37782. auth, &authSz);
  37783. if (ret != BAD_FUNC_ARG)
  37784. return WC_TEST_RET_ENC_EC(ret);
  37785. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37786. auth, NULL);
  37787. if (ret != BAD_FUNC_ARG)
  37788. return WC_TEST_RET_ENC_EC(ret);
  37789. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37790. auth, &authSz);
  37791. if (ret != BAD_FUNC_ARG)
  37792. return WC_TEST_RET_ENC_EC(ret);
  37793. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37794. auth, &authSz);
  37795. if (ret != BAD_FUNC_ARG)
  37796. return WC_TEST_RET_ENC_EC(ret);
  37797. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37798. auth, &authSz);
  37799. if (ret != BAD_STATE_E)
  37800. return WC_TEST_RET_ENC_EC(ret);
  37801. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  37802. if (ret != BAD_FUNC_ARG)
  37803. return WC_TEST_RET_ENC_EC(ret);
  37804. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  37805. if (ret != BAD_FUNC_ARG)
  37806. return WC_TEST_RET_ENC_EC(ret);
  37807. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  37808. if (ret != BAD_FUNC_ARG)
  37809. return WC_TEST_RET_ENC_EC(ret);
  37810. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  37811. if (ret != BAD_FUNC_ARG)
  37812. return WC_TEST_RET_ENC_EC(ret);
  37813. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  37814. if (ret != BAD_FUNC_ARG)
  37815. return WC_TEST_RET_ENC_EC(ret);
  37816. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  37817. if (ret != BAD_FUNC_ARG)
  37818. return WC_TEST_RET_ENC_EC(ret);
  37819. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  37820. if (ret != BAD_FUNC_ARG)
  37821. return WC_TEST_RET_ENC_EC(ret);
  37822. ssvSz = sizeof(ssv);
  37823. authSz = sizeof(auth);
  37824. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  37825. authSz);
  37826. if (ret != BAD_FUNC_ARG)
  37827. return WC_TEST_RET_ENC_EC(ret);
  37828. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  37829. authSz);
  37830. if (ret != BAD_FUNC_ARG)
  37831. return WC_TEST_RET_ENC_EC(ret);
  37832. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  37833. authSz);
  37834. if (ret != BAD_FUNC_ARG)
  37835. return WC_TEST_RET_ENC_EC(ret);
  37836. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  37837. authSz);
  37838. if (ret != BAD_FUNC_ARG)
  37839. return WC_TEST_RET_ENC_EC(ret);
  37840. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  37841. authSz);
  37842. if (ret != BAD_FUNC_ARG)
  37843. return WC_TEST_RET_ENC_EC(ret);
  37844. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  37845. authSz);
  37846. if (ret != BAD_FUNC_ARG)
  37847. return WC_TEST_RET_ENC_EC(ret);
  37848. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37849. authSz);
  37850. if (ret != BAD_FUNC_ARG)
  37851. return WC_TEST_RET_ENC_EC(ret);
  37852. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37853. authSz);
  37854. if (ret != BAD_STATE_E)
  37855. return WC_TEST_RET_ENC_EC(ret);
  37856. ret = wc_SetSakkeIdentity(key, id, 1);
  37857. if (ret != 0)
  37858. return WC_TEST_RET_ENC_EC(ret);
  37859. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37860. authSz);
  37861. if (ret != BAD_STATE_E)
  37862. return WC_TEST_RET_ENC_EC(ret);
  37863. ret = wc_SetSakkeIdentity(key, id, 0);
  37864. if (ret != 0)
  37865. return WC_TEST_RET_ENC_EC(ret);
  37866. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  37867. if (ret != 0)
  37868. return WC_TEST_RET_ENC_EC(ret);
  37869. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37870. authSz);
  37871. if (ret != BAD_STATE_E)
  37872. return WC_TEST_RET_ENC_EC(ret);
  37873. wc_FreeSakkeKey(key);
  37874. return 0;
  37875. }
  37876. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  37877. {
  37878. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  37879. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  37880. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  37881. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  37882. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  37883. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  37884. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  37885. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  37886. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  37887. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  37888. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  37889. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  37890. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  37891. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  37892. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  37893. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  37894. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  37895. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  37896. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  37897. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  37898. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  37899. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  37900. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  37901. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  37902. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  37903. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  37904. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  37905. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  37906. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  37907. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  37908. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  37909. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  37910. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  37911. };
  37912. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  37913. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  37914. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  37915. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  37916. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  37917. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  37918. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  37919. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  37920. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  37921. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  37922. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  37923. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  37924. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  37925. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  37926. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  37927. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  37928. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  37929. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  37930. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  37931. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  37932. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  37933. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  37934. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  37935. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  37936. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  37937. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  37938. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  37939. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  37940. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  37941. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  37942. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  37943. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  37944. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  37945. };
  37946. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  37947. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  37948. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  37949. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  37950. 0x33, 0x00
  37951. };
  37952. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  37953. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  37954. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  37955. };
  37956. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  37957. 0x04,
  37958. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  37959. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  37960. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  37961. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  37962. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  37963. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  37964. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  37965. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  37966. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  37967. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  37968. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  37969. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  37970. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  37971. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  37972. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  37973. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  37974. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  37975. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  37976. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  37977. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  37978. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  37979. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  37980. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  37981. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  37982. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  37983. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  37984. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  37985. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  37986. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  37987. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  37988. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  37989. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  37990. };
  37991. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  37992. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  37993. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  37994. };
  37995. wc_test_ret_t ret;
  37996. int valid;
  37997. byte pubKey[sizeof(pubData) + 1];
  37998. word32 sz = sizeof(pubKey);
  37999. byte tmpSsv[sizeof(encSsv)];
  38000. byte* iTable = NULL;
  38001. word32 iTableLen;
  38002. byte* table = NULL;
  38003. word32 len;
  38004. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  38005. if (ret != 0)
  38006. return WC_TEST_RET_ENC_EC(ret);
  38007. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  38008. if (ret != 0)
  38009. return WC_TEST_RET_ENC_EC(ret);
  38010. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  38011. if (ret != 0)
  38012. return WC_TEST_RET_ENC_EC(ret);
  38013. if (valid != 1)
  38014. return WC_TEST_RET_ENC_NC;
  38015. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  38016. if (ret != 0)
  38017. return WC_TEST_RET_ENC_EC(ret);
  38018. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  38019. if (ret != 0)
  38020. return WC_TEST_RET_ENC_EC(ret);
  38021. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  38022. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  38023. auth, sizeof(auth));
  38024. if (ret != 0)
  38025. return WC_TEST_RET_ENC_EC(ret);
  38026. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  38027. return WC_TEST_RET_ENC_NC;
  38028. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  38029. if (ret != 0)
  38030. return WC_TEST_RET_ENC_EC(ret);
  38031. iTableLen = 0;
  38032. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  38033. if (ret != LENGTH_ONLY_E)
  38034. return WC_TEST_RET_ENC_EC(ret);
  38035. if (iTableLen != 0) {
  38036. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38037. if (iTable == NULL)
  38038. return WC_TEST_RET_ENC_ERRNO;
  38039. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  38040. if (ret != 0)
  38041. return WC_TEST_RET_ENC_EC(ret);
  38042. }
  38043. len = 0;
  38044. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  38045. if (ret != LENGTH_ONLY_E)
  38046. return WC_TEST_RET_ENC_EC(ret);
  38047. if (len > 0) {
  38048. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38049. if (table == NULL)
  38050. return WC_TEST_RET_ENC_ERRNO;
  38051. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  38052. if (ret != 0)
  38053. return WC_TEST_RET_ENC_EC(ret);
  38054. }
  38055. ret = wc_SetSakkeRsk(key, rsk, table, len);
  38056. if (ret != 0)
  38057. return WC_TEST_RET_ENC_EC(ret);
  38058. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  38059. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  38060. auth, sizeof(auth));
  38061. if (ret != 0)
  38062. return WC_TEST_RET_ENC_EC(ret);
  38063. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  38064. return WC_TEST_RET_ENC_NC;
  38065. /* Don't reference table that is about to be freed. */
  38066. ret = wc_ClearSakkePointITable(key);
  38067. if (ret != 0)
  38068. return WC_TEST_RET_ENC_EC(ret);
  38069. /* Dispose of tables */
  38070. if (iTable != NULL)
  38071. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38072. if (table != NULL)
  38073. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38074. /* Make sure the key public key is exportable - convert to Montgomery form
  38075. * in Validation.
  38076. */
  38077. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  38078. if (ret != 0)
  38079. return WC_TEST_RET_ENC_EC(ret);
  38080. if (sz != sizeof(pubData))
  38081. return WC_TEST_RET_ENC_NC;
  38082. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  38083. return WC_TEST_RET_ENC_NC;
  38084. sz = sizeof(pubData) + 1;
  38085. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  38086. if (ret != 0)
  38087. return WC_TEST_RET_ENC_EC(ret);
  38088. if (sz != sizeof(pubData) + 1)
  38089. return WC_TEST_RET_ENC_NC;
  38090. if (pubKey[0] != 0x04)
  38091. return WC_TEST_RET_ENC_NC;
  38092. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  38093. return WC_TEST_RET_ENC_NC;
  38094. return 0;
  38095. }
  38096. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  38097. {
  38098. static const byte pubData[] = {
  38099. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  38100. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  38101. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  38102. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  38103. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  38104. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  38105. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  38106. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  38107. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  38108. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  38109. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  38110. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  38111. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  38112. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  38113. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  38114. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  38115. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  38116. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  38117. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  38118. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  38119. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  38120. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  38121. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  38122. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  38123. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  38124. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  38125. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  38126. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  38127. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  38128. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  38129. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  38130. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  38131. };
  38132. static const byte id[] = {
  38133. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  38134. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  38135. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  38136. 0x33, 0x00
  38137. };
  38138. static const word32 idSz = sizeof(id);
  38139. byte ssv[] = {
  38140. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  38141. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  38142. };
  38143. static const word16 ssvSz = sizeof(ssv);
  38144. static const byte expAuth[] = {
  38145. 0x04,
  38146. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  38147. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  38148. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  38149. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  38150. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  38151. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  38152. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  38153. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  38154. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  38155. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  38156. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  38157. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  38158. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  38159. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  38160. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  38161. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  38162. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  38163. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  38164. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  38165. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  38166. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  38167. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  38168. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  38169. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  38170. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  38171. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  38172. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  38173. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  38174. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  38175. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  38176. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  38177. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  38178. };
  38179. static const byte encSsv[] = {
  38180. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  38181. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  38182. };
  38183. wc_test_ret_t ret;
  38184. byte auth[257];
  38185. word16 authSz = sizeof(auth);
  38186. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  38187. if (ret != 0)
  38188. return WC_TEST_RET_ENC_EC(ret);
  38189. ret = wc_SetSakkeIdentity(key, id, idSz);
  38190. if (ret != 0)
  38191. return WC_TEST_RET_ENC_EC(ret);
  38192. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38193. auth, &authSz);
  38194. if (ret != 0)
  38195. return WC_TEST_RET_ENC_EC(ret);
  38196. if (authSz != 257)
  38197. return WC_TEST_RET_ENC_NC;
  38198. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  38199. return WC_TEST_RET_ENC_NC;
  38200. if (XMEMCMP(auth, expAuth, authSz) != 0)
  38201. return WC_TEST_RET_ENC_NC;
  38202. return 0;
  38203. }
  38204. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  38205. WC_RNG* rng, ecc_point* rsk)
  38206. {
  38207. wc_test_ret_t ret;
  38208. byte data[440];
  38209. byte pubData[257];
  38210. word32 sz;
  38211. char mail[] = "test@wolfssl.com";
  38212. byte* id = (byte*)mail;
  38213. word32 idSz = (word32)XSTRLEN(mail);
  38214. int valid;
  38215. ecc_point* pubKey = rsk;
  38216. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  38217. if (ret != 0)
  38218. return WC_TEST_RET_ENC_EC(ret);
  38219. ret = wc_MakeSakkeKey(priv, rng);
  38220. if (ret != 0)
  38221. return WC_TEST_RET_ENC_EC(ret);
  38222. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  38223. if (ret != LENGTH_ONLY_E)
  38224. return WC_TEST_RET_ENC_EC(ret);
  38225. if (sz != 384)
  38226. return WC_TEST_RET_ENC_NC;
  38227. sz--;
  38228. ret = wc_ExportSakkeKey(priv, data, &sz);
  38229. if (ret == 0)
  38230. return WC_TEST_RET_ENC_NC;
  38231. sz++;
  38232. ret = wc_ExportSakkeKey(priv, data, &sz);
  38233. if (ret != 0)
  38234. return WC_TEST_RET_ENC_EC(ret);
  38235. if (sz != 384)
  38236. return WC_TEST_RET_ENC_NC;
  38237. ret = wc_ImportSakkeKey(key, data, sz - 1);
  38238. if (ret == 0)
  38239. return WC_TEST_RET_ENC_NC;
  38240. ret = wc_ImportSakkeKey(key, data, sz);
  38241. if (ret != 0)
  38242. return WC_TEST_RET_ENC_EC(ret);
  38243. wc_FreeSakkeKey(key);
  38244. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  38245. if (ret != 0)
  38246. return WC_TEST_RET_ENC_EC(ret);
  38247. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  38248. if (ret != LENGTH_ONLY_E)
  38249. return WC_TEST_RET_ENC_EC(ret);
  38250. if (sz != 128)
  38251. return WC_TEST_RET_ENC_NC;
  38252. sz--;
  38253. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  38254. if (ret == 0)
  38255. return WC_TEST_RET_ENC_NC;
  38256. sz++;
  38257. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  38258. if (ret != 0)
  38259. return WC_TEST_RET_ENC_EC(ret);
  38260. if (sz != 128)
  38261. return WC_TEST_RET_ENC_NC;
  38262. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  38263. if (ret == 0)
  38264. return WC_TEST_RET_ENC_NC;
  38265. ret = wc_ImportSakkePrivateKey(key, data, sz);
  38266. if (ret != 0)
  38267. return WC_TEST_RET_ENC_EC(ret);
  38268. ret = wc_MakeSakkePublicKey(key, pubKey);
  38269. if (ret != 0)
  38270. return WC_TEST_RET_ENC_EC(ret);
  38271. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  38272. if (ret != LENGTH_ONLY_E)
  38273. return WC_TEST_RET_ENC_EC(ret);
  38274. if (sz != 256)
  38275. return WC_TEST_RET_ENC_NC;
  38276. sz--;
  38277. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  38278. if (ret == 0)
  38279. return WC_TEST_RET_ENC_NC;
  38280. sz++;
  38281. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  38282. if (ret != 0)
  38283. return WC_TEST_RET_ENC_EC(ret);
  38284. if (sz != 256)
  38285. return WC_TEST_RET_ENC_NC;
  38286. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  38287. if (ret == 0)
  38288. return WC_TEST_RET_ENC_NC;
  38289. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  38290. if (ret != 0)
  38291. return WC_TEST_RET_ENC_EC(ret);
  38292. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  38293. if (ret != 0)
  38294. return WC_TEST_RET_ENC_EC(ret);
  38295. if (sz != 256)
  38296. return WC_TEST_RET_ENC_NC;
  38297. if (XMEMCMP(data, pubData, sz) != 0)
  38298. return WC_TEST_RET_ENC_NC;
  38299. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  38300. if (ret != 0)
  38301. return WC_TEST_RET_ENC_EC(ret);
  38302. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  38303. if (ret != 0)
  38304. return WC_TEST_RET_ENC_EC(ret);
  38305. if (valid != 1)
  38306. return WC_TEST_RET_ENC_NC;
  38307. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  38308. if (ret != 0)
  38309. return WC_TEST_RET_ENC_EC(ret);
  38310. if (valid != 1)
  38311. return WC_TEST_RET_ENC_NC;
  38312. sz = sizeof(data);
  38313. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  38314. if (ret != 0)
  38315. return WC_TEST_RET_ENC_EC(ret);
  38316. if (sz != 256)
  38317. return WC_TEST_RET_ENC_NC;
  38318. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  38319. if (ret != 0)
  38320. return WC_TEST_RET_ENC_EC(ret);
  38321. sz = sizeof(pubData);
  38322. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  38323. if (ret != 0)
  38324. return WC_TEST_RET_ENC_EC(ret);
  38325. if (sz != sizeof(pubData))
  38326. return WC_TEST_RET_ENC_NC;
  38327. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  38328. if (ret != 0)
  38329. return WC_TEST_RET_ENC_EC(ret);
  38330. wc_FreeSakkeKey(key);
  38331. return 0;
  38332. }
  38333. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  38334. ecc_point* rsk)
  38335. {
  38336. wc_test_ret_t ret;
  38337. byte ssv[16];
  38338. word16 ssvSz;
  38339. byte auth[257];
  38340. word16 authSz;
  38341. char mail[] = "test@wolfssl.com";
  38342. byte* id = (byte*)mail;
  38343. word32 idSz = (word32)XSTRLEN(mail);
  38344. byte pointI[256];
  38345. word32 sz;
  38346. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  38347. if (ret != LENGTH_ONLY_E)
  38348. return WC_TEST_RET_ENC_EC(ret);
  38349. if (ssvSz != 16)
  38350. return WC_TEST_RET_ENC_NC;
  38351. ssvSz += 128;
  38352. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  38353. if (ret == 0)
  38354. return WC_TEST_RET_ENC_NC;
  38355. ssvSz -= 128;
  38356. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  38357. if (ret != 0)
  38358. return WC_TEST_RET_ENC_EC(ret);
  38359. if (ssvSz != 16)
  38360. return WC_TEST_RET_ENC_NC;
  38361. ret = wc_GetSakkeAuthSize(pub, &authSz);
  38362. if (ret != 0)
  38363. return WC_TEST_RET_ENC_EC(ret);
  38364. ret = wc_SetSakkeIdentity(pub, id, idSz);
  38365. if (ret != 0)
  38366. return WC_TEST_RET_ENC_EC(ret);
  38367. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38368. NULL, &authSz);
  38369. if (ret != LENGTH_ONLY_E)
  38370. return WC_TEST_RET_ENC_EC(ret);
  38371. if (authSz != 257)
  38372. return WC_TEST_RET_ENC_NC;
  38373. authSz--;
  38374. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38375. auth, &authSz);
  38376. if (ret == 0)
  38377. return WC_TEST_RET_ENC_NC;
  38378. authSz++;
  38379. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38380. auth, &authSz);
  38381. if (ret != 0)
  38382. return WC_TEST_RET_ENC_EC(ret);
  38383. if (authSz != 257)
  38384. return WC_TEST_RET_ENC_NC;
  38385. ret = wc_GetSakkePointI(pub, NULL, &sz);
  38386. if (ret != LENGTH_ONLY_E)
  38387. return WC_TEST_RET_ENC_EC(ret);
  38388. if (sz != 256)
  38389. return WC_TEST_RET_ENC_NC;
  38390. ret = wc_GetSakkePointI(pub, pointI, &sz);
  38391. if (ret != 0)
  38392. return WC_TEST_RET_ENC_EC(ret);
  38393. if (sz != 256)
  38394. return WC_TEST_RET_ENC_NC;
  38395. /* Bogus identity - make it check and regenerate I. */
  38396. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  38397. if (ret != 0)
  38398. return WC_TEST_RET_ENC_EC(ret);
  38399. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38400. auth, &authSz);
  38401. if (ret != 0)
  38402. return WC_TEST_RET_ENC_EC(ret);
  38403. if (authSz != 257)
  38404. return WC_TEST_RET_ENC_NC;
  38405. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  38406. if (ret != 0)
  38407. return WC_TEST_RET_ENC_EC(ret);
  38408. ret = wc_SetSakkeIdentity(priv, id, idSz);
  38409. if (ret != 0)
  38410. return WC_TEST_RET_ENC_EC(ret);
  38411. authSz--;
  38412. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38413. authSz);
  38414. if (ret == 0)
  38415. return WC_TEST_RET_ENC_NC;
  38416. authSz++;
  38417. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38418. authSz);
  38419. if (ret != 0)
  38420. return WC_TEST_RET_ENC_EC(ret);
  38421. ssv[0] ^= 0x80;
  38422. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38423. authSz);
  38424. if (ret != SAKKE_VERIFY_FAIL_E)
  38425. return WC_TEST_RET_ENC_EC(ret);
  38426. ssv[0] ^= 0x80;
  38427. /* Bogus identity - make it check and regenerate I. */
  38428. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  38429. if (ret != 0)
  38430. return WC_TEST_RET_ENC_EC(ret);
  38431. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38432. authSz);
  38433. if (ret != 0)
  38434. return WC_TEST_RET_ENC_EC(ret);
  38435. return 0;
  38436. }
  38437. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  38438. {
  38439. wc_test_ret_t ret = 0;
  38440. WC_RNG rng;
  38441. int rng_inited = 0;
  38442. SakkeKey* priv = NULL;
  38443. SakkeKey* pub = NULL;
  38444. SakkeKey* key = NULL;
  38445. ecc_point* rsk = NULL;
  38446. WOLFSSL_ENTER("sakke_test");
  38447. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38448. DYNAMIC_TYPE_TMP_BUFFER);
  38449. if (priv == NULL)
  38450. ret = WC_TEST_RET_ENC_NC;
  38451. else
  38452. XMEMSET(priv, 0, sizeof(*priv));
  38453. if (ret == 0) {
  38454. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38455. DYNAMIC_TYPE_TMP_BUFFER);
  38456. if (pub == NULL)
  38457. ret = WC_TEST_RET_ENC_NC;
  38458. else
  38459. XMEMSET(pub, 0, sizeof(*pub));
  38460. }
  38461. if (ret == 0) {
  38462. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38463. DYNAMIC_TYPE_TMP_BUFFER);
  38464. if (key == NULL)
  38465. ret = WC_TEST_RET_ENC_NC;
  38466. else
  38467. XMEMSET(key, 0, sizeof(*key));
  38468. }
  38469. if (ret == 0) {
  38470. #ifndef HAVE_FIPS
  38471. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38472. #else
  38473. ret = wc_InitRng(&rng);
  38474. #endif
  38475. if (ret == 0)
  38476. rng_inited = 1;
  38477. else
  38478. ret = WC_TEST_RET_ENC_EC(ret);
  38479. }
  38480. if (ret == 0) {
  38481. rsk = wc_ecc_new_point();
  38482. if (rsk == NULL)
  38483. ret = WC_TEST_RET_ENC_NC;
  38484. }
  38485. if (ret == 0) {
  38486. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  38487. if (ret != 0)
  38488. ret = WC_TEST_RET_ENC_EC(ret);
  38489. }
  38490. if (ret == 0) {
  38491. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  38492. if (ret != 0)
  38493. ret = WC_TEST_RET_ENC_EC(ret);
  38494. }
  38495. if (ret == 0) {
  38496. ret = sakke_api_test(&rng, key, rsk);
  38497. }
  38498. if (ret == 0) {
  38499. ret = sakke_kat_derive_test(pub, rsk);
  38500. }
  38501. if (ret == 0) {
  38502. ret = sakke_kat_encapsulate_test(pub);
  38503. }
  38504. if (ret == 0) {
  38505. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  38506. }
  38507. if (ret == 0) {
  38508. ret = sakke_op_test(priv, pub, &rng, rsk);
  38509. }
  38510. if (rsk != NULL) {
  38511. wc_ecc_forcezero_point(rsk);
  38512. wc_ecc_del_point(rsk);
  38513. }
  38514. if (rng_inited)
  38515. wc_FreeRng(&rng);
  38516. if (key != NULL)
  38517. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38518. if (pub != NULL) {
  38519. wc_FreeSakkeKey(pub);
  38520. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38521. }
  38522. if (priv != NULL) {
  38523. wc_FreeSakkeKey(priv);
  38524. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38525. }
  38526. return ret;
  38527. }
  38528. #endif /* WOLFCRYPT_HAVE_SAKKE */
  38529. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  38530. typedef struct CMAC_Test_Case {
  38531. int type;
  38532. int partial;
  38533. const byte* m;
  38534. word32 mSz;
  38535. const byte* k;
  38536. word32 kSz;
  38537. const byte* t;
  38538. word32 tSz;
  38539. } CMAC_Test_Case;
  38540. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  38541. {
  38542. #ifdef WOLFSSL_AES_128
  38543. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  38544. {
  38545. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  38546. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  38547. };
  38548. #define KLEN_128 (sizeof(k128))
  38549. #endif
  38550. #ifdef WOLFSSL_AES_192
  38551. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  38552. {
  38553. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  38554. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  38555. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  38556. };
  38557. #define KLEN_192 (sizeof(k192))
  38558. #endif
  38559. #ifdef WOLFSSL_AES_256
  38560. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  38561. {
  38562. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38563. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38564. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38565. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38566. };
  38567. #define KLEN_256 (sizeof(k256))
  38568. #endif
  38569. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  38570. {
  38571. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38572. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  38573. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  38574. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  38575. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  38576. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  38577. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  38578. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  38579. };
  38580. #define MLEN_0 (0)
  38581. #define MLEN_128 (128/8)
  38582. #define MLEN_320 (320/8)
  38583. #define MLEN_319 (MLEN_320 - 1)
  38584. #define MLEN_512 (512/8)
  38585. #ifdef WOLFSSL_AES_128
  38586. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  38587. {
  38588. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  38589. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  38590. };
  38591. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  38592. {
  38593. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  38594. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  38595. };
  38596. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  38597. {
  38598. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  38599. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  38600. };
  38601. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  38602. {
  38603. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  38604. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  38605. };
  38606. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  38607. {
  38608. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  38609. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  38610. };
  38611. #endif
  38612. #ifdef WOLFSSL_AES_192
  38613. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  38614. {
  38615. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  38616. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  38617. };
  38618. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  38619. {
  38620. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  38621. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  38622. };
  38623. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  38624. {
  38625. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  38626. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  38627. };
  38628. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  38629. {
  38630. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  38631. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  38632. };
  38633. #endif
  38634. #ifdef WOLFSSL_AES_256
  38635. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  38636. {
  38637. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  38638. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  38639. };
  38640. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  38641. {
  38642. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  38643. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  38644. };
  38645. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  38646. {
  38647. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  38648. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  38649. };
  38650. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  38651. {
  38652. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  38653. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  38654. };
  38655. #endif
  38656. const CMAC_Test_Case testCases[] =
  38657. {
  38658. #ifdef WOLFSSL_AES_128
  38659. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  38660. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  38661. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  38662. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  38663. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  38664. #endif
  38665. #ifdef WOLFSSL_AES_192
  38666. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  38667. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  38668. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  38669. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  38670. #endif
  38671. #ifdef WOLFSSL_AES_256
  38672. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  38673. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  38674. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  38675. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  38676. #endif
  38677. #ifdef WOLFSSL_AES_128
  38678. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  38679. #endif
  38680. };
  38681. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38682. Cmac *cmac;
  38683. #else
  38684. Cmac cmac[1];
  38685. #endif
  38686. byte tag[AES_BLOCK_SIZE];
  38687. const CMAC_Test_Case* tc;
  38688. word32 i, tagSz;
  38689. wc_test_ret_t ret;
  38690. WOLFSSL_ENTER("cmac_test");
  38691. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38692. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  38693. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38694. #endif
  38695. for (i = 0, tc = testCases;
  38696. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  38697. i++, tc++) {
  38698. XMEMSET(tag, 0, sizeof(tag));
  38699. tagSz = AES_BLOCK_SIZE;
  38700. #if !defined(HAVE_FIPS) || \
  38701. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  38702. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  38703. #else
  38704. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  38705. #endif
  38706. if (ret != 0)
  38707. {
  38708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38709. }
  38710. if (tc->partial) {
  38711. ret = wc_CmacUpdate(cmac, tc->m,
  38712. tc->mSz/2 - tc->partial);
  38713. if (ret != 0)
  38714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38715. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  38716. tc->mSz/2 + tc->partial);
  38717. if (ret != 0)
  38718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38719. }
  38720. else {
  38721. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  38722. if (ret != 0)
  38723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38724. }
  38725. ret = wc_CmacFinal(cmac, tag, &tagSz);
  38726. if (ret != 0)
  38727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38728. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  38729. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38730. XMEMSET(tag, 0, sizeof(tag));
  38731. tagSz = sizeof(tag);
  38732. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38733. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  38734. tc->k, tc->kSz, NULL, devId);
  38735. #else
  38736. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  38737. tc->k, tc->kSz);
  38738. #endif
  38739. if (ret != 0)
  38740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38741. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  38742. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38743. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38744. ret = wc_AesCmacVerify_ex(cmac, tc->t, tc->tSz, tc->m, tc->mSz,
  38745. tc->k, tc->kSz, HEAP_HINT, devId);
  38746. #else
  38747. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  38748. tc->k, tc->kSz);
  38749. #endif
  38750. if (ret != 0)
  38751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38752. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38753. /* Test that keyless generate with init is the same */
  38754. XMEMSET(tag, 0, sizeof(tag));
  38755. tagSz = sizeof(tag);
  38756. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  38757. if (ret != 0) {
  38758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38759. }
  38760. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  38761. NULL, 0, HEAP_HINT, devId);
  38762. if (ret != 0) {
  38763. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38764. }
  38765. #endif
  38766. }
  38767. ret = 0;
  38768. out:
  38769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38770. if (cmac)
  38771. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  38772. #endif
  38773. return ret;
  38774. }
  38775. #endif /* NO_AES && WOLFSSL_CMAC */
  38776. #if defined(WOLFSSL_SIPHASH)
  38777. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  38778. /* Test vectors from:
  38779. * https://github.com/veorq/SipHash/blob/master/vectors.h
  38780. */
  38781. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  38782. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38783. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  38784. };
  38785. static const unsigned char siphash_msg[64] = {
  38786. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38787. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  38788. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  38789. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  38790. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  38791. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  38792. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  38793. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  38794. };
  38795. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  38796. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  38797. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  38798. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  38799. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  38800. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  38801. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  38802. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  38803. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  38804. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  38805. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  38806. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  38807. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  38808. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  38809. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  38810. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  38811. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  38812. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  38813. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  38814. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  38815. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  38816. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  38817. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  38818. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  38819. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  38820. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  38821. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  38822. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  38823. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  38824. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  38825. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  38826. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  38827. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  38828. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  38829. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  38830. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  38831. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  38832. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  38833. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  38834. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  38835. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  38836. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  38837. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  38838. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  38839. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  38840. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  38841. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  38842. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  38843. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  38844. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  38845. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  38846. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  38847. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  38848. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  38849. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  38850. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  38851. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  38852. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  38853. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  38854. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  38855. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  38856. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  38857. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  38858. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  38859. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  38860. };
  38861. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  38862. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  38863. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  38864. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  38865. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  38866. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  38867. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  38868. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  38869. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  38870. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  38871. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  38872. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  38873. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  38874. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  38875. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  38876. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  38877. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  38878. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  38879. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  38880. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  38881. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  38882. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  38883. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  38884. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  38885. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  38886. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  38887. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  38888. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  38889. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  38890. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  38891. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  38892. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  38893. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  38894. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  38895. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  38896. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  38897. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  38898. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  38899. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  38900. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  38901. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  38902. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  38903. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  38904. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  38905. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  38906. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  38907. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  38908. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  38909. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  38910. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  38911. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  38912. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  38913. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  38914. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  38915. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  38916. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  38917. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  38918. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  38919. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  38920. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  38921. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  38922. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  38923. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  38924. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  38925. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  38926. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  38927. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  38928. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  38929. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  38930. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  38931. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  38932. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  38933. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  38934. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  38935. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  38936. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  38937. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  38938. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  38939. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  38940. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  38941. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  38942. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  38943. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  38944. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  38945. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  38946. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  38947. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  38948. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  38949. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  38950. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  38951. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  38952. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  38953. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  38954. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  38955. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  38956. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  38957. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  38958. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  38959. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  38960. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  38961. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  38962. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  38963. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  38964. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  38965. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  38966. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  38967. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  38968. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  38969. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  38970. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  38971. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  38972. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  38973. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  38974. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  38975. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  38976. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  38977. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  38978. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  38979. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  38980. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  38981. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  38982. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  38983. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  38984. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  38985. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  38986. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  38987. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  38988. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  38989. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  38990. };
  38991. #endif
  38992. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  38993. {
  38994. wc_test_ret_t ret = 0;
  38995. int i;
  38996. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  38997. unsigned char res[SIPHASH_MAC_SIZE_16];
  38998. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  38999. SipHash siphash;
  39000. WOLFSSL_ENTER("siphash_test (1)");
  39001. for (i = 0; i < 64; i++) {
  39002. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39003. if (ret != 0)
  39004. return WC_TEST_RET_ENC_I(i);
  39005. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  39006. if (ret != 0)
  39007. return WC_TEST_RET_ENC_I(i);
  39008. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  39009. if (ret != 0)
  39010. return WC_TEST_RET_ENC_I(i);
  39011. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  39012. return WC_TEST_RET_ENC_I(i);
  39013. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_8);
  39014. if (ret != 0)
  39015. return WC_TEST_RET_ENC_I(i);
  39016. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  39017. return WC_TEST_RET_ENC_I(i);
  39018. }
  39019. for (i = 0; i < 64; i++) {
  39020. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  39021. if (ret != 0)
  39022. return WC_TEST_RET_ENC_I(i);
  39023. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  39024. if (ret != 0)
  39025. return WC_TEST_RET_ENC_I(i);
  39026. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  39027. if (ret != 0)
  39028. return WC_TEST_RET_ENC_I(i);
  39029. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  39030. return WC_TEST_RET_ENC_I(i);
  39031. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_16);
  39032. if (ret != 0)
  39033. return WC_TEST_RET_ENC_I(i);
  39034. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  39035. return WC_TEST_RET_ENC_I(i);
  39036. }
  39037. #else
  39038. WOLFSSL_ENTER("siphash_test (1)");
  39039. #endif
  39040. /* Testing bad parameters. */
  39041. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  39042. if (ret != BAD_FUNC_ARG)
  39043. return WC_TEST_RET_ENC_EC(ret);
  39044. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  39045. if (ret != BAD_FUNC_ARG)
  39046. return WC_TEST_RET_ENC_EC(ret);
  39047. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  39048. if (ret != BAD_FUNC_ARG)
  39049. return WC_TEST_RET_ENC_EC(ret);
  39050. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  39051. if (ret != BAD_FUNC_ARG)
  39052. return WC_TEST_RET_ENC_EC(ret);
  39053. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39054. if (ret != 0)
  39055. return WC_TEST_RET_ENC_EC(ret);
  39056. ret = wc_SipHashUpdate(NULL, NULL, 0);
  39057. if (ret != BAD_FUNC_ARG)
  39058. return WC_TEST_RET_ENC_EC(ret);
  39059. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  39060. if (ret != BAD_FUNC_ARG)
  39061. return WC_TEST_RET_ENC_EC(ret);
  39062. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  39063. if (ret != BAD_FUNC_ARG)
  39064. return WC_TEST_RET_ENC_EC(ret);
  39065. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  39066. if (ret != BAD_FUNC_ARG)
  39067. return WC_TEST_RET_ENC_EC(ret);
  39068. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  39069. if (ret != BAD_FUNC_ARG)
  39070. return WC_TEST_RET_ENC_EC(ret);
  39071. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  39072. if (ret != BAD_FUNC_ARG)
  39073. return WC_TEST_RET_ENC_EC(ret);
  39074. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  39075. if (ret != BAD_FUNC_ARG)
  39076. return WC_TEST_RET_ENC_EC(ret);
  39077. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  39078. if (ret != BAD_FUNC_ARG)
  39079. return WC_TEST_RET_ENC_EC(ret);
  39080. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  39081. if (ret != BAD_FUNC_ARG)
  39082. return WC_TEST_RET_ENC_EC(ret);
  39083. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  39084. if (ret != BAD_FUNC_ARG)
  39085. return WC_TEST_RET_ENC_EC(ret);
  39086. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  39087. if (ret != BAD_FUNC_ARG)
  39088. return WC_TEST_RET_ENC_EC(ret);
  39089. /* Test cache with multiple non blocksize bytes */
  39090. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39091. if (ret != 0)
  39092. return WC_TEST_RET_ENC_EC(ret);
  39093. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  39094. if (ret != 0)
  39095. return WC_TEST_RET_ENC_EC(ret);
  39096. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  39097. if (ret != 0)
  39098. return WC_TEST_RET_ENC_EC(ret);
  39099. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  39100. if (ret != 0)
  39101. return WC_TEST_RET_ENC_EC(ret);
  39102. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39103. if (ret != 0)
  39104. return WC_TEST_RET_ENC_EC(ret);
  39105. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  39106. if (ret != 0)
  39107. return WC_TEST_RET_ENC_EC(ret);
  39108. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  39109. if (ret != 0)
  39110. return WC_TEST_RET_ENC_EC(ret);
  39111. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  39112. return WC_TEST_RET_ENC_NC;
  39113. return 0;
  39114. }
  39115. #endif /* WOLFSSL_SIPHASH */
  39116. #ifdef HAVE_LIBZ
  39117. static const byte sample_text[] =
  39118. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  39119. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  39120. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  39121. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  39122. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  39123. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  39124. "\n"
  39125. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  39126. "four loko you probably haven't heard of them high life. Messenger bag\n"
  39127. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  39128. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  39129. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  39130. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  39131. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  39132. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  39133. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  39134. "four loko whatever street art yr farm-to-table.\n"
  39135. "\n"
  39136. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  39137. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  39138. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  39139. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  39140. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  39141. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  39142. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  39143. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  39144. "locavore.\n"
  39145. "\n"
  39146. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  39147. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  39148. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  39149. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  39150. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  39151. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  39152. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  39153. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  39154. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  39155. "neutra PBR selvage.\n"
  39156. "\n"
  39157. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  39158. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  39159. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  39160. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  39161. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  39162. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  39163. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  39164. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  39165. "mollit magna, sriracha sartorial helvetica.\n"
  39166. "\n"
  39167. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  39168. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  39169. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  39170. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  39171. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  39172. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  39173. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  39174. "\n"
  39175. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  39176. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  39177. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  39178. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  39179. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  39180. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  39181. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  39182. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  39183. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  39184. "\n"
  39185. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  39186. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  39187. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  39188. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  39189. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  39190. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  39191. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  39192. "bag dolor terry richardson sapiente.\n";
  39193. static const byte sample_text_gz[] = {
  39194. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  39195. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  39196. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  39197. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  39198. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  39199. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  39200. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  39201. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  39202. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  39203. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  39204. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  39205. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  39206. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  39207. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  39208. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  39209. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  39210. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  39211. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  39212. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  39213. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  39214. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  39215. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  39216. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  39217. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  39218. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  39219. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  39220. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  39221. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  39222. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  39223. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  39224. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  39225. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  39226. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  39227. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  39228. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  39229. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  39230. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  39231. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  39232. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  39233. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  39234. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  39235. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  39236. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  39237. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  39238. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  39239. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  39240. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  39241. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  39242. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  39243. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  39244. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  39245. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  39246. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  39247. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  39248. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  39249. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  39250. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  39251. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  39252. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  39253. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  39254. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  39255. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  39256. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  39257. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  39258. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  39259. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  39260. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  39261. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  39262. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  39263. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  39264. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  39265. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  39266. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  39267. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  39268. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  39269. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  39270. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  39271. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  39272. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  39273. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  39274. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  39275. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  39276. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  39277. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  39278. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  39279. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  39280. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  39281. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  39282. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  39283. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  39284. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  39285. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  39286. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  39287. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  39288. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  39289. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  39290. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  39291. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  39292. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  39293. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  39294. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  39295. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  39296. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  39297. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  39298. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  39299. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  39300. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  39301. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  39302. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  39303. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  39304. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  39305. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  39306. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  39307. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  39308. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  39309. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  39310. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  39311. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  39312. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  39313. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  39314. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  39315. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  39316. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  39317. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  39318. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  39319. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  39320. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  39321. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  39322. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  39323. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  39324. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  39325. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  39326. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  39327. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  39328. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  39329. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  39330. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  39331. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  39332. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  39333. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  39334. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  39335. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  39336. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  39337. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  39338. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  39339. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  39340. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  39341. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  39342. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  39343. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  39344. };
  39345. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  39346. {
  39347. wc_test_ret_t ret = 0;
  39348. word32 dSz = sizeof(sample_text);
  39349. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  39350. byte *c;
  39351. byte *d;
  39352. WOLFSSL_ENTER("compress_test");
  39353. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39354. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39355. if (c == NULL || d == NULL) {
  39356. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  39357. }
  39358. /* follow calloc and initialize to 0 */
  39359. XMEMSET(c, 0, cSz);
  39360. XMEMSET(d, 0, dSz);
  39361. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  39362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39363. }
  39364. cSz = (word32)ret;
  39365. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  39366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39367. }
  39368. dSz = (word32)ret;
  39369. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39370. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39371. }
  39372. /* GZIP tests */
  39373. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  39374. XMEMSET(c, 0, cSz);
  39375. XMEMSET(d, 0, dSz);
  39376. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  39377. if (ret < 0)
  39378. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39379. cSz = (word32)ret;
  39380. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  39381. if (ret < 0)
  39382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39383. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39384. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39385. }
  39386. /* Try with gzip generated output */
  39387. XMEMSET(d, 0, dSz);
  39388. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  39389. LIBZ_WINBITS_GZIP);
  39390. if (ret < 0)
  39391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39392. dSz = (word32)ret;
  39393. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39394. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39395. }
  39396. ret = 0; /* success */
  39397. exit:
  39398. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39399. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39400. return ret;
  39401. }
  39402. #endif /* HAVE_LIBZ */
  39403. #ifdef HAVE_PKCS7
  39404. /* External Debugging/Testing Note:
  39405. *
  39406. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  39407. * additional testing. To dump bundles to files DER encoded files, please
  39408. * define:
  39409. *
  39410. * #define PKCS7_OUTPUT_TEST_BUNDLES
  39411. */
  39412. /* Loads certs and keys for use with PKCS7 tests, from either files
  39413. * or buffers.
  39414. *
  39415. * rsaClientCertBuf - output buffer for RSA client cert
  39416. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  39417. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  39418. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  39419. *
  39420. * rsaServerCertBuf - output buffer for RSA server cert
  39421. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  39422. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  39423. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  39424. *
  39425. * rsaCaCertBuf - output buffer for RSA CA cert
  39426. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  39427. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  39428. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  39429. *
  39430. * eccClientCertBuf - output buffer for ECC cert
  39431. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  39432. * eccClientPrivKeyBuf - output buffer for ECC private key
  39433. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  39434. *
  39435. * Returns 0 on success, negative on error
  39436. */
  39437. static wc_test_ret_t pkcs7_load_certs_keys(
  39438. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  39439. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  39440. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  39441. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  39442. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  39443. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  39444. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  39445. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  39446. {
  39447. #ifndef NO_FILESYSTEM
  39448. XFILE certFile;
  39449. XFILE keyFile;
  39450. (void)certFile;
  39451. (void)keyFile;
  39452. #endif
  39453. #ifndef NO_RSA
  39454. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  39455. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  39456. return BAD_FUNC_ARG;
  39457. #endif
  39458. #ifdef HAVE_ECC
  39459. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  39460. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  39461. return BAD_FUNC_ARG;
  39462. #endif
  39463. /* RSA */
  39464. #ifndef NO_RSA
  39465. #ifdef USE_CERT_BUFFERS_1024
  39466. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  39467. return WC_TEST_RET_ENC_NC;
  39468. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  39469. sizeof_client_cert_der_1024);
  39470. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  39471. if (rsaServerCertBuf != NULL) {
  39472. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  39473. return WC_TEST_RET_ENC_NC;
  39474. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  39475. sizeof_server_cert_der_1024);
  39476. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  39477. }
  39478. if (rsaCaCertBuf != NULL) {
  39479. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  39480. return WC_TEST_RET_ENC_NC;
  39481. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  39482. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  39483. }
  39484. #elif defined(USE_CERT_BUFFERS_2048)
  39485. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  39486. return WC_TEST_RET_ENC_NC;
  39487. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  39488. sizeof_client_cert_der_2048);
  39489. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  39490. if (rsaServerCertBuf != NULL) {
  39491. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  39492. return WC_TEST_RET_ENC_NC;
  39493. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  39494. sizeof_server_cert_der_2048);
  39495. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  39496. }
  39497. if (rsaCaCertBuf != NULL) {
  39498. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  39499. return WC_TEST_RET_ENC_NC;
  39500. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  39501. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  39502. }
  39503. #else
  39504. certFile = XFOPEN(clientCert, "rb");
  39505. if (!certFile)
  39506. return WC_TEST_RET_ENC_ERRNO;
  39507. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  39508. *rsaClientCertBufSz, certFile);
  39509. XFCLOSE(certFile);
  39510. if (*rsaClientCertBufSz == 0)
  39511. return WC_TEST_RET_ENC_ERRNO;
  39512. if (rsaServerCertBuf != NULL) {
  39513. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  39514. if (!certFile)
  39515. return WC_TEST_RET_ENC_ERRNO;
  39516. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  39517. *rsaServerCertBufSz, certFile);
  39518. XFCLOSE(certFile);
  39519. if (*rsaServerCertBufSz == 0)
  39520. return WC_TEST_RET_ENC_ERRNO;
  39521. }
  39522. if (rsaCaCertBuf != NULL) {
  39523. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  39524. if (!certFile)
  39525. return WC_TEST_RET_ENC_ERRNO;
  39526. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  39527. certFile);
  39528. XFCLOSE(certFile);
  39529. if (*rsaCaCertBufSz == 0)
  39530. return WC_TEST_RET_ENC_ERRNO;
  39531. }
  39532. #endif
  39533. #ifdef USE_CERT_BUFFERS_1024
  39534. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  39535. return WC_TEST_RET_ENC_NC;
  39536. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  39537. sizeof_client_key_der_1024);
  39538. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  39539. if (rsaServerPrivKeyBuf != NULL) {
  39540. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  39541. return WC_TEST_RET_ENC_NC;
  39542. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  39543. sizeof_server_key_der_1024);
  39544. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  39545. }
  39546. if (rsaCaPrivKeyBuf != NULL) {
  39547. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  39548. return WC_TEST_RET_ENC_NC;
  39549. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  39550. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  39551. }
  39552. #elif defined(USE_CERT_BUFFERS_2048)
  39553. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  39554. return WC_TEST_RET_ENC_NC;
  39555. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  39556. sizeof_client_key_der_2048);
  39557. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  39558. if (rsaServerPrivKeyBuf != NULL) {
  39559. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  39560. return WC_TEST_RET_ENC_NC;
  39561. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  39562. sizeof_server_key_der_2048);
  39563. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  39564. }
  39565. if (rsaCaPrivKeyBuf != NULL) {
  39566. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  39567. return WC_TEST_RET_ENC_NC;
  39568. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  39569. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  39570. }
  39571. #else
  39572. keyFile = XFOPEN(clientKey, "rb");
  39573. if (!keyFile)
  39574. return WC_TEST_RET_ENC_ERRNO;
  39575. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  39576. *rsaClientPrivKeyBufSz, keyFile);
  39577. XFCLOSE(keyFile);
  39578. if (*rsaClientPrivKeyBufSz == 0)
  39579. return WC_TEST_RET_ENC_ERRNO;
  39580. if (rsaServerPrivKeyBuf != NULL) {
  39581. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  39582. if (!keyFile)
  39583. return WC_TEST_RET_ENC_ERRNO;
  39584. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  39585. *rsaServerPrivKeyBufSz, keyFile);
  39586. XFCLOSE(keyFile);
  39587. if (*rsaServerPrivKeyBufSz == 0)
  39588. return WC_TEST_RET_ENC_ERRNO;
  39589. }
  39590. if (rsaCaPrivKeyBuf != NULL) {
  39591. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  39592. if (!keyFile)
  39593. return WC_TEST_RET_ENC_ERRNO;
  39594. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  39595. *rsaCaPrivKeyBufSz, keyFile);
  39596. XFCLOSE(keyFile);
  39597. if (*rsaCaPrivKeyBufSz == 0)
  39598. return WC_TEST_RET_ENC_ERRNO;
  39599. }
  39600. #endif /* USE_CERT_BUFFERS */
  39601. #endif /* NO_RSA */
  39602. /* ECC */
  39603. #ifdef HAVE_ECC
  39604. #ifdef USE_CERT_BUFFERS_256
  39605. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  39606. return WC_TEST_RET_ENC_NC;
  39607. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  39608. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  39609. #else
  39610. certFile = XFOPEN(eccClientCert, "rb");
  39611. if (!certFile)
  39612. return WC_TEST_RET_ENC_ERRNO;
  39613. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  39614. *eccClientCertBufSz, certFile);
  39615. XFCLOSE(certFile);
  39616. if (*eccClientCertBufSz == 0)
  39617. return WC_TEST_RET_ENC_ERRNO;
  39618. #endif /* USE_CERT_BUFFERS_256 */
  39619. #ifdef USE_CERT_BUFFERS_256
  39620. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  39621. return WC_TEST_RET_ENC_NC;
  39622. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  39623. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  39624. #else
  39625. keyFile = XFOPEN(eccClientKey, "rb");
  39626. if (!keyFile)
  39627. return WC_TEST_RET_ENC_ERRNO;
  39628. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  39629. *eccClientPrivKeyBufSz, keyFile);
  39630. XFCLOSE(keyFile);
  39631. if (*eccClientPrivKeyBufSz == 0)
  39632. return WC_TEST_RET_ENC_ERRNO;
  39633. #endif /* USE_CERT_BUFFERS_256 */
  39634. #endif /* HAVE_ECC */
  39635. #ifdef NO_RSA
  39636. (void)rsaClientCertBuf;
  39637. (void)rsaClientCertBufSz;
  39638. (void)rsaClientPrivKeyBuf;
  39639. (void)rsaClientPrivKeyBufSz;
  39640. (void)rsaServerCertBuf;
  39641. (void)rsaServerCertBufSz;
  39642. (void)rsaServerPrivKeyBuf;
  39643. (void)rsaServerPrivKeyBufSz;
  39644. (void)rsaCaCertBuf;
  39645. (void)rsaCaCertBufSz;
  39646. (void)rsaCaPrivKeyBuf;
  39647. (void)rsaCaPrivKeyBufSz;
  39648. #endif
  39649. #ifndef HAVE_ECC
  39650. (void)eccClientCertBuf;
  39651. (void)eccClientCertBufSz;
  39652. (void)eccClientPrivKeyBuf;
  39653. (void)eccClientPrivKeyBufSz;
  39654. #endif
  39655. #ifndef NO_FILESYSTEM
  39656. (void)certFile;
  39657. (void)keyFile;
  39658. #endif
  39659. return 0;
  39660. }
  39661. typedef struct {
  39662. const byte* content;
  39663. word32 contentSz;
  39664. int contentOID;
  39665. int encryptOID;
  39666. int keyWrapOID;
  39667. int keyAgreeOID;
  39668. byte* cert;
  39669. size_t certSz;
  39670. byte* privateKey;
  39671. word32 privateKeySz;
  39672. byte* optionalUkm;
  39673. word32 optionalUkmSz;
  39674. int ktriOptions; /* KTRI options flags */
  39675. int kariOptions; /* KARI options flags */
  39676. /* KEKRI specific */
  39677. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  39678. word32 secretKeySz; /* size of secretKey, bytes */
  39679. const byte* secretKeyId; /* key identifier */
  39680. word32 secretKeyIdSz; /* size of key identifier, bytes */
  39681. void* timePtr; /* time_t pointer */
  39682. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  39683. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  39684. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  39685. word32 otherAttrSz; /* size of otherAttr, bytes */
  39686. int kekriOptions; /* KEKRI options flags */
  39687. /* PWRI specific */
  39688. const char* password;
  39689. word32 passwordSz;
  39690. const byte* salt;
  39691. word32 saltSz;
  39692. int kdfOID;
  39693. int hashOID;
  39694. int kdfIterations;
  39695. int pwriOptions; /* PWRI options flags */
  39696. /* ORI specific */
  39697. int isOri;
  39698. int oriOptions; /* ORI options flags */
  39699. const char* outFileName;
  39700. } pkcs7EnvelopedVector;
  39701. static const byte asnDataOid[] = {
  39702. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  39703. };
  39704. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  39705. * and giving wolfCrypt the value for oriOID and oriValue to place in
  39706. * OtherRecipientInfo.
  39707. *
  39708. * Returns 0 on success, negative upon error. */
  39709. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  39710. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  39711. void* ctx)
  39712. {
  39713. int i;
  39714. /* make sure buffers are large enough */
  39715. if (*oriValueSz < (2 + cekSz))
  39716. return WC_TEST_RET_ENC_NC;
  39717. if (*oriTypeSz < sizeof(asnDataOid))
  39718. return WC_TEST_RET_ENC_NC;
  39719. /* our simple encryption algorithm will be take the bitwise complement */
  39720. oriValue[0] = 0x04; /*ASN OCTET STRING */
  39721. oriValue[1] = (byte)cekSz; /* length */
  39722. for (i = 0; i < (int)cekSz; i++) {
  39723. oriValue[2 + i] = ~cek[i];
  39724. }
  39725. *oriValueSz = 2 + cekSz;
  39726. /* set oriType to ASN.1 encoded data OID */
  39727. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  39728. *oriTypeSz = sizeof(asnDataOid);
  39729. (void)pkcs7;
  39730. (void)ctx;
  39731. return 0;
  39732. }
  39733. /* ORI decrypt callback, responsible for providing a decrypted content
  39734. * encryption key (CEK) placed into decryptedKey and size placed into
  39735. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  39736. * in decrypting the encrypted CEK.
  39737. *
  39738. * Returns 0 on success, negative upon error. */
  39739. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  39740. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  39741. word32* decryptedKeySz, void* ctx)
  39742. {
  39743. int i;
  39744. /* make sure oriType matches what we expect */
  39745. if (oriTypeSz != sizeof(asnDataOid))
  39746. return WC_TEST_RET_ENC_NC;
  39747. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  39748. return WC_TEST_RET_ENC_NC;
  39749. /* make sure decrypted buffer is large enough */
  39750. if (*decryptedKeySz < oriValueSz)
  39751. return WC_TEST_RET_ENC_NC;
  39752. /* decrypt encrypted CEK using simple bitwise complement,
  39753. only for example */
  39754. for (i = 0; i < (int)oriValueSz - 2; i++) {
  39755. decryptedKey[i] = ~oriValue[2 + i];
  39756. }
  39757. *decryptedKeySz = oriValueSz - 2;
  39758. (void)pkcs7;
  39759. (void)ctx;
  39760. return 0;
  39761. }
  39762. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39763. /* returns 0 on success */
  39764. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  39765. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  39766. byte* in, int inSz, byte* out, void* usrCtx)
  39767. {
  39768. wc_test_ret_t ret;
  39769. int keyId = -1, keySz;
  39770. word32 keyIdSz = 8;
  39771. const byte* key;
  39772. byte keyIdRaw[8];
  39773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39774. Aes *aes;
  39775. #else
  39776. Aes aes[1];
  39777. #endif
  39778. /* looking for KEY ID
  39779. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  39780. */
  39781. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  39782. /* 0x06, 0x0B do not pass in tag and length */
  39783. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  39784. 0x01, 0x09, 0x10, 0x02, 0x25
  39785. };
  39786. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  39787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39788. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39789. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39790. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39791. };
  39792. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  39793. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39794. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39795. };
  39796. /* test user context passed in */
  39797. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  39798. return WC_TEST_RET_ENC_NC;
  39799. }
  39800. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39801. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  39802. return WC_TEST_RET_ENC_ERRNO;
  39803. #endif
  39804. /* if needing to find keyIdSz can call with NULL */
  39805. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  39806. &keyIdSz);
  39807. if (ret != LENGTH_ONLY_E) {
  39808. printf("Unexpected error %d when getting keyIdSz\n", ret);
  39809. printf("Possibly no KEY ID attribute set\n");
  39810. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39811. }
  39812. else {
  39813. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  39814. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  39815. &keyIdSz);
  39816. if (ret < 0) {
  39817. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39818. }
  39819. if (keyIdSz < 3) {
  39820. printf("keyIdSz is smaller than expected\n");
  39821. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39822. }
  39823. if (keyIdSz > 2 + sizeof(int)) {
  39824. printf("example case was only expecting a keyId of int size\n");
  39825. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39826. }
  39827. /* keyIdRaw[0] OCTET TAG */
  39828. /* keyIdRaw[1] Length */
  39829. #ifdef BIG_ENDIAN_ORDER
  39830. if (keyIdRaw[1] == 0x01) {
  39831. keyId = 1;
  39832. }
  39833. #else
  39834. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  39835. #endif
  39836. }
  39837. /* Use keyID here if found to select key and decrypt in HSM or in this
  39838. * example just select key and do software decryption */
  39839. if (keyId == 1) {
  39840. key = altKey;
  39841. keySz = sizeof(altKey);
  39842. }
  39843. else {
  39844. key = defKey;
  39845. keySz = sizeof(defKey);
  39846. }
  39847. switch (encryptOID) {
  39848. #ifdef WOLFSSL_AES_256
  39849. case AES256CBCb:
  39850. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  39851. WARNING_OUT(BAD_FUNC_ARG, out);
  39852. break;
  39853. #endif
  39854. #ifdef WOLFSSL_AES_128
  39855. case AES128CBCb:
  39856. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  39857. ERROR_OUT(BAD_FUNC_ARG, out);
  39858. break;
  39859. #endif
  39860. default:
  39861. printf("Unsupported content cipher type for example");
  39862. ERROR_OUT(ALGO_ID_E, out);
  39863. };
  39864. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  39865. if (ret == 0) {
  39866. ret = wc_AesSetKey(aes, key, (word32)keySz, iv, AES_DECRYPTION);
  39867. if (ret == 0)
  39868. ret = wc_AesCbcDecrypt(aes, out, in, (word32)inSz);
  39869. wc_AesFree(aes);
  39870. }
  39871. out:
  39872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39873. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39874. #endif
  39875. (void)aad;
  39876. (void)aadSz;
  39877. (void)authTag;
  39878. (void)authTagSz;
  39879. return (int)ret;
  39880. }
  39881. #endif /* !NO_AES && HAVE_AES_CBC */
  39882. #define PKCS7_BUF_SIZE 2048
  39883. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  39884. byte* rsaPrivKey, word32 rsaPrivKeySz,
  39885. byte* eccCert, word32 eccCertSz,
  39886. byte* eccPrivKey, word32 eccPrivKeySz)
  39887. {
  39888. wc_test_ret_t ret = 0;
  39889. int testSz = 0, i;
  39890. int envelopedSz, decodedSz;
  39891. byte *enveloped = NULL;
  39892. byte *decoded = NULL;
  39893. PKCS7* pkcs7 = NULL;
  39894. #ifdef ECC_TIMING_RESISTANT
  39895. WC_RNG rng;
  39896. #endif
  39897. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39898. XFILE pkcs7File;
  39899. #endif
  39900. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39901. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39902. 0x72,0x6c,0x64
  39903. };
  39904. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  39905. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  39906. byte optionalUkm[] = {
  39907. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  39908. };
  39909. #endif /* NO_AES */
  39910. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  39911. !defined(NO_SHA)
  39912. /* encryption key for kekri recipient types */
  39913. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  39914. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39915. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  39916. };
  39917. /* encryption key identifier */
  39918. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  39919. 0x02,0x02,0x03,0x04
  39920. };
  39921. #endif
  39922. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  39923. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  39924. #ifndef HAVE_FIPS
  39925. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  39926. #else
  39927. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  39928. #endif
  39929. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  39930. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  39931. };
  39932. #endif
  39933. #define MAX_TESTVECTORS_LEN 13
  39934. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  39935. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  39936. if (testSz == MAX_TESTVECTORS_LEN) { \
  39937. ret = WC_TEST_RET_ENC_NC; \
  39938. goto out; \
  39939. } \
  39940. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  39941. }
  39942. pkcs7EnvelopedVector *testVectors = NULL;
  39943. #ifdef ECC_TIMING_RESISTANT
  39944. XMEMSET(&rng, 0, sizeof(rng));
  39945. #endif
  39946. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  39947. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39948. if (testVectors == NULL) {
  39949. ret = WC_TEST_RET_ENC_ERRNO;
  39950. goto out;
  39951. }
  39952. {
  39953. /* key transport key encryption technique */
  39954. #ifndef NO_RSA
  39955. #ifndef NO_DES3
  39956. ADD_PKCS7ENVELOPEDVECTOR(
  39957. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  39958. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  39959. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  39960. "pkcs7envelopedDataDES3.der");
  39961. #endif
  39962. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39963. #ifdef WOLFSSL_AES_128
  39964. ADD_PKCS7ENVELOPEDVECTOR(
  39965. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  39966. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  39967. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  39968. "pkcs7envelopedDataAES128CBC.der");
  39969. #endif
  39970. #ifdef WOLFSSL_AES_192
  39971. ADD_PKCS7ENVELOPEDVECTOR(
  39972. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  39973. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  39974. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  39975. "pkcs7envelopedDataAES192CBC.der");
  39976. #endif
  39977. #ifdef WOLFSSL_AES_256
  39978. ADD_PKCS7ENVELOPEDVECTOR(
  39979. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  39980. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  39981. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  39982. "pkcs7envelopedDataAES256CBC.der");
  39983. /* explicitly using SKID for SubjectKeyIdentifier */
  39984. ADD_PKCS7ENVELOPEDVECTOR(
  39985. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  39986. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  39987. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  39988. "pkcs7envelopedDataAES256CBC_SKID.der");
  39989. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  39990. ADD_PKCS7ENVELOPEDVECTOR(
  39991. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  39992. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  39993. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  39994. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  39995. #endif
  39996. #endif /* !NO_AES && HAVE_AES_CBC */
  39997. #endif
  39998. /* key agreement key encryption technique*/
  39999. #ifdef HAVE_ECC
  40000. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40001. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40002. ADD_PKCS7ENVELOPEDVECTOR(
  40003. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  40004. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40005. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40006. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40007. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  40008. #endif
  40009. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40010. ADD_PKCS7ENVELOPEDVECTOR(
  40011. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40012. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40013. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40014. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40015. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  40016. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  40017. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  40018. ADD_PKCS7ENVELOPEDVECTOR(
  40019. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40020. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40021. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40022. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40023. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  40024. /* with optional user keying material (ukm) */
  40025. ADD_PKCS7ENVELOPEDVECTOR(
  40026. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40027. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40028. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  40029. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40030. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  40031. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  40032. #endif /* !NO_AES && HAVE_AES_CBC */
  40033. #endif
  40034. /* kekri (KEKRecipientInfo) recipient types */
  40035. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40036. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40037. ADD_PKCS7ENVELOPEDVECTOR(
  40038. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  40039. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  40040. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  40041. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40042. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  40043. #endif
  40044. #endif /* !NO_AES && HAVE_AES_CBC */
  40045. /* pwri (PasswordRecipientInfo) recipient types */
  40046. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  40047. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40048. ADD_PKCS7ENVELOPEDVECTOR(
  40049. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  40050. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40051. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  40052. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  40053. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  40054. #endif
  40055. #endif
  40056. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  40057. /* ori (OtherRecipientInfo) recipient types */
  40058. ADD_PKCS7ENVELOPEDVECTOR(
  40059. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  40060. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  40061. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  40062. #endif
  40063. };
  40064. #undef MAX_TESTVECTORS_LEN
  40065. #undef ADD_PKCS7ENVELOPEDVECTOR
  40066. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40067. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40068. if ((! enveloped) || (! decoded)) {
  40069. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40070. }
  40071. #ifdef ECC_TIMING_RESISTANT
  40072. #ifndef HAVE_FIPS
  40073. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40074. #else
  40075. ret = wc_InitRng(&rng);
  40076. #endif
  40077. if (ret != 0)
  40078. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40079. #endif
  40080. for (i = 0; i < testSz; i++) {
  40081. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  40082. #ifdef WOLFSSL_ASYNC_CRYPT
  40083. INVALID_DEVID /* async PKCS7 is not supported */
  40084. #else
  40085. devId
  40086. #endif
  40087. );
  40088. if (pkcs7 == NULL) {
  40089. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40090. }
  40091. if (testVectors[i].secretKey != NULL) {
  40092. /* KEKRI recipient type */
  40093. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40094. if (ret != 0)
  40095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40096. pkcs7->content = (byte*)testVectors[i].content;
  40097. pkcs7->contentSz = testVectors[i].contentSz;
  40098. pkcs7->contentOID = testVectors[i].contentOID;
  40099. pkcs7->encryptOID = testVectors[i].encryptOID;
  40100. pkcs7->ukm = testVectors[i].optionalUkm;
  40101. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40102. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  40103. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  40104. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  40105. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  40106. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  40107. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  40108. if (ret < 0) {
  40109. wc_PKCS7_Free(pkcs7);
  40110. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40111. }
  40112. /* set key, for decryption */
  40113. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  40114. testVectors[i].secretKeySz);
  40115. if (ret != 0) {
  40116. wc_PKCS7_Free(pkcs7);
  40117. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40118. }
  40119. } else if (testVectors[i].password != NULL) {
  40120. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  40121. /* PWRI recipient type */
  40122. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40123. if (ret != 0)
  40124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40125. pkcs7->content = (byte*)testVectors[i].content;
  40126. pkcs7->contentSz = testVectors[i].contentSz;
  40127. pkcs7->contentOID = testVectors[i].contentOID;
  40128. pkcs7->encryptOID = testVectors[i].encryptOID;
  40129. pkcs7->ukm = testVectors[i].optionalUkm;
  40130. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40131. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  40132. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  40133. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  40134. testVectors[i].kdfOID,
  40135. testVectors[i].hashOID, testVectors[i].kdfIterations,
  40136. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  40137. if (ret < 0) {
  40138. wc_PKCS7_Free(pkcs7);
  40139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40140. }
  40141. /* set password, for decryption */
  40142. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  40143. testVectors[i].passwordSz);
  40144. if (ret < 0) {
  40145. wc_PKCS7_Free(pkcs7);
  40146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40147. }
  40148. #endif /* ! NO_PWDBASED && ! NO_SHA */
  40149. } else if (testVectors[i].isOri == 1) {
  40150. /* ORI recipient type */
  40151. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40152. if (ret != 0)
  40153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40154. pkcs7->content = (byte*)testVectors[i].content;
  40155. pkcs7->contentSz = testVectors[i].contentSz;
  40156. pkcs7->contentOID = testVectors[i].contentOID;
  40157. pkcs7->encryptOID = testVectors[i].encryptOID;
  40158. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  40159. testVectors[i].oriOptions);
  40160. if (ret < 0) {
  40161. wc_PKCS7_Free(pkcs7);
  40162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40163. }
  40164. /* set decrypt callback for decryption */
  40165. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  40166. if (ret < 0) {
  40167. wc_PKCS7_Free(pkcs7);
  40168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40169. }
  40170. } else {
  40171. /* KTRI or KARI recipient types */
  40172. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40173. if (ret != 0)
  40174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40175. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  40176. (word32)testVectors[i].certSz);
  40177. if (ret != 0) {
  40178. wc_PKCS7_Free(pkcs7);
  40179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40180. }
  40181. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  40182. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  40183. pkcs7->privateKey = testVectors[i].privateKey;
  40184. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  40185. pkcs7->content = (byte*)testVectors[i].content;
  40186. pkcs7->contentSz = testVectors[i].contentSz;
  40187. pkcs7->contentOID = testVectors[i].contentOID;
  40188. pkcs7->encryptOID = testVectors[i].encryptOID;
  40189. pkcs7->ukm = testVectors[i].optionalUkm;
  40190. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40191. /* set SubjectIdentifier type for KTRI types */
  40192. if (testVectors[i].ktriOptions & CMS_SKID) {
  40193. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  40194. if (ret != 0) {
  40195. wc_PKCS7_Free(pkcs7);
  40196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40197. }
  40198. } else if (testVectors[i].ktriOptions &
  40199. CMS_ISSUER_AND_SERIAL_NUMBER) {
  40200. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  40201. CMS_ISSUER_AND_SERIAL_NUMBER);
  40202. if (ret != 0) {
  40203. wc_PKCS7_Free(pkcs7);
  40204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40205. }
  40206. }
  40207. }
  40208. #ifdef ECC_TIMING_RESISTANT
  40209. pkcs7->rng = &rng;
  40210. #endif
  40211. /* encode envelopedData */
  40212. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  40213. PKCS7_BUF_SIZE);
  40214. if (envelopedSz <= 0) {
  40215. wc_PKCS7_Free(pkcs7);
  40216. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  40217. }
  40218. /* decode envelopedData */
  40219. pkcs7->contentOID = 0;
  40220. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, (word32)envelopedSz,
  40221. decoded, PKCS7_BUF_SIZE);
  40222. if (pkcs7->contentOID != testVectors[i].contentOID ||
  40223. decodedSz <= 0) {
  40224. wc_PKCS7_Free(pkcs7);
  40225. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40226. }
  40227. /* test decode result */
  40228. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  40229. wc_PKCS7_Free(pkcs7);
  40230. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40231. }
  40232. #ifndef NO_PKCS7_STREAM
  40233. { /* test reading byte by byte */
  40234. int z;
  40235. for (z = 0; z < envelopedSz; z++) {
  40236. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  40237. decoded, PKCS7_BUF_SIZE);
  40238. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  40239. printf("unexpected error %d\n", decodedSz);
  40240. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40241. }
  40242. }
  40243. /* test decode result */
  40244. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  40245. printf("stream read compare failed\n");
  40246. wc_PKCS7_Free(pkcs7);
  40247. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40248. }
  40249. }
  40250. #endif
  40251. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40252. /* output pkcs7 envelopedData for external testing */
  40253. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  40254. if (!pkcs7File) {
  40255. wc_PKCS7_Free(pkcs7);
  40256. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40257. }
  40258. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  40259. XFCLOSE(pkcs7File);
  40260. if (ret != envelopedSz) {
  40261. wc_PKCS7_Free(pkcs7);
  40262. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40263. } else {
  40264. /* reset ret to 0 for success */
  40265. ret = 0;
  40266. }
  40267. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  40268. wc_PKCS7_Free(pkcs7);
  40269. pkcs7 = NULL;
  40270. }
  40271. #ifdef ECC_TIMING_RESISTANT
  40272. wc_FreeRng(&rng);
  40273. #endif
  40274. (void)eccCert;
  40275. (void)eccCertSz;
  40276. (void)eccPrivKey;
  40277. (void)eccPrivKeySz;
  40278. (void)rsaCert;
  40279. (void)rsaCertSz;
  40280. (void)rsaPrivKey;
  40281. (void)rsaPrivKeySz;
  40282. out:
  40283. if (testVectors)
  40284. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40285. if (enveloped)
  40286. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40287. if (decoded)
  40288. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40289. return ret;
  40290. }
  40291. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  40292. {
  40293. wc_test_ret_t ret = 0;
  40294. byte* rsaCert = NULL;
  40295. byte* rsaPrivKey = NULL;
  40296. word32 rsaCertSz = 0;
  40297. word32 rsaPrivKeySz = 0;
  40298. byte* eccCert = NULL;
  40299. byte* eccPrivKey = NULL;
  40300. word32 eccCertSz = 0;
  40301. word32 eccPrivKeySz = 0;
  40302. WOLFSSL_ENTER("pkcs7enveloped_test");
  40303. #ifndef NO_RSA
  40304. /* read client RSA cert and key in DER format */
  40305. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40306. if (rsaCert == NULL)
  40307. return WC_TEST_RET_ENC_ERRNO;
  40308. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40309. if (rsaPrivKey == NULL) {
  40310. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40311. return WC_TEST_RET_ENC_NC;
  40312. }
  40313. rsaCertSz = FOURK_BUF;
  40314. rsaPrivKeySz = FOURK_BUF;
  40315. #endif /* NO_RSA */
  40316. #ifdef HAVE_ECC
  40317. /* read client ECC cert and key in DER format */
  40318. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40319. if (eccCert == NULL) {
  40320. #ifndef NO_RSA
  40321. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40322. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40323. #endif
  40324. return WC_TEST_RET_ENC_NC;
  40325. }
  40326. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40327. if (eccPrivKey == NULL) {
  40328. #ifndef NO_RSA
  40329. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40330. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40331. #endif
  40332. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40333. return WC_TEST_RET_ENC_NC;
  40334. }
  40335. eccCertSz = FOURK_BUF;
  40336. eccPrivKeySz = FOURK_BUF;
  40337. #endif /* HAVE_ECC */
  40338. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  40339. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  40340. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  40341. eccPrivKey, &eccPrivKeySz);
  40342. if (ret < 0) {
  40343. #ifndef NO_RSA
  40344. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40345. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40346. #endif
  40347. #ifdef HAVE_ECC
  40348. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40349. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40350. #endif
  40351. return WC_TEST_RET_ENC_EC(ret);
  40352. }
  40353. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  40354. rsaPrivKey, (word32)rsaPrivKeySz,
  40355. eccCert, (word32)eccCertSz,
  40356. eccPrivKey, (word32)eccPrivKeySz);
  40357. #ifndef NO_RSA
  40358. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40359. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40360. #endif
  40361. #ifdef HAVE_ECC
  40362. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40363. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40364. #endif
  40365. return ret;
  40366. }
  40367. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  40368. typedef struct {
  40369. const byte* content;
  40370. word32 contentSz;
  40371. int contentOID;
  40372. int encryptOID;
  40373. int keyWrapOID;
  40374. int keyAgreeOID;
  40375. byte* cert;
  40376. size_t certSz;
  40377. byte* privateKey;
  40378. word32 privateKeySz;
  40379. PKCS7Attrib* authAttribs;
  40380. word32 authAttribsSz;
  40381. PKCS7Attrib* unauthAttribs;
  40382. word32 unauthAttribsSz;
  40383. /* KARI / KTRI specific */
  40384. byte* optionalUkm;
  40385. word32 optionalUkmSz;
  40386. int ktriOptions; /* KTRI options flags */
  40387. int kariOptions; /* KARI options flags */
  40388. /* KEKRI specific */
  40389. byte* secretKey; /* key, only for kekri RecipientInfo types */
  40390. word32 secretKeySz; /* size of secretKey, bytes */
  40391. byte* secretKeyId; /* key identifier */
  40392. word32 secretKeyIdSz; /* size of key identifier, bytes */
  40393. void* timePtr; /* time_t pointer */
  40394. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  40395. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  40396. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  40397. word32 otherAttrSz; /* size of otherAttr, bytes */
  40398. int kekriOptions; /* KEKRI options flags */
  40399. /* PWRI specific */
  40400. char* password; /* password */
  40401. word32 passwordSz; /* password size, bytes */
  40402. byte* salt; /* KDF salt */
  40403. word32 saltSz; /* KDF salt size, bytes */
  40404. int kdfOID; /* KDF OID */
  40405. int hashOID; /* KDF hash algorithm OID */
  40406. int kdfIterations; /* KDF iterations */
  40407. int kekEncryptOID; /* KEK encryption algorithm OID */
  40408. int pwriOptions; /* PWRI options flags */
  40409. /* ORI specific */
  40410. int isOri;
  40411. int oriOptions; /* ORI options flags */
  40412. const char* outFileName;
  40413. } pkcs7AuthEnvelopedVector;
  40414. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  40415. byte* rsaPrivKey, word32 rsaPrivKeySz,
  40416. byte* eccCert, word32 eccCertSz,
  40417. byte* eccPrivKey, word32 eccPrivKeySz)
  40418. {
  40419. wc_test_ret_t ret = 0;
  40420. int testSz = 0, i;
  40421. int envelopedSz, decodedSz;
  40422. byte *enveloped = NULL;
  40423. byte *decoded = NULL;
  40424. WC_RNG rng;
  40425. PKCS7* pkcs7;
  40426. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40427. XFILE pkcs7File;
  40428. #endif
  40429. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  40430. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  40431. 0x72,0x6c,0x64
  40432. };
  40433. byte senderNonce[PKCS7_NONCE_SZ + 2];
  40434. #ifdef HAVE_ECC
  40435. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40436. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40437. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  40438. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  40439. 0x09, 0x05 };
  40440. PKCS7Attrib attribs[] =
  40441. {
  40442. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  40443. sizeof(senderNonce) }
  40444. };
  40445. #endif
  40446. #endif
  40447. #endif
  40448. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  40449. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  40450. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  40451. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  40452. };
  40453. #endif /* NO_AES */
  40454. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40455. /* encryption key for kekri recipient types */
  40456. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  40457. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  40458. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  40459. };
  40460. /* encryption key identifier */
  40461. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  40462. 0x02,0x02,0x03,0x04
  40463. };
  40464. #endif
  40465. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  40466. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  40467. #ifndef HAVE_FIPS
  40468. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  40469. #else
  40470. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  40471. #endif
  40472. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  40473. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  40474. };
  40475. #endif
  40476. #define MAX_TESTVECTORS_LEN 20
  40477. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  40478. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  40479. if (testSz == MAX_TESTVECTORS_LEN) { \
  40480. ret = WC_TEST_RET_ENC_NC; \
  40481. goto out; \
  40482. } \
  40483. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  40484. sizeof _this_vector); \
  40485. }
  40486. pkcs7AuthEnvelopedVector *testVectors = NULL;
  40487. XMEMSET(&rng, 0, sizeof(rng));
  40488. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  40489. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40490. if (testVectors == NULL) {
  40491. ret = WC_TEST_RET_ENC_ERRNO;
  40492. goto out;
  40493. }
  40494. {
  40495. /* key transport key encryption technique */
  40496. #ifndef NO_RSA
  40497. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40498. #ifdef WOLFSSL_AES_128
  40499. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40500. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  40501. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40502. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40503. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  40504. #endif
  40505. #ifdef WOLFSSL_AES_192
  40506. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40507. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  40508. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40509. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40510. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  40511. #endif
  40512. #ifdef WOLFSSL_AES_256
  40513. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40514. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40515. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40516. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40517. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  40518. /* test with contentType set to FirmwarePkgData */
  40519. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40520. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  40521. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  40522. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  40523. 0, 0, 0, 0, 0, 0, 0, 0,
  40524. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  40525. /* explicitly using SKID for SubjectKeyIdentifier */
  40526. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40527. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40528. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  40529. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  40530. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  40531. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  40532. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40533. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40534. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  40535. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  40536. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40537. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  40538. #endif
  40539. #else /* NO_AES || !HAVE_AESGCM */
  40540. (void)rsaCert;
  40541. (void)rsaCertSz;
  40542. (void)rsaPrivKey;
  40543. (void)rsaPrivKeySz;
  40544. #endif /* NO_AES || !HAVE_AESGCM */
  40545. #endif
  40546. /* key agreement key encryption technique*/
  40547. #ifdef HAVE_ECC
  40548. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40549. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40550. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40551. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  40552. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40553. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  40554. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40555. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  40556. #endif
  40557. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40558. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40559. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40560. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40561. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  40562. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40563. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  40564. /* with authenticated attributes */
  40565. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40566. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40567. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40568. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40569. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40570. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  40571. 0, 0, 0,
  40572. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  40573. /* with unauthenticated attributes */
  40574. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40575. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40576. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40577. eccPrivKeySz, NULL, 0, attribs,
  40578. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  40579. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  40580. 0, 0, 0,
  40581. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  40582. /* with authenticated AND unauthenticated attributes */
  40583. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40584. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40585. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40586. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40587. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  40588. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40589. 0, 0, 0, 0, 0, 0,
  40590. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  40591. /* with authenticated AND unauthenticated attributes AND
  40592. * contentType of FirmwarePkgData */
  40593. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40594. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  40595. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40596. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40597. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  40598. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40599. 0, 0, 0, 0, 0, 0,
  40600. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  40601. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  40602. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  40603. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40604. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40605. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40606. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  40607. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40608. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  40609. /* with optional user keying material (ukm) */
  40610. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40611. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40612. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40613. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  40614. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40615. 0, 0, 0, 0, 0, 0,
  40616. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  40617. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  40618. #endif /* NO_AES */
  40619. #endif
  40620. /* kekri (KEKRecipientInfo) recipient types */
  40621. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40622. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40623. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40624. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  40625. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  40626. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  40627. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40628. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  40629. #endif
  40630. #endif
  40631. /* pwri (PasswordRecipientInfo) recipient types */
  40632. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  40633. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  40634. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40635. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  40636. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40637. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  40638. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  40639. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  40640. #endif
  40641. #endif
  40642. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40643. #ifdef WOLFSSL_AES_128
  40644. /* ori (OtherRecipientInfo) recipient types */
  40645. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40646. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  40647. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  40648. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  40649. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  40650. #endif
  40651. #endif
  40652. }
  40653. #undef MAX_TESTVECTORS_LEN
  40654. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  40655. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40656. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40657. if ((! enveloped) || (! decoded)) {
  40658. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40659. }
  40660. /* generate senderNonce */
  40661. {
  40662. #ifndef HAVE_FIPS
  40663. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40664. #else
  40665. ret = wc_InitRng(&rng);
  40666. #endif
  40667. if (ret != 0)
  40668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40669. senderNonce[0] = 0x04;
  40670. senderNonce[1] = PKCS7_NONCE_SZ;
  40671. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  40672. if (ret != 0) {
  40673. wc_FreeRng(&rng);
  40674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40675. }
  40676. }
  40677. for (i = 0; i < testSz; i++) {
  40678. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  40679. #ifdef WOLFSSL_ASYNC_CRYPT
  40680. INVALID_DEVID /* async PKCS7 is not supported */
  40681. #else
  40682. devId
  40683. #endif
  40684. );
  40685. if (pkcs7 == NULL) {
  40686. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40687. }
  40688. if (testVectors[i].secretKey != NULL) {
  40689. /* KEKRI recipient type */
  40690. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40691. if (ret != 0)
  40692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40693. pkcs7->content = (byte*)testVectors[i].content;
  40694. pkcs7->contentSz = testVectors[i].contentSz;
  40695. pkcs7->contentOID = testVectors[i].contentOID;
  40696. pkcs7->encryptOID = testVectors[i].encryptOID;
  40697. pkcs7->ukm = testVectors[i].optionalUkm;
  40698. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40699. pkcs7->authAttribs = testVectors[i].authAttribs;
  40700. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40701. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40702. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40703. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  40704. testVectors[i].secretKey, testVectors[i].secretKeySz,
  40705. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  40706. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  40707. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  40708. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  40709. if (ret < 0) {
  40710. wc_PKCS7_Free(pkcs7);
  40711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40712. }
  40713. /* set key, for decryption */
  40714. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  40715. testVectors[i].secretKeySz);
  40716. if (ret != 0) {
  40717. wc_PKCS7_Free(pkcs7);
  40718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40719. }
  40720. } else if (testVectors[i].password != NULL) {
  40721. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  40722. /* PWRI recipient type */
  40723. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40724. if (ret != 0)
  40725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40726. pkcs7->content = (byte*)testVectors[i].content;
  40727. pkcs7->contentSz = testVectors[i].contentSz;
  40728. pkcs7->contentOID = testVectors[i].contentOID;
  40729. pkcs7->encryptOID = testVectors[i].encryptOID;
  40730. pkcs7->ukm = testVectors[i].optionalUkm;
  40731. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40732. pkcs7->authAttribs = testVectors[i].authAttribs;
  40733. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40734. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40735. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40736. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  40737. (byte*)testVectors[i].password,
  40738. testVectors[i].passwordSz, testVectors[i].salt,
  40739. testVectors[i].saltSz, testVectors[i].kdfOID,
  40740. testVectors[i].hashOID, testVectors[i].kdfIterations,
  40741. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  40742. if (ret < 0) {
  40743. wc_PKCS7_Free(pkcs7);
  40744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40745. }
  40746. /* set password, for decryption */
  40747. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  40748. testVectors[i].passwordSz);
  40749. if (ret < 0) {
  40750. wc_PKCS7_Free(pkcs7);
  40751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40752. }
  40753. #endif /* ! NO_PWDBASED && ! NO_SHA */
  40754. } else if (testVectors[i].isOri == 1) {
  40755. /* ORI recipient type */
  40756. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40757. if (ret != 0)
  40758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40759. pkcs7->content = (byte*)testVectors[i].content;
  40760. pkcs7->contentSz = testVectors[i].contentSz;
  40761. pkcs7->contentOID = testVectors[i].contentOID;
  40762. pkcs7->encryptOID = testVectors[i].encryptOID;
  40763. pkcs7->authAttribs = testVectors[i].authAttribs;
  40764. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40765. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40766. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40767. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  40768. testVectors[i].oriOptions);
  40769. if (ret < 0) {
  40770. wc_PKCS7_Free(pkcs7);
  40771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40772. }
  40773. /* set decrypt callback for decryption */
  40774. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  40775. if (ret < 0) {
  40776. wc_PKCS7_Free(pkcs7);
  40777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40778. }
  40779. } else {
  40780. /* KTRI or KARI recipient types */
  40781. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  40782. (word32)testVectors[i].certSz);
  40783. if (ret != 0) {
  40784. wc_PKCS7_Free(pkcs7);
  40785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40786. }
  40787. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  40788. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  40789. pkcs7->privateKey = testVectors[i].privateKey;
  40790. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  40791. pkcs7->content = (byte*)testVectors[i].content;
  40792. pkcs7->contentSz = testVectors[i].contentSz;
  40793. pkcs7->contentOID = testVectors[i].contentOID;
  40794. pkcs7->encryptOID = testVectors[i].encryptOID;
  40795. pkcs7->ukm = testVectors[i].optionalUkm;
  40796. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40797. pkcs7->authAttribs = testVectors[i].authAttribs;
  40798. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40799. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40800. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40801. /* set SubjectIdentifier type for KTRI types */
  40802. if (testVectors[i].ktriOptions & CMS_SKID) {
  40803. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  40804. if (ret != 0) {
  40805. wc_PKCS7_Free(pkcs7);
  40806. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40807. }
  40808. } else if (testVectors[i].ktriOptions &
  40809. CMS_ISSUER_AND_SERIAL_NUMBER) {
  40810. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  40811. CMS_ISSUER_AND_SERIAL_NUMBER);
  40812. if (ret != 0) {
  40813. wc_PKCS7_Free(pkcs7);
  40814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40815. }
  40816. }
  40817. }
  40818. #ifdef ECC_TIMING_RESISTANT
  40819. pkcs7->rng = &rng;
  40820. #endif
  40821. /* encode envelopedData */
  40822. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  40823. PKCS7_BUF_SIZE);
  40824. if (envelopedSz <= 0) {
  40825. wc_PKCS7_Free(pkcs7);
  40826. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  40827. }
  40828. #ifndef NO_PKCS7_STREAM
  40829. { /* test reading byte by byte */
  40830. int z;
  40831. for (z = 0; z < envelopedSz; z++) {
  40832. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  40833. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  40834. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  40835. printf("unexpected error %d\n", decodedSz);
  40836. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40837. }
  40838. }
  40839. /* test decode result */
  40840. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  40841. printf("stream read compare failed\n");
  40842. wc_PKCS7_Free(pkcs7);
  40843. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40844. }
  40845. }
  40846. #endif
  40847. /* decode envelopedData */
  40848. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  40849. (word32)envelopedSz, decoded,
  40850. PKCS7_BUF_SIZE);
  40851. if (decodedSz <= 0) {
  40852. wc_PKCS7_Free(pkcs7);
  40853. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40854. }
  40855. /* test decode result */
  40856. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  40857. wc_PKCS7_Free(pkcs7);
  40858. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40859. }
  40860. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40861. /* output pkcs7 envelopedData for external testing */
  40862. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  40863. if (!pkcs7File) {
  40864. wc_PKCS7_Free(pkcs7);
  40865. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40866. }
  40867. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  40868. XFCLOSE(pkcs7File);
  40869. if (ret != envelopedSz) {
  40870. wc_PKCS7_Free(pkcs7);
  40871. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40872. } else {
  40873. /* reset ret to 0 for success */
  40874. ret = 0;
  40875. }
  40876. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  40877. wc_PKCS7_Free(pkcs7);
  40878. pkcs7 = NULL;
  40879. }
  40880. wc_FreeRng(&rng);
  40881. (void)eccCert;
  40882. (void)eccCertSz;
  40883. (void)eccPrivKey;
  40884. (void)eccPrivKeySz;
  40885. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40886. (void)secretKey;
  40887. (void)secretKeyId;
  40888. #endif
  40889. #ifdef NO_RSA
  40890. (void)rsaCert;
  40891. (void)rsaCertSz;
  40892. (void)rsaPrivKey;
  40893. (void)rsaPrivKeySz;
  40894. #endif
  40895. out:
  40896. if (testVectors)
  40897. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40898. if (enveloped)
  40899. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40900. if (decoded)
  40901. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40902. return ret;
  40903. }
  40904. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  40905. {
  40906. wc_test_ret_t ret = 0;
  40907. byte* rsaCert = NULL;
  40908. byte* rsaPrivKey = NULL;
  40909. word32 rsaCertSz = 0;
  40910. word32 rsaPrivKeySz = 0;
  40911. byte* eccCert = NULL;
  40912. byte* eccPrivKey = NULL;
  40913. word32 eccCertSz = 0;
  40914. word32 eccPrivKeySz = 0;
  40915. WOLFSSL_ENTER("pkcs7authenveloped_test");
  40916. #ifndef NO_RSA
  40917. /* read client RSA cert and key in DER format */
  40918. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40919. if (rsaCert == NULL)
  40920. return WC_TEST_RET_ENC_ERRNO;
  40921. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40922. if (rsaPrivKey == NULL) {
  40923. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40924. return WC_TEST_RET_ENC_NC;
  40925. }
  40926. rsaCertSz = FOURK_BUF;
  40927. rsaPrivKeySz = FOURK_BUF;
  40928. #endif /* NO_RSA */
  40929. #ifdef HAVE_ECC
  40930. /* read client ECC cert and key in DER format */
  40931. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40932. if (eccCert == NULL) {
  40933. #ifndef NO_RSA
  40934. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40935. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40936. #endif
  40937. return WC_TEST_RET_ENC_NC;
  40938. }
  40939. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40940. if (eccPrivKey == NULL) {
  40941. #ifndef NO_RSA
  40942. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40943. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40944. #endif
  40945. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40946. return WC_TEST_RET_ENC_NC;
  40947. }
  40948. eccCertSz = FOURK_BUF;
  40949. eccPrivKeySz = FOURK_BUF;
  40950. #endif /* HAVE_ECC */
  40951. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  40952. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  40953. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  40954. eccPrivKey, &eccPrivKeySz);
  40955. if (ret < 0) {
  40956. #ifndef NO_RSA
  40957. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40958. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40959. #endif
  40960. #ifdef HAVE_ECC
  40961. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40962. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40963. #endif
  40964. return WC_TEST_RET_ENC_EC(ret);
  40965. }
  40966. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  40967. rsaPrivKey, (word32)rsaPrivKeySz,
  40968. eccCert, (word32)eccCertSz,
  40969. eccPrivKey, (word32)eccPrivKeySz);
  40970. #ifndef NO_RSA
  40971. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40972. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40973. #endif
  40974. #ifdef HAVE_ECC
  40975. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40976. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40977. #endif
  40978. return ret;
  40979. }
  40980. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  40981. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40982. static const byte p7DefKey[] = {
  40983. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  40984. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  40985. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  40986. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  40987. };
  40988. static const byte p7AltKey[] = {
  40989. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  40990. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  40991. };
  40992. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  40993. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  40994. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  40995. {
  40996. wc_test_ret_t ret;
  40997. if (cek == NULL || out == NULL)
  40998. return BAD_FUNC_ARG;
  40999. /* test case sanity checks */
  41000. if (keyIdSz != 1) {
  41001. return WC_TEST_RET_ENC_NC;
  41002. }
  41003. if (keyId[0] != 0x00) {
  41004. return WC_TEST_RET_ENC_NC;
  41005. }
  41006. if (type != (int)PKCS7_KEKRI) {
  41007. return WC_TEST_RET_ENC_NC;
  41008. }
  41009. switch (keyWrapAlgo) {
  41010. case AES256_WRAP:
  41011. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  41012. out, outSz, NULL);
  41013. if (ret <= 0)
  41014. return (int)ret;
  41015. break;
  41016. default:
  41017. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  41018. return BAD_KEYWRAP_ALG_E;
  41019. };
  41020. (void)pkcs7;
  41021. (void)direction;
  41022. (void)orginKey; /* used with KAKRI */
  41023. (void)orginKeySz;
  41024. return (int)ret;
  41025. }
  41026. /* returns key size on success */
  41027. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  41028. {
  41029. wc_test_ret_t ret;
  41030. word32 atrSz;
  41031. byte atr[256];
  41032. /* Additionally can look for fwWrappedFirmwareKey
  41033. * 1.2.840.113529.1.9.16.1.16 */
  41034. const unsigned char fwWrappedFirmwareKey[] = {
  41035. /* 0x06, 0x0B */
  41036. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41037. 0x01, 0x09, 0x10, 0x02, 0x27
  41038. };
  41039. /* find keyID in fwWrappedFirmwareKey */
  41040. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  41041. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  41042. if (ret == LENGTH_ONLY_E) {
  41043. XMEMSET(atr, 0, sizeof(atr));
  41044. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  41045. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  41046. /* keyIdRaw[0] OCTET TAG */
  41047. /* keyIdRaw[1] Length */
  41048. if (ret > 0) {
  41049. PKCS7* envPkcs7;
  41050. envPkcs7 = wc_PKCS7_New(NULL, 0);
  41051. if (envPkcs7 == NULL) {
  41052. return MEMORY_E;
  41053. }
  41054. wc_PKCS7_Init(envPkcs7, NULL, 0);
  41055. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  41056. if (ret == 0) {
  41057. /* expecting FIRMWARE_PKG_DATA content */
  41058. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  41059. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  41060. key, keySz);
  41061. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  41062. /* the contentOID should have been set to the inner
  41063. * FIRMWARE_PKG_DATA content */
  41064. ret = BAD_STATE_E;
  41065. }
  41066. }
  41067. wc_PKCS7_Free(envPkcs7);
  41068. }
  41069. }
  41070. return ret;
  41071. }
  41072. /* create a KEKRI enveloped data
  41073. * return size on success */
  41074. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  41075. word32 outSz)
  41076. {
  41077. wc_test_ret_t ret;
  41078. PKCS7* pkcs7;
  41079. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  41080. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  41081. if (pkcs7 == NULL)
  41082. return WC_TEST_RET_ENC_ERRNO;
  41083. pkcs7->content = in;
  41084. pkcs7->contentSz = inSz;
  41085. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  41086. pkcs7->encryptOID = AES256CBCb;
  41087. pkcs7->ukm = NULL;
  41088. pkcs7->ukmSz = 0;
  41089. /* add recipient (KEKRI type) */
  41090. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  41091. sizeof(p7DefKey), (byte*)keyId,
  41092. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  41093. if (ret < 0) {
  41094. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  41095. wc_PKCS7_Free(pkcs7);
  41096. return WC_TEST_RET_ENC_EC(ret);
  41097. }
  41098. /* encode envelopedData, returns size */
  41099. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  41100. if (ret <= 0) {
  41101. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  41102. wc_PKCS7_Free(pkcs7);
  41103. return WC_TEST_RET_ENC_EC(ret);
  41104. }
  41105. wc_PKCS7_Free(pkcs7);
  41106. return ret;
  41107. }
  41108. /*
  41109. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  41110. * returns size of buffer output on success
  41111. */
  41112. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  41113. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  41114. byte* key, word32 keySz)
  41115. {
  41116. wc_test_ret_t ret;
  41117. int attribNum = 1;
  41118. PKCS7* pkcs7;
  41119. /* KEY ID
  41120. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  41121. */
  41122. const unsigned char fwDecryptKeyID[] = {
  41123. 0x06, 0x0B,
  41124. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41125. 0x01, 0x09, 0x10, 0x02, 0x25
  41126. };
  41127. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  41128. const unsigned char fwWrappedFirmwareKey[] = {
  41129. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41130. 0x01, 0x09, 0x10, 0x02, 0x27
  41131. };
  41132. byte keyID[] = { 0x04, 0x01, 0x00 };
  41133. byte env[256];
  41134. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  41135. PKCS7Attrib attribs[] =
  41136. {
  41137. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  41138. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  41139. };
  41140. keyID[2] = keyHint;
  41141. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  41142. if (keyHint == 0) {
  41143. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  41144. sizeof(env));
  41145. if (ret <= 0) {
  41146. return ret;
  41147. }
  41148. attribs[1].valueSz = (word32)ret;
  41149. attribNum++;
  41150. }
  41151. /* init PKCS7 */
  41152. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  41153. if (pkcs7 == NULL)
  41154. return WC_TEST_RET_ENC_ERRNO;
  41155. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  41156. if (ret != 0) {
  41157. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  41158. wc_PKCS7_Free(pkcs7);
  41159. return WC_TEST_RET_ENC_EC(ret);
  41160. }
  41161. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  41162. if (ret != 0) {
  41163. wc_PKCS7_Free(pkcs7);
  41164. return WC_TEST_RET_ENC_EC(ret);
  41165. }
  41166. /* encode Signed Encrypted FirmwarePkgData */
  41167. if (encryptKeySz == 16) {
  41168. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  41169. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  41170. (byte*)data, sizeof(data), NULL, 0,
  41171. attribs, (word32)attribNum, out, *outSz);
  41172. }
  41173. else {
  41174. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  41175. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  41176. (byte*)data, sizeof(data), NULL, 0,
  41177. attribs, (word32)attribNum, out, *outSz);
  41178. }
  41179. if (ret <= 0) {
  41180. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  41181. "ret = %d\n", ret);
  41182. wc_PKCS7_Free(pkcs7);
  41183. return WC_TEST_RET_ENC_EC(ret);
  41184. } else {
  41185. *outSz = (word32)ret;
  41186. }
  41187. wc_PKCS7_Free(pkcs7);
  41188. return ret;
  41189. }
  41190. /* test verification and decryption of PKCS7 bundle
  41191. * return 0 on success
  41192. */
  41193. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  41194. {
  41195. wc_test_ret_t ret = 0;
  41196. int usrCtx = 1; /* test value to pass as user context to callback */
  41197. PKCS7* pkcs7 = NULL;
  41198. byte* sid = NULL;
  41199. word32 sidSz;
  41200. byte key[256];
  41201. word32 keySz = sizeof(key);
  41202. byte *decoded = NULL;
  41203. int decodedSz = FOURK_BUF/2;
  41204. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  41205. #ifdef NO_SHA
  41206. #ifdef USE_CERT_BUFFERS_1024
  41207. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  41208. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  41209. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  41210. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  41211. #else
  41212. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  41213. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  41214. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  41215. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  41216. #endif
  41217. #else /* !NO_SHA */
  41218. #ifdef USE_CERT_BUFFERS_1024
  41219. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  41220. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  41221. 0x10, 0x69, 0x59, 0xec,
  41222. #else
  41223. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  41224. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  41225. 0xD7, 0x85, 0x65, 0xC0
  41226. #endif
  41227. #endif /* !NO_SHA */
  41228. };
  41229. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41230. if (decoded == NULL) {
  41231. ret = MEMORY_E;
  41232. goto out;
  41233. }
  41234. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  41235. if (pkcs7 == NULL) {
  41236. ret = MEMORY_E;
  41237. goto out;
  41238. }
  41239. /* Test verify */
  41240. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  41241. if (ret != 0)
  41242. goto out;
  41243. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  41244. if (ret != 0)
  41245. goto out;
  41246. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  41247. if (ret != 0)
  41248. goto out;
  41249. /* Get size of SID and print it out */
  41250. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  41251. if (ret != LENGTH_ONLY_E)
  41252. goto out;
  41253. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41254. if (sid == NULL) {
  41255. ret = MEMORY_E;
  41256. goto out;
  41257. }
  41258. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  41259. if (ret != 0)
  41260. goto out;
  41261. ret = XMEMCMP(sid, expectedSid, sidSz);
  41262. if (ret != 0) {
  41263. ret = PKCS7_NO_SIGNER_E; /* close enough */
  41264. goto out;
  41265. }
  41266. /* get expected fwWrappedFirmwareKey */
  41267. if (keyHint == 0) {
  41268. ret = getFirmwareKey(pkcs7, key, keySz);
  41269. if (ret < 0)
  41270. goto out;
  41271. pkcs7->encryptionKey = key;
  41272. pkcs7->encryptionKeySz = (word32)ret;
  41273. }
  41274. else {
  41275. decodedSz = PKCS7_BUF_SIZE;
  41276. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  41277. if (ret != 0)
  41278. goto out;
  41279. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  41280. if (ret != 0)
  41281. goto out;
  41282. }
  41283. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  41284. pkcs7->contentSz, decoded, (word32)decodedSz);
  41285. if (decodedSz < 0) {
  41286. ret = decodedSz;
  41287. goto out;
  41288. }
  41289. ret = 0;
  41290. out:
  41291. if (decoded)
  41292. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41293. if (pkcs7)
  41294. wc_PKCS7_Free(pkcs7);
  41295. if (sid)
  41296. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41297. return ret;
  41298. }
  41299. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  41300. {
  41301. wc_test_ret_t ret = 0;
  41302. word32 derSz;
  41303. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41304. WOLFSSL_ENTER("pkcs7callback_test");
  41305. if (! derBuf)
  41306. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41307. /* Doing default generation and verify */
  41308. derSz = FOURK_BUF;
  41309. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  41310. certSz, key, keySz);
  41311. if (ret <= 0) {
  41312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41313. }
  41314. ret = verifyBundle(derBuf, derSz, 0);
  41315. if (ret != 0)
  41316. ERROR_OUT(ret, out);
  41317. /* test choosing other key with keyID */
  41318. derSz = FOURK_BUF;
  41319. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  41320. cert, certSz, key, keySz);
  41321. if (ret <= 0) {
  41322. ERROR_OUT(ret, out);
  41323. }
  41324. ret = verifyBundle(derBuf, derSz, 1);
  41325. if (ret != 0)
  41326. ERROR_OUT(ret, out);
  41327. /* test fail case with wrong keyID */
  41328. derSz = FOURK_BUF;
  41329. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  41330. cert, certSz, key, keySz);
  41331. if (ret <= 0) {
  41332. ERROR_OUT(ret, out);
  41333. }
  41334. ret = verifyBundle(derBuf, derSz, 1);
  41335. if (ret == 0) {
  41336. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41337. }
  41338. ret = 0;
  41339. out:
  41340. if (derBuf)
  41341. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41342. return ret;
  41343. }
  41344. #endif /* !NO_AES && HAVE_AES_CBC */
  41345. #ifndef NO_PKCS7_ENCRYPTED_DATA
  41346. typedef struct {
  41347. const byte* content;
  41348. word32 contentSz;
  41349. int contentOID;
  41350. int encryptOID;
  41351. byte* encryptionKey;
  41352. word32 encryptionKeySz;
  41353. PKCS7Attrib* attribs;
  41354. word32 attribsSz;
  41355. const char* outFileName;
  41356. } pkcs7EncryptedVector;
  41357. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  41358. {
  41359. wc_test_ret_t ret = 0;
  41360. int i, testSz;
  41361. int encryptedSz, decodedSz, attribIdx;
  41362. PKCS7* pkcs7;
  41363. byte *encrypted;
  41364. byte *decoded;
  41365. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41366. XFILE pkcs7File;
  41367. #endif
  41368. PKCS7Attrib* expectedAttrib;
  41369. PKCS7DecodedAttrib* decodedAttrib;
  41370. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41371. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41372. 0x72,0x6c,0x64
  41373. };
  41374. #ifndef NO_DES3
  41375. byte desKey[] = {
  41376. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  41377. };
  41378. byte des3Key[] = {
  41379. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  41380. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  41381. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  41382. };
  41383. #endif
  41384. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41385. #ifdef WOLFSSL_AES_128
  41386. byte aes128Key[] = {
  41387. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41388. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41389. };
  41390. #endif
  41391. #ifdef WOLFSSL_AES_192
  41392. byte aes192Key[] = {
  41393. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41394. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41395. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41396. };
  41397. #endif
  41398. #ifdef WOLFSSL_AES_256
  41399. byte aes256Key[] = {
  41400. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41401. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41402. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41403. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41404. };
  41405. #endif
  41406. #ifdef WOLFSSL_AES_256
  41407. /* Attribute example from RFC 4134, Section 7.2
  41408. * OID = 1.2.5555
  41409. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  41410. static const byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  41411. static const byte genAttr[] = { 0x04, 47,
  41412. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  41413. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  41414. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  41415. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  41416. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  41417. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  41418. static const byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  41419. static const byte genAttr2[] = { 0x04, 47,
  41420. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  41421. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  41422. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  41423. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  41424. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  41425. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  41426. PKCS7Attrib attribs[] =
  41427. {
  41428. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  41429. };
  41430. PKCS7Attrib multiAttribs[] =
  41431. {
  41432. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  41433. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  41434. };
  41435. #endif
  41436. #endif /* NO_AES */
  41437. const pkcs7EncryptedVector testVectors[] =
  41438. {
  41439. #ifndef NO_DES3
  41440. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  41441. NULL, 0, "pkcs7encryptedDataDES3.der"},
  41442. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  41443. NULL, 0, "pkcs7encryptedDataDES.der"},
  41444. #endif /* NO_DES3 */
  41445. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41446. #ifdef WOLFSSL_AES_128
  41447. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  41448. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  41449. #endif
  41450. #ifdef WOLFSSL_AES_192
  41451. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  41452. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  41453. #endif
  41454. #ifdef WOLFSSL_AES_256
  41455. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41456. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  41457. /* test with optional unprotected attributes */
  41458. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41459. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41460. "pkcs7encryptedDataAES256CBC_attribs.der"},
  41461. /* test with multiple optional unprotected attributes */
  41462. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41463. sizeof(aes256Key), multiAttribs,
  41464. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  41465. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  41466. /* test with contentType set to FirmwarePkgData */
  41467. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  41468. sizeof(aes256Key), NULL, 0,
  41469. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  41470. #endif
  41471. #endif /* !NO_AES && HAVE_AES_CBC */
  41472. };
  41473. WOLFSSL_ENTER("pkcs7encrypted_test");
  41474. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41475. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41476. if ((! encrypted) || (! decoded)) {
  41477. ERROR_OUT(MEMORY_E, out);
  41478. }
  41479. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  41480. for (i = 0; i < testSz; i++) {
  41481. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  41482. if (pkcs7 == NULL) {
  41483. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41484. }
  41485. pkcs7->content = (byte*)testVectors[i].content;
  41486. pkcs7->contentSz = testVectors[i].contentSz;
  41487. pkcs7->contentOID = testVectors[i].contentOID;
  41488. pkcs7->encryptOID = testVectors[i].encryptOID;
  41489. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  41490. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  41491. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  41492. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  41493. /* encode encryptedData */
  41494. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  41495. PKCS7_BUF_SIZE);
  41496. if (encryptedSz <= 0) {
  41497. wc_PKCS7_Free(pkcs7);
  41498. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  41499. }
  41500. /* decode encryptedData */
  41501. #ifndef NO_PKCS7_STREAM
  41502. { /* test reading byte by byte */
  41503. int z;
  41504. for (z = 0; z < encryptedSz; z++) {
  41505. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  41506. decoded, PKCS7_BUF_SIZE);
  41507. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  41508. printf("unexpected error %d\n", decodedSz);
  41509. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41510. }
  41511. }
  41512. /* test decode result */
  41513. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  41514. printf("stream read failed\n");
  41515. wc_PKCS7_Free(pkcs7);
  41516. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41517. }
  41518. }
  41519. #endif
  41520. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  41521. decoded, PKCS7_BUF_SIZE);
  41522. if (decodedSz <= 0){
  41523. wc_PKCS7_Free(pkcs7);
  41524. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41525. }
  41526. /* test decode result */
  41527. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  41528. wc_PKCS7_Free(pkcs7);
  41529. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41530. }
  41531. /* verify decoded unprotected attributes */
  41532. if (pkcs7->decodedAttrib != NULL) {
  41533. decodedAttrib = pkcs7->decodedAttrib;
  41534. attribIdx = 1;
  41535. while (decodedAttrib != NULL) {
  41536. /* expected attribute, stored list is reversed */
  41537. expectedAttrib = &(pkcs7->unprotectedAttribs
  41538. [pkcs7->unprotectedAttribsSz - attribIdx]);
  41539. /* verify oid */
  41540. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  41541. decodedAttrib->oidSz) != 0) {
  41542. wc_PKCS7_Free(pkcs7);
  41543. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41544. }
  41545. /* verify value */
  41546. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  41547. decodedAttrib->valueSz) != 0) {
  41548. wc_PKCS7_Free(pkcs7);
  41549. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41550. }
  41551. decodedAttrib = decodedAttrib->next;
  41552. attribIdx++;
  41553. }
  41554. }
  41555. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41556. /* output pkcs7 envelopedData for external testing */
  41557. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  41558. if (!pkcs7File) {
  41559. wc_PKCS7_Free(pkcs7);
  41560. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41561. }
  41562. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  41563. if (ret < 0)
  41564. ret = WC_TEST_RET_ENC_ERRNO;
  41565. else
  41566. ret = 0;
  41567. XFCLOSE(pkcs7File);
  41568. #endif
  41569. wc_PKCS7_Free(pkcs7);
  41570. }
  41571. out:
  41572. if (encrypted)
  41573. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41574. if (decoded)
  41575. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41576. return ret;
  41577. }
  41578. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  41579. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  41580. typedef struct {
  41581. const byte* content;
  41582. word32 contentSz;
  41583. int contentOID;
  41584. const char* outFileName;
  41585. } pkcs7CompressedVector;
  41586. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  41587. {
  41588. wc_test_ret_t ret = 0;
  41589. int i, testSz;
  41590. int compressedSz, decodedSz;
  41591. PKCS7* pkcs7;
  41592. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41593. byte *compressed;
  41594. byte *decoded;
  41595. #else
  41596. byte compressed[PKCS7_BUF_SIZE];
  41597. byte decoded[PKCS7_BUF_SIZE];
  41598. #endif
  41599. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41600. XFILE pkcs7File;
  41601. #endif
  41602. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41603. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41604. 0x72,0x6c,0x64
  41605. };
  41606. const pkcs7CompressedVector testVectors[] =
  41607. {
  41608. {data, (word32)sizeof(data), DATA,
  41609. "pkcs7compressedData_data_zlib.der"},
  41610. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  41611. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  41612. };
  41613. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41614. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41615. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41616. if ((! compressed) || (! decoded)) {
  41617. ERROR_OUT(MEMORY_E, out);
  41618. }
  41619. #endif
  41620. WOLFSSL_ENTER("pkcs7compressed_test");
  41621. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  41622. for (i = 0; i < testSz; i++) {
  41623. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  41624. if (pkcs7 == NULL) {
  41625. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41626. }
  41627. pkcs7->content = (byte*)testVectors[i].content;
  41628. pkcs7->contentSz = testVectors[i].contentSz;
  41629. pkcs7->contentOID = testVectors[i].contentOID;
  41630. /* encode compressedData */
  41631. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  41632. PKCS7_BUF_SIZE);
  41633. if (compressedSz <= 0) {
  41634. wc_PKCS7_Free(pkcs7);
  41635. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  41636. }
  41637. /* decode compressedData */
  41638. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  41639. compressedSz, decoded,
  41640. PKCS7_BUF_SIZE);
  41641. if (decodedSz <= 0){
  41642. wc_PKCS7_Free(pkcs7);
  41643. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41644. }
  41645. /* test decode result */
  41646. if (XMEMCMP(decoded, testVectors[i].content,
  41647. testVectors[i].contentSz) != 0) {
  41648. wc_PKCS7_Free(pkcs7);
  41649. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41650. }
  41651. /* make sure content type is the same */
  41652. if (testVectors[i].contentOID != pkcs7->contentOID) {
  41653. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41654. }
  41655. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41656. /* output pkcs7 compressedData for external testing */
  41657. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  41658. if (!pkcs7File) {
  41659. wc_PKCS7_Free(pkcs7);
  41660. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41661. }
  41662. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  41663. if (ret < 0)
  41664. ret = WC_TEST_RET_ENC_ERRNO;
  41665. else
  41666. ret = 0;
  41667. XFCLOSE(pkcs7File);
  41668. #endif
  41669. wc_PKCS7_Free(pkcs7);
  41670. }
  41671. out:
  41672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41673. if (compressed)
  41674. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41675. if (decoded)
  41676. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41677. #endif
  41678. return ret;
  41679. } /* pkcs7compressed_test() */
  41680. #undef PKCS7_BUF_SIZE
  41681. #endif /* HAVE_LIBZ */
  41682. typedef struct {
  41683. const byte* content;
  41684. word32 contentSz;
  41685. int hashOID;
  41686. int signOID;
  41687. byte* privateKey;
  41688. word32 privateKeySz;
  41689. byte* cert;
  41690. size_t certSz;
  41691. byte* caCert;
  41692. size_t caCertSz;
  41693. PKCS7Attrib* signedAttribs;
  41694. word32 signedAttribsSz;
  41695. const char* outFileName;
  41696. int contentOID;
  41697. const byte* contentType;
  41698. word32 contentTypeSz;
  41699. int sidType;
  41700. int encryptOID; /* for single-shot encrypt alg OID */
  41701. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  41702. const byte* encryptKey; /* for single-shot, encryptedData */
  41703. word32 encryptKeySz; /* for single-shot, encryptedData */
  41704. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  41705. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  41706. word16 detachedSignature; /* generate detached signature (0:1) */
  41707. } pkcs7SignedVector;
  41708. static wc_test_ret_t pkcs7signed_run_vectors(
  41709. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  41710. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  41711. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  41712. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  41713. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  41714. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  41715. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  41716. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  41717. {
  41718. wc_test_ret_t ret;
  41719. int testSz = 0, i;
  41720. int encodedSz;
  41721. byte* out = NULL;
  41722. word32 outSz;
  41723. WC_RNG rng;
  41724. PKCS7* pkcs7 = NULL;
  41725. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41726. XFILE file;
  41727. #endif
  41728. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41729. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41730. 0x72,0x6c,0x64
  41731. };
  41732. static const byte transIdOid[] =
  41733. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41734. 0x09, 0x07 };
  41735. static const byte messageTypeOid[] =
  41736. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41737. 0x09, 0x02 };
  41738. static const byte senderNonceOid[] =
  41739. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41740. 0x09, 0x05 };
  41741. #ifndef NO_SHA
  41742. byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  41743. #else
  41744. byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  41745. #endif
  41746. static const byte messageType[] = { 0x13, 2, '1', '9' };
  41747. byte senderNonce[PKCS7_NONCE_SZ + 2];
  41748. PKCS7Attrib attribs[] =
  41749. {
  41750. { transIdOid, sizeof(transIdOid), transId,
  41751. sizeof(transId) - 1 }, /* take off the null */
  41752. { messageTypeOid, sizeof(messageTypeOid), messageType,
  41753. sizeof(messageType) },
  41754. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  41755. sizeof(senderNonce) }
  41756. };
  41757. /* for testing custom contentType, FirmwarePkgData */
  41758. static const byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  41759. 0x48, 0x86, 0xF7, 0x0D,
  41760. 0x01, 0x09, 0x10, 0x01, 0x10 };
  41761. #define MAX_TESTVECTORS_LEN 20
  41762. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  41763. const pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  41764. if (testSz == MAX_TESTVECTORS_LEN) { \
  41765. ret = WC_TEST_RET_ENC_NC; \
  41766. goto out; \
  41767. } \
  41768. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  41769. sizeof _this_vector); \
  41770. }
  41771. pkcs7SignedVector *testVectors = NULL;
  41772. XMEMSET(&rng, 0, sizeof(rng));
  41773. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  41774. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41775. if (testVectors == NULL) {
  41776. ret = WC_TEST_RET_ENC_ERRNO;
  41777. goto out;
  41778. }
  41779. {
  41780. #ifndef NO_RSA
  41781. #ifndef NO_SHA
  41782. /* RSA with SHA */
  41783. ADD_PKCS7SIGNEDVECTOR(
  41784. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  41785. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41786. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41787. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  41788. 0, 0);
  41789. /* RSA with SHA, no signed attributes */
  41790. ADD_PKCS7SIGNEDVECTOR(
  41791. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  41792. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  41793. NULL, 0, NULL, 0,
  41794. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41795. NULL, 0, 0);
  41796. #endif
  41797. #ifdef WOLFSSL_SHA224
  41798. /* RSA with SHA224 */
  41799. ADD_PKCS7SIGNEDVECTOR(
  41800. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  41801. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41802. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41803. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41804. NULL, 0, 0);
  41805. #endif
  41806. #ifndef NO_SHA256
  41807. /* RSA with SHA256 */
  41808. ADD_PKCS7SIGNEDVECTOR(
  41809. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41810. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41811. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41812. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41813. NULL, 0, 0);
  41814. /* RSA with SHA256, detached signature */
  41815. ADD_PKCS7SIGNEDVECTOR(
  41816. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41817. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41818. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41819. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  41820. NULL, 0, NULL, 0, 1);
  41821. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  41822. ADD_PKCS7SIGNEDVECTOR(
  41823. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41824. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41825. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41826. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  41827. NULL, 0, NULL, 0, 0);
  41828. /* RSA with SHA256 and custom contentType */
  41829. ADD_PKCS7SIGNEDVECTOR(
  41830. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41831. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41832. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41833. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  41834. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  41835. NULL, 0, 0);
  41836. /* RSA with SHA256 and FirmwarePkgData contentType */
  41837. ADD_PKCS7SIGNEDVECTOR(
  41838. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41839. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41840. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41841. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  41842. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  41843. /* RSA with SHA256 using server cert and ca cert */
  41844. ADD_PKCS7SIGNEDVECTOR(
  41845. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  41846. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  41847. rsaCaCertBuf, rsaCaCertBufSz,
  41848. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41849. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  41850. NULL, 0, NULL, 0, 0);
  41851. #endif
  41852. #if defined(WOLFSSL_SHA384)
  41853. /* RSA with SHA384 */
  41854. ADD_PKCS7SIGNEDVECTOR(
  41855. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  41856. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41857. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41858. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41859. NULL, 0, 0);
  41860. #endif
  41861. #if defined(WOLFSSL_SHA512)
  41862. /* RSA with SHA512 */
  41863. ADD_PKCS7SIGNEDVECTOR(
  41864. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  41865. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41866. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41867. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41868. NULL, 0, 0);
  41869. #endif
  41870. #endif /* NO_RSA */
  41871. #ifdef HAVE_ECC
  41872. #ifndef NO_SHA
  41873. /* ECDSA with SHA */
  41874. ADD_PKCS7SIGNEDVECTOR(
  41875. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  41876. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41877. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41878. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41879. NULL, 0, 0);
  41880. /* ECDSA with SHA, no signed attributes */
  41881. ADD_PKCS7SIGNEDVECTOR(
  41882. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  41883. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  41884. NULL, 0, NULL, 0,
  41885. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41886. NULL, 0, 0);
  41887. #endif
  41888. #ifdef WOLFSSL_SHA224
  41889. /* ECDSA with SHA224 */
  41890. ADD_PKCS7SIGNEDVECTOR(
  41891. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  41892. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41893. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41894. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41895. NULL, 0, 0);
  41896. #endif
  41897. #ifndef NO_SHA256
  41898. /* ECDSA with SHA256 */
  41899. ADD_PKCS7SIGNEDVECTOR(
  41900. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41901. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41902. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41903. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41904. NULL, 0, 0);
  41905. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  41906. ADD_PKCS7SIGNEDVECTOR(
  41907. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41908. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41909. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41910. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  41911. NULL, 0, NULL, 0, 0);
  41912. /* ECDSA with SHA256 and custom contentType */
  41913. ADD_PKCS7SIGNEDVECTOR(
  41914. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41915. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41916. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41917. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  41918. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  41919. NULL, 0, 0);
  41920. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  41921. ADD_PKCS7SIGNEDVECTOR(
  41922. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41923. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41924. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41925. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  41926. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  41927. #endif
  41928. #ifdef WOLFSSL_SHA384
  41929. /* ECDSA with SHA384 */
  41930. ADD_PKCS7SIGNEDVECTOR(
  41931. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  41932. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41933. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41934. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41935. NULL, 0, 0);
  41936. #endif
  41937. #ifdef WOLFSSL_SHA512
  41938. /* ECDSA with SHA512 */
  41939. ADD_PKCS7SIGNEDVECTOR(
  41940. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  41941. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41942. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41943. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41944. NULL, 0, 0);
  41945. #endif
  41946. #endif /* HAVE_ECC */
  41947. };
  41948. #undef MAX_TESTVECTORS_LEN
  41949. #undef ADD_PKCS7SIGNEDVECTOR
  41950. outSz = FOURK_BUF;
  41951. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41952. if (out == NULL)
  41953. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41954. XMEMSET(out, 0, outSz);
  41955. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  41956. if (ret < 0)
  41957. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41958. #ifndef HAVE_FIPS
  41959. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  41960. #else
  41961. ret = wc_InitRng(&rng);
  41962. #endif
  41963. if (ret != 0)
  41964. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41965. for (i = 0; i < testSz; i++) {
  41966. if (pkcs7)
  41967. wc_PKCS7_Free(pkcs7);
  41968. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  41969. if (pkcs7 == NULL)
  41970. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41971. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  41972. (word32)testVectors[i].certSz);
  41973. if (ret != 0)
  41974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41975. /* load CA certificate, if present */
  41976. if (testVectors[i].caCert != NULL) {
  41977. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  41978. (word32)testVectors[i].caCertSz);
  41979. if (ret != 0)
  41980. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41981. }
  41982. pkcs7->rng = &rng;
  41983. pkcs7->content = (byte*)testVectors[i].content;
  41984. pkcs7->contentSz = testVectors[i].contentSz;
  41985. pkcs7->contentOID = testVectors[i].contentOID;
  41986. pkcs7->hashOID = testVectors[i].hashOID;
  41987. pkcs7->encryptOID = testVectors[i].signOID;
  41988. pkcs7->privateKey = testVectors[i].privateKey;
  41989. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  41990. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  41991. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  41992. /* optional custom contentType, default is DATA,
  41993. overrides contentOID if set */
  41994. if (testVectors[i].contentType != NULL) {
  41995. ret = wc_PKCS7_SetContentType(pkcs7,
  41996. (byte *)testVectors[i].contentType,
  41997. testVectors[i].contentTypeSz);
  41998. if (ret != 0)
  41999. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42000. }
  42001. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  42002. default is IssuerAndSerialNumber */
  42003. if (testVectors[i].sidType == CMS_SKID) {
  42004. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42005. if (ret != 0)
  42006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42007. }
  42008. /* generate senderNonce */
  42009. {
  42010. senderNonce[0] = 0x04;
  42011. senderNonce[1] = PKCS7_NONCE_SZ;
  42012. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  42013. if (ret != 0)
  42014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42015. }
  42016. /* generate transactionID (used with SCEP) */
  42017. {
  42018. #ifndef NO_SHA
  42019. wc_Sha sha;
  42020. byte digest[WC_SHA_DIGEST_SIZE];
  42021. #else
  42022. wc_Sha256 sha;
  42023. byte digest[WC_SHA256_DIGEST_SIZE];
  42024. #endif
  42025. int j,k;
  42026. transId[0] = 0x13;
  42027. transId[1] = sizeof(digest) * 2;
  42028. #ifndef NO_SHA
  42029. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  42030. if (ret != 0)
  42031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42032. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  42033. wc_ShaFinal(&sha, digest);
  42034. wc_ShaFree(&sha);
  42035. #else
  42036. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  42037. if (ret != 0)
  42038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42039. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  42040. wc_Sha256Final(&sha, digest);
  42041. wc_Sha256Free(&sha);
  42042. #endif
  42043. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  42044. #if defined(WOLF_C89)
  42045. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  42046. #else
  42047. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  42048. #endif
  42049. }
  42050. }
  42051. /* enable detached signature generation, if set */
  42052. if (testVectors[i].detachedSignature == 1) {
  42053. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  42054. if (ret != 0)
  42055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42056. }
  42057. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  42058. if (encodedSz < 0)
  42059. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42060. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42061. /* write PKCS#7 to output file for more testing */
  42062. file = XFOPEN(testVectors[i].outFileName, "wb");
  42063. if (!file) {
  42064. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42065. }
  42066. ret = (int)XFWRITE(out, 1, encodedSz, file);
  42067. XFCLOSE(file);
  42068. if (ret != (int)encodedSz)
  42069. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42070. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42071. wc_PKCS7_Free(pkcs7);
  42072. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42073. if (pkcs7 == NULL)
  42074. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42075. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  42076. if (testVectors[i].detachedSignature == 1) {
  42077. /* set content for verifying detached signatures */
  42078. pkcs7->content = (byte*)testVectors[i].content;
  42079. pkcs7->contentSz = testVectors[i].contentSz;
  42080. }
  42081. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  42082. if (ret < 0)
  42083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42084. /* verify contentType extracted successfully for custom content types */
  42085. if (testVectors[i].contentTypeSz > 0) {
  42086. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  42087. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42088. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  42089. pkcs7->contentTypeSz) != 0) {
  42090. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42091. }
  42092. }
  42093. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  42094. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42095. {
  42096. /* check getting signed attributes */
  42097. #ifndef NO_SHA
  42098. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  42099. #else
  42100. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  42101. #endif
  42102. const byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  42103. int oidSz = (int)sizeof(transIdOid) - 2;
  42104. int bufSz = 0;
  42105. if (testVectors[i].signedAttribs != NULL) {
  42106. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  42107. NULL, (word32*)&bufSz);
  42108. if (ret != LENGTH_ONLY_E)
  42109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42110. ret = 0;
  42111. }
  42112. if (bufSz > (int)sizeof(buf))
  42113. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42114. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  42115. buf, (word32*)&bufSz);
  42116. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  42117. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  42118. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42119. }
  42120. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42121. file = XFOPEN("./pkcs7cert.der", "wb");
  42122. if (!file)
  42123. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42124. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  42125. if (ret < 0)
  42126. ret = WC_TEST_RET_ENC_ERRNO;
  42127. else
  42128. ret = 0;
  42129. XFCLOSE(file);
  42130. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42131. }
  42132. out:
  42133. if (pkcs7 != NULL)
  42134. wc_PKCS7_Free(pkcs7);
  42135. if (out != NULL)
  42136. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42137. if (testVectors != NULL)
  42138. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42139. wc_FreeRng(&rng);
  42140. if (ret > 0)
  42141. return 0;
  42142. (void)rsaClientCertBuf;
  42143. (void)rsaClientCertBufSz;
  42144. (void)rsaClientPrivKeyBuf;
  42145. (void)rsaClientPrivKeyBufSz;
  42146. (void)rsaServerCertBuf;
  42147. (void)rsaServerCertBufSz;
  42148. (void)rsaServerPrivKeyBuf;
  42149. (void)rsaServerPrivKeyBufSz;
  42150. (void)rsaCaCertBuf;
  42151. (void)rsaCaCertBufSz;
  42152. (void)rsaCaPrivKeyBuf;
  42153. (void)rsaCaPrivKeyBufSz;
  42154. (void)eccClientCertBuf;
  42155. (void)eccClientCertBufSz;
  42156. (void)eccClientPrivKeyBuf;
  42157. (void)eccClientPrivKeyBufSz;
  42158. return ret;
  42159. }
  42160. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  42161. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  42162. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  42163. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  42164. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  42165. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  42166. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  42167. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  42168. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  42169. {
  42170. wc_test_ret_t ret;
  42171. int testSz = 0, i;
  42172. int encodedSz;
  42173. byte* out = NULL;
  42174. word32 outSz;
  42175. WC_RNG rng;
  42176. PKCS7* pkcs7 = NULL;
  42177. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42178. XFILE file;
  42179. #endif
  42180. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  42181. !defined(NO_PKCS7_ENCRYPTED_DATA)
  42182. byte* encryptedTmp = NULL;
  42183. int encryptedTmpSz;
  42184. #endif
  42185. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  42186. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42187. 0x72,0x6c,0x64
  42188. };
  42189. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42190. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42191. static const byte aes256Key[] = {
  42192. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42193. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42194. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42195. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42196. };
  42197. #endif
  42198. static const byte messageTypeOid[] =
  42199. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  42200. 0x09, 0x02 };
  42201. static const byte messageType[] = { 0x13, 2, '1', '9' };
  42202. PKCS7Attrib attribs[] =
  42203. {
  42204. { messageTypeOid, sizeof(messageTypeOid), messageType,
  42205. sizeof(messageType) },
  42206. };
  42207. #define MAX_TESTVECTORS_LEN 19
  42208. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  42209. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  42210. if (testSz == MAX_TESTVECTORS_LEN) { \
  42211. ret = WC_TEST_RET_ENC_NC; \
  42212. goto out; \
  42213. } \
  42214. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  42215. sizeof _this_vector); \
  42216. }
  42217. pkcs7SignedVector *testVectors = NULL;
  42218. XMEMSET(&rng, 0, sizeof(rng));
  42219. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  42220. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42221. if (testVectors == NULL) {
  42222. ret = WC_TEST_RET_ENC_ERRNO;
  42223. goto out;
  42224. }
  42225. {
  42226. #ifndef NO_RSA
  42227. #ifndef NO_SHA256
  42228. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  42229. ADD_PKCS7SIGNEDVECTOR(
  42230. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42231. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42232. NULL, 0,
  42233. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  42234. 0, 0, NULL, 0, NULL, 0, 0);
  42235. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  42236. ADD_PKCS7SIGNEDVECTOR(
  42237. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42238. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42239. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42240. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  42241. NULL, 0, NULL, 0, 0);
  42242. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  42243. ADD_PKCS7SIGNEDVECTOR(
  42244. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42245. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42246. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42247. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  42248. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  42249. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  42250. ADD_PKCS7SIGNEDVECTOR(
  42251. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  42252. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  42253. rsaCaCertBuf, rsaCaCertBufSz,
  42254. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42255. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  42256. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42257. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42258. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42259. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  42260. ADD_PKCS7SIGNEDVECTOR(
  42261. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42262. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42263. NULL, 0,
  42264. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  42265. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  42266. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  42267. ADD_PKCS7SIGNEDVECTOR(
  42268. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42269. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42270. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42271. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  42272. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  42273. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42274. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  42275. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42276. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  42277. ADD_PKCS7SIGNEDVECTOR(
  42278. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42279. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42280. NULL, 0,
  42281. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  42282. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42283. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  42284. ADD_PKCS7SIGNEDVECTOR(
  42285. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42286. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42287. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42288. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  42289. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42290. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42291. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  42292. no attribs */
  42293. ADD_PKCS7SIGNEDVECTOR(
  42294. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42295. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42296. NULL, 0,
  42297. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  42298. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  42299. 0, 0);
  42300. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  42301. attribs */
  42302. ADD_PKCS7SIGNEDVECTOR(
  42303. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42304. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42305. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42306. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  42307. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  42308. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42309. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  42310. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42311. #endif /* NO_SHA256 */
  42312. #endif /* NO_RSA */
  42313. #ifdef HAVE_ECC
  42314. #ifndef NO_SHA256
  42315. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  42316. ADD_PKCS7SIGNEDVECTOR(
  42317. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42318. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42319. NULL, 0,
  42320. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42321. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42322. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  42323. ADD_PKCS7SIGNEDVECTOR(
  42324. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42325. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42326. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42327. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42328. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42329. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  42330. ADD_PKCS7SIGNEDVECTOR(
  42331. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42332. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42333. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42334. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  42335. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  42336. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42337. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42338. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  42339. ADD_PKCS7SIGNEDVECTOR(
  42340. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42341. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42342. NULL, 0,
  42343. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42344. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  42345. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  42346. ADD_PKCS7SIGNEDVECTOR(
  42347. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42348. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42349. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42350. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42351. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  42352. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42353. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  42354. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42355. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  42356. ADD_PKCS7SIGNEDVECTOR(
  42357. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42358. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42359. NULL, 0,
  42360. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42361. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42362. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  42363. ADD_PKCS7SIGNEDVECTOR(
  42364. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42365. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42366. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42367. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42368. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42369. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42370. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  42371. no attribs */
  42372. ADD_PKCS7SIGNEDVECTOR(
  42373. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42374. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42375. NULL, 0,
  42376. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  42377. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  42378. 0, 0);
  42379. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  42380. attribs */
  42381. ADD_PKCS7SIGNEDVECTOR(
  42382. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42383. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42384. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42385. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  42386. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  42387. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42388. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  42389. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42390. #endif /* NO_SHA256 */
  42391. #endif /* HAVE_ECC */
  42392. };
  42393. #undef MAX_TESTVECTORS_LEN
  42394. #undef ADD_PKCS7SIGNEDVECTOR
  42395. outSz = FOURK_BUF;
  42396. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42397. if (out == NULL)
  42398. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42399. XMEMSET(out, 0, outSz);
  42400. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  42401. if (ret < 0)
  42402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42403. #ifndef HAVE_FIPS
  42404. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42405. #else
  42406. ret = wc_InitRng(&rng);
  42407. #endif
  42408. if (ret != 0)
  42409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42410. for (i = 0; i < testSz; i++) {
  42411. if (pkcs7)
  42412. wc_PKCS7_Free(pkcs7);
  42413. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42414. if (pkcs7 == NULL)
  42415. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42416. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42417. (word32)testVectors[i].certSz);
  42418. if (ret != 0)
  42419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42420. /* load CA certificate, if present */
  42421. if (testVectors[i].caCert != NULL) {
  42422. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  42423. (word32)testVectors[i].caCertSz);
  42424. if (ret != 0)
  42425. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42426. }
  42427. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  42428. default is IssuerAndSerialNumber */
  42429. if (testVectors[i].sidType == CMS_SKID) {
  42430. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42431. if (ret != 0)
  42432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42433. }
  42434. if (testVectors[i].encCompFlag == 0) {
  42435. /* encode Signed FirmwarePkgData */
  42436. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  42437. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42438. testVectors[i].signOID, testVectors[i].hashOID,
  42439. (byte*)testVectors[i].content, testVectors[i].contentSz,
  42440. testVectors[i].signedAttribs,
  42441. testVectors[i].signedAttribsSz, out, outSz);
  42442. if (encodedSz < 0)
  42443. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42444. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42445. } else if (testVectors[i].encCompFlag == 1) {
  42446. /* encode Signed Encrypted FirmwarePkgData */
  42447. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  42448. (byte *)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  42449. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42450. testVectors[i].encryptOID, testVectors[i].signOID,
  42451. testVectors[i].hashOID, (byte*)testVectors[i].content,
  42452. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  42453. testVectors[i].unprotectedAttribsSz,
  42454. testVectors[i].signedAttribs,
  42455. testVectors[i].signedAttribsSz, out, outSz);
  42456. if (encodedSz <= 0)
  42457. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42458. #endif
  42459. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42460. } else if (testVectors[i].encCompFlag == 2) {
  42461. /* encode Signed Compressed FirmwarePkgData */
  42462. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  42463. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42464. testVectors[i].signOID, testVectors[i].hashOID,
  42465. (byte*)testVectors[i].content, testVectors[i].contentSz,
  42466. testVectors[i].signedAttribs,
  42467. testVectors[i].signedAttribsSz, out, outSz);
  42468. if (encodedSz <= 0)
  42469. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42470. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42471. } else if (testVectors[i].encCompFlag == 3) {
  42472. /* encode Signed Encrypted Compressed FirmwarePkgData */
  42473. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  42474. (byte*)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  42475. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42476. testVectors[i].encryptOID, testVectors[i].signOID,
  42477. testVectors[i].hashOID, (byte*)testVectors[i].content,
  42478. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  42479. testVectors[i].unprotectedAttribsSz,
  42480. testVectors[i].signedAttribs,
  42481. testVectors[i].signedAttribsSz, out, outSz);
  42482. if (encodedSz <= 0)
  42483. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42484. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  42485. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42486. } else {
  42487. /* unsupported SignedData single-shot combination */
  42488. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42489. }
  42490. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42491. /* write PKCS#7 to output file for more testing */
  42492. file = XFOPEN(testVectors[i].outFileName, "wb");
  42493. if (!file)
  42494. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42495. ret = (int)XFWRITE(out, 1, encodedSz, file);
  42496. XFCLOSE(file);
  42497. file = NULL;
  42498. if (ret != (int)encodedSz)
  42499. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42500. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42501. wc_PKCS7_Free(pkcs7);
  42502. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42503. if (pkcs7 == NULL)
  42504. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42505. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  42506. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  42507. if (ret < 0)
  42508. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42509. #ifndef NO_PKCS7_STREAM
  42510. {
  42511. word32 z;
  42512. for (z = 0; z < outSz && ret != 0; z++) {
  42513. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  42514. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  42515. printf("unexpected error %d\n", ret);
  42516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42517. }
  42518. }
  42519. }
  42520. #endif
  42521. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  42522. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42523. if (testVectors[i].encCompFlag == 0) {
  42524. /* verify decoded content matches expected */
  42525. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  42526. XMEMCMP(pkcs7->content, testVectors[i].content,
  42527. pkcs7->contentSz)) {
  42528. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42529. }
  42530. }
  42531. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42532. else if (testVectors[i].encCompFlag == 1) {
  42533. /* decrypt inner encryptedData */
  42534. pkcs7->encryptionKey = (byte *)testVectors[i].encryptKey;
  42535. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  42536. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  42537. pkcs7->contentSz, out, outSz);
  42538. if (ret < 0)
  42539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42540. /* compare decrypted to expected */
  42541. if (((word32)ret != testVectors[i].contentSz) ||
  42542. XMEMCMP(out, testVectors[i].content, ret))
  42543. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42544. }
  42545. #endif
  42546. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42547. else if (testVectors[i].encCompFlag == 2) {
  42548. /* decompress inner compressedData */
  42549. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  42550. pkcs7->contentSz, out, outSz);
  42551. if (ret < 0)
  42552. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42553. /* compare decompressed to expected */
  42554. if (((word32)ret != testVectors[i].contentSz) ||
  42555. XMEMCMP(out, testVectors[i].content, ret))
  42556. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42557. }
  42558. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42559. else if (testVectors[i].encCompFlag == 3) {
  42560. encryptedTmpSz = FOURK_BUF;
  42561. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  42562. DYNAMIC_TYPE_TMP_BUFFER);
  42563. if (encryptedTmp == NULL)
  42564. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42565. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  42566. /* decrypt inner encryptedData */
  42567. pkcs7->encryptionKey = (byte*)testVectors[i].encryptKey;
  42568. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  42569. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  42570. pkcs7->contentSz, encryptedTmp,
  42571. encryptedTmpSz);
  42572. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  42573. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42574. /* decompress inner compressedData */
  42575. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  42576. encryptedTmpSz, out, outSz);
  42577. if (ret < 0)
  42578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42579. /* compare decompressed to expected */
  42580. if (((word32)ret != testVectors[i].contentSz) ||
  42581. XMEMCMP(out, testVectors[i].content, ret))
  42582. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42583. }
  42584. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  42585. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42586. }
  42587. out:
  42588. if (pkcs7 != NULL)
  42589. wc_PKCS7_Free(pkcs7);
  42590. if (out != NULL)
  42591. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42592. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  42593. !defined(NO_PKCS7_ENCRYPTED_DATA)
  42594. if (encryptedTmp != NULL)
  42595. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42596. #endif
  42597. if (testVectors != NULL)
  42598. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42599. wc_FreeRng(&rng);
  42600. if (ret > 0)
  42601. return 0;
  42602. (void)eccClientCertBuf;
  42603. (void)eccClientCertBufSz;
  42604. (void)eccClientPrivKeyBuf;
  42605. (void)eccClientPrivKeyBufSz;
  42606. (void)rsaClientCertBuf;
  42607. (void)rsaClientCertBufSz;
  42608. (void)rsaClientPrivKeyBuf;
  42609. (void)rsaClientPrivKeyBufSz;
  42610. (void)rsaServerCertBuf;
  42611. (void)rsaServerCertBufSz;
  42612. (void)rsaServerPrivKeyBuf;
  42613. (void)rsaServerPrivKeyBufSz;
  42614. (void)rsaCaCertBuf;
  42615. (void)rsaCaCertBufSz;
  42616. (void)rsaCaPrivKeyBuf;
  42617. (void)rsaCaPrivKeyBufSz;
  42618. return ret;
  42619. }
  42620. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  42621. {
  42622. wc_test_ret_t ret = 0;
  42623. byte* rsaClientCertBuf = NULL;
  42624. byte* rsaServerCertBuf = NULL;
  42625. byte* rsaCaCertBuf = NULL;
  42626. byte* eccClientCertBuf = NULL;
  42627. byte* rsaClientPrivKeyBuf = NULL;
  42628. byte* rsaServerPrivKeyBuf = NULL;
  42629. byte* rsaCaPrivKeyBuf = NULL;
  42630. byte* eccClientPrivKeyBuf = NULL;
  42631. word32 rsaClientCertBufSz = 0;
  42632. word32 rsaServerCertBufSz = 0;
  42633. word32 rsaCaCertBufSz = 0;
  42634. word32 eccClientCertBufSz = 0;
  42635. word32 rsaClientPrivKeyBufSz = 0;
  42636. word32 rsaServerPrivKeyBufSz = 0;
  42637. word32 rsaCaPrivKeyBufSz = 0;
  42638. word32 eccClientPrivKeyBufSz = 0;
  42639. WOLFSSL_ENTER("pkcs7signed_test");
  42640. #ifndef NO_RSA
  42641. /* read client RSA cert and key in DER format */
  42642. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42643. DYNAMIC_TYPE_TMP_BUFFER);
  42644. if (rsaClientCertBuf == NULL)
  42645. ret = WC_TEST_RET_ENC_NC;
  42646. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42647. DYNAMIC_TYPE_TMP_BUFFER);
  42648. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  42649. ret = WC_TEST_RET_ENC_ERRNO;
  42650. }
  42651. rsaClientCertBufSz = FOURK_BUF;
  42652. rsaClientPrivKeyBufSz = FOURK_BUF;
  42653. /* read server RSA cert and key in DER format */
  42654. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42655. DYNAMIC_TYPE_TMP_BUFFER);
  42656. if (ret == 0 && rsaServerCertBuf == NULL)
  42657. ret = WC_TEST_RET_ENC_NC;
  42658. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42659. DYNAMIC_TYPE_TMP_BUFFER);
  42660. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  42661. ret = WC_TEST_RET_ENC_ERRNO;
  42662. }
  42663. rsaServerCertBufSz = FOURK_BUF;
  42664. rsaServerPrivKeyBufSz = FOURK_BUF;
  42665. /* read CA RSA cert and key in DER format, for use with server cert */
  42666. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42667. DYNAMIC_TYPE_TMP_BUFFER);
  42668. if (ret == 0 && rsaCaCertBuf == NULL)
  42669. ret = WC_TEST_RET_ENC_NC;
  42670. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42671. DYNAMIC_TYPE_TMP_BUFFER);
  42672. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  42673. ret = WC_TEST_RET_ENC_ERRNO;
  42674. }
  42675. rsaCaCertBufSz = FOURK_BUF;
  42676. rsaCaPrivKeyBufSz = FOURK_BUF;
  42677. #endif /* NO_RSA */
  42678. #ifdef HAVE_ECC
  42679. /* read client ECC cert and key in DER format */
  42680. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42681. DYNAMIC_TYPE_TMP_BUFFER);
  42682. if (ret == 0 && eccClientCertBuf == NULL) {
  42683. ret = WC_TEST_RET_ENC_ERRNO;
  42684. }
  42685. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42686. DYNAMIC_TYPE_TMP_BUFFER);
  42687. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  42688. ret = WC_TEST_RET_ENC_ERRNO;
  42689. }
  42690. eccClientCertBufSz = FOURK_BUF;
  42691. eccClientPrivKeyBufSz = FOURK_BUF;
  42692. #endif /* HAVE_ECC */
  42693. if (ret >= 0)
  42694. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  42695. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  42696. rsaServerCertBuf, &rsaServerCertBufSz,
  42697. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  42698. rsaCaCertBuf, &rsaCaCertBufSz,
  42699. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  42700. eccClientCertBuf, &eccClientCertBufSz,
  42701. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  42702. if (ret < 0) {
  42703. ret = WC_TEST_RET_ENC_EC(ret);
  42704. }
  42705. if (ret >= 0)
  42706. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42707. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  42708. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  42709. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  42710. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  42711. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  42712. eccClientCertBuf, (word32)eccClientCertBufSz,
  42713. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  42714. if (ret >= 0)
  42715. ret = pkcs7signed_run_SingleShotVectors(
  42716. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42717. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  42718. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  42719. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  42720. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  42721. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  42722. eccClientCertBuf, (word32)eccClientCertBufSz,
  42723. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  42724. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  42725. if (ret >= 0)
  42726. ret = pkcs7callback_test(
  42727. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42728. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  42729. #endif
  42730. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42731. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42732. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42733. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42734. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42735. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42736. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42737. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42738. return ret;
  42739. }
  42740. #endif /* HAVE_PKCS7 */
  42741. #if defined(WOLFSSL_PUBLIC_MP) && \
  42742. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42743. defined(USE_FAST_MATH))
  42744. /* Maximum number of bytes in a number to test. */
  42745. #define MP_MAX_TEST_BYTE_LEN 32
  42746. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  42747. {
  42748. byte d[MP_MAX_TEST_BYTE_LEN];
  42749. wc_test_ret_t ret;
  42750. (void)heap;
  42751. do {
  42752. ret = wc_RNG_GenerateBlock(rng, d, (word32)len);
  42753. if (ret != 0)
  42754. return ret;
  42755. ret = mp_read_unsigned_bin(n, d, (word32)len);
  42756. if (ret != 0)
  42757. return ret;
  42758. } while (mp_iszero(n));
  42759. return 0;
  42760. }
  42761. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  42762. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  42763. {
  42764. int i, j;
  42765. mp_digit rem;
  42766. mp_digit rem2;
  42767. wc_test_ret_t ret;
  42768. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42769. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  42770. for (i = 0; i < 10; i++) {
  42771. for (j = 1; j < 10; j++) {
  42772. ret = randNum(a, j, rng, NULL);
  42773. if (ret != 0)
  42774. return WC_TEST_RET_ENC_EC(ret);
  42775. ret = mp_div_3(a, r, &rem);
  42776. if (ret != 0)
  42777. return WC_TEST_RET_ENC_EC(ret);
  42778. ret = mp_mul_d(r, 3, r);
  42779. if (ret != 0)
  42780. return WC_TEST_RET_ENC_EC(ret);
  42781. ret = mp_add_d(r, rem, r);
  42782. if (ret != 0)
  42783. return WC_TEST_RET_ENC_EC(ret);
  42784. ret = mp_cmp(r, a);
  42785. if (ret != MP_EQ)
  42786. return WC_TEST_RET_ENC_NC;
  42787. }
  42788. }
  42789. ret = mp_div_3(a, r, &rem);
  42790. if (ret != 0)
  42791. return WC_TEST_RET_ENC_NC;
  42792. ret = mp_div_3(a, a, NULL);
  42793. if (ret != 0)
  42794. return WC_TEST_RET_ENC_NC;
  42795. ret = mp_cmp(r, a);
  42796. if (ret != MP_EQ)
  42797. return WC_TEST_RET_ENC_NC;
  42798. #endif
  42799. #if defined(WOLFSSL_SP_MATH_ALL)
  42800. ret = mp_div_d(a, 10, r, &rem);
  42801. if (ret != 0)
  42802. return WC_TEST_RET_ENC_EC(ret);
  42803. ret = mp_div_d(a, 10, a, NULL);
  42804. if (ret != 0)
  42805. return WC_TEST_RET_ENC_EC(ret);
  42806. ret = mp_cmp(r, a);
  42807. if (ret != MP_EQ)
  42808. return WC_TEST_RET_ENC_NC;
  42809. ret = mp_div_d(a, 12, r, &rem);
  42810. if (ret != 0)
  42811. return WC_TEST_RET_ENC_EC(ret);
  42812. ret = mp_div_d(a, 12, a, NULL);
  42813. if (ret != 0)
  42814. return WC_TEST_RET_ENC_EC(ret);
  42815. ret = mp_cmp(r, a);
  42816. if (ret != MP_EQ)
  42817. return WC_TEST_RET_ENC_NC;
  42818. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  42819. if (ret != 0)
  42820. return WC_TEST_RET_ENC_EC(ret);
  42821. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  42822. if (ret != 0)
  42823. return WC_TEST_RET_ENC_EC(ret);
  42824. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  42825. if (ret != 0)
  42826. return WC_TEST_RET_ENC_EC(ret);
  42827. ret = mp_cmp(r, a);
  42828. if (ret != MP_EQ)
  42829. return WC_TEST_RET_ENC_NC;
  42830. if (rem != rem2)
  42831. return WC_TEST_RET_ENC_NC;
  42832. #endif
  42833. (void)a;
  42834. (void)r;
  42835. (void)rng;
  42836. (void)i;
  42837. (void)j;
  42838. (void)rem;
  42839. (void)rem2;
  42840. (void)ret;
  42841. return 0;
  42842. }
  42843. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  42844. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  42845. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42846. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  42847. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  42848. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  42849. {
  42850. wc_test_ret_t ret;
  42851. int i, j;
  42852. int size;
  42853. char str[30];
  42854. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  42855. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  42856. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  42857. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  42858. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  42859. for (i = 0; i < 10; i++) {
  42860. for (j = 2; j < 12; j++) {
  42861. ret = randNum(a, j, rng, NULL);
  42862. if (ret != 0)
  42863. return WC_TEST_RET_ENC_EC(ret);
  42864. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  42865. if (ret != MP_OKAY)
  42866. return WC_TEST_RET_ENC_EC(ret);
  42867. ret = mp_toradix(a, str, MP_RADIX_DEC);
  42868. if (ret != MP_OKAY)
  42869. return WC_TEST_RET_ENC_EC(ret);
  42870. if ((int)XSTRLEN(str) != size - 1)
  42871. return WC_TEST_RET_ENC_NC;
  42872. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  42873. if (ret != MP_OKAY)
  42874. return WC_TEST_RET_ENC_EC(ret);
  42875. ret = mp_cmp(a, r);
  42876. if (ret != MP_EQ)
  42877. return WC_TEST_RET_ENC_NC;
  42878. }
  42879. }
  42880. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  42881. if (ret != MP_VAL)
  42882. return WC_TEST_RET_ENC_EC(ret);
  42883. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  42884. if (ret != MP_VAL)
  42885. return WC_TEST_RET_ENC_EC(ret);
  42886. ret = mp_read_radix(r, empty2, MP_RADIX_DEC);
  42887. if (ret != MP_OKAY)
  42888. return WC_TEST_RET_ENC_EC(ret);
  42889. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  42890. if (ret != MP_OKAY)
  42891. return WC_TEST_RET_ENC_EC(ret);
  42892. if (!mp_iszero(r))
  42893. return WC_TEST_RET_ENC_NC;
  42894. mp_set(r, 1);
  42895. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  42896. if (ret != MP_OKAY)
  42897. return WC_TEST_RET_ENC_EC(ret);
  42898. if (!mp_iszero(r))
  42899. return WC_TEST_RET_ENC_NC;
  42900. mp_zero(a);
  42901. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  42902. if (ret != 0)
  42903. return WC_TEST_RET_ENC_EC(ret);
  42904. if (size != 2)
  42905. return WC_TEST_RET_ENC_NC;
  42906. ret = mp_toradix(a, str, MP_RADIX_DEC);
  42907. if (ret != 0)
  42908. return WC_TEST_RET_ENC_EC(ret);
  42909. if ((int)XSTRLEN(str) != size - 1)
  42910. return WC_TEST_RET_ENC_NC;
  42911. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  42912. if (ret != 0)
  42913. return WC_TEST_RET_ENC_EC(ret);
  42914. if (!mp_iszero(r))
  42915. return WC_TEST_RET_ENC_NC;
  42916. return 0;
  42917. }
  42918. #endif
  42919. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  42920. defined(HAVE_ECC))
  42921. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  42922. {
  42923. wc_test_ret_t ret;
  42924. int i, j;
  42925. int size;
  42926. char str[30];
  42927. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  42928. static char longStr[2 * sizeof(a->dp) + 2];
  42929. #endif
  42930. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  42931. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  42932. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  42933. for (i = 0; i < 10; i++) {
  42934. for (j = 2; j < 12; j++) {
  42935. ret = randNum(a, j, rng, NULL);
  42936. if (ret != 0)
  42937. return WC_TEST_RET_ENC_EC(ret);
  42938. mp_radix_size(a, MP_RADIX_HEX, &size);
  42939. mp_toradix(a, str, MP_RADIX_HEX);
  42940. if ((int)XSTRLEN(str) != size - 1)
  42941. return WC_TEST_RET_ENC_NC;
  42942. mp_read_radix(r, str, MP_RADIX_HEX);
  42943. ret = mp_cmp(a, r);
  42944. if (ret != MP_EQ)
  42945. return WC_TEST_RET_ENC_NC;
  42946. }
  42947. }
  42948. ret = mp_read_radix(r, empty2, MP_RADIX_HEX);
  42949. if (ret != MP_OKAY)
  42950. return WC_TEST_RET_ENC_EC(ret);
  42951. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  42952. if (ret != MP_VAL)
  42953. return WC_TEST_RET_ENC_EC(ret);
  42954. mp_set(r, 1);
  42955. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  42956. if (ret != MP_OKAY)
  42957. return WC_TEST_RET_ENC_EC(ret);
  42958. if (!mp_iszero(r))
  42959. return WC_TEST_RET_ENC_NC;
  42960. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  42961. /* Fixed MP data size - string can be too long. */
  42962. longStr[0] = '8';
  42963. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  42964. longStr[sizeof(longStr)-1] = '\0';
  42965. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  42966. if (ret != MP_VAL)
  42967. return WC_TEST_RET_ENC_EC(ret);
  42968. #endif
  42969. mp_zero(a);
  42970. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  42971. if (ret != 0)
  42972. return WC_TEST_RET_ENC_EC(ret);
  42973. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42974. if (size != 3)
  42975. #else
  42976. if (size != 2)
  42977. #endif
  42978. return WC_TEST_RET_ENC_NC;
  42979. ret = mp_toradix(a, str, MP_RADIX_HEX);
  42980. if (ret != 0)
  42981. return WC_TEST_RET_ENC_EC(ret);
  42982. if ((int)XSTRLEN(str) != size - 1)
  42983. return WC_TEST_RET_ENC_NC;
  42984. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  42985. if (ret != 0)
  42986. return WC_TEST_RET_ENC_EC(ret);
  42987. if (!mp_iszero(r))
  42988. return WC_TEST_RET_ENC_NC;
  42989. #ifdef WOLFSSL_SP_MATH
  42990. ret = mp_toradix(a, str, 8);
  42991. if (ret != MP_VAL)
  42992. return WC_TEST_RET_ENC_EC(ret);
  42993. ret = mp_radix_size(a, 8, &size);
  42994. if (ret != MP_VAL)
  42995. return WC_TEST_RET_ENC_EC(ret);
  42996. #endif
  42997. return 0;
  42998. }
  42999. #endif
  43000. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  43001. {
  43002. int i;
  43003. wc_test_ret_t ret;
  43004. ret = randNum(a, 4, rng, NULL);
  43005. if (ret != 0)
  43006. return WC_TEST_RET_ENC_EC(ret);
  43007. for (i = 0; i < 4; i++) {
  43008. mp_copy(r1, a);
  43009. ret = mp_lshd(r1, i);
  43010. if (ret != MP_OKAY)
  43011. return WC_TEST_RET_ENC_EC(ret);
  43012. #ifndef WOLFSSL_SP_MATH
  43013. mp_rshd(r1, i);
  43014. #else
  43015. mp_rshb(r1, i * SP_WORD_SIZE);
  43016. #endif
  43017. ret = mp_cmp(a, r1);
  43018. if (ret != MP_EQ)
  43019. return WC_TEST_RET_ENC_NC;
  43020. }
  43021. #ifndef WOLFSSL_SP_MATH
  43022. for (i = 0; i < DIGIT_BIT+1; i++) {
  43023. ret = mp_mul_2d(a, i, r1);
  43024. if (ret != MP_OKAY)
  43025. return WC_TEST_RET_ENC_EC(ret);
  43026. mp_rshb(r1, i);
  43027. ret = mp_cmp(a, r1);
  43028. if (ret != MP_EQ)
  43029. return WC_TEST_RET_ENC_NC;
  43030. }
  43031. #endif
  43032. return 0;
  43033. }
  43034. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  43035. {
  43036. int i, j;
  43037. wc_test_ret_t ret;
  43038. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  43039. mp_zero(a);
  43040. mp_set_bit(a, i);
  43041. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  43042. return WC_TEST_RET_ENC_NC;
  43043. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  43044. mp_zero(r1);
  43045. mp_set_bit(r1, i);
  43046. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  43047. if (ret != MP_OKAY)
  43048. return WC_TEST_RET_ENC_EC(ret);
  43049. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  43050. if (ret != MP_OKAY)
  43051. return WC_TEST_RET_ENC_EC(ret);
  43052. ret = mp_cmp(a, r1);
  43053. if (ret != MP_EQ)
  43054. return WC_TEST_RET_ENC_NC;
  43055. }
  43056. }
  43057. mp_zero(r1);
  43058. ret = mp_add_d(r1, 1, r1);
  43059. if (ret != MP_OKAY)
  43060. return WC_TEST_RET_ENC_EC(ret);
  43061. if (r1->used != 1)
  43062. return WC_TEST_RET_ENC_NC;
  43063. ret = mp_sub_d(r1, 1, r1);
  43064. if (ret != MP_OKAY)
  43065. return WC_TEST_RET_ENC_EC(ret);
  43066. if (r1->used != 0)
  43067. return WC_TEST_RET_ENC_NC;
  43068. return 0;
  43069. }
  43070. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  43071. {
  43072. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  43073. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  43074. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  43075. };
  43076. byte out[24];
  43077. int i, j, k;
  43078. const byte* p;
  43079. wc_test_ret_t ret;
  43080. for (i = 0; i < (int)sizeof(in); i++) {
  43081. p = in + sizeof(in) - i;
  43082. ret = mp_read_unsigned_bin(a, p, (word32)i);
  43083. if (ret != 0)
  43084. return WC_TEST_RET_ENC_EC(ret);
  43085. for (j = i; j < (int)sizeof(out); j++) {
  43086. XMEMSET(out, 0xff, sizeof(out));
  43087. ret = mp_to_unsigned_bin_len(a, out, j);
  43088. if (ret != 0)
  43089. return WC_TEST_RET_ENC_EC(ret);
  43090. for (k = 0; k < j - i; k++) {
  43091. if (out[k] != 0)
  43092. return WC_TEST_RET_ENC_NC;
  43093. }
  43094. for (; k < j; k++) {
  43095. if (out[k] != p[k - (j - i)])
  43096. return WC_TEST_RET_ENC_NC;
  43097. }
  43098. }
  43099. }
  43100. /* Length too small. */
  43101. ret = mp_to_unsigned_bin_len(a, out, 1);
  43102. if (ret != MP_VAL)
  43103. return WC_TEST_RET_ENC_EC(ret);
  43104. ret = mp_read_unsigned_bin(a, NULL, 0);
  43105. if (ret != 0)
  43106. return WC_TEST_RET_ENC_EC(ret);
  43107. if (!mp_iszero(a))
  43108. return WC_TEST_RET_ENC_NC;
  43109. return 0;
  43110. }
  43111. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43112. static wc_test_ret_t mp_test_set_int(mp_int* a)
  43113. {
  43114. #if SP_ULONG_BITS == 64
  43115. unsigned long n = 0xfedcba9876543210UL;
  43116. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  43117. byte out[8] = { 0 };
  43118. #elif SP_ULONG_BITS == 32
  43119. unsigned long n = 0xfedcba98UL;
  43120. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  43121. byte out[4] = { 0 };
  43122. #elif SP_ULONG_BITS == 16
  43123. unsigned long n = 0xfedc;
  43124. byte exp[2] = { 0xfe, 0xdc };
  43125. byte out[2] = { 0 };
  43126. #elif SP_ULONG_BITS == 8
  43127. unsigned long n = 0xfe;
  43128. byte exp[1] = { 0xfe };
  43129. byte out[1] = { 0 };
  43130. #endif
  43131. wc_test_ret_t ret;
  43132. ret = mp_set_int(a, n);
  43133. if (ret != 0)
  43134. return WC_TEST_RET_ENC_EC(ret);
  43135. ret = mp_unsigned_bin_size(a);
  43136. if (ret != sizeof(exp))
  43137. return WC_TEST_RET_ENC_NC;
  43138. ret = mp_to_unsigned_bin(a, out);
  43139. if (ret != 0)
  43140. return WC_TEST_RET_ENC_EC(ret);
  43141. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  43142. return WC_TEST_RET_ENC_NC;
  43143. return 0;
  43144. }
  43145. #endif
  43146. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43147. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  43148. {
  43149. byte buffer[16];
  43150. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  43151. char hexStr[] = "abcdef0123456789";
  43152. #ifndef WOLFSSL_SP_INT_NEGATIVE
  43153. char negStr[] = "-1234";
  43154. #endif
  43155. #endif
  43156. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  43157. defined(HAVE_COMP_KEY)
  43158. char decStr[] = "0987654321";
  43159. #endif
  43160. wc_test_ret_t ret;
  43161. #ifdef WOLFSSL_SP_MATH_ALL
  43162. mp_digit rho;
  43163. int size;
  43164. #endif
  43165. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43166. int result;
  43167. #endif
  43168. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  43169. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  43170. mp_digit rd;
  43171. #endif
  43172. (void)rng;
  43173. (void)r;
  43174. ret = mp_init(NULL);
  43175. if (ret != MP_VAL)
  43176. return WC_TEST_RET_ENC_EC(ret);
  43177. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  43178. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  43179. if (ret != MP_OKAY)
  43180. return WC_TEST_RET_ENC_EC(ret);
  43181. #endif
  43182. mp_free(NULL);
  43183. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  43184. ret = mp_grow(NULL, 1);
  43185. if (ret != MP_VAL)
  43186. return WC_TEST_RET_ENC_EC(ret);
  43187. #ifdef WOLFSSL_SP_MATH
  43188. ret = mp_grow(a, SP_INT_DIGITS + 1);
  43189. if (ret != MP_MEM)
  43190. return WC_TEST_RET_ENC_EC(ret);
  43191. #endif
  43192. #endif
  43193. mp_clear(NULL);
  43194. ret = mp_abs(NULL, NULL);
  43195. if (ret != MP_VAL)
  43196. return WC_TEST_RET_ENC_EC(ret);
  43197. ret = mp_abs(a, NULL);
  43198. if (ret != MP_VAL)
  43199. return WC_TEST_RET_ENC_EC(ret);
  43200. ret = mp_abs(NULL, b);
  43201. if (ret != MP_VAL)
  43202. return WC_TEST_RET_ENC_EC(ret);
  43203. ret = mp_unsigned_bin_size(NULL);
  43204. if (ret != 0)
  43205. return WC_TEST_RET_ENC_EC(ret);
  43206. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  43207. if (ret != MP_VAL)
  43208. return WC_TEST_RET_ENC_EC(ret);
  43209. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  43210. if (ret != MP_VAL)
  43211. return WC_TEST_RET_ENC_EC(ret);
  43212. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  43213. if (ret != MP_VAL)
  43214. return WC_TEST_RET_ENC_EC(ret);
  43215. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  43216. if (ret != MP_VAL)
  43217. return WC_TEST_RET_ENC_EC(ret);
  43218. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  43219. ret = mp_read_radix(NULL, NULL, 16);
  43220. if (ret != MP_VAL)
  43221. return WC_TEST_RET_ENC_EC(ret);
  43222. ret = mp_read_radix(a, NULL, 16);
  43223. if (ret != MP_VAL)
  43224. return WC_TEST_RET_ENC_EC(ret);
  43225. ret = mp_read_radix(NULL, hexStr, 16);
  43226. if (ret != MP_VAL)
  43227. return WC_TEST_RET_ENC_EC(ret);
  43228. #ifndef WOLFSSL_SP_INT_NEGATIVE
  43229. ret = mp_read_radix(a, negStr, 16);
  43230. if (ret != MP_VAL)
  43231. return WC_TEST_RET_ENC_EC(ret);
  43232. #ifdef WOLFSSL_SP_MATH_ALL
  43233. ret = mp_read_radix(a, negStr, 10);
  43234. if (ret != MP_VAL)
  43235. return WC_TEST_RET_ENC_EC(ret);
  43236. #endif /* WOLFSSL_SP_MATH_ALL */
  43237. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  43238. #endif
  43239. #ifndef WOLFSSL_SP_MATH_ALL
  43240. /* Radix 10 only supported with ALL. */
  43241. ret = mp_read_radix(a, decStr, 10);
  43242. if (ret != MP_VAL)
  43243. return WC_TEST_RET_ENC_EC(ret);
  43244. #endif
  43245. /* Radix 8 not supported SP_INT. */
  43246. ret = mp_read_radix(a, "0123", 8);
  43247. if (ret != MP_VAL)
  43248. return WC_TEST_RET_ENC_EC(ret);
  43249. ret = mp_count_bits(NULL);
  43250. if (ret != 0)
  43251. return WC_TEST_RET_ENC_EC(ret);
  43252. ret = mp_is_bit_set(NULL, 0);
  43253. if (ret != 0)
  43254. return WC_TEST_RET_ENC_EC(ret);
  43255. ret = mp_leading_bit(NULL);
  43256. if (ret != 0)
  43257. return WC_TEST_RET_ENC_EC(ret);
  43258. mp_zero(a);
  43259. ret = mp_leading_bit(a);
  43260. if (ret != 0)
  43261. return WC_TEST_RET_ENC_EC(ret);
  43262. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  43263. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  43264. !defined(NO_RSA)
  43265. ret = mp_set_bit(NULL, 1);
  43266. if (ret != MP_VAL)
  43267. return WC_TEST_RET_ENC_EC(ret);
  43268. #endif
  43269. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  43270. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43271. ret = mp_to_unsigned_bin(NULL, NULL);
  43272. if (ret != MP_VAL)
  43273. return WC_TEST_RET_ENC_EC(ret);
  43274. ret = mp_to_unsigned_bin(a, NULL);
  43275. if (ret != MP_VAL)
  43276. return WC_TEST_RET_ENC_EC(ret);
  43277. ret = mp_to_unsigned_bin(NULL, buffer);
  43278. if (ret != MP_VAL)
  43279. return WC_TEST_RET_ENC_EC(ret);
  43280. #endif
  43281. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  43282. if (ret != MP_VAL)
  43283. return WC_TEST_RET_ENC_EC(ret);
  43284. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  43285. if (ret != MP_VAL)
  43286. return WC_TEST_RET_ENC_EC(ret);
  43287. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  43288. if (ret != MP_VAL)
  43289. return WC_TEST_RET_ENC_EC(ret);
  43290. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  43291. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43292. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  43293. if (ret != MP_VAL)
  43294. return WC_TEST_RET_ENC_EC(ret);
  43295. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  43296. if (ret != MP_VAL)
  43297. return WC_TEST_RET_ENC_EC(ret);
  43298. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  43299. if (ret != MP_VAL)
  43300. return WC_TEST_RET_ENC_EC(ret);
  43301. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  43302. if (ret != MP_OKAY)
  43303. return WC_TEST_RET_ENC_EC(ret);
  43304. #endif
  43305. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  43306. ret = mp_copy(NULL, NULL);
  43307. if (ret != MP_VAL)
  43308. return WC_TEST_RET_ENC_EC(ret);
  43309. ret = mp_copy(a, NULL);
  43310. if (ret != MP_VAL)
  43311. return WC_TEST_RET_ENC_EC(ret);
  43312. ret = mp_copy(NULL, b);
  43313. if (ret != MP_VAL)
  43314. return WC_TEST_RET_ENC_EC(ret);
  43315. #endif
  43316. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  43317. ret = sp_2expt(NULL, 1);
  43318. if (ret != MP_VAL)
  43319. return WC_TEST_RET_ENC_EC(ret);
  43320. #endif
  43321. ret = mp_set(NULL, 0);
  43322. if (ret != MP_VAL)
  43323. return WC_TEST_RET_ENC_EC(ret);
  43324. ret = mp_cmp_d(NULL, 0);
  43325. if (ret != MP_LT)
  43326. return WC_TEST_RET_ENC_EC(ret);
  43327. ret = mp_cmp(NULL, NULL);
  43328. if (ret != MP_EQ)
  43329. return WC_TEST_RET_ENC_NC;
  43330. ret = mp_cmp(a, NULL);
  43331. if (ret != MP_GT)
  43332. return WC_TEST_RET_ENC_NC;
  43333. ret = mp_cmp(NULL, b);
  43334. if (ret != MP_LT)
  43335. return WC_TEST_RET_ENC_NC;
  43336. #ifdef WOLFSSL_SP_MATH_ALL
  43337. mp_rshd(NULL, 1);
  43338. #endif
  43339. mp_zero(NULL);
  43340. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  43341. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43342. ret = mp_lshd(NULL, 0);
  43343. if (ret != MP_VAL)
  43344. return WC_TEST_RET_ENC_EC(ret);
  43345. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  43346. if (ret != MP_VAL)
  43347. return WC_TEST_RET_ENC_EC(ret);
  43348. #endif
  43349. #if defined(WOLFSSL_SP_MATH_ALL)
  43350. ret = mp_div(NULL, NULL, a, b);
  43351. if (ret != MP_VAL)
  43352. return WC_TEST_RET_ENC_EC(ret);
  43353. ret = mp_div(a, NULL, a, b);
  43354. if (ret != MP_VAL)
  43355. return WC_TEST_RET_ENC_EC(ret);
  43356. ret = mp_div(NULL, b, a, b);
  43357. if (ret != MP_VAL)
  43358. return WC_TEST_RET_ENC_EC(ret);
  43359. ret = mp_div(a, b, NULL, NULL);
  43360. if (ret != MP_VAL)
  43361. return WC_TEST_RET_ENC_EC(ret);
  43362. #endif
  43363. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  43364. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43365. ret = mp_mod(NULL, NULL, NULL);
  43366. if (ret != MP_VAL)
  43367. return WC_TEST_RET_ENC_EC(ret);
  43368. ret = mp_mod(a, NULL, NULL);
  43369. if (ret != MP_VAL)
  43370. return WC_TEST_RET_ENC_EC(ret);
  43371. ret = mp_mod(NULL, b, NULL);
  43372. if (ret != MP_VAL)
  43373. return WC_TEST_RET_ENC_EC(ret);
  43374. ret = mp_mod(NULL, NULL, r);
  43375. if (ret != MP_VAL)
  43376. return WC_TEST_RET_ENC_EC(ret);
  43377. ret = mp_mod(a, b, NULL);
  43378. if (ret != MP_VAL)
  43379. return WC_TEST_RET_ENC_EC(ret);
  43380. ret = mp_mod(a, NULL, r);
  43381. if (ret != MP_VAL)
  43382. return WC_TEST_RET_ENC_EC(ret);
  43383. ret = mp_mod(NULL, b, r);
  43384. if (ret != MP_VAL)
  43385. return WC_TEST_RET_ENC_EC(ret);
  43386. #endif
  43387. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  43388. ret = mp_set_int(NULL, 0);
  43389. if (ret != MP_VAL)
  43390. return WC_TEST_RET_ENC_EC(ret);
  43391. #endif
  43392. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  43393. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  43394. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  43395. if (ret != MP_VAL)
  43396. return WC_TEST_RET_ENC_EC(ret);
  43397. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  43398. if (ret != MP_VAL)
  43399. return WC_TEST_RET_ENC_EC(ret);
  43400. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  43401. if (ret != MP_VAL)
  43402. return WC_TEST_RET_ENC_EC(ret);
  43403. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  43404. if (ret != MP_VAL)
  43405. return WC_TEST_RET_ENC_EC(ret);
  43406. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  43407. if (ret != MP_VAL)
  43408. return WC_TEST_RET_ENC_EC(ret);
  43409. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  43410. if (ret != MP_VAL)
  43411. return WC_TEST_RET_ENC_EC(ret);
  43412. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  43413. if (ret != MP_VAL)
  43414. return WC_TEST_RET_ENC_EC(ret);
  43415. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  43416. if (ret != MP_VAL)
  43417. return WC_TEST_RET_ENC_EC(ret);
  43418. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  43419. if (ret != MP_VAL)
  43420. return WC_TEST_RET_ENC_EC(ret);
  43421. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  43422. if (ret != MP_VAL)
  43423. return WC_TEST_RET_ENC_EC(ret);
  43424. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  43425. if (ret != MP_VAL)
  43426. return WC_TEST_RET_ENC_EC(ret);
  43427. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  43428. if (ret != MP_VAL)
  43429. return WC_TEST_RET_ENC_EC(ret);
  43430. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  43431. if (ret != MP_VAL)
  43432. return WC_TEST_RET_ENC_EC(ret);
  43433. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  43434. if (ret != MP_VAL)
  43435. return WC_TEST_RET_ENC_EC(ret);
  43436. ret = mp_exptmod_nct(a, a, a, NULL);
  43437. if (ret != MP_VAL)
  43438. return WC_TEST_RET_ENC_EC(ret);
  43439. ret = mp_exptmod_nct(a, a, NULL, a);
  43440. if (ret != MP_VAL)
  43441. return WC_TEST_RET_ENC_EC(ret);
  43442. ret = mp_exptmod_nct(a, NULL, a, a);
  43443. if (ret != MP_VAL)
  43444. return WC_TEST_RET_ENC_EC(ret);
  43445. ret = mp_exptmod_nct(NULL, a, a, a);
  43446. if (ret != MP_VAL)
  43447. return WC_TEST_RET_ENC_EC(ret);
  43448. #endif
  43449. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  43450. !defined(WC_NO_RNG)
  43451. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  43452. if (ret != MP_VAL)
  43453. return WC_TEST_RET_ENC_EC(ret);
  43454. ret = mp_rand_prime(a, 32, NULL, NULL);
  43455. if (ret != MP_VAL)
  43456. return WC_TEST_RET_ENC_EC(ret);
  43457. ret = mp_rand_prime(NULL, 32, rng, NULL);
  43458. if (ret != MP_VAL)
  43459. return WC_TEST_RET_ENC_EC(ret);
  43460. ret = mp_rand_prime(a, 0, rng, NULL);
  43461. if (ret != MP_VAL)
  43462. return WC_TEST_RET_ENC_EC(ret);
  43463. #endif
  43464. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43465. ret = mp_mul(NULL, NULL, NULL);
  43466. if (ret != MP_VAL)
  43467. return WC_TEST_RET_ENC_EC(ret);
  43468. ret = mp_mul(a, NULL, NULL);
  43469. if (ret != MP_VAL)
  43470. return WC_TEST_RET_ENC_EC(ret);
  43471. ret = mp_mul(NULL, b, NULL);
  43472. if (ret != MP_VAL)
  43473. return WC_TEST_RET_ENC_EC(ret);
  43474. ret = mp_mul(NULL, NULL, r);
  43475. if (ret != MP_VAL)
  43476. return WC_TEST_RET_ENC_EC(ret);
  43477. ret = mp_mul(a, b, NULL);
  43478. if (ret != MP_VAL)
  43479. return WC_TEST_RET_ENC_EC(ret);
  43480. ret = mp_mul(a, NULL, r);
  43481. if (ret != MP_VAL)
  43482. return WC_TEST_RET_ENC_EC(ret);
  43483. ret = mp_mul(NULL, b, r);
  43484. if (ret != MP_VAL)
  43485. return WC_TEST_RET_ENC_EC(ret);
  43486. #endif
  43487. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  43488. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43489. ret = mp_sqr(NULL, NULL);
  43490. if (ret != MP_VAL)
  43491. return WC_TEST_RET_ENC_EC(ret);
  43492. ret = mp_sqr(a, NULL);
  43493. if (ret != MP_VAL)
  43494. return WC_TEST_RET_ENC_EC(ret);
  43495. ret = mp_sqr(NULL, r);
  43496. if (ret != MP_VAL)
  43497. return WC_TEST_RET_ENC_EC(ret);
  43498. #endif
  43499. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43500. ret = mp_sqrmod(NULL, NULL, NULL);
  43501. if (ret != MP_VAL)
  43502. return WC_TEST_RET_ENC_EC(ret);
  43503. ret = mp_sqrmod(a, NULL, NULL);
  43504. if (ret != MP_VAL)
  43505. return WC_TEST_RET_ENC_EC(ret);
  43506. ret = mp_sqrmod(NULL, a, NULL);
  43507. if (ret != MP_VAL)
  43508. return WC_TEST_RET_ENC_EC(ret);
  43509. ret = mp_sqrmod(NULL, NULL, a);
  43510. if (ret != MP_VAL)
  43511. return WC_TEST_RET_ENC_EC(ret);
  43512. ret = mp_sqrmod(a, b, NULL);
  43513. if (ret != MP_VAL)
  43514. return WC_TEST_RET_ENC_EC(ret);
  43515. ret = mp_sqrmod(a, NULL, b);
  43516. if (ret != MP_VAL)
  43517. return WC_TEST_RET_ENC_EC(ret);
  43518. ret = mp_sqrmod(NULL, a, b);
  43519. if (ret != MP_VAL)
  43520. return WC_TEST_RET_ENC_EC(ret);
  43521. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  43522. if (ret != MP_VAL)
  43523. return WC_TEST_RET_ENC_EC(ret);
  43524. ret = mp_mulmod(a, NULL, NULL, NULL);
  43525. if (ret != MP_VAL)
  43526. return WC_TEST_RET_ENC_EC(ret);
  43527. ret = mp_mulmod(NULL, a, NULL, NULL);
  43528. if (ret != MP_VAL)
  43529. return WC_TEST_RET_ENC_EC(ret);
  43530. ret = mp_mulmod(NULL, NULL, a, NULL);
  43531. if (ret != MP_VAL)
  43532. return WC_TEST_RET_ENC_EC(ret);
  43533. ret = mp_mulmod(NULL, NULL, NULL, a);
  43534. if (ret != MP_VAL)
  43535. return WC_TEST_RET_ENC_EC(ret);
  43536. ret = mp_mulmod(a, b, b, NULL);
  43537. if (ret != MP_VAL)
  43538. return WC_TEST_RET_ENC_EC(ret);
  43539. ret = mp_mulmod(a, b, NULL, a);
  43540. if (ret != MP_VAL)
  43541. return WC_TEST_RET_ENC_EC(ret);
  43542. ret = mp_mulmod(a, NULL, b, a);
  43543. if (ret != MP_VAL)
  43544. return WC_TEST_RET_ENC_EC(ret);
  43545. ret = mp_mulmod(NULL, b, b, a);
  43546. if (ret != MP_VAL)
  43547. return WC_TEST_RET_ENC_EC(ret);
  43548. #endif
  43549. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  43550. !defined(NO_RSA) || !defined(NO_DSA)
  43551. ret = mp_add_d(NULL, 1, NULL);
  43552. if (ret != MP_VAL)
  43553. return WC_TEST_RET_ENC_EC(ret);
  43554. ret = mp_add_d(a, 1, NULL);
  43555. if (ret != MP_VAL)
  43556. return WC_TEST_RET_ENC_EC(ret);
  43557. ret = mp_add_d(NULL, 1, b);
  43558. if (ret != MP_VAL)
  43559. return WC_TEST_RET_ENC_EC(ret);
  43560. #endif
  43561. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43562. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  43563. ret = mp_sub_d(NULL, 1, NULL);
  43564. if (ret != MP_VAL)
  43565. return WC_TEST_RET_ENC_EC(ret);
  43566. ret = mp_sub_d(a, 1, NULL);
  43567. if (ret != MP_VAL)
  43568. return WC_TEST_RET_ENC_EC(ret);
  43569. ret = mp_sub_d(NULL, 1, b);
  43570. if (ret != MP_VAL)
  43571. return WC_TEST_RET_ENC_EC(ret);
  43572. #endif
  43573. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43574. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  43575. ret = mp_div_d(NULL, 0, NULL, NULL);
  43576. if (ret != MP_VAL)
  43577. return WC_TEST_RET_ENC_EC(ret);
  43578. ret = mp_div_d(a, 0, NULL, NULL);
  43579. if (ret != MP_VAL)
  43580. return WC_TEST_RET_ENC_EC(ret);
  43581. ret = mp_div_d(NULL, 1, NULL, NULL);
  43582. if (ret != MP_VAL)
  43583. return WC_TEST_RET_ENC_EC(ret);
  43584. #endif
  43585. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  43586. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  43587. ret = mp_mod_d(NULL, 0, NULL);
  43588. if (ret != MP_VAL)
  43589. return WC_TEST_RET_ENC_EC(ret);
  43590. ret = mp_mod_d(a, 0, NULL);
  43591. if (ret != MP_VAL)
  43592. return WC_TEST_RET_ENC_EC(ret);
  43593. ret = mp_mod_d(NULL, 0, &rd);
  43594. if (ret != MP_VAL)
  43595. return WC_TEST_RET_ENC_EC(ret);
  43596. #endif
  43597. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  43598. ret = mp_gcd(NULL, NULL, NULL);
  43599. if (ret != MP_VAL)
  43600. return WC_TEST_RET_ENC_EC(ret);
  43601. ret = mp_gcd(a, NULL, NULL);
  43602. if (ret != MP_VAL)
  43603. return WC_TEST_RET_ENC_EC(ret);
  43604. ret = mp_gcd(NULL, a, NULL);
  43605. if (ret != MP_VAL)
  43606. return WC_TEST_RET_ENC_EC(ret);
  43607. ret = mp_gcd(NULL, NULL, a);
  43608. if (ret != MP_VAL)
  43609. return WC_TEST_RET_ENC_EC(ret);
  43610. ret = mp_gcd(a, b, NULL);
  43611. if (ret != MP_VAL)
  43612. return WC_TEST_RET_ENC_EC(ret);
  43613. ret = mp_gcd(a, NULL, b);
  43614. if (ret != MP_VAL)
  43615. return WC_TEST_RET_ENC_EC(ret);
  43616. ret = mp_gcd(NULL, a, b);
  43617. if (ret != MP_VAL)
  43618. return WC_TEST_RET_ENC_EC(ret);
  43619. #endif
  43620. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  43621. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  43622. if (ret != MP_VAL)
  43623. return WC_TEST_RET_ENC_EC(ret);
  43624. ret = mp_div_2_mod_ct(a, NULL, NULL);
  43625. if (ret != MP_VAL)
  43626. return WC_TEST_RET_ENC_EC(ret);
  43627. ret = mp_div_2_mod_ct(NULL, b, NULL);
  43628. if (ret != MP_VAL)
  43629. return WC_TEST_RET_ENC_EC(ret);
  43630. ret = mp_div_2_mod_ct(NULL, NULL, a);
  43631. if (ret != MP_VAL)
  43632. return WC_TEST_RET_ENC_EC(ret);
  43633. ret = mp_div_2_mod_ct(a, b, NULL);
  43634. if (ret != MP_VAL)
  43635. return WC_TEST_RET_ENC_EC(ret);
  43636. ret = mp_div_2_mod_ct(a, b, NULL);
  43637. if (ret != MP_VAL)
  43638. return WC_TEST_RET_ENC_EC(ret);
  43639. ret = mp_div_2_mod_ct(NULL, b, a);
  43640. if (ret != MP_VAL)
  43641. return WC_TEST_RET_ENC_EC(ret);
  43642. ret = mp_div_2(NULL, NULL);
  43643. if (ret != MP_VAL)
  43644. return WC_TEST_RET_ENC_EC(ret);
  43645. ret = mp_div_2(a, NULL);
  43646. if (ret != MP_VAL)
  43647. return WC_TEST_RET_ENC_EC(ret);
  43648. ret = mp_div_2(NULL, a);
  43649. if (ret != MP_VAL)
  43650. return WC_TEST_RET_ENC_EC(ret);
  43651. #endif
  43652. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43653. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  43654. ret = mp_invmod(NULL, NULL, NULL);
  43655. if (ret != MP_VAL)
  43656. return WC_TEST_RET_ENC_EC(ret);
  43657. ret = mp_invmod(a, NULL, NULL);
  43658. if (ret != MP_VAL)
  43659. return WC_TEST_RET_ENC_EC(ret);
  43660. ret = mp_invmod(NULL, b, NULL);
  43661. if (ret != MP_VAL)
  43662. return WC_TEST_RET_ENC_EC(ret);
  43663. ret = mp_invmod(NULL, NULL, a);
  43664. if (ret != MP_VAL)
  43665. return WC_TEST_RET_ENC_EC(ret);
  43666. ret = mp_invmod(a, b, NULL);
  43667. if (ret != MP_VAL)
  43668. return WC_TEST_RET_ENC_EC(ret);
  43669. ret = mp_invmod(a, NULL, a);
  43670. if (ret != MP_VAL)
  43671. return WC_TEST_RET_ENC_EC(ret);
  43672. ret = mp_invmod(NULL, b, a);
  43673. if (ret != MP_VAL)
  43674. return WC_TEST_RET_ENC_EC(ret);
  43675. #endif
  43676. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  43677. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  43678. if (ret != MP_VAL)
  43679. return WC_TEST_RET_ENC_EC(ret);
  43680. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  43681. if (ret != MP_VAL)
  43682. return WC_TEST_RET_ENC_EC(ret);
  43683. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  43684. if (ret != MP_VAL)
  43685. return WC_TEST_RET_ENC_EC(ret);
  43686. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  43687. if (ret != MP_VAL)
  43688. return WC_TEST_RET_ENC_EC(ret);
  43689. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  43690. if (ret != MP_VAL)
  43691. return WC_TEST_RET_ENC_EC(ret);
  43692. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  43693. if (ret != MP_VAL)
  43694. return WC_TEST_RET_ENC_EC(ret);
  43695. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  43696. if (ret != MP_VAL)
  43697. return WC_TEST_RET_ENC_EC(ret);
  43698. #endif
  43699. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  43700. ret = mp_lcm(NULL, NULL, NULL);
  43701. if (ret != MP_VAL)
  43702. return WC_TEST_RET_ENC_EC(ret);
  43703. ret = mp_lcm(a, NULL, NULL);
  43704. if (ret != MP_VAL)
  43705. return WC_TEST_RET_ENC_EC(ret);
  43706. ret = mp_lcm(NULL, b, NULL);
  43707. if (ret != MP_VAL)
  43708. return WC_TEST_RET_ENC_EC(ret);
  43709. ret = mp_lcm(NULL, NULL, a);
  43710. if (ret != MP_VAL)
  43711. return WC_TEST_RET_ENC_EC(ret);
  43712. ret = mp_lcm(a, b, NULL);
  43713. if (ret != MP_VAL)
  43714. return WC_TEST_RET_ENC_EC(ret);
  43715. ret = mp_lcm(a, NULL, a);
  43716. if (ret != MP_VAL)
  43717. return WC_TEST_RET_ENC_EC(ret);
  43718. ret = mp_lcm(NULL, b, a);
  43719. if (ret != MP_VAL)
  43720. return WC_TEST_RET_ENC_EC(ret);
  43721. #endif
  43722. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43723. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  43724. if (ret != MP_VAL)
  43725. return WC_TEST_RET_ENC_EC(ret);
  43726. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  43727. if (ret != MP_VAL)
  43728. return WC_TEST_RET_ENC_EC(ret);
  43729. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  43730. if (ret != MP_VAL)
  43731. return WC_TEST_RET_ENC_EC(ret);
  43732. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  43733. if (ret != MP_VAL)
  43734. return WC_TEST_RET_ENC_EC(ret);
  43735. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  43736. if (ret != MP_VAL)
  43737. return WC_TEST_RET_ENC_EC(ret);
  43738. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  43739. if (ret != MP_VAL)
  43740. return WC_TEST_RET_ENC_EC(ret);
  43741. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  43742. if (ret != MP_VAL)
  43743. return WC_TEST_RET_ENC_EC(ret);
  43744. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  43745. if (ret != MP_VAL)
  43746. return WC_TEST_RET_ENC_EC(ret);
  43747. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  43748. if (ret != MP_VAL)
  43749. return WC_TEST_RET_ENC_EC(ret);
  43750. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  43751. if (ret != MP_VAL)
  43752. return WC_TEST_RET_ENC_EC(ret);
  43753. ret = mp_exptmod(a, NULL, NULL, NULL);
  43754. if (ret != MP_VAL)
  43755. return WC_TEST_RET_ENC_EC(ret);
  43756. ret = mp_exptmod(NULL, b, NULL, NULL);
  43757. if (ret != MP_VAL)
  43758. return WC_TEST_RET_ENC_EC(ret);
  43759. ret = mp_exptmod(NULL, NULL, b, NULL);
  43760. if (ret != MP_VAL)
  43761. return WC_TEST_RET_ENC_EC(ret);
  43762. ret = mp_exptmod(NULL, NULL, NULL, a);
  43763. if (ret != MP_VAL)
  43764. return WC_TEST_RET_ENC_EC(ret);
  43765. ret = mp_exptmod(a, b, b, NULL);
  43766. if (ret != MP_VAL)
  43767. return WC_TEST_RET_ENC_EC(ret);
  43768. ret = mp_exptmod(a, b, NULL, a);
  43769. if (ret != MP_VAL)
  43770. return WC_TEST_RET_ENC_EC(ret);
  43771. ret = mp_exptmod(a, NULL, b, a);
  43772. if (ret != MP_VAL)
  43773. return WC_TEST_RET_ENC_EC(ret);
  43774. ret = mp_exptmod(NULL, b, b, a);
  43775. if (ret != MP_VAL)
  43776. return WC_TEST_RET_ENC_EC(ret);
  43777. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  43778. if (ret != MP_VAL)
  43779. return WC_TEST_RET_ENC_EC(ret);
  43780. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  43781. if (ret != MP_VAL)
  43782. return WC_TEST_RET_ENC_EC(ret);
  43783. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  43784. if (ret != MP_VAL)
  43785. return WC_TEST_RET_ENC_EC(ret);
  43786. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  43787. if (ret != MP_VAL)
  43788. return WC_TEST_RET_ENC_EC(ret);
  43789. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  43790. if (ret != MP_VAL)
  43791. return WC_TEST_RET_ENC_EC(ret);
  43792. ret = mp_exptmod_nct(a, b, b, NULL);
  43793. if (ret != MP_VAL)
  43794. return WC_TEST_RET_ENC_EC(ret);
  43795. ret = mp_exptmod_nct(a, b, NULL, a);
  43796. if (ret != MP_VAL)
  43797. return WC_TEST_RET_ENC_EC(ret);
  43798. ret = mp_exptmod_nct(a, NULL, b, a);
  43799. if (ret != MP_VAL)
  43800. return WC_TEST_RET_ENC_EC(ret);
  43801. ret = mp_exptmod_nct(NULL, b, b, a);
  43802. if (ret != MP_VAL)
  43803. return WC_TEST_RET_ENC_EC(ret);
  43804. #endif
  43805. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  43806. ret = mp_cnt_lsb(NULL);
  43807. if (ret != 0)
  43808. return WC_TEST_RET_ENC_EC(ret);
  43809. #endif
  43810. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43811. ret = mp_prime_is_prime(NULL, 1, NULL);
  43812. if (ret != MP_VAL)
  43813. return WC_TEST_RET_ENC_EC(ret);
  43814. ret = mp_prime_is_prime(a, 1, NULL);
  43815. if (ret != MP_VAL)
  43816. return WC_TEST_RET_ENC_EC(ret);
  43817. ret = mp_prime_is_prime(NULL, 1, &result);
  43818. if (ret != MP_VAL)
  43819. return WC_TEST_RET_ENC_EC(ret);
  43820. ret = mp_prime_is_prime(a, 0, &result);
  43821. if (ret != MP_VAL)
  43822. return WC_TEST_RET_ENC_EC(ret);
  43823. ret = mp_prime_is_prime(a, 1024, &result);
  43824. if (ret != MP_VAL)
  43825. return WC_TEST_RET_ENC_EC(ret);
  43826. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  43827. if (ret != MP_VAL)
  43828. return WC_TEST_RET_ENC_EC(ret);
  43829. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  43830. if (ret != MP_VAL)
  43831. return WC_TEST_RET_ENC_EC(ret);
  43832. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  43833. if (ret != MP_VAL)
  43834. return WC_TEST_RET_ENC_EC(ret);
  43835. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  43836. if (ret != MP_VAL)
  43837. return WC_TEST_RET_ENC_EC(ret);
  43838. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  43839. if (ret != MP_VAL)
  43840. return WC_TEST_RET_ENC_EC(ret);
  43841. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  43842. if (ret != MP_VAL)
  43843. return WC_TEST_RET_ENC_EC(ret);
  43844. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  43845. if (ret != MP_VAL)
  43846. return WC_TEST_RET_ENC_EC(ret);
  43847. #endif
  43848. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  43849. ret = mp_exch(NULL, NULL);
  43850. if (ret != MP_VAL)
  43851. return WC_TEST_RET_ENC_EC(ret);
  43852. ret = mp_exch(a, NULL);
  43853. if (ret != MP_VAL)
  43854. return WC_TEST_RET_ENC_EC(ret);
  43855. ret = mp_exch(NULL, b);
  43856. if (ret != MP_VAL)
  43857. return WC_TEST_RET_ENC_EC(ret);
  43858. #endif
  43859. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  43860. defined(WOLFSSL_SP_MATH_ALL)
  43861. ret = mp_mul_d(NULL, 1, NULL);
  43862. if (ret != MP_VAL)
  43863. return WC_TEST_RET_ENC_EC(ret);
  43864. ret = mp_mul_d(a, 1, NULL);
  43865. if (ret != MP_VAL)
  43866. return WC_TEST_RET_ENC_EC(ret);
  43867. ret = mp_mul_d(NULL, 1, b);
  43868. if (ret != MP_VAL)
  43869. return WC_TEST_RET_ENC_EC(ret);
  43870. #endif
  43871. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43872. ret = mp_add(NULL, NULL, NULL);
  43873. if (ret != MP_VAL)
  43874. return WC_TEST_RET_ENC_EC(ret);
  43875. ret = mp_add(a, NULL, NULL);
  43876. if (ret != MP_VAL)
  43877. return WC_TEST_RET_ENC_EC(ret);
  43878. ret = mp_add(NULL, b, NULL);
  43879. if (ret != MP_VAL)
  43880. return WC_TEST_RET_ENC_EC(ret);
  43881. ret = mp_add(NULL, NULL, r);
  43882. if (ret != MP_VAL)
  43883. return WC_TEST_RET_ENC_EC(ret);
  43884. ret = mp_add(a, b, NULL);
  43885. if (ret != MP_VAL)
  43886. return WC_TEST_RET_ENC_EC(ret);
  43887. ret = mp_add(a, NULL, r);
  43888. if (ret != MP_VAL)
  43889. return WC_TEST_RET_ENC_EC(ret);
  43890. ret = mp_add(NULL, b, r);
  43891. if (ret != MP_VAL)
  43892. return WC_TEST_RET_ENC_EC(ret);
  43893. #endif
  43894. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  43895. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43896. ret = mp_sub(NULL, NULL, NULL);
  43897. if (ret != MP_VAL)
  43898. return WC_TEST_RET_ENC_EC(ret);
  43899. ret = mp_sub(a, NULL, NULL);
  43900. if (ret != MP_VAL)
  43901. return WC_TEST_RET_ENC_EC(ret);
  43902. ret = mp_sub(NULL, b, NULL);
  43903. if (ret != MP_VAL)
  43904. return WC_TEST_RET_ENC_EC(ret);
  43905. ret = mp_sub(NULL, NULL, r);
  43906. if (ret != MP_VAL)
  43907. return WC_TEST_RET_ENC_EC(ret);
  43908. ret = mp_sub(a, b, NULL);
  43909. if (ret != MP_VAL)
  43910. return WC_TEST_RET_ENC_EC(ret);
  43911. ret = mp_sub(a, NULL, r);
  43912. if (ret != MP_VAL)
  43913. return WC_TEST_RET_ENC_EC(ret);
  43914. ret = mp_sub(NULL, b, r);
  43915. if (ret != MP_VAL)
  43916. return WC_TEST_RET_ENC_EC(ret);
  43917. #endif
  43918. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  43919. defined(WOLFSSL_CUSTOM_CURVES))
  43920. ret = mp_addmod(NULL, NULL, NULL, NULL);
  43921. if (ret != MP_VAL)
  43922. return WC_TEST_RET_ENC_EC(ret);
  43923. ret = mp_addmod(a, NULL, NULL, NULL);
  43924. if (ret != MP_VAL)
  43925. return WC_TEST_RET_ENC_EC(ret);
  43926. ret = mp_addmod(NULL, b, NULL, NULL);
  43927. if (ret != MP_VAL)
  43928. return WC_TEST_RET_ENC_EC(ret);
  43929. ret = mp_addmod(NULL, NULL, b, NULL);
  43930. if (ret != MP_VAL)
  43931. return WC_TEST_RET_ENC_EC(ret);
  43932. ret = mp_addmod(NULL, NULL, NULL, a);
  43933. if (ret != MP_VAL)
  43934. return WC_TEST_RET_ENC_EC(ret);
  43935. ret = mp_addmod(a, b, b, NULL);
  43936. if (ret != MP_VAL)
  43937. return WC_TEST_RET_ENC_EC(ret);
  43938. ret = mp_addmod(a, b, NULL, a);
  43939. if (ret != MP_VAL)
  43940. return WC_TEST_RET_ENC_EC(ret);
  43941. ret = mp_addmod(a, NULL, b, a);
  43942. if (ret != MP_VAL)
  43943. return WC_TEST_RET_ENC_EC(ret);
  43944. ret = mp_addmod(NULL, b, b, a);
  43945. if (ret != MP_VAL)
  43946. return WC_TEST_RET_ENC_EC(ret);
  43947. #endif
  43948. #ifdef WOLFSSL_SP_MATH_ALL
  43949. ret = mp_submod(NULL, NULL, NULL, NULL);
  43950. if (ret != MP_VAL)
  43951. return WC_TEST_RET_ENC_EC(ret);
  43952. ret = mp_submod(a, NULL, NULL, NULL);
  43953. if (ret != MP_VAL)
  43954. return WC_TEST_RET_ENC_EC(ret);
  43955. ret = mp_submod(NULL, b, NULL, NULL);
  43956. if (ret != MP_VAL)
  43957. return WC_TEST_RET_ENC_EC(ret);
  43958. ret = mp_submod(NULL, NULL, b, NULL);
  43959. if (ret != MP_VAL)
  43960. return WC_TEST_RET_ENC_EC(ret);
  43961. ret = mp_submod(NULL, NULL, NULL, a);
  43962. if (ret != MP_VAL)
  43963. return WC_TEST_RET_ENC_EC(ret);
  43964. ret = mp_submod(a, b, b, NULL);
  43965. if (ret != MP_VAL)
  43966. return WC_TEST_RET_ENC_EC(ret);
  43967. ret = mp_submod(a, b, NULL, a);
  43968. if (ret != MP_VAL)
  43969. return WC_TEST_RET_ENC_EC(ret);
  43970. ret = mp_submod(a, NULL, b, a);
  43971. if (ret != MP_VAL)
  43972. return WC_TEST_RET_ENC_EC(ret);
  43973. ret = mp_submod(NULL, b, b, a);
  43974. if (ret != MP_VAL)
  43975. return WC_TEST_RET_ENC_EC(ret);
  43976. #endif
  43977. #ifdef WOLFSSL_SP_MATH_ALL
  43978. ret = mp_div_2d(NULL, 1, a, b);
  43979. if (ret != MP_VAL)
  43980. return WC_TEST_RET_ENC_EC(ret);
  43981. ret = mp_mod_2d(NULL, 1, NULL);
  43982. if (ret != MP_VAL)
  43983. return WC_TEST_RET_ENC_EC(ret);
  43984. ret = mp_mod_2d(a, 1, NULL);
  43985. if (ret != MP_VAL)
  43986. return WC_TEST_RET_ENC_EC(ret);
  43987. ret = mp_mod_2d(NULL, 1, b);
  43988. if (ret != MP_VAL)
  43989. return WC_TEST_RET_ENC_EC(ret);
  43990. ret = mp_mul_2d(NULL, 1, NULL);
  43991. if (ret != MP_VAL)
  43992. return WC_TEST_RET_ENC_EC(ret);
  43993. ret = mp_mul_2d(a, 1, NULL);
  43994. if (ret != MP_VAL)
  43995. return WC_TEST_RET_ENC_EC(ret);
  43996. ret = mp_mul_2d(NULL, 1, b);
  43997. if (ret != MP_VAL)
  43998. return WC_TEST_RET_ENC_EC(ret);
  43999. #endif
  44000. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  44001. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  44002. ret = mp_montgomery_reduce(NULL, NULL, 1);
  44003. if (ret != MP_VAL)
  44004. return WC_TEST_RET_ENC_EC(ret);
  44005. ret = mp_montgomery_reduce(a, NULL, 1);
  44006. if (ret != MP_VAL)
  44007. return WC_TEST_RET_ENC_EC(ret);
  44008. ret = mp_montgomery_reduce(NULL, b, 1);
  44009. if (ret != MP_VAL)
  44010. return WC_TEST_RET_ENC_EC(ret);
  44011. mp_zero(b);
  44012. ret = mp_montgomery_reduce(a, b, 1);
  44013. if (ret != MP_VAL)
  44014. return WC_TEST_RET_ENC_EC(ret);
  44015. #endif
  44016. #ifdef WOLFSSL_SP_MATH_ALL
  44017. ret = mp_montgomery_setup(NULL, NULL);
  44018. if (ret != MP_VAL)
  44019. return WC_TEST_RET_ENC_EC(ret);
  44020. ret = mp_montgomery_setup(a, NULL);
  44021. if (ret != MP_VAL)
  44022. return WC_TEST_RET_ENC_EC(ret);
  44023. ret = mp_montgomery_setup(NULL, &rho);
  44024. if (ret != MP_VAL)
  44025. return WC_TEST_RET_ENC_EC(ret);
  44026. ret = mp_montgomery_calc_normalization(NULL, NULL);
  44027. if (ret != MP_VAL)
  44028. return WC_TEST_RET_ENC_EC(ret);
  44029. ret = mp_montgomery_calc_normalization(a, NULL);
  44030. if (ret != MP_VAL)
  44031. return WC_TEST_RET_ENC_EC(ret);
  44032. ret = mp_montgomery_calc_normalization(NULL, b);
  44033. if (ret != MP_VAL)
  44034. return WC_TEST_RET_ENC_EC(ret);
  44035. #endif
  44036. ret = mp_unsigned_bin_size(NULL);
  44037. if (ret != 0)
  44038. return WC_TEST_RET_ENC_EC(ret);
  44039. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  44040. ret = mp_tohex(NULL, NULL);
  44041. if (ret != MP_VAL)
  44042. return WC_TEST_RET_ENC_EC(ret);
  44043. ret = mp_tohex(a, NULL);
  44044. if (ret != MP_VAL)
  44045. return WC_TEST_RET_ENC_EC(ret);
  44046. ret = mp_tohex(NULL, hexStr);
  44047. if (ret != MP_VAL)
  44048. return WC_TEST_RET_ENC_EC(ret);
  44049. #endif
  44050. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  44051. ret = mp_todecimal(NULL, NULL);
  44052. if (ret != MP_VAL)
  44053. return WC_TEST_RET_ENC_EC(ret);
  44054. ret = mp_todecimal(a, NULL);
  44055. if (ret != MP_VAL)
  44056. return WC_TEST_RET_ENC_EC(ret);
  44057. ret = mp_todecimal(NULL, decStr);
  44058. if (ret != MP_VAL)
  44059. return WC_TEST_RET_ENC_EC(ret);
  44060. #endif
  44061. #ifdef WOLFSSL_SP_MATH_ALL
  44062. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  44063. if (ret != MP_VAL)
  44064. return WC_TEST_RET_ENC_EC(ret);
  44065. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  44066. if (ret != MP_VAL)
  44067. return WC_TEST_RET_ENC_EC(ret);
  44068. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  44069. if (ret != MP_VAL)
  44070. return WC_TEST_RET_ENC_EC(ret);
  44071. ret = mp_toradix(a, hexStr, 3);
  44072. if (ret != MP_VAL)
  44073. return WC_TEST_RET_ENC_EC(ret);
  44074. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  44075. if (ret != MP_VAL)
  44076. return WC_TEST_RET_ENC_EC(ret);
  44077. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  44078. if (ret != MP_VAL)
  44079. return WC_TEST_RET_ENC_EC(ret);
  44080. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  44081. if (ret != MP_VAL)
  44082. return WC_TEST_RET_ENC_EC(ret);
  44083. ret = mp_radix_size(a, 3, &size);
  44084. if (ret != MP_VAL)
  44085. return WC_TEST_RET_ENC_EC(ret);
  44086. #endif
  44087. return 0;
  44088. }
  44089. #endif
  44090. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  44091. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  44092. {
  44093. int i, j;
  44094. wc_test_ret_t ret;
  44095. mp_zero(a);
  44096. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44097. if (mp_is_bit_set(a, i))
  44098. return WC_TEST_RET_ENC_NC;
  44099. for (j = 0; j < i; j++) {
  44100. if (!mp_is_bit_set(a, j))
  44101. return WC_TEST_RET_ENC_NC;
  44102. }
  44103. if (mp_set_bit(a, i) != 0)
  44104. return WC_TEST_RET_ENC_NC;
  44105. if (!mp_is_bit_set(a, i))
  44106. return WC_TEST_RET_ENC_NC;
  44107. }
  44108. mp_zero(a);
  44109. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44110. if (mp_is_bit_set(a, i))
  44111. return WC_TEST_RET_ENC_NC;
  44112. }
  44113. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44114. mp_zero(a);
  44115. if (mp_set_bit(a, i) != 0)
  44116. return WC_TEST_RET_ENC_NC;
  44117. for (j = 0; j < i; j++) {
  44118. if (mp_is_bit_set(a, j))
  44119. return WC_TEST_RET_ENC_NC;
  44120. }
  44121. if (!mp_is_bit_set(a, i))
  44122. return WC_TEST_RET_ENC_NC;
  44123. }
  44124. #ifdef WOLFSSL_KEY_GEN
  44125. for (i = 0; i < DIGIT_BIT * 2; i++) {
  44126. mp_set(a, 1);
  44127. ret = mp_2expt(a, i);
  44128. if (ret != 0)
  44129. return WC_TEST_RET_ENC_EC(ret);
  44130. for (j = 0; j < i; j++) {
  44131. if (mp_is_bit_set(a, j))
  44132. return WC_TEST_RET_ENC_NC;
  44133. }
  44134. if (!mp_is_bit_set(a, i))
  44135. return WC_TEST_RET_ENC_NC;
  44136. }
  44137. #endif
  44138. #ifdef WOLFSSL_SP_MATH
  44139. mp_zero(a);
  44140. for (j = 1; j <= 3; j++) {
  44141. i = SP_INT_MAX_BITS - j;
  44142. if (mp_is_bit_set(a, i))
  44143. return WC_TEST_RET_ENC_NC;
  44144. if (mp_set_bit(a, i) != 0)
  44145. return WC_TEST_RET_ENC_NC;
  44146. if (!mp_is_bit_set(a, i))
  44147. return WC_TEST_RET_ENC_NC;
  44148. #ifdef WOLFSSL_KEY_GEN
  44149. ret = mp_2expt(a, i);
  44150. if (ret != 0)
  44151. return WC_TEST_RET_ENC_EC(ret);
  44152. if (!mp_is_bit_set(a, i))
  44153. return WC_TEST_RET_ENC_NC;
  44154. #endif
  44155. }
  44156. mp_zero(a);
  44157. for (j = 0; j <= 3; j++) {
  44158. i = SP_INT_MAX_BITS + j;
  44159. if (mp_is_bit_set(a, i))
  44160. return WC_TEST_RET_ENC_NC;
  44161. if (mp_set_bit(a, i) != MP_VAL)
  44162. return WC_TEST_RET_ENC_NC;
  44163. #ifdef WOLFSSL_KEY_GEN
  44164. ret = mp_2expt(a, i);
  44165. if (ret != MP_VAL)
  44166. return WC_TEST_RET_ENC_EC(ret);
  44167. #endif
  44168. }
  44169. #endif
  44170. (void)ret;
  44171. return 0;
  44172. }
  44173. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  44174. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  44175. {
  44176. wc_test_ret_t ret;
  44177. mp_zero(a);
  44178. mp_zero(b);
  44179. ret = mp_cmp_d(a, 0);
  44180. if (ret != MP_EQ)
  44181. return WC_TEST_RET_ENC_EC(ret);
  44182. ret = mp_cmp_d(a, 1);
  44183. if (ret != MP_LT)
  44184. return WC_TEST_RET_ENC_EC(ret);
  44185. ret = mp_cmp(a, b);
  44186. if (ret != MP_EQ)
  44187. return WC_TEST_RET_ENC_NC;
  44188. mp_set(a, 1);
  44189. ret = mp_cmp_d(a, 0);
  44190. if (ret != MP_GT)
  44191. return WC_TEST_RET_ENC_EC(ret);
  44192. ret = mp_cmp_d(a, 1);
  44193. if (ret != MP_EQ)
  44194. return WC_TEST_RET_ENC_EC(ret);
  44195. ret = mp_cmp_d(a, 2);
  44196. if (ret != MP_LT)
  44197. return WC_TEST_RET_ENC_EC(ret);
  44198. ret = mp_cmp(a, b);
  44199. if (ret != MP_GT)
  44200. return WC_TEST_RET_ENC_NC;
  44201. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  44202. ret = mp_cmp_d(b, -1);
  44203. if (ret != MP_GT)
  44204. return WC_TEST_RET_ENC_EC(ret);
  44205. ret = mp_cmp(a, b);
  44206. if (ret != MP_LT)
  44207. return WC_TEST_RET_ENC_NC;
  44208. ret = mp_cmp(b, a);
  44209. if (ret != MP_GT)
  44210. return WC_TEST_RET_ENC_NC;
  44211. ret = mp_cmp(b, b);
  44212. if (ret != MP_EQ)
  44213. return WC_TEST_RET_ENC_NC;
  44214. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  44215. defined(WOLFSSL_SP_INT_NEGATIVE)
  44216. mp_read_radix(a, "-1", MP_RADIX_HEX);
  44217. mp_read_radix(a, "1", MP_RADIX_HEX);
  44218. ret = mp_cmp(a, b);
  44219. if (ret != MP_LT)
  44220. return WC_TEST_RET_ENC_NC;
  44221. ret = mp_cmp(b, a);
  44222. if (ret != MP_GT)
  44223. return WC_TEST_RET_ENC_NC;
  44224. mp_read_radix(b, "-2", MP_RADIX_HEX);
  44225. ret = mp_cmp(a, b);
  44226. if (ret != MP_GT)
  44227. return WC_TEST_RET_ENC_NC;
  44228. ret = mp_cmp(b, a);
  44229. if (ret != MP_LT)
  44230. return WC_TEST_RET_ENC_NC;
  44231. mp_read_radix(a, "-2", MP_RADIX_HEX);
  44232. ret = mp_cmp(a, b);
  44233. if (ret != MP_EQ)
  44234. return WC_TEST_RET_ENC_NC;
  44235. #endif
  44236. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  44237. defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
  44238. mp_zero(a);
  44239. mp_zero(b);
  44240. ret = mp_cmp_ct(a, b, 1);
  44241. if (ret != MP_EQ)
  44242. return WC_TEST_RET_ENC_EC(ret);
  44243. ret = mp_cmp_ct(a, a, a->used);
  44244. if (ret != MP_EQ)
  44245. return WC_TEST_RET_ENC_EC(ret);
  44246. #ifdef WOLFSSL_SP_MATH_ALL
  44247. ret = mp_cmp_ct(a, NULL, a->used);
  44248. if (ret != MP_GT)
  44249. return WC_TEST_RET_ENC_EC(ret);
  44250. ret = mp_cmp_ct(NULL, a, a->used);
  44251. if (ret != MP_LT)
  44252. return WC_TEST_RET_ENC_EC(ret);
  44253. #endif
  44254. mp_read_radix(a, "1", MP_RADIX_HEX);
  44255. ret = mp_cmp_ct(a, b, 1);
  44256. if (ret != MP_GT)
  44257. return WC_TEST_RET_ENC_EC(ret);
  44258. ret = mp_cmp_ct(b, a, 1);
  44259. if (ret != MP_LT)
  44260. return WC_TEST_RET_ENC_EC(ret);
  44261. mp_read_radix(a, "0123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  44262. ret = mp_cmp_ct(a, b, a->used);
  44263. if (ret != MP_GT)
  44264. return WC_TEST_RET_ENC_EC(ret);
  44265. ret = mp_cmp_ct(b, a, a->used);
  44266. if (ret != MP_LT)
  44267. return WC_TEST_RET_ENC_EC(ret);
  44268. mp_read_radix(b, "1123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  44269. ret = mp_cmp_ct(b, a, a->used);
  44270. if (ret != MP_GT)
  44271. return WC_TEST_RET_ENC_EC(ret);
  44272. ret = mp_cmp_ct(a, b, a->used);
  44273. if (ret != MP_LT)
  44274. return WC_TEST_RET_ENC_EC(ret);
  44275. mp_read_radix(b, "0123456789abcdef0123456789abcdf0", MP_RADIX_HEX);
  44276. ret = mp_cmp_ct(b, a, a->used);
  44277. if (ret != MP_GT)
  44278. return WC_TEST_RET_ENC_EC(ret);
  44279. ret = mp_cmp_ct(a, b, a->used);
  44280. if (ret != MP_LT)
  44281. return WC_TEST_RET_ENC_EC(ret);
  44282. mp_read_radix(b, "0123456789abcdf0", MP_RADIX_HEX);
  44283. ret = mp_cmp_ct(a, b, a->used);
  44284. if (ret != MP_GT)
  44285. return WC_TEST_RET_ENC_EC(ret);
  44286. ret = mp_cmp_ct(b, a, a->used);
  44287. if (ret != MP_LT)
  44288. return WC_TEST_RET_ENC_EC(ret);
  44289. #endif
  44290. return 0;
  44291. }
  44292. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  44293. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  44294. {
  44295. wc_test_ret_t ret;
  44296. int i, j, k;
  44297. #ifndef WOLFSSL_SP_MATH
  44298. for (i = 0; i < 10; i++) {
  44299. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  44300. ret = randNum(a, j, rng, NULL);
  44301. if (ret != MP_OKAY)
  44302. return WC_TEST_RET_ENC_EC(ret);
  44303. mp_copy(a, b);
  44304. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  44305. ret = mp_mul_2d(a, k, a);
  44306. if (ret != MP_OKAY)
  44307. return WC_TEST_RET_ENC_EC(ret);
  44308. mp_rshb(a, k);
  44309. ret = mp_cmp(a, b);
  44310. if (ret != MP_EQ)
  44311. return WC_TEST_RET_ENC_NC;
  44312. }
  44313. }
  44314. }
  44315. #endif
  44316. for (i = 0; i < 10; i++) {
  44317. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  44318. ret = randNum(a, j, rng, NULL);
  44319. if (ret != MP_OKAY)
  44320. return WC_TEST_RET_ENC_EC(ret);
  44321. mp_copy(a, b);
  44322. for (k = 0; k < 10; k++) {
  44323. ret = mp_lshd(a, k);
  44324. if (ret != MP_OKAY)
  44325. return WC_TEST_RET_ENC_EC(ret);
  44326. #ifndef WOLFSSL_SP_MATH
  44327. mp_rshd(a, k);
  44328. #else
  44329. mp_rshb(a, k * SP_WORD_SIZE);
  44330. #endif
  44331. ret = mp_cmp(a, b);
  44332. if (ret != MP_EQ)
  44333. return WC_TEST_RET_ENC_NC;
  44334. }
  44335. }
  44336. }
  44337. #ifndef WOLFSSL_SP_MATH
  44338. mp_zero(a);
  44339. mp_rshd(a, 1);
  44340. if (!mp_iszero(a))
  44341. return WC_TEST_RET_ENC_NC;
  44342. mp_set(a, 1);
  44343. mp_rshd(a, 1);
  44344. if (!mp_iszero(a))
  44345. return WC_TEST_RET_ENC_NC;
  44346. mp_set(a, 1);
  44347. mp_rshd(a, 2);
  44348. if (!mp_iszero(a))
  44349. return WC_TEST_RET_ENC_NC;
  44350. #endif
  44351. return 0;
  44352. }
  44353. #endif
  44354. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  44355. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  44356. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  44357. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  44358. WC_RNG* rng)
  44359. {
  44360. wc_test_ret_t ret;
  44361. int i, j, k;
  44362. mp_zero(a);
  44363. mp_zero(d);
  44364. ret = mp_div(a, d, r, rem);
  44365. if (ret != MP_VAL)
  44366. return WC_TEST_RET_ENC_EC(ret);
  44367. mp_set(d, 1);
  44368. ret = mp_div(a, d, r, rem);
  44369. if (ret != MP_OKAY)
  44370. return WC_TEST_RET_ENC_EC(ret);
  44371. if (!mp_iszero(r))
  44372. return WC_TEST_RET_ENC_EC(ret);
  44373. if (!mp_iszero(rem))
  44374. return WC_TEST_RET_ENC_EC(ret);
  44375. mp_set(a, 1);
  44376. ret = mp_div(a, d, r, rem);
  44377. if (ret != MP_OKAY)
  44378. return WC_TEST_RET_ENC_EC(ret);
  44379. if (!mp_isone(r))
  44380. return WC_TEST_RET_ENC_EC(ret);
  44381. if (!mp_iszero(rem))
  44382. return WC_TEST_RET_ENC_EC(ret);
  44383. for (i = 0; i < 100; i++) {
  44384. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  44385. ret = randNum(d, j, rng, NULL);
  44386. if (ret != MP_OKAY)
  44387. return WC_TEST_RET_ENC_EC(ret);
  44388. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  44389. ret = randNum(a, k, rng, NULL);
  44390. if (ret != MP_OKAY)
  44391. return WC_TEST_RET_ENC_EC(ret);
  44392. ret = mp_div(a, d, NULL, rem);
  44393. if (ret != MP_OKAY)
  44394. return WC_TEST_RET_ENC_EC(ret);
  44395. ret = mp_div(a, d, r, NULL);
  44396. if (ret != MP_OKAY)
  44397. return WC_TEST_RET_ENC_EC(ret);
  44398. ret = mp_div(a, d, r, rem);
  44399. if (ret != MP_OKAY)
  44400. return WC_TEST_RET_ENC_EC(ret);
  44401. mp_mul(r, d, r);
  44402. mp_add(r, rem, r);
  44403. if (mp_cmp(r, a) != MP_EQ)
  44404. return WC_TEST_RET_ENC_NC;
  44405. }
  44406. }
  44407. }
  44408. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  44409. if (ret != MP_OKAY)
  44410. return WC_TEST_RET_ENC_EC(ret);
  44411. mp_add(d, d, a);
  44412. mp_set(rem, 1);
  44413. mp_div(a, d, NULL, rem);
  44414. if (ret != MP_OKAY)
  44415. return WC_TEST_RET_ENC_EC(ret);
  44416. if (!mp_iszero(rem))
  44417. return WC_TEST_RET_ENC_EC(ret);
  44418. mp_set(r, 1);
  44419. mp_div(a, d, r, NULL);
  44420. if (ret != MP_OKAY)
  44421. return WC_TEST_RET_ENC_EC(ret);
  44422. if (mp_cmp_d(r, 2) != MP_EQ)
  44423. return WC_TEST_RET_ENC_EC(ret);
  44424. mp_set(r, 1);
  44425. mp_set(rem, 1);
  44426. mp_div(a, d, r, rem);
  44427. if (ret != MP_OKAY)
  44428. return WC_TEST_RET_ENC_EC(ret);
  44429. if (mp_cmp_d(r, 2) != MP_EQ)
  44430. return WC_TEST_RET_ENC_EC(ret);
  44431. if (!mp_iszero(rem))
  44432. return WC_TEST_RET_ENC_EC(ret);
  44433. mp_set(a, 0xfe);
  44434. mp_lshd(a, 3);
  44435. mp_add_d(a, 0xff, a);
  44436. mp_set(d, 0xfe);
  44437. mp_lshd(d, 2);
  44438. ret = mp_div(a, d, r, rem);
  44439. if (ret != MP_OKAY)
  44440. return WC_TEST_RET_ENC_EC(ret);
  44441. mp_mul(r, d, d);
  44442. mp_add(rem, d, d);
  44443. if (mp_cmp(a, d) != MP_EQ)
  44444. return WC_TEST_RET_ENC_NC;
  44445. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  44446. * a digit. So mp_div must detect and handle.
  44447. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  44448. */
  44449. mp_zero(a);
  44450. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  44451. mp_zero(d);
  44452. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  44453. mp_add_d(d, 1, d);
  44454. ret = mp_div(a, d, r, rem);
  44455. if (ret != MP_OKAY)
  44456. return WC_TEST_RET_ENC_EC(ret);
  44457. /* Make sure [d | d] / d is handled. */
  44458. mp_zero(a);
  44459. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  44460. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  44461. mp_zero(d);
  44462. mp_set_bit(d, DIGIT_BIT - 1);
  44463. ret = mp_div(a, d, r, rem);
  44464. if (ret != MP_OKAY)
  44465. return WC_TEST_RET_ENC_EC(ret);
  44466. mp_zero(a);
  44467. mp_set_bit(a, DIGIT_BIT);
  44468. mp_set_bit(a, 0);
  44469. mp_zero(d);
  44470. if (mp_cmp(r, a) != MP_EQ)
  44471. return WC_TEST_RET_ENC_NC;
  44472. if (mp_cmp(rem, d) != MP_EQ)
  44473. return WC_TEST_RET_ENC_NC;
  44474. return 0;
  44475. }
  44476. #endif
  44477. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  44478. !defined(WC_NO_RNG)
  44479. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  44480. {
  44481. wc_test_ret_t ret;
  44482. int res;
  44483. ret = mp_rand_prime(a, 1, rng, NULL);
  44484. #if defined(WOLFSSL_SP_MATH_ALL)
  44485. if (ret != 0)
  44486. return WC_TEST_RET_ENC_EC(ret);
  44487. #else
  44488. if (ret != MP_VAL)
  44489. return WC_TEST_RET_ENC_NC;
  44490. #endif
  44491. #ifndef WOLFSSL_SP_MATH
  44492. ret = mp_rand_prime(a, -5, rng, NULL);
  44493. if (ret != 0 || (a->dp[0] & 3) != 3)
  44494. return WC_TEST_RET_ENC_NC;
  44495. #endif
  44496. ret = mp_prime_is_prime(a, 1, &res);
  44497. if (ret != MP_OKAY)
  44498. return WC_TEST_RET_ENC_EC(ret);
  44499. #ifndef WOLFSSL_SP_MATH
  44500. if (res != MP_YES)
  44501. return WC_TEST_RET_ENC_EC(res);
  44502. #else
  44503. if (res != MP_NO)
  44504. return WC_TEST_RET_ENC_EC(res);
  44505. #endif
  44506. ret = mp_prime_is_prime(a, 0, &res);
  44507. if (ret != MP_VAL)
  44508. return WC_TEST_RET_ENC_EC(ret);
  44509. ret = mp_prime_is_prime(a, -1, &res);
  44510. if (ret != MP_VAL)
  44511. return WC_TEST_RET_ENC_EC(ret);
  44512. ret = mp_prime_is_prime(a, 257, &res);
  44513. if (ret != MP_VAL)
  44514. return WC_TEST_RET_ENC_EC(ret);
  44515. mp_set(a, 1);
  44516. ret = mp_prime_is_prime(a, 1, &res);
  44517. if (ret != MP_OKAY)
  44518. return WC_TEST_RET_ENC_EC(ret);
  44519. if (res != MP_NO)
  44520. return WC_TEST_RET_ENC_EC(res);
  44521. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44522. if (ret != MP_OKAY)
  44523. return WC_TEST_RET_ENC_EC(ret);
  44524. if (res != MP_NO)
  44525. return WC_TEST_RET_ENC_EC(res);
  44526. mp_set(a, 2);
  44527. ret = mp_prime_is_prime(a, 1, &res);
  44528. if (ret != MP_OKAY)
  44529. return WC_TEST_RET_ENC_EC(ret);
  44530. if (res != MP_YES)
  44531. return WC_TEST_RET_ENC_EC(res);
  44532. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44533. if (ret != MP_OKAY)
  44534. return WC_TEST_RET_ENC_EC(ret);
  44535. if (res != MP_YES)
  44536. return WC_TEST_RET_ENC_EC(res);
  44537. mp_set(a, 0xfb);
  44538. ret = mp_prime_is_prime(a, 1, &res);
  44539. if (ret != MP_OKAY)
  44540. return WC_TEST_RET_ENC_EC(ret);
  44541. if (res != MP_YES)
  44542. return WC_TEST_RET_ENC_EC(res);
  44543. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44544. if (ret != MP_OKAY)
  44545. return WC_TEST_RET_ENC_EC(ret);
  44546. if (res != MP_YES)
  44547. return WC_TEST_RET_ENC_EC(res);
  44548. mp_set(a, 0x6);
  44549. ret = mp_prime_is_prime(a, 1, &res);
  44550. if (ret != MP_OKAY)
  44551. return WC_TEST_RET_ENC_EC(ret);
  44552. if (res != MP_NO)
  44553. return WC_TEST_RET_ENC_EC(res);
  44554. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44555. if (ret != MP_OKAY)
  44556. return WC_TEST_RET_ENC_EC(ret);
  44557. if (res != MP_NO)
  44558. return WC_TEST_RET_ENC_EC(res);
  44559. mp_set_int(a, 0x655 * 0x65b);
  44560. ret = mp_prime_is_prime(a, 10, &res);
  44561. if (ret != MP_OKAY)
  44562. return WC_TEST_RET_ENC_EC(ret);
  44563. if (res != MP_NO)
  44564. return WC_TEST_RET_ENC_EC(res);
  44565. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  44566. if (ret != MP_OKAY)
  44567. return WC_TEST_RET_ENC_EC(ret);
  44568. if (res != MP_NO)
  44569. return WC_TEST_RET_ENC_EC(res);
  44570. return 0;
  44571. }
  44572. #endif
  44573. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  44574. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  44575. WC_RNG* rng)
  44576. {
  44577. wc_test_ret_t ret;
  44578. int i;
  44579. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  44580. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  44581. { 12, 56, 168 }, { 56, 12, 168 }
  44582. };
  44583. (void)exp;
  44584. mp_set(a, 0);
  44585. mp_set(b, 1);
  44586. ret = mp_lcm(a, a, r);
  44587. if (ret != MP_VAL)
  44588. return WC_TEST_RET_ENC_EC(ret);
  44589. ret = mp_lcm(a, b, r);
  44590. if (ret != MP_VAL)
  44591. return WC_TEST_RET_ENC_EC(ret);
  44592. ret = mp_lcm(b, a, r);
  44593. if (ret != MP_VAL)
  44594. return WC_TEST_RET_ENC_EC(ret);
  44595. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  44596. mp_set(a, kat[i][0]);
  44597. mp_set(b, kat[i][1]);
  44598. ret = mp_lcm(a, b, r);
  44599. if (ret != MP_OKAY)
  44600. return WC_TEST_RET_ENC_EC(ret);
  44601. mp_set(exp, kat[i][2]);
  44602. ret = mp_cmp(r, exp);
  44603. if (ret != MP_EQ)
  44604. return WC_TEST_RET_ENC_NC;
  44605. }
  44606. (void)rng;
  44607. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  44608. !defined(WC_NO_RNG)
  44609. ret = mp_rand_prime(a, 20, rng, NULL);
  44610. if (ret != MP_OKAY)
  44611. return WC_TEST_RET_ENC_EC(ret);
  44612. ret = mp_rand_prime(b, 20, rng, NULL);
  44613. if (ret != MP_OKAY)
  44614. return WC_TEST_RET_ENC_EC(ret);
  44615. ret = mp_mul(a, b, exp);
  44616. if (ret != MP_OKAY)
  44617. return WC_TEST_RET_ENC_EC(ret);
  44618. ret = mp_lcm(a, b, r);
  44619. if (ret != MP_OKAY)
  44620. return WC_TEST_RET_ENC_EC(ret);
  44621. ret = mp_cmp(r, exp);
  44622. if (ret != MP_EQ)
  44623. return WC_TEST_RET_ENC_NC;
  44624. ret = mp_lcm(b, a, r);
  44625. if (ret != MP_OKAY)
  44626. return WC_TEST_RET_ENC_EC(ret);
  44627. ret = mp_cmp(r, exp);
  44628. if (ret != MP_EQ)
  44629. return WC_TEST_RET_ENC_NC;
  44630. #endif
  44631. mp_set(a, 11);
  44632. mp_zero(b);
  44633. ret = mp_gcd(a, b, r);
  44634. if (ret != MP_OKAY)
  44635. return WC_TEST_RET_ENC_EC(ret);
  44636. ret = mp_cmp_d(r, 11);
  44637. if (ret != MP_EQ)
  44638. return WC_TEST_RET_ENC_EC(ret);
  44639. ret = mp_gcd(b, a, r);
  44640. if (ret != MP_OKAY)
  44641. return WC_TEST_RET_ENC_EC(ret);
  44642. ret = mp_cmp_d(r, 11);
  44643. if (ret != MP_EQ)
  44644. return WC_TEST_RET_ENC_EC(ret);
  44645. ret = mp_gcd(b, b, r);
  44646. if (ret != MP_VAL)
  44647. return WC_TEST_RET_ENC_EC(ret);
  44648. return 0;
  44649. }
  44650. #endif
  44651. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  44652. defined(WOLFSSL_SP_MATH_ALL)
  44653. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  44654. {
  44655. wc_test_ret_t ret;
  44656. int i;
  44657. int j;
  44658. mp_set(a, 10);
  44659. ret = mp_mod_2d(a, 0, r);
  44660. if (ret != MP_OKAY)
  44661. return WC_TEST_RET_ENC_EC(ret);
  44662. if (!mp_iszero(r))
  44663. return WC_TEST_RET_ENC_NC;
  44664. ret = mp_mod_2d(a, 1, r);
  44665. if (ret != MP_OKAY)
  44666. return WC_TEST_RET_ENC_EC(ret);
  44667. if (!mp_iszero(r))
  44668. return WC_TEST_RET_ENC_NC;
  44669. ret = mp_mod_2d(a, 2, r);
  44670. if (ret != MP_OKAY)
  44671. return WC_TEST_RET_ENC_EC(ret);
  44672. ret = mp_cmp_d(r, 2);
  44673. if (ret != 0)
  44674. return WC_TEST_RET_ENC_EC(ret);
  44675. for (i = 2; i < 20; i++) {
  44676. ret = randNum(a, i, rng, NULL);
  44677. if (ret != 0)
  44678. return WC_TEST_RET_ENC_EC(ret);
  44679. for (j = 1; j <= mp_count_bits(a); j++) {
  44680. /* Get top part */
  44681. ret = mp_div_2d(a, j, t, NULL);
  44682. if (ret != 0)
  44683. return WC_TEST_RET_ENC_EC(ret);
  44684. ret = mp_mul_2d(t, j, t);
  44685. if (ret != 0)
  44686. return WC_TEST_RET_ENC_EC(ret);
  44687. /* Get bottom part */
  44688. ret = mp_mod_2d(a, j, r);
  44689. if (ret != 0)
  44690. return WC_TEST_RET_ENC_EC(ret);
  44691. /* Reassemble */
  44692. ret = mp_add(t, r, r);
  44693. if (ret != 0)
  44694. return WC_TEST_RET_ENC_EC(ret);
  44695. ret = mp_cmp(a, r);
  44696. if (ret != MP_EQ)
  44697. return WC_TEST_RET_ENC_NC;
  44698. }
  44699. }
  44700. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  44701. /* Test negative value being moded. */
  44702. for (j = 0; j < 20; j++) {
  44703. ret = randNum(a, 2, rng, NULL);
  44704. if (ret != 0)
  44705. return WC_TEST_RET_ENC_EC(ret);
  44706. a->sign = MP_NEG;
  44707. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  44708. ret = mp_mod_2d(a, i, r);
  44709. if (ret != 0)
  44710. return WC_TEST_RET_ENC_EC(ret);
  44711. mp_zero(t);
  44712. ret = mp_set_bit(t, i);
  44713. if (ret != 0)
  44714. return WC_TEST_RET_ENC_EC(ret);
  44715. ret = mp_mod(a, t, t);
  44716. if (ret != 0)
  44717. return WC_TEST_RET_ENC_EC(ret);
  44718. ret = mp_cmp(r, t);
  44719. if (ret != MP_EQ)
  44720. return WC_TEST_RET_ENC_NC;
  44721. }
  44722. }
  44723. #endif
  44724. return 0;
  44725. }
  44726. #endif
  44727. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  44728. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  44729. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  44730. {
  44731. wc_test_ret_t ret;
  44732. mp_digit r;
  44733. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  44734. mp_digit rem;
  44735. int i;
  44736. #endif
  44737. (void)rng;
  44738. ret = mp_set(a, 1);
  44739. if (ret != MP_OKAY)
  44740. return WC_TEST_RET_ENC_EC(ret);
  44741. ret = mp_mod_d(a, 0, &r);
  44742. if (ret != MP_VAL)
  44743. return WC_TEST_RET_ENC_EC(ret);
  44744. mp_zero(a);
  44745. ret = mp_mod_d(a, 1, &r);
  44746. if (ret != MP_OKAY)
  44747. return WC_TEST_RET_ENC_EC(ret);
  44748. ret = mp_mod_d(a, 3, &r);
  44749. if (ret != MP_OKAY)
  44750. return WC_TEST_RET_ENC_EC(ret);
  44751. ret = mp_mod_d(a, 5, &r);
  44752. if (ret != MP_OKAY)
  44753. return WC_TEST_RET_ENC_EC(ret);
  44754. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  44755. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  44756. ret = randNum(a, i, rng, NULL);
  44757. if (ret != MP_OKAY)
  44758. return WC_TEST_RET_ENC_EC(ret);
  44759. ret = mp_mod_d(a, 3, &r);
  44760. if (ret != MP_OKAY)
  44761. return WC_TEST_RET_ENC_EC(ret);
  44762. ret = mp_div_d(a, 3, a, &rem);
  44763. if (ret != MP_OKAY)
  44764. return WC_TEST_RET_ENC_EC(ret);
  44765. if (r != rem)
  44766. return WC_TEST_RET_ENC_NC;
  44767. }
  44768. #endif
  44769. return 0;
  44770. }
  44771. #endif
  44772. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  44773. WC_RNG* rng)
  44774. {
  44775. wc_test_ret_t ret;
  44776. int i;
  44777. for (i = 1; i < 16; i++) {
  44778. ret = randNum(a, i, rng, NULL);
  44779. if (ret != 0)
  44780. return WC_TEST_RET_ENC_EC(ret);
  44781. ret = mp_mul(a, a, r1);
  44782. if (ret != 0)
  44783. return WC_TEST_RET_ENC_EC(ret);
  44784. ret = mp_sqr(a, r2);
  44785. if (ret != 0)
  44786. return WC_TEST_RET_ENC_EC(ret);
  44787. ret = mp_cmp(r1, r2);
  44788. if (ret != MP_EQ)
  44789. return WC_TEST_RET_ENC_NC;
  44790. }
  44791. ret = mp_set(b, 0);
  44792. if (ret != MP_OKAY)
  44793. return WC_TEST_RET_ENC_EC(ret);
  44794. ret = mp_mul(a, b, r1);
  44795. if (ret != MP_OKAY)
  44796. return WC_TEST_RET_ENC_EC(ret);
  44797. if (!mp_iszero(r1))
  44798. return WC_TEST_RET_ENC_EC(ret);
  44799. ret = mp_sqr(b, r1);
  44800. if (ret != MP_OKAY)
  44801. return WC_TEST_RET_ENC_EC(ret);
  44802. if (!mp_iszero(r1))
  44803. return WC_TEST_RET_ENC_NC;
  44804. #ifdef WOLFSSL_SP_MATH_ALL
  44805. ret = mp_set(a, 1);
  44806. if (ret != MP_OKAY)
  44807. return WC_TEST_RET_ENC_EC(ret);
  44808. i = (SP_INT_DIGITS / 2) + 1;
  44809. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  44810. if (ret != MP_OKAY)
  44811. return WC_TEST_RET_ENC_EC(ret);
  44812. ret = mp_set(b, 1);
  44813. if (ret != MP_OKAY)
  44814. return WC_TEST_RET_ENC_EC(ret);
  44815. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  44816. if (ret != MP_OKAY)
  44817. return WC_TEST_RET_ENC_EC(ret);
  44818. ret = mp_mul(a, b, r1);
  44819. if (ret != MP_OKAY)
  44820. return WC_TEST_RET_ENC_EC(ret);
  44821. ret = mp_mul(a, a, r1);
  44822. if (ret == MP_OKAY)
  44823. return WC_TEST_RET_ENC_NC;
  44824. ret = mp_sqr(a, r1);
  44825. if (ret == MP_OKAY)
  44826. return WC_TEST_RET_ENC_NC;
  44827. ret = mp_sqr(b, r1);
  44828. if (ret != MP_OKAY)
  44829. return WC_TEST_RET_ENC_EC(ret);
  44830. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  44831. (defined(HAVE_ECC) && defined(FP_ECC))
  44832. ret = mp_mulmod(a, b, b, r1);
  44833. if (ret != MP_OKAY)
  44834. return WC_TEST_RET_ENC_EC(ret);
  44835. ret = mp_mulmod(a, a, b, r1);
  44836. if (ret == MP_OKAY)
  44837. return WC_TEST_RET_ENC_NC;
  44838. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  44839. ret = mp_sqrmod(a, b, r1);
  44840. if (ret == MP_OKAY)
  44841. return WC_TEST_RET_ENC_NC;
  44842. ret = mp_sqrmod(b, a, r1);
  44843. if (ret != MP_OKAY)
  44844. return WC_TEST_RET_ENC_EC(ret);
  44845. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  44846. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  44847. #endif /* WOLFSSL_SP_MATH_ALL */
  44848. return 0;
  44849. }
  44850. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  44851. defined(OPENSSL_EXTRA)
  44852. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  44853. {
  44854. wc_test_ret_t ret;
  44855. mp_set(a, 0);
  44856. mp_set(m, 1);
  44857. ret = mp_invmod(a, m, r);
  44858. if (ret != MP_VAL)
  44859. return WC_TEST_RET_ENC_EC(ret);
  44860. ret = mp_invmod(m, a, r);
  44861. if (ret != MP_VAL)
  44862. return WC_TEST_RET_ENC_EC(ret);
  44863. mp_set(a, 2);
  44864. mp_set(m, 4);
  44865. ret = mp_invmod(a, m, r);
  44866. if (ret != MP_VAL)
  44867. return WC_TEST_RET_ENC_EC(ret);
  44868. mp_set(a, 3);
  44869. mp_set(m, 6);
  44870. ret = mp_invmod(a, m, r);
  44871. if (ret != MP_VAL)
  44872. return WC_TEST_RET_ENC_EC(ret);
  44873. mp_set(a, 5*9);
  44874. mp_set(m, 6*9);
  44875. ret = mp_invmod(a, m, r);
  44876. if (ret != MP_VAL)
  44877. return WC_TEST_RET_ENC_EC(ret);
  44878. mp_set(a, 1);
  44879. mp_set(m, 4);
  44880. ret = mp_invmod(a, m, r);
  44881. if (ret != MP_OKAY)
  44882. return WC_TEST_RET_ENC_EC(ret);
  44883. if (!mp_isone(r))
  44884. return WC_TEST_RET_ENC_NC;
  44885. mp_set(a, 3);
  44886. mp_set(m, 4);
  44887. ret = mp_invmod(a, m, r);
  44888. if (ret != MP_OKAY)
  44889. return WC_TEST_RET_ENC_EC(ret);
  44890. ret = mp_cmp_d(r, 3);
  44891. if (ret != 0)
  44892. return WC_TEST_RET_ENC_EC(ret);
  44893. mp_set(a, 3);
  44894. mp_set(m, 5);
  44895. ret = mp_invmod(a, m, r);
  44896. if (ret != MP_OKAY)
  44897. return WC_TEST_RET_ENC_EC(ret);
  44898. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  44899. /* Maximum 'a' */
  44900. mp_set(a, 0);
  44901. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  44902. mp_sub_d(a, 1, a);
  44903. /* Modulus too big. */
  44904. mp_set(m, 0);
  44905. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  44906. ret = mp_invmod(a, m, r);
  44907. if (ret != MP_VAL)
  44908. return WC_TEST_RET_ENC_EC(ret);
  44909. /* Maximum modulus - even. */
  44910. mp_set(m, 0);
  44911. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  44912. ret = mp_invmod(a, m, r);
  44913. if (ret != MP_OKAY)
  44914. return WC_TEST_RET_ENC_EC(ret);
  44915. #endif
  44916. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  44917. mp_read_radix(a, "-3", 16);
  44918. ret = mp_invmod(a, m, r);
  44919. if (ret != MP_OKAY)
  44920. return WC_TEST_RET_ENC_EC(ret);
  44921. #endif
  44922. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  44923. mp_set(a, 0);
  44924. mp_set(m, 3);
  44925. ret = mp_invmod_mont_ct(a, m, r, 1);
  44926. if (ret != MP_VAL)
  44927. return WC_TEST_RET_ENC_EC(ret);
  44928. mp_set(a, 1);
  44929. mp_set(m, 0);
  44930. ret = mp_invmod_mont_ct(a, m, r, 1);
  44931. if (ret != MP_VAL)
  44932. return WC_TEST_RET_ENC_EC(ret);
  44933. mp_set(a, 1);
  44934. mp_set(m, 1);
  44935. ret = mp_invmod_mont_ct(a, m, r, 1);
  44936. if (ret != MP_VAL)
  44937. return WC_TEST_RET_ENC_EC(ret);
  44938. mp_set(a, 1);
  44939. mp_set(m, 2);
  44940. ret = mp_invmod_mont_ct(a, m, r, 1);
  44941. if (ret != MP_VAL)
  44942. return WC_TEST_RET_ENC_EC(ret);
  44943. mp_set(a, 1);
  44944. mp_set(m, 3);
  44945. ret = mp_invmod_mont_ct(a, m, r, 1);
  44946. if (ret != MP_OKAY)
  44947. return WC_TEST_RET_ENC_EC(ret);
  44948. #endif
  44949. return 0;
  44950. }
  44951. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  44952. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  44953. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  44954. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  44955. {
  44956. wc_test_ret_t ret;
  44957. mp_set(b, 0x2);
  44958. mp_set(e, 0x3);
  44959. mp_set(m, 0x0);
  44960. ret = mp_exptmod_ex(b, e, 1, m, r);
  44961. if (ret != MP_VAL)
  44962. return WC_TEST_RET_ENC_EC(ret);
  44963. ret = mp_exptmod_nct(b, e, m, r);
  44964. if (ret != MP_VAL)
  44965. return WC_TEST_RET_ENC_EC(ret);
  44966. mp_set(b, 0x2);
  44967. mp_set(e, 0x3);
  44968. mp_set(m, 0x1);
  44969. ret = mp_exptmod_ex(b, e, 1, m, r);
  44970. if (ret != MP_OKAY)
  44971. return WC_TEST_RET_ENC_EC(ret);
  44972. if (!mp_iszero(r))
  44973. return WC_TEST_RET_ENC_NC;
  44974. ret = mp_exptmod_nct(b, e, m, r);
  44975. if (ret != MP_OKAY)
  44976. return WC_TEST_RET_ENC_EC(ret);
  44977. if (!mp_iszero(r))
  44978. return WC_TEST_RET_ENC_NC;
  44979. mp_set(b, 0x2);
  44980. mp_set(e, 0x0);
  44981. mp_set(m, 0x7);
  44982. ret = mp_exptmod_ex(b, e, 1, m, r);
  44983. if (ret != MP_OKAY)
  44984. return WC_TEST_RET_ENC_EC(ret);
  44985. if (!mp_isone(r))
  44986. return WC_TEST_RET_ENC_NC;
  44987. ret = mp_exptmod_nct(b, e, m, r);
  44988. if (ret != MP_OKAY)
  44989. return WC_TEST_RET_ENC_EC(ret);
  44990. if (!mp_isone(r))
  44991. return WC_TEST_RET_ENC_NC;
  44992. mp_set(b, 0x0);
  44993. mp_set(e, 0x3);
  44994. mp_set(m, 0x7);
  44995. ret = mp_exptmod_ex(b, e, 1, m, r);
  44996. if (ret != MP_OKAY)
  44997. return WC_TEST_RET_ENC_EC(ret);
  44998. if (!mp_iszero(r))
  44999. return WC_TEST_RET_ENC_NC;
  45000. ret = mp_exptmod_nct(b, e, m, r);
  45001. if (ret != MP_OKAY)
  45002. return WC_TEST_RET_ENC_EC(ret);
  45003. if (!mp_iszero(r))
  45004. return WC_TEST_RET_ENC_NC;
  45005. mp_set(b, 0x10);
  45006. mp_set(e, 0x3);
  45007. mp_set(m, 0x7);
  45008. ret = mp_exptmod_ex(b, e, 1, m, r);
  45009. if (ret != MP_OKAY)
  45010. return WC_TEST_RET_ENC_EC(ret);
  45011. ret = mp_exptmod_nct(b, e, m, r);
  45012. if (ret != MP_OKAY)
  45013. return WC_TEST_RET_ENC_EC(ret);
  45014. mp_set(b, 0x7);
  45015. mp_set(e, 0x3);
  45016. mp_set(m, 0x7);
  45017. ret = mp_exptmod_ex(b, e, 1, m, r);
  45018. if (ret != MP_OKAY)
  45019. return WC_TEST_RET_ENC_EC(ret);
  45020. if (!mp_iszero(r))
  45021. return WC_TEST_RET_ENC_NC;
  45022. ret = mp_exptmod_nct(b, e, m, r);
  45023. if (ret != MP_OKAY)
  45024. return WC_TEST_RET_ENC_EC(ret);
  45025. if (!mp_iszero(r))
  45026. return WC_TEST_RET_ENC_NC;
  45027. #ifndef WOLFSSL_SP_MATH
  45028. mp_set(b, 0x01);
  45029. mp_mul_2d(b, DIGIT_BIT, b);
  45030. mp_add_d(b, 1, b);
  45031. mp_set(e, 0x3);
  45032. mp_copy(b, m);
  45033. ret = mp_exptmod_ex(b, e, 1, m, r);
  45034. if (ret != MP_OKAY)
  45035. return WC_TEST_RET_ENC_EC(ret);
  45036. if (!mp_iszero(r))
  45037. return WC_TEST_RET_ENC_NC;
  45038. ret = mp_exptmod_nct(b, e, m, r);
  45039. if (ret != MP_OKAY)
  45040. return WC_TEST_RET_ENC_EC(ret);
  45041. if (!mp_iszero(r))
  45042. return WC_TEST_RET_ENC_NC;
  45043. #endif
  45044. mp_set(b, 0x2);
  45045. mp_set(e, 0x3);
  45046. mp_set(m, 0x7);
  45047. ret = mp_exptmod_ex(b, e, 1, m, r);
  45048. if (ret != MP_OKAY)
  45049. return WC_TEST_RET_ENC_EC(ret);
  45050. ret = mp_exptmod_nct(b, e, m, r);
  45051. if (ret != MP_OKAY)
  45052. return WC_TEST_RET_ENC_EC(ret);
  45053. #ifdef WOLFSSL_SP_MATH_ALL
  45054. mp_set(b, 0x2);
  45055. mp_set(e, 0x3);
  45056. mp_set(m, 0x01);
  45057. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  45058. mp_add_d(m, 0x01, m);
  45059. ret = mp_exptmod_ex(b, e, 1, m, r);
  45060. if (ret != MP_VAL)
  45061. return WC_TEST_RET_ENC_EC(ret);
  45062. ret = mp_exptmod_nct(b, e, m, r);
  45063. if (ret != MP_VAL)
  45064. return WC_TEST_RET_ENC_EC(ret);
  45065. #endif
  45066. return 0;
  45067. }
  45068. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  45069. * OPENSSL_EXTRA */
  45070. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45071. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45072. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  45073. {
  45074. wc_test_ret_t ret;
  45075. mp_digit mp;
  45076. static int exp[] = { 7, 8, 16, 27, 32, 64,
  45077. 127, 128, 255, 256,
  45078. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  45079. 383, 384, 2033, 2048
  45080. #endif
  45081. };
  45082. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  45083. 0x01, 0x9f, 0x13, 0xbd,
  45084. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  45085. 0x1f, 0x13d, 0x45, 0x615
  45086. #endif
  45087. };
  45088. int bits[] = { 256, 384,
  45089. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  45090. 2048,
  45091. #endif
  45092. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  45093. 3072
  45094. #endif
  45095. };
  45096. int i;
  45097. int j;
  45098. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  45099. if (exp[i] >= DIGIT_BIT)
  45100. continue;
  45101. mp_zero(m);
  45102. ret = mp_set_bit(m, exp[i]);
  45103. if (ret != MP_OKAY)
  45104. return WC_TEST_RET_ENC_EC(ret);
  45105. ret = mp_sub_d(m, sub[i], m);
  45106. if (ret != MP_OKAY)
  45107. return WC_TEST_RET_ENC_EC(ret);
  45108. ret = mp_montgomery_setup(m, &mp);
  45109. if (ret != MP_OKAY)
  45110. return WC_TEST_RET_ENC_EC(ret);
  45111. ret = mp_montgomery_calc_normalization(n, m);
  45112. if (ret != MP_OKAY)
  45113. return WC_TEST_RET_ENC_EC(ret);
  45114. for (j = 0; j < 10; j++) {
  45115. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  45116. if (ret != 0)
  45117. return WC_TEST_RET_ENC_EC(ret);
  45118. ret = mp_mod(a, m, a);
  45119. if (ret != 0)
  45120. return WC_TEST_RET_ENC_EC(ret);
  45121. /* r = a * a */
  45122. ret = mp_sqrmod(a, m, r);
  45123. if (ret != MP_OKAY)
  45124. return WC_TEST_RET_ENC_EC(ret);
  45125. /* Convert to Montgomery form = a*n */
  45126. ret = mp_mulmod(a, n, m, a);
  45127. if (ret != MP_OKAY)
  45128. return WC_TEST_RET_ENC_EC(ret);
  45129. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  45130. ret = mp_sqr(a, a);
  45131. if (ret != MP_OKAY)
  45132. return WC_TEST_RET_ENC_EC(ret);
  45133. ret = mp_montgomery_reduce(a, m, mp);
  45134. if (ret != MP_OKAY)
  45135. return WC_TEST_RET_ENC_EC(ret);
  45136. ret = mp_montgomery_reduce(a, m, mp);
  45137. if (ret != MP_OKAY)
  45138. return WC_TEST_RET_ENC_EC(ret);
  45139. if (mp_cmp(a, r) != MP_EQ)
  45140. return WC_TEST_RET_ENC_NC;
  45141. }
  45142. }
  45143. /* Force carries. */
  45144. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  45145. /* a = 2^(bits*2) - 1 */
  45146. mp_zero(a);
  45147. mp_set_bit(a, bits[i] * 2);
  45148. mp_sub_d(a, 1, a);
  45149. /* m = 2^(bits) - 1 */
  45150. mp_zero(m);
  45151. mp_set_bit(m, bits[i]);
  45152. mp_sub_d(m, 1, m);
  45153. mp = 1;
  45154. /* result = r = 2^(bits) - 1 */
  45155. mp_zero(r);
  45156. mp_set_bit(r, bits[i]);
  45157. mp_sub_d(r, 1, r);
  45158. ret = mp_montgomery_reduce(a, m, mp);
  45159. if (ret != MP_OKAY)
  45160. return WC_TEST_RET_ENC_EC(ret);
  45161. /* Result is m or 0 if reduced to range of modulus. */
  45162. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  45163. return WC_TEST_RET_ENC_NC;
  45164. }
  45165. return 0;
  45166. }
  45167. #endif
  45168. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  45169. {
  45170. WC_RNG rng;
  45171. int rng_inited = 0;
  45172. wc_test_ret_t ret;
  45173. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  45174. int i, j;
  45175. #ifndef WOLFSSL_SP_MATH
  45176. int k;
  45177. #endif
  45178. mp_digit d = 0;
  45179. #endif
  45180. #ifdef WOLFSSL_SMALL_STACK
  45181. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45182. DYNAMIC_TYPE_TMP_BUFFER),
  45183. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45184. DYNAMIC_TYPE_TMP_BUFFER),
  45185. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45186. DYNAMIC_TYPE_TMP_BUFFER),
  45187. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45188. DYNAMIC_TYPE_TMP_BUFFER),
  45189. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45190. DYNAMIC_TYPE_TMP_BUFFER);
  45191. if ((a == NULL) ||
  45192. (b == NULL) ||
  45193. (r1 == NULL) ||
  45194. (r2 == NULL) ||
  45195. (p == NULL))
  45196. {
  45197. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  45198. }
  45199. #else
  45200. mp_int a[1], b[1], r1[1], r2[1], p[1];
  45201. #endif
  45202. WOLFSSL_ENTER("mp_test");
  45203. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  45204. if (ret != 0)
  45205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45206. #ifdef WOLFSSL_SP_MATH_ALL
  45207. mp_init_copy(p, a);
  45208. #else
  45209. ret = mp_init(p);
  45210. if (ret != 0)
  45211. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45212. #endif
  45213. #ifndef HAVE_FIPS
  45214. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  45215. #else
  45216. ret = wc_InitRng(&rng);
  45217. #endif
  45218. if (ret != 0)
  45219. goto done;
  45220. rng_inited = 1;
  45221. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  45222. mp_set_int(a, 0);
  45223. if (a->used != 0 || a->dp[0] != 0)
  45224. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45225. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  45226. for (i = 0; i < 4 * j; i++) {
  45227. /* New values to use. */
  45228. ret = randNum(p, j, &rng, NULL);
  45229. if (ret != 0)
  45230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45231. ret = randNum(a, j, &rng, NULL);
  45232. if (ret != 0)
  45233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45234. ret = randNum(b, j, &rng, NULL);
  45235. if (ret != 0)
  45236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45237. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  45238. if (ret != 0)
  45239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45240. d &= MP_MASK;
  45241. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  45242. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  45243. /* Ensure sqrmod produce same result as mulmod. */
  45244. ret = mp_sqrmod(a, p, r1);
  45245. if (ret != 0)
  45246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45247. ret = mp_mulmod(a, a, p, r2);
  45248. if (ret != 0)
  45249. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45250. if (mp_cmp(r1, r2) != 0) {
  45251. WOLFSSL_MSG("Fail: mp_mulmod result does not match mp_sqrmod!");
  45252. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45253. }
  45254. #endif
  45255. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45256. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  45257. !defined(WOLFSSL_SP_INT_NEGATIVE))
  45258. ret = mp_addmod(a, b, p, r1);
  45259. if (ret != 0)
  45260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45261. ret = mp_submod(r1, b, p, r2);
  45262. if (ret != 0)
  45263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45264. ret = mp_mod(a, p, r1);
  45265. if (ret != 0)
  45266. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45267. if (mp_cmp(r1, r2) != MP_EQ)
  45268. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45269. #else
  45270. /* Ensure add with mod produce same result as sub with mod. */
  45271. ret = mp_addmod(a, b, p, r1);
  45272. if (ret != 0)
  45273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45274. b->sign ^= 1;
  45275. ret = mp_submod(a, b, p, r2);
  45276. if (ret != 0)
  45277. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45278. if (mp_cmp(r1, r2) != 0)
  45279. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45280. #endif
  45281. #endif
  45282. /* Ensure add digit produce same result as sub digit. */
  45283. ret = mp_add_d(a, d, r1);
  45284. if (ret != 0)
  45285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45286. ret = mp_sub_d(r1, d, r2);
  45287. if (ret != 0)
  45288. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45289. if (mp_cmp(a, r2) != 0)
  45290. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45291. /* Invert - if p is even it will use the slow impl.
  45292. * - if p and a are even it will fail.
  45293. */
  45294. ret = mp_invmod(a, p, r1);
  45295. if (ret != 0 && ret != MP_VAL)
  45296. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45297. #ifndef WOLFSSL_SP_MATH
  45298. /* Shift up and down number all bits in a digit. */
  45299. for (k = 0; k < DIGIT_BIT; k++) {
  45300. mp_mul_2d(a, k, r1);
  45301. mp_div_2d(r1, k, r2, p);
  45302. if (mp_cmp(a, r2) != 0)
  45303. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45304. if (!mp_iszero(p))
  45305. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45306. mp_rshb(r1, k);
  45307. if (mp_cmp(a, r1) != 0)
  45308. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45309. }
  45310. #endif
  45311. }
  45312. }
  45313. /* Test adding and subtracting zero from zero. */
  45314. mp_zero(a);
  45315. ret = mp_add_d(a, 0, r1);
  45316. if (ret != 0)
  45317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45318. if (!mp_iszero(r1)) {
  45319. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45320. }
  45321. ret = mp_sub_d(a, 0, r2);
  45322. if (ret != 0)
  45323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45324. if (!mp_iszero(r2)) {
  45325. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45326. }
  45327. #if DIGIT_BIT >= 32
  45328. /* Check that setting a 32-bit digit works. */
  45329. d &= 0xffffffffU;
  45330. mp_set_int(a, d);
  45331. if (a->used != 1 || a->dp[0] != d)
  45332. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45333. #endif
  45334. /* Check setting a bit and testing a bit works. */
  45335. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  45336. mp_zero(a);
  45337. mp_set_bit(a, i);
  45338. if (!mp_is_bit_set(a, i))
  45339. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45340. }
  45341. #endif
  45342. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  45343. mp_zero(a);
  45344. i = mp_cnt_lsb(a);
  45345. if (i != 0)
  45346. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45347. mp_set(a, 1);
  45348. i = mp_cnt_lsb(a);
  45349. if (i != 0)
  45350. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45351. mp_set(a, 32);
  45352. i = mp_cnt_lsb(a);
  45353. if (i != 5)
  45354. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45355. mp_zero(a);
  45356. mp_set_bit(a, 129);
  45357. i = mp_cnt_lsb(a);
  45358. if (i != 129)
  45359. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45360. #endif
  45361. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45362. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  45363. goto done;
  45364. #endif
  45365. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  45366. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  45367. goto done;
  45368. #endif
  45369. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  45370. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45371. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  45372. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  45373. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  45374. goto done;
  45375. #endif
  45376. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  45377. defined(HAVE_ECC))
  45378. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  45379. goto done;
  45380. #endif
  45381. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  45382. goto done;
  45383. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  45384. goto done;
  45385. if ((ret = mp_test_read_to_bin(a)) != 0)
  45386. goto done;
  45387. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45388. if ((ret = mp_test_set_int(a)) != 0)
  45389. goto done;
  45390. #endif
  45391. if ((ret = mp_test_cmp(a, r1)) != 0)
  45392. goto done;
  45393. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45394. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  45395. goto done;
  45396. #endif
  45397. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45398. if ((ret = mp_test_set_is_bit(a)) != 0)
  45399. goto done;
  45400. #endif
  45401. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  45402. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  45403. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  45404. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  45405. goto done;
  45406. #endif
  45407. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  45408. !defined(WC_NO_RNG)
  45409. if ((ret = mp_test_prime(a, &rng)) != 0)
  45410. goto done;
  45411. #endif
  45412. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  45413. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  45414. goto done;
  45415. #endif
  45416. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  45417. defined(WOLFSSL_SP_MATH_ALL)
  45418. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  45419. goto done;
  45420. #endif
  45421. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  45422. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  45423. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  45424. goto done;
  45425. #endif
  45426. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  45427. goto done;
  45428. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  45429. defined(OPENSSL_EXTRA)
  45430. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  45431. goto done;
  45432. #endif
  45433. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  45434. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  45435. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  45436. goto done;
  45437. #endif
  45438. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45439. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45440. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  45441. goto done;
  45442. #endif
  45443. done:
  45444. #ifdef WOLFSSL_SMALL_STACK
  45445. if (p) {
  45446. mp_clear(p);
  45447. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45448. }
  45449. if (r2) {
  45450. mp_clear(r2);
  45451. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45452. }
  45453. if (r1) {
  45454. mp_clear(r1);
  45455. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45456. }
  45457. if (b) {
  45458. mp_clear(b);
  45459. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45460. }
  45461. if (a) {
  45462. mp_clear(a);
  45463. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45464. }
  45465. #else
  45466. mp_clear(p);
  45467. mp_clear(r2);
  45468. mp_clear(r1);
  45469. mp_clear(b);
  45470. mp_clear(a);
  45471. #endif
  45472. if (rng_inited)
  45473. wc_FreeRng(&rng);
  45474. return ret;
  45475. }
  45476. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  45477. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  45478. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  45479. typedef struct pairs_t {
  45480. const unsigned char* coeff;
  45481. int coeffSz;
  45482. int exp;
  45483. } pairs_t;
  45484. /*
  45485. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  45486. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  45487. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  45488. */
  45489. static const unsigned char c192a[] =
  45490. {
  45491. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  45492. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  45493. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  45494. };
  45495. static const unsigned char c0a[] =
  45496. {
  45497. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  45498. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  45499. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  45500. };
  45501. static const pairs_t ecPairsA[] =
  45502. {
  45503. {c192a, sizeof(c192a), 192},
  45504. {c0a, sizeof(c0a), 0}
  45505. };
  45506. static const int kA[] = {173, 293};
  45507. static const unsigned char controlPrime[] = {
  45508. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  45509. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  45510. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  45511. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  45512. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  45513. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  45514. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  45515. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  45516. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  45517. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  45518. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  45519. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  45520. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  45521. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  45522. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  45523. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  45524. };
  45525. static const unsigned char testOne[] = { 1 };
  45526. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  45527. {
  45528. wc_test_ret_t ret;
  45529. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45530. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45531. if (ki == NULL)
  45532. return MEMORY_E;
  45533. #else
  45534. mp_int ki[1];
  45535. #endif
  45536. ret = mp_init(ki);
  45537. if (ret != 0)
  45538. ret = WC_TEST_RET_ENC_EC(ret);
  45539. if (ret == 0) {
  45540. ret = mp_set(ki, (mp_digit)k);
  45541. if (ret != 0)
  45542. ret = WC_TEST_RET_ENC_EC(ret);
  45543. }
  45544. if (ret == 0) {
  45545. ret = mp_sub_d(p1, 1, p2);
  45546. if (ret != 0)
  45547. ret = WC_TEST_RET_ENC_EC(ret);
  45548. }
  45549. if (ret == 0) {
  45550. ret = mp_mul(p2, ki, p2);
  45551. if (ret != 0)
  45552. ret = WC_TEST_RET_ENC_EC(ret);
  45553. }
  45554. if (ret == 0) {
  45555. ret = mp_add_d(p2, 1, p2);
  45556. if (ret != 0)
  45557. ret = WC_TEST_RET_ENC_EC(ret);
  45558. }
  45559. mp_clear(ki);
  45560. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45561. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45562. #endif
  45563. return ret;
  45564. }
  45565. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  45566. const pairs_t* ecPairs, int ecPairsSz,
  45567. const int* k)
  45568. {
  45569. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45570. mp_int *x = NULL, *y = NULL;
  45571. #else
  45572. mp_int x[1], y[1];
  45573. #endif
  45574. wc_test_ret_t ret;
  45575. int i;
  45576. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45577. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  45578. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  45579. ret = MEMORY_E;
  45580. goto out;
  45581. }
  45582. #endif
  45583. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  45584. if (ret != 0) {
  45585. ret = WC_TEST_RET_ENC_EC(ret);
  45586. goto out;
  45587. }
  45588. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  45589. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, (word32)ecPairs[i].coeffSz);
  45590. if (ret != 0) {
  45591. ret = WC_TEST_RET_ENC_EC(ret);
  45592. break;
  45593. }
  45594. /* p1 = 2^exp */
  45595. ret = mp_2expt(y, ecPairs[i].exp);
  45596. if (ret != 0) {
  45597. ret = WC_TEST_RET_ENC_EC(ret);
  45598. break;
  45599. }
  45600. /* p1 = p1 * m */
  45601. ret = mp_mul(x, y, x);
  45602. if (ret != 0) {
  45603. ret = WC_TEST_RET_ENC_EC(ret);
  45604. break;
  45605. }
  45606. /* p1 += */
  45607. ret = mp_add(p1, x, p1);
  45608. if (ret != 0) {
  45609. ret = WC_TEST_RET_ENC_EC(ret);
  45610. break;
  45611. }
  45612. mp_zero(x);
  45613. mp_zero(y);
  45614. }
  45615. if (ret == 0)
  45616. ret = GenerateNextP(p1, p2, k[0]);
  45617. if (ret == 0)
  45618. ret = GenerateNextP(p1, p3, k[1]);
  45619. out:
  45620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45621. if (x != NULL) {
  45622. mp_clear(x);
  45623. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45624. }
  45625. if (y != NULL) {
  45626. mp_clear(y);
  45627. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45628. }
  45629. #else
  45630. mp_clear(x);
  45631. mp_clear(y);
  45632. #endif
  45633. return ret;
  45634. }
  45635. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  45636. {
  45637. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45638. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45639. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45640. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45641. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45642. #else
  45643. mp_int n[1],
  45644. p1[1],
  45645. p2[1],
  45646. p3[1];
  45647. #endif
  45648. wc_test_ret_t ret;
  45649. int isPrime = 0;
  45650. WC_RNG rng;
  45651. WOLFSSL_ENTER("prime_test");
  45652. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45653. if ((n == NULL) ||
  45654. (p1 == NULL) ||
  45655. (p2 == NULL) ||
  45656. (p3 == NULL))
  45657. ERROR_OUT(MEMORY_E, out);
  45658. #endif
  45659. ret = wc_InitRng(&rng);
  45660. if (ret != 0)
  45661. ret = WC_TEST_RET_ENC_EC(ret);
  45662. if (ret == 0) {
  45663. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  45664. if (ret != 0)
  45665. ret = WC_TEST_RET_ENC_EC(ret);
  45666. }
  45667. if (ret == 0)
  45668. ret = GenerateP(p1, p2, p3,
  45669. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  45670. if (ret == 0) {
  45671. ret = mp_mul(p1, p2, n);
  45672. if (ret != 0)
  45673. ret = WC_TEST_RET_ENC_EC(ret);
  45674. }
  45675. if (ret == 0) {
  45676. ret = mp_mul(n, p3, n);
  45677. if (ret != 0)
  45678. ret = WC_TEST_RET_ENC_EC(ret);
  45679. }
  45680. if (ret != 0)
  45681. ERROR_OUT(ret, out);
  45682. /* Check the old prime test using the number that false positives.
  45683. * This test result should indicate as not prime. */
  45684. ret = mp_prime_is_prime(n, 40, &isPrime);
  45685. if (ret != 0)
  45686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45687. if (isPrime)
  45688. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45689. /* This test result should fail. It should indicate the value as prime. */
  45690. ret = mp_prime_is_prime(n, 8, &isPrime);
  45691. if (ret != 0)
  45692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45693. if (!isPrime)
  45694. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45695. /* This test result should indicate the value as not prime. */
  45696. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45697. if (ret != 0)
  45698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45699. if (isPrime)
  45700. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45701. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  45702. if (ret != 0)
  45703. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45704. /* This test result should indicate the value as prime. */
  45705. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45706. if (ret != 0)
  45707. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45708. if (!isPrime)
  45709. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45710. /* This test result should indicate the value as prime. */
  45711. isPrime = -1;
  45712. ret = mp_prime_is_prime(n, 8, &isPrime);
  45713. if (ret != 0)
  45714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45715. if (!isPrime)
  45716. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45717. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  45718. if (ret != 0)
  45719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45720. /* This test result should indicate the value as not prime. */
  45721. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45722. if (ret != 0)
  45723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45724. if (isPrime)
  45725. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45726. ret = mp_prime_is_prime(n, 8, &isPrime);
  45727. if (ret != 0)
  45728. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45729. if (isPrime)
  45730. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45731. ret = 0;
  45732. out:
  45733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45734. if (n != NULL) {
  45735. mp_clear(n);
  45736. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45737. }
  45738. if (p1 != NULL) {
  45739. mp_clear(p1);
  45740. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45741. }
  45742. if (p2 != NULL) {
  45743. mp_clear(p2);
  45744. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45745. }
  45746. if (p3 != NULL) {
  45747. mp_clear(p3);
  45748. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45749. }
  45750. #else
  45751. mp_clear(p3);
  45752. mp_clear(p2);
  45753. mp_clear(p1);
  45754. mp_clear(n);
  45755. #endif
  45756. wc_FreeRng(&rng);
  45757. return ret;
  45758. }
  45759. #endif /* WOLFSSL_PUBLIC_MP */
  45760. #if defined(ASN_BER_TO_DER) && \
  45761. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  45762. defined(OPENSSL_EXTRA_X509_SMALL))
  45763. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  45764. typedef struct berDerTestData {
  45765. const byte *in;
  45766. word32 inSz;
  45767. const byte *out;
  45768. word32 outSz;
  45769. } berDerTestData;
  45770. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  45771. {
  45772. wc_test_ret_t ret;
  45773. int i;
  45774. word32 len = 0, l;
  45775. byte out[32];
  45776. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  45777. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  45778. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  45779. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  45780. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  45781. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  45782. };
  45783. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  45784. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  45785. 0x30, 0x80,
  45786. 0x02, 0x01, 0x01,
  45787. 0x30, 0x80,
  45788. 0x24, 0x80,
  45789. 0x04, 0x01, 0x01,
  45790. 0x04, 0x02, 0x02, 0x03,
  45791. 0x00, 0x00,
  45792. 0x06, 0x01, 0x01,
  45793. 0x00, 0x00,
  45794. 0x31, 0x80,
  45795. 0x06, 0x01, 0x01,
  45796. 0x00, 0x00,
  45797. 0x00, 0x00,
  45798. };
  45799. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  45800. 0x30, 0x12,
  45801. 0x02, 0x01, 0x01,
  45802. 0x30, 0x08,
  45803. 0x04, 0x03, 0x01, 0x02, 0x03,
  45804. 0x06, 0x01, 0x01,
  45805. 0x31, 0x03,
  45806. 0x06, 0x01, 0x01
  45807. };
  45808. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  45809. berDerTestData testData[] = {
  45810. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  45811. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  45812. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  45813. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  45814. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  45815. };
  45816. WOLFSSL_ENTER("berder_test");
  45817. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  45818. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  45819. if (ret != LENGTH_ONLY_E)
  45820. return WC_TEST_RET_ENC_I(i);
  45821. if (len != testData[i].outSz)
  45822. return WC_TEST_RET_ENC_I(i);
  45823. len = testData[i].outSz;
  45824. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  45825. if (ret != 0)
  45826. return WC_TEST_RET_ENC_I(i);
  45827. if (XMEMCMP(out, testData[i].out, len) != 0)
  45828. return WC_TEST_RET_ENC_I(i);
  45829. for (l = 1; l < testData[i].inSz; l++) {
  45830. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  45831. if (ret != ASN_PARSE_E)
  45832. return WC_TEST_RET_ENC_EC(ret);
  45833. len = testData[i].outSz;
  45834. ret = wc_BerToDer(testData[i].in, l, out, &len);
  45835. if (ret != ASN_PARSE_E)
  45836. return WC_TEST_RET_ENC_EC(ret);
  45837. }
  45838. for (l = 0; l < testData[i].outSz-1; l++) {
  45839. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  45840. if (ret != BUFFER_E)
  45841. return WC_TEST_RET_ENC_EC(ret);
  45842. }
  45843. }
  45844. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  45845. if (ret != BAD_FUNC_ARG)
  45846. return WC_TEST_RET_ENC_EC(ret);
  45847. ret = wc_BerToDer(out, 4, NULL, NULL);
  45848. if (ret != BAD_FUNC_ARG)
  45849. return WC_TEST_RET_ENC_EC(ret);
  45850. ret = wc_BerToDer(NULL, 4, NULL, &len);
  45851. if (ret != BAD_FUNC_ARG)
  45852. return WC_TEST_RET_ENC_EC(ret);
  45853. ret = wc_BerToDer(NULL, 4, out, NULL);
  45854. if (ret != BAD_FUNC_ARG)
  45855. return WC_TEST_RET_ENC_EC(ret);
  45856. ret = wc_BerToDer(out, 4, out, NULL);
  45857. if (ret != BAD_FUNC_ARG)
  45858. return WC_TEST_RET_ENC_EC(ret);
  45859. ret = wc_BerToDer(NULL, 4, out, &len);
  45860. if (ret != BAD_FUNC_ARG)
  45861. return WC_TEST_RET_ENC_EC(ret);
  45862. for (l = 1; l < sizeof(good4_out); l++) {
  45863. len = l;
  45864. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  45865. if (ret != BUFFER_E)
  45866. return WC_TEST_RET_ENC_EC(ret);
  45867. }
  45868. return 0;
  45869. }
  45870. #endif
  45871. #ifdef DEBUG_WOLFSSL
  45872. static THREAD_LS_T int log_cnt = 0;
  45873. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  45874. {
  45875. (void)logLevel;
  45876. (void)logMessage;
  45877. log_cnt++;
  45878. }
  45879. #endif /* DEBUG_WOLFSSL */
  45880. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  45881. {
  45882. wc_test_ret_t ret;
  45883. #ifdef DEBUG_WOLFSSL
  45884. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  45885. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  45886. byte b[256];
  45887. int i;
  45888. WOLFSSL_ENTER("logging_test (debug)");
  45889. for (i = 0; i < (int)sizeof(b); i++)
  45890. b[i] = i;
  45891. ret = wolfSSL_Debugging_ON();
  45892. if (ret != 0)
  45893. return WC_TEST_RET_ENC_EC(ret);
  45894. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  45895. if (ret != 0)
  45896. return WC_TEST_RET_ENC_EC(ret);
  45897. WOLFSSL_MSG(msg);
  45898. WOLFSSL_BUFFER(a, sizeof(a));
  45899. WOLFSSL_BUFFER(b, sizeof(b));
  45900. WOLFSSL_BUFFER(NULL, 0);
  45901. WOLFSSL_ERROR(MEMORY_E);
  45902. WOLFSSL_ERROR_MSG(msg);
  45903. /* turn off logs */
  45904. wolfSSL_Debugging_OFF();
  45905. /* capture log count */
  45906. i = log_cnt;
  45907. /* validate no logs are output when disabled */
  45908. WOLFSSL_MSG(msg);
  45909. WOLFSSL_BUFFER(a, sizeof(a));
  45910. WOLFSSL_BUFFER(b, sizeof(b));
  45911. WOLFSSL_BUFFER(NULL, 0);
  45912. WOLFSSL_ERROR(MEMORY_E);
  45913. WOLFSSL_ERROR_MSG(msg);
  45914. /* check the logs were disabled */
  45915. if (i != log_cnt)
  45916. return WC_TEST_RET_ENC_NC;
  45917. /* restore callback and leave logging enabled */
  45918. wolfSSL_SetLoggingCb(NULL);
  45919. wolfSSL_Debugging_ON();
  45920. /* suppress unused args */
  45921. (void)a;
  45922. (void)b;
  45923. #else
  45924. WOLFSSL_ENTER("logging_test");
  45925. ret = wolfSSL_Debugging_ON();
  45926. if (ret != NOT_COMPILED_IN)
  45927. return WC_TEST_RET_ENC_EC(ret);
  45928. wolfSSL_Debugging_OFF();
  45929. ret = wolfSSL_SetLoggingCb(NULL);
  45930. if (ret != NOT_COMPILED_IN)
  45931. return WC_TEST_RET_ENC_EC(ret);
  45932. #endif /* DEBUG_WOLFSSL */
  45933. return 0;
  45934. }
  45935. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  45936. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  45937. #else
  45938. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  45939. #endif
  45940. {
  45941. #ifdef WOLFSSL_PTHREADS
  45942. wolfSSL_Mutex m;
  45943. #endif
  45944. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  45945. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  45946. wc_test_ret_t ret;
  45947. #endif
  45948. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  45949. #ifndef WOLFSSL_STATIC_MEMORY
  45950. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  45951. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (1)");
  45952. #else
  45953. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  45954. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  45955. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (2)");
  45956. if (mm != NULL) {
  45957. ret = wc_InitMutex(mm);
  45958. if (ret != 0) {
  45959. WOLFSSL_MSG("Init Mutex failed");
  45960. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  45961. return WC_TEST_RET_ENC_EC(ret);
  45962. }
  45963. }
  45964. #endif
  45965. if (mm == NULL)
  45966. return WC_TEST_RET_ENC_ERRNO;
  45967. wc_FreeMutex(mm);
  45968. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  45969. #endif
  45970. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  45971. #ifdef WOLFSSL_PTHREADS
  45972. ret = wc_InitMutex(&m);
  45973. if (ret != 0)
  45974. return WC_TEST_RET_ENC_EC(ret);
  45975. ret = wc_LockMutex(&m);
  45976. if (ret != 0)
  45977. return WC_TEST_RET_ENC_EC(ret);
  45978. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  45979. /* trying to free a locked mutex is not portable behavior with pthread */
  45980. /* Attempting to destroy a locked mutex results in undefined behavior */
  45981. ret = wc_FreeMutex(&m);
  45982. if (ret != BAD_MUTEX_E)
  45983. return WC_TEST_RET_ENC_EC(ret);
  45984. #endif
  45985. ret = wc_UnLockMutex(&m);
  45986. if (ret != 0)
  45987. return WC_TEST_RET_ENC_EC(ret);
  45988. ret = wc_FreeMutex(&m);
  45989. if (ret != 0)
  45990. return WC_TEST_RET_ENC_EC(ret);
  45991. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  45992. /* Trying to use a pthread after free'ing is not portable behavior */
  45993. ret = wc_LockMutex(&m);
  45994. if (ret != BAD_MUTEX_E)
  45995. return WC_TEST_RET_ENC_EC(ret);
  45996. ret = wc_UnLockMutex(&m);
  45997. if (ret != BAD_MUTEX_E)
  45998. return WC_TEST_RET_ENC_EC(ret);
  45999. #endif
  46000. #endif
  46001. return 0;
  46002. }
  46003. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  46004. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46005. !defined(WOLFSSL_STATIC_MEMORY)
  46006. static wc_test_ret_t malloc_cnt = 0;
  46007. static wc_test_ret_t realloc_cnt = 0;
  46008. static wc_test_ret_t free_cnt = 0;
  46009. #ifdef WOLFSSL_DEBUG_MEMORY
  46010. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  46011. {
  46012. (void) func;
  46013. (void) line;
  46014. #else
  46015. static void *my_Malloc_cb(size_t size)
  46016. {
  46017. #endif
  46018. malloc_cnt++;
  46019. #ifndef WOLFSSL_NO_MALLOC
  46020. return malloc(size);
  46021. #else
  46022. WOLFSSL_MSG("No malloc available");
  46023. (void)size;
  46024. return NULL;
  46025. #endif
  46026. }
  46027. #ifdef WOLFSSL_DEBUG_MEMORY
  46028. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  46029. {
  46030. (void) func;
  46031. (void) line;
  46032. #else
  46033. static void my_Free_cb(void *ptr)
  46034. {
  46035. #endif
  46036. free_cnt++;
  46037. #ifndef WOLFSSL_NO_MALLOC
  46038. free(ptr);
  46039. #else
  46040. WOLFSSL_MSG("No free available");
  46041. (void)ptr;
  46042. #endif
  46043. }
  46044. #ifdef WOLFSSL_DEBUG_MEMORY
  46045. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  46046. {
  46047. (void) func;
  46048. (void) line;
  46049. #else
  46050. static void *my_Realloc_cb(void *ptr, size_t size)
  46051. {
  46052. #endif
  46053. realloc_cnt++;
  46054. #ifndef WOLFSSL_NO_MALLOC
  46055. return realloc(ptr, size);
  46056. #else
  46057. WOLFSSL_MSG("No realloc available");
  46058. (void)ptr;
  46059. (void)size;
  46060. return NULL;
  46061. #endif
  46062. }
  46063. #endif /* !WOLFSSL_NO_MALLOC */
  46064. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  46065. {
  46066. wc_test_ret_t ret = 0;
  46067. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46068. !defined(WOLFSSL_STATIC_MEMORY)
  46069. byte* b = NULL;
  46070. #endif
  46071. wolfSSL_Malloc_cb mc;
  46072. wolfSSL_Free_cb fc;
  46073. wolfSSL_Realloc_cb rc;
  46074. WOLFSSL_ENTER("memcb_test");
  46075. /* Save existing memory callbacks */
  46076. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  46077. if (ret != 0)
  46078. return WC_TEST_RET_ENC_EC(ret);
  46079. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46080. !defined(WOLFSSL_STATIC_MEMORY)
  46081. /* test realloc */
  46082. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46083. if (b == NULL) {
  46084. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  46085. }
  46086. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46087. b = NULL;
  46088. /* Use API. */
  46089. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  46090. (wolfSSL_Free_cb)my_Free_cb,
  46091. (wolfSSL_Realloc_cb)my_Realloc_cb);
  46092. if (ret != 0) {
  46093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  46094. }
  46095. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46096. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46097. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46098. #ifndef WOLFSSL_STATIC_MEMORY
  46099. #ifndef WOLFSSL_CHECK_MEM_ZERO
  46100. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  46101. #else
  46102. /* Checking zeroized memory means realloc is a malloc and free. */
  46103. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  46104. #endif
  46105. #else
  46106. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  46107. #endif
  46108. ret = WC_TEST_RET_ENC_NC;
  46109. #endif /* !WOLFSSL_NO_MALLOC */
  46110. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46111. !defined(WOLFSSL_STATIC_MEMORY)
  46112. exit_memcb:
  46113. /* reset malloc/free/realloc counts */
  46114. malloc_cnt = 0;
  46115. free_cnt = 0;
  46116. realloc_cnt = 0;
  46117. #endif
  46118. /* restore memory callbacks */
  46119. wolfSSL_SetAllocators(mc, fc, rc);
  46120. return ret;
  46121. }
  46122. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  46123. #if defined(WOLFSSL_CAAM_BLOB)
  46124. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  46125. {
  46126. wc_test_ret_t ret = 0;
  46127. byte out[112];
  46128. byte blob[112];
  46129. word32 outSz;
  46130. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  46131. {
  46132. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  46133. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  46134. };
  46135. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  46136. {
  46137. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  46138. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  46139. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  46140. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  46141. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  46142. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  46143. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  46144. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  46145. };
  46146. WOLFSSL_ENTER("blob_test");
  46147. XMEMSET(blob, 0, sizeof(blob));
  46148. XMEMSET(out, 0, sizeof(out));
  46149. outSz = sizeof(blob);
  46150. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  46151. if (ret != 0)
  46152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46153. blob[outSz - 2] += 1;
  46154. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46155. if (ret == 0) { /* should fail with altered blob */
  46156. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46157. }
  46158. XMEMSET(blob, 0, sizeof(blob));
  46159. outSz = sizeof(blob);
  46160. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  46161. if (ret != 0)
  46162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46163. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46164. if (ret != 0)
  46165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46166. if (XMEMCMP(out, iv, sizeof(iv))) {
  46167. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46168. }
  46169. XMEMSET(blob, 0, sizeof(blob));
  46170. outSz = sizeof(blob);
  46171. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  46172. if (ret != 0)
  46173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46174. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46175. if (ret != 0)
  46176. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46177. if (XMEMCMP(out, text, sizeof(text))) {
  46178. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46179. }
  46180. exit_blob:
  46181. return ret;
  46182. }
  46183. #endif /* WOLFSSL_CAAM_BLOB */
  46184. #ifdef WOLF_CRYPTO_CB
  46185. /* Example custom context for crypto callback */
  46186. typedef struct {
  46187. int exampleVar; /* flag for testing if only crypt is enabled. */
  46188. } myCryptoDevCtx;
  46189. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  46190. /* Testing rsa cb when CB_ONLY_RSA is enabled
  46191. * When CB_ONLY_RSA is enabled, software imple. is not available.
  46192. *
  46193. * ctx callback ctx
  46194. * returen 0 on success, otherwise return negative
  46195. */
  46196. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  46197. {
  46198. wc_test_ret_t ret = 0;
  46199. #if !defined(NO_RSA)
  46200. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46201. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  46202. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46203. byte* tmp = NULL;
  46204. #else
  46205. RsaKey key[1];
  46206. byte tmp[FOURK_BUF];
  46207. #endif
  46208. size_t bytes;
  46209. const word32 inLen = (word32)TEST_STRING_SZ;
  46210. word32 idx = 0;
  46211. word32 sigSz;
  46212. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  46213. byte out[RSA_TEST_BYTES];
  46214. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  46215. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  46216. !defined(NO_FILESYSTEM)
  46217. XFILE file;
  46218. #endif
  46219. #ifdef WOLFSSL_KEY_GEN
  46220. WC_RNG rng;
  46221. #endif
  46222. #ifdef USE_CERT_BUFFERS_1024
  46223. bytes = (size_t)sizeof_client_key_der_1024;
  46224. if (bytes < (size_t)sizeof_client_cert_der_1024)
  46225. bytes = (size_t)sizeof_client_cert_der_1024;
  46226. #elif defined(USE_CERT_BUFFERS_2048)
  46227. bytes = (size_t)sizeof_client_key_der_2048;
  46228. if (bytes < (size_t)sizeof_client_cert_der_2048)
  46229. bytes = (size_t)sizeof_client_cert_der_2048;
  46230. #elif defined(USE_CERT_BUFFERS_3072)
  46231. bytes = (size_t)sizeof_client_key_der_3072;
  46232. if (bytes < (size_t)sizeof_client_cert_der_3072)
  46233. bytes = (size_t)sizeof_client_cert_der_3072;
  46234. #elif defined(USE_CERT_BUFFERS_4096)
  46235. bytes = (size_t)sizeof_client_key_der_4096;
  46236. if (bytes < (size_t)sizeof_client_cert_der_4096)
  46237. bytes = (size_t)sizeof_client_cert_der_4096;
  46238. #else
  46239. bytes = FOURK_BUF;
  46240. #endif
  46241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46242. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46243. if (tmp == NULL)
  46244. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46245. #endif
  46246. #ifdef USE_CERT_BUFFERS_1024
  46247. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  46248. #elif defined(USE_CERT_BUFFERS_2048)
  46249. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  46250. #elif defined(USE_CERT_BUFFERS_3072)
  46251. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  46252. #elif defined(USE_CERT_BUFFERS_4096)
  46253. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  46254. #elif !defined(NO_FILESYSTEM)
  46255. file = XFOPEN(clientKey, "rb");
  46256. if (!file) {
  46257. ret = WC_TEST_RET_ENC_ERRNO;
  46258. err_sys("can't open ./certs/client-key.der, "
  46259. "Please run from wolfSSL home dir", ret);
  46260. ERROR_OUT(ret, exit_onlycb);
  46261. }
  46262. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  46263. XFCLOSE(file);
  46264. if (bytes == 0)
  46265. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46266. #endif
  46267. #ifdef WOLFSSL_KEY_GEN
  46268. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  46269. * wc_MakeRsaKey() -> rsa cb ->
  46270. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  46271. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  46272. */
  46273. ctx->exampleVar = 99;
  46274. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  46275. if (ret != 0)
  46276. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46277. /* wc_MakeRsaKey() -> rsa cb ->
  46278. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  46279. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  46280. */
  46281. ctx->exampleVar = 1;
  46282. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  46283. if (ret != NO_VALID_DEVID) {
  46284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46285. } else
  46286. /* reset return code */
  46287. ret = 0;
  46288. #endif
  46289. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  46290. if (ret != 0)
  46291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46292. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  46293. if (ret != 0)
  46294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46295. sigSz = (word32)wc_RsaEncryptSize(key);
  46296. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  46297. if (ret == 0) {
  46298. /* wc_SignatureGenerate() -> rsa cb ->
  46299. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  46300. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  46301. */
  46302. ctx->exampleVar = 99;
  46303. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  46304. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  46305. if (ret != 0)
  46306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46307. }
  46308. if (ret == 0) {
  46309. /* wc_SignatureGenerate() -> rsa cb ->
  46310. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  46311. * wc_SignatureGenerate(INVALID_DEVID) expects to
  46312. * return NO_VALID_DEVID(failure)
  46313. */
  46314. ctx->exampleVar = 1;
  46315. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  46316. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  46317. if (ret != NO_VALID_DEVID) {
  46318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46319. } else
  46320. /* reset return code */
  46321. ret = 0;
  46322. }
  46323. exit_onlycb:
  46324. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46325. if (key != NULL) {
  46326. wc_FreeRsaKey(key);
  46327. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46328. }
  46329. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46330. #else
  46331. wc_FreeRsaKey(key);
  46332. #endif
  46333. #endif
  46334. return ret;
  46335. }
  46336. #endif
  46337. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  46338. /* Testing rsa cb when CB_ONLY_ECC is enabled
  46339. * When CB_ONLY_ECC is enabled, software imple. is not available.
  46340. *
  46341. * ctx callback ctx
  46342. * returen 0 on success, otherwise return negative
  46343. */
  46344. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  46345. {
  46346. wc_test_ret_t ret = 0;
  46347. #if defined(HAVE_ECC)
  46348. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46349. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  46350. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46351. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  46352. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46353. byte* out = (byte*)XMALLOC(sizeof(byte),
  46354. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46355. #ifdef OPENSSL_EXTRA
  46356. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  46357. DYNAMIC_TYPE_TMP_BUFFER);
  46358. #endif
  46359. #else
  46360. ecc_key key[1];
  46361. ecc_key pub[1];
  46362. byte out[256];
  46363. #ifdef OPENSSL_EXTRA
  46364. unsigned char check[256];
  46365. #endif
  46366. #endif
  46367. #ifdef OPENSSL_EXTRA
  46368. EVP_PKEY* privKey = NULL;
  46369. EVP_PKEY* pubKey = NULL;
  46370. #ifdef USE_CERT_BUFFERS_256
  46371. ecc_key* pkey;
  46372. const unsigned char* cp;
  46373. #endif
  46374. EVP_MD_CTX mdCtx;
  46375. const char testData[] = "Hi There";
  46376. size_t checkSz = -1;
  46377. const unsigned char* p;
  46378. const unsigned char check_v[256] = {
  46379. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  46380. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  46381. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  46382. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  46383. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  46384. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  46385. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  46386. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  46387. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  46388. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  46389. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  46390. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  46391. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  46392. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  46393. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  46394. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  46395. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  46396. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  46397. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  46398. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  46399. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  46400. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  46401. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  46402. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  46403. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  46404. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  46405. };
  46406. #endif
  46407. WC_RNG rng;
  46408. EncryptedInfo encInfo;
  46409. int keyFormat = 0;
  46410. word32 keyIdx = 0;
  46411. byte in[] = "Everyone gets Friday off. ecc p";
  46412. word32 inLen = (word32)XSTRLEN((char*)in);
  46413. word32 outLen;
  46414. int verify;
  46415. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46416. if (key == NULL || pub == NULL) {
  46417. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46418. }
  46419. #endif
  46420. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  46421. if (ret != 0)
  46422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46423. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  46424. ctx->exampleVar = 99;
  46425. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  46426. if (ret != 0)
  46427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46428. ctx->exampleVar = 1;
  46429. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  46430. if (ret != NO_VALID_DEVID) {
  46431. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46432. } else
  46433. /* reset return code */
  46434. ret = 0;
  46435. #ifdef USE_CERT_BUFFERS_256
  46436. if (ret == 0) {
  46437. /* load ECC private key and perform private transform */
  46438. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  46439. key, sizeof_ecc_key_der_256);
  46440. }
  46441. if (ret != 0)
  46442. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46443. /* wc_CryptoCb_EccSign cb test, no actual testing */
  46444. ctx->exampleVar = 99;
  46445. if (ret == 0) {
  46446. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  46447. }
  46448. if (ret != 0)
  46449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46450. ctx->exampleVar = 1;
  46451. if (ret == 0) {
  46452. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  46453. }
  46454. if (ret != NO_VALID_DEVID) {
  46455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46456. }
  46457. else
  46458. ret = 0;
  46459. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  46460. ctx->exampleVar = 99;
  46461. if (ret == 0) {
  46462. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  46463. }
  46464. if (ret != 0)
  46465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46466. ctx->exampleVar = 1;
  46467. if (ret == 0) {
  46468. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  46469. }
  46470. if (ret != NO_VALID_DEVID) {
  46471. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46472. }
  46473. else
  46474. ret = 0;
  46475. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  46476. /* make public key for shared secret */
  46477. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  46478. ctx->exampleVar = 99;
  46479. if (ret == 0) {
  46480. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  46481. }
  46482. if (ret != 0)
  46483. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46484. ctx->exampleVar = 1;
  46485. if (ret == 0) {
  46486. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  46487. }
  46488. if (ret != NO_VALID_DEVID) {
  46489. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46490. }
  46491. else
  46492. ret = 0;
  46493. #ifdef OPENSSL_EXTRA
  46494. (void)pkey;
  46495. cp = ecc_clikey_der_256;
  46496. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  46497. sizeof_ecc_clikey_der_256);
  46498. if (privKey == NULL) {
  46499. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46500. }
  46501. pkey = (ecc_key*)privKey->ecc->internal;
  46502. pkey->devId = devId;
  46503. p = ecc_clikeypub_der_256;
  46504. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  46505. if (pubKey == NULL) {
  46506. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46507. }
  46508. pkey = (ecc_key*)pubKey->ecc->internal;
  46509. pkey->devId = devId;
  46510. /* sign */
  46511. EVP_MD_CTX_init(&mdCtx);
  46512. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  46513. if (ret != WOLFSSL_SUCCESS) {
  46514. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46515. }
  46516. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  46517. (unsigned int)XSTRLEN(testData));
  46518. if (ret != WOLFSSL_SUCCESS) {
  46519. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46520. }
  46521. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  46522. if (ret != WOLFSSL_SUCCESS) {
  46523. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46524. }
  46525. ctx->exampleVar = 99;
  46526. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  46527. /* just called crypt callback as dummy
  46528. * EVP_DigestSignFinal returns 0 internally.
  46529. */
  46530. if (ret != 0)
  46531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46532. ctx->exampleVar = 1;
  46533. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  46534. /* just called crypt callback as dummy
  46535. * EVP_DigestSignFinal returns 0 internally.
  46536. */
  46537. if (ret != 0)
  46538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46539. /* restore checkSz for verify */
  46540. checkSz = 71;
  46541. ret = EVP_MD_CTX_cleanup(&mdCtx);
  46542. if (ret != SSL_SUCCESS) {
  46543. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46544. }
  46545. /* verify */
  46546. EVP_MD_CTX_init(&mdCtx);
  46547. if (ret == SSL_SUCCESS) {
  46548. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  46549. }
  46550. if (ret != WOLFSSL_SUCCESS) {
  46551. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46552. }
  46553. if (ret == WOLFSSL_SUCCESS) {
  46554. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  46555. (unsigned int)XSTRLEN(testData));
  46556. }
  46557. if (ret != WOLFSSL_SUCCESS) {
  46558. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46559. }
  46560. ctx->exampleVar = 99;
  46561. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  46562. /* just called crypt callback as dummy
  46563. * EVP_DigestSignFinal returns 0 internally.
  46564. */
  46565. if (ret != 0)
  46566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46567. ctx->exampleVar = 1;
  46568. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  46569. /* just called crypt callback as dummy
  46570. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  46571. */
  46572. if (ret != -1) {
  46573. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46574. }
  46575. ret = EVP_MD_CTX_cleanup(&mdCtx);
  46576. if (ret != SSL_SUCCESS) {
  46577. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46578. } else
  46579. ret = 0;
  46580. #endif
  46581. #else
  46582. (void)verify;
  46583. (void)outLen;
  46584. (void)inLen;
  46585. (void)out;
  46586. (void)pub;
  46587. #ifdef OPENSSL_EXTRA
  46588. (void)privKey;
  46589. (void)pubKey;
  46590. (void)mdCtx;
  46591. (void)check;
  46592. (void)checkSz;
  46593. (void)p;
  46594. #endif
  46595. #endif
  46596. (void)keyFormat;
  46597. (void)encInfo;
  46598. exit_onlycb:
  46599. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46600. if (key != NULL) {
  46601. wc_ecc_free(key);
  46602. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46603. }
  46604. if (pub != NULL) {
  46605. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46606. }
  46607. if (out != NULL) {
  46608. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46609. }
  46610. #ifdef OPENSSL_EXTRA
  46611. if (check) {
  46612. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46613. }
  46614. #endif
  46615. #else
  46616. wc_ecc_free(key);
  46617. #ifdef OPENSSL_EXTRA
  46618. if (privKey)
  46619. EVP_PKEY_free(privKey);
  46620. if (pubKey)
  46621. EVP_PKEY_free(pubKey);
  46622. #endif
  46623. #endif
  46624. #endif /* HAVE_ECC */
  46625. return ret;
  46626. }
  46627. #endif
  46628. /* Example crypto dev callback function that calls software version */
  46629. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  46630. {
  46631. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  46632. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  46633. if (info == NULL)
  46634. return BAD_FUNC_ARG;
  46635. #ifdef DEBUG_WOLFSSL
  46636. WOLFSSL_MSG_EX("CryptoDevCb: Algo Type %d\n", info->algo_type);
  46637. #endif
  46638. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  46639. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  46640. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  46641. /* if RNG only supports crypto callback, just use seed */
  46642. ret = wc_GenerateSeed(&info->rng.rng->seed,
  46643. info->rng.out, info->rng.sz);
  46644. #elif !defined(WC_NO_RNG)
  46645. /* set devId to invalid, so software is used */
  46646. info->rng.rng->devId = INVALID_DEVID;
  46647. ret = wc_RNG_GenerateBlock(info->rng.rng,
  46648. info->rng.out, info->rng.sz);
  46649. /* reset devId */
  46650. info->rng.rng->devId = devIdArg;
  46651. #endif
  46652. }
  46653. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  46654. #ifndef WC_NO_RNG
  46655. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  46656. word32* seedWord32 = (word32*)seed;
  46657. word32 len;
  46658. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  46659. while (info->seed.sz > 0) {
  46660. len = (word32)sizeof(seed);
  46661. if (info->seed.sz < len)
  46662. len = info->seed.sz;
  46663. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  46664. info->seed.seed += len;
  46665. info->seed.sz -= len;
  46666. (*seedWord32)++;
  46667. }
  46668. ret = 0;
  46669. #endif
  46670. }
  46671. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  46672. #ifdef DEBUG_WOLFSSL
  46673. WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
  46674. #endif
  46675. #ifndef NO_RSA
  46676. if (info->pk.type == WC_PK_TYPE_RSA) {
  46677. /* set devId to invalid, so software is used */
  46678. info->pk.rsa.key->devId = INVALID_DEVID;
  46679. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  46680. #ifdef DEBUG_WOLFSSL
  46681. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46682. #endif
  46683. if (myCtx->exampleVar == 99) {
  46684. info->pk.rsa.key->devId = devIdArg;
  46685. return 0;
  46686. }
  46687. #endif
  46688. switch (info->pk.rsa.type) {
  46689. case RSA_PUBLIC_ENCRYPT:
  46690. case RSA_PUBLIC_DECRYPT:
  46691. /* perform software based RSA public op */
  46692. ret = wc_RsaFunction(
  46693. info->pk.rsa.in, info->pk.rsa.inLen,
  46694. info->pk.rsa.out, info->pk.rsa.outLen,
  46695. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  46696. break;
  46697. case RSA_PRIVATE_ENCRYPT:
  46698. case RSA_PRIVATE_DECRYPT:
  46699. /* perform software based RSA private op */
  46700. ret = wc_RsaFunction(
  46701. info->pk.rsa.in, info->pk.rsa.inLen,
  46702. info->pk.rsa.out, info->pk.rsa.outLen,
  46703. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  46704. break;
  46705. }
  46706. /* reset devId */
  46707. info->pk.rsa.key->devId = devIdArg;
  46708. }
  46709. #ifdef WOLFSSL_KEY_GEN
  46710. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  46711. info->pk.rsakg.key->devId = INVALID_DEVID;
  46712. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  46713. #ifdef DEBUG_WOLFSSL
  46714. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46715. #endif
  46716. if (myCtx->exampleVar == 99) {
  46717. info->pk.rsakg.key->devId = devIdArg;
  46718. return 0;
  46719. }
  46720. #endif
  46721. #ifdef HAVE_FIPS
  46722. for (;;) {
  46723. #endif
  46724. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  46725. info->pk.rsakg.e, info->pk.rsakg.rng);
  46726. #ifdef HAVE_FIPS
  46727. if (ret == PRIME_GEN_E)
  46728. continue;
  46729. break;
  46730. }
  46731. #endif
  46732. /* reset devId */
  46733. info->pk.rsakg.key->devId = devIdArg;
  46734. }
  46735. #endif
  46736. #endif /* !NO_RSA */
  46737. #ifdef HAVE_ECC
  46738. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  46739. /* set devId to invalid, so software is used */
  46740. info->pk.eckg.key->devId = INVALID_DEVID;
  46741. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46742. #ifdef DEBUG_WOLFSSL
  46743. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46744. #endif
  46745. if (myCtx->exampleVar == 99) {
  46746. info->pk.eckg.key->devId = devIdArg;
  46747. return 0;
  46748. }
  46749. #endif
  46750. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  46751. info->pk.eckg.key, info->pk.eckg.curveId);
  46752. /* reset devId */
  46753. info->pk.eckg.key->devId = devIdArg;
  46754. }
  46755. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  46756. /* set devId to invalid, so software is used */
  46757. info->pk.eccsign.key->devId = INVALID_DEVID;
  46758. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46759. #ifdef DEBUG_WOLFSSL
  46760. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46761. #endif
  46762. if (myCtx->exampleVar == 99) {
  46763. info->pk.eccsign.key->devId = devIdArg;
  46764. return 0;
  46765. }
  46766. #endif
  46767. ret = wc_ecc_sign_hash(
  46768. info->pk.eccsign.in, info->pk.eccsign.inlen,
  46769. info->pk.eccsign.out, info->pk.eccsign.outlen,
  46770. info->pk.eccsign.rng, info->pk.eccsign.key);
  46771. /* reset devId */
  46772. info->pk.eccsign.key->devId = devIdArg;
  46773. }
  46774. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  46775. /* set devId to invalid, so software is used */
  46776. info->pk.eccverify.key->devId = INVALID_DEVID;
  46777. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46778. #ifdef DEBUG_WOLFSSL
  46779. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46780. #endif
  46781. if (myCtx->exampleVar == 99) {
  46782. info->pk.eccverify.key->devId = devIdArg;
  46783. return 0;
  46784. }
  46785. #endif
  46786. ret = wc_ecc_verify_hash(
  46787. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  46788. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  46789. info->pk.eccverify.res, info->pk.eccverify.key);
  46790. /* reset devId */
  46791. info->pk.eccverify.key->devId = devIdArg;
  46792. }
  46793. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  46794. /* set devId to invalid, so software is used */
  46795. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  46796. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46797. #ifdef DEBUG_WOLFSSL
  46798. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46799. #endif
  46800. if (myCtx->exampleVar == 99) {
  46801. info->pk.ecdh.private_key->devId = devIdArg;
  46802. return 0;
  46803. }
  46804. #endif
  46805. ret = wc_ecc_shared_secret(
  46806. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  46807. info->pk.ecdh.out, info->pk.ecdh.outlen);
  46808. /* reset devId */
  46809. info->pk.ecdh.private_key->devId = devIdArg;
  46810. }
  46811. #endif /* HAVE_ECC */
  46812. #ifdef HAVE_CURVE25519
  46813. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  46814. /* set devId to invalid, so software is used */
  46815. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  46816. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  46817. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  46818. /* reset devId */
  46819. info->pk.curve25519kg.key->devId = devIdArg;
  46820. }
  46821. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  46822. /* set devId to invalid, so software is used */
  46823. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  46824. ret = wc_curve25519_shared_secret_ex(
  46825. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  46826. info->pk.curve25519.out, info->pk.curve25519.outlen,
  46827. info->pk.curve25519.endian);
  46828. /* reset devId */
  46829. info->pk.curve25519.private_key->devId = devIdArg;
  46830. }
  46831. #endif /* HAVE_CURVE25519 */
  46832. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  46833. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  46834. /* set devId to invalid, so software is used */
  46835. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  46836. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  46837. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  46838. /* reset devId */
  46839. info->pk.ed25519kg.key->devId = devIdArg;
  46840. }
  46841. #ifdef HAVE_ED25519_SIGN
  46842. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  46843. /* set devId to invalid, so software is used */
  46844. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  46845. ret = wc_ed25519_sign_msg_ex(
  46846. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  46847. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  46848. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  46849. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  46850. /* reset devId */
  46851. info->pk.ed25519sign.key->devId = devIdArg;
  46852. }
  46853. #endif
  46854. #ifdef HAVE_ED25519_VERIFY
  46855. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  46856. /* set devId to invalid, so software is used */
  46857. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  46858. ret = wc_ed25519_verify_msg_ex(
  46859. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  46860. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  46861. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  46862. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  46863. info->pk.ed25519verify.contextLen);
  46864. /* reset devId */
  46865. info->pk.ed25519verify.key->devId = devIdArg;
  46866. }
  46867. #endif
  46868. #endif /* HAVE_ED25519 */
  46869. }
  46870. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  46871. #if !defined(NO_AES) || !defined(NO_DES3)
  46872. #ifdef HAVE_AESGCM
  46873. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  46874. if (info->cipher.enc) {
  46875. /* set devId to invalid, so software is used */
  46876. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  46877. ret = wc_AesGcmEncrypt(
  46878. info->cipher.aesgcm_enc.aes,
  46879. info->cipher.aesgcm_enc.out,
  46880. info->cipher.aesgcm_enc.in,
  46881. info->cipher.aesgcm_enc.sz,
  46882. info->cipher.aesgcm_enc.iv,
  46883. info->cipher.aesgcm_enc.ivSz,
  46884. info->cipher.aesgcm_enc.authTag,
  46885. info->cipher.aesgcm_enc.authTagSz,
  46886. info->cipher.aesgcm_enc.authIn,
  46887. info->cipher.aesgcm_enc.authInSz);
  46888. /* reset devId */
  46889. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  46890. }
  46891. else {
  46892. /* set devId to invalid, so software is used */
  46893. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  46894. ret = wc_AesGcmDecrypt(
  46895. info->cipher.aesgcm_dec.aes,
  46896. info->cipher.aesgcm_dec.out,
  46897. info->cipher.aesgcm_dec.in,
  46898. info->cipher.aesgcm_dec.sz,
  46899. info->cipher.aesgcm_dec.iv,
  46900. info->cipher.aesgcm_dec.ivSz,
  46901. info->cipher.aesgcm_dec.authTag,
  46902. info->cipher.aesgcm_dec.authTagSz,
  46903. info->cipher.aesgcm_dec.authIn,
  46904. info->cipher.aesgcm_dec.authInSz);
  46905. /* reset devId */
  46906. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  46907. }
  46908. }
  46909. #endif /* HAVE_AESGCM */
  46910. #ifdef HAVE_AES_CBC
  46911. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  46912. if (info->cipher.enc) {
  46913. /* set devId to invalid, so software is used */
  46914. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  46915. ret = wc_AesCbcEncrypt(
  46916. info->cipher.aescbc.aes,
  46917. info->cipher.aescbc.out,
  46918. info->cipher.aescbc.in,
  46919. info->cipher.aescbc.sz);
  46920. /* reset devId */
  46921. info->cipher.aescbc.aes->devId = devIdArg;
  46922. }
  46923. else {
  46924. /* set devId to invalid, so software is used */
  46925. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  46926. ret = wc_AesCbcDecrypt(
  46927. info->cipher.aescbc.aes,
  46928. info->cipher.aescbc.out,
  46929. info->cipher.aescbc.in,
  46930. info->cipher.aescbc.sz);
  46931. /* reset devId */
  46932. info->cipher.aescbc.aes->devId = devIdArg;
  46933. }
  46934. }
  46935. #endif /* HAVE_AES_CBC */
  46936. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  46937. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  46938. if (info->cipher.enc) {
  46939. /* set devId to invalid, so software is used */
  46940. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  46941. ret = wc_AesEcbEncrypt(
  46942. info->cipher.aesecb.aes,
  46943. info->cipher.aesecb.out,
  46944. info->cipher.aesecb.in,
  46945. info->cipher.aesecb.sz);
  46946. /* reset devId */
  46947. info->cipher.aesecb.aes->devId = devIdArg;
  46948. }
  46949. else {
  46950. /* set devId to invalid, so software is used */
  46951. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  46952. ret = wc_AesEcbDecrypt(
  46953. info->cipher.aesecb.aes,
  46954. info->cipher.aesecb.out,
  46955. info->cipher.aesecb.in,
  46956. info->cipher.aesecb.sz);
  46957. /* reset devId */
  46958. info->cipher.aesecb.aes->devId = devIdArg;
  46959. }
  46960. }
  46961. #endif /* HAVE_AES_ECB */
  46962. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  46963. !defined(HAVE_SELFTEST)
  46964. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  46965. /* set devId to invalid, so software is used */
  46966. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  46967. ret = wc_AesCtrEncrypt(
  46968. info->cipher.aesctr.aes,
  46969. info->cipher.aesctr.out,
  46970. info->cipher.aesctr.in,
  46971. info->cipher.aesctr.sz);
  46972. /* reset devId */
  46973. info->cipher.aesctr.aes->devId = devIdArg;
  46974. }
  46975. #endif /* WOLFSSL_AES_COUNTER */
  46976. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  46977. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  46978. if (info->cipher.enc) {
  46979. /* set devId to invalid, so software is used */
  46980. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  46981. ret = wc_AesCcmEncrypt(
  46982. info->cipher.aesccm_enc.aes,
  46983. info->cipher.aesccm_enc.out,
  46984. info->cipher.aesccm_enc.in,
  46985. info->cipher.aesccm_enc.sz,
  46986. info->cipher.aesccm_enc.nonce,
  46987. info->cipher.aesccm_enc.nonceSz,
  46988. info->cipher.aesccm_enc.authTag,
  46989. info->cipher.aesccm_enc.authTagSz,
  46990. info->cipher.aesccm_enc.authIn,
  46991. info->cipher.aesccm_enc.authInSz);
  46992. /* reset devId */
  46993. info->cipher.aesccm_enc.aes->devId = devIdArg;
  46994. }
  46995. else {
  46996. /* set devId to invalid, so software is used */
  46997. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  46998. ret = wc_AesCcmDecrypt(
  46999. info->cipher.aesccm_dec.aes,
  47000. info->cipher.aesccm_dec.out,
  47001. info->cipher.aesccm_dec.in,
  47002. info->cipher.aesccm_dec.sz,
  47003. info->cipher.aesccm_dec.nonce,
  47004. info->cipher.aesccm_dec.nonceSz,
  47005. info->cipher.aesccm_dec.authTag,
  47006. info->cipher.aesccm_dec.authTagSz,
  47007. info->cipher.aesccm_dec.authIn,
  47008. info->cipher.aesccm_dec.authInSz);
  47009. /* reset devId */
  47010. info->cipher.aesccm_dec.aes->devId = devIdArg;
  47011. }
  47012. }
  47013. #endif
  47014. #ifndef NO_DES3
  47015. if (info->cipher.type == WC_CIPHER_DES3) {
  47016. if (info->cipher.enc) {
  47017. /* set devId to invalid, so software is used */
  47018. info->cipher.des3.des->devId = INVALID_DEVID;
  47019. ret = wc_Des3_CbcEncrypt(
  47020. info->cipher.des3.des,
  47021. info->cipher.des3.out,
  47022. info->cipher.des3.in,
  47023. info->cipher.des3.sz);
  47024. /* reset devId */
  47025. info->cipher.des3.des->devId = devIdArg;
  47026. }
  47027. else {
  47028. /* set devId to invalid, so software is used */
  47029. info->cipher.des3.des->devId = INVALID_DEVID;
  47030. ret = wc_Des3_CbcDecrypt(
  47031. info->cipher.des3.des,
  47032. info->cipher.des3.out,
  47033. info->cipher.des3.in,
  47034. info->cipher.des3.sz);
  47035. /* reset devId */
  47036. info->cipher.des3.des->devId = devIdArg;
  47037. }
  47038. }
  47039. #endif /* !NO_DES3 */
  47040. #endif /* !NO_AES || !NO_DES3 */
  47041. }
  47042. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  47043. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  47044. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  47045. #if !defined(NO_SHA)
  47046. if (info->hash.type == WC_HASH_TYPE_SHA) {
  47047. if (info->hash.sha1 == NULL)
  47048. return NOT_COMPILED_IN;
  47049. /* set devId to invalid, so software is used */
  47050. info->hash.sha1->devId = INVALID_DEVID;
  47051. if (info->hash.in != NULL) {
  47052. ret = wc_ShaUpdate(
  47053. info->hash.sha1,
  47054. info->hash.in,
  47055. info->hash.inSz);
  47056. }
  47057. if (info->hash.digest != NULL) {
  47058. ret = wc_ShaFinal(
  47059. info->hash.sha1,
  47060. info->hash.digest);
  47061. }
  47062. /* reset devId */
  47063. info->hash.sha1->devId = devIdArg;
  47064. }
  47065. else
  47066. #endif
  47067. #if !defined(NO_SHA256)
  47068. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  47069. if (info->hash.sha256 == NULL)
  47070. return NOT_COMPILED_IN;
  47071. /* set devId to invalid, so software is used */
  47072. info->hash.sha256->devId = INVALID_DEVID;
  47073. if (info->hash.in != NULL) {
  47074. ret = wc_Sha256Update(
  47075. info->hash.sha256,
  47076. info->hash.in,
  47077. info->hash.inSz);
  47078. }
  47079. if (info->hash.digest != NULL) {
  47080. ret = wc_Sha256Final(
  47081. info->hash.sha256,
  47082. info->hash.digest);
  47083. }
  47084. /* reset devId */
  47085. info->hash.sha256->devId = devIdArg;
  47086. }
  47087. else
  47088. #endif
  47089. #ifdef WOLFSSL_SHA384
  47090. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  47091. if (info->hash.sha384 == NULL)
  47092. return NOT_COMPILED_IN;
  47093. #ifndef NO_SHA2_CRYPTO_CB
  47094. /* set devId to invalid, so software is used */
  47095. info->hash.sha384->devId = INVALID_DEVID;
  47096. #endif
  47097. if (info->hash.in != NULL) {
  47098. ret = wc_Sha384Update(
  47099. info->hash.sha384,
  47100. info->hash.in,
  47101. info->hash.inSz);
  47102. }
  47103. if (info->hash.digest != NULL) {
  47104. ret = wc_Sha384Final(
  47105. info->hash.sha384,
  47106. info->hash.digest);
  47107. }
  47108. #ifndef NO_SHA2_CRYPTO_CB
  47109. /* reset devId */
  47110. info->hash.sha384->devId = devIdArg;
  47111. #endif
  47112. }
  47113. else
  47114. #endif
  47115. #ifdef WOLFSSL_SHA512
  47116. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  47117. if (info->hash.sha512 == NULL)
  47118. return NOT_COMPILED_IN;
  47119. #ifndef NO_SHA2_CRYPTO_CB
  47120. /* set devId to invalid, so software is used */
  47121. info->hash.sha512->devId = INVALID_DEVID;
  47122. #endif
  47123. if (info->hash.in != NULL) {
  47124. ret = wc_Sha512Update(
  47125. info->hash.sha512,
  47126. info->hash.in,
  47127. info->hash.inSz);
  47128. }
  47129. if (info->hash.digest != NULL) {
  47130. ret = wc_Sha512Final(
  47131. info->hash.sha512,
  47132. info->hash.digest);
  47133. }
  47134. #ifndef NO_SHA2_CRYPTO_CB
  47135. /* reset devId */
  47136. info->hash.sha512->devId = devIdArg;
  47137. #endif
  47138. }
  47139. else
  47140. #endif
  47141. {
  47142. }
  47143. }
  47144. #endif /* !NO_SHA || !NO_SHA256 */
  47145. #ifndef NO_HMAC
  47146. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  47147. if (info->hmac.hmac == NULL)
  47148. return NOT_COMPILED_IN;
  47149. /* set devId to invalid, so software is used */
  47150. info->hmac.hmac->devId = INVALID_DEVID;
  47151. if (info->hmac.in != NULL) {
  47152. ret = wc_HmacUpdate(
  47153. info->hmac.hmac,
  47154. info->hmac.in,
  47155. info->hmac.inSz);
  47156. }
  47157. else if (info->hmac.digest != NULL) {
  47158. ret = wc_HmacFinal(
  47159. info->hmac.hmac,
  47160. info->hmac.digest);
  47161. }
  47162. /* reset devId */
  47163. info->hmac.hmac->devId = devIdArg;
  47164. }
  47165. #endif
  47166. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  47167. else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
  47168. if (info->cmac.cmac == NULL) {
  47169. return NOT_COMPILED_IN;
  47170. }
  47171. /* set devId to invalid so software is used */
  47172. info->cmac.cmac->devId = INVALID_DEVID;
  47173. /* Handle one-shot cases */
  47174. if (info->cmac.key != NULL && info->cmac.in != NULL
  47175. && info->cmac.out != NULL) {
  47176. ret = wc_AesCmacGenerate(info->cmac.out,
  47177. info->cmac.outSz,
  47178. info->cmac.in,
  47179. info->cmac.inSz,
  47180. info->cmac.key,
  47181. info->cmac.keySz);
  47182. /* Sequentially handle incremental cases */
  47183. } else {
  47184. if (info->cmac.key != NULL) {
  47185. ret = wc_InitCmac(info->cmac.cmac,
  47186. info->cmac.key,
  47187. info->cmac.keySz,
  47188. info->cmac.type,
  47189. NULL);
  47190. }
  47191. if ((ret == 0) && (info->cmac.in != NULL)) {
  47192. ret = wc_CmacUpdate(info->cmac.cmac,
  47193. info->cmac.in,
  47194. info->cmac.inSz);
  47195. }
  47196. if ((ret ==0) && (info->cmac.out != NULL)) {
  47197. ret = wc_CmacFinal(info->cmac.cmac,
  47198. info->cmac.out,
  47199. info->cmac.outSz);
  47200. }
  47201. }
  47202. /* reset devId */
  47203. info->cmac.cmac->devId = devIdArg;
  47204. }
  47205. #endif /* WOLFSSL_CMAC && !(NO_AES) && WOLFSSL_AES_DIRECT */
  47206. (void)devIdArg;
  47207. (void)myCtx;
  47208. return ret;
  47209. }
  47210. #ifdef WOLF_CRYPTO_CB_FIND
  47211. static int myCryptoCbFind(int currentId, int algoType)
  47212. {
  47213. /* can have algo specific overrides here
  47214. switch (algoType) {
  47215. i.e.
  47216. WC_ALGO_TYPE_CMAC
  47217. WC_ALGO_TYPE_SEED
  47218. WC_ALGO_TYPE_HMAC
  47219. WC_ALGO_TYPE_HASH
  47220. WC_ALGO_TYPE_CIPHER
  47221. WC_ALGO_TYPE_PK
  47222. }
  47223. */
  47224. (void)algoType;
  47225. if (currentId == INVALID_DEVID) {
  47226. /* can override invalid devid found with 1 */
  47227. }
  47228. return currentId;
  47229. }
  47230. #endif /* WOLF_CRYPTO_CB_FIND */
  47231. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  47232. {
  47233. wc_test_ret_t ret = 0;
  47234. int origDevId = devId;
  47235. myCryptoDevCtx myCtx;
  47236. WOLFSSL_ENTER("cryptocb_test");
  47237. /* example data for callback */
  47238. myCtx.exampleVar = 1;
  47239. /* set devId to something other than INVALID_DEVID */
  47240. devId = 1;
  47241. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  47242. #ifdef WOLF_CRYPTO_CB_FIND
  47243. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  47244. #endif /* WOLF_CRYPTO_CB_FIND */
  47245. #ifndef WC_NO_RNG
  47246. if (ret == 0)
  47247. ret = random_test();
  47248. #endif /* WC_NO_RNG */
  47249. #if !defined(NO_RSA)
  47250. PRIVATE_KEY_UNLOCK();
  47251. if (ret == 0)
  47252. ret = rsa_test();
  47253. PRIVATE_KEY_LOCK();
  47254. #endif
  47255. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  47256. PRIVATE_KEY_UNLOCK();
  47257. if (ret == 0)
  47258. ret = rsa_onlycb_test(&myCtx);
  47259. PRIVATE_KEY_LOCK();
  47260. #endif
  47261. #if defined(HAVE_ECC)
  47262. PRIVATE_KEY_UNLOCK();
  47263. if (ret == 0)
  47264. ret = ecc_test();
  47265. PRIVATE_KEY_LOCK();
  47266. #endif
  47267. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  47268. PRIVATE_KEY_UNLOCK();
  47269. if (ret == 0)
  47270. ret = ecc_onlycb_test(&myCtx);
  47271. PRIVATE_KEY_LOCK();
  47272. #endif
  47273. #ifdef HAVE_ED25519
  47274. PRIVATE_KEY_UNLOCK();
  47275. if (ret == 0)
  47276. ret = ed25519_test();
  47277. PRIVATE_KEY_LOCK();
  47278. #endif
  47279. #ifdef HAVE_CURVE25519
  47280. if (ret == 0)
  47281. ret = curve25519_test();
  47282. #endif
  47283. #ifndef NO_AES
  47284. #ifdef HAVE_AESGCM
  47285. if (ret == 0)
  47286. ret = aesgcm_test();
  47287. #endif
  47288. #ifdef HAVE_AES_CBC
  47289. if (ret == 0)
  47290. ret = aes_test();
  47291. #endif
  47292. #ifdef WOLFSSL_AES_XTS
  47293. if (ret == 0)
  47294. ret = aes_xts_test();
  47295. #endif
  47296. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  47297. if (ret == 0)
  47298. ret = aesccm_test();
  47299. #endif
  47300. #endif /* !NO_AES */
  47301. #ifndef NO_DES3
  47302. if (ret == 0)
  47303. ret = des3_test();
  47304. #endif /* !NO_DES3 */
  47305. #ifndef NO_SHA
  47306. if (ret == 0)
  47307. ret = sha_test();
  47308. #endif
  47309. #ifndef NO_SHA256
  47310. if (ret == 0)
  47311. ret = sha256_test();
  47312. #endif
  47313. #ifdef WOLFSSL_SHA384
  47314. if (ret == 0)
  47315. ret = sha384_test();
  47316. #endif
  47317. #ifdef WOLFSSL_SHA512
  47318. if (ret == 0)
  47319. ret = sha512_test();
  47320. #endif
  47321. #ifndef NO_HMAC
  47322. #ifndef NO_SHA
  47323. if (ret == 0)
  47324. ret = hmac_sha_test();
  47325. #endif
  47326. #ifndef NO_SHA256
  47327. if (ret == 0)
  47328. ret = hmac_sha256_test();
  47329. #endif
  47330. #endif
  47331. #ifndef NO_PWDBASED
  47332. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  47333. PRIVATE_KEY_UNLOCK();
  47334. if (ret == 0)
  47335. ret = pbkdf2_test();
  47336. PRIVATE_KEY_LOCK();
  47337. #endif
  47338. #endif
  47339. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  47340. if (ret == 0)
  47341. ret = cmac_test();
  47342. #endif
  47343. /* restore devId */
  47344. devId = origDevId;
  47345. return ret;
  47346. }
  47347. #endif /* WOLF_CRYPTO_CB */
  47348. #ifdef WOLFSSL_CERT_PIV
  47349. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  47350. {
  47351. wc_test_ret_t ret;
  47352. wc_CertPIV piv;
  47353. /* Template for Identiv PIV cert, nonce and signature */
  47354. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  47355. 0x0A, 0x0B,
  47356. 0x53, 0x09, /* NIST PIV Cert */
  47357. 0x70, 0x02, /* Certificate */
  47358. 0x30, 0x00,
  47359. 0x71, 0x01, 0x05, /* Cert Info */
  47360. 0xFE, 0x00, /* Error Detection */
  47361. 0x0B, 0x01, 0x00, /* Nonce */
  47362. 0x0C, 0x01, 0x00, /* Signed Nonce */
  47363. };
  47364. /* PIV certificate data including certificate, info and error detection. */
  47365. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  47366. 0x53, 0x09, /* NIST PIV Cert */
  47367. 0x70, 0x02, /* Certificate */
  47368. 0x30, 0x00,
  47369. 0x71, 0x01, 0x04, /* Cert Info */
  47370. 0xFE, 0x00, /* Error Detection */
  47371. };
  47372. WOLFSSL_ENTER("certpiv_test");
  47373. XMEMSET(&piv, 0, sizeof(piv));
  47374. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  47375. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  47376. if (ret != 0) {
  47377. return WC_TEST_RET_ENC_EC(ret);
  47378. }
  47379. if (!piv.isIdentiv) {
  47380. return WC_TEST_RET_ENC_NC;
  47381. }
  47382. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  47383. return WC_TEST_RET_ENC_NC;
  47384. }
  47385. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  47386. return WC_TEST_RET_ENC_NC;
  47387. }
  47388. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  47389. return WC_TEST_RET_ENC_NC;
  47390. }
  47391. if (!piv.isX509) {
  47392. return WC_TEST_RET_ENC_NC;
  47393. }
  47394. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  47395. return WC_TEST_RET_ENC_NC;
  47396. }
  47397. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  47398. return WC_TEST_RET_ENC_NC;
  47399. }
  47400. XMEMSET(&piv, 0, sizeof(piv));
  47401. /* Test with NIST PIV format */
  47402. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  47403. if (ret != 0) {
  47404. return WC_TEST_RET_ENC_EC(ret);
  47405. }
  47406. if (piv.isIdentiv) {
  47407. return WC_TEST_RET_ENC_NC;
  47408. }
  47409. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  47410. return WC_TEST_RET_ENC_NC;
  47411. }
  47412. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  47413. return WC_TEST_RET_ENC_NC;
  47414. }
  47415. if ((piv.compression != 0)) {
  47416. return WC_TEST_RET_ENC_NC;
  47417. }
  47418. if (!piv.isX509) {
  47419. return WC_TEST_RET_ENC_NC;
  47420. }
  47421. return ret;
  47422. }
  47423. #endif /* WOLFSSL_CERT_PIV */
  47424. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  47425. static time_t time_cb(time_t* t)
  47426. {
  47427. if (t != NULL) {
  47428. *t = 99;
  47429. }
  47430. return 99;
  47431. }
  47432. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  47433. {
  47434. time_t t;
  47435. wc_test_ret_t ret;
  47436. WOLFSSL_ENTER("time_test");
  47437. ret = wc_SetTimeCb(time_cb);
  47438. if (ret != 0)
  47439. return WC_TEST_RET_ENC_EC(ret);
  47440. t = wc_Time(NULL);
  47441. if (t != 99)
  47442. return WC_TEST_RET_ENC_NC;
  47443. ret = wc_GetTime(&t, sizeof(time_t));
  47444. if (ret != 0)
  47445. return WC_TEST_RET_ENC_EC(ret);
  47446. if (t != 99)
  47447. return WC_TEST_RET_ENC_NC;
  47448. ret = wc_SetTimeCb(NULL);
  47449. if (ret != 0)
  47450. return WC_TEST_RET_ENC_EC(ret);
  47451. return 0;
  47452. }
  47453. #endif
  47454. #ifdef WOLFSSL_AES_SIV
  47455. typedef struct {
  47456. const byte key[33];
  47457. word32 keySz;
  47458. const byte nonce[49];
  47459. word32 nonceSz;
  47460. const byte assoc[81];
  47461. word32 assocSz;
  47462. const byte plaintext[83];
  47463. word32 plaintextSz;
  47464. const byte siv[AES_BLOCK_SIZE+1];
  47465. const byte ciphertext[82];
  47466. word32 ciphertextSz;
  47467. } AesSivTestVector;
  47468. #define AES_SIV_TEST_VECTORS 7
  47469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  47470. {
  47471. /* These test vectors come from chrony 4.1's SIV unit tests. */
  47472. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  47473. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47474. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47475. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47476. "", 0,
  47477. "", 0,
  47478. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  47479. "", 0
  47480. },
  47481. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47482. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47483. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47484. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47485. "", 0,
  47486. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  47487. "", 0
  47488. },
  47489. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47490. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47491. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47492. "", 0,
  47493. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47494. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  47495. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  47496. },
  47497. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47498. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47499. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  47500. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  47501. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  47502. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  47503. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  47504. },
  47505. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47506. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47507. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47508. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47509. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  47510. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  47511. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  47512. },
  47513. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47514. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47515. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  47516. "\xd5", 17,
  47517. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  47518. "\xa0", 17,
  47519. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  47520. "\x08", 17,
  47521. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  47522. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  47523. "\xbd", 17
  47524. },
  47525. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47526. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47527. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  47528. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  47529. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  47530. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  47531. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  47532. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  47533. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  47534. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  47535. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  47536. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  47537. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  47538. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  47539. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  47540. "\x31", 81,
  47541. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  47542. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  47543. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  47544. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  47545. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  47546. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  47547. "\xa1", 81
  47548. }};
  47549. int i;
  47550. byte computedCiphertext[82];
  47551. byte computedPlaintext[82];
  47552. byte siv[AES_BLOCK_SIZE];
  47553. wc_test_ret_t ret = 0;
  47554. WOLFSSL_ENTER("aes_siv_test");
  47555. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  47556. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  47557. testVectors[i].assoc, testVectors[i].assocSz,
  47558. testVectors[i].nonce, testVectors[i].nonceSz,
  47559. testVectors[i].plaintext,
  47560. testVectors[i].plaintextSz, siv,
  47561. computedCiphertext);
  47562. if (ret != 0) {
  47563. return WC_TEST_RET_ENC_EC(ret);
  47564. }
  47565. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  47566. if (ret != 0) {
  47567. return WC_TEST_RET_ENC_NC;
  47568. }
  47569. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  47570. testVectors[i].ciphertextSz);
  47571. if (ret != 0) {
  47572. return WC_TEST_RET_ENC_NC;
  47573. }
  47574. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  47575. testVectors[i].assoc, testVectors[i].assocSz,
  47576. testVectors[i].nonce, testVectors[i].nonceSz,
  47577. computedCiphertext, testVectors[i].plaintextSz,
  47578. siv, computedPlaintext);
  47579. if (ret != 0) {
  47580. return WC_TEST_RET_ENC_EC(ret);
  47581. }
  47582. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  47583. testVectors[i].plaintextSz);
  47584. if (ret != 0) {
  47585. return WC_TEST_RET_ENC_NC;
  47586. }
  47587. }
  47588. return 0;
  47589. }
  47590. #endif
  47591. #undef ERROR_OUT
  47592. static const int fiducial4 = WC_TEST_RET_LN;
  47593. /* print the fiducial line numbers assigned above, allowing confirmation of
  47594. * source code version match when in doubt.
  47595. */
  47596. static void print_fiducials(void) {
  47597. printf(" [fiducial line numbers: %d %d %d %d]\n",
  47598. fiducial1, fiducial2, fiducial3, fiducial4);
  47599. }
  47600. #else
  47601. #ifndef NO_MAIN_DRIVER
  47602. int main(void) { return 0; }
  47603. #endif
  47604. #endif /* NO_CRYPT_TEST */