test.c 1.7 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714
  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/rc2.h>
  242. #include <wolfssl/wolfcrypt/arc4.h>
  243. #if !defined(WC_NO_RNG)
  244. #include <wolfssl/wolfcrypt/random.h>
  245. #endif
  246. #include <wolfssl/wolfcrypt/wolfmath.h>
  247. #include <wolfssl/wolfcrypt/coding.h>
  248. #include <wolfssl/wolfcrypt/signature.h>
  249. #include <wolfssl/wolfcrypt/rsa.h>
  250. #include <wolfssl/wolfcrypt/des3.h>
  251. #include <wolfssl/wolfcrypt/aes.h>
  252. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #include <wolfssl/wolfcrypt/siphash.h>
  255. #include <wolfssl/wolfcrypt/poly1305.h>
  256. #include <wolfssl/wolfcrypt/camellia.h>
  257. #include <wolfssl/wolfcrypt/hmac.h>
  258. #include <wolfssl/wolfcrypt/kdf.h>
  259. #include <wolfssl/wolfcrypt/dh.h>
  260. #include <wolfssl/wolfcrypt/dsa.h>
  261. #include <wolfssl/wolfcrypt/srp.h>
  262. #include <wolfssl/wolfcrypt/chacha.h>
  263. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  264. #include <wolfssl/wolfcrypt/pwdbased.h>
  265. #include <wolfssl/wolfcrypt/ripemd.h>
  266. #include <wolfssl/wolfcrypt/error-crypt.h>
  267. #ifdef HAVE_ECC
  268. #include <wolfssl/wolfcrypt/ecc.h>
  269. #endif
  270. #ifdef WOLFSSL_SM2
  271. #include <wolfssl/wolfcrypt/sm2.h>
  272. #endif
  273. #ifdef HAVE_HPKE
  274. #include <wolfssl/wolfcrypt/hpke.h>
  275. #endif
  276. #ifdef HAVE_CURVE25519
  277. #include <wolfssl/wolfcrypt/curve25519.h>
  278. #endif
  279. #ifdef HAVE_ED25519
  280. #include <wolfssl/wolfcrypt/ed25519.h>
  281. #endif
  282. #ifdef HAVE_CURVE448
  283. #include <wolfssl/wolfcrypt/curve448.h>
  284. #endif
  285. #ifdef HAVE_ED448
  286. #include <wolfssl/wolfcrypt/ed448.h>
  287. #endif
  288. #ifdef WOLFSSL_HAVE_KYBER
  289. #include <wolfssl/wolfcrypt/kyber.h>
  290. #ifdef WOLFSSL_WC_KYBER
  291. #include <wolfssl/wolfcrypt/wc_kyber.h>
  292. #endif
  293. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  294. #include <wolfssl/wolfcrypt/ext_kyber.h>
  295. #endif
  296. #endif
  297. #if defined(WOLFSSL_HAVE_XMSS)
  298. #include <wolfssl/wolfcrypt/xmss.h>
  299. #ifdef HAVE_LIBXMSS
  300. #include <wolfssl/wolfcrypt/ext_xmss.h>
  301. #else
  302. #include <wolfssl/wolfcrypt/wc_xmss.h>
  303. #endif
  304. #endif
  305. #if defined(WOLFSSL_HAVE_LMS)
  306. #include <wolfssl/wolfcrypt/lms.h>
  307. #ifdef HAVE_LIBLMS
  308. #include <wolfssl/wolfcrypt/ext_lms.h>
  309. #else
  310. #include <wolfssl/wolfcrypt/wc_lms.h>
  311. #endif
  312. #endif
  313. #ifdef WOLFCRYPT_HAVE_ECCSI
  314. #include <wolfssl/wolfcrypt/eccsi.h>
  315. #endif
  316. #ifdef WOLFCRYPT_HAVE_SAKKE
  317. #include <wolfssl/wolfcrypt/sakke.h>
  318. #endif
  319. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  320. #include <wolfssl/wolfcrypt/blake2.h>
  321. #endif
  322. #ifdef WOLFSSL_SHA3
  323. #include <wolfssl/wolfcrypt/sha3.h>
  324. #endif
  325. #ifdef WOLFSSL_SM3
  326. #include <wolfssl/wolfcrypt/sm3.h>
  327. #endif
  328. #ifdef WOLFSSL_SM4
  329. #include <wolfssl/wolfcrypt/sm4.h>
  330. #endif
  331. #ifdef HAVE_LIBZ
  332. #include <wolfssl/wolfcrypt/compress.h>
  333. #endif
  334. #ifdef HAVE_PKCS7
  335. #include <wolfssl/wolfcrypt/pkcs7.h>
  336. #endif
  337. #ifdef HAVE_FIPS
  338. #include <wolfssl/wolfcrypt/fips_test.h>
  339. #endif
  340. #ifdef HAVE_SELFTEST
  341. #include <wolfssl/wolfcrypt/selftest.h>
  342. #endif
  343. #ifdef WOLFSSL_ASYNC_CRYPT
  344. #include <wolfssl/wolfcrypt/async.h>
  345. #endif
  346. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  347. #include <wolfssl/wolfcrypt/logging.h>
  348. #endif
  349. #ifdef WOLFSSL_CAAM
  350. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  351. #endif
  352. #ifdef WOLF_CRYPTO_CB
  353. #include <wolfssl/wolfcrypt/cryptocb.h>
  354. #ifdef HAVE_INTEL_QA_SYNC
  355. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  356. #endif
  357. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  358. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  359. #endif
  360. #ifdef HAVE_RENESAS_SYNC
  361. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  362. #endif
  363. #endif
  364. #ifdef _MSC_VER
  365. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  366. #pragma warning(disable: 4996)
  367. #endif
  368. #ifdef OPENSSL_EXTRA
  369. #ifndef WOLFCRYPT_ONLY
  370. #include <wolfssl/openssl/evp.h>
  371. #include <wolfssl/openssl/hmac.h>
  372. #endif
  373. #include <wolfssl/openssl/rand.h>
  374. #include <wolfssl/openssl/aes.h>
  375. #include <wolfssl/openssl/des.h>
  376. #endif
  377. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  378. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  379. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  380. #define USE_CERT_BUFFERS_2048
  381. #endif
  382. #if !defined(USE_CERT_BUFFERS_256)
  383. #define USE_CERT_BUFFERS_256
  384. #endif
  385. #endif
  386. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  387. #define ENABLE_ECC384_CERT_GEN_TEST
  388. #endif
  389. #include <wolfssl/certs_test.h>
  390. #ifdef DEVKITPRO
  391. #include <wiiuse/wpad.h>
  392. #endif
  393. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  394. /* FIPS build has replaced ecc.h. */
  395. #define wc_ecc_key_get_priv(key) (&((key)->k))
  396. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  397. #endif
  398. #ifdef WOLFSSL_STATIC_MEMORY
  399. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  400. #else
  401. #define HEAP_HINT NULL
  402. #endif /* WOLFSSL_STATIC_MEMORY */
  403. /* these cases do not have intermediate hashing support */
  404. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  405. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  406. #define NO_INTM_HASH_TEST
  407. #endif
  408. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  409. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  410. defined(WOLFSSL_SECO_CAAM)
  411. #define HASH_SIZE_LIMIT
  412. #endif
  413. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  414. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  415. static void initDefaultName(void);
  416. #endif
  417. /* for async devices */
  418. #ifdef WOLFSSL_CAAM_DEVID
  419. static int devId = WOLFSSL_CAAM_DEVID;
  420. #else
  421. #ifdef WC_USE_DEVID
  422. static int devId = WC_USE_DEVID;
  423. #else
  424. static int devId = INVALID_DEVID;
  425. #endif
  426. #endif
  427. #ifdef HAVE_WNR
  428. const char* wnrConfigFile = "wnr-example.conf";
  429. #endif
  430. #define TEST_STRING "Everyone gets Friday off."
  431. #define TEST_STRING_SZ 25
  432. typedef struct testVector {
  433. const char* input;
  434. const char* output;
  435. size_t inLen;
  436. size_t outLen;
  437. } testVector;
  438. #ifndef WOLFSSL_TEST_SUBROUTINE
  439. #define WOLFSSL_TEST_SUBROUTINE
  440. #endif
  441. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  442. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  443. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  444. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  445. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  446. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  449. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  452. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  453. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  454. #if !defined(WOLFSSL_NOSHA512_224) && \
  455. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  456. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  457. #endif
  458. #if !defined(WOLFSSL_NOSHA512_256) && \
  459. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  461. #endif
  462. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  464. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  465. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  470. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  472. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  473. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  474. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  475. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  476. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  477. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  478. defined(WOLFSSL_AFALG_XILINX_RSA)
  479. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  480. static wc_test_ret_t hkdf_test(void);
  481. #else
  482. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  483. #endif
  484. #endif
  485. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  486. #ifdef WOLFSSL_TLS13
  487. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  488. #endif
  489. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  490. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  491. #ifdef WC_SRTP_KDF
  492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void);
  493. #endif
  494. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  495. #ifdef WC_RC2
  496. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  497. #endif
  498. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  499. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  500. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  501. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  502. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  503. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  505. #if defined(WOLFSSL_AES_CFB)
  506. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void);
  507. #endif
  508. #ifdef WOLFSSL_AES_XTS
  509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void);
  510. #endif
  511. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  512. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  514. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  515. #if defined(WOLFSSL_SIPHASH)
  516. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  517. #endif
  518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  519. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  521. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  522. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  524. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  525. #ifdef WOLFSSL_SM4
  526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  527. #endif
  528. #ifdef WC_RSA_NO_PADDING
  529. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  530. #endif
  531. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  535. #ifndef WC_NO_RNG
  536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  537. #endif /* WC_NO_RNG */
  538. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  540. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  541. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  542. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  544. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  545. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  546. #endif
  547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  548. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  549. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  551. #endif
  552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  553. #ifdef HAVE_ECC
  554. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  555. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  556. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  557. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  558. #endif
  559. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  560. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  561. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  562. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  563. /* skip for ATECC508/608A, cannot import private key buffers */
  564. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  565. #endif
  566. #endif
  567. #ifdef HAVE_CURVE25519
  568. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  569. #endif
  570. #ifdef HAVE_ED25519
  571. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  572. #endif
  573. #ifdef HAVE_CURVE448
  574. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  575. #endif
  576. #ifdef HAVE_ED448
  577. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  578. #endif
  579. #ifdef WOLFSSL_HAVE_KYBER
  580. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  581. #endif
  582. #if defined(WOLFSSL_HAVE_XMSS)
  583. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  584. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void);
  585. #endif
  586. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  587. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void);
  588. #endif
  589. #endif
  590. #if defined(WOLFSSL_HAVE_LMS)
  591. #if !defined(WOLFSSL_SMALL_STACK)
  592. #if defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)
  593. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void);
  594. #endif
  595. #endif
  596. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  597. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void);
  598. #endif
  599. #endif
  600. #ifdef WOLFCRYPT_HAVE_ECCSI
  601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  602. #endif
  603. #ifdef WOLFCRYPT_HAVE_SAKKE
  604. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  605. #endif
  606. #ifdef HAVE_BLAKE2
  607. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  608. #endif
  609. #ifdef HAVE_BLAKE2S
  610. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  611. #endif
  612. #ifdef HAVE_LIBZ
  613. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  614. #endif
  615. #ifdef HAVE_PKCS7
  616. #ifndef NO_PKCS7_ENCRYPTED_DATA
  617. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  618. #endif
  619. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  620. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  621. #endif
  622. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  623. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  624. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  625. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  626. #endif
  627. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  628. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  629. word32 keySz);
  630. #endif
  631. #endif
  632. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  633. !defined(NO_FILESYSTEM)
  634. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  635. #endif
  636. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  637. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  638. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  639. #endif
  640. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  641. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  642. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  643. #endif
  644. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  645. #if defined(WOLFSSL_PUBLIC_MP) && \
  646. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  647. defined(USE_FAST_MATH))
  648. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  649. #endif
  650. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  651. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  652. #endif
  653. #if defined(ASN_BER_TO_DER) && \
  654. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  655. defined(OPENSSL_EXTRA_X509_SMALL))
  656. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  657. #endif
  658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  659. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  660. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  661. #endif
  662. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  663. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  664. #else
  665. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  666. #endif
  667. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  668. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  669. #endif
  670. #ifdef WOLFSSL_CAAM_BLOB
  671. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  672. #endif
  673. #ifdef HAVE_ARIA
  674. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  675. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  676. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  677. #endif
  678. #ifdef WOLF_CRYPTO_CB
  679. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  680. #endif
  681. #ifdef WOLFSSL_CERT_PIV
  682. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  683. #endif
  684. #ifdef WOLFSSL_AES_SIV
  685. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  686. #endif
  687. #if defined(WOLFSSL_AES_EAX) && \
  688. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  689. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void);
  690. #endif /* WOLFSSL_AES_EAX */
  691. /* General big buffer size for many tests. */
  692. #define FOURK_BUF 4096
  693. /* If not defined in user_settings, the ERROR_OUT pause is 120 seconds. */
  694. #ifndef WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION
  695. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION 120
  696. #endif
  697. #if defined(WOLFSSL_ESPIDF_ERROR_PAUSE)
  698. #if defined(CONFIG_FREERTOS_HZ)
  699. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  700. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * CONFIG_FREERTOS_HZ)
  701. #else
  702. /* If not defined, assume RTOS is 1000 ticks per second. */
  703. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  704. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * 1000)
  705. #endif
  706. /* When defined, pause at error condition rather than exit with error. */
  707. #define ERROR_OUT(err, eLabel) \
  708. do { \
  709. ret = (err); \
  710. ESP_LOGE(ESPIDF_TAG, "Failed: Error = %d during %s, line %d", \
  711. err, __FUNCTION__, __LINE__); \
  712. ESP_LOGI(ESPIDF_TAG, "Extended system info:"); \
  713. esp_ShowExtendedSystemInfo(); \
  714. ESP_LOGW(ESPIDF_TAG, "Paused for %d seconds! " \
  715. "WOLFSSL_ESPIDF_ERROR_PAUSE is enabled.", \
  716. WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION); \
  717. vTaskDelay(WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS); \
  718. goto eLabel; \
  719. } while (0)
  720. #else
  721. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  722. #endif
  723. /* Not all unexpected conditions are actually errors .*/
  724. #define WARNING_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  725. static void render_error_message(const char* msg, wc_test_ret_t es)
  726. {
  727. (void)msg;
  728. (void)es;
  729. #ifdef WOLFSSL_LINUXKM
  730. #define err_sys_printf lkm_printf
  731. #else
  732. #define err_sys_printf printf
  733. #endif
  734. switch (WC_TEST_RET_DEC_TAG(es)) {
  735. case WC_TEST_RET_TAG_NC:
  736. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  737. break;
  738. case WC_TEST_RET_TAG_EC:
  739. #ifdef NO_ERROR_STRINGS
  740. err_sys_printf("%s error L=%d code=%d\n", msg,
  741. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  742. #else
  743. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  744. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  745. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  746. #endif
  747. break;
  748. case WC_TEST_RET_TAG_ERRNO:
  749. {
  750. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  751. * flavor that always returns a non-null char pointer that must be used
  752. * directly, and a POSIX flavor that returns an error int, and iff success,
  753. * stores an error string in the supplied buffer. this is all most
  754. * infelicitous...
  755. */
  756. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  757. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  758. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  759. (defined(__USE_XOPEN2K) && \
  760. defined(_POSIX_C_SOURCE) && \
  761. (_POSIX_C_SOURCE >= 200112L)))
  762. char errno_buf[64], *errno_string;
  763. /* precisely mirror the gate used in glibc string.h */
  764. #if defined __USE_XOPEN2K && !defined __USE_GNU
  765. if (strerror_r(WC_TEST_RET_DEC_I(es),
  766. errno_buf, sizeof(errno_buf)) != 0)
  767. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  768. errno_string = errno_buf;
  769. #else
  770. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  771. errno_buf, sizeof(errno_buf));
  772. #endif
  773. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  774. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  775. errno_string);
  776. #else /* can't figure out how to strerror_r(), or don't want error strings */
  777. err_sys_printf("%s error L=%d errno=%d\n", msg,
  778. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  779. #endif
  780. break;
  781. }
  782. case WC_TEST_RET_TAG_I:
  783. err_sys_printf("%s error L=%d i=%d\n", msg,
  784. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  785. break;
  786. }
  787. #undef err_sys_printf
  788. }
  789. static void print_fiducials(void);
  790. #ifdef HAVE_STACK_SIZE
  791. static THREAD_RETURN err_sys(const char* msg, int es)
  792. #else
  793. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  794. #endif
  795. {
  796. render_error_message(msg, es);
  797. print_fiducials();
  798. #ifdef WOLFSSL_LINUXKM
  799. EXIT_TEST(es);
  800. #else
  801. EXIT_TEST(-1);
  802. #endif
  803. }
  804. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  805. /* func_args from test.h, so don't have to pull in other stuff */
  806. typedef struct func_args {
  807. int argc;
  808. char** argv;
  809. wc_test_ret_t return_code;
  810. } func_args;
  811. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  812. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  813. static void myFipsCb(int ok, int err, const char* hash)
  814. {
  815. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  816. printf("message = %s\n", wc_GetErrorString(err));
  817. printf("hash = %s\n", hash);
  818. if (err == IN_CORE_FIPS_E) {
  819. printf("In core integrity hash check failure, copy above hash\n");
  820. printf("into verifyCore[] in fips_test.c and rebuild\n");
  821. }
  822. }
  823. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  824. #ifdef WOLFSSL_STATIC_MEMORY
  825. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  826. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  827. #elif defined(BENCH_EMBEDDED)
  828. static byte gTestMemory[14000];
  829. #elif defined(WOLFSSL_CERT_EXT)
  830. static byte gTestMemory[140000];
  831. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  832. !defined(ALT_ECC_SIZE)
  833. static byte gTestMemory[160000];
  834. #else
  835. static byte gTestMemory[80000];
  836. #endif
  837. #endif
  838. #ifdef WOLFSSL_PB
  839. static int wolfssl_pb_print(const char* msg, ...)
  840. {
  841. int ret;
  842. va_list args;
  843. char tmpBuf[80];
  844. va_start(args, msg);
  845. ret = vsprint(tmpBuf, msg, args);
  846. va_end(args);
  847. fnDumpStringToSystemLog(tmpBuf);
  848. return ret;
  849. }
  850. #endif /* WOLFSSL_PB */
  851. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  852. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  853. /* Enable support for RNG with crypto callback */
  854. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  855. {
  856. int rc = CRYPTOCB_UNAVAILABLE;
  857. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  858. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  859. }
  860. (void)ctx;
  861. (void)thisDevId;
  862. return rc;
  863. }
  864. #endif
  865. /* optional macro to add sleep between tests */
  866. #ifndef TEST_SLEEP
  867. #define TEST_SLEEP() WC_DO_NOTHING
  868. #else
  869. #define TEST_PASS test_pass
  870. #include <stdarg.h> /* for var args */
  871. static WC_INLINE void test_pass(const char* fmt, ...)
  872. {
  873. va_list args;
  874. va_start(args, fmt);
  875. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  876. va_end(args);
  877. PRINT_HEAP_CHECKPOINT();
  878. TEST_SLEEP();
  879. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  880. }
  881. #endif
  882. /* set test pass output to printf if not overridden */
  883. #ifndef TEST_PASS
  884. /* redirect to printf */
  885. #define TEST_PASS(...) { \
  886. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  887. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  888. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  889. } \
  890. PRINT_HEAP_CHECKPOINT(); \
  891. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  892. }
  893. #endif
  894. #ifdef TEST_ALWAYS_RUN_TO_END
  895. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  896. #elif !defined(TEST_FAIL)
  897. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  898. #endif
  899. #ifdef HAVE_STACK_SIZE
  900. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  901. #else
  902. wc_test_ret_t wolfcrypt_test(void* args)
  903. #endif
  904. {
  905. wc_test_ret_t ret;
  906. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  907. long heap_baselineAllocs, heap_baselineBytes;
  908. #endif
  909. #ifdef TEST_ALWAYS_RUN_TO_END
  910. int last_failed_test_ret = 0;
  911. #endif
  912. STACK_SIZE_INIT();
  913. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  914. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  915. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  916. (void)wolfCrypt_heap_peakBytes_checkpoint();
  917. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  918. #endif
  919. printf("------------------------------------------------------------------------------\n");
  920. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  921. #ifdef WOLF_CRYPTO_CB
  922. if (devId != INVALID_DEVID)
  923. printf(" CryptoCB with DevID:%X\n", devId);
  924. #endif
  925. printf("------------------------------------------------------------------------------\n");
  926. if (args) {
  927. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  928. int ch;
  929. #endif
  930. ((func_args*)args)->return_code = -1; /* error state */
  931. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  932. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  933. switch(ch) {
  934. case 's':
  935. #ifdef HAVE_STACK_SIZE_VERBOSE
  936. max_relative_stack = (ssize_t)atoi(myoptarg);
  937. break;
  938. #else
  939. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  940. #endif
  941. case 'm':
  942. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  943. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  944. break;
  945. #else
  946. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  947. #endif
  948. case 'a':
  949. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  950. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  951. break;
  952. #else
  953. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  954. #endif
  955. case 'h':
  956. return err_sys("\
  957. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  958. [-a max_relative_heap_allocs] [-h]\n", 0);
  959. default:
  960. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  961. }
  962. }
  963. #endif
  964. }
  965. #ifdef WOLFSSL_STATIC_MEMORY
  966. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  967. WOLFMEM_GENERAL, 1) != 0) {
  968. printf("unable to load static memory.\n");
  969. return(EXIT_FAILURE);
  970. }
  971. #endif
  972. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  973. wolfSSL_Debugging_ON();
  974. #endif
  975. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  976. wc_SetLoggingHeap(HEAP_HINT);
  977. #endif
  978. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  979. wolfCrypt_SetCb_fips(myFipsCb);
  980. #endif
  981. #if !defined(NO_BIG_INT)
  982. if (CheckCtcSettings() != 1) {
  983. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  984. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  985. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  986. }
  987. #if defined(USE_FAST_MATH) && \
  988. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  989. if (CheckFastMathSettings() != 1)
  990. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  991. WC_TEST_RET_ENC_NC);
  992. #endif /* USE_FAST_MATH */
  993. #endif /* !NO_BIG_INT */
  994. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  995. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  996. initDefaultName();
  997. #endif
  998. #ifdef WOLFSSL_ASYNC_CRYPT
  999. ret = wolfAsync_DevOpen(&devId);
  1000. if (ret < 0) {
  1001. printf("Async device open failed\nRunning without async\n");
  1002. }
  1003. #else
  1004. (void)devId;
  1005. #endif /* WOLFSSL_ASYNC_CRYPT */
  1006. #ifdef WOLF_CRYPTO_CB
  1007. #ifdef HAVE_INTEL_QA_SYNC
  1008. devId = wc_CryptoCb_InitIntelQa();
  1009. if (INVALID_DEVID == devId) {
  1010. printf("Couldn't init the Intel QA\n");
  1011. }
  1012. #endif
  1013. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1014. devId = wc_CryptoCb_InitOcteon();
  1015. if (INVALID_DEVID == devId) {
  1016. printf("Couldn't init the Cavium Octeon\n");
  1017. }
  1018. #endif
  1019. #ifdef HAVE_RENESAS_SYNC
  1020. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  1021. if (devId == INVALID_DEVID) {
  1022. printf("Couldn't get the Renesas device ID\n");
  1023. }
  1024. #endif
  1025. #endif
  1026. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  1027. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  1028. if (devId == INVALID_DEVID) {
  1029. /* for testing RNG with crypto callback register function */
  1030. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  1031. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  1032. }
  1033. #endif
  1034. #ifdef HAVE_SELFTEST
  1035. if ( (ret = wolfCrypt_SelfTest()) != 0)
  1036. TEST_FAIL("CAVP selftest failed!\n", ret);
  1037. else
  1038. TEST_PASS("CAVP selftest passed!\n");
  1039. #endif
  1040. if ( (ret = error_test()) != 0)
  1041. TEST_FAIL("error test failed!\n", ret);
  1042. else
  1043. TEST_PASS("error test passed!\n");
  1044. if ( (ret = memory_test()) != 0)
  1045. TEST_FAIL("MEMORY test failed!\n", ret);
  1046. else
  1047. TEST_PASS("MEMORY test passed!\n");
  1048. #ifndef NO_CODING
  1049. if ( (ret = base64_test()) != 0)
  1050. TEST_FAIL("base64 test failed!\n", ret);
  1051. else
  1052. TEST_PASS("base64 test passed!\n");
  1053. #ifdef WOLFSSL_BASE16
  1054. if ( (ret = base16_test()) != 0)
  1055. TEST_FAIL("base16 test failed!\n", ret);
  1056. else
  1057. TEST_PASS("base16 test passed!\n");
  1058. #endif
  1059. #endif /* !NO_CODING */
  1060. #ifndef NO_ASN
  1061. if ( (ret = asn_test()) != 0)
  1062. TEST_FAIL("asn test failed!\n", ret);
  1063. else
  1064. TEST_PASS("asn test passed!\n");
  1065. #endif
  1066. #ifndef WC_NO_RNG
  1067. if ( (ret = random_test()) != 0)
  1068. TEST_FAIL("RANDOM test failed!\n", ret);
  1069. else
  1070. TEST_PASS("RANDOM test passed!\n");
  1071. #endif /* WC_NO_RNG */
  1072. #ifndef NO_MD5
  1073. if ( (ret = md5_test()) != 0)
  1074. TEST_FAIL("MD5 test failed!\n", ret);
  1075. else
  1076. TEST_PASS("MD5 test passed!\n");
  1077. #endif
  1078. #ifdef WOLFSSL_MD2
  1079. if ( (ret = md2_test()) != 0)
  1080. TEST_FAIL("MD2 test failed!\n", ret);
  1081. else
  1082. TEST_PASS("MD2 test passed!\n");
  1083. #endif
  1084. #ifndef NO_MD4
  1085. if ( (ret = md4_test()) != 0)
  1086. TEST_FAIL("MD4 test failed!\n", ret);
  1087. else
  1088. TEST_PASS("MD4 test passed!\n");
  1089. #endif
  1090. #ifndef NO_SHA
  1091. if ( (ret = sha_test()) != 0)
  1092. TEST_FAIL("SHA test failed!\n", ret);
  1093. else
  1094. TEST_PASS("SHA test passed!\n");
  1095. #endif
  1096. #ifdef WOLFSSL_SHA224
  1097. if ( (ret = sha224_test()) != 0)
  1098. TEST_FAIL("SHA-224 test failed!\n", ret);
  1099. else
  1100. TEST_PASS("SHA-224 test passed!\n");
  1101. #endif
  1102. #ifndef NO_SHA256
  1103. if ( (ret = sha256_test()) != 0)
  1104. TEST_FAIL("SHA-256 test failed!\n", ret);
  1105. else
  1106. TEST_PASS("SHA-256 test passed!\n");
  1107. #endif
  1108. #ifdef WOLFSSL_SHA384
  1109. if ( (ret = sha384_test()) != 0)
  1110. TEST_FAIL("SHA-384 test failed!\n", ret);
  1111. else
  1112. TEST_PASS("SHA-384 test passed!\n");
  1113. #endif
  1114. #ifdef WOLFSSL_SHA512
  1115. if ((ret = sha512_test()) != 0) {
  1116. TEST_FAIL("SHA-512 test failed!\n", ret);
  1117. }
  1118. else {
  1119. TEST_PASS("SHA-512 test passed!\n");
  1120. }
  1121. #if !defined(WOLFSSL_NOSHA512_224) && \
  1122. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1123. if ((ret = sha512_224_test()) != 0) {
  1124. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1125. }
  1126. else
  1127. TEST_PASS("SHA-512/224 test passed!\n");
  1128. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1129. #if !defined(WOLFSSL_NOSHA512_256) && \
  1130. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1131. if ((ret = sha512_256_test()) != 0) {
  1132. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1133. }
  1134. else
  1135. TEST_PASS("SHA-512/256 test passed!\n");
  1136. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1137. #endif /* WOLFSSL_SHA512 */
  1138. #ifdef WOLFSSL_SHA3
  1139. if ( (ret = sha3_test()) != 0)
  1140. TEST_FAIL("SHA-3 test failed!\n", ret);
  1141. else
  1142. TEST_PASS("SHA-3 test passed!\n");
  1143. #endif
  1144. #ifdef WOLFSSL_SHAKE128
  1145. if ( (ret = shake128_test()) != 0)
  1146. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1147. else
  1148. TEST_PASS("SHAKE128 test passed!\n");
  1149. #endif
  1150. #ifdef WOLFSSL_SHAKE256
  1151. if ( (ret = shake256_test()) != 0)
  1152. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1153. else
  1154. TEST_PASS("SHAKE256 test passed!\n");
  1155. #endif
  1156. #ifdef WOLFSSL_SM3
  1157. if ( (ret = sm3_test()) != 0)
  1158. return err_sys("SM-3 test failed!\n", ret);
  1159. else
  1160. TEST_PASS("SM-3 test passed!\n");
  1161. #endif
  1162. #ifndef NO_HASH_WRAPPER
  1163. if ( (ret = hash_test()) != 0)
  1164. TEST_FAIL("Hash test failed!\n", ret);
  1165. else
  1166. TEST_PASS("Hash test passed!\n");
  1167. #endif
  1168. #ifdef WOLFSSL_RIPEMD
  1169. if ( (ret = ripemd_test()) != 0)
  1170. TEST_FAIL("RIPEMD test failed!\n", ret);
  1171. else
  1172. TEST_PASS("RIPEMD test passed!\n");
  1173. #endif
  1174. #ifdef HAVE_BLAKE2
  1175. if ( (ret = blake2b_test()) != 0)
  1176. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1177. else
  1178. TEST_PASS("BLAKE2b test passed!\n");
  1179. #endif
  1180. #ifdef HAVE_BLAKE2S
  1181. if ( (ret = blake2s_test()) != 0)
  1182. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1183. else
  1184. TEST_PASS("BLAKE2s test passed!\n");
  1185. #endif
  1186. #ifndef NO_HMAC
  1187. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1188. && (HAVE_FIPS_VERSION >= 5))
  1189. if ( (ret = hmac_md5_test()) != 0)
  1190. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1191. else
  1192. TEST_PASS("HMAC-MD5 test passed!\n");
  1193. #endif
  1194. #ifndef NO_SHA
  1195. if ( (ret = hmac_sha_test()) != 0)
  1196. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1197. else
  1198. TEST_PASS("HMAC-SHA test passed!\n");
  1199. #endif
  1200. #ifdef WOLFSSL_SHA224
  1201. if ( (ret = hmac_sha224_test()) != 0)
  1202. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1203. else
  1204. TEST_PASS("HMAC-SHA224 test passed!\n");
  1205. #endif
  1206. #ifndef NO_SHA256
  1207. if ( (ret = hmac_sha256_test()) != 0)
  1208. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1209. else
  1210. TEST_PASS("HMAC-SHA256 test passed!\n");
  1211. #endif
  1212. #ifdef WOLFSSL_SHA384
  1213. if ( (ret = hmac_sha384_test()) != 0)
  1214. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1215. else
  1216. TEST_PASS("HMAC-SHA384 test passed!\n");
  1217. #endif
  1218. #ifdef WOLFSSL_SHA512
  1219. if ( (ret = hmac_sha512_test()) != 0)
  1220. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1221. else
  1222. TEST_PASS("HMAC-SHA512 test passed!\n");
  1223. #endif
  1224. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1225. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1226. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1227. if ( (ret = hmac_sha3_test()) != 0)
  1228. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1229. else
  1230. TEST_PASS("HMAC-SHA3 test passed!\n");
  1231. #endif
  1232. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1233. PRIVATE_KEY_UNLOCK();
  1234. if ( (ret = hkdf_test()) != 0)
  1235. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1236. else
  1237. TEST_PASS("HMAC-KDF test passed!\n");
  1238. PRIVATE_KEY_LOCK();
  1239. #endif
  1240. #endif /* !NO_HMAC */
  1241. #ifdef WOLFSSL_WOLFSSH
  1242. PRIVATE_KEY_UNLOCK();
  1243. if ( (ret = sshkdf_test()) != 0)
  1244. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1245. else
  1246. TEST_PASS("SSH-KDF test passed!\n");
  1247. PRIVATE_KEY_LOCK();
  1248. #endif /* WOLFSSL_WOLFSSH */
  1249. #ifdef WOLFSSL_TLS13
  1250. PRIVATE_KEY_UNLOCK();
  1251. if ( (ret = tls13_kdf_test()) != 0)
  1252. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1253. else
  1254. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1255. PRIVATE_KEY_LOCK();
  1256. #endif /* WOLFSSL_TLS13 */
  1257. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1258. if ( (ret = x963kdf_test()) != 0)
  1259. TEST_FAIL("X963-KDF test failed!\n", ret);
  1260. else
  1261. TEST_PASS("X963-KDF test passed!\n");
  1262. #endif
  1263. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1264. if ( (ret = hpke_test()) != 0)
  1265. TEST_FAIL("HPKE test failed!\n", ret);
  1266. else
  1267. TEST_PASS("HPKE test passed!\n");
  1268. #endif
  1269. #if defined(WC_SRTP_KDF)
  1270. if ( (ret = srtpkdf_test()) != 0)
  1271. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1272. else
  1273. TEST_PASS("SRTP KDF test passed!\n");
  1274. #endif
  1275. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1276. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1277. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1278. if ( (ret = gmac_test()) != 0)
  1279. TEST_FAIL("GMAC test failed!\n", ret);
  1280. else
  1281. TEST_PASS("GMAC test passed!\n");
  1282. #endif
  1283. #ifdef WC_RC2
  1284. if ( (ret = rc2_test()) != 0)
  1285. TEST_FAIL("RC2 test failed!\n", ret);
  1286. else
  1287. TEST_PASS("RC2 test passed!\n");
  1288. #endif
  1289. #ifndef NO_RC4
  1290. if ( (ret = arc4_test()) != 0)
  1291. TEST_FAIL("ARC4 test failed!\n", ret);
  1292. else
  1293. TEST_PASS("ARC4 test passed!\n");
  1294. #endif
  1295. #ifdef HAVE_CHACHA
  1296. if ( (ret = chacha_test()) != 0)
  1297. TEST_FAIL("Chacha test failed!\n", ret);
  1298. else
  1299. TEST_PASS("Chacha test passed!\n");
  1300. #endif
  1301. #ifdef HAVE_XCHACHA
  1302. if ( (ret = XChaCha_test()) != 0)
  1303. TEST_FAIL("XChacha test failed!\n", ret);
  1304. else
  1305. TEST_PASS("XChacha test passed!\n");
  1306. #endif
  1307. #ifdef HAVE_POLY1305
  1308. if ( (ret = poly1305_test()) != 0)
  1309. TEST_FAIL("POLY1305 test failed!\n", ret);
  1310. else
  1311. TEST_PASS("POLY1305 test passed!\n");
  1312. #endif
  1313. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1314. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1315. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1316. else
  1317. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1318. #endif
  1319. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1320. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1321. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1322. else
  1323. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1324. #endif
  1325. #ifndef NO_DES3
  1326. if ( (ret = des_test()) != 0)
  1327. TEST_FAIL("DES test failed!\n", ret);
  1328. else
  1329. TEST_PASS("DES test passed!\n");
  1330. #endif
  1331. #ifndef NO_DES3
  1332. if ( (ret = des3_test()) != 0)
  1333. TEST_FAIL("DES3 test failed!\n", ret);
  1334. else
  1335. TEST_PASS("DES3 test passed!\n");
  1336. #endif
  1337. #ifndef NO_AES
  1338. if ( (ret = aes_test()) != 0)
  1339. TEST_FAIL("AES test failed!\n", ret);
  1340. else
  1341. TEST_PASS("AES test passed!\n");
  1342. #if defined(WOLFSSL_AES_192) && \
  1343. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1344. if ( (ret = aes192_test()) != 0)
  1345. TEST_FAIL("AES192 test failed!\n", ret);
  1346. else
  1347. TEST_PASS("AES192 test passed!\n");
  1348. #endif
  1349. #if defined(WOLFSSL_AES_256)
  1350. if ( (ret = aes256_test()) != 0)
  1351. TEST_FAIL("AES256 test failed!\n", ret);
  1352. else
  1353. TEST_PASS("AES256 test passed!\n");
  1354. #endif
  1355. #ifdef WOLFSSL_AES_OFB
  1356. if ( (ret = aesofb_test()) != 0)
  1357. TEST_FAIL("AES-OFB test failed!\n", ret);
  1358. else
  1359. TEST_PASS("AES-OFB test passed!\n");
  1360. #endif
  1361. #ifdef HAVE_AESGCM
  1362. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1363. if ( (ret = aesgcm_test()) != 0)
  1364. TEST_FAIL("AES-GCM test failed!\n", ret);
  1365. #endif
  1366. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1367. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1368. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1369. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1370. if ((ret = aesgcm_default_test()) != 0) {
  1371. TEST_FAIL("AES-GCM test failed!\n", ret);
  1372. }
  1373. #endif
  1374. if (ret == 0) {
  1375. TEST_PASS("AES-GCM test passed!\n");
  1376. }
  1377. #endif
  1378. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1379. if ( (ret = aesccm_test()) != 0)
  1380. TEST_FAIL("AES-CCM test failed!\n", ret);
  1381. else
  1382. TEST_PASS("AES-CCM test passed!\n");
  1383. #endif
  1384. #ifdef WOLFSSL_AES_CFB
  1385. if ( (ret = aes_cfb_test()) != 0)
  1386. TEST_FAIL("AES-CFB test failed!\n", ret);
  1387. else
  1388. TEST_PASS("AES-CFB test passed!\n");
  1389. #endif
  1390. #ifdef WOLFSSL_AES_XTS
  1391. if ( (ret = aes_xts_test()) != 0)
  1392. TEST_FAIL("AES-XTS test failed!\n", ret);
  1393. else
  1394. TEST_PASS("AES-XTS test passed!\n");
  1395. #endif
  1396. #ifdef HAVE_AES_KEYWRAP
  1397. if ( (ret = aeskeywrap_test()) != 0)
  1398. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1399. else
  1400. TEST_PASS("AES Key Wrap test passed!\n");
  1401. #endif
  1402. #ifdef WOLFSSL_AES_SIV
  1403. if ( (ret = aes_siv_test()) != 0)
  1404. TEST_FAIL("AES-SIV test failed!\n", ret);
  1405. else
  1406. TEST_PASS("AES-SIV test passed!\n");
  1407. #endif
  1408. #endif
  1409. #if defined(WOLFSSL_AES_EAX) && \
  1410. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1411. if ( (ret = aes_eax_test()) != 0)
  1412. TEST_FAIL("AES-EAX test failed!\n", ret);
  1413. else
  1414. TEST_PASS("AES-EAX test passed!\n");
  1415. #endif /* WOLFSSL_AES_EAX */
  1416. #ifdef HAVE_ARIA
  1417. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1418. TEST_FAIL("ARIA128 test failed!\n", ret);
  1419. else
  1420. TEST_PASS("ARIA128 test passed!\n");
  1421. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1422. TEST_FAIL("ARIA192 test failed!\n", ret);
  1423. else
  1424. TEST_PASS("ARIA192 test passed!\n");
  1425. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1426. TEST_FAIL("ARIA256 test failed!\n", ret);
  1427. else
  1428. TEST_PASS("ARIA256 test passed!\n");
  1429. #endif
  1430. #ifdef HAVE_CAMELLIA
  1431. if ( (ret = camellia_test()) != 0)
  1432. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1433. else
  1434. TEST_PASS("CAMELLIA test passed!\n");
  1435. #endif
  1436. #ifdef WOLFSSL_SM4
  1437. if ( (ret = sm4_test()) != 0)
  1438. return err_sys("SM-4 test failed!\n", ret);
  1439. else
  1440. TEST_PASS("SM-4 test passed!\n");
  1441. #endif
  1442. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1443. #ifdef WC_RSA_NO_PADDING
  1444. if ( (ret = rsa_no_pad_test()) != 0)
  1445. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1446. else
  1447. TEST_PASS("RSA NOPAD test passed!\n");
  1448. #endif
  1449. if ( (ret = rsa_test()) != 0)
  1450. TEST_FAIL("RSA test failed!\n", ret);
  1451. else
  1452. TEST_PASS("RSA test passed!\n");
  1453. #endif
  1454. #ifndef NO_DH
  1455. PRIVATE_KEY_UNLOCK();
  1456. if ( (ret = dh_test()) != 0)
  1457. TEST_FAIL("DH test failed!\n", ret);
  1458. else
  1459. TEST_PASS("DH test passed!\n");
  1460. PRIVATE_KEY_LOCK();
  1461. #endif
  1462. #ifndef NO_DSA
  1463. if ( (ret = dsa_test()) != 0)
  1464. TEST_FAIL("DSA test failed!\n", ret);
  1465. else
  1466. TEST_PASS("DSA test passed!\n");
  1467. #endif
  1468. #ifdef WOLFCRYPT_HAVE_SRP
  1469. if ( (ret = srp_test()) != 0)
  1470. TEST_FAIL("SRP test failed!\n", ret);
  1471. else
  1472. TEST_PASS("SRP test passed!\n");
  1473. #endif
  1474. #ifndef NO_PWDBASED
  1475. if ( (ret = pwdbased_test()) != 0)
  1476. TEST_FAIL("PWDBASED test failed!\n", ret);
  1477. else
  1478. TEST_PASS("PWDBASED test passed!\n");
  1479. #endif
  1480. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1481. if ( (ret = openssl_test()) != 0)
  1482. TEST_FAIL("OPENSSL test failed!\n", ret);
  1483. else
  1484. TEST_PASS("OPENSSL test passed!\n");
  1485. if ( (ret = openSSL_evpMD_test()) != 0)
  1486. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1487. else
  1488. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1489. if ( (ret = openssl_pkey0_test()) != 0)
  1490. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1491. else
  1492. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1493. if ( (ret = openssl_pkey1_test()) != 0)
  1494. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1495. else
  1496. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1497. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1498. if ( (ret = openssl_evpSig_test()) != 0)
  1499. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1500. else
  1501. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1502. #endif
  1503. #endif
  1504. #if defined(HAVE_ECC)
  1505. PRIVATE_KEY_UNLOCK();
  1506. if ( (ret = ecc_test()) != 0)
  1507. TEST_FAIL("ECC test failed!\n", ret);
  1508. else
  1509. TEST_PASS("ECC test passed!\n");
  1510. PRIVATE_KEY_LOCK();
  1511. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1512. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1513. if ( (ret = ecc_encrypt_test()) != 0)
  1514. TEST_FAIL("ECC Enc test failed!\n", ret);
  1515. else
  1516. TEST_PASS("ECC Enc test passed!\n");
  1517. #endif
  1518. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1519. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1520. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1521. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1522. /* skip for ATECC508/608A, cannot import private key buffers */
  1523. if ( (ret = ecc_test_buffers()) != 0)
  1524. TEST_FAIL("ECC buffer test failed!\n", ret);
  1525. else
  1526. TEST_PASS("ECC buffer test passed!\n");
  1527. #endif
  1528. #endif
  1529. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1530. !defined(NO_FILESYSTEM)
  1531. if ( (ret = cert_test()) != 0)
  1532. TEST_FAIL("CERT test failed!\n", ret);
  1533. else
  1534. TEST_PASS("CERT test passed!\n");
  1535. #endif
  1536. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1537. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1538. if ( (ret = certext_test()) != 0)
  1539. TEST_FAIL("CERT EXT test failed!\n", ret);
  1540. else
  1541. TEST_PASS("CERT EXT test passed!\n");
  1542. #endif
  1543. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1544. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1545. if ( (ret = decodedCertCache_test()) != 0)
  1546. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1547. else
  1548. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1549. #endif
  1550. #ifdef HAVE_CURVE25519
  1551. if ( (ret = curve25519_test()) != 0)
  1552. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1553. else
  1554. TEST_PASS("CURVE25519 test passed!\n");
  1555. #endif
  1556. #ifdef HAVE_ED25519
  1557. if ( (ret = ed25519_test()) != 0)
  1558. TEST_FAIL("ED25519 test failed!\n", ret);
  1559. else
  1560. TEST_PASS("ED25519 test passed!\n");
  1561. #endif
  1562. #ifdef HAVE_CURVE448
  1563. if ( (ret = curve448_test()) != 0)
  1564. TEST_FAIL("CURVE448 test failed!\n", ret);
  1565. else
  1566. TEST_PASS("CURVE448 test passed!\n");
  1567. #endif
  1568. #ifdef HAVE_ED448
  1569. if ( (ret = ed448_test()) != 0)
  1570. TEST_FAIL("ED448 test failed!\n", ret);
  1571. else
  1572. TEST_PASS("ED448 test passed!\n");
  1573. #endif
  1574. #ifdef WOLFSSL_HAVE_KYBER
  1575. if ( (ret = kyber_test()) != 0)
  1576. TEST_FAIL("KYBER test failed!\n", ret);
  1577. else
  1578. TEST_PASS("KYBER test passed!\n");
  1579. #endif
  1580. #if defined(WOLFSSL_HAVE_XMSS)
  1581. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  1582. if ( (ret = xmss_test_verify_only()) != 0)
  1583. TEST_FAIL("XMSS Vfy test failed!\n", ret);
  1584. else
  1585. TEST_PASS("XMSS Vfy test passed!\n");
  1586. #endif
  1587. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1588. if ( (ret = xmss_test()) != 0)
  1589. TEST_FAIL("XMSS test failed!\n", ret);
  1590. else
  1591. TEST_PASS("XMSS test passed!\n");
  1592. #endif
  1593. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  1594. #if defined(WOLFSSL_HAVE_LMS)
  1595. #if !defined(WOLFSSL_SMALL_STACK)
  1596. #if defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)
  1597. if ( (ret = lms_test_verify_only()) != 0)
  1598. TEST_FAIL("LMS Vfy test failed!\n", ret);
  1599. else
  1600. TEST_PASS("LMS Vfy test passed!\n");
  1601. #endif
  1602. #endif
  1603. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  1604. if ( (ret = lms_test()) != 0)
  1605. TEST_FAIL("LMS test failed!\n", ret);
  1606. else
  1607. TEST_PASS("LMS test passed!\n");
  1608. #endif
  1609. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  1610. #ifdef WOLFCRYPT_HAVE_ECCSI
  1611. if ( (ret = eccsi_test()) != 0)
  1612. TEST_FAIL("ECCSI test failed!\n", ret);
  1613. else
  1614. TEST_PASS("ECCSI test passed!\n");
  1615. #endif
  1616. #ifdef WOLFCRYPT_HAVE_SAKKE
  1617. if ( (ret = sakke_test()) != 0)
  1618. TEST_FAIL("SAKKE test failed!\n", ret);
  1619. else
  1620. TEST_PASS("SAKKE test passed!\n");
  1621. #endif
  1622. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1623. if ( (ret = cmac_test()) != 0)
  1624. TEST_FAIL("CMAC test failed!\n", ret);
  1625. else
  1626. TEST_PASS("CMAC test passed!\n");
  1627. #endif
  1628. #if defined(WOLFSSL_SIPHASH)
  1629. if ( (ret = siphash_test()) != 0)
  1630. TEST_FAIL("SipHash test failed!\n", ret);
  1631. else
  1632. TEST_PASS("SipHash test passed!\n");
  1633. #endif
  1634. #ifdef HAVE_LIBZ
  1635. if ( (ret = compress_test()) != 0)
  1636. TEST_FAIL("COMPRESS test failed!\n", ret);
  1637. else
  1638. TEST_PASS("COMPRESS test passed!\n");
  1639. #endif
  1640. #ifdef HAVE_PKCS7
  1641. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1642. if ( (ret = pkcs7encrypted_test()) != 0)
  1643. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  1644. else
  1645. TEST_PASS("PKCS7encrypted test passed!\n");
  1646. #endif
  1647. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1648. if ( (ret = pkcs7compressed_test()) != 0)
  1649. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  1650. else
  1651. TEST_PASS("PKCS7compressed test passed!\n");
  1652. #endif
  1653. if ( (ret = pkcs7signed_test()) != 0)
  1654. TEST_FAIL("PKCS7signed test failed!\n", ret);
  1655. else
  1656. TEST_PASS("PKCS7signed test passed!\n");
  1657. if ( (ret = pkcs7enveloped_test()) != 0)
  1658. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  1659. else
  1660. TEST_PASS("PKCS7enveloped test passed!\n");
  1661. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1662. if ( (ret = pkcs7authenveloped_test()) != 0)
  1663. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  1664. else
  1665. TEST_PASS("PKCS7authenveloped test passed!\n");
  1666. #endif
  1667. #endif
  1668. #if defined(WOLFSSL_PUBLIC_MP) && \
  1669. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  1670. defined(USE_FAST_MATH))
  1671. if ( (ret = mp_test()) != 0)
  1672. TEST_FAIL("mp test failed!\n", ret);
  1673. else
  1674. TEST_PASS("mp test passed!\n");
  1675. #endif
  1676. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1677. if ( (ret = prime_test()) != 0)
  1678. TEST_FAIL("prime test failed!\n", ret);
  1679. else
  1680. TEST_PASS("prime test passed!\n");
  1681. #endif
  1682. #if defined(ASN_BER_TO_DER) && \
  1683. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1684. defined(OPENSSL_EXTRA_X509_SMALL))
  1685. if ( (ret = berder_test()) != 0)
  1686. TEST_FAIL("ber-der test failed!\n", ret);
  1687. else
  1688. TEST_PASS("ber-der test passed!\n");
  1689. #endif
  1690. if ( (ret = logging_test()) != 0)
  1691. TEST_FAIL("logging test failed!\n", ret);
  1692. else
  1693. TEST_PASS("logging test passed!\n");
  1694. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1695. if ( (ret = time_test()) != 0)
  1696. TEST_FAIL("time test failed!\n", ret);
  1697. else
  1698. TEST_PASS("time test passed!\n");
  1699. #endif
  1700. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  1701. if ((ret = wolfcrypt_mutex_test()) != 0)
  1702. #else
  1703. if ((ret = mutex_test()) != 0)
  1704. #endif
  1705. TEST_FAIL("mutex test failed!\n", ret);
  1706. else
  1707. TEST_PASS("mutex test passed!\n");
  1708. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1709. if ( (ret = memcb_test()) != 0)
  1710. TEST_FAIL("memcb test failed!\n", ret);
  1711. else
  1712. TEST_PASS("memcb test passed!\n");
  1713. #endif
  1714. #ifdef WOLFSSL_CAAM_BLOB
  1715. if ( (ret = blob_test()) != 0)
  1716. TEST_FAIL("blob test failed!\n", ret);
  1717. else
  1718. TEST_PASS("blob test passed!\n");
  1719. #endif
  1720. #if defined(WOLF_CRYPTO_CB) && \
  1721. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1722. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  1723. if ( (ret = cryptocb_test()) != 0)
  1724. TEST_FAIL("crypto callback test failed!\n", ret);
  1725. else
  1726. TEST_PASS("crypto callback test passed!\n");
  1727. #endif
  1728. #ifdef WOLFSSL_CERT_PIV
  1729. if ( (ret = certpiv_test()) != 0)
  1730. TEST_FAIL("cert piv test failed!\n", ret);
  1731. else
  1732. TEST_PASS("cert piv test passed!\n");
  1733. #endif
  1734. #ifdef WOLF_CRYPTO_CB
  1735. #ifdef HAVE_INTEL_QA_SYNC
  1736. wc_CryptoCb_CleanupIntelQa(&devId);
  1737. #endif
  1738. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1739. wc_CryptoCb_CleanupOcteon(&devId);
  1740. #endif
  1741. #endif
  1742. #ifdef WOLFSSL_ASYNC_CRYPT
  1743. wolfAsync_DevClose(&devId);
  1744. #endif
  1745. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1746. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1747. wc_ecc_fp_free();
  1748. #endif
  1749. #ifdef TEST_ALWAYS_RUN_TO_END
  1750. if (last_failed_test_ret != 0)
  1751. ret = last_failed_test_ret;
  1752. #endif
  1753. if (args)
  1754. ((func_args*)args)->return_code = ret;
  1755. /* If hardware acceleration and respective metrics tracked, show results: */
  1756. #ifdef WOLFSSL_HW_METRICS
  1757. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  1758. esp_hw_show_mp_metrics();
  1759. #endif
  1760. #endif
  1761. TEST_PASS("Test complete\n");
  1762. EXIT_TEST(ret);
  1763. } /* end of wolfcrypt_test() */
  1764. #ifndef NO_MAIN_DRIVER
  1765. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1766. int myoptind = 0;
  1767. char* myoptarg = NULL;
  1768. #endif
  1769. /* so overall tests can pull in test function */
  1770. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1771. wc_test_ret_t wolf_test_task(void)
  1772. #else
  1773. #ifndef NO_MAIN_FUNCTION
  1774. int main(int argc, char** argv)
  1775. {
  1776. return (int)wolfcrypt_test_main(argc, argv);
  1777. }
  1778. #endif
  1779. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  1780. #endif
  1781. {
  1782. wc_test_ret_t ret;
  1783. func_args args = { 0, 0, 0 };
  1784. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1785. /* set dummy wallclock time. */
  1786. struct timeval utctime;
  1787. struct timezone tz;
  1788. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1789. utctime.tv_usec = 0;
  1790. tz.tz_minuteswest = 0;
  1791. tz.tz_dsttime = 0;
  1792. settimeofday(&utctime, &tz);
  1793. #endif
  1794. #ifdef WOLFSSL_APACHE_MYNEWT
  1795. #ifdef ARCH_sim
  1796. mcu_sim_parse_args(argc, argv);
  1797. #endif
  1798. sysinit();
  1799. /* set dummy wallclock time. */
  1800. struct os_timeval utctime;
  1801. struct os_timezone tz;
  1802. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1803. utctime.tv_usec = 0;
  1804. tz.tz_minuteswest = 0;
  1805. tz.tz_dsttime = 0;
  1806. os_settimeofday(&utctime, &tz);
  1807. #endif
  1808. #ifdef WOLFSSL_ZEPHYR
  1809. /* set dummy wallclock time. */
  1810. struct timespec utctime;
  1811. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1812. utctime.tv_nsec = 0;
  1813. clock_settime(CLOCK_REALTIME, &utctime);
  1814. #endif
  1815. #ifdef DEVKITPRO
  1816. void *framebuffer;
  1817. GXRModeObj *rmode = NULL;
  1818. VIDEO_Init();
  1819. WPAD_Init();
  1820. rmode = VIDEO_GetPreferredMode(NULL);
  1821. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1822. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1823. #pragma GCC diagnostic pop
  1824. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1825. VIDEO_Configure(rmode);
  1826. VIDEO_SetNextFramebuffer(framebuffer);
  1827. VIDEO_SetBlack(FALSE);
  1828. VIDEO_Flush();
  1829. VIDEO_WaitVSync();
  1830. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1831. #endif
  1832. #ifdef HAVE_WNR
  1833. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  1834. err_sys("Whitewood netRandom global config failed",
  1835. WC_TEST_RET_ENC_EC(ret));
  1836. return WC_TEST_RET_ENC_EC(ret);
  1837. }
  1838. #endif
  1839. #ifndef WOLFSSL_ESPIDF
  1840. args.argc = argc;
  1841. args.argv = argv;
  1842. #endif
  1843. if ((ret = wolfCrypt_Init()) != 0) {
  1844. printf("wolfCrypt_Init failed %d\n", (int)ret);
  1845. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  1846. }
  1847. #ifdef HAVE_WC_INTROSPECTION
  1848. printf("Math: %s\n", wc_GetMathInfo());
  1849. #endif
  1850. #ifdef WC_RNG_SEED_CB
  1851. wc_SetSeed_Cb(wc_GenerateSeed);
  1852. #endif
  1853. #ifdef HAVE_STACK_SIZE
  1854. StackSizeCheck(&args, wolfcrypt_test);
  1855. #else
  1856. wolfcrypt_test(&args);
  1857. #endif
  1858. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1859. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  1860. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  1861. }
  1862. #ifdef HAVE_WNR
  1863. if ((ret = wc_FreeNetRandom()) < 0)
  1864. err_sys("Failed to free netRandom context",
  1865. WC_TEST_RET_ENC_EC(ret));
  1866. #endif /* HAVE_WNR */
  1867. #ifdef DOLPHIN_EMULATOR
  1868. /* Returning from main panics the emulator. Just hang
  1869. * and let the user force quit the emulator window. */
  1870. printf("args.return_code: %d\n", args.return_code);
  1871. printf("Testing complete. You may close the window now\n");
  1872. while (1);
  1873. #endif
  1874. #if defined(WOLFSSL_ESPIDF)
  1875. /* ESP_LOGI to print takes up a lot less memory than printf */
  1876. ESP_LOGI(ESPIDF_TAG, "Exiting main with return code: % d\n",
  1877. args.return_code);
  1878. #else
  1879. /* gate this for target platforms wishing to avoid printf reference */
  1880. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  1881. #endif
  1882. return args.return_code;
  1883. } /* wolfcrypt_test_main or wolf_test_task */
  1884. #endif /* NO_MAIN_DRIVER */
  1885. /* helper to save DER, convert to PEM and save PEM */
  1886. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1887. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1888. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1889. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1890. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  1891. #else
  1892. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  1893. #endif
  1894. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  1895. const char* fileDer, const char* filePem, int pemType, int calling_line)
  1896. {
  1897. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1898. wc_test_ret_t ret;
  1899. XFILE derFile;
  1900. derFile = XFOPEN(fileDer, "wb");
  1901. if (!derFile) {
  1902. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  1903. }
  1904. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1905. XFCLOSE(derFile);
  1906. if (ret != derSz) {
  1907. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  1908. }
  1909. #endif
  1910. #ifdef WOLFSSL_DER_TO_PEM
  1911. if (filePem) {
  1912. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1913. XFILE pemFile;
  1914. #endif
  1915. #ifndef WOLFSSL_NO_MALLOC
  1916. byte* pem;
  1917. #else
  1918. byte pem[1024];
  1919. #endif
  1920. int pemSz;
  1921. /* calculate PEM size */
  1922. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1923. if (pemSz < 0) {
  1924. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  1925. }
  1926. #ifndef WOLFSSL_NO_MALLOC
  1927. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1928. if (pem == NULL) {
  1929. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  1930. }
  1931. #else
  1932. if (pemSz > (int)sizeof(pem))
  1933. return BAD_FUNC_ARG;
  1934. #endif
  1935. /* Convert to PEM */
  1936. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1937. if (pemSz < 0) {
  1938. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1939. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  1940. }
  1941. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1942. pemFile = XFOPEN(filePem, "wb");
  1943. if (!pemFile) {
  1944. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1945. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  1946. }
  1947. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1948. XFCLOSE(pemFile);
  1949. if (ret != pemSz) {
  1950. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1951. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  1952. }
  1953. #endif
  1954. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1955. }
  1956. #endif /* WOLFSSL_DER_TO_PEM */
  1957. /* suppress unused variable warnings */
  1958. (void)der;
  1959. (void)derSz;
  1960. (void)filePem;
  1961. (void)fileDer;
  1962. (void)pemType;
  1963. (void)calling_line;
  1964. return 0;
  1965. }
  1966. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1967. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  1968. {
  1969. const char* errStr;
  1970. char out[WOLFSSL_MAX_ERROR_SZ];
  1971. const char* unknownStr = wc_GetErrorString(0);
  1972. #ifdef NO_ERROR_STRINGS
  1973. /* Ensure a valid error code's string matches an invalid code's.
  1974. * The string is that error strings are not available.
  1975. */
  1976. WOLFSSL_ENTER("error_test NO_ERROR_STRINGS");
  1977. errStr = wc_GetErrorString(OPEN_RAN_E);
  1978. wc_ErrorString(OPEN_RAN_E, out);
  1979. if (XSTRCMP(errStr, unknownStr) != 0)
  1980. return WC_TEST_RET_ENC_NC;
  1981. if (XSTRCMP(out, unknownStr) != 0)
  1982. return WC_TEST_RET_ENC_NC;
  1983. #else
  1984. int i;
  1985. int j = 0;
  1986. /* Values that are not or no longer error codes. */
  1987. int missing[] = { -124, -128, -129, -159, -163, -164,
  1988. -165, -166, -167, -168, -169, -233, 0 };
  1989. WOLFSSL_ENTER("error_test !NO_ERROR_STRINGS");
  1990. /* Check that all errors have a string and it's the same through the two
  1991. * APIs. Check that the values that are not errors map to the unknown
  1992. * string.
  1993. */
  1994. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1995. errStr = wc_GetErrorString(i);
  1996. wc_ErrorString(i, out);
  1997. if (i != missing[j]) {
  1998. if (XSTRCMP(errStr, unknownStr) == 0) {
  1999. WOLFSSL_MSG("errStr unknown");
  2000. return WC_TEST_RET_ENC_NC;
  2001. }
  2002. if (XSTRCMP(out, unknownStr) == 0) {
  2003. WOLFSSL_MSG("out unknown");
  2004. return WC_TEST_RET_ENC_NC;
  2005. }
  2006. if (XSTRCMP(errStr, out) != 0) {
  2007. WOLFSSL_MSG("errStr does not match output");
  2008. return WC_TEST_RET_ENC_NC;
  2009. }
  2010. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
  2011. WOLFSSL_MSG("errStr too long");
  2012. return WC_TEST_RET_ENC_NC;
  2013. }
  2014. }
  2015. else {
  2016. j++;
  2017. if (XSTRCMP(errStr, unknownStr) != 0)
  2018. return WC_TEST_RET_ENC_NC;
  2019. if (XSTRCMP(out, unknownStr) != 0)
  2020. return WC_TEST_RET_ENC_NC;
  2021. }
  2022. }
  2023. /* Check if the next possible value has been given a string. */
  2024. errStr = wc_GetErrorString(i);
  2025. wc_ErrorString(i, out);
  2026. if (XSTRCMP(errStr, unknownStr) != 0)
  2027. return WC_TEST_RET_ENC_NC;
  2028. if (XSTRCMP(out, unknownStr) != 0)
  2029. return WC_TEST_RET_ENC_NC;
  2030. #endif
  2031. return 0;
  2032. }
  2033. #ifndef NO_CODING
  2034. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  2035. {
  2036. wc_test_ret_t ret;
  2037. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  2038. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  2039. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  2040. byte out[128];
  2041. word32 outLen;
  2042. #ifdef WOLFSSL_BASE64_ENCODE
  2043. byte data[3];
  2044. word32 dataLen;
  2045. byte longData[79] = { 0 };
  2046. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  2047. #endif
  2048. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  2049. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  2050. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  2051. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  2052. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  2053. byte goodChar[] =
  2054. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2055. "abcdefghijklmnopqrstuvwxyz"
  2056. "0123456789+/;";
  2057. byte charTest[] = "A+Gd\0\0\0";
  2058. int i;
  2059. WOLFSSL_ENTER("base64_test");
  2060. /* Good Base64 encodings. */
  2061. outLen = sizeof(out);
  2062. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2063. if (ret != 0)
  2064. return WC_TEST_RET_ENC_EC(ret);
  2065. outLen = sizeof(out);
  2066. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  2067. if (ret != 0)
  2068. return WC_TEST_RET_ENC_EC(ret);
  2069. outLen = sizeof(goodChar);
  2070. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  2071. if (ret != 0)
  2072. return WC_TEST_RET_ENC_EC(ret);
  2073. if (outLen != 64 / 4 * 3)
  2074. return WC_TEST_RET_ENC_NC;
  2075. outLen = sizeof(out);
  2076. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2077. if (ret != 0)
  2078. return WC_TEST_RET_ENC_EC(ret);
  2079. /* Bad parameters. */
  2080. outLen = 1;
  2081. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2082. if (ret != BAD_FUNC_ARG)
  2083. return WC_TEST_RET_ENC_EC(ret);
  2084. outLen = sizeof(out);
  2085. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2086. if (ret != ASN_INPUT_E)
  2087. return WC_TEST_RET_ENC_EC(ret);
  2088. outLen = sizeof(out);
  2089. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2090. if (ret != ASN_INPUT_E)
  2091. return WC_TEST_RET_ENC_EC(ret);
  2092. /* Bad character at each offset 0-3. */
  2093. for (i = 0; i < 4; i++) {
  2094. outLen = sizeof(out);
  2095. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2096. if (ret != ASN_INPUT_E)
  2097. return WC_TEST_RET_ENC_I(i);
  2098. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2099. if (ret != ASN_INPUT_E)
  2100. return WC_TEST_RET_ENC_I(i);
  2101. }
  2102. /* Invalid character less than 0x2b */
  2103. for (i = 1; i < 0x2b; i++) {
  2104. outLen = sizeof(out);
  2105. charTest[0] = (byte)i;
  2106. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2107. if (ret != ASN_INPUT_E)
  2108. return WC_TEST_RET_ENC_I(i);
  2109. }
  2110. /* Bad characters in range 0x2b - 0x7a. */
  2111. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2112. outLen = sizeof(out);
  2113. charTest[0] = badChar[i];
  2114. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2115. if (ret != ASN_INPUT_E)
  2116. return WC_TEST_RET_ENC_I(i);
  2117. }
  2118. /* Invalid character greater than 0x7a */
  2119. for (i = 0x7b; i < 0x100; i++) {
  2120. outLen = sizeof(out);
  2121. charTest[0] = (byte)i;
  2122. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2123. if (ret != ASN_INPUT_E)
  2124. return WC_TEST_RET_ENC_I(i);
  2125. }
  2126. #ifdef WOLFSSL_BASE64_ENCODE
  2127. /* Decode and encode all symbols - non-alphanumeric. */
  2128. dataLen = sizeof(data);
  2129. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2130. if (ret != 0)
  2131. return WC_TEST_RET_ENC_EC(ret);
  2132. outLen = sizeof(out);
  2133. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  2134. if (ret != LENGTH_ONLY_E)
  2135. return WC_TEST_RET_ENC_EC(ret);
  2136. outLen = sizeof(out);
  2137. ret = Base64_Encode(data, dataLen, out, &outLen);
  2138. if (ret != 0)
  2139. return WC_TEST_RET_ENC_EC(ret);
  2140. outLen = 7;
  2141. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2142. if (ret != BUFFER_E)
  2143. return WC_TEST_RET_ENC_EC(ret);
  2144. outLen = sizeof(out);
  2145. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2146. if (ret != LENGTH_ONLY_E)
  2147. return WC_TEST_RET_ENC_EC(ret);
  2148. outLen = sizeof(out);
  2149. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2150. if (ret != 0)
  2151. return WC_TEST_RET_ENC_EC(ret);
  2152. outLen = sizeof(out);
  2153. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2154. if (ret != 0)
  2155. return WC_TEST_RET_ENC_EC(ret);
  2156. /* Data that results in an encoding longer than one line. */
  2157. outLen = sizeof(out);
  2158. dataLen = sizeof(longData);
  2159. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2160. if (ret != 0)
  2161. return WC_TEST_RET_ENC_EC(ret);
  2162. outLen = sizeof(out);
  2163. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2164. if (ret != 0)
  2165. return WC_TEST_RET_ENC_EC(ret);
  2166. outLen = sizeof(out);
  2167. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2168. if (ret != 0)
  2169. return WC_TEST_RET_ENC_EC(ret);
  2170. #endif
  2171. return 0;
  2172. }
  2173. #ifdef WOLFSSL_BASE16
  2174. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2175. {
  2176. wc_test_ret_t ret;
  2177. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2178. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2179. byte encoded[40];
  2180. word32 encodedLen;
  2181. byte plain[40];
  2182. word32 len;
  2183. WOLFSSL_ENTER("base16_test");
  2184. /* length returned includes null termination */
  2185. encodedLen = sizeof(encoded);
  2186. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2187. if (ret != 0)
  2188. return WC_TEST_RET_ENC_EC(ret);
  2189. len = (word32)XSTRLEN((char*)encoded);
  2190. if (len != encodedLen - 1)
  2191. return WC_TEST_RET_ENC_NC;
  2192. len = sizeof(plain);
  2193. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2194. if (ret != 0)
  2195. return WC_TEST_RET_ENC_EC(ret);
  2196. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2197. return WC_TEST_RET_ENC_NC;
  2198. if (encodedLen != sizeof(encodedTestData) ||
  2199. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2200. return WC_TEST_RET_ENC_NC;
  2201. }
  2202. return 0;
  2203. }
  2204. #endif /* WOLFSSL_BASE16 */
  2205. #endif /* !NO_CODING */
  2206. #ifndef NO_ASN
  2207. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2208. {
  2209. wc_test_ret_t ret;
  2210. /* ASN1 encoded date buffer */
  2211. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {
  2212. 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2213. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a
  2214. };
  2215. byte format;
  2216. int length;
  2217. const byte* datePart;
  2218. #ifndef NO_ASN_TIME
  2219. struct tm timearg;
  2220. time_t now;
  2221. #endif
  2222. WOLFSSL_ENTER("asn_test");
  2223. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2224. &length);
  2225. if (ret != 0)
  2226. return WC_TEST_RET_ENC_EC(ret);
  2227. #ifndef NO_ASN_TIME
  2228. /* Parameter Validation tests. */
  2229. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2230. return WC_TEST_RET_ENC_EC(ret);
  2231. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2232. return WC_TEST_RET_ENC_EC(ret);
  2233. now = 0;
  2234. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2235. return WC_TEST_RET_ENC_EC(ret);
  2236. }
  2237. if (now == 0) {
  2238. printf("RTC/Time not set!\n");
  2239. return WC_TEST_RET_ENC_NC;
  2240. }
  2241. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2242. if (ret != 0)
  2243. return WC_TEST_RET_ENC_EC(ret);
  2244. #endif /* !NO_ASN_TIME */
  2245. return 0;
  2246. }
  2247. #endif /* !NO_ASN */
  2248. #ifdef WOLFSSL_MD2
  2249. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2250. {
  2251. wc_test_ret_t ret = 0;
  2252. Md2 md2;
  2253. byte hash[MD2_DIGEST_SIZE];
  2254. testVector a, b, c, d, e, f, g;
  2255. testVector test_md2[7];
  2256. int times = sizeof(test_md2) / sizeof(testVector), i;
  2257. WOLFSSL_ENTER("md2_test");
  2258. a.input = "";
  2259. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2260. "\x27\x73";
  2261. a.inLen = XSTRLEN(a.input);
  2262. a.outLen = MD2_DIGEST_SIZE;
  2263. b.input = "a";
  2264. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2265. "\xb5\xd1";
  2266. b.inLen = XSTRLEN(b.input);
  2267. b.outLen = MD2_DIGEST_SIZE;
  2268. c.input = "abc";
  2269. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2270. "\xd6\xbb";
  2271. c.inLen = XSTRLEN(c.input);
  2272. c.outLen = MD2_DIGEST_SIZE;
  2273. d.input = "message digest";
  2274. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2275. "\x06\xb0";
  2276. d.inLen = XSTRLEN(d.input);
  2277. d.outLen = MD2_DIGEST_SIZE;
  2278. e.input = "abcdefghijklmnopqrstuvwxyz";
  2279. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2280. "\x94\x0b";
  2281. e.inLen = XSTRLEN(e.input);
  2282. e.outLen = MD2_DIGEST_SIZE;
  2283. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2284. "6789";
  2285. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2286. "\x38\xcd";
  2287. f.inLen = XSTRLEN(f.input);
  2288. f.outLen = MD2_DIGEST_SIZE;
  2289. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2290. "9012345678901234567890";
  2291. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2292. "\xef\xd8";
  2293. g.inLen = XSTRLEN(g.input);
  2294. g.outLen = MD2_DIGEST_SIZE;
  2295. test_md2[0] = a;
  2296. test_md2[1] = b;
  2297. test_md2[2] = c;
  2298. test_md2[3] = d;
  2299. test_md2[4] = e;
  2300. test_md2[5] = f;
  2301. test_md2[6] = g;
  2302. wc_InitMd2(&md2);
  2303. for (i = 0; i < times; ++i) {
  2304. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2305. wc_Md2Final(&md2, hash);
  2306. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2307. return WC_TEST_RET_ENC_I(i);
  2308. }
  2309. for (i = 0; i < times; ++i) {
  2310. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2311. if (ret != 0) {
  2312. return WC_TEST_RET_ENC_I(i);
  2313. }
  2314. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2315. return WC_TEST_RET_ENC_I(i);
  2316. }
  2317. }
  2318. return 0;
  2319. }
  2320. #endif
  2321. #ifndef NO_MD5
  2322. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2323. {
  2324. wc_test_ret_t ret = 0;
  2325. wc_Md5 md5, md5Copy;
  2326. byte hash[WC_MD5_DIGEST_SIZE];
  2327. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2328. testVector a, b, c, d, e, f;
  2329. testVector test_md5[6];
  2330. int times = sizeof(test_md5) / sizeof(testVector), i;
  2331. WOLFSSL_ENTER("md5_test");
  2332. a.input = "";
  2333. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2334. "\x7e";
  2335. a.inLen = XSTRLEN(a.input);
  2336. a.outLen = WC_MD5_DIGEST_SIZE;
  2337. b.input = "abc";
  2338. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2339. "\x72";
  2340. b.inLen = XSTRLEN(b.input);
  2341. b.outLen = WC_MD5_DIGEST_SIZE;
  2342. c.input = "message digest";
  2343. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2344. "\xd0";
  2345. c.inLen = XSTRLEN(c.input);
  2346. c.outLen = WC_MD5_DIGEST_SIZE;
  2347. d.input = "abcdefghijklmnopqrstuvwxyz";
  2348. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2349. "\x3b";
  2350. d.inLen = XSTRLEN(d.input);
  2351. d.outLen = WC_MD5_DIGEST_SIZE;
  2352. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2353. "6789";
  2354. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2355. "\x9f";
  2356. e.inLen = XSTRLEN(e.input);
  2357. e.outLen = WC_MD5_DIGEST_SIZE;
  2358. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2359. "9012345678901234567890";
  2360. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2361. "\x7a";
  2362. f.inLen = XSTRLEN(f.input);
  2363. f.outLen = WC_MD5_DIGEST_SIZE;
  2364. test_md5[0] = a;
  2365. test_md5[1] = b;
  2366. test_md5[2] = c;
  2367. test_md5[3] = d;
  2368. test_md5[4] = e;
  2369. test_md5[5] = f;
  2370. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2371. if (ret != 0)
  2372. return WC_TEST_RET_ENC_EC(ret);
  2373. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2374. if (ret != 0) {
  2375. wc_Md5Free(&md5);
  2376. return WC_TEST_RET_ENC_EC(ret);
  2377. }
  2378. for (i = 0; i < times; ++i) {
  2379. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2380. (word32)test_md5[i].inLen);
  2381. if (ret != 0)
  2382. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2383. ret = wc_Md5GetHash(&md5, hashcopy);
  2384. if (ret != 0)
  2385. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2386. ret = wc_Md5Copy(&md5, &md5Copy);
  2387. if (ret != 0)
  2388. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2389. ret = wc_Md5Final(&md5, hash);
  2390. if (ret != 0)
  2391. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2392. wc_Md5Free(&md5Copy);
  2393. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2394. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2395. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2396. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2397. }
  2398. #ifndef NO_LARGE_HASH_TEST
  2399. /* BEGIN LARGE HASH TEST */ {
  2400. byte large_input[1024];
  2401. const char* large_digest =
  2402. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2403. for (i = 0; i < (int)sizeof(large_input); i++) {
  2404. large_input[i] = (byte)(i & 0xFF);
  2405. }
  2406. times = 100;
  2407. #ifdef WOLFSSL_PIC32MZ_HASH
  2408. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2409. #endif
  2410. for (i = 0; i < times; ++i) {
  2411. ret = wc_Md5Update(&md5, (byte*)large_input,
  2412. (word32)sizeof(large_input));
  2413. if (ret != 0)
  2414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2415. }
  2416. ret = wc_Md5Final(&md5, hash);
  2417. if (ret != 0)
  2418. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2419. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2420. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2421. } /* END LARGE HASH TEST */
  2422. #endif /* NO_LARGE_HASH_TEST */
  2423. exit:
  2424. wc_Md5Free(&md5);
  2425. wc_Md5Free(&md5Copy);
  2426. return ret;
  2427. }
  2428. #endif /* NO_MD5 */
  2429. #ifndef NO_MD4
  2430. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2431. {
  2432. Md4 md4;
  2433. byte hash[MD4_DIGEST_SIZE];
  2434. testVector a, b, c, d, e, f, g;
  2435. testVector test_md4[7];
  2436. int times = sizeof(test_md4) / sizeof(testVector), i;
  2437. WOLFSSL_ENTER("md4_test");
  2438. a.input = "";
  2439. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2440. "\xc0";
  2441. a.inLen = XSTRLEN(a.input);
  2442. a.outLen = MD4_DIGEST_SIZE;
  2443. b.input = "a";
  2444. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2445. "\x24";
  2446. b.inLen = XSTRLEN(b.input);
  2447. b.outLen = MD4_DIGEST_SIZE;
  2448. c.input = "abc";
  2449. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2450. "\x9d";
  2451. c.inLen = XSTRLEN(c.input);
  2452. c.outLen = MD4_DIGEST_SIZE;
  2453. d.input = "message digest";
  2454. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2455. "\x4b";
  2456. d.inLen = XSTRLEN(d.input);
  2457. d.outLen = MD4_DIGEST_SIZE;
  2458. e.input = "abcdefghijklmnopqrstuvwxyz";
  2459. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2460. "\xa9";
  2461. e.inLen = XSTRLEN(e.input);
  2462. e.outLen = MD4_DIGEST_SIZE;
  2463. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2464. "6789";
  2465. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2466. "\xe4";
  2467. f.inLen = XSTRLEN(f.input);
  2468. f.outLen = MD4_DIGEST_SIZE;
  2469. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2470. "9012345678901234567890";
  2471. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2472. "\x36";
  2473. g.inLen = XSTRLEN(g.input);
  2474. g.outLen = MD4_DIGEST_SIZE;
  2475. test_md4[0] = a;
  2476. test_md4[1] = b;
  2477. test_md4[2] = c;
  2478. test_md4[3] = d;
  2479. test_md4[4] = e;
  2480. test_md4[5] = f;
  2481. test_md4[6] = g;
  2482. wc_InitMd4(&md4);
  2483. for (i = 0; i < times; ++i) {
  2484. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2485. wc_Md4Final(&md4, hash);
  2486. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2487. return WC_TEST_RET_ENC_I(i);
  2488. }
  2489. return 0;
  2490. }
  2491. #endif /* NO_MD4 */
  2492. #ifndef NO_SHA
  2493. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2494. {
  2495. wc_test_ret_t ret = 0;
  2496. wc_Sha sha, shaCopy;
  2497. byte hash[WC_SHA_DIGEST_SIZE];
  2498. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2499. testVector a, b, c, d, e;
  2500. testVector test_sha[5];
  2501. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2502. WOLFSSL_ENTER("sha_test");
  2503. a.input = "";
  2504. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2505. "\x90\xaf\xd8\x07\x09";
  2506. a.inLen = XSTRLEN(a.input);
  2507. a.outLen = WC_SHA_DIGEST_SIZE;
  2508. b.input = "abc";
  2509. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2510. "\x6C\x9C\xD0\xD8\x9D";
  2511. b.inLen = XSTRLEN(b.input);
  2512. b.outLen = WC_SHA_DIGEST_SIZE;
  2513. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2514. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2515. "\xE5\xE5\x46\x70\xF1";
  2516. c.inLen = XSTRLEN(c.input);
  2517. c.outLen = WC_SHA_DIGEST_SIZE;
  2518. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2519. "aaaaaa";
  2520. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2521. "\x2A\x25\xEC\x64\x4D";
  2522. d.inLen = XSTRLEN(d.input);
  2523. d.outLen = WC_SHA_DIGEST_SIZE;
  2524. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2525. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2526. "aaaaaaaaaa";
  2527. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2528. "\x53\x99\x5E\x26\xA0";
  2529. e.inLen = XSTRLEN(e.input);
  2530. e.outLen = WC_SHA_DIGEST_SIZE;
  2531. test_sha[0] = a;
  2532. test_sha[1] = b;
  2533. test_sha[2] = c;
  2534. test_sha[3] = d;
  2535. test_sha[4] = e;
  2536. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2537. if (ret != 0)
  2538. return WC_TEST_RET_ENC_EC(ret);
  2539. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2540. if (ret != 0) {
  2541. wc_ShaFree(&sha);
  2542. return WC_TEST_RET_ENC_EC(ret);
  2543. }
  2544. for (i = 0; i < times; ++i) {
  2545. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2546. (word32)test_sha[i].inLen);
  2547. if (ret != 0)
  2548. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2549. ret = wc_ShaGetHash(&sha, hashcopy);
  2550. if (ret != 0)
  2551. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2552. ret = wc_ShaCopy(&sha, &shaCopy);
  2553. if (ret != 0)
  2554. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2555. ret = wc_ShaFinal(&sha, hash);
  2556. if (ret != 0)
  2557. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2558. wc_ShaFree(&shaCopy);
  2559. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2560. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2561. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2562. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2563. }
  2564. #ifndef NO_LARGE_HASH_TEST
  2565. /* BEGIN LARGE HASH TEST */ {
  2566. byte large_input[1024];
  2567. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2568. defined(HASH_SIZE_LIMIT)
  2569. const char* large_digest =
  2570. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2571. "\x15\x8c\x6d\xb6";
  2572. #else
  2573. const char* large_digest =
  2574. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2575. "\xc5\xd9\x0a\xac";
  2576. #endif
  2577. for (i = 0; i < (int)sizeof(large_input); i++) {
  2578. large_input[i] = (byte)(i & 0xFF);
  2579. }
  2580. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2581. defined(HASH_SIZE_LIMIT)
  2582. times = 20;
  2583. #else
  2584. times = 100;
  2585. #endif
  2586. #ifdef WOLFSSL_PIC32MZ_HASH
  2587. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2588. #endif
  2589. for (i = 0; i < times; ++i) {
  2590. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2591. (word32)sizeof(large_input));
  2592. if (ret != 0)
  2593. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2594. }
  2595. ret = wc_ShaFinal(&sha, hash);
  2596. if (ret != 0)
  2597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2598. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2599. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2600. } /* END LARGE HASH TEST */
  2601. #endif /* NO_LARGE_HASH_TEST */
  2602. exit:
  2603. wc_ShaFree(&sha);
  2604. wc_ShaFree(&shaCopy);
  2605. return ret;
  2606. }
  2607. #endif /* NO_SHA */
  2608. #ifdef WOLFSSL_RIPEMD
  2609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  2610. {
  2611. RipeMd ripemd;
  2612. wc_test_ret_t ret;
  2613. byte hash[RIPEMD_DIGEST_SIZE];
  2614. testVector a, b, c, d;
  2615. testVector test_ripemd[4];
  2616. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2617. WOLFSSL_ENTER("ripemd_test");
  2618. a.input = "abc";
  2619. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2620. "\xb0\x87\xf1\x5a\x0b\xfc";
  2621. a.inLen = XSTRLEN(a.input);
  2622. a.outLen = RIPEMD_DIGEST_SIZE;
  2623. b.input = "message digest";
  2624. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2625. "\x5f\xfa\x21\x59\x5f\x36";
  2626. b.inLen = XSTRLEN(b.input);
  2627. b.outLen = RIPEMD_DIGEST_SIZE;
  2628. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2629. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2630. "\xf4\x9a\xda\x62\xeb\x2b";
  2631. c.inLen = XSTRLEN(c.input);
  2632. c.outLen = RIPEMD_DIGEST_SIZE;
  2633. d.input = "12345678901234567890123456789012345678901234567890123456"
  2634. "789012345678901234567890";
  2635. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2636. "\x82\xbf\x63\x32\x6b\xfb";
  2637. d.inLen = XSTRLEN(d.input);
  2638. d.outLen = RIPEMD_DIGEST_SIZE;
  2639. test_ripemd[0] = a;
  2640. test_ripemd[1] = b;
  2641. test_ripemd[2] = c;
  2642. test_ripemd[3] = d;
  2643. ret = wc_InitRipeMd(&ripemd);
  2644. if (ret != 0) {
  2645. return WC_TEST_RET_ENC_EC(ret);
  2646. }
  2647. for (i = 0; i < times; ++i) {
  2648. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2649. (word32)test_ripemd[i].inLen);
  2650. if (ret != 0) {
  2651. return WC_TEST_RET_ENC_I(i);
  2652. }
  2653. ret = wc_RipeMdFinal(&ripemd, hash);
  2654. if (ret != 0) {
  2655. return WC_TEST_RET_ENC_I(i);
  2656. }
  2657. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2658. return WC_TEST_RET_ENC_I(i);
  2659. }
  2660. return 0;
  2661. }
  2662. #endif /* WOLFSSL_RIPEMD */
  2663. #ifdef HAVE_BLAKE2
  2664. #define BLAKE2B_TESTS 3
  2665. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2666. {
  2667. {
  2668. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2669. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2670. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2671. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2672. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2673. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2674. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2675. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2676. },
  2677. {
  2678. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2679. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2680. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2681. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2682. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2683. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2684. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2685. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2686. },
  2687. {
  2688. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2689. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2690. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2691. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2692. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2693. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2694. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2695. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2696. }
  2697. };
  2698. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  2699. {
  2700. Blake2b b2b;
  2701. byte digest[64];
  2702. byte input[64];
  2703. int i, ret;
  2704. WOLFSSL_ENTER("blake2b_test");
  2705. for (i = 0; i < (int)sizeof(input); i++)
  2706. input[i] = (byte)i;
  2707. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2708. ret = wc_InitBlake2b(&b2b, 64);
  2709. if (ret != 0)
  2710. return WC_TEST_RET_ENC_I(i);
  2711. ret = wc_Blake2bUpdate(&b2b, input, i);
  2712. if (ret != 0)
  2713. return WC_TEST_RET_ENC_I(i);
  2714. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2715. if (ret != 0)
  2716. return WC_TEST_RET_ENC_I(i);
  2717. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2718. return WC_TEST_RET_ENC_I(i);
  2719. }
  2720. }
  2721. return 0;
  2722. }
  2723. #endif /* HAVE_BLAKE2 */
  2724. #ifdef HAVE_BLAKE2S
  2725. #define BLAKE2S_TESTS 3
  2726. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2727. {
  2728. {
  2729. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2730. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2731. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2732. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2733. },
  2734. {
  2735. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2736. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2737. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2738. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2739. },
  2740. {
  2741. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2742. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2743. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2744. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2745. }
  2746. };
  2747. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  2748. {
  2749. Blake2s b2s;
  2750. byte digest[32];
  2751. byte input[64];
  2752. int i, ret;
  2753. WOLFSSL_ENTER("blake2s_test");
  2754. for (i = 0; i < (int)sizeof(input); i++)
  2755. input[i] = (byte)i;
  2756. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2757. ret = wc_InitBlake2s(&b2s, 32);
  2758. if (ret != 0)
  2759. return WC_TEST_RET_ENC_I(i);
  2760. ret = wc_Blake2sUpdate(&b2s, input, i);
  2761. if (ret != 0)
  2762. return WC_TEST_RET_ENC_I(i);
  2763. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2764. if (ret != 0)
  2765. return WC_TEST_RET_ENC_I(i);
  2766. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2767. return WC_TEST_RET_ENC_I(i);
  2768. }
  2769. }
  2770. return 0;
  2771. }
  2772. #endif /* HAVE_BLAKE2S */
  2773. #ifdef WOLFSSL_SHA224
  2774. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  2775. {
  2776. wc_Sha224 sha, shaCopy;
  2777. byte hash[WC_SHA224_DIGEST_SIZE];
  2778. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2779. wc_test_ret_t ret = 0;
  2780. testVector a, b, c;
  2781. testVector test_sha[3];
  2782. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2783. WOLFSSL_ENTER("sha224_test");
  2784. a.input = "";
  2785. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2786. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2787. a.inLen = XSTRLEN(a.input);
  2788. a.outLen = WC_SHA224_DIGEST_SIZE;
  2789. b.input = "abc";
  2790. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2791. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2792. b.inLen = XSTRLEN(b.input);
  2793. b.outLen = WC_SHA224_DIGEST_SIZE;
  2794. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2795. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2796. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2797. c.inLen = XSTRLEN(c.input);
  2798. c.outLen = WC_SHA224_DIGEST_SIZE;
  2799. test_sha[0] = a;
  2800. test_sha[1] = b;
  2801. test_sha[2] = c;
  2802. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2803. if (ret != 0)
  2804. return WC_TEST_RET_ENC_EC(ret);
  2805. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2806. if (ret != 0) {
  2807. wc_Sha224Free(&sha);
  2808. return WC_TEST_RET_ENC_EC(ret);
  2809. }
  2810. for (i = 0; i < times; ++i) {
  2811. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2812. (word32)test_sha[i].inLen);
  2813. if (ret != 0)
  2814. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2815. ret = wc_Sha224GetHash(&sha, hashcopy);
  2816. if (ret != 0)
  2817. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2818. ret = wc_Sha224Copy(&sha, &shaCopy);
  2819. if (ret != 0)
  2820. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2821. ret = wc_Sha224Final(&sha, hash);
  2822. if (ret != 0)
  2823. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2824. wc_Sha224Free(&shaCopy);
  2825. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2826. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2827. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2828. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2829. }
  2830. exit:
  2831. wc_Sha224Free(&sha);
  2832. wc_Sha224Free(&shaCopy);
  2833. return ret;
  2834. }
  2835. #endif
  2836. #ifndef NO_SHA256
  2837. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  2838. {
  2839. wc_Sha256 sha, shaCopy;
  2840. byte hash[WC_SHA256_DIGEST_SIZE];
  2841. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2842. wc_test_ret_t ret = 0;
  2843. testVector a, b, c, d;
  2844. testVector test_sha[4];
  2845. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2846. byte i_hash[WC_SHA256_DIGEST_SIZE];
  2847. byte i_hashcopy[WC_SHA256_DIGEST_SIZE];
  2848. testVector interleave_test_sha[4];
  2849. wc_Sha256 i_sha, i_shaCopy;
  2850. #endif
  2851. #ifndef NO_LARGE_HASH_TEST
  2852. #define LARGE_HASH_TEST_INPUT_SZ 1024
  2853. #ifdef WOLFSSL_SMALL_STACK
  2854. byte *large_input = NULL;
  2855. #else
  2856. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  2857. #endif
  2858. #endif
  2859. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2860. WOLFSSL_ENTER("sha256_test");
  2861. a.input = "";
  2862. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2863. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2864. "\xb8\x55";
  2865. a.inLen = XSTRLEN(a.input);
  2866. a.outLen = WC_SHA256_DIGEST_SIZE;
  2867. b.input = "abc";
  2868. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2869. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2870. "\x15\xAD";
  2871. b.inLen = XSTRLEN(b.input);
  2872. b.outLen = WC_SHA256_DIGEST_SIZE;
  2873. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2874. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2875. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2876. "\x06\xC1";
  2877. c.inLen = XSTRLEN(c.input);
  2878. c.outLen = WC_SHA256_DIGEST_SIZE;
  2879. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2880. "aaaaaa"; /* this is BLOCKSIZE length */
  2881. d.output = "\xFF\xE0\x54\xFE\x7A\xE0\xCB\x6D\xC6\x5C\x3A\xF9\xB6\x1D\x52"
  2882. "\x09\xF4\x39\x85\x1D\xB4\x3D\x0B\xA5\x99\x73\x37\xDF\x15\x46"
  2883. "\x68\xEB";
  2884. d.inLen = XSTRLEN(d.input);
  2885. d.outLen = WC_SHA256_DIGEST_SIZE;
  2886. test_sha[0] = a;
  2887. test_sha[1] = b;
  2888. test_sha[2] = c;
  2889. test_sha[3] = d;
  2890. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2891. interleave_test_sha[0] = a;
  2892. interleave_test_sha[1] = b;
  2893. interleave_test_sha[2] = c;
  2894. interleave_test_sha[3] = d;
  2895. #endif
  2896. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2897. if (ret != 0)
  2898. return WC_TEST_RET_ENC_EC(ret);
  2899. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2900. ret = wc_InitSha256_ex(&i_sha, HEAP_HINT, devId);
  2901. if (ret != 0)
  2902. return WC_TEST_RET_ENC_EC(ret);
  2903. #endif
  2904. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2905. if (ret != 0) {
  2906. wc_Sha256Free(&sha);
  2907. return WC_TEST_RET_ENC_EC(ret);
  2908. }
  2909. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2910. ret = wc_InitSha256_ex(&i_shaCopy, HEAP_HINT, devId);
  2911. if (ret != 0) {
  2912. wc_Sha256Free(&sha);
  2913. wc_Sha256Free(&i_sha);
  2914. return WC_TEST_RET_ENC_EC(ret);
  2915. }
  2916. #endif
  2917. for (i = 0; i < times; ++i) {
  2918. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2919. (word32)test_sha[i].inLen);
  2920. if (ret != 0) {
  2921. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2922. }
  2923. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2924. ret = wc_Sha256Update(&i_sha, (byte*)interleave_test_sha[i].input,
  2925. (word32)interleave_test_sha[i].inLen);
  2926. if (ret != 0) {
  2927. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2928. }
  2929. #endif
  2930. ret = wc_Sha256GetHash(&sha, hashcopy);
  2931. if (ret != 0)
  2932. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2933. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2934. ret = wc_Sha256GetHash(&i_sha, i_hashcopy);
  2935. if (ret != 0)
  2936. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2937. #endif
  2938. ret = wc_Sha256Copy(&sha, &shaCopy);
  2939. if (ret != 0)
  2940. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2941. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2942. ret = wc_Sha256Copy(&i_sha, &i_shaCopy);
  2943. if (ret != 0)
  2944. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2945. #endif
  2946. ret = wc_Sha256Final(&sha, hash);
  2947. if (ret != 0)
  2948. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2949. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2950. ret = wc_Sha256Final(&i_sha, i_hash);
  2951. if (ret != 0)
  2952. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2953. #endif
  2954. wc_Sha256Free(&shaCopy);
  2955. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2956. wc_Sha256Free(&i_shaCopy);
  2957. #endif
  2958. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  2959. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2960. }
  2961. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  2962. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2963. }
  2964. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  2965. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  2966. WC_SHA256_DIGEST_SIZE) != 0) {
  2967. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2968. }
  2969. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  2970. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2971. }
  2972. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  2973. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2974. }
  2975. if (XMEMCMP(i_hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  2976. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2977. }
  2978. #endif
  2979. }
  2980. #ifndef NO_LARGE_HASH_TEST
  2981. /* BEGIN LARGE HASH TEST */ {
  2982. #ifdef HASH_SIZE_LIMIT
  2983. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  2984. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2985. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2986. #else
  2987. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  2988. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2989. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2990. #endif
  2991. #ifdef WOLFSSL_SMALL_STACK
  2992. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  2993. DYNAMIC_TYPE_TMP_BUFFER);
  2994. if (large_input == NULL) {
  2995. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  2996. }
  2997. #endif
  2998. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  2999. large_input[i] = (byte)(i & 0xFF);
  3000. }
  3001. #ifdef HASH_SIZE_LIMIT
  3002. times = 20;
  3003. #else
  3004. times = 100;
  3005. #endif
  3006. #ifdef WOLFSSL_PIC32MZ_HASH
  3007. wc_Sha256SizeSet(&sha, times * LARGE_HASH_TEST_INPUT_SZ);
  3008. #endif
  3009. for (i = 0; i < times; ++i) {
  3010. ret = wc_Sha256Update(&sha, (byte*)large_input,
  3011. LARGE_HASH_TEST_INPUT_SZ);
  3012. if (ret != 0)
  3013. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3014. }
  3015. ret = wc_Sha256Final(&sha, hash);
  3016. if (ret != 0)
  3017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3018. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  3019. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3020. } /* END LARGE HASH TEST */
  3021. #undef LARGE_HASH_TEST_INPUT_SZ
  3022. #endif /* NO_LARGE_HASH_TEST */
  3023. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
  3024. unsigned char data_hb[WC_SHA256_BLOCK_SIZE] = {
  3025. 0x61, 0x62, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00,
  3026. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3027. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3028. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3029. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3030. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3031. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3032. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18
  3033. };
  3034. ret = wc_Sha256HashBlock(&sha, data_hb, hash);
  3035. if (ret != 0) {
  3036. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3037. }
  3038. if (XMEMCMP(hash, b.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3039. {
  3040. for (int ii = 0; ii < WC_SHA256_DIGEST_SIZE; ii++)
  3041. fprintf(stderr, " %02x", hash[ii]);
  3042. fprintf(stderr, "\n");
  3043. for (int ii = 0; ii < WC_SHA256_DIGEST_SIZE; ii++)
  3044. fprintf(stderr, " %02x", b.output[ii]);
  3045. fprintf(stderr, "\n");
  3046. }
  3047. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3048. }
  3049. #endif
  3050. exit:
  3051. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3052. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3053. #endif
  3054. wc_Sha256Free(&sha);
  3055. wc_Sha256Free(&shaCopy);
  3056. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3057. wc_Sha256Free(&i_sha);
  3058. wc_Sha256Free(&i_shaCopy);
  3059. #endif
  3060. return ret;
  3061. }
  3062. #endif
  3063. #ifdef WOLFSSL_SHA512
  3064. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  3065. {
  3066. /*
  3067. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  3068. */
  3069. wc_Sha512 sha, shaCopy;
  3070. byte hash[WC_SHA512_DIGEST_SIZE];
  3071. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  3072. wc_test_ret_t ret = 0;
  3073. testVector a, b, c;
  3074. testVector test_sha[3];
  3075. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3076. wc_Sha512 i_sha, i_shaCopy;
  3077. byte i_hash[WC_SHA512_DIGEST_SIZE];
  3078. byte i_hashcopy[WC_SHA512_DIGEST_SIZE];
  3079. testVector interleave_test_sha[3];
  3080. #endif
  3081. #ifndef NO_LARGE_HASH_TEST
  3082. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3083. #ifdef WOLFSSL_SMALL_STACK
  3084. byte *large_input = NULL;
  3085. #else
  3086. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3087. #endif
  3088. #endif
  3089. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3090. WOLFSSL_ENTER("sha512_test");
  3091. a.input = "";
  3092. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  3093. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  3094. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  3095. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  3096. "\xf9\x27\xda\x3e";
  3097. a.inLen = XSTRLEN(a.input);
  3098. a.outLen = WC_SHA512_DIGEST_SIZE;
  3099. b.input = "abc";
  3100. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3101. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  3102. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  3103. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  3104. "\xa5\x4c\xa4\x9f";
  3105. b.inLen = XSTRLEN(b.input);
  3106. b.outLen = WC_SHA512_DIGEST_SIZE;
  3107. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3108. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3109. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  3110. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  3111. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  3112. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  3113. "\x87\x4b\xe9\x09";
  3114. c.inLen = XSTRLEN(c.input);
  3115. c.outLen = WC_SHA512_DIGEST_SIZE;
  3116. test_sha[0] = a;
  3117. test_sha[1] = b;
  3118. test_sha[2] = c;
  3119. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3120. interleave_test_sha[0] = a;
  3121. interleave_test_sha[1] = b;
  3122. interleave_test_sha[2] = c;
  3123. #endif
  3124. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  3125. if (ret != 0)
  3126. return WC_TEST_RET_ENC_EC(ret);
  3127. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3128. ret = wc_InitSha512_ex(&i_sha, HEAP_HINT, devId);
  3129. if (ret != 0)
  3130. return WC_TEST_RET_ENC_EC(ret);
  3131. #endif
  3132. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  3133. if (ret != 0) {
  3134. wc_Sha512Free(&sha);
  3135. return WC_TEST_RET_ENC_EC(ret);
  3136. }
  3137. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3138. ret = wc_InitSha512_ex(&i_shaCopy, HEAP_HINT, devId);
  3139. if (ret != 0) {
  3140. wc_Sha512Free(&sha);
  3141. wc_Sha512Free(&i_sha);
  3142. return WC_TEST_RET_ENC_EC(ret);
  3143. }
  3144. #endif
  3145. for (i = 0; i < times; ++i) {
  3146. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  3147. (word32)test_sha[i].inLen);
  3148. if (ret != 0)
  3149. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3150. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3151. ret = wc_Sha512Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3152. (word32)interleave_test_sha[i].inLen);
  3153. if (ret != 0)
  3154. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3155. #endif
  3156. ret = wc_Sha512GetHash(&sha, hashcopy);
  3157. if (ret != 0)
  3158. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3159. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3160. ret = wc_Sha512GetHash(&i_sha, i_hashcopy);
  3161. if (ret != 0)
  3162. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3163. #endif
  3164. ret = wc_Sha512Copy(&sha, &shaCopy);
  3165. if (ret != 0)
  3166. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3167. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3168. ret = wc_Sha512Copy(&i_sha, &i_shaCopy);
  3169. if (ret != 0)
  3170. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3171. #endif
  3172. ret = wc_Sha512Final(&sha, hash);
  3173. if (ret != 0)
  3174. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3175. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3176. ret = wc_Sha512Final(&i_sha, i_hash);
  3177. if (ret != 0)
  3178. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3179. #endif
  3180. wc_Sha512Free(&shaCopy);
  3181. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3182. wc_Sha512Free(&i_shaCopy);
  3183. #endif
  3184. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3186. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  3187. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3188. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3189. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3190. WC_SHA512_DIGEST_SIZE) != 0) {
  3191. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3192. }
  3193. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3194. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3195. }
  3196. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0) {
  3197. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3198. }
  3199. if (XMEMCMP(i_hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3200. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3201. }
  3202. #endif
  3203. }
  3204. #ifndef NO_LARGE_HASH_TEST
  3205. /* BEGIN LARGE HASH TEST */ {
  3206. #ifdef HASH_SIZE_LIMIT
  3207. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3208. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  3209. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  3210. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  3211. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  3212. #else
  3213. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3214. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  3215. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  3216. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  3217. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  3218. #endif
  3219. #ifdef WOLFSSL_SMALL_STACK
  3220. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3221. DYNAMIC_TYPE_TMP_BUFFER);
  3222. if (large_input == NULL) {
  3223. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3224. }
  3225. #endif
  3226. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3227. large_input[i] = (byte)(i & 0xFF);
  3228. }
  3229. #ifdef HASH_SIZE_LIMIT
  3230. times = 20;
  3231. #else
  3232. times = 100;
  3233. #endif
  3234. for (i = 0; i < times; ++i) {
  3235. ret = wc_Sha512Update(&sha, (byte*)large_input,
  3236. LARGE_HASH_TEST_INPUT_SZ);
  3237. if (ret != 0)
  3238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3239. }
  3240. ret = wc_Sha512Final(&sha, hash);
  3241. if (ret != 0)
  3242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3243. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  3244. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3245. #ifndef NO_UNALIGNED_MEMORY_TEST
  3246. /* Unaligned memory access test */
  3247. for (i = 1; i < 16; i++) {
  3248. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  3249. LARGE_HASH_TEST_INPUT_SZ - i);
  3250. if (ret != 0)
  3251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3252. ret = wc_Sha512Final(&sha, hash);
  3253. }
  3254. #endif
  3255. } /* END LARGE HASH TEST */
  3256. #undef LARGE_HASH_TEST_INPUT_SZ
  3257. #endif /* NO_LARGE_HASH_TEST */
  3258. exit:
  3259. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3260. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3261. #endif
  3262. wc_Sha512Free(&sha);
  3263. wc_Sha512Free(&shaCopy);
  3264. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3265. wc_Sha512Free(&i_sha);
  3266. wc_Sha512Free(&i_shaCopy);
  3267. #endif
  3268. return ret;
  3269. }
  3270. #if !defined(WOLFSSL_NOSHA512_224) && \
  3271. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3272. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  3273. {
  3274. /*
  3275. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3276. **
  3277. ** NIST SHA512/224 Document Example:
  3278. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  3279. */
  3280. wc_Sha512 sha, shaCopy;
  3281. byte hash[WC_SHA512_224_DIGEST_SIZE];
  3282. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  3283. wc_test_ret_t ret = 0;
  3284. testVector a, b, c;
  3285. testVector test_sha[3];
  3286. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3287. WOLFSSL_ENTER("sha512_224_test");
  3288. a.input = "";
  3289. a.output = "\x6e\xd0\xdd\x02"
  3290. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  3291. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  3292. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  3293. a.inLen = XSTRLEN(a.input);
  3294. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  3295. /*
  3296. ** See page 1 in above document for the SHA512/224 "abc" test:
  3297. */
  3298. b.input = "abc";
  3299. /*
  3300. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3301. */
  3302. b.output = "\x46\x34\x27\x0f"
  3303. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3304. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3305. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3306. b.inLen = XSTRLEN(b.input);
  3307. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3308. /*
  3309. ** See page 4 in above for the 2-block test:
  3310. */
  3311. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3312. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3313. /*
  3314. ** see page 9 in above document for the 2-block test message digest:
  3315. */
  3316. c.output = "\x23\xfe\xc5\xbb"
  3317. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3318. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3319. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3320. c.inLen = XSTRLEN(c.input);
  3321. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3322. test_sha[0] = a;
  3323. test_sha[1] = b;
  3324. test_sha[2] = c;
  3325. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3326. if (ret != 0)
  3327. return WC_TEST_RET_ENC_EC(ret);
  3328. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3329. if (ret != 0) {
  3330. wc_Sha512_224Free(&sha);
  3331. return WC_TEST_RET_ENC_EC(ret);
  3332. }
  3333. for (i = 0; i < times; ++i) {
  3334. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3335. (word32)test_sha[i].inLen);
  3336. if (ret != 0)
  3337. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3338. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3339. if (ret != 0)
  3340. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3341. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3342. if (ret != 0)
  3343. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3344. ret = wc_Sha512_224Final(&sha, hash);
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3347. wc_Sha512_224Free(&shaCopy);
  3348. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3349. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3350. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3351. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3352. }
  3353. #ifndef NO_LARGE_HASH_TEST
  3354. /* BEGIN LARGE HASH TEST */ {
  3355. byte large_input[1024];
  3356. #ifdef HASH_SIZE_LIMIT
  3357. const char* large_digest =
  3358. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3359. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3360. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3361. "\x14\x6c\xcf\x6a";
  3362. #else
  3363. const char* large_digest =
  3364. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3365. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3366. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3367. "\xe2\x4e\x85\x05";
  3368. #endif
  3369. for (i = 0; i < (int)sizeof(large_input); i++) {
  3370. large_input[i] = (byte)(i & 0xFF);
  3371. }
  3372. #ifdef HASH_SIZE_LIMIT
  3373. times = 20;
  3374. #else
  3375. times = 100;
  3376. #endif
  3377. for (i = 0; i < times; ++i) {
  3378. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3379. (word32)sizeof(large_input));
  3380. if (ret != 0)
  3381. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3382. }
  3383. ret = wc_Sha512_224Final(&sha, hash);
  3384. if (ret != 0)
  3385. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3386. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3387. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3388. #ifndef NO_UNALIGNED_MEMORY_TEST
  3389. /* Unaligned memory access test */
  3390. for (i = 1; i < 16; i++) {
  3391. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3392. (word32)sizeof(large_input) - i);
  3393. if (ret != 0)
  3394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3395. ret = wc_Sha512_224Final(&sha, hash);
  3396. }
  3397. #endif
  3398. } /* END LARGE HASH TEST */
  3399. #endif /* NO_LARGE_HASH_TEST */
  3400. exit:
  3401. wc_Sha512_224Free(&sha);
  3402. wc_Sha512_224Free(&shaCopy);
  3403. return ret;
  3404. } /* sha512_224_test */
  3405. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3406. #if !defined(WOLFSSL_NOSHA512_256) && \
  3407. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3408. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3409. {
  3410. /*
  3411. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3412. ** NIST SHA512/256 Document Example:
  3413. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3414. */
  3415. wc_Sha512 sha, shaCopy;
  3416. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3417. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3418. wc_test_ret_t ret = 0;
  3419. testVector a, b, c;
  3420. testVector test_sha[3];
  3421. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3422. WOLFSSL_ENTER("sha512_256_test");
  3423. a.input = "";
  3424. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3425. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3426. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3427. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3428. a.inLen = XSTRLEN(a.input);
  3429. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3430. /*
  3431. ** See page 1 of above document for "abc" example:
  3432. */
  3433. b.input = "abc";
  3434. /*
  3435. ** See page 4 of above document for "abc" example digest:
  3436. */
  3437. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3438. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3439. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3440. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3441. b.inLen = XSTRLEN(b.input);
  3442. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3443. /*
  3444. ** See page 4 of above document for Two Block Message Sample:
  3445. */
  3446. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3447. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3448. /*
  3449. ** See page 10 of above document for Two Block Message Sample digest:
  3450. */
  3451. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3452. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3453. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3454. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3455. c.inLen = XSTRLEN(c.input);
  3456. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3457. test_sha[0] = a;
  3458. test_sha[1] = b;
  3459. test_sha[2] = c;
  3460. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3461. if (ret != 0)
  3462. return WC_TEST_RET_ENC_EC(ret);
  3463. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3464. if (ret != 0) {
  3465. wc_Sha512_256Free(&sha);
  3466. return WC_TEST_RET_ENC_EC(ret);
  3467. }
  3468. for (i = 0; i < times; ++i) {
  3469. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3470. (word32)test_sha[i].inLen);
  3471. if (ret != 0)
  3472. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3473. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3474. if (ret != 0)
  3475. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3476. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3477. if (ret != 0)
  3478. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3479. ret = wc_Sha512_256Final(&sha, hash);
  3480. if (ret != 0)
  3481. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3482. wc_Sha512_256Free(&shaCopy);
  3483. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3484. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3485. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3486. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3487. }
  3488. #ifndef NO_LARGE_HASH_TEST
  3489. /* BEGIN LARGE HASH TEST */ {
  3490. byte large_input[1024];
  3491. #ifdef HASH_SIZE_LIMIT
  3492. const char* large_digest =
  3493. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3494. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3495. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3496. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3497. #else
  3498. const char* large_digest =
  3499. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3500. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3501. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3502. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3503. #endif
  3504. for (i = 0; i < (int)sizeof(large_input); i++) {
  3505. large_input[i] = (byte)(i & 0xFF);
  3506. }
  3507. #ifdef HASH_SIZE_LIMIT
  3508. times = 20;
  3509. #else
  3510. times = 100;
  3511. #endif
  3512. for (i = 0; i < times; ++i) {
  3513. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3514. (word32)sizeof(large_input));
  3515. if (ret != 0)
  3516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3517. }
  3518. ret = wc_Sha512_256Final(&sha, hash);
  3519. if (ret != 0)
  3520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3521. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3522. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3523. #ifndef NO_UNALIGNED_MEMORY_TEST
  3524. /* Unaligned memory access test */
  3525. for (i = 1; i < 16; i++) {
  3526. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3527. (word32)sizeof(large_input) - i);
  3528. if (ret != 0)
  3529. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3530. ret = wc_Sha512_256Final(&sha, hash);
  3531. }
  3532. #endif
  3533. } /* END LARGE HASH TEST */
  3534. #endif /* NO_LARGE_HASH_TEST */
  3535. exit:
  3536. wc_Sha512_256Free(&sha);
  3537. wc_Sha512_256Free(&shaCopy);
  3538. return ret;
  3539. } /* sha512_256_test */
  3540. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3541. #endif /* WOLFSSL_SHA512 */
  3542. #ifdef WOLFSSL_SHA384
  3543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3544. {
  3545. wc_Sha384 sha, shaCopy;
  3546. byte hash[WC_SHA384_DIGEST_SIZE];
  3547. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3548. wc_test_ret_t ret = 0;
  3549. testVector a, b, c;
  3550. testVector test_sha[3];
  3551. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3552. WOLFSSL_ENTER("sha384_test");
  3553. a.input = "";
  3554. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3555. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3556. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3557. "\x98\xb9\x5b";
  3558. a.inLen = XSTRLEN(a.input);
  3559. a.outLen = WC_SHA384_DIGEST_SIZE;
  3560. b.input = "abc";
  3561. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3562. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3563. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3564. "\xc8\x25\xa7";
  3565. b.inLen = XSTRLEN(b.input);
  3566. b.outLen = WC_SHA384_DIGEST_SIZE;
  3567. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3568. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3569. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3570. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3571. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3572. "\x74\x60\x39";
  3573. c.inLen = XSTRLEN(c.input);
  3574. c.outLen = WC_SHA384_DIGEST_SIZE;
  3575. test_sha[0] = a;
  3576. test_sha[1] = b;
  3577. test_sha[2] = c;
  3578. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3579. if (ret != 0)
  3580. return WC_TEST_RET_ENC_EC(ret);
  3581. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3582. if (ret != 0) {
  3583. wc_Sha384Free(&sha);
  3584. return WC_TEST_RET_ENC_EC(ret);
  3585. }
  3586. for (i = 0; i < times; ++i) {
  3587. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3588. (word32)test_sha[i].inLen);
  3589. if (ret != 0)
  3590. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3591. ret = wc_Sha384GetHash(&sha, hashcopy);
  3592. if (ret != 0)
  3593. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3594. ret = wc_Sha384Copy(&sha, &shaCopy);
  3595. if (ret != 0)
  3596. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3597. ret = wc_Sha384Final(&sha, hash);
  3598. if (ret != 0)
  3599. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3600. wc_Sha384Free(&shaCopy);
  3601. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3602. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3603. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3604. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3605. }
  3606. #ifndef NO_LARGE_HASH_TEST
  3607. /* BEGIN LARGE HASH TEST */ {
  3608. byte large_input[1024];
  3609. #ifdef HASH_SIZE_LIMIT
  3610. const char* large_digest =
  3611. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3612. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3613. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  3614. #else
  3615. const char* large_digest =
  3616. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  3617. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  3618. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  3619. #endif
  3620. for (i = 0; i < (int)sizeof(large_input); i++) {
  3621. large_input[i] = (byte)(i & 0xFF);
  3622. }
  3623. #ifdef HASH_SIZE_LIMIT
  3624. times = 20;
  3625. #else
  3626. times = 100;
  3627. #endif
  3628. for (i = 0; i < times; ++i) {
  3629. ret = wc_Sha384Update(&sha, (byte*)large_input,
  3630. (word32)sizeof(large_input));
  3631. if (ret != 0)
  3632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3633. }
  3634. ret = wc_Sha384Final(&sha, hash);
  3635. if (ret != 0)
  3636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3637. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  3638. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3639. } /* END LARGE HASH TEST */
  3640. #endif /* NO_LARGE_HASH_TEST */
  3641. exit:
  3642. wc_Sha384Free(&sha);
  3643. wc_Sha384Free(&shaCopy);
  3644. return ret;
  3645. }
  3646. #endif /* WOLFSSL_SHA384 */
  3647. #ifdef WOLFSSL_SHA3
  3648. #ifndef WOLFSSL_NOSHA3_224
  3649. static wc_test_ret_t sha3_224_test(void)
  3650. {
  3651. wc_Sha3 sha;
  3652. byte hash[WC_SHA3_224_DIGEST_SIZE];
  3653. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  3654. testVector a, b, c;
  3655. testVector test_sha[3];
  3656. wc_test_ret_t ret = 0;
  3657. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3658. a.input = "";
  3659. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  3660. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  3661. a.inLen = XSTRLEN(a.input);
  3662. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  3663. b.input = "abc";
  3664. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  3665. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  3666. b.inLen = XSTRLEN(b.input);
  3667. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  3668. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3669. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  3670. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  3671. c.inLen = XSTRLEN(c.input);
  3672. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  3673. test_sha[0] = a;
  3674. test_sha[1] = b;
  3675. test_sha[2] = c;
  3676. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  3677. if (ret != 0)
  3678. return WC_TEST_RET_ENC_EC(ret);
  3679. for (i = 0; i < times; ++i) {
  3680. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  3681. (word32)test_sha[i].inLen);
  3682. if (ret != 0)
  3683. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3684. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  3685. if (ret != 0)
  3686. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3687. ret = wc_Sha3_224_Final(&sha, hash);
  3688. if (ret != 0)
  3689. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3690. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  3691. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3692. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  3693. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3694. }
  3695. #ifndef NO_LARGE_HASH_TEST
  3696. /* BEGIN LARGE HASH TEST */ {
  3697. byte large_input[1024];
  3698. const char* large_digest =
  3699. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  3700. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  3701. for (i = 0; i < (int)sizeof(large_input); i++) {
  3702. large_input[i] = (byte)(i & 0xFF);
  3703. }
  3704. times = 100;
  3705. for (i = 0; i < times; ++i) {
  3706. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  3707. (word32)sizeof(large_input));
  3708. if (ret != 0)
  3709. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3710. }
  3711. ret = wc_Sha3_224_Final(&sha, hash);
  3712. if (ret != 0)
  3713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3714. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  3715. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3716. } /* END LARGE HASH TEST */
  3717. #endif /* NO_LARGE_HASH_TEST */
  3718. exit:
  3719. wc_Sha3_224_Free(&sha);
  3720. return ret;
  3721. }
  3722. #endif /* WOLFSSL_NOSHA3_224 */
  3723. #ifndef WOLFSSL_NOSHA3_256
  3724. static wc_test_ret_t sha3_256_test(void)
  3725. {
  3726. wc_Sha3 sha;
  3727. byte hash[WC_SHA3_256_DIGEST_SIZE];
  3728. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  3729. testVector a, b, c;
  3730. testVector test_sha[3];
  3731. wc_test_ret_t ret = 0;
  3732. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3733. byte large_input[1024];
  3734. const char* large_digest =
  3735. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  3736. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  3737. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3738. /* test vector with hash of empty string */
  3739. const char* Keccak256EmptyOut =
  3740. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  3741. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  3742. #endif
  3743. /*
  3744. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  3745. */
  3746. a.input = "";
  3747. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  3748. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  3749. "\x43\x4a";
  3750. a.inLen = XSTRLEN(a.input);
  3751. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  3752. b.input = "abc";
  3753. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  3754. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  3755. "\x15\x32";
  3756. b.inLen = XSTRLEN(b.input);
  3757. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  3758. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3759. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  3760. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  3761. "\x33\x76";
  3762. c.inLen = XSTRLEN(c.input);
  3763. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  3764. test_sha[0] = a;
  3765. test_sha[1] = b;
  3766. test_sha[2] = c;
  3767. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  3768. if (ret != 0)
  3769. return WC_TEST_RET_ENC_EC(ret);
  3770. for (i = 0; i < times; ++i) {
  3771. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  3772. (word32)test_sha[i].inLen);
  3773. if (ret != 0)
  3774. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3775. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  3776. if (ret != 0)
  3777. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3778. ret = wc_Sha3_256_Final(&sha, hash);
  3779. if (ret != 0)
  3780. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3781. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  3782. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3783. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  3784. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3785. }
  3786. #ifndef NO_LARGE_HASH_TEST
  3787. /* BEGIN LARGE HASH TEST */ {
  3788. for (i = 0; i < (int)sizeof(large_input); i++) {
  3789. large_input[i] = (byte)(i & 0xFF);
  3790. }
  3791. times = 100;
  3792. for (i = 0; i < times; ++i) {
  3793. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  3794. (word32)sizeof(large_input));
  3795. if (ret != 0)
  3796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3797. }
  3798. ret = wc_Sha3_256_Final(&sha, hash);
  3799. if (ret != 0)
  3800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3801. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  3802. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3803. } /* END LARGE HASH TEST */
  3804. #endif /* NO_LARGE_HASH_TEST */
  3805. /* this is a software only variant of SHA3 not supported by external hardware devices */
  3806. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3807. /* Test for Keccak256 */
  3808. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  3809. if (ret != 0) {
  3810. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3811. }
  3812. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  3813. if (ret != 0) {
  3814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3815. }
  3816. ret = wc_Sha3_256_Final(&sha, hash);
  3817. if (ret != 0) {
  3818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3819. }
  3820. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  3821. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3822. }
  3823. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  3824. exit:
  3825. wc_Sha3_256_Free(&sha);
  3826. return ret;
  3827. }
  3828. #endif /* WOLFSSL_NOSHA3_256 */
  3829. #ifndef WOLFSSL_NOSHA3_384
  3830. static wc_test_ret_t sha3_384_test(void)
  3831. {
  3832. wc_Sha3 sha;
  3833. byte hash[WC_SHA3_384_DIGEST_SIZE];
  3834. byte buf[64];
  3835. #ifndef NO_INTM_HASH_TEST
  3836. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  3837. #endif
  3838. testVector a, b, c;
  3839. testVector test_sha[3];
  3840. wc_test_ret_t ret;
  3841. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3842. /*
  3843. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  3844. */
  3845. a.input = "";
  3846. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  3847. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  3848. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  3849. "\xd5\xf0\x04";
  3850. a.inLen = XSTRLEN(a.input);
  3851. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  3852. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3853. /* NIST test vector with a length that is a multiple of 4 */
  3854. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  3855. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  3856. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  3857. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  3858. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  3859. "\x19\x87\x22";
  3860. b.inLen = XSTRLEN(b.input);
  3861. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3862. #else
  3863. b.input = "abc";
  3864. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  3865. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  3866. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  3867. "\x37\x6d\x25";
  3868. b.inLen = XSTRLEN(b.input);
  3869. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3870. #endif
  3871. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3872. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  3873. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  3874. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  3875. "\x65\x7c\x22";
  3876. c.inLen = XSTRLEN(c.input);
  3877. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  3878. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3879. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  3880. #else
  3881. test_sha[0] = a;
  3882. #endif
  3883. test_sha[1] = b;
  3884. test_sha[2] = c;
  3885. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  3886. if (ret != 0)
  3887. return WC_TEST_RET_ENC_EC(ret);
  3888. for (i = 0; i < times; ++i) {
  3889. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  3890. ret = wc_Sha3_384_Update(&sha, buf,
  3891. (word32)test_sha[i].inLen);
  3892. if (ret != 0)
  3893. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3894. #ifndef NO_INTM_HASH_TEST
  3895. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  3896. if (ret != 0)
  3897. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3898. #endif
  3899. ret = wc_Sha3_384_Final(&sha, hash);
  3900. if (ret != 0)
  3901. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3902. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  3903. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3904. #ifndef NO_INTM_HASH_TEST
  3905. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  3906. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3907. #endif
  3908. }
  3909. #ifndef NO_LARGE_HASH_TEST
  3910. /* BEGIN LARGE HASH TEST */ {
  3911. byte large_input[1024];
  3912. const char* large_digest =
  3913. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3914. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3915. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3916. for (i = 0; i < (int)sizeof(large_input); i++) {
  3917. large_input[i] = (byte)(i & 0xFF);
  3918. }
  3919. times = 100;
  3920. for (i = 0; i < times; ++i) {
  3921. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3922. (word32)sizeof(large_input));
  3923. if (ret != 0)
  3924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3925. }
  3926. ret = wc_Sha3_384_Final(&sha, hash);
  3927. if (ret != 0)
  3928. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3929. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3930. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3931. } /* END LARGE HASH TEST */
  3932. #endif /* NO_LARGE_HASH_TEST */
  3933. exit:
  3934. wc_Sha3_384_Free(&sha);
  3935. return ret;
  3936. }
  3937. #endif /* WOLFSSL_NOSHA3_384 */
  3938. #ifndef WOLFSSL_NOSHA3_512
  3939. static wc_test_ret_t sha3_512_test(void)
  3940. {
  3941. wc_Sha3 sha;
  3942. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3943. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3944. testVector a, b, c;
  3945. testVector test_sha[3];
  3946. wc_test_ret_t ret;
  3947. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3948. /*
  3949. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  3950. */
  3951. a.input = "";
  3952. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3953. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3954. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3955. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3956. "\x28\x1d\xcd\x26";
  3957. a.inLen = XSTRLEN(a.input);
  3958. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3959. b.input = "abc";
  3960. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3961. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3962. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3963. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3964. "\x4e\xec\x53\xf0";
  3965. b.inLen = XSTRLEN(b.input);
  3966. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3967. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3968. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3969. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3970. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3971. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3972. "\x39\xd6\xe7\x5e";
  3973. c.inLen = XSTRLEN(c.input);
  3974. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3975. test_sha[0] = a;
  3976. test_sha[1] = b;
  3977. test_sha[2] = c;
  3978. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3979. if (ret != 0)
  3980. return WC_TEST_RET_ENC_EC(ret);
  3981. for (i = 0; i < times; ++i) {
  3982. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3983. (word32)test_sha[i].inLen);
  3984. if (ret != 0)
  3985. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3986. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3987. if (ret != 0)
  3988. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3989. ret = wc_Sha3_512_Final(&sha, hash);
  3990. if (ret != 0)
  3991. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3992. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3993. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3994. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3995. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3996. }
  3997. #ifndef NO_LARGE_HASH_TEST
  3998. /* BEGIN LARGE HASH TEST */ {
  3999. byte large_input[1024];
  4000. const char* large_digest =
  4001. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  4002. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  4003. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  4004. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  4005. for (i = 0; i < (int)sizeof(large_input); i++) {
  4006. large_input[i] = (byte)(i & 0xFF);
  4007. }
  4008. times = 100;
  4009. for (i = 0; i < times; ++i) {
  4010. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  4011. (word32)sizeof(large_input));
  4012. if (ret != 0)
  4013. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4014. }
  4015. ret = wc_Sha3_512_Final(&sha, hash);
  4016. if (ret != 0)
  4017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4018. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  4019. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4020. } /* END LARGE HASH TEST */
  4021. #endif /* NO_LARGE_HASH_TEST */
  4022. exit:
  4023. wc_Sha3_512_Free(&sha);
  4024. return ret;
  4025. }
  4026. #endif /* WOLFSSL_NOSHA3_512 */
  4027. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  4028. {
  4029. wc_test_ret_t ret;
  4030. (void)ret;
  4031. WOLFSSL_ENTER("sha3_test");
  4032. #ifndef WOLFSSL_NOSHA3_224
  4033. if ((ret = sha3_224_test()) != 0)
  4034. return ret;
  4035. #endif
  4036. #ifndef WOLFSSL_NOSHA3_256
  4037. if ((ret = sha3_256_test()) != 0)
  4038. return ret;
  4039. #endif
  4040. #ifndef WOLFSSL_NOSHA3_384
  4041. if ((ret = sha3_384_test()) != 0)
  4042. return ret;
  4043. #endif
  4044. #ifndef WOLFSSL_NOSHA3_512
  4045. if ((ret = sha3_512_test()) != 0)
  4046. return ret;
  4047. #endif
  4048. return 0;
  4049. }
  4050. #endif /* WOLFSSL_SHA3 */
  4051. #ifdef WOLFSSL_SHAKE128
  4052. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4053. size_t large_input_buf_size)
  4054. {
  4055. testVector a, b, c, d, e;
  4056. testVector test_sha[5];
  4057. wc_test_ret_t ret = 0;
  4058. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4059. static const char large_digest[] =
  4060. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  4061. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  4062. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  4063. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  4064. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  4065. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  4066. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  4067. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  4068. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  4069. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  4070. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  4071. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  4072. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  4073. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  4074. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4075. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  4076. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  4077. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  4078. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  4079. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  4080. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  4081. byte hash[sizeof(large_digest) - 1];
  4082. /*
  4083. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4084. */
  4085. a.input = "";
  4086. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4087. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4088. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4089. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4090. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4091. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4092. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4093. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  4094. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  4095. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  4096. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  4097. "\xb0\xfc\xa9";
  4098. a.inLen = XSTRLEN(a.input);
  4099. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  4100. b.input = "abc";
  4101. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4102. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4103. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4104. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4105. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4106. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4107. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4108. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  4109. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  4110. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  4111. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  4112. "\x47\x58\x4e";
  4113. b.inLen = XSTRLEN(b.input);
  4114. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  4115. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4116. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4117. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4118. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4119. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4120. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4121. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4122. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4123. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  4124. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  4125. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  4126. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  4127. "\xb4\xc9\x54";
  4128. c.inLen = XSTRLEN(c.input);
  4129. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  4130. /* Taken from NIST CAVP test vectors - full rate output. */
  4131. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4132. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4133. "\x85\xe0";
  4134. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4135. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4136. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4137. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4138. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4139. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4140. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4141. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4142. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4143. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  4144. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  4145. "\x55\x4d\x09";
  4146. d.inLen = 32;
  4147. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  4148. /* Taken from NIST CAVP test vectors - more than one output block. */
  4149. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4150. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4151. "\x47\xe4";
  4152. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4153. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4154. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4155. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4156. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4157. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4158. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4159. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4160. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4161. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4162. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4163. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4164. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4165. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4166. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4167. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4168. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  4169. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  4170. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  4171. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  4172. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  4173. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  4174. "\xd9\xe9\xe6\xf9\xff\xa5";
  4175. e.inLen = 32;
  4176. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  4177. test_sha[0] = a;
  4178. test_sha[1] = b;
  4179. test_sha[2] = c;
  4180. test_sha[3] = d;
  4181. test_sha[4] = e;
  4182. for (i = 0; i < times; ++i) {
  4183. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4184. if (ret != 0)
  4185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4186. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  4187. (word32)test_sha[i].inLen);
  4188. if (ret != 0)
  4189. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4190. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4191. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  4192. if (ret != 0)
  4193. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4194. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4195. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4196. }
  4197. #ifndef NO_LARGE_HASH_TEST
  4198. /* BEGIN LARGE HASH TEST */ {
  4199. for (i = 0; i < (int)large_input_buf_size; i++) {
  4200. large_input_buf[i] = (byte)(i & 0xFF);
  4201. }
  4202. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4203. if (ret != 0)
  4204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4205. /* Absorb is non-incremental. */
  4206. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  4207. (word32)large_input_buf_size);
  4208. if (ret != 0)
  4209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4210. /* Able to squeeze out blocks incrementally. */
  4211. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  4212. if (ret != 0)
  4213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4214. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4215. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  4216. if (ret != 0)
  4217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4218. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4219. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4220. } /* END LARGE HASH TEST */
  4221. #endif /* NO_LARGE_HASH_TEST */
  4222. exit:
  4223. return ret;
  4224. }
  4225. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  4226. {
  4227. wc_Shake sha;
  4228. byte hash[250];
  4229. testVector a, b, c, d, e;
  4230. testVector test_sha[5];
  4231. wc_test_ret_t ret = 0;
  4232. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4233. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  4234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4235. byte *large_input = NULL;
  4236. #else
  4237. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  4238. #endif
  4239. static const char large_digest[] =
  4240. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  4241. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  4242. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  4243. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  4244. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  4245. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  4246. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  4247. "\xfa\x1b";
  4248. WOLFSSL_ENTER("shake128_test");
  4249. /*
  4250. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4251. */
  4252. a.input = "";
  4253. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4254. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4255. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4256. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4257. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4258. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4259. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4260. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  4261. a.inLen = XSTRLEN(a.input);
  4262. a.outLen = 114;
  4263. b.input = "abc";
  4264. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4265. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4266. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4267. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4268. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4269. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4270. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4271. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  4272. b.inLen = XSTRLEN(b.input);
  4273. b.outLen = 114;
  4274. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4275. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4276. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4277. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4278. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4279. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4280. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4281. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4282. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  4283. c.inLen = XSTRLEN(c.input);
  4284. c.outLen = 114;
  4285. /* Taken from NIST CAVP test vectors - full rate output. */
  4286. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4287. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4288. "\x85\xe0";
  4289. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4290. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4291. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4292. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4293. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4294. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4295. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4296. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4297. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4298. "\xa1";
  4299. d.inLen = 32;
  4300. d.outLen = 136;
  4301. /* Taken from NIST CAVP test vectors - more than one output block. */
  4302. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4303. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4304. "\x47\xe4";
  4305. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4306. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4307. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4308. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4309. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4310. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4311. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4312. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4313. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4314. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4315. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4316. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4317. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4318. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4319. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4320. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4321. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4322. e.inLen = 32;
  4323. e.outLen = 250;
  4324. test_sha[0] = a;
  4325. test_sha[1] = b;
  4326. test_sha[2] = c;
  4327. test_sha[3] = d;
  4328. test_sha[4] = e;
  4329. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4330. if (ret != 0)
  4331. return WC_TEST_RET_ENC_EC(ret);
  4332. for (i = 0; i < times; ++i) {
  4333. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4334. (word32)test_sha[i].inLen);
  4335. if (ret != 0)
  4336. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4337. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4338. if (ret != 0)
  4339. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4340. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4341. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4342. }
  4343. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4344. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4345. DYNAMIC_TYPE_TMP_BUFFER);
  4346. if (large_input == NULL)
  4347. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4348. #endif
  4349. #ifndef NO_LARGE_HASH_TEST
  4350. /* BEGIN LARGE HASH TEST */ {
  4351. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4352. large_input[i] = (byte)(i & 0xFF);
  4353. }
  4354. times = 100;
  4355. for (i = 0; i < times; ++i) {
  4356. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4357. SHAKE128_LARGE_INPUT_BUFSIZ);
  4358. if (ret != 0)
  4359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4360. }
  4361. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4362. if (ret != 0)
  4363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4364. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4365. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4366. } /* END LARGE HASH TEST */
  4367. #endif /* NO_LARGE_HASH_TEST */
  4368. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4369. exit:
  4370. wc_Shake128_Free(&sha);
  4371. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4372. if (large_input != NULL)
  4373. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4374. #endif
  4375. return ret;
  4376. }
  4377. #endif
  4378. #ifdef WOLFSSL_SHAKE256
  4379. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4380. size_t large_input_buf_size)
  4381. {
  4382. testVector a, b, c, d, e;
  4383. testVector test_sha[5];
  4384. wc_test_ret_t ret = 0;
  4385. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4386. static const char large_digest[] =
  4387. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4388. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4389. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4390. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4391. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4392. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4393. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4394. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4395. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4396. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4397. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4398. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4399. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4400. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4401. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4402. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4403. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4404. byte hash[sizeof(large_digest) - 1];
  4405. /*
  4406. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4407. */
  4408. a.input = "";
  4409. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4410. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4411. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4412. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4413. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4414. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4415. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4416. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4417. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4418. "\xdd";
  4419. a.inLen = XSTRLEN(a.input);
  4420. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4421. b.input = "abc";
  4422. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4423. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4424. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4425. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4426. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4427. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4428. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4429. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4430. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4431. "\x29";
  4432. b.inLen = XSTRLEN(b.input);
  4433. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4434. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4435. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4436. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4437. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4438. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4439. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4440. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4441. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4442. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4443. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4444. "\x8d";
  4445. c.inLen = XSTRLEN(c.input);
  4446. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4447. /* Taken from NIST CAVP test vectors - full rate output. */
  4448. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4449. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4450. "\x85\xe0";
  4451. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4452. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4453. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4454. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4455. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4456. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4457. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4458. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4459. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4460. "\xc2";
  4461. d.inLen = 32;
  4462. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4463. /* Taken from NIST CAVP test vectors - more than one output block. */
  4464. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4465. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4466. "\x47\xe4";
  4467. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4468. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4469. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4470. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4471. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4472. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4473. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4474. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4475. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4476. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4477. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4478. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4479. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4480. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4481. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4482. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4483. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4484. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4485. "\x68\x44";
  4486. e.inLen = 32;
  4487. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4488. test_sha[0] = a;
  4489. test_sha[1] = b;
  4490. test_sha[2] = c;
  4491. test_sha[3] = d;
  4492. test_sha[4] = e;
  4493. for (i = 0; i < times; ++i) {
  4494. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4495. if (ret != 0)
  4496. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4497. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4498. (word32)test_sha[i].inLen);
  4499. if (ret != 0)
  4500. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4501. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4502. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4503. if (ret != 0)
  4504. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4505. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4506. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4507. }
  4508. #ifndef NO_LARGE_HASH_TEST
  4509. /* BEGIN LARGE HASH TEST */ {
  4510. for (i = 0; i < (int)large_input_buf_size; i++) {
  4511. large_input_buf[i] = (byte)(i & 0xFF);
  4512. }
  4513. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4514. if (ret != 0)
  4515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4516. /* Absorb is non-incremental. */
  4517. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4518. (word32)large_input_buf_size);
  4519. if (ret != 0)
  4520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4521. /* Able to squeeze out blocks incrementally. */
  4522. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4523. if (ret != 0)
  4524. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4525. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4526. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4527. if (ret != 0)
  4528. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4529. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4530. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4531. } /* END LARGE HASH TEST */
  4532. #endif /* NO_LARGE_HASH_TEST */
  4533. exit:
  4534. return ret;
  4535. }
  4536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4537. {
  4538. wc_Shake sha;
  4539. byte hash[250];
  4540. testVector a, b, c, d, e;
  4541. testVector test_sha[5];
  4542. wc_test_ret_t ret = 0;
  4543. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4544. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4545. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4546. byte *large_input = NULL;
  4547. #else
  4548. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4549. #endif
  4550. const char* large_digest =
  4551. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4552. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4553. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4554. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4555. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4556. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4557. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4558. "\xea\x26";
  4559. WOLFSSL_ENTER("shake256_test");
  4560. /*
  4561. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4562. */
  4563. a.input = "";
  4564. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4565. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4566. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4567. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4568. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4569. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4570. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4571. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4572. a.inLen = XSTRLEN(a.input);
  4573. a.outLen = 114;
  4574. b.input = "abc";
  4575. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4576. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4577. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4578. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4579. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4580. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4581. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4582. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4583. b.inLen = XSTRLEN(b.input);
  4584. b.outLen = 114;
  4585. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4586. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4587. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4588. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4589. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4590. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4591. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4592. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4593. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4594. c.inLen = XSTRLEN(c.input);
  4595. c.outLen = 114;
  4596. /* Taken from NIST CAVP test vectors - full rate output. */
  4597. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4598. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4599. "\x85\xe0";
  4600. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4601. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4602. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4603. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4604. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4605. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4606. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4607. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4608. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4609. "\xc2";
  4610. d.inLen = 32;
  4611. d.outLen = 136;
  4612. /* Taken from NIST CAVP test vectors - more than one output block. */
  4613. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4614. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4615. "\x47\xe4";
  4616. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4617. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4618. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4619. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4620. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4621. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4622. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4623. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4624. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4625. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4626. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4627. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4628. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4629. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4630. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4631. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4632. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  4633. e.inLen = 32;
  4634. e.outLen = 250;
  4635. test_sha[0] = a;
  4636. test_sha[1] = b;
  4637. test_sha[2] = c;
  4638. test_sha[3] = d;
  4639. test_sha[4] = e;
  4640. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  4641. if (ret != 0)
  4642. return WC_TEST_RET_ENC_EC(ret);
  4643. for (i = 0; i < times; ++i) {
  4644. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  4645. (word32)test_sha[i].inLen);
  4646. if (ret != 0)
  4647. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4648. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  4649. if (ret != 0)
  4650. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4651. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4652. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4653. }
  4654. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4655. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  4656. DYNAMIC_TYPE_TMP_BUFFER);
  4657. if (large_input == NULL)
  4658. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4659. #endif
  4660. #ifndef NO_LARGE_HASH_TEST
  4661. /* BEGIN LARGE HASH TEST */ {
  4662. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  4663. large_input[i] = (byte)(i & 0xFF);
  4664. }
  4665. times = 100;
  4666. for (i = 0; i < times; ++i) {
  4667. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  4668. SHAKE256_LARGE_INPUT_BUFSIZ);
  4669. if (ret != 0)
  4670. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4671. }
  4672. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  4673. if (ret != 0)
  4674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4675. if (XMEMCMP(hash, large_digest, 114) != 0)
  4676. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4677. } /* END LARGE HASH TEST */
  4678. #endif /* NO_LARGE_HASH_TEST */
  4679. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  4680. exit:
  4681. wc_Shake256_Free(&sha);
  4682. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4683. if (large_input != NULL)
  4684. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4685. #endif
  4686. return ret;
  4687. }
  4688. #endif
  4689. #ifdef WOLFSSL_SM3
  4690. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  4691. {
  4692. wc_Sm3 sm3, sm3Copy;
  4693. byte hash[WC_SM3_DIGEST_SIZE];
  4694. byte hashGet[WC_SM3_DIGEST_SIZE];
  4695. byte hashCopy[WC_SM3_DIGEST_SIZE];
  4696. wc_test_ret_t ret = 0;
  4697. WOLFSSL_ENTER("sm3_test");
  4698. testVector a, b, c;
  4699. testVector test_sm3[3];
  4700. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  4701. a.input = "";
  4702. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  4703. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  4704. "\xaa\x2b";
  4705. a.inLen = XSTRLEN(a.input);
  4706. a.outLen = WC_SM3_DIGEST_SIZE;
  4707. b.input = "abc";
  4708. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  4709. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  4710. "\xa8\xe0";
  4711. b.inLen = XSTRLEN(b.input);
  4712. b.outLen = WC_SM3_DIGEST_SIZE;
  4713. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4714. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  4715. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  4716. "\x8c\x05";
  4717. c.inLen = XSTRLEN(c.input);
  4718. c.outLen = WC_SM3_DIGEST_SIZE;
  4719. test_sm3[0] = a;
  4720. test_sm3[1] = b;
  4721. test_sm3[2] = c;
  4722. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  4723. if (ret != 0)
  4724. return WC_TEST_RET_ENC_EC(ret);
  4725. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  4726. if (ret != 0) {
  4727. wc_Sm3Free(&sm3);
  4728. return WC_TEST_RET_ENC_EC(ret);
  4729. }
  4730. /* Test all the KATs. */
  4731. for (i = 0; i < times; ++i) {
  4732. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  4733. (word32)test_sm3[i].inLen);
  4734. if (ret != 0) {
  4735. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4736. }
  4737. /* Get the final hash but leave ready for more updates. */
  4738. ret = wc_Sm3GetHash(&sm3, hashGet);
  4739. if (ret != 0)
  4740. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4741. /* Make a copy of the hash. */
  4742. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  4743. if (ret != 0)
  4744. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4745. /* Get the final hash with original. */
  4746. ret = wc_Sm3Final(&sm3, hash);
  4747. if (ret != 0)
  4748. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4749. /* Get the final hash with copy. */
  4750. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  4751. if (ret != 0)
  4752. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4753. /* Dispose of copy. */
  4754. wc_Sm3Free(&sm3Copy);
  4755. /* Check hashes match expected. */
  4756. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  4757. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4758. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  4759. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4760. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  4761. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4762. }
  4763. #ifndef NO_LARGE_HASH_TEST
  4764. {
  4765. word32 sz;
  4766. byte large_input[1024];
  4767. #ifdef HASH_SIZE_LIMIT
  4768. const char* large_digest =
  4769. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  4770. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  4771. times = 20;
  4772. #else
  4773. const char* large_digest =
  4774. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  4775. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  4776. times = 100;
  4777. #endif
  4778. /* Set large input to something. */
  4779. for (i = 0; i < (int)sizeof(large_input); i++) {
  4780. large_input[i] = (byte)(i & 0xFF);
  4781. }
  4782. /* Hash a large number of times. */
  4783. for (i = 0; i < times; ++i) {
  4784. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  4785. (word32)sizeof(large_input));
  4786. if (ret != 0)
  4787. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4788. }
  4789. /* Calculate hash and compare to expected. */
  4790. ret = wc_Sm3Final(&sm3, hash);
  4791. if (ret != 0)
  4792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4793. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4794. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4795. /* Check updating with various sizes works. */
  4796. for (sz = 1; sz <= 64; sz++) {
  4797. /* Hash a large number of times. */
  4798. for (i = 0; i < times; ++i) {
  4799. word32 o;
  4800. /* Update sz bytes at a time from large input buffer. */
  4801. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  4802. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  4803. if (ret != 0)
  4804. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  4805. }
  4806. /* Check for left-overs. */
  4807. if (o < (word32)sizeof(large_input)) {
  4808. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  4809. (word32)sizeof(large_input) - o);
  4810. if (ret != 0)
  4811. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4812. }
  4813. }
  4814. /* Calculate hash and compare to expected. */
  4815. ret = wc_Sm3Final(&sm3, hash);
  4816. if (ret != 0)
  4817. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4818. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4819. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4820. }
  4821. }
  4822. #endif /* NO_LARGE_HASH_TEST */
  4823. exit:
  4824. wc_Sm3Free(&sm3);
  4825. wc_Sm3Free(&sm3Copy);
  4826. return ret;
  4827. }
  4828. #endif
  4829. #ifndef NO_HASH_WRAPPER
  4830. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  4831. {
  4832. wc_HashAlg hash;
  4833. int ret, exp_ret;
  4834. int i, j;
  4835. int digestSz;
  4836. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  4837. byte out[WC_MAX_DIGEST_SIZE];
  4838. byte hashOut[WC_MAX_DIGEST_SIZE];
  4839. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4840. enum wc_HashType hashType;
  4841. #endif
  4842. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  4843. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  4844. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  4845. WC_HASH_TYPE_SHA3_224,
  4846. WC_HASH_TYPE_SHA3_256,
  4847. WC_HASH_TYPE_SHA3_384,
  4848. WC_HASH_TYPE_SHA3_512 };
  4849. enum wc_HashType typesNoImpl[] = {
  4850. #ifdef NO_MD5
  4851. WC_HASH_TYPE_MD5,
  4852. #endif
  4853. #ifdef NO_SHA
  4854. WC_HASH_TYPE_SHA,
  4855. #endif
  4856. #ifndef WOLFSSL_SHA224
  4857. WC_HASH_TYPE_SHA224,
  4858. #endif
  4859. #ifdef NO_SHA256
  4860. WC_HASH_TYPE_SHA256,
  4861. #endif
  4862. #ifndef WOLFSSL_SHA384
  4863. WC_HASH_TYPE_SHA384,
  4864. #endif
  4865. #ifndef WOLFSSL_SHA512
  4866. WC_HASH_TYPE_SHA512,
  4867. #endif
  4868. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  4869. WC_HASH_TYPE_SHA3_224,
  4870. #endif
  4871. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  4872. WC_HASH_TYPE_SHA3_256,
  4873. #endif
  4874. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  4875. WC_HASH_TYPE_SHA3_384,
  4876. #endif
  4877. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  4878. WC_HASH_TYPE_SHA3_512,
  4879. #endif
  4880. WC_HASH_TYPE_NONE
  4881. };
  4882. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  4883. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  4884. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  4885. WC_HASH_TYPE_BLAKE2B,
  4886. WC_HASH_TYPE_NONE };
  4887. WOLFSSL_ENTER("hash_test");
  4888. /* Parameter Validation testing. */
  4889. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  4890. if (ret != BAD_FUNC_ARG)
  4891. return WC_TEST_RET_ENC_EC(ret);
  4892. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4893. if (ret != BAD_FUNC_ARG)
  4894. return WC_TEST_RET_ENC_EC(ret);
  4895. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4896. if (ret != BAD_FUNC_ARG)
  4897. return WC_TEST_RET_ENC_EC(ret);
  4898. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  4899. if (ret != BAD_FUNC_ARG)
  4900. return WC_TEST_RET_ENC_EC(ret);
  4901. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  4902. if (ret != BAD_FUNC_ARG)
  4903. return WC_TEST_RET_ENC_EC(ret);
  4904. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  4905. if (ret != BAD_FUNC_ARG)
  4906. return WC_TEST_RET_ENC_EC(ret);
  4907. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  4908. if (ret != BAD_FUNC_ARG)
  4909. return WC_TEST_RET_ENC_EC(ret);
  4910. /* Try invalid hash algorithms. */
  4911. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  4912. ret = wc_HashInit(&hash, typesBad[i]);
  4913. if (ret != BAD_FUNC_ARG)
  4914. return WC_TEST_RET_ENC_I(i);
  4915. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  4916. if (ret != BAD_FUNC_ARG)
  4917. return WC_TEST_RET_ENC_I(i);
  4918. ret = wc_HashFinal(&hash, typesBad[i], out);
  4919. if (ret != BAD_FUNC_ARG)
  4920. return WC_TEST_RET_ENC_I(i);
  4921. wc_HashFree(&hash, typesBad[i]);
  4922. }
  4923. /* Try valid hash algorithms. */
  4924. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  4925. exp_ret = 0;
  4926. if (typesGood[i] == typesNoImpl[j]) {
  4927. /* Recognized but no implementation compiled in. */
  4928. exp_ret = HASH_TYPE_E;
  4929. j++;
  4930. }
  4931. ret = wc_HashInit(&hash, typesGood[i]);
  4932. if (ret != exp_ret)
  4933. return WC_TEST_RET_ENC_I(i);
  4934. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  4935. if (ret != exp_ret)
  4936. return WC_TEST_RET_ENC_I(i);
  4937. ret = wc_HashFinal(&hash, typesGood[i], out);
  4938. if (ret != exp_ret)
  4939. return WC_TEST_RET_ENC_I(i);
  4940. wc_HashFree(&hash, typesGood[i]);
  4941. digestSz = wc_HashGetDigestSize(typesGood[i]);
  4942. if (exp_ret < 0 && digestSz != exp_ret)
  4943. return WC_TEST_RET_ENC_I(i);
  4944. if (exp_ret == 0 && digestSz < 0)
  4945. return WC_TEST_RET_ENC_I(i);
  4946. if (exp_ret == 0) {
  4947. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  4948. digestSz - 1);
  4949. if (ret != BUFFER_E)
  4950. return WC_TEST_RET_ENC_I(i);
  4951. }
  4952. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  4953. if (ret != exp_ret)
  4954. return WC_TEST_RET_ENC_I(i);
  4955. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  4956. return WC_TEST_RET_ENC_I(i);
  4957. ret = wc_HashGetBlockSize(typesGood[i]);
  4958. if (exp_ret < 0 && ret != exp_ret)
  4959. return WC_TEST_RET_ENC_I(i);
  4960. if (exp_ret == 0 && ret < 0)
  4961. return WC_TEST_RET_ENC_I(i);
  4962. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4963. ret = wc_HashGetOID(typesGood[i]);
  4964. if (ret == BAD_FUNC_ARG ||
  4965. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  4966. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  4967. return WC_TEST_RET_ENC_I(i);
  4968. }
  4969. hashType = wc_OidGetHash(ret);
  4970. if (exp_ret == 0 && hashType != typesGood[i])
  4971. return WC_TEST_RET_ENC_I(i);
  4972. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4973. }
  4974. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  4975. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  4976. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  4977. return WC_TEST_RET_ENC_I(i);
  4978. }
  4979. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4980. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  4981. #ifdef WOLFSSL_MD2
  4982. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4983. return WC_TEST_RET_ENC_EC(ret);
  4984. #else
  4985. if (ret != HASH_TYPE_E)
  4986. return WC_TEST_RET_ENC_EC(ret);
  4987. #endif
  4988. hashType = wc_OidGetHash(646); /* Md2h */
  4989. #ifdef WOLFSSL_MD2
  4990. if (hashType != WC_HASH_TYPE_MD2)
  4991. return WC_TEST_RET_ENC_NC;
  4992. #else
  4993. if (hashType != WC_HASH_TYPE_NONE)
  4994. return WC_TEST_RET_ENC_NC;
  4995. #endif
  4996. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  4997. #ifndef NO_MD5
  4998. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4999. return WC_TEST_RET_ENC_EC(ret);
  5000. #else
  5001. if (ret != HASH_TYPE_E)
  5002. return WC_TEST_RET_ENC_EC(ret);
  5003. #endif
  5004. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  5005. if (ret != BAD_FUNC_ARG)
  5006. return WC_TEST_RET_ENC_EC(ret);
  5007. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  5008. if (ret != BAD_FUNC_ARG)
  5009. return WC_TEST_RET_ENC_EC(ret);
  5010. hashType = wc_OidGetHash(0);
  5011. if (hashType != WC_HASH_TYPE_NONE)
  5012. return WC_TEST_RET_ENC_NC;
  5013. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5014. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  5015. #ifdef WOLFSSL_MD2
  5016. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5017. return WC_TEST_RET_ENC_EC(ret);
  5018. #else
  5019. if (ret != HASH_TYPE_E)
  5020. return WC_TEST_RET_ENC_EC(ret);
  5021. #endif
  5022. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  5023. #ifdef WOLFSSL_MD2
  5024. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5025. return WC_TEST_RET_ENC_EC(ret);
  5026. #else
  5027. if (ret != HASH_TYPE_E)
  5028. return WC_TEST_RET_ENC_EC(ret);
  5029. #endif
  5030. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  5031. #ifndef NO_MD4
  5032. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5033. return WC_TEST_RET_ENC_EC(ret);
  5034. #else
  5035. if (ret != HASH_TYPE_E)
  5036. return WC_TEST_RET_ENC_EC(ret);
  5037. #endif
  5038. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  5039. #ifndef NO_MD4
  5040. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5041. return WC_TEST_RET_ENC_EC(ret);
  5042. #else
  5043. if (ret != HASH_TYPE_E)
  5044. return WC_TEST_RET_ENC_EC(ret);
  5045. #endif
  5046. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  5047. #if !defined(NO_MD5) && !defined(NO_SHA)
  5048. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5049. return WC_TEST_RET_ENC_EC(ret);
  5050. #else
  5051. if (ret != HASH_TYPE_E)
  5052. return WC_TEST_RET_ENC_EC(ret);
  5053. #endif
  5054. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  5055. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5056. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5057. return WC_TEST_RET_ENC_EC(ret);
  5058. #else
  5059. if (ret != HASH_TYPE_E)
  5060. return WC_TEST_RET_ENC_EC(ret);
  5061. #endif
  5062. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  5063. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5064. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5065. return WC_TEST_RET_ENC_EC(ret);
  5066. #else
  5067. if (ret != HASH_TYPE_E)
  5068. return WC_TEST_RET_ENC_EC(ret);
  5069. #endif
  5070. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  5071. if (ret != BAD_FUNC_ARG)
  5072. return WC_TEST_RET_ENC_EC(ret);
  5073. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  5074. if (ret != BAD_FUNC_ARG)
  5075. return WC_TEST_RET_ENC_EC(ret);
  5076. #if !defined(NO_CERTS) && !defined(NO_ASN)
  5077. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5078. ret = wc_GetCTC_HashOID(MD2);
  5079. if (ret == 0)
  5080. return WC_TEST_RET_ENC_EC(ret);
  5081. #endif
  5082. #ifndef NO_MD5
  5083. ret = wc_GetCTC_HashOID(WC_MD5);
  5084. if (ret == 0)
  5085. return WC_TEST_RET_ENC_EC(ret);
  5086. #endif
  5087. #ifndef NO_SHA
  5088. ret = wc_GetCTC_HashOID(WC_SHA);
  5089. if (ret == 0)
  5090. return WC_TEST_RET_ENC_EC(ret);
  5091. #endif
  5092. #ifdef WOLFSSL_SHA224
  5093. ret = wc_GetCTC_HashOID(WC_SHA224);
  5094. if (ret == 0)
  5095. return WC_TEST_RET_ENC_EC(ret);
  5096. #endif
  5097. #ifndef NO_SHA256
  5098. ret = wc_GetCTC_HashOID(WC_SHA256);
  5099. if (ret == 0)
  5100. return WC_TEST_RET_ENC_EC(ret);
  5101. #endif
  5102. #ifdef WOLFSSL_SHA384
  5103. ret = wc_GetCTC_HashOID(WC_SHA384);
  5104. if (ret == 0)
  5105. return WC_TEST_RET_ENC_EC(ret);
  5106. #endif
  5107. #ifdef WOLFSSL_SHA512
  5108. ret = wc_GetCTC_HashOID(WC_SHA512);
  5109. if (ret == 0)
  5110. return WC_TEST_RET_ENC_EC(ret);
  5111. #endif
  5112. ret = wc_GetCTC_HashOID(-1);
  5113. if (ret != 0)
  5114. return WC_TEST_RET_ENC_EC(ret);
  5115. #endif
  5116. return 0;
  5117. }
  5118. #endif /* !NO_HASH_WRAPPER */
  5119. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  5120. defined(HAVE_FIPS_VERSION) && \
  5121. (HAVE_FIPS_VERSION >= 5))
  5122. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  5123. {
  5124. Hmac hmac;
  5125. byte hash[WC_MD5_DIGEST_SIZE];
  5126. const char* keys[]=
  5127. {
  5128. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5129. "Jefe",
  5130. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5131. };
  5132. testVector a, b, c;
  5133. testVector test_hmac[3];
  5134. wc_test_ret_t ret;
  5135. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5136. WOLFSSL_ENTER("hmac_md5_test");
  5137. a.input = "Hi There";
  5138. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5139. "\x9d";
  5140. a.inLen = XSTRLEN(a.input);
  5141. a.outLen = WC_MD5_DIGEST_SIZE;
  5142. b.input = "what do ya want for nothing?";
  5143. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  5144. "\x38";
  5145. b.inLen = XSTRLEN(b.input);
  5146. b.outLen = WC_MD5_DIGEST_SIZE;
  5147. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5148. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5149. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5150. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5151. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  5152. "\xf6";
  5153. c.inLen = XSTRLEN(c.input);
  5154. c.outLen = WC_MD5_DIGEST_SIZE;
  5155. test_hmac[0] = a;
  5156. test_hmac[1] = b;
  5157. test_hmac[2] = c;
  5158. for (i = 0; i < times; ++i) {
  5159. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5160. if (i == 1) {
  5161. continue; /* cavium can't handle short keys, fips not allowed */
  5162. }
  5163. #endif
  5164. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  5165. if (ret != 0)
  5166. return WC_TEST_RET_ENC_EC(ret);
  5167. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  5168. (word32)XSTRLEN(keys[i]));
  5169. if (ret != 0)
  5170. return WC_TEST_RET_ENC_EC(ret);
  5171. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5172. (word32)test_hmac[i].inLen);
  5173. if (ret != 0)
  5174. return WC_TEST_RET_ENC_EC(ret);
  5175. ret = wc_HmacFinal(&hmac, hash);
  5176. if (ret != 0)
  5177. return WC_TEST_RET_ENC_EC(ret);
  5178. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  5179. return WC_TEST_RET_ENC_I(i);
  5180. wc_HmacFree(&hmac);
  5181. }
  5182. #ifndef HAVE_FIPS
  5183. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  5184. return WC_TEST_RET_ENC_EC(ret);
  5185. #endif
  5186. return 0;
  5187. }
  5188. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  5189. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5190. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  5191. {
  5192. Hmac hmac;
  5193. byte hash[WC_SHA_DIGEST_SIZE];
  5194. const char* keys[]=
  5195. {
  5196. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5197. "\x0b\x0b\x0b",
  5198. "Jefe",
  5199. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5200. "\xAA\xAA\xAA"
  5201. };
  5202. testVector a, b, c;
  5203. testVector test_hmac[3];
  5204. wc_test_ret_t ret;
  5205. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5206. WOLFSSL_ENTER("hmac_sha_test");
  5207. a.input = "Hi There";
  5208. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5209. "\x8e\xf1\x46\xbe\x00";
  5210. a.inLen = XSTRLEN(a.input);
  5211. a.outLen = WC_SHA_DIGEST_SIZE;
  5212. b.input = "what do ya want for nothing?";
  5213. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  5214. "\x9c\x25\x9a\x7c\x79";
  5215. b.inLen = XSTRLEN(b.input);
  5216. b.outLen = WC_SHA_DIGEST_SIZE;
  5217. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5218. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5219. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5220. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5221. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  5222. "\x4f\x63\xf1\x75\xd3";
  5223. c.inLen = XSTRLEN(c.input);
  5224. c.outLen = WC_SHA_DIGEST_SIZE;
  5225. test_hmac[0] = a;
  5226. test_hmac[1] = b;
  5227. test_hmac[2] = c;
  5228. for (i = 0; i < times; ++i) {
  5229. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5230. if (i == 1)
  5231. continue; /* cavium can't handle short keys, fips not allowed */
  5232. #endif
  5233. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5234. return WC_TEST_RET_ENC_EC(ret);
  5235. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  5236. (word32)XSTRLEN(keys[i]));
  5237. if (ret != 0)
  5238. return WC_TEST_RET_ENC_EC(ret);
  5239. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5240. (word32)test_hmac[i].inLen);
  5241. if (ret != 0)
  5242. return WC_TEST_RET_ENC_EC(ret);
  5243. ret = wc_HmacFinal(&hmac, hash);
  5244. if (ret != 0)
  5245. return WC_TEST_RET_ENC_EC(ret);
  5246. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  5247. return WC_TEST_RET_ENC_I(i);
  5248. wc_HmacFree(&hmac);
  5249. }
  5250. #ifndef HAVE_FIPS
  5251. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  5252. return WC_TEST_RET_ENC_EC(ret);
  5253. #endif
  5254. return 0;
  5255. }
  5256. #endif
  5257. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5258. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  5259. {
  5260. Hmac hmac;
  5261. byte hash[WC_SHA224_DIGEST_SIZE];
  5262. const char* keys[]=
  5263. {
  5264. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5265. "\x0b\x0b\x0b",
  5266. "Jefe",
  5267. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5268. "\xAA\xAA\xAA",
  5269. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5270. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5271. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5272. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5273. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5274. };
  5275. testVector a, b, c, d;
  5276. testVector test_hmac[4];
  5277. wc_test_ret_t ret;
  5278. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5279. WOLFSSL_ENTER("hmac_sha224_test");
  5280. a.input = "Hi There";
  5281. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5282. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5283. a.inLen = XSTRLEN(a.input);
  5284. a.outLen = WC_SHA224_DIGEST_SIZE;
  5285. b.input = "what do ya want for nothing?";
  5286. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  5287. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  5288. b.inLen = XSTRLEN(b.input);
  5289. b.outLen = WC_SHA224_DIGEST_SIZE;
  5290. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5291. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5292. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5293. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5294. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  5295. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  5296. c.inLen = XSTRLEN(c.input);
  5297. c.outLen = WC_SHA224_DIGEST_SIZE;
  5298. d.input = "Big Key Input";
  5299. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  5300. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  5301. d.inLen = XSTRLEN(d.input);
  5302. d.outLen = WC_SHA224_DIGEST_SIZE;
  5303. test_hmac[0] = a;
  5304. test_hmac[1] = b;
  5305. test_hmac[2] = c;
  5306. test_hmac[3] = d;
  5307. for (i = 0; i < times; ++i) {
  5308. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5309. if (i == 1)
  5310. continue; /* cavium can't handle short keys, fips not allowed */
  5311. #endif
  5312. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5313. return WC_TEST_RET_ENC_EC(ret);
  5314. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5315. (word32)XSTRLEN(keys[i]));
  5316. if (ret != 0)
  5317. return WC_TEST_RET_ENC_EC(ret);
  5318. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5319. (word32)test_hmac[i].inLen);
  5320. if (ret != 0)
  5321. return WC_TEST_RET_ENC_EC(ret);
  5322. ret = wc_HmacFinal(&hmac, hash);
  5323. if (ret != 0)
  5324. return WC_TEST_RET_ENC_EC(ret);
  5325. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5326. return WC_TEST_RET_ENC_I(i);
  5327. wc_HmacFree(&hmac);
  5328. }
  5329. #ifndef HAVE_FIPS
  5330. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5331. return WC_TEST_RET_ENC_EC(ret);
  5332. #endif
  5333. return 0;
  5334. }
  5335. #endif
  5336. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5337. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5338. {
  5339. Hmac hmac;
  5340. byte hash[WC_SHA256_DIGEST_SIZE];
  5341. const char* keys[]=
  5342. {
  5343. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5344. "\x0b\x0b\x0b",
  5345. "Jefe",
  5346. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5347. "\xAA\xAA\xAA",
  5348. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5349. "\xAA\xAA\xAA",
  5350. };
  5351. testVector a, b, c, d;
  5352. testVector test_hmac[4];
  5353. wc_test_ret_t ret;
  5354. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5355. WOLFSSL_ENTER("hmac_sha256_test");
  5356. a.input = "Hi There";
  5357. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5358. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5359. "\xcf\xf7";
  5360. a.inLen = XSTRLEN(a.input);
  5361. a.outLen = WC_SHA256_DIGEST_SIZE;
  5362. b.input = "what do ya want for nothing?";
  5363. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5364. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5365. "\x38\x43";
  5366. b.inLen = XSTRLEN(b.input);
  5367. b.outLen = WC_SHA256_DIGEST_SIZE;
  5368. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5369. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5370. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5371. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5372. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5373. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5374. "\x65\xfe";
  5375. c.inLen = XSTRLEN(c.input);
  5376. c.outLen = WC_SHA256_DIGEST_SIZE;
  5377. d.input = 0;
  5378. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5379. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5380. "\x3e\x46";
  5381. d.inLen = 0;
  5382. d.outLen = WC_SHA256_DIGEST_SIZE;
  5383. test_hmac[0] = a;
  5384. test_hmac[1] = b;
  5385. test_hmac[2] = c;
  5386. test_hmac[3] = d;
  5387. for (i = 0; i < times; ++i) {
  5388. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5389. if (i == 1)
  5390. continue; /* cavium can't handle short keys, fips not allowed */
  5391. #endif
  5392. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5393. if (i == 3)
  5394. continue; /* QuickAssist can't handle empty HMAC */
  5395. #endif
  5396. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5397. return WC_TEST_RET_ENC_I(i);
  5398. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5399. (word32)XSTRLEN(keys[i]));
  5400. if (ret != 0)
  5401. return WC_TEST_RET_ENC_I(i);
  5402. if (test_hmac[i].input != NULL) {
  5403. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5404. (word32)test_hmac[i].inLen);
  5405. if (ret != 0)
  5406. return WC_TEST_RET_ENC_I(i);
  5407. }
  5408. ret = wc_HmacFinal(&hmac, hash);
  5409. if (ret != 0)
  5410. return WC_TEST_RET_ENC_I(i);
  5411. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5412. return WC_TEST_RET_ENC_I(i);
  5413. wc_HmacFree(&hmac);
  5414. }
  5415. #ifndef HAVE_FIPS
  5416. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5417. return WC_TEST_RET_ENC_EC(ret);
  5418. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5419. return WC_TEST_RET_ENC_EC(ret);
  5420. #endif
  5421. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5422. return WC_TEST_RET_ENC_EC(ret);
  5423. return 0;
  5424. }
  5425. #endif
  5426. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5427. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5428. {
  5429. Hmac hmac;
  5430. byte hash[WC_SHA384_DIGEST_SIZE];
  5431. const char* keys[]=
  5432. {
  5433. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5434. "\x0b\x0b\x0b",
  5435. "Jefe",
  5436. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5437. "\xAA\xAA\xAA",
  5438. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5439. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5440. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5441. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5442. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5443. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5444. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5445. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5446. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5447. };
  5448. testVector a, b, c, d;
  5449. testVector test_hmac[4];
  5450. wc_test_ret_t ret;
  5451. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5452. WOLFSSL_ENTER("hmac_sha384_test");
  5453. a.input = "Hi There";
  5454. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5455. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5456. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5457. "\xfa\x9c\xb6";
  5458. a.inLen = XSTRLEN(a.input);
  5459. a.outLen = WC_SHA384_DIGEST_SIZE;
  5460. b.input = "what do ya want for nothing?";
  5461. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5462. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5463. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5464. "\xb2\x16\x49";
  5465. b.inLen = XSTRLEN(b.input);
  5466. b.outLen = WC_SHA384_DIGEST_SIZE;
  5467. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5468. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5469. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5470. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5471. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5472. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5473. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5474. "\xa3\x4f\x27";
  5475. c.inLen = XSTRLEN(c.input);
  5476. c.outLen = WC_SHA384_DIGEST_SIZE;
  5477. d.input = "Big Key Input";
  5478. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  5479. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  5480. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  5481. "\x57\x41\x69";
  5482. d.inLen = XSTRLEN(d.input);
  5483. d.outLen = WC_SHA384_DIGEST_SIZE;
  5484. test_hmac[0] = a;
  5485. test_hmac[1] = b;
  5486. test_hmac[2] = c;
  5487. test_hmac[3] = d;
  5488. for (i = 0; i < times; ++i) {
  5489. #if defined(HAVE_FIPS)
  5490. if (i == 1)
  5491. continue; /* fips not allowed */
  5492. #endif
  5493. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5494. return WC_TEST_RET_ENC_EC(ret);
  5495. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5496. (word32)XSTRLEN(keys[i]));
  5497. if (ret != 0)
  5498. return WC_TEST_RET_ENC_EC(ret);
  5499. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5500. (word32)test_hmac[i].inLen);
  5501. if (ret != 0)
  5502. return WC_TEST_RET_ENC_EC(ret);
  5503. ret = wc_HmacFinal(&hmac, hash);
  5504. if (ret != 0)
  5505. return WC_TEST_RET_ENC_EC(ret);
  5506. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5507. return WC_TEST_RET_ENC_I(i);
  5508. wc_HmacFree(&hmac);
  5509. }
  5510. #ifndef HAVE_FIPS
  5511. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5512. return WC_TEST_RET_ENC_EC(ret);
  5513. #endif
  5514. return 0;
  5515. }
  5516. #endif
  5517. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5519. {
  5520. Hmac hmac;
  5521. byte hash[WC_SHA512_DIGEST_SIZE];
  5522. const char* keys[]=
  5523. {
  5524. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5525. "\x0b\x0b\x0b",
  5526. "Jefe",
  5527. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5528. "\xAA\xAA\xAA",
  5529. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5530. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5531. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5532. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5533. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5534. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5535. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5536. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5537. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5538. };
  5539. testVector a, b, c, d;
  5540. testVector test_hmac[4];
  5541. wc_test_ret_t ret;
  5542. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5543. WOLFSSL_ENTER("hmac_sha512_test");
  5544. a.input = "Hi There";
  5545. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5546. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5547. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5548. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  5549. "\x3a\x12\x68\x54";
  5550. a.inLen = XSTRLEN(a.input);
  5551. a.outLen = WC_SHA512_DIGEST_SIZE;
  5552. b.input = "what do ya want for nothing?";
  5553. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  5554. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  5555. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  5556. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  5557. "\x38\xbc\xe7\x37";
  5558. b.inLen = XSTRLEN(b.input);
  5559. b.outLen = WC_SHA512_DIGEST_SIZE;
  5560. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5561. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5562. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5563. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5564. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  5565. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  5566. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  5567. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  5568. "\xe1\x32\x92\xfb";
  5569. c.inLen = XSTRLEN(c.input);
  5570. c.outLen = WC_SHA512_DIGEST_SIZE;
  5571. d.input = "Big Key Input";
  5572. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  5573. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  5574. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  5575. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  5576. "\x1e\x18\xfe\xfa";
  5577. d.inLen = XSTRLEN(d.input);
  5578. d.outLen = WC_SHA512_DIGEST_SIZE;
  5579. test_hmac[0] = a;
  5580. test_hmac[1] = b;
  5581. test_hmac[2] = c;
  5582. test_hmac[3] = d;
  5583. for (i = 0; i < times; ++i) {
  5584. #if defined(HAVE_FIPS)
  5585. if (i == 1)
  5586. continue; /* fips not allowed */
  5587. #endif
  5588. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5589. return WC_TEST_RET_ENC_EC(ret);
  5590. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  5591. (word32)XSTRLEN(keys[i]));
  5592. if (ret != 0)
  5593. return WC_TEST_RET_ENC_EC(ret);
  5594. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5595. (word32)test_hmac[i].inLen);
  5596. if (ret != 0)
  5597. return WC_TEST_RET_ENC_EC(ret);
  5598. ret = wc_HmacFinal(&hmac, hash);
  5599. if (ret != 0)
  5600. return WC_TEST_RET_ENC_EC(ret);
  5601. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  5602. return WC_TEST_RET_ENC_I(i);
  5603. wc_HmacFree(&hmac);
  5604. }
  5605. #ifndef HAVE_FIPS
  5606. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  5607. return WC_TEST_RET_ENC_EC(ret);
  5608. #endif
  5609. return 0;
  5610. }
  5611. #endif
  5612. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  5613. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  5614. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  5615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  5616. {
  5617. Hmac hmac;
  5618. byte hash[WC_SHA3_512_DIGEST_SIZE];
  5619. const char* key[4] =
  5620. {
  5621. "Jefe",
  5622. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5623. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5624. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  5625. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  5626. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5627. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5628. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5629. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5630. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5631. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5632. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5633. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5634. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5635. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5636. };
  5637. const char* input[4] =
  5638. {
  5639. "what do ya want for nothing?",
  5640. "Hi There",
  5641. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5642. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5643. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5644. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5645. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  5646. "Big Key Input"
  5647. };
  5648. const int hashType[4] =
  5649. {
  5650. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  5651. };
  5652. const int hashSz[4] =
  5653. {
  5654. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  5655. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  5656. };
  5657. const char* output[16] =
  5658. {
  5659. /* key = jefe, input = what do ya want for nothing? */
  5660. /* HMAC-SHA3-224 */
  5661. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  5662. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  5663. /* HMAC-SHA3-256 */
  5664. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  5665. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  5666. /* HMAC-SHA3-384 */
  5667. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  5668. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  5669. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  5670. /* HMAC-SHA3-512 */
  5671. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  5672. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  5673. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  5674. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  5675. /* key = 0b..., input = Hi There */
  5676. /* HMAC-SHA3-224 */
  5677. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  5678. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  5679. /* HMAC-SHA3-256 */
  5680. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  5681. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  5682. /* HMAC-SHA3-384 */
  5683. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  5684. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  5685. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  5686. /* HMAC-SHA3-512 */
  5687. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  5688. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  5689. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  5690. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  5691. /* key = aa..., output = dd... */
  5692. /* HMAC-SHA3-224 */
  5693. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  5694. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  5695. /* HMAC-SHA3-256 */
  5696. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  5697. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  5698. /* HMAC-SHA3-384 */
  5699. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  5700. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  5701. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  5702. /* HMAC-SHA3-512 */
  5703. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  5704. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  5705. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  5706. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  5707. /* key = big key, input = Big Key Input */
  5708. /* HMAC-SHA3-224 */
  5709. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  5710. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  5711. /* HMAC-SHA3-256 */
  5712. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  5713. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  5714. /* HMAC-SHA3-384 */
  5715. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  5716. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  5717. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  5718. /* HMAC-SHA3-512 */
  5719. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  5720. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  5721. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  5722. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  5723. };
  5724. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  5725. j, jMax = sizeof(hashType) / sizeof(hashType[0]);
  5726. int ret;
  5727. WOLFSSL_ENTER("hmac_sha3_test");
  5728. #ifdef HAVE_FIPS
  5729. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  5730. * short. Skip it in FIPS builds. */
  5731. i = 1;
  5732. #endif
  5733. for (; i < iMax; i++) {
  5734. for (j = 0; j < jMax; j++) {
  5735. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5736. return WC_TEST_RET_ENC_EC(ret);
  5737. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  5738. (word32)XSTRLEN(key[i]));
  5739. if (ret != 0)
  5740. return WC_TEST_RET_ENC_EC(ret);
  5741. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  5742. (word32)XSTRLEN(input[i]));
  5743. if (ret != 0)
  5744. return WC_TEST_RET_ENC_EC(ret);
  5745. ret = wc_HmacFinal(&hmac, hash);
  5746. if (ret != 0)
  5747. return WC_TEST_RET_ENC_EC(ret);
  5748. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  5749. return WC_TEST_RET_ENC_NC;
  5750. wc_HmacFree(&hmac);
  5751. if (i > 0)
  5752. continue;
  5753. #ifndef HAVE_FIPS
  5754. ret = wc_HmacSizeByType(hashType[j]);
  5755. if (ret != hashSz[j])
  5756. return WC_TEST_RET_ENC_EC(ret);
  5757. #endif
  5758. }
  5759. }
  5760. return 0;
  5761. }
  5762. #endif
  5763. #ifdef WC_RC2
  5764. typedef struct rc2TestVector {
  5765. const char* input;
  5766. const char* output;
  5767. const char* key; /* Key, variable up to 128 bytes */
  5768. const char* iv; /* IV, 8-bytes */
  5769. int inLen;
  5770. int outLen;
  5771. int keyLen;
  5772. int effectiveKeyBits; /* Up to 1024 bits supported */
  5773. } rc2TestVector;
  5774. static wc_test_ret_t rc2_ecb_test(void)
  5775. {
  5776. wc_test_ret_t ret = 0;
  5777. byte cipher[RC2_BLOCK_SIZE];
  5778. byte plain[RC2_BLOCK_SIZE];
  5779. rc2TestVector a, b, c, d, e, f, g, h;
  5780. rc2TestVector test_rc2[8];
  5781. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  5782. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5783. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  5784. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5785. a.inLen = RC2_BLOCK_SIZE;
  5786. a.outLen = RC2_BLOCK_SIZE;
  5787. a.keyLen = 8;
  5788. a.effectiveKeyBits = 63;
  5789. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5790. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  5791. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5792. b.inLen = RC2_BLOCK_SIZE;
  5793. b.outLen = RC2_BLOCK_SIZE;
  5794. b.keyLen = 8;
  5795. b.effectiveKeyBits = 64;
  5796. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  5797. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  5798. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5799. c.inLen = RC2_BLOCK_SIZE;
  5800. c.outLen = RC2_BLOCK_SIZE;
  5801. c.keyLen = 8;
  5802. c.effectiveKeyBits = 64;
  5803. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5804. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  5805. d.key = "\x88";
  5806. d.inLen = RC2_BLOCK_SIZE;
  5807. d.outLen = RC2_BLOCK_SIZE;
  5808. d.keyLen = 1;
  5809. d.effectiveKeyBits = 64;
  5810. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5811. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  5812. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5813. e.inLen = RC2_BLOCK_SIZE;
  5814. e.outLen = RC2_BLOCK_SIZE;
  5815. e.keyLen = 7;
  5816. e.effectiveKeyBits = 64;
  5817. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5818. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  5819. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5820. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5821. f.inLen = RC2_BLOCK_SIZE;
  5822. f.outLen = RC2_BLOCK_SIZE;
  5823. f.keyLen = 16;
  5824. f.effectiveKeyBits = 64;
  5825. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5826. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  5827. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5828. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5829. g.inLen = RC2_BLOCK_SIZE;
  5830. g.outLen = RC2_BLOCK_SIZE;
  5831. g.keyLen = 16;
  5832. g.effectiveKeyBits = 128;
  5833. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5834. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  5835. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5836. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5837. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5838. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5839. "\x1e";
  5840. h.inLen = RC2_BLOCK_SIZE;
  5841. h.outLen = RC2_BLOCK_SIZE;
  5842. h.keyLen = 33;
  5843. h.effectiveKeyBits = 129;
  5844. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  5845. test_rc2[0] = a;
  5846. test_rc2[1] = b;
  5847. test_rc2[2] = c;
  5848. test_rc2[3] = d;
  5849. test_rc2[4] = e;
  5850. test_rc2[5] = f;
  5851. test_rc2[6] = g;
  5852. test_rc2[7] = h;
  5853. for (i = 0; i < times; ++i) {
  5854. Rc2 enc;
  5855. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  5856. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  5857. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  5858. NULL, test_rc2[i].effectiveKeyBits);
  5859. if (ret != 0) {
  5860. return WC_TEST_RET_ENC_EC(ret);
  5861. }
  5862. /* ECB encrypt */
  5863. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  5864. (word32)test_rc2[i].outLen);
  5865. if (ret != 0) {
  5866. return WC_TEST_RET_ENC_EC(ret);
  5867. }
  5868. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  5869. return WC_TEST_RET_ENC_NC;
  5870. }
  5871. /* ECB decrypt */
  5872. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  5873. if (ret != 0) {
  5874. return WC_TEST_RET_ENC_EC(ret);
  5875. }
  5876. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  5877. return WC_TEST_RET_ENC_NC;
  5878. }
  5879. }
  5880. return 0;
  5881. }
  5882. static wc_test_ret_t rc2_cbc_test(void)
  5883. {
  5884. wc_test_ret_t ret = 0;
  5885. byte cipher[128];
  5886. byte plain[128];
  5887. rc2TestVector a, b, c, d, e, f, g, h, i;
  5888. rc2TestVector test_rc2[9];
  5889. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  5890. /* key length = 7, effective key bits = 63 */
  5891. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5892. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5893. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  5894. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  5895. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5896. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5897. a.inLen = RC2_BLOCK_SIZE*2;
  5898. a.outLen = RC2_BLOCK_SIZE*2;
  5899. a.keyLen = 8;
  5900. a.effectiveKeyBits = 63;
  5901. /* key length = 8, effective key bits = 64, all 0xFF */
  5902. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  5903. "\xff\xff\xff\xff\xff\xff\xff\xff";
  5904. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  5905. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  5906. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5907. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5908. b.inLen = RC2_BLOCK_SIZE*2;
  5909. b.outLen = RC2_BLOCK_SIZE*2;
  5910. b.keyLen = 8;
  5911. b.effectiveKeyBits = 64;
  5912. /* key length = 8, effective key bits = 64 */
  5913. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  5914. "\x10\x00\x00\x00\x00\x00\x00\x01";
  5915. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  5916. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  5917. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5918. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5919. c.inLen = RC2_BLOCK_SIZE*2;
  5920. c.outLen = RC2_BLOCK_SIZE*2;
  5921. c.keyLen = 8;
  5922. c.effectiveKeyBits = 64;
  5923. /* key length = 1, effective key bits = 64 */
  5924. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5925. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5926. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  5927. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  5928. d.key = "\x88";
  5929. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5930. d.inLen = RC2_BLOCK_SIZE*2;
  5931. d.outLen = RC2_BLOCK_SIZE*2;
  5932. d.keyLen = 1;
  5933. d.effectiveKeyBits = 64;
  5934. /* key length = 7, effective key bits = 64 */
  5935. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5936. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5937. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  5938. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  5939. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5940. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5941. e.inLen = RC2_BLOCK_SIZE*2;
  5942. e.outLen = RC2_BLOCK_SIZE*2;
  5943. e.keyLen = 7;
  5944. e.effectiveKeyBits = 64;
  5945. /* key length = 16, effective key bits = 64 */
  5946. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5947. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5948. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  5949. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  5950. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5951. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5952. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5953. f.inLen = RC2_BLOCK_SIZE*2;
  5954. f.outLen = RC2_BLOCK_SIZE*2;
  5955. f.keyLen = 16;
  5956. f.effectiveKeyBits = 64;
  5957. /* key length = 16, effective bits = 128 */
  5958. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5959. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5960. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  5961. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  5962. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5963. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5964. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5965. g.inLen = RC2_BLOCK_SIZE*2;
  5966. g.outLen = RC2_BLOCK_SIZE*2;
  5967. g.keyLen = 16;
  5968. g.effectiveKeyBits = 128;
  5969. /* key length = 33, effective bits = 129 */
  5970. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5971. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5972. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  5973. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  5974. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5975. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5976. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5977. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5978. "\x1e";
  5979. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5980. h.inLen = RC2_BLOCK_SIZE*2;
  5981. h.outLen = RC2_BLOCK_SIZE*2;
  5982. h.keyLen = 33;
  5983. h.effectiveKeyBits = 129;
  5984. /* key length = 10, effective bits = 40 */
  5985. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  5986. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  5987. "\x11\x22\x33\x44\x55\x66\x77\x88"
  5988. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  5989. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  5990. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  5991. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  5992. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  5993. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  5994. "\x3E\x96";
  5995. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  5996. i.inLen = RC2_BLOCK_SIZE*4;
  5997. i.outLen = RC2_BLOCK_SIZE*4;
  5998. i.keyLen = 10;
  5999. i.effectiveKeyBits = 40;
  6000. test_rc2[0] = a;
  6001. test_rc2[1] = b;
  6002. test_rc2[2] = c;
  6003. test_rc2[3] = d;
  6004. test_rc2[4] = e;
  6005. test_rc2[5] = f;
  6006. test_rc2[6] = g;
  6007. test_rc2[7] = h;
  6008. test_rc2[8] = i;
  6009. for (j = 0; j < times; ++j) {
  6010. Rc2 rc2;
  6011. XMEMSET(cipher, 0, sizeof(cipher));
  6012. XMEMSET(plain, 0, sizeof(plain));
  6013. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  6014. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  6015. if (ret != 0) {
  6016. return WC_TEST_RET_ENC_EC(ret);
  6017. }
  6018. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  6019. test_rc2[j].inLen);
  6020. if (ret != 0) {
  6021. return WC_TEST_RET_ENC_EC(ret);
  6022. }
  6023. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  6024. return WC_TEST_RET_ENC_NC;
  6025. }
  6026. /* reset IV for decrypt, since overridden by encrypt operation */
  6027. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  6028. if (ret != 0) {
  6029. return WC_TEST_RET_ENC_EC(ret);
  6030. }
  6031. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  6032. if (ret != 0) {
  6033. return WC_TEST_RET_ENC_EC(ret);
  6034. }
  6035. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  6036. return WC_TEST_RET_ENC_NC;
  6037. }
  6038. }
  6039. return 0;
  6040. }
  6041. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  6042. {
  6043. wc_test_ret_t ret = 0;
  6044. WOLFSSL_ENTER("rc2_test");
  6045. ret = rc2_ecb_test();
  6046. if (ret != 0) {
  6047. return ret;
  6048. }
  6049. return rc2_cbc_test();
  6050. }
  6051. #endif
  6052. #ifndef NO_RC4
  6053. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  6054. {
  6055. byte cipher[16];
  6056. byte plain[16];
  6057. wc_test_ret_t ret;
  6058. const char* keys[] =
  6059. {
  6060. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6061. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6062. "\x00\x00\x00\x00\x00\x00\x00\x00",
  6063. "\xef\x01\x23\x45"
  6064. };
  6065. testVector a, b, c, d;
  6066. testVector test_arc4[4];
  6067. int times = sizeof(test_arc4) / sizeof(testVector), i;
  6068. WOLFSSL_ENTER("arc4_test");
  6069. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  6070. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  6071. a.inLen = 8;
  6072. a.outLen = 8;
  6073. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6074. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  6075. b.inLen = 8;
  6076. b.outLen = 8;
  6077. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6078. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  6079. c.inLen = 8;
  6080. c.outLen = 8;
  6081. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  6082. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  6083. d.inLen = 10;
  6084. d.outLen = 10;
  6085. test_arc4[0] = a;
  6086. test_arc4[1] = b;
  6087. test_arc4[2] = c;
  6088. test_arc4[3] = d;
  6089. for (i = 0; i < times; ++i) {
  6090. Arc4 enc;
  6091. Arc4 dec;
  6092. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  6093. if (i == 3)
  6094. keylen = 4;
  6095. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  6096. if (ret != 0)
  6097. return WC_TEST_RET_ENC_EC(ret);
  6098. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  6099. if (ret != 0)
  6100. return WC_TEST_RET_ENC_EC(ret);
  6101. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  6102. if (ret != 0)
  6103. return WC_TEST_RET_ENC_EC(ret);
  6104. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  6105. if (ret != 0)
  6106. return WC_TEST_RET_ENC_EC(ret);
  6107. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  6108. (word32)test_arc4[i].outLen);
  6109. if (ret != 0)
  6110. return WC_TEST_RET_ENC_EC(ret);
  6111. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  6112. if (ret != 0)
  6113. return WC_TEST_RET_ENC_EC(ret);
  6114. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  6115. return WC_TEST_RET_ENC_I(i);
  6116. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  6117. return WC_TEST_RET_ENC_I(i);
  6118. wc_Arc4Free(&enc);
  6119. wc_Arc4Free(&dec);
  6120. }
  6121. return 0;
  6122. }
  6123. #endif
  6124. #ifdef HAVE_CHACHA
  6125. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  6126. {
  6127. ChaCha enc;
  6128. ChaCha dec;
  6129. byte cipher[128];
  6130. byte plain[128];
  6131. byte sliver[64];
  6132. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6133. word32 keySz = 32;
  6134. wc_test_ret_t ret = 0;
  6135. int i;
  6136. int times = 4;
  6137. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6138. {
  6139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6141. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6142. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6143. };
  6144. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6145. {
  6146. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6147. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6148. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6149. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6150. };
  6151. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6152. {
  6153. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6154. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6155. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6156. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6157. };
  6158. /* 128 bit key */
  6159. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  6160. {
  6161. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6162. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6163. };
  6164. const byte* keys[] = {key1, key2, key3, key4};
  6165. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6166. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6167. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  6168. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6169. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  6170. #ifndef BENCH_EMBEDDED
  6171. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  6172. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  6173. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  6174. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  6175. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  6176. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  6177. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  6178. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  6179. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  6180. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  6181. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  6182. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  6183. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  6184. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  6185. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  6186. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  6187. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  6188. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  6189. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  6190. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  6191. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  6192. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  6193. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  6194. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  6195. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  6196. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  6197. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  6198. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  6199. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  6200. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  6201. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  6202. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  6203. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  6204. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  6205. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  6206. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  6207. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  6208. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  6209. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  6210. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  6211. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  6212. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  6213. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  6214. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  6215. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  6216. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  6217. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  6218. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  6219. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  6220. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  6221. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  6222. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  6223. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  6224. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  6225. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  6226. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  6227. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  6228. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  6229. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  6230. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  6231. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  6232. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  6233. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  6234. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  6235. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  6236. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  6237. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  6238. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  6239. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  6240. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  6241. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  6242. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  6243. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  6244. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  6245. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  6246. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  6247. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  6248. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  6249. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  6250. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  6251. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  6252. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  6253. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  6254. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  6255. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  6256. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  6257. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  6258. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  6259. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  6260. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  6261. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  6262. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  6263. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  6264. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  6265. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  6266. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  6267. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  6268. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  6269. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  6270. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  6271. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  6272. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  6273. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  6274. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  6275. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  6276. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  6277. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  6278. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  6279. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  6280. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  6281. };
  6282. #define CHACHA_BIG_TEST_SIZE 1305
  6283. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  6284. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  6285. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  6286. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  6287. #else
  6288. byte* cipher_big;
  6289. byte* plain_big;
  6290. byte* input_big;
  6291. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6292. int block_size;
  6293. #endif /* BENCH_EMBEDDED */
  6294. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  6295. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  6296. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  6297. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  6298. byte* test_chacha[4];
  6299. test_chacha[0] = a;
  6300. test_chacha[1] = b;
  6301. test_chacha[2] = c;
  6302. test_chacha[3] = d;
  6303. WOLFSSL_ENTER("chacha_test");
  6304. #ifndef BENCH_EMBEDDED
  6305. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6306. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6307. DYNAMIC_TYPE_TMP_BUFFER);
  6308. if (cipher_big == NULL) {
  6309. return MEMORY_E;
  6310. }
  6311. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6312. DYNAMIC_TYPE_TMP_BUFFER);
  6313. if (plain_big == NULL) {
  6314. return MEMORY_E;
  6315. }
  6316. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6317. DYNAMIC_TYPE_TMP_BUFFER);
  6318. if (input_big == NULL) {
  6319. return MEMORY_E;
  6320. }
  6321. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6322. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6323. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6324. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6325. #endif /* BENCH_EMBEDDED */
  6326. for (i = 0; i < times; ++i) {
  6327. if (i < 3) {
  6328. keySz = 32;
  6329. }
  6330. else {
  6331. keySz = 16;
  6332. }
  6333. XMEMCPY(plain, keys[i], keySz);
  6334. XMEMSET(cipher, 0, 32);
  6335. XMEMCPY(cipher + 4, ivs[i], 8);
  6336. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6337. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6338. if (ret != 0)
  6339. return ret;
  6340. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6341. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6342. if (ret != 0)
  6343. return ret;
  6344. XMEMCPY(plain, input, 8);
  6345. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6346. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6347. if (ret != 0)
  6348. return ret;
  6349. if (XMEMCMP(test_chacha[i], cipher, 8))
  6350. return WC_TEST_RET_ENC_I(i);
  6351. if (XMEMCMP(plain, input, 8))
  6352. return WC_TEST_RET_ENC_I(i);
  6353. }
  6354. /* test of starting at a different counter
  6355. encrypts all of the information and decrypts starting at 2nd chunk */
  6356. XMEMSET(plain, 0, sizeof(plain));
  6357. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6358. XMEMSET(cipher, 0, sizeof(cipher));
  6359. XMEMCPY(cipher + 4, ivs[0], 8);
  6360. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6361. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6362. if (ret != 0)
  6363. return ret;
  6364. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6365. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6366. if (ret != 0)
  6367. return ret;
  6368. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6369. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6370. if (ret != 0)
  6371. return ret;
  6372. if (XMEMCMP(plain + 64, sliver, 64))
  6373. return WC_TEST_RET_ENC_NC;
  6374. #ifndef BENCH_EMBEDDED
  6375. /* test of encrypting more data */
  6376. keySz = 32;
  6377. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6378. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6379. if (ret != 0)
  6380. return ret;
  6381. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6382. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6383. if (ret != 0)
  6384. return ret;
  6385. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6386. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6387. CHACHA_BIG_TEST_SIZE);
  6388. if (ret != 0)
  6389. return ret;
  6390. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6391. return WC_TEST_RET_ENC_NC;
  6392. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6393. return WC_TEST_RET_ENC_NC;
  6394. for (i = 0; i < 18; ++i) {
  6395. /* this will test all paths
  6396. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6397. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6398. */
  6399. block_size = (2 << (i%9)) - (i<9?1:0);
  6400. keySz = 32;
  6401. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6402. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6403. if (ret != 0)
  6404. return ret;
  6405. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6406. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6407. if (ret != 0)
  6408. return ret;
  6409. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  6410. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  6411. if (ret != 0)
  6412. return ret;
  6413. if (XMEMCMP(plain_big, input_big, block_size))
  6414. return WC_TEST_RET_ENC_I(i);
  6415. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6416. return WC_TEST_RET_ENC_I(i);
  6417. }
  6418. /* Streaming test */
  6419. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6420. int j, rem;
  6421. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6422. if (ret != 0)
  6423. return WC_TEST_RET_ENC_EC(ret);
  6424. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6425. if (ret != 0)
  6426. return WC_TEST_RET_ENC_EC(ret);
  6427. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6428. if (ret != 0)
  6429. return WC_TEST_RET_ENC_EC(ret);
  6430. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6431. if (ret != 0)
  6432. return WC_TEST_RET_ENC_EC(ret);
  6433. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6434. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  6435. if (ret != 0)
  6436. return WC_TEST_RET_ENC_EC(ret);
  6437. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  6438. if (ret != 0)
  6439. return WC_TEST_RET_ENC_EC(ret);
  6440. }
  6441. rem = CHACHA_BIG_TEST_SIZE - j;
  6442. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  6443. if (ret != 0)
  6444. return WC_TEST_RET_ENC_EC(ret);
  6445. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  6446. if (ret != 0)
  6447. return WC_TEST_RET_ENC_EC(ret);
  6448. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6449. return WC_TEST_RET_ENC_NC;
  6450. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6451. return WC_TEST_RET_ENC_NC;
  6452. }
  6453. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6454. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6455. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6456. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6457. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6458. #endif /* BENCH_EMBEDDED */
  6459. return 0;
  6460. }
  6461. #endif /* HAVE_CHACHA */
  6462. #ifdef HAVE_POLY1305
  6463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6464. {
  6465. byte tag[16];
  6466. Poly1305 enc;
  6467. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6468. {
  6469. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6470. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6471. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6472. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6473. 0x75,0x70
  6474. };
  6475. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6476. {
  6477. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6478. 0x6c,0x64,0x21
  6479. };
  6480. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6481. {
  6482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6485. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6486. };
  6487. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6488. {
  6489. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6490. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6491. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6492. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6493. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6494. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6495. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6496. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6497. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6498. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6499. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6500. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6501. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6502. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6503. 0x61,0x16
  6504. };
  6505. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6506. {
  6507. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6508. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6509. };
  6510. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6511. {
  6512. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6513. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6514. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6515. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6516. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6517. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6518. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6519. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6520. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6521. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6522. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6523. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6524. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6525. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6526. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6527. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6528. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6529. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6530. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6531. 0x61,0x16
  6532. };
  6533. byte additional[] =
  6534. {
  6535. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6536. 0xc4,0xc5,0xc6,0xc7
  6537. };
  6538. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6539. {
  6540. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6541. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6542. };
  6543. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6544. {
  6545. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6546. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6547. };
  6548. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  6549. {
  6550. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  6551. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  6552. };
  6553. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  6554. {
  6555. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  6556. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  6557. };
  6558. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  6559. {
  6560. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  6561. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  6562. };
  6563. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  6564. {
  6565. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6567. };
  6568. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  6569. {
  6570. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  6571. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  6572. };
  6573. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  6574. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  6575. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  6576. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6577. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6578. };
  6579. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6580. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  6581. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  6582. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  6583. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  6584. };
  6585. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  6586. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  6587. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  6588. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  6589. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  6590. };
  6591. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  6592. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6594. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6595. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6596. };
  6597. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  6598. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  6599. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  6600. const byte* keys[] = {key, key, key2, key2, key5, key};
  6601. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  6602. correct6};
  6603. int i;
  6604. wc_test_ret_t ret = 0;
  6605. WOLFSSL_ENTER("poly1305_test");
  6606. for (i = 0; i < 6; i++) {
  6607. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  6608. if (ret != 0)
  6609. return WC_TEST_RET_ENC_I(i);
  6610. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  6611. if (ret != 0)
  6612. return WC_TEST_RET_ENC_I(i);
  6613. ret = wc_Poly1305Final(&enc, tag);
  6614. if (ret != 0)
  6615. return WC_TEST_RET_ENC_I(i);
  6616. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  6617. return WC_TEST_RET_ENC_I(i);
  6618. }
  6619. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  6620. XMEMSET(tag, 0, sizeof(tag));
  6621. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  6622. if (ret != 0)
  6623. return WC_TEST_RET_ENC_EC(ret);
  6624. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6625. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6626. if (ret != 0)
  6627. return WC_TEST_RET_ENC_EC(ret);
  6628. if (XMEMCMP(tag, correct4, sizeof(tag)))
  6629. return WC_TEST_RET_ENC_NC;
  6630. /* Check fail of TLS MAC function if altering additional data */
  6631. XMEMSET(tag, 0, sizeof(tag));
  6632. additional[0]++;
  6633. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6634. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6635. if (ret != 0)
  6636. return WC_TEST_RET_ENC_EC(ret);
  6637. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  6638. return WC_TEST_RET_ENC_NC;
  6639. return 0;
  6640. }
  6641. #endif /* HAVE_POLY1305 */
  6642. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  6643. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  6644. {
  6645. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  6646. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6647. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  6648. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  6649. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  6650. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  6651. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  6652. };
  6653. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  6654. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  6655. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  6656. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  6657. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  6658. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  6659. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  6660. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  6661. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  6662. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  6663. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  6664. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  6665. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  6666. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  6667. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  6668. 0x74, 0x2e
  6669. };
  6670. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  6671. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  6672. 0x44, 0x45, 0x46, 0x47
  6673. };
  6674. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  6675. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  6676. 0xc4, 0xc5, 0xc6, 0xc7
  6677. };
  6678. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  6679. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  6680. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  6681. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  6682. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  6683. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  6684. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  6685. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  6686. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  6687. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  6688. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  6689. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  6690. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  6691. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  6692. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  6693. 0x61, 0x16
  6694. };
  6695. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  6696. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  6697. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  6698. };
  6699. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  6700. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6701. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6702. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  6703. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  6704. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  6705. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  6706. };
  6707. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  6708. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  6709. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  6710. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  6711. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6712. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  6713. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  6714. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  6715. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  6716. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  6717. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  6718. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  6719. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  6720. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  6721. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  6722. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  6723. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6724. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  6725. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  6726. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  6727. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  6728. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  6729. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  6730. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  6731. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  6732. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  6733. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  6734. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  6735. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  6736. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  6737. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  6738. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  6739. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  6740. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  6741. 0x9d
  6742. };
  6743. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6744. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  6745. 0x05, 0x06, 0x07, 0x08
  6746. };
  6747. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  6748. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  6749. 0x00, 0x00, 0x4e, 0x91
  6750. };
  6751. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  6752. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  6753. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  6754. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  6755. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  6756. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  6757. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  6758. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  6759. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  6760. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  6761. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  6762. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  6763. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  6764. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  6765. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  6766. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  6767. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  6768. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  6769. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  6770. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  6771. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  6772. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  6773. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  6774. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  6775. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  6776. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  6777. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  6778. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  6779. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  6780. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  6781. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  6782. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  6783. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  6784. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  6785. 0x9b
  6786. };
  6787. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  6788. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  6789. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  6790. };
  6791. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  6792. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  6793. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  6794. ChaChaPoly_Aead aead;
  6795. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  6796. #define TEST_SMALL_CHACHA_CHUNKS 32
  6797. #else
  6798. #define TEST_SMALL_CHACHA_CHUNKS 64
  6799. #endif
  6800. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6801. word32 testLen;
  6802. #endif
  6803. wc_test_ret_t err;
  6804. WOLFSSL_ENTER("chacha20_poly1305_aead_test");
  6805. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6806. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6807. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6808. /* Parameter Validation testing */
  6809. /* Encrypt */
  6810. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  6811. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6812. if (err != BAD_FUNC_ARG)
  6813. return WC_TEST_RET_ENC_EC(err);
  6814. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  6815. plaintext1, sizeof(plaintext1), generatedCiphertext,
  6816. generatedAuthTag);
  6817. if (err != BAD_FUNC_ARG)
  6818. return WC_TEST_RET_ENC_EC(err);
  6819. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6820. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6821. if (err != BAD_FUNC_ARG)
  6822. return WC_TEST_RET_ENC_EC(err);
  6823. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6824. sizeof(plaintext1), NULL, generatedAuthTag);
  6825. if (err != BAD_FUNC_ARG)
  6826. return WC_TEST_RET_ENC_EC(err);
  6827. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6828. sizeof(plaintext1), generatedCiphertext, NULL);
  6829. if (err != BAD_FUNC_ARG)
  6830. return WC_TEST_RET_ENC_EC(err);
  6831. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6832. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6833. if (err != BAD_FUNC_ARG)
  6834. return WC_TEST_RET_ENC_EC(err);
  6835. /* Decrypt */
  6836. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  6837. sizeof(cipher2), authTag2, generatedPlaintext);
  6838. if (err != BAD_FUNC_ARG)
  6839. return WC_TEST_RET_ENC_EC(err);
  6840. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  6841. sizeof(cipher2), authTag2, generatedPlaintext);
  6842. if (err != BAD_FUNC_ARG)
  6843. return WC_TEST_RET_ENC_EC(err);
  6844. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6845. sizeof(cipher2), authTag2, generatedPlaintext);
  6846. if (err != BAD_FUNC_ARG)
  6847. return WC_TEST_RET_ENC_EC(err);
  6848. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6849. sizeof(cipher2), NULL, generatedPlaintext);
  6850. if (err != BAD_FUNC_ARG)
  6851. return WC_TEST_RET_ENC_EC(err);
  6852. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6853. sizeof(cipher2), authTag2, NULL);
  6854. if (err != BAD_FUNC_ARG)
  6855. return WC_TEST_RET_ENC_EC(err);
  6856. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6857. sizeof(cipher2), authTag2, generatedPlaintext);
  6858. if (err != BAD_FUNC_ARG)
  6859. return WC_TEST_RET_ENC_EC(err);
  6860. /* Test #1 */
  6861. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  6862. aad1, sizeof(aad1),
  6863. plaintext1, sizeof(plaintext1),
  6864. generatedCiphertext, generatedAuthTag);
  6865. if (err) {
  6866. return WC_TEST_RET_ENC_EC(err);
  6867. }
  6868. /* -- Check the ciphertext and authtag */
  6869. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6870. return WC_TEST_RET_ENC_NC;
  6871. }
  6872. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  6873. return WC_TEST_RET_ENC_NC;
  6874. }
  6875. /* -- Verify decryption works */
  6876. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  6877. aad1, sizeof(aad1),
  6878. cipher1, sizeof(cipher1),
  6879. authTag1, generatedPlaintext);
  6880. if (err) {
  6881. return err;
  6882. }
  6883. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6884. return WC_TEST_RET_ENC_NC;
  6885. }
  6886. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6887. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6888. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6889. /* Test #2 */
  6890. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  6891. aad2, sizeof(aad2),
  6892. plaintext2, sizeof(plaintext2),
  6893. generatedCiphertext, generatedAuthTag);
  6894. if (err) {
  6895. return err;
  6896. }
  6897. /* -- Check the ciphertext and authtag */
  6898. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6899. return WC_TEST_RET_ENC_NC;
  6900. }
  6901. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  6902. return WC_TEST_RET_ENC_NC;
  6903. }
  6904. /* -- Verify decryption works */
  6905. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  6906. aad2, sizeof(aad2),
  6907. cipher2, sizeof(cipher2),
  6908. authTag2, generatedPlaintext);
  6909. if (err) {
  6910. return err;
  6911. }
  6912. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6913. return WC_TEST_RET_ENC_NC;
  6914. }
  6915. /* AEAD init/update/final - bad argument tests */
  6916. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  6917. CHACHA20_POLY1305_AEAD_DECRYPT);
  6918. if (err != BAD_FUNC_ARG)
  6919. return WC_TEST_RET_ENC_EC(err);
  6920. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  6921. CHACHA20_POLY1305_AEAD_DECRYPT);
  6922. if (err != BAD_FUNC_ARG)
  6923. return WC_TEST_RET_ENC_EC(err);
  6924. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  6925. CHACHA20_POLY1305_AEAD_DECRYPT);
  6926. if (err != BAD_FUNC_ARG)
  6927. return WC_TEST_RET_ENC_EC(err);
  6928. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  6929. if (err != BAD_FUNC_ARG)
  6930. return WC_TEST_RET_ENC_EC(err);
  6931. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  6932. if (err != BAD_FUNC_ARG)
  6933. return WC_TEST_RET_ENC_EC(err);
  6934. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  6935. generatedPlaintext, sizeof(plaintext1));
  6936. if (err != BAD_FUNC_ARG)
  6937. return WC_TEST_RET_ENC_EC(err);
  6938. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  6939. sizeof(plaintext1));
  6940. if (err != BAD_FUNC_ARG)
  6941. return WC_TEST_RET_ENC_EC(err);
  6942. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  6943. sizeof(plaintext1));
  6944. if (err != BAD_FUNC_ARG)
  6945. return WC_TEST_RET_ENC_EC(err);
  6946. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  6947. if (err != BAD_FUNC_ARG)
  6948. return WC_TEST_RET_ENC_EC(err);
  6949. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  6950. if (err != BAD_FUNC_ARG)
  6951. return WC_TEST_RET_ENC_EC(err);
  6952. /* AEAD init/update/final - bad state tests */
  6953. /* clear struct - make valgrind happy to resolve
  6954. "Conditional jump or move depends on uninitialised value(s)".
  6955. The enum is "int" size and aead.state is "byte" */
  6956. /* The wc_ChaCha20Poly1305_Init function does this normally */
  6957. XMEMSET(&aead, 0, sizeof(aead));
  6958. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6959. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6960. if (err != BAD_STATE_E)
  6961. return WC_TEST_RET_ENC_EC(err);
  6962. aead.state = CHACHA20_POLY1305_STATE_DATA;
  6963. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6964. if (err != BAD_STATE_E)
  6965. return WC_TEST_RET_ENC_EC(err);
  6966. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6967. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  6968. generatedPlaintext, sizeof(plaintext1));
  6969. if (err != BAD_STATE_E)
  6970. return WC_TEST_RET_ENC_EC(err);
  6971. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6972. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6973. if (err != BAD_STATE_E)
  6974. return WC_TEST_RET_ENC_EC(err);
  6975. aead.state = CHACHA20_POLY1305_STATE_READY;
  6976. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6977. if (err != BAD_STATE_E)
  6978. return WC_TEST_RET_ENC_EC(err);
  6979. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6980. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6981. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6982. /* Test 1 - Encrypt */
  6983. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6984. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6985. if (err != 0)
  6986. return WC_TEST_RET_ENC_EC(err);
  6987. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6988. if (err != 0)
  6989. return WC_TEST_RET_ENC_EC(err);
  6990. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6991. /* test doing data in smaller chunks */
  6992. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6993. word32 dataLen = sizeof(plaintext1) - testLen;
  6994. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6995. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6996. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  6997. &generatedCiphertext[testLen], dataLen);
  6998. if (err != 0)
  6999. return WC_TEST_RET_ENC_EC(err);
  7000. testLen += dataLen;
  7001. }
  7002. #else
  7003. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  7004. generatedCiphertext, sizeof(plaintext1));
  7005. #endif
  7006. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7007. if (err != 0)
  7008. return WC_TEST_RET_ENC_EC(err);
  7009. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7010. if (err != 0)
  7011. return WC_TEST_RET_ENC_EC(err);
  7012. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7013. return WC_TEST_RET_ENC_NC;
  7014. }
  7015. /* Test 1 - Decrypt */
  7016. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7017. CHACHA20_POLY1305_AEAD_DECRYPT);
  7018. if (err != 0)
  7019. return WC_TEST_RET_ENC_EC(err);
  7020. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7021. if (err != 0)
  7022. return WC_TEST_RET_ENC_EC(err);
  7023. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7024. /* test doing data in smaller chunks */
  7025. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7026. word32 dataLen = sizeof(plaintext1) - testLen;
  7027. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7028. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7029. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7030. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7031. dataLen);
  7032. if (err != 0)
  7033. return WC_TEST_RET_ENC_EC(err);
  7034. testLen += dataLen;
  7035. }
  7036. #else
  7037. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7038. generatedPlaintext, sizeof(cipher1));
  7039. #endif
  7040. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7041. if (err != 0)
  7042. return WC_TEST_RET_ENC_EC(err);
  7043. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7044. if (err != 0)
  7045. return WC_TEST_RET_ENC_EC(err);
  7046. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7047. return WC_TEST_RET_ENC_NC;
  7048. }
  7049. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7050. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7051. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7052. /* Test 2 - Encrypt */
  7053. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7054. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7055. if (err != 0)
  7056. return WC_TEST_RET_ENC_EC(err);
  7057. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7058. if (err != 0)
  7059. return WC_TEST_RET_ENC_EC(err);
  7060. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7061. /* test doing data in smaller chunks */
  7062. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7063. word32 dataLen = sizeof(plaintext2) - testLen;
  7064. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7065. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7066. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  7067. &generatedCiphertext[testLen], dataLen);
  7068. if (err != 0)
  7069. return WC_TEST_RET_ENC_EC(err);
  7070. testLen += dataLen;
  7071. }
  7072. #else
  7073. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  7074. sizeof(plaintext2));
  7075. #endif
  7076. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7077. if (err != 0)
  7078. return WC_TEST_RET_ENC_EC(err);
  7079. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7080. if (err != 0)
  7081. return WC_TEST_RET_ENC_EC(err);
  7082. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7083. return WC_TEST_RET_ENC_NC;
  7084. }
  7085. /* Test 2 - Decrypt */
  7086. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7087. CHACHA20_POLY1305_AEAD_DECRYPT);
  7088. if (err != 0)
  7089. return WC_TEST_RET_ENC_EC(err);
  7090. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7091. if (err != 0)
  7092. return WC_TEST_RET_ENC_EC(err);
  7093. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7094. /* test doing data in smaller chunks */
  7095. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7096. word32 dataLen = sizeof(plaintext2) - testLen;
  7097. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7098. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7099. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7100. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7101. dataLen);
  7102. if (err != 0)
  7103. return WC_TEST_RET_ENC_EC(err);
  7104. testLen += dataLen;
  7105. }
  7106. #else
  7107. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7108. generatedPlaintext, sizeof(cipher2));
  7109. #endif
  7110. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7111. if (err != 0)
  7112. return WC_TEST_RET_ENC_EC(err);
  7113. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7114. if (err != 0)
  7115. return WC_TEST_RET_ENC_EC(err);
  7116. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7117. return WC_TEST_RET_ENC_NC;
  7118. }
  7119. return err;
  7120. }
  7121. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  7122. #ifndef NO_DES3
  7123. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  7124. {
  7125. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  7126. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7127. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7128. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7129. };
  7130. byte plain[24];
  7131. byte cipher[24];
  7132. Des enc;
  7133. Des dec;
  7134. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  7135. {
  7136. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  7137. };
  7138. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  7139. {
  7140. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  7141. };
  7142. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7143. {
  7144. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  7145. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  7146. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  7147. };
  7148. wc_test_ret_t ret;
  7149. WOLFSSL_ENTER("des_test");
  7150. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  7151. if (ret != 0)
  7152. return WC_TEST_RET_ENC_EC(ret);
  7153. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7154. if (ret != 0)
  7155. return WC_TEST_RET_ENC_EC(ret);
  7156. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  7157. if (ret != 0)
  7158. return WC_TEST_RET_ENC_EC(ret);
  7159. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7160. if (ret != 0)
  7161. return WC_TEST_RET_ENC_EC(ret);
  7162. if (XMEMCMP(plain, vector, sizeof(plain)))
  7163. return WC_TEST_RET_ENC_NC;
  7164. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  7165. return WC_TEST_RET_ENC_NC;
  7166. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  7167. if (ret != 0)
  7168. return WC_TEST_RET_ENC_EC(ret);
  7169. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7170. {
  7171. EncryptedInfo info;
  7172. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7173. XMEMCPY(info.iv, iv, sizeof(iv));
  7174. info.ivSz = sizeof(iv);
  7175. info.keySz = sizeof(key);
  7176. info.cipherType = WC_CIPHER_DES;
  7177. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7178. sizeof(key), WC_HASH_TYPE_SHA);
  7179. if (ret != 0)
  7180. return WC_TEST_RET_ENC_EC(ret);
  7181. /* Test invalid info ptr */
  7182. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  7183. sizeof(key), WC_HASH_TYPE_SHA);
  7184. if (ret != BAD_FUNC_ARG)
  7185. return WC_TEST_RET_ENC_EC(ret);
  7186. #ifndef NO_PWDBASED
  7187. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  7188. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7189. sizeof(key), WC_HASH_TYPE_NONE);
  7190. if (ret == 0)
  7191. return WC_TEST_RET_ENC_EC(ret);
  7192. #endif /* !NO_PWDBASED */
  7193. }
  7194. #endif
  7195. return 0;
  7196. }
  7197. #endif /* !NO_DES3 */
  7198. #ifndef NO_DES3
  7199. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  7200. {
  7201. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  7202. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7203. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7204. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7205. };
  7206. byte plain[24];
  7207. byte cipher[24];
  7208. Des3 enc;
  7209. Des3 dec;
  7210. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7211. {
  7212. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  7213. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  7214. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  7215. };
  7216. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7217. {
  7218. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  7219. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  7220. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  7221. };
  7222. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  7223. {
  7224. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  7225. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  7226. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  7227. };
  7228. wc_test_ret_t ret;
  7229. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7230. size_t i;
  7231. #endif
  7232. WOLFSSL_ENTER("des3_test");
  7233. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  7234. if (ret != 0)
  7235. return WC_TEST_RET_ENC_EC(ret);
  7236. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  7237. if (ret != 0)
  7238. return WC_TEST_RET_ENC_EC(ret);
  7239. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  7240. if (ret != 0)
  7241. return WC_TEST_RET_ENC_EC(ret);
  7242. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  7243. if (ret != 0)
  7244. return WC_TEST_RET_ENC_EC(ret);
  7245. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7246. #if defined(WOLFSSL_ASYNC_CRYPT)
  7247. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7248. #endif
  7249. if (ret != 0)
  7250. return WC_TEST_RET_ENC_EC(ret);
  7251. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7252. #if defined(WOLFSSL_ASYNC_CRYPT)
  7253. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7254. #endif
  7255. if (ret != 0)
  7256. return WC_TEST_RET_ENC_EC(ret);
  7257. if (XMEMCMP(plain, vector, sizeof(plain)))
  7258. return WC_TEST_RET_ENC_NC;
  7259. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7260. return WC_TEST_RET_ENC_NC;
  7261. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7262. /* test the same vectors with using compatibility layer */
  7263. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  7264. DES_key_schedule ks1;
  7265. DES_key_schedule ks2;
  7266. DES_key_schedule ks3;
  7267. DES_cblock iv4;
  7268. byte tmp[sizeof(vector)];
  7269. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  7270. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  7271. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  7272. XMEMSET(plain, 0, sizeof(plain));
  7273. XMEMSET(cipher, 0, sizeof(cipher));
  7274. /* Test in-place encrypt/decrypt */
  7275. XMEMCPY(tmp, vector, sizeof(vector));
  7276. /* Use i as the splitter */
  7277. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7278. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7279. &iv4, DES_ENCRYPT);
  7280. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  7281. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  7282. XMEMCPY(cipher, tmp, sizeof(cipher));
  7283. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7284. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7285. &iv4, DES_DECRYPT);
  7286. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  7287. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  7288. XMEMCPY(plain, tmp, sizeof(plain));
  7289. if (XMEMCMP(plain, vector, sizeof(plain)))
  7290. return WC_TEST_RET_ENC_NC;
  7291. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7292. return WC_TEST_RET_ENC_NC;
  7293. }
  7294. #endif /* OPENSSL_EXTRA */
  7295. wc_Des3Free(&enc);
  7296. wc_Des3Free(&dec);
  7297. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7298. {
  7299. EncryptedInfo info;
  7300. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7301. XMEMCPY(info.iv, iv3, sizeof(iv3));
  7302. info.ivSz = sizeof(iv3);
  7303. info.keySz = sizeof(key3);
  7304. info.cipherType = WC_CIPHER_DES3;
  7305. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  7306. sizeof(key3), WC_HASH_TYPE_SHA);
  7307. if (ret != 0)
  7308. return WC_TEST_RET_ENC_EC(ret);
  7309. }
  7310. #endif
  7311. return 0;
  7312. }
  7313. #endif /* NO_DES3 */
  7314. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  7315. * see print_fiducials() below.
  7316. */
  7317. #ifndef NO_AES
  7318. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7319. defined(WOLFSSL_AES_XTS)
  7320. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7321. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7322. /* pass in the function, key, iv, plain text and expected and this function
  7323. * tests that the encryption and decryption is successful */
  7324. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7325. const byte* iv, const byte* plain, int plainSz,
  7326. const byte* expected, int expectedSz)
  7327. {
  7328. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7329. EVP_CIPHER_CTX *ctx = NULL;
  7330. #else
  7331. EVP_CIPHER_CTX ctx[1];
  7332. #endif
  7333. int ctx_inited = 0;
  7334. int idx, cipherSz;
  7335. wc_test_ret_t ret = 0;
  7336. byte* cipher;
  7337. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7338. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7339. return MEMORY_E;
  7340. #endif
  7341. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7342. if (cipher == NULL) {
  7343. ret = WC_TEST_RET_ENC_ERRNO;
  7344. goto EVP_TEST_END;
  7345. }
  7346. /* test encrypt */
  7347. EVP_CIPHER_CTX_init(ctx);
  7348. ctx_inited = 1;
  7349. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7350. ret = WC_TEST_RET_ENC_NC;
  7351. goto EVP_TEST_END;
  7352. }
  7353. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7354. ret = WC_TEST_RET_ENC_NC;
  7355. goto EVP_TEST_END;
  7356. }
  7357. cipherSz = idx;
  7358. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7359. ret = WC_TEST_RET_ENC_NC;
  7360. goto EVP_TEST_END;
  7361. }
  7362. cipherSz += idx;
  7363. if (XMEMCMP(cipher, expected, plainSz)) {
  7364. ret = WC_TEST_RET_ENC_NC;
  7365. goto EVP_TEST_END;
  7366. }
  7367. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7368. ctx_inited = 0;
  7369. if (ret == WOLFSSL_SUCCESS)
  7370. ret = 0;
  7371. else {
  7372. ret = WC_TEST_RET_ENC_NC;
  7373. goto EVP_TEST_END;
  7374. }
  7375. /* test decrypt */
  7376. EVP_CIPHER_CTX_init(ctx);
  7377. ctx_inited = 1;
  7378. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7379. ret = WC_TEST_RET_ENC_NC;
  7380. goto EVP_TEST_END;
  7381. }
  7382. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7383. ret = WC_TEST_RET_ENC_NC;
  7384. goto EVP_TEST_END;
  7385. }
  7386. cipherSz = idx;
  7387. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7388. ret = WC_TEST_RET_ENC_NC;
  7389. goto EVP_TEST_END;
  7390. }
  7391. cipherSz += idx;
  7392. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7393. ret = WC_TEST_RET_ENC_NC;
  7394. goto EVP_TEST_END;
  7395. }
  7396. EVP_TEST_END:
  7397. if (cipher)
  7398. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7399. (void)cipherSz;
  7400. if (ctx_inited) {
  7401. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7402. if (cleanup_ret != WOLFSSL_SUCCESS)
  7403. ret = WC_TEST_RET_ENC_NC;
  7404. }
  7405. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7406. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7407. #endif
  7408. return ret;
  7409. }
  7410. #endif /* OPENSSL_EXTRA */
  7411. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7412. #ifdef WOLFSSL_AES_OFB
  7413. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7414. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7415. {
  7416. #ifdef WOLFSSL_AES_256
  7417. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7418. {
  7419. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7420. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7421. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7422. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7423. };
  7424. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7425. {
  7426. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7427. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7428. };
  7429. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7430. {
  7431. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7432. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7433. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7434. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7435. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7436. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7437. };
  7438. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7439. {
  7440. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7441. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7442. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7443. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7444. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7445. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7446. };
  7447. #endif /* WOLFSSL_AES_256 */
  7448. #ifdef WOLFSSL_AES_128
  7449. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7450. {
  7451. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7452. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7453. };
  7454. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  7455. {
  7456. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7457. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7458. };
  7459. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  7460. {
  7461. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7462. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7463. };
  7464. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7465. {
  7466. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7467. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7468. };
  7469. #endif /* WOLFSSL_AES_128 */
  7470. #ifdef WOLFSSL_AES_192
  7471. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7472. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7473. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7474. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7475. };
  7476. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7477. {
  7478. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7479. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7480. };
  7481. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7482. {
  7483. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7484. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7485. };
  7486. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7487. {
  7488. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7489. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7490. };
  7491. #endif /* WOLFSSL_AES_192 */
  7492. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7493. Aes *enc = NULL;
  7494. #else
  7495. Aes enc[1];
  7496. #endif
  7497. byte cipher[AES_BLOCK_SIZE * 4];
  7498. #ifdef HAVE_AES_DECRYPT
  7499. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7500. Aes *dec = NULL;
  7501. #else
  7502. Aes dec[1];
  7503. #endif
  7504. byte plain [AES_BLOCK_SIZE * 4];
  7505. #endif
  7506. wc_test_ret_t ret = 0;
  7507. WOLFSSL_ENTER("aesofb_test");
  7508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7509. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7510. ERROR_OUT(-1, out);
  7511. #ifdef HAVE_AES_DECRYPT
  7512. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7513. ERROR_OUT(-1, out);
  7514. #endif
  7515. #endif
  7516. XMEMSET(enc, 0, sizeof *enc);
  7517. #ifdef HAVE_AES_DECRYPT
  7518. XMEMSET(dec, 0, sizeof *dec);
  7519. #endif
  7520. #ifdef WOLFSSL_AES_128
  7521. /* 128 key size test */
  7522. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7523. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7524. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7525. cipher2, sizeof(cipher2));
  7526. if (ret != 0) {
  7527. goto out;
  7528. }
  7529. #endif
  7530. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  7531. if (ret != 0)
  7532. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7533. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  7534. if (ret != 0)
  7535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7536. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7537. if (ret != 0)
  7538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7539. #ifdef HAVE_AES_DECRYPT
  7540. /* decrypt uses AES_ENCRYPTION */
  7541. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7542. if (ret != 0)
  7543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7544. #endif
  7545. XMEMSET(cipher, 0, sizeof(cipher));
  7546. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  7547. if (ret != 0)
  7548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7549. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  7550. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7551. #ifdef HAVE_AES_DECRYPT
  7552. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  7553. if (ret != 0)
  7554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7555. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  7556. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7557. #endif /* HAVE_AES_DECRYPT */
  7558. #endif /* WOLFSSL_AES_128 */
  7559. #ifdef WOLFSSL_AES_192
  7560. /* 192 key size test */
  7561. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7562. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7563. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  7564. cipher3, sizeof(cipher3));
  7565. if (ret != 0) {
  7566. goto out;
  7567. }
  7568. #endif
  7569. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7570. if (ret != 0)
  7571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7572. #ifdef HAVE_AES_DECRYPT
  7573. /* decrypt uses AES_ENCRYPTION */
  7574. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7575. if (ret != 0)
  7576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7577. #endif
  7578. XMEMSET(cipher, 0, sizeof(cipher));
  7579. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  7580. if (ret != 0)
  7581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7582. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  7583. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7584. #ifdef HAVE_AES_DECRYPT
  7585. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  7586. if (ret != 0)
  7587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7588. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  7589. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7590. #endif /* HAVE_AES_DECRYPT */
  7591. #endif /* WOLFSSL_AES_192 */
  7592. #ifdef WOLFSSL_AES_256
  7593. /* 256 key size test */
  7594. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7595. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7596. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  7597. cipher1, sizeof(cipher1));
  7598. if (ret != 0) {
  7599. goto out;
  7600. }
  7601. #endif
  7602. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7603. if (ret != 0)
  7604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7605. #ifdef HAVE_AES_DECRYPT
  7606. /* decrypt uses AES_ENCRYPTION */
  7607. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7608. if (ret != 0)
  7609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7610. #endif
  7611. XMEMSET(cipher, 0, sizeof(cipher));
  7612. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  7613. if (ret != 0)
  7614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7615. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  7616. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7617. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7618. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7619. if (ret != 0)
  7620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7621. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  7622. AES_BLOCK_SIZE))
  7623. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7624. #ifdef HAVE_AES_DECRYPT
  7625. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  7626. if (ret != 0)
  7627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7628. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  7629. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7630. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  7631. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7632. if (ret != 0)
  7633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7634. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  7635. AES_BLOCK_SIZE))
  7636. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7637. #endif /* HAVE_AES_DECRYPT */
  7638. /* multiple blocks at once */
  7639. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7640. if (ret != 0)
  7641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7642. #ifdef HAVE_AES_DECRYPT
  7643. /* decrypt uses AES_ENCRYPTION */
  7644. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7645. if (ret != 0)
  7646. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7647. #endif
  7648. XMEMSET(cipher, 0, sizeof(cipher));
  7649. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  7650. if (ret != 0)
  7651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7652. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  7653. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7654. #ifdef HAVE_AES_DECRYPT
  7655. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  7656. if (ret != 0)
  7657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7658. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  7659. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7660. #endif /* HAVE_AES_DECRYPT */
  7661. /* inline decrypt/encrypt*/
  7662. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7663. if (ret != 0)
  7664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7665. #ifdef HAVE_AES_DECRYPT
  7666. /* decrypt uses AES_ENCRYPTION */
  7667. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7668. if (ret != 0)
  7669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7670. #endif
  7671. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  7672. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  7673. if (ret != 0)
  7674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7675. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7676. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7677. #ifdef HAVE_AES_DECRYPT
  7678. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  7679. if (ret != 0)
  7680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7681. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  7682. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7683. #endif /* HAVE_AES_DECRYPT */
  7684. /* 256 key size test leftover support */
  7685. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7686. if (ret != 0)
  7687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7688. #ifdef HAVE_AES_DECRYPT
  7689. /* decrypt uses AES_ENCRYPTION */
  7690. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7691. if (ret != 0)
  7692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7693. #endif
  7694. XMEMSET(cipher, 0, sizeof(cipher));
  7695. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  7696. if (ret != 0)
  7697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7698. if (XMEMCMP(cipher, cipher1, 3))
  7699. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7700. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  7701. if (ret != 0)
  7702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7703. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  7704. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7705. #ifdef HAVE_AES_DECRYPT
  7706. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  7707. if (ret != 0)
  7708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7709. if (XMEMCMP(plain, plain1, 6))
  7710. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7711. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  7712. if (ret != 0)
  7713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7714. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  7715. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7716. #endif /* HAVE_AES_DECRYPT */
  7717. out:
  7718. wc_AesFree(enc);
  7719. wc_AesFree(dec);
  7720. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7721. if (enc)
  7722. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7723. #ifdef HAVE_AES_DECRYPT
  7724. if (dec)
  7725. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7726. #endif
  7727. #endif
  7728. #endif /* WOLFSSL_AES_256 */
  7729. return ret;
  7730. }
  7731. #endif /* WOLFSSL_AES_OFB */
  7732. #if defined(WOLFSSL_AES_CFB)
  7733. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of
  7734. * Operation Methods and Techniques
  7735. */
  7736. static wc_test_ret_t aescfb_test_0(void)
  7737. {
  7738. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7739. Aes *enc = NULL;
  7740. #else
  7741. Aes enc[1];
  7742. #endif
  7743. int enc_inited = 0;
  7744. byte cipher[AES_BLOCK_SIZE * 4];
  7745. #ifdef HAVE_AES_DECRYPT
  7746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7747. Aes *dec = NULL;
  7748. #else
  7749. Aes dec[1];
  7750. #endif
  7751. int dec_inited = 0;
  7752. byte plain [AES_BLOCK_SIZE * 4];
  7753. #endif
  7754. wc_test_ret_t ret = 0;
  7755. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7756. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7757. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  7758. };
  7759. #ifdef WOLFSSL_AES_128
  7760. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7761. {
  7762. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7763. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7764. };
  7765. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7766. {
  7767. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  7768. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  7769. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  7770. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  7771. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  7772. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  7773. };
  7774. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7775. {
  7776. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7777. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7778. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7779. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7780. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7781. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  7782. };
  7783. #endif /* WOLFSSL_AES_128 */
  7784. #ifdef WOLFSSL_AES_192
  7785. /* 192 size key test */
  7786. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7787. {
  7788. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7789. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7790. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7791. };
  7792. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7793. {
  7794. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  7795. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  7796. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  7797. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  7798. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  7799. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  7800. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  7801. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  7802. };
  7803. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7804. {
  7805. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7806. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7807. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7808. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7809. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7810. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7811. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7812. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7813. };
  7814. #endif /* WOLFSSL_AES_192 */
  7815. #ifdef WOLFSSL_AES_256
  7816. /* 256 size key simple test */
  7817. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7818. {
  7819. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7820. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7821. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7822. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7823. };
  7824. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7825. {
  7826. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  7827. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  7828. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  7829. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  7830. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  7831. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  7832. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  7833. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  7834. };
  7835. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7836. {
  7837. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7838. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7839. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7840. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7841. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7842. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7843. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7844. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7845. };
  7846. #endif /* WOLFSSL_AES_256 */
  7847. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7848. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7849. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7850. #ifdef HAVE_AES_DECRYPT
  7851. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7852. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7853. #endif
  7854. #endif
  7855. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7856. if (ret != 0)
  7857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7858. else
  7859. enc_inited = 1;
  7860. #ifdef HAVE_AES_DECRYPT
  7861. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7862. if (ret != 0)
  7863. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7864. else
  7865. dec_inited = 1;
  7866. #endif
  7867. #ifdef WOLFSSL_AES_128
  7868. /* 128 key tests */
  7869. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7870. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7871. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  7872. cipher1, sizeof(cipher1));
  7873. if (ret != 0) {
  7874. return ret;
  7875. }
  7876. #endif
  7877. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7878. if (ret != 0)
  7879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7880. #ifdef HAVE_AES_DECRYPT
  7881. /* decrypt uses AES_ENCRYPTION */
  7882. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7883. if (ret != 0)
  7884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7885. #endif
  7886. XMEMSET(cipher, 0, sizeof(cipher));
  7887. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  7888. if (ret != 0)
  7889. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7890. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7891. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7892. /* test restarting encryption process */
  7893. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  7894. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  7895. if (ret != 0)
  7896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7897. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  7898. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  7899. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7900. #ifdef HAVE_AES_DECRYPT
  7901. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  7902. if (ret != 0)
  7903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7904. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  7905. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7906. #endif /* HAVE_AES_DECRYPT */
  7907. #endif /* WOLFSSL_AES_128 */
  7908. #ifdef WOLFSSL_AES_192
  7909. /* 192 key size test */
  7910. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7911. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7912. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  7913. cipher2, sizeof(cipher2));
  7914. if (ret != 0) {
  7915. return ret;
  7916. }
  7917. #endif
  7918. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7919. if (ret != 0)
  7920. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7921. #ifdef HAVE_AES_DECRYPT
  7922. /* decrypt uses AES_ENCRYPTION */
  7923. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7924. if (ret != 0)
  7925. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7926. #endif
  7927. XMEMSET(cipher, 0, sizeof(cipher));
  7928. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  7929. if (ret != 0)
  7930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7931. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  7932. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7933. #ifdef HAVE_AES_DECRYPT
  7934. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  7935. if (ret != 0)
  7936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7937. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  7938. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7939. #endif /* HAVE_AES_DECRYPT */
  7940. #endif /* WOLFSSL_AES_192 */
  7941. #ifdef WOLFSSL_AES_256
  7942. /* 256 key size test */
  7943. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7944. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7945. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  7946. cipher3, sizeof(cipher3));
  7947. if (ret != 0) {
  7948. return ret;
  7949. }
  7950. #endif
  7951. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7952. if (ret != 0)
  7953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7954. #ifdef HAVE_AES_DECRYPT
  7955. /* decrypt uses AES_ENCRYPTION */
  7956. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7957. if (ret != 0)
  7958. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7959. #endif
  7960. /* test with data left overs, magic lengths are checking near edges */
  7961. XMEMSET(cipher, 0, sizeof(cipher));
  7962. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  7963. if (ret != 0)
  7964. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7965. if (XMEMCMP(cipher, cipher3, 4))
  7966. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7967. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  7968. if (ret != 0)
  7969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7970. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  7971. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7972. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  7973. (AES_BLOCK_SIZE * 4) - 31);
  7974. if (ret != 0)
  7975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7976. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  7977. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7978. #ifdef HAVE_AES_DECRYPT
  7979. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  7980. if (ret != 0)
  7981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7982. if (XMEMCMP(plain, msg3, 4))
  7983. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7984. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  7985. if (ret != 0)
  7986. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7987. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  7988. if (ret != 0)
  7989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7990. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  7991. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7992. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  7993. (AES_BLOCK_SIZE * 4) - 31);
  7994. if (ret != 0)
  7995. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7996. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  7997. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7998. #endif /* HAVE_AES_DECRYPT */
  7999. #endif /* WOLFSSL_AES_256 */
  8000. out:
  8001. if (enc_inited)
  8002. wc_AesFree(enc);
  8003. if (dec_inited)
  8004. wc_AesFree(dec);
  8005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8006. if (enc)
  8007. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8008. #ifdef HAVE_AES_DECRYPT
  8009. if (dec)
  8010. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8011. #endif
  8012. #endif
  8013. return ret;
  8014. }
  8015. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8016. static wc_test_ret_t aescfb1_test(void)
  8017. {
  8018. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8019. Aes *enc = NULL;
  8020. #else
  8021. Aes enc[1];
  8022. #endif
  8023. int enc_inited = 0;
  8024. byte cipher[AES_BLOCK_SIZE];
  8025. #ifdef HAVE_AES_DECRYPT
  8026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8027. Aes *dec = NULL;
  8028. #else
  8029. Aes dec[1];
  8030. #endif
  8031. int dec_inited = 0;
  8032. byte plain [AES_BLOCK_SIZE];
  8033. #endif
  8034. wc_test_ret_t ret = 0;
  8035. #ifdef WOLFSSL_AES_128
  8036. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8037. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  8038. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  8039. };
  8040. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8041. {
  8042. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  8043. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  8044. };
  8045. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8046. {
  8047. 0x00
  8048. };
  8049. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8050. {
  8051. 0xC0
  8052. };
  8053. #endif /* WOLFSSL_AES_128 */
  8054. #ifdef WOLFSSL_AES_192
  8055. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8056. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  8057. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  8058. };
  8059. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8060. {
  8061. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  8062. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  8063. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  8064. };
  8065. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8066. {
  8067. 0x30
  8068. };
  8069. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8070. {
  8071. 0x80
  8072. };
  8073. #endif /* WOLFSSL_AES_192 */
  8074. #ifdef WOLFSSL_AES_256
  8075. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8076. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  8077. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  8078. };
  8079. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8080. {
  8081. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  8082. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  8083. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  8084. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  8085. };
  8086. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8087. {
  8088. 0xF7, 0x00
  8089. };
  8090. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8091. {
  8092. 0x41, 0xC0
  8093. };
  8094. #endif /* WOLFSSL_AES_256 */
  8095. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8096. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8097. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8098. #ifdef HAVE_AES_DECRYPT
  8099. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8100. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8101. #endif
  8102. #endif
  8103. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8104. if (ret != 0)
  8105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8106. else
  8107. enc_inited = 1;
  8108. #ifdef HAVE_AES_DECRYPT
  8109. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8110. if (ret != 0)
  8111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8112. else
  8113. dec_inited = 1;
  8114. #endif
  8115. #ifdef WOLFSSL_AES_128
  8116. /* 128 key tests */
  8117. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, 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, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8123. if (ret != 0)
  8124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8125. #endif
  8126. XMEMSET(cipher, 0, sizeof(cipher));
  8127. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  8128. if (ret != 0)
  8129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8130. if (cipher[0] != cipher1[0])
  8131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8132. #ifdef HAVE_AES_DECRYPT
  8133. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  8134. if (ret != 0)
  8135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8136. if (plain[0] != msg1[0])
  8137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8138. #endif /* HAVE_AES_DECRYPT */
  8139. #ifdef OPENSSL_EXTRA
  8140. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8141. if (ret != 0)
  8142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8143. XMEMSET(cipher, 0, sizeof(cipher));
  8144. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  8145. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  8146. if (ret != 0)
  8147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8148. #ifndef WOLFCRYPT_ONLY
  8149. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  8150. cipher, sizeof(msg1));
  8151. if (ret != 0) {
  8152. goto out;
  8153. }
  8154. #endif
  8155. #endif
  8156. #endif /* WOLFSSL_AES_128 */
  8157. #ifdef WOLFSSL_AES_192
  8158. /* 192 key tests */
  8159. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8160. if (ret != 0)
  8161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8162. XMEMSET(cipher, 0, sizeof(cipher));
  8163. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  8164. if (ret != 0)
  8165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8166. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  8167. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8168. #ifdef OPENSSL_EXTRA
  8169. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8170. if (ret != 0)
  8171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8172. XMEMSET(cipher, 0, sizeof(cipher));
  8173. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  8174. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  8175. if (ret != 0)
  8176. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8177. #ifndef WOLFCRYPT_ONLY
  8178. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  8179. cipher, sizeof(msg2));
  8180. if (ret != 0) {
  8181. goto out;
  8182. }
  8183. #endif
  8184. #endif
  8185. #endif /* WOLFSSL_AES_192 */
  8186. #ifdef WOLFSSL_AES_256
  8187. /* 256 key tests */
  8188. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8189. if (ret != 0)
  8190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8191. XMEMSET(cipher, 0, sizeof(cipher));
  8192. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  8193. if (ret != 0)
  8194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8195. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8196. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8197. #ifdef OPENSSL_EXTRA
  8198. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8199. if (ret != 0)
  8200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8201. XMEMSET(cipher, 0, sizeof(cipher));
  8202. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  8203. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  8204. if (ret != 0)
  8205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8206. #ifndef WOLFCRYPT_ONLY
  8207. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  8208. cipher, sizeof(msg3));
  8209. if (ret != 0) {
  8210. goto out;
  8211. }
  8212. #endif
  8213. #endif
  8214. #endif /* WOLFSSL_AES_256 */
  8215. out:
  8216. if (enc_inited)
  8217. wc_AesFree(enc);
  8218. #ifdef HAVE_AES_DECRYPT
  8219. if (dec_inited)
  8220. wc_AesFree(dec);
  8221. #endif
  8222. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8223. if (enc)
  8224. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8225. #ifdef HAVE_AES_DECRYPT
  8226. if (dec)
  8227. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8228. #endif
  8229. #endif
  8230. return ret;
  8231. }
  8232. static wc_test_ret_t aescfb8_test(void)
  8233. {
  8234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8235. Aes *enc = NULL;
  8236. #else
  8237. Aes enc[1];
  8238. #endif
  8239. int enc_inited = 0;
  8240. byte cipher[AES_BLOCK_SIZE];
  8241. #ifdef HAVE_AES_DECRYPT
  8242. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8243. Aes *dec = NULL;
  8244. #else
  8245. Aes dec[1];
  8246. #endif
  8247. int dec_inited = 0;
  8248. byte plain [AES_BLOCK_SIZE];
  8249. #endif
  8250. wc_test_ret_t ret = 0;
  8251. #ifdef WOLFSSL_AES_128
  8252. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8253. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  8254. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  8255. };
  8256. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8257. {
  8258. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  8259. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  8260. };
  8261. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8262. {
  8263. 0xd2,0x76,0x91
  8264. };
  8265. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8266. {
  8267. 0xc9,0x06,0x35
  8268. };
  8269. #endif /* WOLFSSL_AES_128 */
  8270. #ifdef WOLFSSL_AES_192
  8271. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8272. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  8273. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  8274. };
  8275. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8276. {
  8277. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  8278. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  8279. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  8280. };
  8281. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8282. {
  8283. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  8284. 0xb5,0xe8
  8285. };
  8286. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8287. {
  8288. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  8289. 0x3a,0x60
  8290. };
  8291. #endif
  8292. #ifdef WOLFSSL_AES_256
  8293. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8294. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  8295. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  8296. };
  8297. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8298. {
  8299. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  8300. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  8301. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  8302. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  8303. };
  8304. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8305. {
  8306. 0x1c,0xff,0x95
  8307. };
  8308. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8309. {
  8310. 0xb9,0x74,0xfa
  8311. };
  8312. #endif
  8313. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8314. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8315. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8316. #ifdef HAVE_AES_DECRYPT
  8317. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8318. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8319. #endif
  8320. #endif
  8321. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8322. if (ret != 0)
  8323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8324. else
  8325. enc_inited = 1;
  8326. #ifdef HAVE_AES_DECRYPT
  8327. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8328. if (ret != 0)
  8329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8330. else
  8331. dec_inited = 1;
  8332. #endif
  8333. #ifdef WOLFSSL_AES_128
  8334. /* 128 key tests */
  8335. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8336. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8337. cipher1, sizeof(cipher1));
  8338. if (ret != 0) {
  8339. return ret;
  8340. }
  8341. #endif
  8342. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8343. if (ret != 0)
  8344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8345. #ifdef HAVE_AES_DECRYPT
  8346. /* decrypt uses AES_ENCRYPTION */
  8347. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8348. if (ret != 0)
  8349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8350. #endif
  8351. XMEMSET(cipher, 0, sizeof(cipher));
  8352. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8353. if (ret != 0)
  8354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8355. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8356. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8357. #ifdef HAVE_AES_DECRYPT
  8358. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8359. if (ret != 0)
  8360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8361. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  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 tests */
  8367. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8368. if (ret != 0)
  8369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8370. XMEMSET(cipher, 0, sizeof(cipher));
  8371. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8372. if (ret != 0)
  8373. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8374. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8375. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8376. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8377. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8378. cipher2, sizeof(msg2));
  8379. if (ret != 0) {
  8380. return ret;
  8381. }
  8382. #endif
  8383. #endif /* WOLFSSL_AES_192 */
  8384. #ifdef WOLFSSL_AES_256
  8385. /* 256 key tests */
  8386. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8387. if (ret != 0)
  8388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8389. XMEMSET(cipher, 0, sizeof(cipher));
  8390. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8391. if (ret != 0)
  8392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8393. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8394. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8395. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8396. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8397. cipher3, sizeof(msg3));
  8398. if (ret != 0) {
  8399. goto out;
  8400. }
  8401. #endif
  8402. #endif /* WOLFSSL_AES_256 */
  8403. out:
  8404. if (enc_inited)
  8405. wc_AesFree(enc);
  8406. #ifdef HAVE_AES_DECRYPT
  8407. if (dec_inited)
  8408. wc_AesFree(dec);
  8409. #endif
  8410. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8411. if (enc)
  8412. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8413. #ifdef HAVE_AES_DECRYPT
  8414. if (dec)
  8415. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8416. #endif
  8417. #endif
  8418. return ret;
  8419. }
  8420. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8421. #endif /* WOLFSSL_AES_CFB */
  8422. static wc_test_ret_t aes_key_size_test(void)
  8423. {
  8424. wc_test_ret_t ret;
  8425. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8426. Aes *aes;
  8427. #else
  8428. Aes aes[1];
  8429. #endif
  8430. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8431. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8432. #ifndef WOLFSSL_CRYPTOCELL
  8433. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8434. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8435. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8436. #endif
  8437. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8438. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8439. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8440. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8441. byte iv[] = "1234567890abcdef";
  8442. #ifndef HAVE_FIPS
  8443. word32 keySize;
  8444. #endif
  8445. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8446. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8447. return WC_TEST_RET_ENC_ERRNO;
  8448. #endif
  8449. #if !defined(HAVE_FIPS) || \
  8450. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8451. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8452. * supported with that FIPS version */
  8453. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8454. if (ret != BAD_FUNC_ARG)
  8455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8456. #endif
  8457. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8458. /* 0 check OK for FIPSv1 */
  8459. if (ret != 0)
  8460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8461. #ifndef HAVE_FIPS
  8462. /* Parameter Validation testing. */
  8463. ret = wc_AesGetKeySize(NULL, NULL);
  8464. if (ret != BAD_FUNC_ARG)
  8465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8466. ret = wc_AesGetKeySize(aes, NULL);
  8467. if (ret != BAD_FUNC_ARG)
  8468. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8469. ret = wc_AesGetKeySize(NULL, &keySize);
  8470. if (ret != BAD_FUNC_ARG)
  8471. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8472. /* Crashes in FIPS */
  8473. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8474. if (ret != BAD_FUNC_ARG)
  8475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8476. #endif
  8477. /* NULL IV indicates to use all zeros IV. */
  8478. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8479. #ifdef WOLFSSL_AES_128
  8480. if (ret != 0)
  8481. #else
  8482. if (ret != BAD_FUNC_ARG)
  8483. #endif
  8484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8485. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8486. if (ret != BAD_FUNC_ARG)
  8487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8488. /* CryptoCell handles rounds internally */
  8489. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8490. /* PSA don't use aes->rounds */
  8491. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8492. /* Force invalid rounds */
  8493. aes->rounds = 16;
  8494. ret = wc_AesGetKeySize(aes, &keySize);
  8495. if (ret != BAD_FUNC_ARG)
  8496. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8497. #endif
  8498. #endif
  8499. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8500. #ifdef WOLFSSL_AES_128
  8501. if (ret != 0)
  8502. #else
  8503. if (ret != BAD_FUNC_ARG)
  8504. #endif
  8505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8506. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8507. ret = wc_AesGetKeySize(aes, &keySize);
  8508. if (ret != 0 || keySize != sizeof(key16))
  8509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8510. #endif
  8511. #ifndef WOLFSSL_CRYPTOCELL
  8512. /* Cryptocell only supports AES-128 key size */
  8513. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8514. #ifdef WOLFSSL_AES_192
  8515. if (ret != 0)
  8516. #else
  8517. if (ret != BAD_FUNC_ARG)
  8518. #endif
  8519. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8520. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8521. ret = wc_AesGetKeySize(aes, &keySize);
  8522. if (ret != 0 || keySize != sizeof(key24))
  8523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8524. #endif
  8525. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8526. #ifdef WOLFSSL_AES_256
  8527. if (ret != 0)
  8528. #else
  8529. if (ret != BAD_FUNC_ARG)
  8530. #endif
  8531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8532. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  8533. ret = wc_AesGetKeySize(aes, &keySize);
  8534. if (ret != 0 || keySize != sizeof(key32))
  8535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8536. #endif
  8537. #endif /* !WOLFSSL_CRYPTOCELL */
  8538. ret = 0; /* success */
  8539. out:
  8540. wc_AesFree(aes);
  8541. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8542. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8543. #endif
  8544. return ret;
  8545. }
  8546. #if defined(WOLFSSL_AES_XTS)
  8547. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  8548. #ifdef WOLFSSL_AES_128
  8549. static wc_test_ret_t aes_xts_128_test(void)
  8550. {
  8551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8552. XtsAes *aes = NULL;
  8553. #else
  8554. XtsAes aes[1];
  8555. #endif
  8556. int aes_inited = 0;
  8557. wc_test_ret_t ret = 0;
  8558. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  8559. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  8560. /* 128 key tests */
  8561. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8562. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  8563. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  8564. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  8565. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  8566. };
  8567. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8568. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  8569. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8570. };
  8571. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8572. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8573. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  8574. };
  8575. /* plain text test of partial block is not from NIST test vector list */
  8576. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8577. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8578. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8579. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8580. };
  8581. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8582. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  8583. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  8584. };
  8585. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8586. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  8587. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  8588. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  8589. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  8590. };
  8591. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8592. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  8593. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  8594. };
  8595. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8596. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  8597. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  8598. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  8599. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  8600. };
  8601. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8602. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  8603. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  8604. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  8605. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  8606. };
  8607. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  8608. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  8609. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  8610. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  8611. };
  8612. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  8613. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  8614. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8615. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8616. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8617. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8618. };
  8619. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  8620. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8621. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8622. };
  8623. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  8624. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8625. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8626. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8627. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8628. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  8629. };
  8630. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  8631. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  8632. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  8633. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  8634. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  8635. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  8636. };
  8637. #endif /* HAVE_FIPS */
  8638. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8639. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8640. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8641. #endif
  8642. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8643. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8644. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8645. if (ret != 0) {
  8646. printf("EVP_aes_128_xts failed!\n");
  8647. goto out;
  8648. }
  8649. #endif
  8650. XMEMSET(buf, 0, sizeof(buf));
  8651. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  8652. if (ret != 0)
  8653. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8654. else
  8655. aes_inited = 1;
  8656. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  8657. if (ret != 0)
  8658. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8659. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8660. #if defined(WOLFSSL_ASYNC_CRYPT)
  8661. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8662. #endif
  8663. if (ret != 0)
  8664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8665. if (XMEMCMP(c2, buf, sizeof(c2)))
  8666. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8667. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8668. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8669. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8670. #if defined(WOLFSSL_ASYNC_CRYPT)
  8671. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8672. #endif
  8673. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8674. if (ret != 0)
  8675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8676. if (XMEMCMP(c2, buf, sizeof(c2)))
  8677. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8678. #endif
  8679. XMEMSET(buf, 0, sizeof(buf));
  8680. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8681. if (ret != 0)
  8682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8683. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8684. #if defined(WOLFSSL_ASYNC_CRYPT)
  8685. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8686. #endif
  8687. if (ret != 0)
  8688. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8689. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8690. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8691. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8692. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8693. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8694. #if defined(WOLFSSL_ASYNC_CRYPT)
  8695. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8696. #endif
  8697. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8698. if (ret != 0)
  8699. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8700. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8701. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8702. #endif
  8703. /* partial block encryption test */
  8704. XMEMSET(cipher, 0, sizeof(cipher));
  8705. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8706. #if defined(WOLFSSL_ASYNC_CRYPT)
  8707. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8708. #endif
  8709. if (ret != 0)
  8710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8711. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  8712. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8713. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8714. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8715. XMEMSET(cipher, 0, sizeof(cipher));
  8716. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8717. #if defined(WOLFSSL_ASYNC_CRYPT)
  8718. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8719. #endif
  8720. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8721. if (ret != 0)
  8722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8723. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  8724. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8725. #endif
  8726. /* partial block decrypt test */
  8727. XMEMSET(buf, 0, sizeof(buf));
  8728. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8729. if (ret != 0)
  8730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8731. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8732. #if defined(WOLFSSL_ASYNC_CRYPT)
  8733. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8734. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8735. #else
  8736. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8737. #endif
  8738. #endif
  8739. if (ret != 0)
  8740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8741. if (XMEMCMP(pp, buf, sizeof(pp)))
  8742. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8743. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8744. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8745. XMEMSET(buf, 0, sizeof(buf));
  8746. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8747. #if defined(WOLFSSL_ASYNC_CRYPT)
  8748. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8749. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8750. #else
  8751. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8752. #endif
  8753. #endif
  8754. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8755. if (ret != 0)
  8756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8757. if (XMEMCMP(pp, buf, sizeof(pp)))
  8758. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8759. #endif
  8760. /* NIST decrypt test vector */
  8761. XMEMSET(buf, 0, sizeof(buf));
  8762. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8763. #if defined(WOLFSSL_ASYNC_CRYPT)
  8764. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8765. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8766. #else
  8767. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8768. #endif
  8769. #endif
  8770. if (ret != 0)
  8771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8772. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8773. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8774. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  8775. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  8776. XMEMSET(buf, 0, sizeof(buf));
  8777. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8778. #if defined(WOLFSSL_ASYNC_CRYPT)
  8779. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8780. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8781. #else
  8782. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8783. #endif
  8784. #endif
  8785. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  8786. if (ret != 0)
  8787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8788. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8789. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8790. #endif
  8791. /* fail case with decrypting using wrong key */
  8792. XMEMSET(buf, 0, sizeof(buf));
  8793. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8794. #if defined(WOLFSSL_ASYNC_CRYPT)
  8795. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8796. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8797. #else
  8798. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8799. #endif
  8800. #endif
  8801. if (ret != 0)
  8802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8803. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  8804. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8805. /* set correct key and retest */
  8806. XMEMSET(buf, 0, sizeof(buf));
  8807. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  8808. if (ret != 0)
  8809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8810. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8811. #if defined(WOLFSSL_ASYNC_CRYPT)
  8812. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8813. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8814. #else
  8815. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8816. #endif
  8817. #endif
  8818. if (ret != 0)
  8819. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8820. if (XMEMCMP(p2, buf, sizeof(p2)))
  8821. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8822. #ifndef HAVE_FIPS
  8823. /* Test ciphertext stealing in-place. */
  8824. XMEMCPY(buf, p3, sizeof(p3));
  8825. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  8826. if (ret != 0)
  8827. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8828. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  8829. #if defined(WOLFSSL_ASYNC_CRYPT)
  8830. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8831. #endif
  8832. if (ret != 0)
  8833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8834. if (XMEMCMP(c3, buf, sizeof(c3)))
  8835. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8836. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  8837. if (ret != 0)
  8838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8839. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  8840. #if defined(WOLFSSL_ASYNC_CRYPT)
  8841. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8842. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  8843. #else
  8844. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8845. #endif
  8846. #endif
  8847. if (ret != 0)
  8848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8849. if (XMEMCMP(p3, buf, sizeof(p3)))
  8850. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8851. #endif /* !HAVE_FIPS */
  8852. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  8853. !defined(WOLFSSL_AFALG)
  8854. {
  8855. #define LARGE_XTS_SZ 1024
  8856. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8857. byte* large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  8858. DYNAMIC_TYPE_TMP_BUFFER);
  8859. #else
  8860. byte large_input[LARGE_XTS_SZ];
  8861. #endif
  8862. int i;
  8863. int j;
  8864. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8865. if (large_input == NULL)
  8866. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  8867. #endif
  8868. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  8869. large_input[i] = (byte)i;
  8870. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  8871. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  8872. if (ret != 0)
  8873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8874. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  8875. sizeof(i1));
  8876. #if defined(WOLFSSL_ASYNC_CRYPT)
  8877. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8878. #endif
  8879. if (ret != 0)
  8880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8881. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  8882. if (ret != 0)
  8883. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8884. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  8885. sizeof(i1));
  8886. #if defined(WOLFSSL_ASYNC_CRYPT)
  8887. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  8888. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  8889. WC_ASYNC_FLAG_NONE);
  8890. #else
  8891. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8892. #endif
  8893. #endif
  8894. if (ret != 0)
  8895. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8896. for (i = 0; i < j; i++) {
  8897. if (large_input[i] != (byte)i) {
  8898. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8899. }
  8900. }
  8901. }
  8902. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8903. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8904. #endif
  8905. }
  8906. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  8907. * !WOLFSSL_AFALG
  8908. */
  8909. out:
  8910. if (aes_inited)
  8911. wc_AesXtsFree(aes);
  8912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8913. if (aes)
  8914. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8915. #endif
  8916. return ret;
  8917. }
  8918. #endif /* WOLFSSL_AES_128 */
  8919. #ifdef WOLFSSL_AES_256
  8920. static wc_test_ret_t aes_xts_256_test(void)
  8921. {
  8922. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8923. XtsAes *aes = NULL;
  8924. #else
  8925. XtsAes aes[1];
  8926. #endif
  8927. int aes_inited = 0;
  8928. wc_test_ret_t ret = 0;
  8929. unsigned char buf[AES_BLOCK_SIZE * 3];
  8930. unsigned char cipher[AES_BLOCK_SIZE * 3];
  8931. /* 256 key tests */
  8932. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8933. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  8934. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  8935. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  8936. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  8937. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  8938. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  8939. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  8940. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  8941. };
  8942. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8943. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  8944. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  8945. };
  8946. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8947. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  8948. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  8949. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  8950. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  8951. };
  8952. /* plain text test of partial block is not from NIST test vector list */
  8953. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8954. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8955. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8956. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8957. };
  8958. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8959. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  8960. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  8961. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  8962. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  8963. };
  8964. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8965. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  8966. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  8967. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  8968. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  8969. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  8970. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  8971. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  8972. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  8973. };
  8974. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8975. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  8976. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  8977. };
  8978. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8979. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  8980. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  8981. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  8982. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  8983. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  8984. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  8985. };
  8986. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8987. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  8988. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  8989. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  8990. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  8991. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  8992. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  8993. };
  8994. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8995. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8996. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8997. #endif
  8998. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8999. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9000. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  9001. if (ret != 0) {
  9002. printf("EVP_aes_256_xts failed\n");
  9003. goto out;
  9004. }
  9005. #endif
  9006. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9007. if (ret != 0)
  9008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9009. else
  9010. aes_inited = 1;
  9011. XMEMSET(buf, 0, sizeof(buf));
  9012. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9013. if (ret != 0)
  9014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9015. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9016. #if defined(WOLFSSL_ASYNC_CRYPT)
  9017. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9018. #endif
  9019. if (ret != 0)
  9020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9021. if (XMEMCMP(c2, buf, sizeof(c2)))
  9022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9023. XMEMSET(buf, 0, sizeof(buf));
  9024. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9025. if (ret != 0)
  9026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9027. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9028. #if defined(WOLFSSL_ASYNC_CRYPT)
  9029. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9030. #endif
  9031. if (ret != 0)
  9032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9033. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9034. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9035. /* partial block encryption test */
  9036. XMEMSET(cipher, 0, sizeof(cipher));
  9037. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9038. #if defined(WOLFSSL_ASYNC_CRYPT)
  9039. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9040. #endif
  9041. if (ret != 0)
  9042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9043. /* partial block decrypt test */
  9044. XMEMSET(buf, 0, sizeof(buf));
  9045. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9046. if (ret != 0)
  9047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9048. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9049. #if defined(WOLFSSL_ASYNC_CRYPT)
  9050. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9051. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9052. #else
  9053. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9054. #endif
  9055. #endif
  9056. if (ret != 0)
  9057. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9058. if (XMEMCMP(pp, buf, sizeof(pp)))
  9059. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9060. /* NIST decrypt test vector */
  9061. XMEMSET(buf, 0, sizeof(buf));
  9062. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9063. #if defined(WOLFSSL_ASYNC_CRYPT)
  9064. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9065. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9066. #else
  9067. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9068. #endif
  9069. #endif
  9070. if (ret != 0)
  9071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9072. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9073. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9074. XMEMSET(buf, 0, sizeof(buf));
  9075. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9076. if (ret != 0)
  9077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9078. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9079. #if defined(WOLFSSL_ASYNC_CRYPT)
  9080. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9081. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9082. #else
  9083. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9084. #endif
  9085. #endif
  9086. if (ret != 0)
  9087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9088. if (XMEMCMP(p2, buf, sizeof(p2)))
  9089. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9090. out:
  9091. if (aes_inited)
  9092. wc_AesXtsFree(aes);
  9093. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9094. if (aes)
  9095. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9096. #endif
  9097. return ret;
  9098. }
  9099. #endif /* WOLFSSL_AES_256 */
  9100. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  9101. /* both 128 and 256 bit key test */
  9102. static wc_test_ret_t aes_xts_sector_test(void)
  9103. {
  9104. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9105. XtsAes *aes = NULL;
  9106. #else
  9107. XtsAes aes[1];
  9108. #endif
  9109. int aes_inited = 0;
  9110. wc_test_ret_t ret = 0;
  9111. unsigned char buf[AES_BLOCK_SIZE * 2];
  9112. /* 128 key tests */
  9113. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9114. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  9115. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  9116. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  9117. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  9118. };
  9119. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9120. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  9121. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  9122. };
  9123. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9124. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  9125. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  9126. };
  9127. word64 s1 = 141;
  9128. /* 256 key tests */
  9129. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9130. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  9131. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  9132. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  9133. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  9134. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  9135. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  9136. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  9137. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  9138. };
  9139. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9140. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  9141. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  9142. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  9143. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  9144. };
  9145. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9146. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  9147. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  9148. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  9149. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  9150. };
  9151. word64 s2 = 187;
  9152. #if !defined(BENCH_EMBEDDED) && \
  9153. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  9154. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  9155. word32 sectorSz = 512;
  9156. unsigned char data[550];
  9157. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9158. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  9159. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  9160. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  9161. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  9162. };
  9163. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9164. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  9165. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9166. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  9167. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  9168. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  9169. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  9170. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  9171. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  9172. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  9173. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  9174. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  9175. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9176. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  9177. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  9178. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  9179. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  9180. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  9181. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  9182. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  9183. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  9184. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  9185. 0xfc, 0xfd, 0xfe, 0xff,
  9186. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  9187. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9188. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  9189. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  9190. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  9191. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  9192. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  9193. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  9194. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  9195. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  9196. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  9197. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9198. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  9199. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  9200. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  9201. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  9202. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  9203. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  9204. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  9205. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  9206. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  9207. 0xfc, 0xfd, 0xfe, 0xff,
  9208. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  9209. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9210. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  9211. };
  9212. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9213. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  9214. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  9215. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  9216. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  9217. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  9218. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  9219. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  9220. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  9221. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  9222. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  9223. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  9224. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  9225. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  9226. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  9227. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  9228. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  9229. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  9230. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  9231. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  9232. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  9233. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  9234. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  9235. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  9236. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  9237. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  9238. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  9239. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  9240. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  9241. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  9242. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  9243. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  9244. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  9245. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  9246. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  9247. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  9248. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  9249. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  9250. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  9251. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  9252. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  9253. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  9254. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  9255. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  9256. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  9257. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  9258. 0x24, 0xe7, 0x3d, 0x6f
  9259. };
  9260. word64 s3 = 0x000000ffffffffff;
  9261. #endif
  9262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9263. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9264. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9265. #endif
  9266. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9267. if (ret != 0)
  9268. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9269. else
  9270. aes_inited = 1;
  9271. XMEMSET(buf, 0, sizeof(buf));
  9272. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9273. if (ret != 0)
  9274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9275. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  9276. #if defined(WOLFSSL_ASYNC_CRYPT)
  9277. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9278. #endif
  9279. if (ret != 0)
  9280. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9281. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9282. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9283. /* decrypt test */
  9284. XMEMSET(buf, 0, sizeof(buf));
  9285. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9286. if (ret != 0)
  9287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9288. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  9289. #if defined(WOLFSSL_ASYNC_CRYPT)
  9290. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9291. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9292. #else
  9293. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9294. #endif
  9295. #endif
  9296. if (ret != 0)
  9297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9298. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9299. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9300. /* 256 bit key tests */
  9301. XMEMSET(buf, 0, sizeof(buf));
  9302. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9303. if (ret != 0)
  9304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9305. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  9306. #if defined(WOLFSSL_ASYNC_CRYPT)
  9307. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9308. #endif
  9309. if (ret != 0)
  9310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9311. if (XMEMCMP(c2, buf, sizeof(c2)))
  9312. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9313. /* decrypt test */
  9314. XMEMSET(buf, 0, sizeof(buf));
  9315. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9316. if (ret != 0)
  9317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9318. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  9319. #if defined(WOLFSSL_ASYNC_CRYPT)
  9320. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9321. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9322. #else
  9323. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9324. #endif
  9325. #endif
  9326. if (ret != 0)
  9327. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9328. if (XMEMCMP(p2, buf, sizeof(p2)))
  9329. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9330. #if !defined(BENCH_EMBEDDED) && \
  9331. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  9332. /* encrypt consecutive sectors test */
  9333. XMEMSET(data, 0, sizeof(buf));
  9334. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  9335. if (ret != 0)
  9336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9337. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  9338. sizeof(p3), s3, sectorSz);
  9339. #if defined(WOLFSSL_ASYNC_CRYPT)
  9340. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9341. #endif
  9342. if (ret != 0)
  9343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9344. if (XMEMCMP(c3, data, sizeof(c3)))
  9345. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9346. /* decrypt consecutive sectors test */
  9347. XMEMSET(data, 0, sizeof(buf));
  9348. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  9349. if (ret != 0)
  9350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9351. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  9352. sizeof(c3), s3, sectorSz);
  9353. #if defined(WOLFSSL_ASYNC_CRYPT)
  9354. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9355. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9356. #else
  9357. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9358. #endif
  9359. #endif
  9360. if (ret != 0)
  9361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9362. if (XMEMCMP(p3, data, sizeof(p3)))
  9363. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9364. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  9365. out:
  9366. if (aes_inited)
  9367. wc_AesXtsFree(aes);
  9368. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9369. if (aes)
  9370. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9371. #endif
  9372. return ret;
  9373. }
  9374. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  9375. #ifdef WOLFSSL_AES_128
  9376. /* testing of bad arguments */
  9377. static wc_test_ret_t aes_xts_args_test(void)
  9378. {
  9379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9380. XtsAes *aes = NULL;
  9381. #else
  9382. XtsAes aes[1];
  9383. #endif
  9384. int aes_inited = 0;
  9385. wc_test_ret_t ret;
  9386. unsigned char buf[AES_BLOCK_SIZE * 2];
  9387. /* 128 key tests */
  9388. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9389. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  9390. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  9391. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  9392. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  9393. };
  9394. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9395. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  9396. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  9397. };
  9398. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9399. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  9400. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  9401. };
  9402. word64 s1 = 141;
  9403. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9404. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9405. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9406. #endif
  9407. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9408. if (ret != 0)
  9409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9410. else
  9411. aes_inited = 1;
  9412. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  9413. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9414. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  9415. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9416. /* encryption operations */
  9417. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9418. if (ret != 0)
  9419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9420. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  9421. #if defined(WOLFSSL_ASYNC_CRYPT)
  9422. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9423. #endif
  9424. if (ret == 0)
  9425. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9426. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  9427. #if defined(WOLFSSL_ASYNC_CRYPT)
  9428. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9429. #endif
  9430. if (ret == 0)
  9431. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9432. /* decryption operations */
  9433. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9434. if (ret != 0)
  9435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9436. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  9437. #if defined(WOLFSSL_ASYNC_CRYPT)
  9438. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9439. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9440. #else
  9441. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9442. #endif
  9443. #endif
  9444. if (ret == 0)
  9445. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9446. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  9447. #if defined(WOLFSSL_ASYNC_CRYPT)
  9448. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9449. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9450. #else
  9451. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9452. #endif
  9453. #endif
  9454. if (ret == 0)
  9455. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9456. ret = 0;
  9457. out:
  9458. if (aes_inited)
  9459. wc_AesXtsFree(aes);
  9460. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9461. if (aes)
  9462. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9463. #endif
  9464. return ret;
  9465. }
  9466. #endif /* WOLFSSL_AES_128 */
  9467. #endif /* WOLFSSL_AES_XTS && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  9468. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  9469. static wc_test_ret_t aes_cbc_test(void)
  9470. {
  9471. byte cipher[AES_BLOCK_SIZE];
  9472. byte plain[AES_BLOCK_SIZE];
  9473. wc_test_ret_t ret;
  9474. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  9475. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9476. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9477. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9478. };
  9479. byte key[] = "0123456789abcdef "; /* align */
  9480. byte iv[] = "1234567890abcdef "; /* align */
  9481. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9482. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9483. /* Parameter Validation testing. */
  9484. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  9485. if (ret != BAD_FUNC_ARG)
  9486. return WC_TEST_RET_ENC_EC(ret);
  9487. #ifdef HAVE_AES_DECRYPT
  9488. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  9489. if (ret != BAD_FUNC_ARG)
  9490. return WC_TEST_RET_ENC_EC(ret);
  9491. #endif
  9492. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  9493. AES_BLOCK_SIZE, iv);
  9494. if (ret != 0)
  9495. return WC_TEST_RET_ENC_EC(ret);
  9496. #ifdef HAVE_AES_DECRYPT
  9497. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  9498. AES_BLOCK_SIZE, iv);
  9499. if (ret != 0)
  9500. return WC_TEST_RET_ENC_EC(ret);
  9501. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  9502. return WC_TEST_RET_ENC_NC;
  9503. #endif /* HAVE_AES_DECRYPT */
  9504. (void)plain;
  9505. return 0;
  9506. }
  9507. #endif
  9508. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9509. static wc_test_ret_t aesecb_test(void)
  9510. {
  9511. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9512. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9513. #else
  9514. Aes enc[1];
  9515. #endif
  9516. int enc_inited = 0;
  9517. byte cipher[AES_BLOCK_SIZE * 4];
  9518. #ifdef HAVE_AES_DECRYPT
  9519. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9520. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9521. #else
  9522. Aes dec[1];
  9523. #endif
  9524. int dec_inited = 0;
  9525. byte plain [AES_BLOCK_SIZE * 4];
  9526. #endif /* HAVE_AES_DECRYPT */
  9527. wc_test_ret_t ret = 0;
  9528. #if defined(WOLFSSL_AES_256)
  9529. {
  9530. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  9531. {
  9532. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9533. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9534. };
  9535. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  9536. {
  9537. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  9538. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  9539. };
  9540. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  9541. {
  9542. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9543. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9544. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9545. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9546. };
  9547. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9548. if (ret != 0)
  9549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9550. enc_inited = 1;
  9551. #if defined(HAVE_AES_DECRYPT)
  9552. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9553. if (ret != 0)
  9554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9555. dec_inited = 1;
  9556. #endif
  9557. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9558. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9559. if (ret != 0)
  9560. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9561. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  9562. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9563. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9564. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9565. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  9566. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9567. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9568. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9569. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9570. if (ret != 0)
  9571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9572. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9573. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9574. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9575. if (ret != 0)
  9576. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9577. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9578. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9579. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9580. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9581. if (ret != 0)
  9582. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9583. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9585. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9586. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9587. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9588. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  9589. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9590. if (ret != 0)
  9591. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9592. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9593. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9594. #endif
  9595. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9596. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9597. if (ret != 0)
  9598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9599. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  9600. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9601. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9602. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9603. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  9604. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9605. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9606. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9607. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9608. if (ret != 0)
  9609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9610. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9611. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9612. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9613. if (ret != 0)
  9614. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9615. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9616. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9617. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9618. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9619. if (ret != 0)
  9620. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9621. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9622. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9623. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9624. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9625. if (ret != 0)
  9626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9627. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9628. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  9629. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9630. if (ret != 0)
  9631. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9632. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9633. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9634. #endif
  9635. }
  9636. out:
  9637. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9638. if (enc) {
  9639. if (enc_inited)
  9640. wc_AesFree(enc);
  9641. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9642. }
  9643. #ifdef HAVE_AES_DECRYPT
  9644. if (dec) {
  9645. if (dec_inited)
  9646. wc_AesFree(dec);
  9647. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9648. }
  9649. #endif
  9650. #else
  9651. if (enc_inited)
  9652. wc_AesFree(enc);
  9653. #ifdef HAVE_AES_DECRYPT
  9654. if (dec_inited)
  9655. wc_AesFree(dec);
  9656. #endif
  9657. #endif
  9658. #endif /* WOLFSSL_AES_256 */
  9659. return ret;
  9660. }
  9661. #endif /* HAVE_AES_ECB */
  9662. #ifdef WOLFSSL_AES_COUNTER
  9663. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  9664. {
  9665. wc_test_ret_t ret = 0;
  9666. /* test vectors from "Recommendation for Block Cipher Modes of
  9667. * Operation" NIST Special Publication 800-38A */
  9668. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  9669. {
  9670. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  9671. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  9672. };
  9673. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  9674. {
  9675. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9676. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  9677. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  9678. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  9679. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  9680. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  9681. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  9682. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  9683. };
  9684. #ifdef WOLFSSL_ARMASM
  9685. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  9686. {
  9687. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  9688. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9689. };
  9690. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  9691. {
  9692. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9693. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  9694. };
  9695. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  9696. {
  9697. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9698. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9699. };
  9700. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  9701. {
  9702. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  9703. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9704. };
  9705. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  9706. {
  9707. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9708. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  9709. };
  9710. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  9711. {
  9712. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  9713. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9714. };
  9715. #endif
  9716. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  9717. {
  9718. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9719. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  9720. };
  9721. #ifdef WOLFSSL_ARMASM
  9722. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  9723. {
  9724. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  9725. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  9726. };
  9727. #endif
  9728. #ifdef WOLFSSL_AES_128
  9729. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  9730. {
  9731. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  9732. 0xc2
  9733. };
  9734. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  9735. {
  9736. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  9737. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  9738. };
  9739. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  9740. {
  9741. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  9742. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  9743. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  9744. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  9745. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  9746. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  9747. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  9748. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  9749. };
  9750. #ifdef WOLFSSL_ARMASM
  9751. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  9752. {
  9753. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  9754. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  9755. 0x7e,0xad,0xea,0x5c,0xd1
  9756. };
  9757. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  9758. {
  9759. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  9760. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  9761. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  9762. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  9763. };
  9764. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  9765. {
  9766. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  9767. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  9768. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  9769. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  9770. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  9771. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  9772. };
  9773. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  9774. {
  9775. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  9776. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  9777. 0xae,0x74,0x94,0x72,0x8e
  9778. };
  9779. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  9780. {
  9781. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  9782. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  9783. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  9784. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  9785. };
  9786. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  9787. {
  9788. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  9789. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  9790. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  9791. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  9792. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  9793. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  9794. };
  9795. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  9796. {
  9797. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  9798. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  9799. 0x9d,0x77,0xc9,0x5f,0x38
  9800. };
  9801. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  9802. {
  9803. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  9804. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  9805. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  9806. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  9807. };
  9808. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  9809. {
  9810. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  9811. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  9812. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  9813. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  9814. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  9815. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  9816. };
  9817. #endif
  9818. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  9819. {
  9820. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  9821. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  9822. 0xd3,0xda,0xe1,0x5b,0x04
  9823. };
  9824. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  9825. {
  9826. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  9827. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  9828. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  9829. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  9830. };
  9831. #ifdef WOLFSSL_ARMASM
  9832. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  9833. {
  9834. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  9835. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  9836. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  9837. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  9838. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  9839. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  9840. };
  9841. #endif
  9842. #endif /* WOLFSSL_AES_128 */
  9843. #ifdef WOLFSSL_AES_192
  9844. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  9845. {
  9846. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9847. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9848. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9849. };
  9850. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  9851. {
  9852. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  9853. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  9854. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  9855. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  9856. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  9857. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  9858. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  9859. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  9860. };
  9861. #ifdef WOLFSSL_ARMASM
  9862. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  9863. {
  9864. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9865. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9866. 0x0c,0x33,0x97,0x06,0xc0
  9867. };
  9868. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  9869. {
  9870. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9871. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9872. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  9873. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  9874. };
  9875. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  9876. {
  9877. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  9878. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  9879. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  9880. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  9881. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  9882. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  9883. };
  9884. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  9885. {
  9886. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9887. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9888. 0x3d,0xf7,0xb4,0xfd,0x8c
  9889. };
  9890. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  9891. {
  9892. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9893. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9894. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  9895. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  9896. };
  9897. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  9898. {
  9899. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  9900. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  9901. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  9902. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  9903. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  9904. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  9905. };
  9906. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  9907. {
  9908. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9909. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9910. 0x47,0xb5,0xf3,0x30,0x3b
  9911. };
  9912. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  9913. {
  9914. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9915. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9916. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  9917. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  9918. };
  9919. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  9920. {
  9921. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  9922. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  9923. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  9924. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  9925. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  9926. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  9927. };
  9928. #endif
  9929. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  9930. {
  9931. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9932. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9933. 0x8c,0x68,0xa7,0xd9,0x57
  9934. };
  9935. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  9936. {
  9937. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9938. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9939. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  9940. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  9941. };
  9942. #ifdef WOLFSSL_ARMASM
  9943. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  9944. {
  9945. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  9946. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  9947. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  9948. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  9949. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  9950. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  9951. };
  9952. #endif
  9953. #endif
  9954. #ifdef WOLFSSL_AES_256
  9955. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  9956. {
  9957. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9958. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9959. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9960. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9961. };
  9962. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  9963. {
  9964. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  9965. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  9966. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  9967. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  9968. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  9969. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  9970. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  9971. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  9972. };
  9973. #ifdef WOLFSSL_ARMASM
  9974. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  9975. {
  9976. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9977. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9978. 0xa2,0x13,0x47,0x74,0xda
  9979. };
  9980. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  9981. {
  9982. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9983. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9984. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  9985. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  9986. };
  9987. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  9988. {
  9989. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  9990. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  9991. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  9992. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  9993. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  9994. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  9995. };
  9996. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  9997. {
  9998. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9999. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  10000. 0xd4,0x94,0x4c,0xcd,0x4d
  10001. };
  10002. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  10003. {
  10004. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  10005. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  10006. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  10007. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  10008. };
  10009. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  10010. {
  10011. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  10012. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  10013. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  10014. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  10015. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  10016. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  10017. };
  10018. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  10019. {
  10020. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  10021. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  10022. 0xb8,0x69,0xa1,0x48,0x45
  10023. };
  10024. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  10025. {
  10026. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  10027. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  10028. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  10029. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  10030. };
  10031. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  10032. {
  10033. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  10034. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  10035. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  10036. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  10037. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  10038. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  10039. };
  10040. #endif
  10041. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  10042. {
  10043. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  10044. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  10045. 0x4b,0x45,0x7c,0xd6,0x8a
  10046. };
  10047. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  10048. {
  10049. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  10050. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  10051. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  10052. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  10053. };
  10054. #ifdef WOLFSSL_ARMASM
  10055. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  10056. {
  10057. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  10058. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  10059. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  10060. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  10061. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  10062. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  10063. };
  10064. #endif
  10065. #endif
  10066. int i;
  10067. struct {
  10068. const byte* key;
  10069. int keySz;
  10070. const byte* iv;
  10071. const byte* plain;
  10072. int len;
  10073. const byte* cipher;
  10074. } testVec[] = {
  10075. #ifdef WOLFSSL_AES_128
  10076. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  10077. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  10078. /* let's try with just 9 bytes, non block size test */
  10079. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  10080. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  10081. /* and an additional 9 bytes to reuse tmp left buffer */
  10082. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  10083. /* Counter wrapping */
  10084. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  10085. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  10086. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  10087. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  10088. ctr128Wrap128CipherLong },
  10089. #ifdef WOLFSSL_ARMASM
  10090. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  10091. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  10092. ctr128Wrap128_2CipherLong },
  10093. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  10094. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  10095. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  10096. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  10097. ctr128Wrap96CipherLong },
  10098. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  10099. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  10100. ctr128Wrap96_2CipherLong },
  10101. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  10102. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  10103. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  10104. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  10105. ctr128Wrap64CipherLong },
  10106. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  10107. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  10108. ctr128Wrap64_2CipherLong },
  10109. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  10110. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  10111. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  10112. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  10113. ctr128Wrap32CipherLong },
  10114. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  10115. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  10116. ctr128Wrap32_2CipherLong },
  10117. #endif
  10118. #endif
  10119. #ifdef WOLFSSL_AES_192
  10120. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  10121. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  10122. /* let's try with just 9 bytes, non block size test */
  10123. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  10124. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  10125. /* Counter wrapping */
  10126. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  10127. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  10128. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  10129. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  10130. ctr192Wrap128CipherLong },
  10131. #ifdef WOLFSSL_ARMASM
  10132. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  10133. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  10134. ctr192Wrap128_2CipherLong },
  10135. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  10136. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  10137. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  10138. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  10139. ctr192Wrap96CipherLong },
  10140. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  10141. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  10142. ctr192Wrap96_2CipherLong },
  10143. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  10144. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  10145. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  10146. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  10147. ctr192Wrap64CipherLong },
  10148. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  10149. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  10150. ctr192Wrap64_2CipherLong },
  10151. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  10152. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  10153. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  10154. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  10155. ctr192Wrap32CipherLong },
  10156. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  10157. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  10158. ctr192Wrap32_2CipherLong },
  10159. #endif
  10160. #endif
  10161. #ifdef WOLFSSL_AES_256
  10162. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  10163. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  10164. /* let's try with just 9 bytes, non block size test */
  10165. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  10166. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  10167. /* Counter wrapping */
  10168. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  10169. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  10170. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  10171. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  10172. ctr256Wrap128CipherLong },
  10173. #ifdef WOLFSSL_ARMASM
  10174. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  10175. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  10176. ctr256Wrap128_2CipherLong },
  10177. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  10178. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  10179. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  10180. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  10181. ctr256Wrap96CipherLong },
  10182. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  10183. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  10184. ctr256Wrap96_2CipherLong },
  10185. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  10186. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  10187. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  10188. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  10189. ctr256Wrap64CipherLong },
  10190. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  10191. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  10192. ctr256Wrap64_2CipherLong },
  10193. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  10194. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  10195. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  10196. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  10197. ctr256Wrap32CipherLong },
  10198. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  10199. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  10200. ctr256Wrap32_2CipherLong },
  10201. #endif
  10202. #endif
  10203. };
  10204. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  10205. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  10206. if (testVec[i].key != NULL) {
  10207. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  10208. testVec[i].iv, AES_ENCRYPTION);
  10209. if (ret != 0) {
  10210. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10211. }
  10212. /* Ctr only uses encrypt, even on key setup */
  10213. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  10214. testVec[i].iv, AES_ENCRYPTION);
  10215. if (ret != 0) {
  10216. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10217. }
  10218. }
  10219. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  10220. if (ret != 0) {
  10221. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10222. }
  10223. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  10224. if (ret != 0) {
  10225. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10226. }
  10227. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  10228. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10229. }
  10230. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  10231. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  10232. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10233. }
  10234. #endif
  10235. }
  10236. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10237. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  10238. if (testVec[i].key != NULL) {
  10239. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  10240. testVec[i].iv, AES_ENCRYPTION);
  10241. if (ret != 0) {
  10242. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10243. }
  10244. /* Ctr only uses encrypt, even on key setup */
  10245. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  10246. testVec[i].iv, AES_ENCRYPTION);
  10247. if (ret != 0) {
  10248. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10249. }
  10250. }
  10251. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10252. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  10253. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10254. if (ret != 0) {
  10255. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10256. }
  10257. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10258. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  10259. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10260. if (ret != 0) {
  10261. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10262. }
  10263. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  10264. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10265. }
  10266. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  10267. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  10268. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10269. }
  10270. #endif
  10271. }
  10272. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  10273. if (testVec[i].key != NULL) {
  10274. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10275. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  10276. testVec[i].iv, AES_ENCRYPTION);
  10277. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10278. if (ret != 0) {
  10279. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10280. }
  10281. /* Ctr only uses encrypt, even on key setup */
  10282. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10283. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  10284. testVec[i].iv, AES_ENCRYPTION);
  10285. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10286. if (ret != 0) {
  10287. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10288. }
  10289. }
  10290. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  10291. if (ret != 0) {
  10292. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10293. }
  10294. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  10295. if (ret != 0) {
  10296. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10297. }
  10298. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  10299. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10300. }
  10301. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  10302. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  10303. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  10304. }
  10305. #endif
  10306. }
  10307. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_AES_C_DYNAMIC_FALLBACK */
  10308. out:
  10309. return ret;
  10310. }
  10311. #endif /* WOLFSSL_AES_COUNTER */
  10312. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  10313. {
  10314. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  10315. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10316. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10317. #else
  10318. Aes enc[1];
  10319. #endif
  10320. int enc_inited = 0;
  10321. byte cipher[AES_BLOCK_SIZE * 4];
  10322. #ifdef HAVE_AES_DECRYPT
  10323. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10324. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10325. #else
  10326. Aes dec[1];
  10327. #endif
  10328. int dec_inited = 0;
  10329. byte plain [AES_BLOCK_SIZE * 4];
  10330. #endif /* HAVE_AES_DECRYPT */
  10331. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  10332. wc_test_ret_t ret = 0;
  10333. #ifdef HAVE_AES_CBC
  10334. #ifdef WOLFSSL_AES_128
  10335. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  10336. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10337. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10338. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10339. };
  10340. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  10341. {
  10342. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  10343. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  10344. };
  10345. #ifdef HAVE_RENESAS_SYNC
  10346. const byte *key =
  10347. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  10348. #else
  10349. WOLFSSL_SMALL_STACK_STATIC const
  10350. byte key[] = "0123456789abcdef "; /* align */
  10351. #endif
  10352. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  10353. WOLFSSL_ENTER("aes_test");
  10354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10355. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  10356. if (enc == NULL)
  10357. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10358. #endif
  10359. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  10360. if (dec == NULL)
  10361. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10362. #endif
  10363. #endif
  10364. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10365. if (ret != 0)
  10366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10367. enc_inited = 1;
  10368. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  10369. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10370. if (ret != 0)
  10371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10372. dec_inited = 1;
  10373. #endif
  10374. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  10375. if (ret != 0)
  10376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10377. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  10378. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  10379. if (ret != 0)
  10380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10381. #endif
  10382. #ifdef HAVE_AES_ECB
  10383. {
  10384. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb[AES_BLOCK_SIZE] = {
  10385. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  10386. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  10387. };
  10388. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  10389. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  10390. #if defined(WOLFSSL_ASYNC_CRYPT)
  10391. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10392. #endif
  10393. if (ret != 0)
  10394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10395. if (XMEMCMP(cipher, verify_ecb, AES_BLOCK_SIZE)) {
  10396. WOLFSSL_MSG("aes_test cipher vs verify_ecb mismatch!");
  10397. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10398. }
  10399. #ifdef HAVE_AES_DECRYPT
  10400. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  10401. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  10402. #if defined(WOLFSSL_ASYNC_CRYPT)
  10403. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10404. #endif
  10405. if (ret != 0)
  10406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10407. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10408. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10409. #endif /* HAVE_AES_DECRYPT */
  10410. }
  10411. #endif
  10412. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  10413. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  10414. #if defined(WOLFSSL_ASYNC_CRYPT)
  10415. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10416. #endif
  10417. if (ret != 0)
  10418. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10419. #ifdef HAVE_AES_DECRYPT
  10420. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  10421. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  10422. #if defined(WOLFSSL_ASYNC_CRYPT)
  10423. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10424. #endif
  10425. if (ret != 0) {
  10426. WOLFSSL_MSG("failed wc_AesCbcDecrypt");
  10427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10428. }
  10429. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) {
  10430. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain compare");
  10431. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10432. }
  10433. #endif /* HAVE_AES_DECRYPT */
  10434. /* skipped because wrapped key use in case of renesas sm */
  10435. #ifndef HAVE_RENESAS_SYNC
  10436. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) {
  10437. WOLFSSL_MSG("wc_AesCbcDecrypt failed cipher-verify compare");
  10438. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10439. }
  10440. #endif
  10441. #endif /* WOLFSSL_AES_128 */
  10442. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  10443. {
  10444. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  10445. /* "All work and no play makes Jack a dull boy. " */
  10446. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10447. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10448. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10449. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10450. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10451. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10452. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10453. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10454. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10455. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10456. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10457. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10458. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10459. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10460. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10461. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10462. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10463. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10464. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10465. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10466. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10467. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10468. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10469. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10470. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10471. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10472. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10473. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10474. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10475. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10476. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10477. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10478. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10479. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10480. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10481. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10482. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  10483. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  10484. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  10485. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  10486. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  10487. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  10488. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  10489. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  10490. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  10491. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  10492. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  10493. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  10494. };
  10495. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  10496. word32 keySz, msgSz;
  10497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10498. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10499. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10500. if ((bigCipher == NULL) ||
  10501. (bigPlain == NULL)) {
  10502. if (bigCipher != NULL)
  10503. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10504. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10505. }
  10506. #else
  10507. byte bigCipher[sizeof(bigMsg)];
  10508. byte bigPlain[sizeof(bigMsg)];
  10509. #endif
  10510. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  10511. * message by AES_BLOCK_SIZE for each size of AES key. */
  10512. for (keySz = 16; keySz <= 32; keySz += 8) {
  10513. for (msgSz = AES_BLOCK_SIZE;
  10514. msgSz <= sizeof(bigMsg);
  10515. msgSz += AES_BLOCK_SIZE) {
  10516. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  10517. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  10518. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  10519. if (ret != 0) {
  10520. ret = WC_TEST_RET_ENC_EC(ret);
  10521. break;
  10522. }
  10523. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  10524. if (ret != 0) {
  10525. ret = WC_TEST_RET_ENC_EC(ret);
  10526. break;
  10527. }
  10528. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  10529. #if defined(WOLFSSL_ASYNC_CRYPT)
  10530. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10531. #endif
  10532. if (ret != 0) {
  10533. ret = WC_TEST_RET_ENC_EC(ret);
  10534. break;
  10535. }
  10536. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  10537. #if defined(WOLFSSL_ASYNC_CRYPT)
  10538. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10539. #endif
  10540. if (ret != 0) {
  10541. ret = WC_TEST_RET_ENC_EC(ret);
  10542. break;
  10543. }
  10544. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  10545. ret = WC_TEST_RET_ENC_NC;
  10546. break;
  10547. }
  10548. }
  10549. if (ret != 0)
  10550. break;
  10551. }
  10552. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  10553. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  10554. * message by AES_BLOCK_SIZE for each size of AES key. */
  10555. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  10556. for (keySz = 16; keySz <= 32; keySz += 8) {
  10557. for (msgSz = AES_BLOCK_SIZE;
  10558. msgSz <= sizeof(bigMsg);
  10559. msgSz += AES_BLOCK_SIZE) {
  10560. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  10561. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  10562. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  10563. if (ret != 0) {
  10564. ret = WC_TEST_RET_ENC_EC(ret);
  10565. break;
  10566. }
  10567. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  10568. if (ret != 0) {
  10569. ret = WC_TEST_RET_ENC_EC(ret);
  10570. break;
  10571. }
  10572. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  10573. #if defined(WOLFSSL_ASYNC_CRYPT)
  10574. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10575. #endif
  10576. if (ret != 0) {
  10577. ret = WC_TEST_RET_ENC_EC(ret);
  10578. break;
  10579. }
  10580. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  10581. #if defined(WOLFSSL_ASYNC_CRYPT)
  10582. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10583. #endif
  10584. if (ret != 0) {
  10585. ret = WC_TEST_RET_ENC_EC(ret);
  10586. break;
  10587. }
  10588. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  10589. ret = WC_TEST_RET_ENC_NC;
  10590. break;
  10591. }
  10592. }
  10593. if (ret != 0)
  10594. break;
  10595. }
  10596. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10597. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_AES_C_DYNAMIC_FALLBACK */
  10598. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10599. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10600. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10601. #endif
  10602. if (ret != 0)
  10603. goto out;
  10604. }
  10605. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  10606. /* Test of AES IV state with encrypt/decrypt */
  10607. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  10608. {
  10609. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  10610. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  10611. */
  10612. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  10613. {
  10614. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  10615. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  10616. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  10617. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  10618. };
  10619. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  10620. {
  10621. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  10622. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  10623. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  10624. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  10625. };
  10626. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  10627. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  10628. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  10629. };
  10630. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  10631. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10632. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  10633. };
  10634. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  10635. if (ret != 0)
  10636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10637. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  10638. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  10639. #if defined(WOLFSSL_ASYNC_CRYPT)
  10640. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10641. #endif
  10642. if (ret != 0)
  10643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10644. #ifndef HAVE_RENESAS_SYNC
  10645. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  10646. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10647. #endif
  10648. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  10649. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  10650. #if defined(WOLFSSL_ASYNC_CRYPT)
  10651. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10652. #endif
  10653. if (ret != 0)
  10654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10655. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  10656. AES_BLOCK_SIZE))
  10657. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10658. #if defined(HAVE_AES_DECRYPT)
  10659. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  10660. if (ret != 0)
  10661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10662. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  10663. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  10664. #if defined(WOLFSSL_ASYNC_CRYPT)
  10665. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10666. #endif
  10667. if (ret != 0)
  10668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10669. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  10670. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10671. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  10672. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  10673. #if defined(WOLFSSL_ASYNC_CRYPT)
  10674. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10675. #endif
  10676. if (ret != 0)
  10677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10678. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  10679. AES_BLOCK_SIZE)) {
  10680. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain-msg2 compare");
  10681. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10682. }
  10683. #endif /* HAVE_AES_DECRYPT */
  10684. }
  10685. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  10686. #endif /* HAVE_AES_CBC */
  10687. #ifdef WOLFSSL_AES_COUNTER
  10688. ret = aesctr_test(enc, dec, cipher, plain);
  10689. if (ret != 0)
  10690. return ret;
  10691. #endif
  10692. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  10693. {
  10694. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  10695. {
  10696. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10697. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10698. };
  10699. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  10700. {
  10701. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  10702. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  10703. };
  10704. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  10705. {
  10706. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10707. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10708. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10709. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10710. };
  10711. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10712. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  10713. if (ret != 0)
  10714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10715. #if !defined(HAVE_SELFTEST) && \
  10716. (defined(WOLFSSL_LINUXKM) || \
  10717. !defined(HAVE_FIPS) || \
  10718. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  10719. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  10720. if (ret != 0)
  10721. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10722. #else
  10723. wc_AesEncryptDirect(enc, cipher, niPlain);
  10724. #endif
  10725. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  10726. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10727. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10728. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  10729. if (ret != 0)
  10730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10731. #if !defined(HAVE_SELFTEST) && \
  10732. (defined(WOLFSSL_LINUXKM) || \
  10733. !defined(HAVE_FIPS) || \
  10734. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  10735. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  10736. if (ret != 0)
  10737. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10738. #else
  10739. wc_AesDecryptDirect(dec, plain, niCipher);
  10740. #endif
  10741. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  10742. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10743. }
  10744. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  10745. #ifndef HAVE_RENESAS_SYNC
  10746. ret = aes_key_size_test();
  10747. if (ret != 0)
  10748. goto out;
  10749. #endif
  10750. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  10751. !defined(HAVE_RENESAS_SYNC)
  10752. ret = aes_cbc_test();
  10753. if (ret != 0)
  10754. goto out;
  10755. #endif
  10756. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10757. ret = aesecb_test();
  10758. if (ret != 0)
  10759. goto out;
  10760. #endif
  10761. out:
  10762. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  10763. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10764. if (enc) {
  10765. if (enc_inited)
  10766. wc_AesFree(enc);
  10767. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10768. }
  10769. #else
  10770. if (enc_inited)
  10771. wc_AesFree(enc);
  10772. #endif
  10773. (void)cipher;
  10774. #ifdef HAVE_AES_DECRYPT
  10775. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10776. if (dec) {
  10777. if (dec_inited)
  10778. wc_AesFree(dec);
  10779. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10780. }
  10781. #else
  10782. if (dec_inited)
  10783. wc_AesFree(dec);
  10784. #endif
  10785. (void)plain;
  10786. #endif /* HAVE_AES_DECRYPT */
  10787. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  10788. return ret;
  10789. }
  10790. #if defined(WOLFSSL_AES_CFB)
  10791. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void)
  10792. {
  10793. int ret;
  10794. WOLFSSL_ENTER("aes_cfb_test");
  10795. ret = aescfb_test_0();
  10796. if (ret != 0)
  10797. return ret;
  10798. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10799. ret = aescfb1_test();
  10800. if (ret != 0)
  10801. return ret;
  10802. ret = aescfb8_test();
  10803. if (ret != 0)
  10804. return ret;
  10805. #endif
  10806. return 0;
  10807. }
  10808. #endif
  10809. #if defined(WOLFSSL_AES_XTS)
  10810. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void)
  10811. {
  10812. int ret = 0;
  10813. WOLFSSL_ENTER("aes_xts_test");
  10814. #ifdef WOLFSSL_AES_128
  10815. ret = aes_xts_128_test();
  10816. if (ret != 0)
  10817. return ret;
  10818. #endif
  10819. #ifdef WOLFSSL_AES_256
  10820. ret = aes_xts_256_test();
  10821. if (ret != 0)
  10822. return ret;
  10823. #endif
  10824. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10825. ret = aes_xts_sector_test();
  10826. if (ret != 0)
  10827. return ret;
  10828. #endif
  10829. #ifdef WOLFSSL_AES_128
  10830. ret = aes_xts_args_test();
  10831. if (ret != 0)
  10832. return ret;
  10833. #endif
  10834. return 0;
  10835. }
  10836. #endif
  10837. #ifdef WOLFSSL_AES_192
  10838. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  10839. {
  10840. #ifdef HAVE_AES_CBC
  10841. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10842. Aes *enc = NULL;
  10843. #else
  10844. Aes enc[1];
  10845. #endif
  10846. int enc_inited = 0;
  10847. byte cipher[AES_BLOCK_SIZE];
  10848. #ifdef HAVE_AES_DECRYPT
  10849. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10850. Aes *dec = NULL;
  10851. #else
  10852. Aes dec[1];
  10853. #endif
  10854. int dec_inited = 0;
  10855. byte plain[AES_BLOCK_SIZE];
  10856. #endif
  10857. #endif /* HAVE_AES_CBC */
  10858. wc_test_ret_t ret = 0;
  10859. #ifdef HAVE_AES_CBC
  10860. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  10861. * Appendix F.2.3 */
  10862. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  10863. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10864. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10865. };
  10866. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  10867. {
  10868. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  10869. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  10870. };
  10871. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  10872. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  10873. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  10874. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  10875. };
  10876. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  10877. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  10878. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  10879. };
  10880. WOLFSSL_ENTER("aes192_test");
  10881. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10882. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10883. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10884. #ifdef HAVE_AES_DECRYPT
  10885. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10886. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10887. #endif
  10888. #endif
  10889. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10890. if (ret != 0)
  10891. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10892. enc_inited = 1;
  10893. #ifdef HAVE_AES_DECRYPT
  10894. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10895. if (ret != 0)
  10896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10897. #endif
  10898. dec_inited = 1;
  10899. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  10900. if (ret != 0)
  10901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10902. #ifdef HAVE_AES_DECRYPT
  10903. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  10904. if (ret != 0)
  10905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10906. #endif
  10907. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  10908. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  10909. #if defined(WOLFSSL_ASYNC_CRYPT)
  10910. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10911. #endif
  10912. if (ret != 0)
  10913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10914. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  10915. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10916. #ifdef HAVE_AES_DECRYPT
  10917. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  10918. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  10919. #if defined(WOLFSSL_ASYNC_CRYPT)
  10920. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10921. #endif
  10922. if (ret != 0)
  10923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10924. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  10925. WOLFSSL_MSG("failed wc_AesCbcDecrypt plain-msg compare");
  10926. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10927. }
  10928. #endif
  10929. out:
  10930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10931. if (enc) {
  10932. if (enc_inited)
  10933. wc_AesFree(enc);
  10934. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10935. }
  10936. #ifdef HAVE_AES_DECRYPT
  10937. if (dec) {
  10938. if (dec_inited)
  10939. wc_AesFree(dec);
  10940. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10941. }
  10942. #endif
  10943. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  10944. if (enc_inited)
  10945. wc_AesFree(enc);
  10946. if (dec_inited)
  10947. wc_AesFree(dec);
  10948. #endif
  10949. #endif /* HAVE_AES_CBC */
  10950. return ret;
  10951. }
  10952. #endif /* WOLFSSL_AES_192 */
  10953. #ifdef WOLFSSL_AES_256
  10954. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  10955. {
  10956. #ifdef HAVE_AES_CBC
  10957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10958. Aes *enc = NULL;
  10959. #else
  10960. Aes enc[1];
  10961. #endif
  10962. int enc_inited = 0;
  10963. byte cipher[AES_BLOCK_SIZE];
  10964. #ifdef HAVE_AES_DECRYPT
  10965. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10966. Aes *dec = NULL;
  10967. #else
  10968. Aes dec[1];
  10969. #endif
  10970. int dec_inited = 0;
  10971. byte plain[AES_BLOCK_SIZE];
  10972. #endif
  10973. #endif /* HAVE_AES_CBC */
  10974. wc_test_ret_t ret = 0;
  10975. #ifdef HAVE_AES_CBC
  10976. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  10977. * Appendix F.2.5 */
  10978. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  10979. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10980. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10981. };
  10982. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  10983. {
  10984. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  10985. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  10986. };
  10987. #ifdef HAVE_RENESAS_SYNC
  10988. byte *key =
  10989. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  10990. int keySz = (256/8);
  10991. #else
  10992. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  10993. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10994. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10995. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10996. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10997. };
  10998. int keySz = (int)sizeof(key);
  10999. #endif
  11000. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  11001. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  11002. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  11003. };
  11004. WOLFSSL_ENTER("aes256_test");
  11005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11006. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11007. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11008. #ifdef HAVE_AES_DECRYPT
  11009. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11010. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11011. #endif
  11012. #endif
  11013. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11014. if (ret != 0)
  11015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11016. enc_inited = 1;
  11017. #ifdef HAVE_AES_DECRYPT
  11018. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11019. if (ret != 0)
  11020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11021. #endif
  11022. dec_inited = 1;
  11023. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  11024. if (ret != 0)
  11025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11026. #ifdef HAVE_AES_DECRYPT
  11027. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  11028. if (ret != 0)
  11029. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11030. #endif
  11031. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11032. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  11033. #if defined(WOLFSSL_ASYNC_CRYPT)
  11034. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11035. #endif
  11036. if (ret != 0)
  11037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11038. #ifdef HAVE_AES_DECRYPT
  11039. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11040. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  11041. #if defined(WOLFSSL_ASYNC_CRYPT)
  11042. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11043. #endif
  11044. if (ret != 0)
  11045. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11046. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  11047. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11048. }
  11049. #endif
  11050. #ifndef HAVE_RENESAS_SYNC
  11051. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  11052. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11053. #endif
  11054. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  11055. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  11056. if (ret != 0)
  11057. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11058. #ifdef HAVE_AES_DECRYPT
  11059. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  11060. if (ret != 0)
  11061. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11062. #endif
  11063. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11064. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11065. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  11066. #if defined(WOLFSSL_ASYNC_CRYPT)
  11067. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11068. #endif
  11069. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11070. if (ret != 0)
  11071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11072. #ifdef HAVE_AES_DECRYPT
  11073. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11074. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11075. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  11076. #if defined(WOLFSSL_ASYNC_CRYPT)
  11077. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11078. #endif
  11079. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11080. if (ret != 0)
  11081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11082. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  11083. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11084. }
  11085. #endif
  11086. #ifndef HAVE_RENESAS_SYNC
  11087. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  11088. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11089. #endif
  11090. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11091. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  11092. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11093. if (ret != 0)
  11094. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11095. #ifdef HAVE_AES_DECRYPT
  11096. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11097. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  11098. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11099. if (ret != 0)
  11100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11101. #endif
  11102. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11103. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  11104. #if defined(WOLFSSL_ASYNC_CRYPT)
  11105. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11106. #endif
  11107. if (ret != 0)
  11108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11109. #ifdef HAVE_AES_DECRYPT
  11110. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11111. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  11112. #if defined(WOLFSSL_ASYNC_CRYPT)
  11113. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11114. #endif
  11115. if (ret != 0)
  11116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11117. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  11118. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11119. }
  11120. #endif
  11121. #ifndef HAVE_RENESAS_SYNC
  11122. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  11123. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11124. #endif
  11125. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_AES_C_DYNAMIC_FALLBACK */
  11126. out:
  11127. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11128. if (enc) {
  11129. if (enc_inited)
  11130. wc_AesFree(enc);
  11131. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11132. }
  11133. #ifdef HAVE_AES_DECRYPT
  11134. if (dec) {
  11135. if (dec_inited)
  11136. wc_AesFree(dec);
  11137. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11138. }
  11139. #endif
  11140. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  11141. if (enc_inited)
  11142. wc_AesFree(enc);
  11143. if (dec_inited)
  11144. wc_AesFree(dec);
  11145. #endif
  11146. #endif /* HAVE_AES_CBC */
  11147. return ret;
  11148. }
  11149. #endif /* WOLFSSL_AES_256 */
  11150. #ifdef HAVE_AESGCM
  11151. #ifdef WOLFSSL_AES_128
  11152. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  11153. byte* plain, int plainSz, byte* cipher, int cipherSz,
  11154. byte* aad, int aadSz, byte* tag, int tagSz)
  11155. {
  11156. wc_test_ret_t ret;
  11157. int enc_inited = 0, dec_inited = 0;
  11158. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11159. Aes *enc = NULL;
  11160. Aes *dec = NULL;
  11161. #else
  11162. Aes enc[1];
  11163. Aes dec[1];
  11164. #endif
  11165. byte resultT[AES_BLOCK_SIZE];
  11166. byte resultP[AES_BLOCK_SIZE * 3];
  11167. byte resultC[AES_BLOCK_SIZE * 3];
  11168. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11169. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11170. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11171. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11172. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11173. #endif
  11174. XMEMSET(resultT, 0, sizeof(resultT));
  11175. XMEMSET(resultC, 0, sizeof(resultC));
  11176. XMEMSET(resultP, 0, sizeof(resultP));
  11177. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11178. if (ret != 0)
  11179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11180. else
  11181. enc_inited = 1;
  11182. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11183. if (ret != 0)
  11184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11185. else
  11186. dec_inited = 1;
  11187. ret = wc_AesGcmSetKey(enc, key, keySz);
  11188. if (ret != 0)
  11189. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11190. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11191. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  11192. resultT, tagSz, aad, aadSz);
  11193. #if defined(WOLFSSL_ASYNC_CRYPT)
  11194. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11195. #endif
  11196. if (ret != 0)
  11197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11198. if (cipher != NULL) {
  11199. if (XMEMCMP(cipher, resultC, cipherSz))
  11200. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11201. }
  11202. if (XMEMCMP(tag, resultT, tagSz))
  11203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11204. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  11205. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11206. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  11207. resultT, tagSz, aad, aadSz);
  11208. #if defined(WOLFSSL_ASYNC_CRYPT)
  11209. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11210. #endif
  11211. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11212. if (ret != 0)
  11213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11214. if (cipher != NULL) {
  11215. if (XMEMCMP(cipher, resultC, cipherSz))
  11216. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11217. }
  11218. if (XMEMCMP(tag, resultT, tagSz))
  11219. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11220. #endif
  11221. #ifdef HAVE_AES_DECRYPT
  11222. ret = wc_AesGcmSetKey(dec, key, keySz);
  11223. if (ret != 0)
  11224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11225. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  11226. iv, ivSz, resultT, tagSz, aad, aadSz);
  11227. #if defined(WOLFSSL_ASYNC_CRYPT)
  11228. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11229. #endif
  11230. if (ret != 0)
  11231. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11232. if (plain != NULL) {
  11233. if (XMEMCMP(plain, resultP, plainSz))
  11234. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11235. }
  11236. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_AES_C_DYNAMIC_FALLBACK)
  11237. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11238. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  11239. iv, ivSz, resultT, tagSz, aad, aadSz);
  11240. #if defined(WOLFSSL_ASYNC_CRYPT)
  11241. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11242. #endif
  11243. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11244. if (ret != 0)
  11245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11246. if (plain != NULL) {
  11247. if (XMEMCMP(plain, resultP, plainSz))
  11248. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11249. }
  11250. #endif
  11251. #endif /* HAVE_AES_DECRYPT */
  11252. ret = 0;
  11253. out:
  11254. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11255. if (enc) {
  11256. if (enc_inited)
  11257. wc_AesFree(enc);
  11258. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11259. }
  11260. if (dec) {
  11261. if (dec_inited)
  11262. wc_AesFree(dec);
  11263. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11264. }
  11265. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  11266. if (enc_inited)
  11267. wc_AesFree(enc);
  11268. if (dec_inited)
  11269. wc_AesFree(dec);
  11270. #endif
  11271. return ret;
  11272. }
  11273. #endif
  11274. /* tests that only use 12 byte IV and 16 or less byte AAD
  11275. * test vectors are from NIST SP 800-38D
  11276. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  11277. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  11278. {
  11279. #ifdef WOLFSSL_AES_128
  11280. byte key1[] = {
  11281. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  11282. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  11283. };
  11284. byte iv1[] = {
  11285. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  11286. 0xe4, 0xed, 0x2f, 0x6d
  11287. };
  11288. ALIGN64 byte plain1[] = {
  11289. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  11290. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  11291. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  11292. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  11293. };
  11294. byte aad1[] = {
  11295. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  11296. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  11297. };
  11298. ALIGN64 byte cipher1[] = {
  11299. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  11300. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  11301. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  11302. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  11303. };
  11304. byte tag1[] = {
  11305. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  11306. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  11307. };
  11308. byte key2[] = {
  11309. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  11310. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  11311. };
  11312. byte iv2[] = {
  11313. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  11314. 0xc9, 0x8a, 0xff, 0xe3
  11315. };
  11316. ALIGN64 byte plain2[] = {
  11317. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  11318. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  11319. };
  11320. ALIGN64 byte cipher2[] = {
  11321. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  11322. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  11323. };
  11324. byte tag2[] = {
  11325. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  11326. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  11327. };
  11328. byte key3[] = {
  11329. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  11330. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  11331. };
  11332. byte iv3[] = {
  11333. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  11334. 0x6a, 0x4b, 0xd5, 0xe1
  11335. };
  11336. byte tag3[] = {
  11337. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  11338. 0x11, 0x64, 0xb2, 0xff
  11339. };
  11340. wc_test_ret_t ret;
  11341. WOLFSSL_ENTER("aesgcm_default_test");
  11342. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  11343. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  11344. aad1, sizeof(aad1), tag1, sizeof(tag1));
  11345. if (ret != 0) {
  11346. return ret;
  11347. }
  11348. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  11349. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  11350. NULL, 0, tag2, sizeof(tag2));
  11351. if (ret != 0) {
  11352. return ret;
  11353. }
  11354. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  11355. NULL, 0, NULL, 0,
  11356. NULL, 0, tag3, sizeof(tag3));
  11357. if (ret != 0) {
  11358. return ret;
  11359. }
  11360. #endif
  11361. return 0;
  11362. }
  11363. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  11364. {
  11365. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11366. Aes *enc = NULL;
  11367. Aes *dec = NULL;
  11368. #else
  11369. Aes enc[1];
  11370. Aes dec[1];
  11371. #endif
  11372. /*
  11373. * This is Test Case 16 from the document Galois/
  11374. * Counter Mode of Operation (GCM) by McGrew and
  11375. * Viega.
  11376. */
  11377. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  11378. {
  11379. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  11380. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  11381. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  11382. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  11383. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  11384. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  11385. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  11386. 0xba, 0x63, 0x7b, 0x39
  11387. };
  11388. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  11389. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  11390. {
  11391. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  11392. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  11393. 0xab, 0xad, 0xda, 0xd2
  11394. };
  11395. #endif
  11396. #ifdef WOLFSSL_AES_256
  11397. #ifdef HAVE_RENESAS_SYNC
  11398. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  11399. int k1Sz = (int)(256/8);
  11400. #else
  11401. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  11402. {
  11403. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  11404. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  11405. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  11406. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  11407. };
  11408. int k1Sz = (int)sizeof(k1);
  11409. #endif
  11410. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  11411. {
  11412. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  11413. 0xde, 0xca, 0xf8, 0x88
  11414. };
  11415. #endif /* WOLFSSL_AES_256 */
  11416. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  11417. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  11418. {
  11419. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  11420. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  11421. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  11422. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  11423. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  11424. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  11425. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  11426. 0xbc, 0xc9, 0xf6, 0x62
  11427. };
  11428. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  11429. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  11430. {
  11431. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  11432. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  11433. };
  11434. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  11435. #if !defined(HAVE_FIPS) && \
  11436. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  11437. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  11438. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  11439. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  11440. !(defined(WOLF_CRYPTO_CB) && \
  11441. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  11442. #define ENABLE_NON_12BYTE_IV_TEST
  11443. #ifdef WOLFSSL_AES_192
  11444. /* Test Case 12, uses same plaintext and AAD data. */
  11445. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  11446. {
  11447. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  11448. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  11449. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  11450. };
  11451. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  11452. {
  11453. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  11454. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  11455. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  11456. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  11457. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  11458. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  11459. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  11460. 0xa6, 0x37, 0xb3, 0x9b
  11461. };
  11462. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  11463. {
  11464. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  11465. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  11466. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  11467. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  11468. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  11469. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  11470. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  11471. 0xe9, 0xb7, 0x37, 0x3b
  11472. };
  11473. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  11474. {
  11475. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  11476. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  11477. };
  11478. #endif /* WOLFSSL_AES_192 */
  11479. #ifdef WOLFSSL_AES_128
  11480. /* The following is an interesting test case from the example
  11481. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  11482. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  11483. {
  11484. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  11485. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  11486. };
  11487. #ifdef HAVE_RENESAS_SYNC
  11488. const byte *k3 =
  11489. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  11490. int k3Sz = (int)(128/8);
  11491. #else
  11492. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  11493. {
  11494. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  11495. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  11496. };
  11497. int k3Sz = (int)sizeof(k3);
  11498. #endif
  11499. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  11500. {
  11501. 0xca
  11502. };
  11503. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  11504. {
  11505. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  11506. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  11507. };
  11508. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  11509. {
  11510. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  11511. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  11512. };
  11513. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  11514. {
  11515. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  11516. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  11517. };
  11518. #endif /* WOLFSSL_AES_128 */
  11519. #ifdef WOLFSSL_AES_256
  11520. int ivlen;
  11521. #endif
  11522. #endif
  11523. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  11524. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  11525. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  11526. wc_test_ret_t ret = 0;
  11527. #ifdef WOLFSSL_AES_256
  11528. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  11529. int alen;
  11530. #endif
  11531. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11532. int plen;
  11533. #endif
  11534. #endif
  11535. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  11536. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  11537. byte bufA[sizeof(a) + 1];
  11538. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11539. #endif
  11540. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  11541. #if !defined(BENCH_AESGCM_LARGE)
  11542. #define BENCH_AESGCM_LARGE 1024
  11543. #endif
  11544. #ifndef WOLFSSL_NO_MALLOC
  11545. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11546. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11547. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11548. if ((! large_input) || (! large_output) || (! large_outdec))
  11549. ERROR_OUT(MEMORY_E, out);
  11550. #else
  11551. byte large_input[BENCH_AESGCM_LARGE];
  11552. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  11553. byte large_outdec[BENCH_AESGCM_LARGE];
  11554. #endif
  11555. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  11556. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  11557. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  11558. #endif
  11559. WOLFSSL_ENTER("aesgcm_test");
  11560. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11561. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11562. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11563. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11564. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  11565. #endif
  11566. XMEMSET(resultT, 0, sizeof(resultT));
  11567. XMEMSET(resultC, 0, sizeof(resultC));
  11568. XMEMSET(resultP, 0, sizeof(resultP));
  11569. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11570. if (ret != 0)
  11571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11572. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11573. if (ret != 0)
  11574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11575. #ifdef WOLFSSL_AES_256
  11576. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  11577. if (ret != 0)
  11578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11579. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11580. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  11581. resultT, sizeof(t1), a, sizeof(a));
  11582. #if defined(WOLFSSL_ASYNC_CRYPT)
  11583. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11584. #endif
  11585. if (ret != 0)
  11586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11587. #ifndef HAVE_RENESAS_SYNC
  11588. if (XMEMCMP(c1, resultC, sizeof(c1)))
  11589. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11590. if (XMEMCMP(t1, resultT, sizeof(t1)))
  11591. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11592. #endif
  11593. #ifdef HAVE_AES_DECRYPT
  11594. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  11595. if (ret != 0)
  11596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11597. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  11598. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11599. #if defined(WOLFSSL_ASYNC_CRYPT)
  11600. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11601. #endif
  11602. if (ret != 0)
  11603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11604. if (XMEMCMP(p, resultP, sizeof(p)))
  11605. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11606. #endif /* HAVE_AES_DECRYPT */
  11607. /* Large buffer test */
  11608. #ifdef BENCH_AESGCM_LARGE
  11609. /* setup test buffer */
  11610. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11611. large_input[alen] = (byte)alen;
  11612. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11613. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11614. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11615. resultT, sizeof(t1), a, sizeof(a));
  11616. #if defined(WOLFSSL_ASYNC_CRYPT)
  11617. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11618. #endif
  11619. if (ret != 0)
  11620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11621. #ifdef HAVE_AES_DECRYPT
  11622. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11623. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11624. sizeof(t1), a, sizeof(a));
  11625. #if defined(WOLFSSL_ASYNC_CRYPT)
  11626. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11627. #endif
  11628. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11629. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11630. if (ret != 0)
  11631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11632. #endif /* HAVE_AES_DECRYPT */
  11633. #endif /* BENCH_AESGCM_LARGE */
  11634. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  11635. /* Variable IV length test */
  11636. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  11637. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11638. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  11639. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  11640. #if defined(WOLFSSL_ASYNC_CRYPT)
  11641. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11642. #endif
  11643. if (ret != 0)
  11644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11645. #ifdef HAVE_AES_DECRYPT
  11646. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  11647. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  11648. #if defined(WOLFSSL_ASYNC_CRYPT)
  11649. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11650. #endif
  11651. if (ret != 0)
  11652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11653. #endif /* HAVE_AES_DECRYPT */
  11654. }
  11655. #endif
  11656. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  11657. /* Variable authenticated data length test */
  11658. for (alen=0; alen<(int)sizeof(p); alen++) {
  11659. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11660. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  11661. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  11662. #if defined(WOLFSSL_ASYNC_CRYPT)
  11663. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11664. #endif
  11665. if (ret != 0)
  11666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11667. #ifdef HAVE_AES_DECRYPT
  11668. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  11669. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  11670. #if defined(WOLFSSL_ASYNC_CRYPT)
  11671. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11672. #endif
  11673. if (ret != 0)
  11674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11675. #endif /* HAVE_AES_DECRYPT */
  11676. }
  11677. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  11678. if (! large_aad)
  11679. ERROR_OUT(MEMORY_E, out);
  11680. XMEMSET(large_aad, 0, 1024+16);
  11681. /* Variable authenticated data length test */
  11682. for (alen=0; alen<=1024; alen+=16) {
  11683. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11684. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  11685. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  11686. if (ret != 0)
  11687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11688. #ifdef HAVE_AES_DECRYPT
  11689. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  11690. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  11691. if (ret != 0)
  11692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11693. #endif /* HAVE_AES_DECRYPT */
  11694. }
  11695. /* Test unaligned memory of all potential arguments */
  11696. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  11697. if (ret != 0)
  11698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11699. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11700. XMEMCPY(&buf[1], p, sizeof(p));
  11701. XMEMCPY(&bufA[1], a, sizeof(a));
  11702. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  11703. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  11704. if (ret != 0)
  11705. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11706. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  11707. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11708. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  11709. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11710. #ifdef HAVE_AES_DECRYPT
  11711. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  11712. if (ret != 0)
  11713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11714. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  11715. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  11716. if (ret != 0)
  11717. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11718. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  11719. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11720. #endif /* HAVE_AES_DECRYPT */
  11721. #endif /* Xilinx Versal */
  11722. #endif
  11723. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11724. #ifdef BENCH_AESGCM_LARGE
  11725. /* Variable plain text length test */
  11726. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  11727. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11728. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11729. plen, iv1, sizeof(iv1), resultT,
  11730. sizeof(t1), a, sizeof(a));
  11731. #if defined(WOLFSSL_ASYNC_CRYPT)
  11732. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11733. #endif
  11734. if (ret != 0)
  11735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11736. #ifdef HAVE_AES_DECRYPT
  11737. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11738. plen, iv1, sizeof(iv1), resultT,
  11739. sizeof(t1), a, sizeof(a));
  11740. #if defined(WOLFSSL_ASYNC_CRYPT)
  11741. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11742. #endif
  11743. if (ret != 0)
  11744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11745. #endif /* HAVE_AES_DECRYPT */
  11746. }
  11747. #else /* BENCH_AESGCM_LARGE */
  11748. /* Variable plain text length test */
  11749. for (plen=1; plen<(int)sizeof(p); plen++) {
  11750. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11751. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  11752. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11753. #if defined(WOLFSSL_ASYNC_CRYPT)
  11754. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11755. #endif
  11756. if (ret != 0)
  11757. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11758. #ifdef HAVE_AES_DECRYPT
  11759. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  11760. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  11761. #if defined(WOLFSSL_ASYNC_CRYPT)
  11762. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11763. #endif
  11764. if (ret != 0)
  11765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11766. #endif /* HAVE_AES_DECRYPT */
  11767. }
  11768. #endif /* BENCH_AESGCM_LARGE */
  11769. #endif
  11770. #endif /* WOLFSSL_AES_256 */
  11771. /* test with IV != 12 bytes */
  11772. #ifdef ENABLE_NON_12BYTE_IV_TEST
  11773. XMEMSET(resultT, 0, sizeof(resultT));
  11774. XMEMSET(resultC, 0, sizeof(resultC));
  11775. XMEMSET(resultP, 0, sizeof(resultP));
  11776. #ifdef WOLFSSL_AES_192
  11777. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  11778. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11779. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  11780. resultT, sizeof(t1), a, sizeof(a));
  11781. #if defined(WOLFSSL_ASYNC_CRYPT)
  11782. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11783. #endif
  11784. if (ret != 0)
  11785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11786. if (XMEMCMP(c2, resultC, sizeof(c2)))
  11787. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11788. if (XMEMCMP(t2, resultT, sizeof(t1)))
  11789. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11790. #ifdef HAVE_AES_DECRYPT
  11791. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  11792. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  11793. #if defined(WOLFSSL_ASYNC_CRYPT)
  11794. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11795. #endif
  11796. if (ret != 0)
  11797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11798. if (XMEMCMP(p, resultP, sizeof(p)))
  11799. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11800. #endif /* HAVE_AES_DECRYPT */
  11801. /* Large buffer test */
  11802. #ifdef BENCH_AESGCM_LARGE
  11803. wc_AesGcmSetKey(enc, k2, k3Sz);
  11804. wc_AesGcmSetKey(dec, k2, k3Sz);
  11805. /* setup test buffer */
  11806. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11807. large_input[alen] = (byte)alen;
  11808. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11809. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11810. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11811. resultT, sizeof(t1), a, sizeof(a));
  11812. #if defined(WOLFSSL_ASYNC_CRYPT)
  11813. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11814. #endif
  11815. if (ret != 0)
  11816. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11817. #ifdef HAVE_AES_DECRYPT
  11818. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11819. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11820. sizeof(t1), a, sizeof(a));
  11821. #if defined(WOLFSSL_ASYNC_CRYPT)
  11822. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11823. #endif
  11824. if (ret != 0)
  11825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11826. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11827. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11828. #endif /* HAVE_AES_DECRYPT */
  11829. #endif /* BENCH_AESGCM_LARGE */
  11830. XMEMSET(resultT, 0, sizeof(resultT));
  11831. XMEMSET(resultC, 0, sizeof(resultC));
  11832. XMEMSET(resultP, 0, sizeof(resultP));
  11833. #endif /* WOLFSSL_AES_192 */
  11834. #ifdef WOLFSSL_AES_128
  11835. wc_AesGcmSetKey(enc, k3, k3Sz);
  11836. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11837. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  11838. resultT, sizeof(t3), a3, sizeof(a3));
  11839. #if defined(WOLFSSL_ASYNC_CRYPT)
  11840. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11841. #endif
  11842. if (ret != 0)
  11843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11844. #ifndef HAVE_RENESAS_SYNC
  11845. if (XMEMCMP(c3, resultC, sizeof(c3)))
  11846. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11847. if (XMEMCMP(t3, resultT, sizeof(t3)))
  11848. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11849. #endif
  11850. #ifdef HAVE_AES_DECRYPT
  11851. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  11852. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  11853. #if defined(WOLFSSL_ASYNC_CRYPT)
  11854. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11855. #endif
  11856. if (ret != 0)
  11857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11858. if (XMEMCMP(p3, resultP, sizeof(p3)))
  11859. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11860. #endif /* HAVE_AES_DECRYPT */
  11861. /* Large buffer test */
  11862. #ifdef BENCH_AESGCM_LARGE
  11863. wc_AesGcmSetKey(enc, k3, k3Sz);
  11864. wc_AesGcmSetKey(dec, k3, k3Sz);
  11865. /* setup test buffer */
  11866. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  11867. large_input[alen] = (byte)alen;
  11868. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11869. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  11870. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  11871. resultT, sizeof(t1), a, sizeof(a));
  11872. #if defined(WOLFSSL_ASYNC_CRYPT)
  11873. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11874. #endif
  11875. if (ret != 0)
  11876. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11877. #ifdef HAVE_AES_DECRYPT
  11878. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  11879. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  11880. sizeof(t1), a, sizeof(a));
  11881. #if defined(WOLFSSL_ASYNC_CRYPT)
  11882. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11883. #endif
  11884. if (ret != 0)
  11885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11886. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  11887. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11888. #endif /* HAVE_AES_DECRYPT */
  11889. #endif /* BENCH_AESGCM_LARGE */
  11890. #endif /* WOLFSSL_AES_128 */
  11891. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  11892. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  11893. !defined(WOLFSSL_XILINX_CRYPT) && \
  11894. !(defined(WOLF_CRYPTO_CB) && \
  11895. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  11896. XMEMSET(resultT, 0, sizeof(resultT));
  11897. XMEMSET(resultC, 0, sizeof(resultC));
  11898. XMEMSET(resultP, 0, sizeof(resultP));
  11899. wc_AesGcmSetKey(enc, k1, k1Sz);
  11900. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  11901. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  11902. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  11903. #if defined(WOLFSSL_ASYNC_CRYPT)
  11904. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11905. #endif
  11906. if (ret != 0)
  11907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11908. #ifndef HAVE_RENESAS_SYNC
  11909. if (XMEMCMP(c1, resultC, sizeof(c1)))
  11910. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11911. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  11912. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11913. #endif
  11914. #ifdef HAVE_AES_DECRYPT
  11915. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  11916. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  11917. #if defined(WOLFSSL_ASYNC_CRYPT)
  11918. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11919. #endif
  11920. if (ret != 0)
  11921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11922. if (XMEMCMP(p, resultP, sizeof(p)))
  11923. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11924. #endif /* HAVE_AES_DECRYPT */
  11925. #endif /* WOLFSSL_AES_256 */
  11926. #if !defined(HAVE_FIPS) || \
  11927. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11928. /* Test encrypt with internally generated IV */
  11929. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  11930. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  11931. {
  11932. WC_RNG rng;
  11933. byte randIV[12];
  11934. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11935. if (ret != 0)
  11936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11937. XMEMSET(randIV, 0, sizeof(randIV));
  11938. XMEMSET(resultT, 0, sizeof(resultT));
  11939. XMEMSET(resultC, 0, sizeof(resultC));
  11940. XMEMSET(resultP, 0, sizeof(resultP));
  11941. wc_AesGcmSetKey(enc, k1, k1Sz);
  11942. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  11943. if (ret != 0)
  11944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11945. ret = wc_AesGcmEncrypt_ex(enc,
  11946. resultC, p, sizeof(p),
  11947. randIV, sizeof(randIV),
  11948. resultT, sizeof(t1),
  11949. a, sizeof(a));
  11950. #if defined(WOLFSSL_ASYNC_CRYPT)
  11951. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11952. #endif
  11953. if (ret != 0)
  11954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11955. /* Check the IV has been set. */
  11956. {
  11957. word32 i, ivSum = 0;
  11958. for (i = 0; i < sizeof(randIV); i++)
  11959. ivSum += randIV[i];
  11960. if (ivSum == 0)
  11961. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11962. }
  11963. #ifdef HAVE_AES_DECRYPT
  11964. wc_AesGcmSetKey(dec, k1, k1Sz);
  11965. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  11966. if (ret != 0)
  11967. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11968. ret = wc_AesGcmDecrypt(dec,
  11969. resultP, resultC, sizeof(c1),
  11970. randIV, sizeof(randIV),
  11971. resultT, sizeof(t1),
  11972. a, sizeof(a));
  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(p, resultP, sizeof(p)))
  11979. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11980. #endif /* HAVE_AES_DECRYPT */
  11981. wc_FreeRng(&rng);
  11982. }
  11983. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  11984. #endif /* HAVE_FIPS_VERSION >= 2 */
  11985. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  11986. #ifdef WOLFSSL_AES_256
  11987. #ifdef WOLFSSL_AESGCM_STREAM
  11988. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  11989. if (ret != 0)
  11990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11991. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  11992. if (ret != 0)
  11993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11994. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  11995. if (ret != 0)
  11996. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11997. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  11998. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11999. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  12000. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12001. #ifdef HAVE_AES_DECRYPT
  12002. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  12003. if (ret != 0)
  12004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12005. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  12006. if (ret != 0)
  12007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12008. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  12009. if (ret != 0)
  12010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12011. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  12012. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12013. #endif
  12014. /* alen is the size to pass in with each update. */
  12015. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  12016. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  12017. if (ret != 0)
  12018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12019. /* plen is the offset into AAD to update with. */
  12020. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  12021. int len = sizeof(a) - plen;
  12022. if (len > alen) len = alen;
  12023. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  12024. if (ret != 0)
  12025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12026. }
  12027. /* plen is the offset into plaintext to update with. */
  12028. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  12029. int len = sizeof(p) - plen;
  12030. if (len > alen) len = alen;
  12031. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  12032. NULL, 0);
  12033. if (ret != 0)
  12034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12035. }
  12036. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  12037. if (ret != 0)
  12038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12039. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  12040. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12041. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  12042. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12043. }
  12044. #ifdef HAVE_AES_DECRYPT
  12045. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  12046. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  12047. if (ret != 0)
  12048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12049. /* plen is the offset into AAD to update with. */
  12050. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  12051. int len = sizeof(a) - plen;
  12052. if (len > alen) len = alen;
  12053. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  12054. if (ret != 0)
  12055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12056. }
  12057. /* plen is the offset into cipher text to update with. */
  12058. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  12059. int len = sizeof(c1) - plen;
  12060. if (len > alen) len = alen;
  12061. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  12062. NULL, 0);
  12063. if (ret != 0)
  12064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12065. }
  12066. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  12067. if (ret != 0)
  12068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12069. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  12070. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12071. }
  12072. #endif /* HAVE_AES_DECRYPT */
  12073. #ifdef BENCH_AESGCM_LARGE
  12074. /* setup test buffer */
  12075. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  12076. if (ret != 0)
  12077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12078. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  12079. BENCH_AESGCM_LARGE, a, sizeof(a));
  12080. if (ret != 0)
  12081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12082. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  12083. if (ret != 0)
  12084. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12085. #ifdef HAVE_AES_DECRYPT
  12086. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  12087. if (ret != 0)
  12088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12089. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  12090. BENCH_AESGCM_LARGE, a, sizeof(a));
  12091. if (ret != 0)
  12092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12093. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  12094. if (ret != 0)
  12095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12096. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  12097. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12098. #endif /* HAVE_AES_DECRYPT */
  12099. #endif /* BENCH_AESGCM_LARGE */
  12100. #endif /* WOLFSSL_AESGCM_STREAM */
  12101. #endif /* WOLFSSL_AES_256 */
  12102. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  12103. wc_AesFree(enc);
  12104. wc_AesFree(dec);
  12105. ret = 0;
  12106. out:
  12107. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  12108. !defined(WOLFSSL_NO_MALLOC)
  12109. if (large_input)
  12110. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12111. if (large_output)
  12112. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12113. if (large_outdec)
  12114. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12115. #endif
  12116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12117. if (enc)
  12118. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12119. if (dec)
  12120. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12121. #endif
  12122. return ret;
  12123. }
  12124. #ifdef WOLFSSL_AES_128
  12125. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  12126. {
  12127. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12128. Gmac *gmac;
  12129. #else
  12130. Gmac gmac[1];
  12131. #endif
  12132. wc_test_ret_t ret;
  12133. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  12134. {
  12135. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  12136. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  12137. };
  12138. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  12139. {
  12140. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  12141. 0xe2, 0x8c, 0x8f, 0x16
  12142. };
  12143. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  12144. {
  12145. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  12146. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  12147. };
  12148. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  12149. {
  12150. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  12151. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  12152. };
  12153. #if (!defined(HAVE_FIPS) || \
  12154. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  12155. /* FIPS builds only allow 16-byte auth tags. */
  12156. /* This sample uses a 15-byte auth tag. */
  12157. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  12158. {
  12159. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  12160. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  12161. };
  12162. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  12163. {
  12164. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  12165. 0x1a, 0x60, 0x24, 0xa7
  12166. };
  12167. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  12168. {
  12169. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  12170. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  12171. };
  12172. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  12173. {
  12174. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  12175. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  12176. };
  12177. #endif
  12178. byte tag[16];
  12179. WOLFSSL_ENTER("gmac_test");
  12180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12181. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12182. return WC_TEST_RET_ENC_ERRNO;
  12183. #endif
  12184. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  12185. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  12186. XMEMSET(tag, 0, sizeof(tag));
  12187. wc_GmacSetKey(gmac, k1, sizeof(k1));
  12188. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  12189. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  12190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12191. #if (!defined(HAVE_FIPS) || \
  12192. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  12193. XMEMSET(tag, 0, sizeof(tag));
  12194. wc_GmacSetKey(gmac, k2, sizeof(k2));
  12195. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  12196. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  12197. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12198. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  12199. {
  12200. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  12201. {
  12202. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  12203. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  12204. };
  12205. WC_RNG rng;
  12206. byte iv[12];
  12207. #ifndef HAVE_FIPS
  12208. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12209. if (ret != 0)
  12210. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12211. #else
  12212. ret = wc_InitRng(&rng);
  12213. if (ret != 0)
  12214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12215. #endif
  12216. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  12217. t1, sizeof(t1));
  12218. if (ret != 0)
  12219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12220. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  12221. badT, sizeof(badT));
  12222. if (ret != AES_GCM_AUTH_E)
  12223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12224. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  12225. t2, sizeof(t2));
  12226. if (ret != 0)
  12227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12228. XMEMSET(tag, 0, sizeof(tag));
  12229. XMEMSET(iv, 0, sizeof(iv));
  12230. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  12231. tag, sizeof(tag), &rng);
  12232. if (ret != 0)
  12233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12234. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  12235. tag, sizeof(tag));
  12236. if (ret != 0)
  12237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12238. wc_FreeRng(&rng);
  12239. }
  12240. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  12241. #endif /* HAVE_FIPS */
  12242. ret = 0;
  12243. out:
  12244. wc_AesFree(&gmac->aes);
  12245. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12246. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  12247. #endif
  12248. return ret;
  12249. }
  12250. #endif /* WOLFSSL_AES_128 */
  12251. #endif /* HAVE_AESGCM */
  12252. #if defined(HAVE_AESCCM)
  12253. #if defined(WOLFSSL_AES_256)
  12254. static wc_test_ret_t aesccm_256_test(void)
  12255. {
  12256. wc_test_ret_t ret;
  12257. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  12258. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  12259. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  12260. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  12261. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  12262. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  12263. };
  12264. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  12265. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  12266. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  12267. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  12268. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  12269. 0x20, 0x21, 0x22, 0x23};
  12270. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  12271. 0x8A, 0xB1, 0xA8, 0x74};
  12272. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  12273. 0x95, 0xFC, 0x08, 0x20};
  12274. byte output[sizeof(in_plaintext)];
  12275. byte atag[sizeof(exp_tag)];
  12276. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12277. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  12278. if (aes == NULL) {
  12279. return MEMORY_E;
  12280. }
  12281. #else
  12282. Aes aes[1];
  12283. #endif
  12284. ret = wc_AesInit(aes, HEAP_HINT, devId);
  12285. if (ret == 0) {
  12286. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  12287. }
  12288. if (ret == 0) {
  12289. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  12290. in_nonce, sizeof(in_nonce),
  12291. atag, sizeof(atag),
  12292. in_auth, sizeof(in_auth));
  12293. }
  12294. /* Verify we produce the proper ciphertext and tag */
  12295. if (ret == 0 &&
  12296. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  12297. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  12298. ret = WC_TEST_RET_ENC_NC;
  12299. }
  12300. if (ret == 0) {
  12301. /* decrypt inline */
  12302. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  12303. in_nonce, sizeof(in_nonce),
  12304. atag, sizeof(atag),
  12305. in_auth, sizeof(in_auth));
  12306. }
  12307. /* Verify decryption was successful */
  12308. if (ret == 0 &&
  12309. XMEMCMP(output, in_plaintext, sizeof(output))) {
  12310. ret = WC_TEST_RET_ENC_NC;
  12311. }
  12312. wc_AesFree(aes);
  12313. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12314. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  12315. #endif
  12316. return ret;
  12317. }
  12318. #endif /* WOLFSSL_AES_256 */
  12319. #if defined(WOLFSSL_AES_128)
  12320. static wc_test_ret_t aesccm_128_test(void)
  12321. {
  12322. wc_test_ret_t ret;
  12323. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12324. Aes *enc;
  12325. #else
  12326. Aes enc[1];
  12327. #endif
  12328. /* key */
  12329. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  12330. {
  12331. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  12332. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  12333. };
  12334. /* nonce */
  12335. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  12336. {
  12337. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  12338. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  12339. };
  12340. /* plaintext */
  12341. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  12342. {
  12343. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  12344. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12345. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  12346. };
  12347. /* plaintext - long */
  12348. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  12349. {
  12350. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  12351. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12352. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  12353. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  12354. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  12355. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12356. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  12357. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  12358. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  12359. 0x50
  12360. };
  12361. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  12362. {
  12363. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12364. };
  12365. /* ciphertext */
  12366. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  12367. {
  12368. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  12369. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  12370. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  12371. };
  12372. /* tag - authentication */
  12373. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  12374. {
  12375. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  12376. };
  12377. /* ciphertext - long */
  12378. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  12379. {
  12380. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  12381. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  12382. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  12383. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  12384. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  12385. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  12386. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  12387. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  12388. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  12389. 0x0b
  12390. };
  12391. /* tag - authentication - long */
  12392. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  12393. {
  12394. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  12395. };
  12396. /* tag - authentication - empty plaintext */
  12397. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  12398. {
  12399. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  12400. };
  12401. byte t2[sizeof(t)];
  12402. byte p2[sizeof(p)];
  12403. byte c2[sizeof(c)];
  12404. byte iv2[sizeof(iv)];
  12405. byte pl2[sizeof(pl)];
  12406. byte cl2[sizeof(cl)];
  12407. byte tl2[sizeof(tl)];
  12408. byte t_empty2[sizeof(t_empty)];
  12409. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12410. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12411. return WC_TEST_RET_ENC_ERRNO;
  12412. #endif
  12413. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  12414. XMEMSET(t2, 0, sizeof(t2));
  12415. XMEMSET(c2, 0, sizeof(c2));
  12416. XMEMSET(p2, 0, sizeof(p2));
  12417. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12418. if (ret != 0)
  12419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12420. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  12421. if (ret != 0)
  12422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12423. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  12424. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  12425. t2, sizeof(t2), a, sizeof(a));
  12426. if (ret != 0)
  12427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12428. if (XMEMCMP(c, c2, sizeof(c2)))
  12429. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12430. if (XMEMCMP(t, t2, sizeof(t2)))
  12431. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12432. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  12433. t2, sizeof(t2), a, sizeof(a));
  12434. if (ret != 0)
  12435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12436. if (XMEMCMP(p, p2, sizeof(p2)))
  12437. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12438. /* Test the authentication failure */
  12439. t2[0]++; /* Corrupt the authentication tag. */
  12440. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  12441. t2, sizeof(t2), a, sizeof(a));
  12442. if (ret == 0)
  12443. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12444. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  12445. * authentication fail. */
  12446. XMEMSET(c2, 0, sizeof(c2));
  12447. if (XMEMCMP(p2, c2, sizeof(p2)))
  12448. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12449. wc_AesFree(enc);
  12450. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  12451. XMEMSET(t2, 0, sizeof(t2));
  12452. XMEMSET(c2, 0, sizeof(c2));
  12453. XMEMSET(p2, 0, sizeof(p2));
  12454. XMEMSET(iv2, 0, sizeof(iv2));
  12455. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12456. if (ret != 0)
  12457. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12458. #ifndef HAVE_SELFTEST
  12459. /* selftest build does not have wc_AesCcmSetNonce() or
  12460. * wc_AesCcmEncrypt_ex() */
  12461. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  12462. if (ret != 0)
  12463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12464. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  12465. if (ret != 0)
  12466. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12467. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  12468. t2, sizeof(t2), a, sizeof(a));
  12469. if (ret != 0)
  12470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12471. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  12472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12473. if (XMEMCMP(c, c2, sizeof(c2)))
  12474. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12475. if (XMEMCMP(t, t2, sizeof(t2)))
  12476. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12477. #endif
  12478. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12479. /* test fail on invalid IV sizes */
  12480. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  12481. if (ret != 0)
  12482. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12483. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  12484. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  12485. t2, 1, a, sizeof(a));
  12486. if (ret == 0) {
  12487. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12488. }
  12489. #endif
  12490. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  12491. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  12492. tl2, sizeof(tl2), a, sizeof(a));
  12493. if (ret != 0)
  12494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12495. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  12496. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12497. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  12498. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12499. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  12500. tl2, sizeof(tl2), a, sizeof(a));
  12501. if (ret != 0)
  12502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12503. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  12504. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12505. /* test empty message as null input or output with nonzero inSz. */
  12506. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  12507. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12508. a, sizeof(a));
  12509. if (ret != BAD_FUNC_ARG)
  12510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12511. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  12512. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12513. a, sizeof(a));
  12514. if (ret != BAD_FUNC_ARG)
  12515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12516. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  12517. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12518. sizeof(a));
  12519. if (ret != BAD_FUNC_ARG)
  12520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12521. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  12522. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12523. sizeof(a));
  12524. if (ret != BAD_FUNC_ARG)
  12525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12526. /* test empty message as null input and output with zero inSz --
  12527. * must either succeed, or fail early with BAD_FUNC_ARG.
  12528. */
  12529. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  12530. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  12531. a, sizeof(a));
  12532. if (ret != BAD_FUNC_ARG) {
  12533. if (ret != 0)
  12534. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12535. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  12536. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12537. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  12538. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  12539. sizeof(t_empty2), a, sizeof(a));
  12540. if (ret != 0)
  12541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12542. }
  12543. /* test empty message as zero-length string -- must work. */
  12544. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  12545. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12546. sizeof(a));
  12547. if (ret != 0)
  12548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12549. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  12550. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12551. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  12552. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  12553. sizeof(a));
  12554. if (ret != 0)
  12555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12556. wc_AesFree(enc);
  12557. ret = 0;
  12558. out:
  12559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12560. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12561. #endif
  12562. return ret;
  12563. }
  12564. #endif /* WOLFSSL_AES_128 */
  12565. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  12566. {
  12567. wc_test_ret_t ret = 0;
  12568. WOLFSSL_ENTER("aesccm_test");
  12569. #ifdef WOLFSSL_AES_128
  12570. if (ret == 0)
  12571. ret = aesccm_128_test();
  12572. #endif
  12573. #ifdef WOLFSSL_AES_256
  12574. if (ret == 0)
  12575. ret = aesccm_256_test();
  12576. #endif
  12577. return ret;
  12578. }
  12579. #endif /* HAVE_AESCCM */
  12580. #if defined(WOLFSSL_AES_EAX) && \
  12581. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  12582. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  12583. {
  12584. typedef struct {
  12585. byte key[AES_256_KEY_SIZE];
  12586. int key_length;
  12587. byte iv[AES_BLOCK_SIZE];
  12588. int iv_length;
  12589. byte aad[AES_BLOCK_SIZE * 2];
  12590. int aad_length;
  12591. byte msg[AES_BLOCK_SIZE * 2];
  12592. int msg_length;
  12593. byte ct[AES_BLOCK_SIZE * 2];
  12594. int ct_length;
  12595. byte tag[AES_BLOCK_SIZE];
  12596. int tag_length;
  12597. int valid;
  12598. } AadVector;
  12599. /* A small selection of Google wycheproof vectors that use vectors
  12600. * from the original paper: eprint.iacr.org/2003/069
  12601. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  12602. */
  12603. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  12604. /* Vector from paper - empty message with auth data */
  12605. {
  12606. /* key, key length */
  12607. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  12608. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  12609. /* iv, iv length */
  12610. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  12611. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  12612. /* aad, aad length */
  12613. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  12614. /* msg, msg length */
  12615. {0}, 0,
  12616. /* ct, ct length */
  12617. {0}, 0,
  12618. /* tag, tag length */
  12619. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  12620. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  12621. /* valid */
  12622. 1,
  12623. },
  12624. /* Vector from paper - no auth data, valid auth tag */
  12625. {
  12626. /* key, key length */
  12627. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12628. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  12629. /* iv , iv length */
  12630. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  12631. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  12632. /* aad, aad length */
  12633. {0}, 0,
  12634. /* msg, msg length */
  12635. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12636. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12637. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  12638. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  12639. /* ct, ct length */
  12640. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  12641. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  12642. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  12643. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  12644. /* tag, tag length */
  12645. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  12646. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  12647. /* valid */
  12648. 1,
  12649. },
  12650. /* Vector from paper - no auth data with invalid auth tag */
  12651. {
  12652. /* key, key length */
  12653. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12654. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  12655. /* iv, iv length */
  12656. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  12657. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  12658. /* aad, aad length */
  12659. {0}, 0,
  12660. /* msg, msg length */
  12661. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  12662. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  12663. /* ct , ct length */
  12664. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  12665. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  12666. /* tag, tag length */
  12667. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  12668. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  12669. /* valid */
  12670. 0,
  12671. },
  12672. };
  12673. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  12674. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  12675. int i;
  12676. int len;
  12677. wc_test_ret_t ret;
  12678. WOLFSSL_ENTER("aes_eax_test");
  12679. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  12680. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  12681. len = sizeof(authtag);
  12682. ret = wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  12683. ciphertext,
  12684. vectors[i].msg, vectors[i].msg_length,
  12685. vectors[i].iv, vectors[i].iv_length,
  12686. authtag, len,
  12687. vectors[i].aad, vectors[i].aad_length);
  12688. if (ret != 0) {
  12689. return WC_TEST_RET_ENC_EC(ret);
  12690. }
  12691. /* check ciphertext matches vector */
  12692. if (XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length)) {
  12693. return WC_TEST_RET_ENC_NC;
  12694. }
  12695. /* check that tag matches vector only for vectors marked as valid */
  12696. ret = XMEMCMP(authtag, vectors[i].tag, len);
  12697. if (vectors[i].valid == 1 && ret != 0 ) {
  12698. return WC_TEST_RET_ENC_NC;
  12699. }
  12700. else if (vectors[i].valid == 0 && ret == 0) {
  12701. return WC_TEST_RET_ENC_NC;
  12702. }
  12703. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  12704. ret = wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  12705. ciphertext,
  12706. vectors[i].ct, vectors[i].ct_length,
  12707. vectors[i].iv, vectors[i].iv_length,
  12708. authtag, len,
  12709. vectors[i].aad, vectors[i].aad_length);
  12710. if (ret != 0) {
  12711. return WC_TEST_RET_ENC_EC(ret);
  12712. }
  12713. /* check decrypted ciphertext matches vector plaintext */
  12714. if (XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length)) {
  12715. return WC_TEST_RET_ENC_NC;
  12716. }
  12717. }
  12718. return 0;
  12719. }
  12720. #endif /* WOLFSSL_AES_EAX */
  12721. #ifdef HAVE_AES_KEYWRAP
  12722. #define MAX_KEYWRAP_TEST_OUTLEN 40
  12723. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  12724. typedef struct keywrapVector {
  12725. const byte* kek;
  12726. const byte* data;
  12727. const byte* verify;
  12728. word32 kekLen;
  12729. word32 dataLen;
  12730. word32 verifyLen;
  12731. } keywrapVector;
  12732. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  12733. {
  12734. int wrapSz, plainSz, testSz, i;
  12735. /* test vectors from RFC 3394 (kek, data, verify) */
  12736. #ifdef WOLFSSL_AES_128
  12737. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  12738. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12739. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12740. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12741. };
  12742. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  12743. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12744. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12745. };
  12746. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  12747. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  12748. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  12749. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  12750. };
  12751. #endif /* WOLFSSL_AES_128 */
  12752. #ifdef WOLFSSL_AES_192
  12753. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  12754. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  12755. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12756. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12757. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  12758. };
  12759. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  12760. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12761. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12762. };
  12763. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  12764. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  12765. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  12766. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  12767. };
  12768. #endif
  12769. #ifdef WOLFSSL_AES_256
  12770. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  12771. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  12772. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12773. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12774. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12775. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12776. };
  12777. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  12778. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12779. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  12780. };
  12781. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  12782. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  12783. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  12784. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  12785. };
  12786. #endif
  12787. #ifdef WOLFSSL_AES_192
  12788. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  12789. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  12790. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12791. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12792. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  12793. };
  12794. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  12795. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12796. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12797. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12798. };
  12799. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  12800. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  12801. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  12802. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  12803. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  12804. };
  12805. #endif
  12806. #ifdef WOLFSSL_AES_256
  12807. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  12808. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  12809. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12810. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12811. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12812. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12813. };
  12814. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  12815. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12816. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12817. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12818. };
  12819. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  12820. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  12821. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  12822. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  12823. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  12824. };
  12825. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  12826. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  12827. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12828. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  12829. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12830. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  12831. };
  12832. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  12833. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  12834. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  12835. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12836. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  12837. };
  12838. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  12839. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  12840. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  12841. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  12842. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  12843. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  12844. };
  12845. #endif /* WOLFSSL_AES_256 */
  12846. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  12847. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  12848. const keywrapVector test_wrap[] =
  12849. {
  12850. #ifdef WOLFSSL_AES_128
  12851. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  12852. #endif
  12853. #ifdef WOLFSSL_AES_192
  12854. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  12855. #endif
  12856. #ifdef WOLFSSL_AES_256
  12857. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  12858. #endif
  12859. #ifdef WOLFSSL_AES_192
  12860. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  12861. #endif
  12862. #ifdef WOLFSSL_AES_256
  12863. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  12864. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  12865. #endif
  12866. };
  12867. WOLFSSL_ENTER("aeskeywrap_test");
  12868. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  12869. XMEMSET(output, 0, sizeof(output));
  12870. XMEMSET(plain, 0, sizeof(plain));
  12871. for (i = 0; i < testSz; i++) {
  12872. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  12873. test_wrap[i].data, test_wrap[i].dataLen,
  12874. output, sizeof(output), NULL);
  12875. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  12876. return WC_TEST_RET_ENC_NC;
  12877. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  12878. return WC_TEST_RET_ENC_NC;
  12879. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  12880. output, wrapSz,
  12881. plain, sizeof(plain), NULL);
  12882. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  12883. return WC_TEST_RET_ENC_NC;
  12884. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  12885. return WC_TEST_RET_ENC_I(i);
  12886. }
  12887. return 0;
  12888. }
  12889. #endif /* HAVE_AES_KEYWRAP */
  12890. #endif /* NO_AES */
  12891. #ifdef HAVE_ARIA
  12892. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  12893. {
  12894. #ifndef DEBUG_WOLFSSL
  12895. (void)strName;
  12896. (void)data;
  12897. (void)dataSz;
  12898. #else
  12899. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  12900. WOLFSSL_BUFFER(data,dataSz);
  12901. #endif
  12902. }
  12903. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  12904. {
  12905. byte data[] = TEST_STRING;
  12906. word32 dataSz = TEST_STRING_SZ;
  12907. /* Arbitrarily random long key that we will truncate to the right size */
  12908. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  12909. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  12910. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  12911. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  12912. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  12913. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  12914. wc_Aria aria;
  12915. int ret = 0;
  12916. WOLFSSL_ENTER("ariagcm_test");
  12917. XMEMSET((void *)&aria, 0, sizeof(aria));
  12918. ret = wc_AriaInitCrypt(&aria, algo);
  12919. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12920. ret = wc_AriaSetKey(&aria, key);
  12921. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12922. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  12923. printOutput("Key", key, keySz);
  12924. WC_RNG rng;
  12925. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12926. if (ret != 0)
  12927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12928. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  12929. if (ret != 0)
  12930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12931. wc_FreeRng(&rng);
  12932. printOutput("Plaintext", data, sizeof(data));
  12933. XMEMSET(cipher, 0, sizeof(cipher));
  12934. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  12935. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  12936. authTag, authTagSz);
  12937. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12938. printOutput("Ciphertext", cipher, sizeof(cipher));
  12939. printOutput("AuthTag", authTag, sizeof(authTag));
  12940. XMEMSET(plain, 0, sizeof(plain));
  12941. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  12942. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  12943. authTag, authTagSz);
  12944. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  12945. printOutput("Plaintext", plain, sizeof(plain));
  12946. if (XMEMCMP(plain, data, dataSz) != 0)
  12947. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  12948. out:
  12949. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  12950. else { ret = wc_AriaFreeCrypt(&aria); }
  12951. return ret;
  12952. }
  12953. #endif /* HAVE_ARIA */
  12954. #ifdef HAVE_CAMELLIA
  12955. enum {
  12956. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  12957. };
  12958. typedef struct {
  12959. int type;
  12960. const byte* plaintext;
  12961. const byte* iv;
  12962. const byte* ciphertext;
  12963. const byte* key;
  12964. word32 keySz;
  12965. int errorCode;
  12966. } test_vector_t;
  12967. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  12968. {
  12969. /* Camellia ECB Test Plaintext */
  12970. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  12971. {
  12972. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12973. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  12974. };
  12975. /* Camellia ECB Test Initialization Vector */
  12976. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  12977. /* Test 1: Camellia ECB 128-bit key */
  12978. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  12979. {
  12980. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12981. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  12982. };
  12983. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  12984. {
  12985. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  12986. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  12987. };
  12988. /* Test 2: Camellia ECB 192-bit key */
  12989. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  12990. {
  12991. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  12992. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  12993. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  12994. };
  12995. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  12996. {
  12997. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  12998. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  12999. };
  13000. /* Test 3: Camellia ECB 256-bit key */
  13001. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  13002. {
  13003. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13004. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  13005. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  13006. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  13007. };
  13008. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  13009. {
  13010. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  13011. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  13012. };
  13013. /* Camellia CBC Test Plaintext */
  13014. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  13015. {
  13016. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  13017. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  13018. };
  13019. /* Camellia CBC Test Initialization Vector */
  13020. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  13021. {
  13022. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13023. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13024. };
  13025. /* Test 4: Camellia-CBC 128-bit key */
  13026. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  13027. {
  13028. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  13029. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  13030. };
  13031. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  13032. {
  13033. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  13034. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  13035. };
  13036. /* Test 5: Camellia-CBC 192-bit key */
  13037. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  13038. {
  13039. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  13040. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  13041. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  13042. };
  13043. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  13044. {
  13045. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  13046. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  13047. };
  13048. /* Test 6: CBC 256-bit key */
  13049. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  13050. {
  13051. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  13052. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  13053. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  13054. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  13055. };
  13056. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  13057. {
  13058. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  13059. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  13060. };
  13061. byte out[CAMELLIA_BLOCK_SIZE];
  13062. Camellia cam;
  13063. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  13064. {
  13065. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  13066. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  13067. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  13068. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  13069. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  13070. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  13071. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  13072. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  13073. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  13074. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  13075. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  13076. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  13077. };
  13078. int i, testsSz;
  13079. int ret;
  13080. WOLFSSL_ENTER("camellia_test");
  13081. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  13082. for (i = 0; i < testsSz; i++) {
  13083. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  13084. testVectors[i].iv) != 0)
  13085. return testVectors[i].errorCode;
  13086. switch (testVectors[i].type) {
  13087. case CAM_ECB_ENC:
  13088. ret = wc_CamelliaEncryptDirect(&cam, out,
  13089. testVectors[i].plaintext);
  13090. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  13091. CAMELLIA_BLOCK_SIZE))
  13092. return testVectors[i].errorCode;
  13093. break;
  13094. case CAM_ECB_DEC:
  13095. ret = wc_CamelliaDecryptDirect(&cam, out,
  13096. testVectors[i].ciphertext);
  13097. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  13098. CAMELLIA_BLOCK_SIZE))
  13099. return testVectors[i].errorCode;
  13100. break;
  13101. case CAM_CBC_ENC:
  13102. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  13103. CAMELLIA_BLOCK_SIZE);
  13104. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  13105. CAMELLIA_BLOCK_SIZE))
  13106. return testVectors[i].errorCode;
  13107. break;
  13108. case CAM_CBC_DEC:
  13109. ret = wc_CamelliaCbcDecrypt(&cam, out,
  13110. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  13111. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  13112. CAMELLIA_BLOCK_SIZE))
  13113. return testVectors[i].errorCode;
  13114. break;
  13115. default:
  13116. break;
  13117. }
  13118. }
  13119. /* Setting the IV and checking it was actually set. */
  13120. ret = wc_CamelliaSetIV(&cam, ivc);
  13121. if (ret != 0)
  13122. return WC_TEST_RET_ENC_EC(ret);
  13123. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  13124. return WC_TEST_RET_ENC_NC;
  13125. /* Setting the IV to NULL should be same as all zeros IV */
  13126. ret = wc_CamelliaSetIV(&cam, NULL);
  13127. if (ret != 0)
  13128. return WC_TEST_RET_ENC_EC(ret);
  13129. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  13130. return WC_TEST_RET_ENC_NC;
  13131. /* First parameter should never be null */
  13132. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  13133. return WC_TEST_RET_ENC_NC;
  13134. /* First parameter should never be null, check it fails */
  13135. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  13136. return WC_TEST_RET_ENC_NC;
  13137. /* Key should have a size of 16, 24, or 32 */
  13138. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  13139. return WC_TEST_RET_ENC_NC;
  13140. return 0;
  13141. }
  13142. #endif /* HAVE_CAMELLIA */
  13143. #ifdef WOLFSSL_SM4
  13144. #ifdef WOLFSSL_SM4_ECB
  13145. static int sm4_ecb_test(void)
  13146. {
  13147. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  13148. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13149. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13150. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13151. };
  13152. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  13153. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  13154. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  13155. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  13156. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  13157. };
  13158. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  13159. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  13160. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  13161. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  13162. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  13163. };
  13164. wc_Sm4 sm4;
  13165. byte enc[SM4_BLOCK_SIZE * 4];
  13166. byte dec[SM4_BLOCK_SIZE * 4];
  13167. int ret;
  13168. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13169. if (ret != 0)
  13170. return WC_TEST_RET_ENC_EC(ret);
  13171. /* Encrypt and decrypt with ECB. */
  13172. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13173. if (ret != 0)
  13174. return WC_TEST_RET_ENC_EC(ret);
  13175. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  13176. if (ret != 0)
  13177. return WC_TEST_RET_ENC_EC(ret);
  13178. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  13179. return WC_TEST_RET_ENC_NC;
  13180. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  13181. if (ret != 0)
  13182. return WC_TEST_RET_ENC_EC(ret);
  13183. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13184. return WC_TEST_RET_ENC_NC;
  13185. wc_Sm4Free(&sm4);
  13186. return 0;
  13187. }
  13188. #endif
  13189. #ifdef WOLFSSL_SM4_CBC
  13190. static int sm4_cbc_test(void)
  13191. {
  13192. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  13193. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13194. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13195. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13196. };
  13197. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  13198. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  13199. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  13200. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  13201. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  13202. };
  13203. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  13204. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13205. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13206. };
  13207. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  13208. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  13209. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  13210. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  13211. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  13212. };
  13213. wc_Sm4 sm4;
  13214. byte enc[SM4_BLOCK_SIZE * 4];
  13215. byte dec[SM4_BLOCK_SIZE * 4];
  13216. int ret;
  13217. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13218. if (ret != 0)
  13219. return WC_TEST_RET_ENC_EC(ret);
  13220. /* Encrypt and decrypt with CBC. */
  13221. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13222. if (ret != 0)
  13223. return WC_TEST_RET_ENC_EC(ret);
  13224. ret = wc_Sm4SetIV(&sm4, i1);
  13225. if (ret != 0)
  13226. return WC_TEST_RET_ENC_EC(ret);
  13227. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  13228. if (ret != 0)
  13229. return WC_TEST_RET_ENC_EC(ret);
  13230. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  13231. return WC_TEST_RET_ENC_NC;
  13232. ret = wc_Sm4SetIV(&sm4, i1);
  13233. if (ret != 0)
  13234. return WC_TEST_RET_ENC_EC(ret);
  13235. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  13236. if (ret != 0)
  13237. return WC_TEST_RET_ENC_EC(ret);
  13238. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13239. return WC_TEST_RET_ENC_NC;
  13240. /* Encrypt and decrypt in-place with CBC. */
  13241. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13242. if (ret != 0)
  13243. return WC_TEST_RET_ENC_EC(ret);
  13244. ret = wc_Sm4SetIV(&sm4, i1);
  13245. if (ret != 0)
  13246. return WC_TEST_RET_ENC_EC(ret);
  13247. XMEMCPY(enc, p1, sizeof(p1));
  13248. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  13249. if (ret != 0)
  13250. return WC_TEST_RET_ENC_EC(ret);
  13251. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  13252. return WC_TEST_RET_ENC_NC;
  13253. ret = wc_Sm4SetIV(&sm4, i1);
  13254. if (ret != 0)
  13255. return WC_TEST_RET_ENC_EC(ret);
  13256. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  13257. if (ret != 0)
  13258. return WC_TEST_RET_ENC_EC(ret);
  13259. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  13260. return WC_TEST_RET_ENC_NC;
  13261. wc_Sm4Free(&sm4);
  13262. return 0;
  13263. }
  13264. #endif
  13265. #ifdef WOLFSSL_SM4_CTR
  13266. static int sm4_ctr_test(void)
  13267. {
  13268. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  13269. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13270. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13271. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13272. };
  13273. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  13274. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13275. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13276. };
  13277. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  13278. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  13279. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  13280. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  13281. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  13282. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13283. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  13284. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  13285. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  13286. };
  13287. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  13288. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  13289. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  13290. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  13291. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  13292. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  13293. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  13294. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  13295. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  13296. };
  13297. wc_Sm4 sm4;
  13298. byte enc[SM4_BLOCK_SIZE * 4];
  13299. byte dec[SM4_BLOCK_SIZE * 4];
  13300. int chunk;
  13301. int i;
  13302. int ret;
  13303. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13304. if (ret != 0)
  13305. return WC_TEST_RET_ENC_EC(ret);
  13306. /* Encrypt and decrypt using encrypt with CTR. */
  13307. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13308. if (ret != 0)
  13309. return WC_TEST_RET_ENC_EC(ret);
  13310. ret = wc_Sm4SetIV(&sm4, i1);
  13311. if (ret != 0)
  13312. return WC_TEST_RET_ENC_EC(ret);
  13313. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  13314. if (ret != 0)
  13315. return WC_TEST_RET_ENC_EC(ret);
  13316. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  13317. return WC_TEST_RET_ENC_NC;
  13318. ret = wc_Sm4SetIV(&sm4, i1);
  13319. if (ret != 0)
  13320. return WC_TEST_RET_ENC_EC(ret);
  13321. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  13322. if (ret != 0)
  13323. return WC_TEST_RET_ENC_EC(ret);
  13324. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  13325. return WC_TEST_RET_ENC_NC;
  13326. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  13327. ret = wc_Sm4SetIV(&sm4, i1);
  13328. if (ret != 0)
  13329. return WC_TEST_RET_ENC_I(chunk);
  13330. XMEMSET(enc, 0, sizeof(enc));
  13331. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  13332. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  13333. if (ret != 0)
  13334. return WC_TEST_RET_ENC_I(i);
  13335. }
  13336. if (i < (int)sizeof(p2)) {
  13337. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  13338. if (ret != 0)
  13339. return WC_TEST_RET_ENC_I(chunk);
  13340. }
  13341. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  13342. return WC_TEST_RET_ENC_I(chunk);
  13343. }
  13344. wc_Sm4Free(&sm4);
  13345. return 0;
  13346. }
  13347. #endif
  13348. #ifdef WOLFSSL_SM4_GCM
  13349. static int sm4_gcm_test(void)
  13350. {
  13351. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13352. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13353. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13354. };
  13355. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  13356. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  13357. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  13358. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  13359. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  13360. };
  13361. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  13362. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13363. 0x08, 0x09, 0x0A, 0x0B
  13364. };
  13365. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  13366. 0xFF, 0xEE, 0xDD
  13367. };
  13368. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  13369. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  13370. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  13371. };
  13372. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  13373. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  13374. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  13375. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  13376. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  13377. };
  13378. /* RFC8998 A.1. */
  13379. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  13380. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  13381. 0x00, 0x00, 0xAB, 0xCD
  13382. };
  13383. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  13384. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13385. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13386. };
  13387. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  13388. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  13389. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  13390. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  13391. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  13392. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13393. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  13394. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13395. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  13396. };
  13397. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  13398. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13399. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13400. 0xAB, 0xAD, 0xDA, 0xD2
  13401. };
  13402. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  13403. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  13404. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  13405. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  13406. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  13407. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  13408. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  13409. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  13410. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  13411. };
  13412. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  13413. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  13414. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  13415. };
  13416. wc_Sm4 sm4;
  13417. byte enc[SM4_BLOCK_SIZE * 4];
  13418. byte dec[SM4_BLOCK_SIZE * 4];
  13419. byte tag[SM4_BLOCK_SIZE];
  13420. int ret;
  13421. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13422. if (ret != 0)
  13423. return WC_TEST_RET_ENC_EC(ret);
  13424. /* Encrypt and decrypt using encrypt with GCM. */
  13425. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  13426. if (ret != 0)
  13427. return WC_TEST_RET_ENC_EC(ret);
  13428. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  13429. sizeof(tag), a1, sizeof(a1));
  13430. if (ret != 0)
  13431. return WC_TEST_RET_ENC_EC(ret);
  13432. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  13433. return WC_TEST_RET_ENC_NC;
  13434. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  13435. return WC_TEST_RET_ENC_NC;
  13436. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  13437. sizeof(tag), a1, sizeof(a1));
  13438. if (ret != 0)
  13439. return WC_TEST_RET_ENC_EC(ret);
  13440. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13441. return WC_TEST_RET_ENC_NC;
  13442. /* RFC8998 test vector. */
  13443. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  13444. if (ret != 0)
  13445. return WC_TEST_RET_ENC_EC(ret);
  13446. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  13447. sizeof(tag), a2, sizeof(a2));
  13448. if (ret != 0)
  13449. return WC_TEST_RET_ENC_EC(ret);
  13450. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  13451. return WC_TEST_RET_ENC_NC;
  13452. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  13453. return WC_TEST_RET_ENC_NC;
  13454. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  13455. sizeof(tag), a2, sizeof(a2));
  13456. if (ret != 0)
  13457. return WC_TEST_RET_ENC_EC(ret);
  13458. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  13459. return WC_TEST_RET_ENC_NC;
  13460. wc_Sm4Free(&sm4);
  13461. return 0;
  13462. }
  13463. #endif
  13464. #ifdef WOLFSSL_SM4_CCM
  13465. static int sm4_ccm_test(void)
  13466. {
  13467. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  13468. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13469. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13470. };
  13471. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  13472. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  13473. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  13474. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  13475. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  13476. };
  13477. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  13478. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13479. 0x08, 0x09, 0x0A, 0x0B
  13480. };
  13481. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  13482. 0xFF, 0xEE, 0xDD
  13483. };
  13484. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  13485. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  13486. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  13487. };
  13488. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  13489. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  13490. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  13491. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  13492. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  13493. };
  13494. /* RFC8998 A.1. */
  13495. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  13496. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  13497. 0x00, 0x00, 0xAB, 0xCD
  13498. };
  13499. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  13500. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  13501. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  13502. };
  13503. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  13504. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  13505. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  13506. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  13507. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  13508. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13509. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  13510. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  13511. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  13512. };
  13513. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  13514. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13515. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  13516. 0xAB, 0xAD, 0xDA, 0xD2
  13517. };
  13518. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  13519. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  13520. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  13521. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  13522. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  13523. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  13524. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  13525. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  13526. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  13527. };
  13528. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  13529. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  13530. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  13531. };
  13532. wc_Sm4 sm4;
  13533. byte enc[SM4_BLOCK_SIZE * 4];
  13534. byte dec[SM4_BLOCK_SIZE * 4];
  13535. byte tag[SM4_BLOCK_SIZE];
  13536. int ret;
  13537. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  13538. if (ret != 0)
  13539. return -6720;
  13540. /* Encrypt and decrypt using encrypt with CCM. */
  13541. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  13542. if (ret != 0)
  13543. return WC_TEST_RET_ENC_EC(ret);
  13544. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  13545. sizeof(tag), a1, sizeof(a1));
  13546. if (ret != 0)
  13547. return WC_TEST_RET_ENC_EC(ret);
  13548. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  13549. return WC_TEST_RET_ENC_NC;
  13550. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  13551. return WC_TEST_RET_ENC_NC;
  13552. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  13553. sizeof(tag), a1, sizeof(a1));
  13554. if (ret != 0)
  13555. return WC_TEST_RET_ENC_EC(ret);
  13556. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  13557. return WC_TEST_RET_ENC_NC;
  13558. /* RFC8998 test vector. */
  13559. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  13560. if (ret != 0)
  13561. return WC_TEST_RET_ENC_EC(ret);
  13562. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  13563. sizeof(tag), a2, sizeof(a2));
  13564. if (ret != 0)
  13565. return WC_TEST_RET_ENC_EC(ret);
  13566. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  13567. return WC_TEST_RET_ENC_NC;
  13568. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  13569. return WC_TEST_RET_ENC_NC;
  13570. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  13571. sizeof(tag), a2, sizeof(a2));
  13572. if (ret != 0)
  13573. return WC_TEST_RET_ENC_EC(ret);
  13574. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  13575. return WC_TEST_RET_ENC_NC;
  13576. wc_Sm4Free(&sm4);
  13577. return 0;
  13578. }
  13579. #endif
  13580. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  13581. {
  13582. wc_test_ret_t ret;
  13583. WOLFSSL_ENTER("sm4_test");
  13584. #ifdef WOLFSSL_SM4_ECB
  13585. ret = sm4_ecb_test();
  13586. if (ret != 0)
  13587. return ret;
  13588. #endif
  13589. #ifdef WOLFSSL_SM4_CBC
  13590. ret = sm4_cbc_test();
  13591. if (ret != 0)
  13592. return ret;
  13593. #endif
  13594. #ifdef WOLFSSL_SM4_CTR
  13595. ret = sm4_ctr_test();
  13596. if (ret != 0)
  13597. return ret;
  13598. #endif
  13599. #ifdef WOLFSSL_SM4_GCM
  13600. ret = sm4_gcm_test();
  13601. if (ret != 0)
  13602. return ret;
  13603. #endif
  13604. #ifdef WOLFSSL_SM4_CCM
  13605. ret = sm4_ccm_test();
  13606. if (ret != 0)
  13607. return ret;
  13608. #endif
  13609. return 0;
  13610. }
  13611. #endif
  13612. #ifdef HAVE_XCHACHA
  13613. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  13614. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  13615. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  13616. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  13617. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  13618. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  13619. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  13620. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  13621. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  13622. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  13623. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  13624. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  13625. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  13626. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  13627. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  13628. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  13629. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  13630. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  13631. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  13632. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  13633. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  13634. };
  13635. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  13636. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13637. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13638. };
  13639. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  13640. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  13641. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  13642. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  13643. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  13644. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  13645. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  13646. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  13647. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  13648. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  13649. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  13650. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  13651. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  13652. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  13653. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  13654. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  13655. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  13656. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  13657. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  13658. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  13659. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  13660. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  13661. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  13662. };
  13663. wc_test_ret_t ret;
  13664. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13665. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  13666. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13667. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13668. WOLFSSL_ENTER("XChaCha_test");
  13669. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  13670. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13671. #else
  13672. struct ChaCha chacha[1];
  13673. byte buf1[sizeof Plaintext];
  13674. byte buf2[sizeof Plaintext];
  13675. WOLFSSL_ENTER("XChaCha_test");
  13676. #endif
  13677. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  13678. if (ret < 0)
  13679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13680. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  13681. if (ret < 0)
  13682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13683. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  13684. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13685. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  13686. if (ret < 0)
  13687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13688. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  13689. if (ret < 0)
  13690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13691. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  13692. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13693. out:
  13694. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13695. if (chacha)
  13696. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  13697. if (buf1)
  13698. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13699. if (buf2)
  13700. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13701. #endif
  13702. return ret;
  13703. }
  13704. #endif /* HAVE_XCHACHA */
  13705. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  13706. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  13707. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  13708. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  13709. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  13710. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  13711. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  13712. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  13713. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  13714. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  13715. 0x74, 0x2e }; /* t. */
  13716. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  13717. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  13718. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13719. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13720. };
  13721. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  13722. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  13723. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  13724. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  13725. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  13726. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  13727. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  13728. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  13729. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  13730. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  13731. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  13732. 0xb5, 0x2e };
  13733. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  13734. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  13735. };
  13736. wc_test_ret_t ret;
  13737. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13738. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13739. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13740. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  13741. if ((buf1 == NULL) || (buf2 == NULL))
  13742. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13743. #else
  13744. byte buf1[sizeof Ciphertext + sizeof Tag];
  13745. byte buf2[sizeof Plaintext];
  13746. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  13747. #endif
  13748. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  13749. Plaintext, sizeof Plaintext,
  13750. AAD, sizeof AAD,
  13751. IV, sizeof IV,
  13752. Key, sizeof Key);
  13753. if (ret < 0)
  13754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13755. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  13756. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13757. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  13758. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13759. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  13760. buf1, sizeof Ciphertext + sizeof Tag,
  13761. AAD, sizeof AAD,
  13762. IV, sizeof IV,
  13763. Key, sizeof Key);
  13764. if (ret < 0)
  13765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13766. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  13767. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13768. out:
  13769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13770. if (buf1 != NULL)
  13771. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13772. if (buf2 != NULL)
  13773. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13774. #endif
  13775. return ret;
  13776. }
  13777. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  13778. #ifndef WC_NO_RNG
  13779. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  13780. {
  13781. byte block[32];
  13782. wc_test_ret_t ret;
  13783. int i;
  13784. XMEMSET(block, 0, sizeof(block));
  13785. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  13786. if (ret != 0) {
  13787. ret = 1;
  13788. goto exit;
  13789. }
  13790. /* Check for 0's */
  13791. for (i=0; i<(int)sizeof(block); i++) {
  13792. if (block[i] == 0) {
  13793. ret++;
  13794. }
  13795. }
  13796. /* All zeros count check */
  13797. if (ret >= (int)sizeof(block)) {
  13798. ret = 2;
  13799. goto exit;
  13800. }
  13801. ret = wc_RNG_GenerateByte(rng, block);
  13802. if (ret != 0) {
  13803. ret = 3;
  13804. goto exit;
  13805. }
  13806. /* Parameter validation testing. */
  13807. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  13808. if (ret != BAD_FUNC_ARG) {
  13809. ret = 4;
  13810. goto exit;
  13811. }
  13812. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  13813. if (ret != BAD_FUNC_ARG) {
  13814. ret = 5;
  13815. goto exit;
  13816. }
  13817. ret = wc_RNG_GenerateByte(NULL, block);
  13818. if (ret != BAD_FUNC_ARG) {
  13819. ret = 6;
  13820. goto exit;
  13821. }
  13822. ret = wc_RNG_GenerateByte(rng, NULL);
  13823. if (ret != BAD_FUNC_ARG) {
  13824. ret = 7;
  13825. goto exit;
  13826. }
  13827. ret = 0;
  13828. exit:
  13829. if (ret != 0)
  13830. ret = errorOffset - (ret * 1000000);
  13831. return ret;
  13832. }
  13833. static wc_test_ret_t random_rng_test(void)
  13834. {
  13835. WC_RNG localRng;
  13836. WC_RNG* rng;
  13837. wc_test_ret_t ret;
  13838. rng = &localRng;
  13839. /* Test stack based RNG. */
  13840. #ifndef HAVE_FIPS
  13841. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  13842. #else
  13843. ret = wc_InitRng(rng);
  13844. #endif
  13845. if (ret != 0)
  13846. return WC_TEST_RET_ENC_EC(ret);
  13847. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  13848. /* Make sure and free RNG */
  13849. wc_FreeRng(rng);
  13850. if (ret != 0)
  13851. return ret;
  13852. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  13853. {
  13854. byte nonce[8] = { 0 };
  13855. /* Test dynamic RNG */
  13856. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  13857. if (rng == NULL)
  13858. return WC_TEST_RET_ENC_ERRNO;
  13859. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  13860. wc_rng_free(rng);
  13861. rng = NULL;
  13862. if (ret != 0)
  13863. return ret;
  13864. /* Test dynamic RNG using extended API */
  13865. ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
  13866. HEAP_HINT, devId);
  13867. if (ret != 0)
  13868. return WC_TEST_RET_ENC_EC(ret);
  13869. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  13870. wc_rng_free(rng);
  13871. if (ret != 0)
  13872. return ret;
  13873. }
  13874. #endif
  13875. return ret;
  13876. }
  13877. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  13878. #ifdef WC_RNG_SEED_CB
  13879. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  13880. {
  13881. word32 i;
  13882. (void)os;
  13883. /* Known answer test. Set the seed to the same value every time. */
  13884. for (i = 0; i < sz; i++)
  13885. output[i] = (byte)i;
  13886. return 0;
  13887. }
  13888. static wc_test_ret_t rng_seed_test(void)
  13889. {
  13890. #ifndef HAVE_FIPS
  13891. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  13892. {
  13893. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  13894. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  13895. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  13896. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  13897. };
  13898. #else
  13899. /* FIPS uses a longer seed, so different check value. */
  13900. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  13901. {
  13902. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  13903. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  13904. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  13905. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  13906. };
  13907. #endif
  13908. byte output[WC_SHA256_DIGEST_SIZE];
  13909. WC_RNG rng;
  13910. wc_test_ret_t ret;
  13911. ret = wc_SetSeed_Cb(seed_cb);
  13912. if (ret != 0) {
  13913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13914. }
  13915. ret = wc_InitRng(&rng);
  13916. if (ret != 0) {
  13917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13918. }
  13919. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  13920. if (ret != 0) {
  13921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13922. }
  13923. ret = XMEMCMP(output, check, sizeof(output));
  13924. if (ret != 0) {
  13925. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13926. }
  13927. ret = wc_FreeRng(&rng);
  13928. if (ret != 0) {
  13929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13930. }
  13931. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  13932. if (ret != 0) {
  13933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13934. }
  13935. out:
  13936. return ret;
  13937. }
  13938. #endif
  13939. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  13940. {
  13941. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  13942. {
  13943. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  13944. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  13945. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  13946. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  13947. };
  13948. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  13949. {
  13950. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  13951. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  13952. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  13953. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  13954. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  13955. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  13956. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  13957. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  13958. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  13959. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  13960. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  13961. };
  13962. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  13963. {
  13964. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  13965. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  13966. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  13967. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  13968. };
  13969. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  13970. {
  13971. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  13972. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  13973. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  13974. };
  13975. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  13976. {
  13977. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  13978. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  13979. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  13980. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  13981. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  13982. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  13983. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  13984. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  13985. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  13986. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  13987. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  13988. };
  13989. byte output[WC_SHA256_DIGEST_SIZE * 4];
  13990. wc_test_ret_t ret;
  13991. WOLFSSL_ENTER("random_test");
  13992. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  13993. output, sizeof(output));
  13994. if (ret != 0)
  13995. return WC_TEST_RET_ENC_EC(ret);
  13996. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  13997. return WC_TEST_RET_ENC_NC;
  13998. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  13999. test2EntropyB, sizeof(test2EntropyB),
  14000. output, sizeof(output));
  14001. if (ret != 0)
  14002. return WC_TEST_RET_ENC_EC(ret);
  14003. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  14004. return WC_TEST_RET_ENC_NC;
  14005. /* Basic RNG generate block test */
  14006. if ((ret = random_rng_test()) != 0)
  14007. return ret;
  14008. /* Test the seed check function. */
  14009. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  14010. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  14011. {
  14012. word32 i, outputSz;
  14013. /* Repeat the same byte over and over. Should fail. */
  14014. outputSz = sizeof(output);
  14015. XMEMSET(output, 1, outputSz);
  14016. ret = wc_RNG_TestSeed(output, outputSz);
  14017. if (ret == 0)
  14018. return WC_TEST_RET_ENC_NC;
  14019. /* Every byte of the entropy scratch is different,
  14020. * entropy is a single byte that shouldn't match. */
  14021. outputSz = (sizeof(output) / 2) + 1;
  14022. for (i = 0; i < outputSz; i++)
  14023. output[i] = (byte)i;
  14024. ret = wc_RNG_TestSeed(output, outputSz);
  14025. if (ret != 0)
  14026. return WC_TEST_RET_ENC_EC(ret);
  14027. outputSz = sizeof(output);
  14028. for (i = 0; i < outputSz; i++)
  14029. output[i] = (byte)i;
  14030. ret = wc_RNG_TestSeed(output, outputSz);
  14031. if (ret != 0)
  14032. return WC_TEST_RET_ENC_EC(ret);
  14033. }
  14034. #endif
  14035. /* Test the seed callback. */
  14036. #ifdef WC_RNG_SEED_CB
  14037. if ((ret = rng_seed_test()) != 0)
  14038. return ret;
  14039. #endif
  14040. return 0;
  14041. }
  14042. #else
  14043. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  14044. {
  14045. WOLFSSL_ENTER("random_test");
  14046. /* Basic RNG generate block test */
  14047. return random_rng_test();
  14048. }
  14049. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  14050. #endif /* WC_NO_RNG */
  14051. #ifndef MEM_TEST_SZ
  14052. #define MEM_TEST_SZ 1024
  14053. #endif
  14054. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  14055. static int simple_mem_test(int sz)
  14056. {
  14057. int ret = 0;
  14058. byte* b;
  14059. int i;
  14060. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14061. if (b == NULL) {
  14062. return WC_TEST_RET_ENC_NC;
  14063. }
  14064. /* utilize memory */
  14065. for (i = 0; i < sz; i++) {
  14066. b[i] = (byte)i;
  14067. }
  14068. /* read back and verify */
  14069. for (i = 0; i < sz; i++) {
  14070. if (b[i] != (byte)i) {
  14071. ret = WC_TEST_RET_ENC_NC;
  14072. break;
  14073. }
  14074. }
  14075. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14076. return ret;
  14077. }
  14078. #endif
  14079. /* If successful, returns the first letter of the byte array `in`.
  14080. **
  14081. ** This is a deceptively simple test of a read-only embedded Linux file system.
  14082. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  14083. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  14084. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  14085. **
  14086. ** Edit with caution. See PR #6523. */
  14087. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  14088. {
  14089. wc_test_ret_t ret = 0;
  14090. volatile word32 j = (word32)-1; /* must be volatile to properly detect error */
  14091. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  14092. (void)ret;
  14093. j = *outJ; /* Found index to use in const array. */
  14094. if (j == 0) {
  14095. #ifdef WOLFSSL_DEBUG
  14096. printf("Testing const byte ptr reference...\n");
  14097. #endif
  14098. /* although j is zero, in[0] does not detect the Illegal instruction */
  14099. ret = in[j]; /* The big test: can we actually access the `in` data? */
  14100. }
  14101. else {
  14102. ret = -1;
  14103. }
  14104. return ret;
  14105. }
  14106. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  14107. {
  14108. wc_test_ret_t ret = 0;
  14109. word32 j = 0; /* used in embedded const pointer test */
  14110. WOLFSSL_ENTER("memory_test");
  14111. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  14112. int i;
  14113. #endif
  14114. #ifdef WOLFSSL_STATIC_MEMORY
  14115. word32 size[] = { WOLFMEM_BUCKETS };
  14116. word32 dist[] = { WOLFMEM_DIST };
  14117. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  14118. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  14119. /* pad to account for if head of buffer is not at set memory
  14120. * alignment when tests are ran */
  14121. #endif
  14122. #ifdef WOLFSSL_STATIC_MEMORY
  14123. /* check macro settings */
  14124. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  14125. return WC_TEST_RET_ENC_NC;
  14126. }
  14127. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  14128. return WC_TEST_RET_ENC_NC;
  14129. }
  14130. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  14131. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  14132. /* each element in array should be divisible by alignment size */
  14133. return WC_TEST_RET_ENC_NC;
  14134. }
  14135. }
  14136. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  14137. if (size[i - 1] >= size[i]) {
  14138. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  14139. }
  14140. }
  14141. /* check that padding size returned is possible */
  14142. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  14143. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  14144. }
  14145. ret = wolfSSL_MemoryPaddingSz();
  14146. if (ret < 0) {
  14147. return WC_TEST_RET_ENC_EC(ret);
  14148. }
  14149. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  14150. return WC_TEST_RET_ENC_NC; /* not aligned! */
  14151. }
  14152. /* check function to return optimum buffer size (rounded down) */
  14153. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  14154. if (ret < 0) {
  14155. return WC_TEST_RET_ENC_EC(ret);
  14156. }
  14157. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  14158. return WC_TEST_RET_ENC_NC; /* not aligned! */
  14159. }
  14160. if ((unsigned int)ret > sizeof(buffer)) {
  14161. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  14162. }
  14163. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  14164. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  14165. * value
  14166. */
  14167. }
  14168. ret = wolfSSL_MemoryPaddingSz();
  14169. ret += pad; /* add space that is going to be needed if buffer not aligned */
  14170. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  14171. (ret + (int)size[0])) {
  14172. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  14173. * value
  14174. */
  14175. }
  14176. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  14177. if ((ret - pad) < 0) {
  14178. return WC_TEST_RET_ENC_NC;
  14179. }
  14180. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  14181. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  14182. }
  14183. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  14184. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  14185. }
  14186. /* check for passing bad or unknown arguments to functions */
  14187. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  14188. return WC_TEST_RET_ENC_NC;
  14189. }
  14190. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  14191. return WC_TEST_RET_ENC_NC; /* should round to 0
  14192. since struct + bucket will not fit */
  14193. }
  14194. (void)dist; /* avoid static analysis warning of variable not used */
  14195. #endif
  14196. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  14197. /* simple test */
  14198. ret = simple_mem_test(MEM_TEST_SZ);
  14199. if (ret != 0)
  14200. return ret;
  14201. #endif
  14202. #ifdef COMPLEX_MEM_TEST
  14203. /* test various size blocks */
  14204. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  14205. ret = simple_mem_test(i);
  14206. if (ret != 0)
  14207. return ret;
  14208. }
  14209. #endif
  14210. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  14211. /* realloc test */
  14212. {
  14213. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  14214. DYNAMIC_TYPE_TMP_BUFFER);
  14215. #ifndef WOLFSSL_NO_REALLOC
  14216. byte *c = NULL;
  14217. if (b) {
  14218. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  14219. DYNAMIC_TYPE_TMP_BUFFER);
  14220. if (c)
  14221. b = c;
  14222. }
  14223. #endif
  14224. if (b)
  14225. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14226. if ((b == NULL)
  14227. #ifndef WOLFSSL_NO_REALLOC
  14228. || (c == NULL)
  14229. #endif
  14230. ) {
  14231. return WC_TEST_RET_ENC_ERRNO;
  14232. }
  14233. }
  14234. #endif
  14235. if (ret == 0) {
  14236. /* This test is only interesting on embedded R/O Flash systems */
  14237. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  14238. ret = 1;
  14239. }
  14240. }
  14241. return ret;
  14242. }
  14243. #ifndef NO_FILESYSTEM
  14244. /* Cert Paths */
  14245. #ifdef FREESCALE_MQX
  14246. #define CERT_PREFIX "a:\\"
  14247. #define CERT_PATH_SEP "\\"
  14248. #elif defined(WOLFSSL_uTKERNEL2)
  14249. #define CERT_PREFIX "/uda/"
  14250. #define CERT_PATH_SEP "/"
  14251. #elif defined(_WIN32_WCE)
  14252. #define CERT_PREFIX "\\windows\\"
  14253. #define CERT_PATH_SEP "\\"
  14254. #endif
  14255. #ifndef CERT_PREFIX
  14256. #define CERT_PREFIX "./"
  14257. #endif
  14258. #ifndef CERT_PATH_SEP
  14259. #define CERT_PATH_SEP "/"
  14260. #endif
  14261. #ifndef CERT_WRITE_TEMP_DIR
  14262. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  14263. #endif
  14264. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  14265. /* Generated Test Certs */
  14266. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14267. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14268. #if !defined(NO_RSA) && !defined(NO_ASN)
  14269. static const char* clientKey = CERT_ROOT "client-key.der";
  14270. static const char* clientCert = CERT_ROOT "client-cert.der";
  14271. #ifdef WOLFSSL_CERT_EXT
  14272. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  14273. #endif
  14274. #endif /* !NO_RSA && !NO_ASN */
  14275. #endif
  14276. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14277. #if !defined(NO_RSA) && !defined(NO_ASN)
  14278. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  14279. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  14280. #ifdef WOLFSSL_CERT_GEN
  14281. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  14282. #endif
  14283. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  14284. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  14285. #endif
  14286. #ifdef HAVE_PKCS7
  14287. static const char* rsaServerCertDerFile =
  14288. CERT_ROOT "server-cert.der";
  14289. static const char* rsaServerKeyDerFile =
  14290. CERT_ROOT "server-key.der";
  14291. #endif
  14292. #endif
  14293. #endif /* !NO_RSA && !NO_ASN */
  14294. #endif /* !USE_CERT_BUFFER_* */
  14295. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14296. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14297. #if !defined(NO_ASN) && !defined(NO_DH)
  14298. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  14299. #endif
  14300. #endif
  14301. #if !defined(NO_ASN) && !defined(NO_DH)
  14302. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  14303. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  14304. #if !defined(USE_CERT_BUFFERS_2048)
  14305. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  14306. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  14307. #endif
  14308. #endif
  14309. #endif
  14310. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14311. #ifndef NO_DSA
  14312. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  14313. #endif
  14314. #endif /* !USE_CERT_BUFFER_* */
  14315. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  14316. #ifdef HAVE_ECC
  14317. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  14318. #ifdef HAVE_ECC_KEY_IMPORT
  14319. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  14320. #endif
  14321. #endif
  14322. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  14323. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  14324. !defined(NO_ECC_SECP)
  14325. #ifndef NO_RSA
  14326. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  14327. #endif
  14328. #ifndef NO_ASN_TIME
  14329. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  14330. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  14331. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14332. static const char* eccCaKey384File =
  14333. CERT_ROOT "ca-ecc384-key.der";
  14334. static const char* eccCaCert384File =
  14335. CERT_ROOT "ca-ecc384-cert.pem";
  14336. #endif
  14337. #endif
  14338. #endif
  14339. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  14340. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  14341. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  14342. #endif
  14343. #endif /* HAVE_ECC */
  14344. #ifdef HAVE_ED25519
  14345. #ifdef WOLFSSL_TEST_CERT
  14346. static const char* serverEd25519Cert =
  14347. CERT_ROOT "ed25519/server-ed25519.der";
  14348. static const char* caEd25519Cert =
  14349. CERT_ROOT "ed25519/ca-ed25519.der";
  14350. #endif
  14351. #endif
  14352. #ifdef HAVE_ED448
  14353. #ifdef WOLFSSL_TEST_CERT
  14354. static const char* serverEd448Cert =
  14355. CERT_ROOT "ed448/server-ed448.der";
  14356. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  14357. #endif
  14358. #endif
  14359. #endif /* !USE_CERT_BUFFER_* */
  14360. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  14361. !defined(NO_FILESYSTEM)
  14362. static const char* certExtNc =
  14363. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  14364. static const char* certExtIa =
  14365. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  14366. static const char* certExtNct =
  14367. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  14368. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  14369. static const char* certBadNegInt =
  14370. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-neg-int.der";
  14371. #endif
  14372. static const char* certBadOid =
  14373. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
  14374. #ifndef WOLFSSL_NO_ASN_STRICT
  14375. static const char* certBadUtf8 =
  14376. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
  14377. #endif
  14378. #endif
  14379. #ifndef NO_WRITE_TEMP_FILES
  14380. #ifdef HAVE_ECC
  14381. #ifndef NO_ECC_SECP
  14382. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  14383. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  14384. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  14385. #endif
  14386. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  14387. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  14388. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  14389. #endif
  14390. #endif
  14391. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  14392. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  14393. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  14394. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  14395. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  14396. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  14397. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  14398. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  14399. #endif
  14400. #endif /* HAVE_ECC_KEY_EXPORT */
  14401. #endif /* HAVE_ECC */
  14402. #ifndef NO_RSA
  14403. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  14404. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  14405. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  14406. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  14407. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  14408. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14409. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  14410. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  14411. #endif
  14412. #endif
  14413. #endif /* !NO_RSA */
  14414. #if !defined(NO_RSA) || !defined(NO_DSA)
  14415. #ifdef WOLFSSL_KEY_GEN
  14416. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  14417. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  14418. #endif
  14419. #endif
  14420. #endif /* !NO_WRITE_TEMP_FILES */
  14421. #endif /* !NO_FILESYSTEM */
  14422. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  14423. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  14424. static CertName certDefaultName;
  14425. static void initDefaultName(void)
  14426. {
  14427. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  14428. NameAttrib* n;
  14429. #endif
  14430. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  14431. certDefaultName.countryEnc = CTC_PRINTABLE;
  14432. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  14433. certDefaultName.stateEnc = CTC_UTF8;
  14434. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  14435. certDefaultName.streetEnc = CTC_UTF8;
  14436. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  14437. certDefaultName.localityEnc = CTC_UTF8;
  14438. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  14439. certDefaultName.surEnc = CTC_UTF8;
  14440. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  14441. certDefaultName.orgEnc = CTC_UTF8;
  14442. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  14443. certDefaultName.unitEnc = CTC_UTF8;
  14444. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  14445. certDefaultName.commonNameEnc = CTC_UTF8;
  14446. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  14447. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  14448. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  14449. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  14450. #ifdef WOLFSSL_CERT_EXT
  14451. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  14452. certDefaultName.busCatEnc = CTC_UTF8;
  14453. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  14454. certDefaultName.joiStEnc = CTC_PRINTABLE;
  14455. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  14456. certDefaultName.joiCEnc = CTC_PRINTABLE;
  14457. #endif
  14458. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  14459. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  14460. certDefaultName.userIdEnc = CTC_PRINTABLE;
  14461. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  14462. /* test having additional OUs and setting DC */
  14463. n = &certDefaultName.name[0];
  14464. n->id = ASN_ORGUNIT_NAME;
  14465. n->type = CTC_UTF8;
  14466. n->sz = XSTRLEN("Development-2");
  14467. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  14468. #if CTC_MAX_ATTRIB > 3
  14469. n = &certDefaultName.name[1];
  14470. n->id = ASN_DOMAIN_COMPONENT;
  14471. n->type = CTC_UTF8;
  14472. n->sz = XSTRLEN("com");
  14473. XMEMCPY(n->value, "com", sizeof("com"));
  14474. n = &certDefaultName.name[2];
  14475. n->id = ASN_DOMAIN_COMPONENT;
  14476. n->type = CTC_UTF8;
  14477. n->sz = XSTRLEN("wolfssl");
  14478. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  14479. #endif
  14480. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  14481. #ifdef WOLFSSL_CUSTOM_OID
  14482. /* TODO: Add test case for custom OID's */
  14483. #endif
  14484. }
  14485. #ifdef WOLFSSL_CERT_EXT
  14486. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  14487. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  14488. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  14489. "digitalSignature,nonRepudiation";
  14490. #endif
  14491. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  14492. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  14493. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  14494. #endif
  14495. #endif /* WOLFSSL_CERT_EXT */
  14496. #endif /* WOLFSSL_CERT_GEN */
  14497. #ifndef NO_RSA
  14498. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  14499. !defined(NO_FILESYSTEM)
  14500. static const byte minSerial[] = { 0x02, 0x01, 0x01 };
  14501. static const byte minName[] = { 0x30, 0x00 };
  14502. static const byte nameBad[] = {
  14503. 0x30, 0x08,
  14504. 0x31, 0x06,
  14505. 0x30, 0x04,
  14506. 0x06, 0x02,
  14507. 0x55, 0x04,
  14508. };
  14509. static const byte minDates[] = {
  14510. 0x30, 0x1e,
  14511. 0x17, 0x0d,
  14512. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  14513. 0x32, 0x33, 0x31, 0x30, 0x5a,
  14514. 0x17, 0x0d,
  14515. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  14516. 0x32, 0x33, 0x31, 0x30, 0x5a
  14517. };
  14518. static const byte minPubKey[] = {
  14519. 0x30, 0x1c,
  14520. 0x30, 0x0d,
  14521. 0x06, 0x09,
  14522. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  14523. 0x01,
  14524. 0x05, 0x00,
  14525. 0x03, 0x0b,
  14526. 0x00, 0x30, 0x08,
  14527. 0x02, 0x01,
  14528. 0x03,
  14529. 0x02, 0x03,
  14530. 0x01, 0x00, 0x01
  14531. };
  14532. static const byte minSigAlg[] = {
  14533. 0x30, 0x0d,
  14534. 0x06, 0x09,
  14535. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  14536. 0x0b,
  14537. 0x05, 0x00
  14538. };
  14539. static const byte minSig[] = {
  14540. 0x03, 0x01,
  14541. 0x00
  14542. };
  14543. static int add_seq(byte* certData, int offset, byte* data, byte length)
  14544. {
  14545. XMEMMOVE(certData + offset + 2, data, length);
  14546. certData[offset++] = 0x30;
  14547. certData[offset++] = length;
  14548. return offset + length;
  14549. }
  14550. static int add_data(byte* certData, int offset, const byte* data, byte length)
  14551. {
  14552. XMEMCPY(certData + offset, data, length);
  14553. return offset + length;
  14554. }
  14555. static wc_test_ret_t cert_asn1_test(void)
  14556. {
  14557. wc_test_ret_t ret;
  14558. int len[3];
  14559. DecodedCert cert;
  14560. byte certData[114];
  14561. byte* badCert = NULL;
  14562. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  14563. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  14564. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14565. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  14566. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14567. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  14568. len[1] = add_seq(certData, 0, certData, len[2]);
  14569. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  14570. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  14571. len[0] = add_seq(certData, 0, certData, len[1]);
  14572. /* Minimal good certificate */
  14573. InitDecodedCert(&cert, certData, len[0], 0);
  14574. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14575. FreeDecodedCert(&cert);
  14576. if (ret != 0)
  14577. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14578. /* Bad issuer name */
  14579. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  14580. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  14581. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  14582. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  14583. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  14584. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  14585. len[1] = add_seq(certData, 0, certData, len[2]);
  14586. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  14587. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  14588. len[0] = add_seq(certData, 0, certData, len[1]);
  14589. /* Put data into allocated buffer to allow access error checking. */
  14590. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14591. XMEMCPY(badCert, certData, len[0]);
  14592. InitDecodedCert(&cert, badCert, len[0], 0);
  14593. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14594. FreeDecodedCert(&cert);
  14595. if (ret != ASN_PARSE_E) {
  14596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14597. }
  14598. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14599. badCert = NULL;
  14600. ret = 0;
  14601. done:
  14602. if (badCert != NULL)
  14603. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14604. return ret;
  14605. }
  14606. static wc_test_ret_t cert_load_bad(const char* fname, byte* tmp, int err)
  14607. {
  14608. wc_test_ret_t ret;
  14609. DecodedCert cert;
  14610. XFILE file;
  14611. size_t bytes;
  14612. if ((fname == NULL) || (tmp == NULL)) {
  14613. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  14614. }
  14615. file = XFOPEN(fname, "rb");
  14616. if (!file) {
  14617. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14618. }
  14619. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14620. XFCLOSE(file);
  14621. if (bytes == 0) {
  14622. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14623. }
  14624. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14625. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14626. FreeDecodedCert(&cert);
  14627. if (ret != err) {
  14628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14629. }
  14630. ret = 0;
  14631. done:
  14632. return ret;
  14633. }
  14634. static wc_test_ret_t cert_bad_asn1_test(void)
  14635. {
  14636. wc_test_ret_t ret = 0;
  14637. byte* tmp;
  14638. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14639. if (tmp == NULL) {
  14640. ret = WC_TEST_RET_ENC_ERRNO;
  14641. }
  14642. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  14643. if (ret == 0) {
  14644. /* Serial number: 0xff 0xa8. 0xff and top bit set on next byte invalid.
  14645. */
  14646. ret = cert_load_bad(certBadNegInt, tmp, ASN_EXPECT_0_E);
  14647. }
  14648. #endif
  14649. if (ret == 0) {
  14650. /* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
  14651. ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
  14652. }
  14653. #ifndef WOLFSSL_NO_ASN_STRICT
  14654. if (ret == 0) {
  14655. /* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
  14656. */
  14657. ret = cert_load_bad(certBadUtf8, tmp, ASN_PARSE_E);
  14658. }
  14659. #endif
  14660. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14661. return ret;
  14662. }
  14663. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  14664. {
  14665. #if !defined(NO_FILESYSTEM)
  14666. DecodedCert cert;
  14667. byte* tmp;
  14668. size_t bytes;
  14669. XFILE file;
  14670. wc_test_ret_t ret;
  14671. WOLFSSL_ENTER("cert_test");
  14672. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14673. if (tmp == NULL)
  14674. return WC_TEST_RET_ENC_ERRNO;
  14675. /* Certificate with Name Constraints extension. */
  14676. file = XFOPEN(certExtNc, "rb");
  14677. if (!file) {
  14678. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14679. }
  14680. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14681. XFCLOSE(file);
  14682. if (bytes == 0)
  14683. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14684. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14685. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14686. if (ret != 0)
  14687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14688. FreeDecodedCert(&cert);
  14689. /* Certificate with Inhibit Any Policy extension. */
  14690. file = XFOPEN(certExtIa, "rb");
  14691. if (!file) {
  14692. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14693. }
  14694. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14695. XFCLOSE(file);
  14696. if (bytes == 0)
  14697. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14698. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14699. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14700. if (ret != 0)
  14701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14702. FreeDecodedCert(&cert);
  14703. /* Certificate with Netscape Certificate Type extension. */
  14704. file = XFOPEN(certExtNct, "rb");
  14705. if (!file) {
  14706. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14707. }
  14708. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14709. XFCLOSE(file);
  14710. if (bytes == 0)
  14711. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  14712. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14713. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  14714. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  14715. if (ret != 0)
  14716. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14717. #else
  14718. if (ret != ASN_CRIT_EXT_E) {
  14719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  14720. }
  14721. ret = 0;
  14722. #endif
  14723. done:
  14724. FreeDecodedCert(&cert);
  14725. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14726. #endif /* !NO_FILESYSTEM */
  14727. if (ret == 0)
  14728. ret = cert_asn1_test();
  14729. if (ret == 0)
  14730. ret = cert_bad_asn1_test();
  14731. return ret;
  14732. }
  14733. #endif /* WOLFSSL_TEST_CERT */
  14734. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  14735. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  14736. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  14737. {
  14738. DecodedCert cert;
  14739. byte* tmp;
  14740. size_t bytes;
  14741. XFILE file;
  14742. wc_test_ret_t ret;
  14743. /* created from rsa_test : othercert.der */
  14744. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  14745. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  14746. /* created from rsa_test : othercert.der */
  14747. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  14748. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  14749. #ifdef HAVE_ECC
  14750. /* created from ecc_test_cert_gen : certecc.der */
  14751. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14752. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  14753. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  14754. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  14755. #else
  14756. /* Authority key id from ./certs/ca-ecc-cert.pem */
  14757. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  14758. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  14759. #endif
  14760. #endif /* HAVE_ECC */
  14761. /* created from rsa_test : cert.der */
  14762. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  14763. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  14764. WOLFSSL_ENTER("certext_test");
  14765. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14766. if (tmp == NULL)
  14767. return WC_TEST_RET_ENC_ERRNO;
  14768. /* load othercert.der (Cert signed by an authority) */
  14769. file = XFOPEN(otherCertDerFile, "rb");
  14770. if (!file) {
  14771. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14772. return WC_TEST_RET_ENC_ERRNO;
  14773. }
  14774. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14775. XFCLOSE(file);
  14776. if (bytes == 0)
  14777. return WC_TEST_RET_ENC_ERRNO;
  14778. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14779. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14780. if (ret != 0)
  14781. return WC_TEST_RET_ENC_EC(ret);
  14782. /* check the SKID from a RSA certificate */
  14783. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  14784. return WC_TEST_RET_ENC_NC;
  14785. /* check the AKID from an RSA certificate */
  14786. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14787. return WC_TEST_RET_ENC_NC;
  14788. /* check the Key Usage from an RSA certificate */
  14789. if (!cert.extKeyUsageSet)
  14790. return WC_TEST_RET_ENC_NC;
  14791. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  14792. return WC_TEST_RET_ENC_NC;
  14793. /* check the CA Basic Constraints from an RSA certificate */
  14794. if (cert.isCA)
  14795. return WC_TEST_RET_ENC_NC;
  14796. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14797. /* check the Certificate Policies Id */
  14798. if (cert.extCertPoliciesNb != 1)
  14799. return WC_TEST_RET_ENC_NC;
  14800. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  14801. return WC_TEST_RET_ENC_NC;
  14802. #endif
  14803. FreeDecodedCert(&cert);
  14804. #ifdef HAVE_ECC
  14805. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  14806. file = XFOPEN(certEccDerFile, "rb");
  14807. if (!file) {
  14808. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14809. return WC_TEST_RET_ENC_ERRNO;
  14810. }
  14811. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14812. XFCLOSE(file);
  14813. if (bytes == 0)
  14814. return WC_TEST_RET_ENC_ERRNO;
  14815. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14816. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14817. if (ret != 0)
  14818. return WC_TEST_RET_ENC_EC(ret);
  14819. /* check the SKID from a ECC certificate - generated dynamically */
  14820. /* check the AKID from an ECC certificate */
  14821. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14822. return WC_TEST_RET_ENC_NC;
  14823. /* check the Key Usage from an ECC certificate */
  14824. if (!cert.extKeyUsageSet)
  14825. return WC_TEST_RET_ENC_NC;
  14826. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  14827. return WC_TEST_RET_ENC_NC;
  14828. /* check the CA Basic Constraints from an ECC certificate */
  14829. if (cert.isCA)
  14830. return WC_TEST_RET_ENC_NC;
  14831. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14832. /* check the Certificate Policies Id */
  14833. if (cert.extCertPoliciesNb != 2)
  14834. return WC_TEST_RET_ENC_NC;
  14835. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  14836. return WC_TEST_RET_ENC_NC;
  14837. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  14838. return WC_TEST_RET_ENC_NC;
  14839. #endif
  14840. FreeDecodedCert(&cert);
  14841. #endif /* HAVE_ECC */
  14842. /* load cert.der (self signed certificate) */
  14843. file = XFOPEN(certDerFile, "rb");
  14844. if (!file) {
  14845. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14846. return WC_TEST_RET_ENC_ERRNO;
  14847. }
  14848. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14849. XFCLOSE(file);
  14850. if (bytes == 0)
  14851. return WC_TEST_RET_ENC_ERRNO;
  14852. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  14853. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  14854. if (ret != 0)
  14855. return WC_TEST_RET_ENC_EC(ret);
  14856. /* check the SKID from a CA certificate */
  14857. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  14858. return WC_TEST_RET_ENC_NC;
  14859. /* check the AKID from an CA certificate */
  14860. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  14861. return WC_TEST_RET_ENC_NC;
  14862. /* check the Key Usage from CA certificate */
  14863. if (!cert.extKeyUsageSet)
  14864. return WC_TEST_RET_ENC_NC;
  14865. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  14866. return WC_TEST_RET_ENC_NC;
  14867. /* check the CA Basic Constraints CA certificate */
  14868. if (!cert.isCA)
  14869. return WC_TEST_RET_ENC_NC;
  14870. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  14871. /* check the Certificate Policies Id */
  14872. if (cert.extCertPoliciesNb != 2)
  14873. return WC_TEST_RET_ENC_NC;
  14874. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  14875. return WC_TEST_RET_ENC_NC;
  14876. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  14877. return WC_TEST_RET_ENC_NC;
  14878. #endif
  14879. FreeDecodedCert(&cert);
  14880. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14881. return 0;
  14882. }
  14883. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  14884. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  14885. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  14886. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  14887. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  14888. {
  14889. wc_test_ret_t ret = 0;
  14890. Cert cert;
  14891. FILE* file;
  14892. byte* der;
  14893. word32 derSz;
  14894. WOLFSSL_ENTER("decodedCertCache_test");
  14895. derSz = FOURK_BUF;
  14896. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14897. if (der == NULL)
  14898. ret = WC_TEST_RET_ENC_NC;
  14899. if (ret == 0) {
  14900. /* load cert.der */
  14901. file = XFOPEN(certDerFile, "rb");
  14902. if (file != NULL) {
  14903. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  14904. XFCLOSE(file);
  14905. if (derSz == 0)
  14906. ret = WC_TEST_RET_ENC_ERRNO;
  14907. }
  14908. else
  14909. ret = WC_TEST_RET_ENC_ERRNO;
  14910. }
  14911. if (ret == 0) {
  14912. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  14913. if (ret != 0)
  14914. ret = WC_TEST_RET_ENC_EC(ret);
  14915. }
  14916. if (ret == 0) {
  14917. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  14918. if (ret != 0)
  14919. ret = WC_TEST_RET_ENC_EC(ret);
  14920. }
  14921. if (ret == 0) {
  14922. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  14923. if (ret == BAD_FUNC_ARG)
  14924. ret = 0;
  14925. else
  14926. ret = WC_TEST_RET_ENC_EC(ret);
  14927. }
  14928. if (ret == 0) {
  14929. ret = wc_SetSubjectRaw(&cert, der, derSz);
  14930. if (ret != 0)
  14931. ret = WC_TEST_RET_ENC_EC(ret);
  14932. }
  14933. if (ret == 0) {
  14934. ret = wc_SetSubjectRaw(NULL, der, derSz);
  14935. if (ret == BAD_FUNC_ARG)
  14936. ret = 0;
  14937. else
  14938. ret = WC_TEST_RET_ENC_EC(ret);
  14939. }
  14940. if (ret == 0) {
  14941. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  14942. if (ret != 0)
  14943. ret = WC_TEST_RET_ENC_EC(ret);
  14944. }
  14945. if (ret == 0) {
  14946. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  14947. if (ret == BAD_FUNC_ARG)
  14948. ret = 0;
  14949. else
  14950. ret = WC_TEST_RET_ENC_EC(ret);
  14951. }
  14952. if (ret == 0) {
  14953. ret = wc_SetIssuerRaw(&cert, der, derSz);
  14954. if (ret != 0)
  14955. ret = WC_TEST_RET_ENC_EC(ret);
  14956. }
  14957. if (ret == 0) {
  14958. ret = wc_SetIssuerRaw(NULL, der, derSz);
  14959. if (ret == BAD_FUNC_ARG)
  14960. ret = 0;
  14961. else
  14962. ret = WC_TEST_RET_ENC_EC(ret);
  14963. }
  14964. #ifdef WOLFSSL_ALT_NAMES
  14965. if (ret == 0) {
  14966. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  14967. if (ret != 0)
  14968. ret = WC_TEST_RET_ENC_EC(ret);
  14969. }
  14970. if (ret == 0) {
  14971. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  14972. if (ret == BAD_FUNC_ARG)
  14973. ret = 0;
  14974. else
  14975. ret = WC_TEST_RET_ENC_EC(ret);
  14976. }
  14977. if (ret == 0) {
  14978. ret = wc_SetDatesBuffer(&cert, der, derSz);
  14979. if (ret != 0)
  14980. ret = WC_TEST_RET_ENC_EC(ret);
  14981. }
  14982. if (ret == 0) {
  14983. ret = wc_SetDatesBuffer(NULL, der, derSz);
  14984. if (ret == BAD_FUNC_ARG)
  14985. ret = 0;
  14986. else
  14987. ret = WC_TEST_RET_ENC_EC(ret);
  14988. }
  14989. #endif
  14990. if (ret == 0) {
  14991. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  14992. if (ret != 0)
  14993. ret = WC_TEST_RET_ENC_EC(ret);
  14994. }
  14995. if (ret == 0) {
  14996. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  14997. if (ret == BAD_FUNC_ARG)
  14998. ret = 0;
  14999. else
  15000. ret = WC_TEST_RET_ENC_NC;
  15001. }
  15002. wc_SetCert_Free(&cert);
  15003. if (ret == 0) {
  15004. if(cert.decodedCert != NULL)
  15005. ret = WC_TEST_RET_ENC_NC;
  15006. }
  15007. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15008. return ret;
  15009. }
  15010. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  15011. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  15012. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  15013. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15014. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  15015. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  15016. {
  15017. wc_test_ret_t ret;
  15018. byte e[RSA_TEST_BYTES];
  15019. byte n[RSA_TEST_BYTES];
  15020. word32 eSz = sizeof(e);
  15021. word32 nSz = sizeof(n);
  15022. /* Parameter Validation testing. */
  15023. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  15024. if (ret != BAD_FUNC_ARG)
  15025. return WC_TEST_RET_ENC_EC(ret);
  15026. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  15027. if (ret != BAD_FUNC_ARG)
  15028. return WC_TEST_RET_ENC_EC(ret);
  15029. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  15030. if (ret != BAD_FUNC_ARG)
  15031. return WC_TEST_RET_ENC_EC(ret);
  15032. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  15033. if (ret != BAD_FUNC_ARG)
  15034. return WC_TEST_RET_ENC_EC(ret);
  15035. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  15036. if (ret != BAD_FUNC_ARG)
  15037. return WC_TEST_RET_ENC_EC(ret);
  15038. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  15039. if (ret != 0)
  15040. return WC_TEST_RET_ENC_EC(ret);
  15041. eSz = 0;
  15042. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  15043. if (ret != RSA_BUFFER_E)
  15044. return WC_TEST_RET_ENC_EC(ret);
  15045. eSz = sizeof(e);
  15046. nSz = 0;
  15047. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  15048. if (ret != RSA_BUFFER_E)
  15049. return WC_TEST_RET_ENC_EC(ret);
  15050. return 0;
  15051. }
  15052. #endif /* NO_ASN */
  15053. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  15054. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  15055. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  15056. {
  15057. wc_test_ret_t ret;
  15058. byte e[3];
  15059. word32 eSz = sizeof(e);
  15060. byte n[RSA_TEST_BYTES];
  15061. word32 nSz = sizeof(n);
  15062. byte d[RSA_TEST_BYTES];
  15063. word32 dSz = sizeof(d);
  15064. byte p[RSA_TEST_BYTES/2];
  15065. word32 pSz = sizeof(p);
  15066. byte q[RSA_TEST_BYTES/2];
  15067. word32 qSz = sizeof(q);
  15068. word32 zero = 0;
  15069. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15070. if (ret != BAD_FUNC_ARG)
  15071. return WC_TEST_RET_ENC_EC(ret);
  15072. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15073. if (ret != BAD_FUNC_ARG)
  15074. return WC_TEST_RET_ENC_EC(ret);
  15075. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15076. if (ret != BAD_FUNC_ARG)
  15077. return WC_TEST_RET_ENC_EC(ret);
  15078. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15079. if (ret != BAD_FUNC_ARG)
  15080. return WC_TEST_RET_ENC_EC(ret);
  15081. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  15082. if (ret != BAD_FUNC_ARG)
  15083. return WC_TEST_RET_ENC_EC(ret);
  15084. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  15085. if (ret != BAD_FUNC_ARG)
  15086. return WC_TEST_RET_ENC_EC(ret);
  15087. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  15088. if (ret != BAD_FUNC_ARG)
  15089. return WC_TEST_RET_ENC_EC(ret);
  15090. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  15091. if (ret != BAD_FUNC_ARG)
  15092. return WC_TEST_RET_ENC_EC(ret);
  15093. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  15094. if (ret != BAD_FUNC_ARG)
  15095. return WC_TEST_RET_ENC_EC(ret);
  15096. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  15097. if (ret != BAD_FUNC_ARG)
  15098. return WC_TEST_RET_ENC_EC(ret);
  15099. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  15100. if (ret != BAD_FUNC_ARG)
  15101. return WC_TEST_RET_ENC_EC(ret);
  15102. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15103. if (ret != RSA_BUFFER_E)
  15104. return WC_TEST_RET_ENC_EC(ret);
  15105. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  15106. if (ret != RSA_BUFFER_E)
  15107. return WC_TEST_RET_ENC_EC(ret);
  15108. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15109. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  15110. if (ret != RSA_BUFFER_E)
  15111. return WC_TEST_RET_ENC_EC(ret);
  15112. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  15113. if (ret != RSA_BUFFER_E)
  15114. return WC_TEST_RET_ENC_EC(ret);
  15115. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  15116. if (ret != RSA_BUFFER_E)
  15117. return WC_TEST_RET_ENC_EC(ret);
  15118. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15119. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  15120. if (ret != 0)
  15121. return WC_TEST_RET_ENC_EC(ret);
  15122. return 0;
  15123. }
  15124. #endif /* !HAVE_FIPS && !NO_ASN && !WOLFSSL_RSA_VERIFY_ONLY */
  15125. #ifndef NO_SIG_WRAPPER
  15126. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  15127. {
  15128. wc_test_ret_t ret;
  15129. word32 sigSz;
  15130. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  15131. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  15132. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  15133. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  15134. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  15135. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  15136. };
  15137. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  15138. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  15139. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  15140. 0x00, 0x04, 0x20,
  15141. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  15142. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  15143. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  15144. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  15145. };
  15146. word32 inLen = (word32)XSTRLEN((char*)in);
  15147. byte out[RSA_TEST_BYTES];
  15148. /* Parameter Validation testing. */
  15149. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  15150. if (ret != BAD_FUNC_ARG)
  15151. return WC_TEST_RET_ENC_EC(ret);
  15152. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  15153. if (ret != BAD_FUNC_ARG)
  15154. return WC_TEST_RET_ENC_EC(ret);
  15155. sigSz = (word32)modLen;
  15156. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  15157. inLen, out, &sigSz, key, keyLen, rng);
  15158. if (ret != BAD_FUNC_ARG)
  15159. return WC_TEST_RET_ENC_EC(ret);
  15160. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15161. 0, out, &sigSz, key, keyLen, rng);
  15162. if (ret != BAD_FUNC_ARG)
  15163. return WC_TEST_RET_ENC_EC(ret);
  15164. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15165. inLen, NULL, &sigSz, key, keyLen, rng);
  15166. if (ret != BAD_FUNC_ARG)
  15167. return WC_TEST_RET_ENC_EC(ret);
  15168. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15169. inLen, out, NULL, key, keyLen, rng);
  15170. if (ret != BAD_FUNC_ARG)
  15171. return WC_TEST_RET_ENC_EC(ret);
  15172. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15173. inLen, out, &sigSz, NULL, keyLen, rng);
  15174. if (ret != BAD_FUNC_ARG)
  15175. return WC_TEST_RET_ENC_EC(ret);
  15176. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15177. inLen, out, &sigSz, key, 0, rng);
  15178. if (ret != BAD_FUNC_ARG)
  15179. return WC_TEST_RET_ENC_EC(ret);
  15180. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15181. inLen, out, &sigSz, key, keyLen, NULL);
  15182. #if defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  15183. /* blinding / rng handled with hardware acceleration */
  15184. if (ret != 0)
  15185. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  15186. /* async may not require RNG */
  15187. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15188. if (ret != NO_VALID_DEVID)
  15189. #else
  15190. if (ret != 0 && ret != MISSING_RNG_E)
  15191. #endif
  15192. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  15193. /* FIPS140 implementation does not do blinding */
  15194. if (ret != 0)
  15195. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  15196. if (ret != SIG_TYPE_E)
  15197. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  15198. /* RNG is handled by hardware */
  15199. if (ret != 0)
  15200. #else
  15201. if (ret != MISSING_RNG_E)
  15202. #endif
  15203. return WC_TEST_RET_ENC_EC(ret);
  15204. sigSz = 0;
  15205. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15206. inLen, out, &sigSz, key, keyLen, rng);
  15207. if (ret != BAD_FUNC_ARG)
  15208. return WC_TEST_RET_ENC_EC(ret);
  15209. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  15210. inLen, out, (word32)modLen, key, keyLen);
  15211. if (ret != BAD_FUNC_ARG)
  15212. return WC_TEST_RET_ENC_EC(ret);
  15213. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15214. 0, out, (word32)modLen, key, keyLen);
  15215. if (ret != BAD_FUNC_ARG)
  15216. return WC_TEST_RET_ENC_EC(ret);
  15217. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15218. inLen, NULL, (word32)modLen, key, keyLen);
  15219. if (ret != BAD_FUNC_ARG)
  15220. return WC_TEST_RET_ENC_EC(ret);
  15221. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15222. inLen, out, 0, key, keyLen);
  15223. if (ret != BAD_FUNC_ARG)
  15224. return WC_TEST_RET_ENC_EC(ret);
  15225. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15226. inLen, out, (word32)modLen, NULL, keyLen);
  15227. if (ret != BAD_FUNC_ARG)
  15228. return WC_TEST_RET_ENC_EC(ret);
  15229. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15230. inLen, out, (word32)modLen, key, 0);
  15231. if (ret != BAD_FUNC_ARG)
  15232. return WC_TEST_RET_ENC_EC(ret);
  15233. #ifndef HAVE_ECC
  15234. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  15235. if (ret != SIG_TYPE_E)
  15236. return WC_TEST_RET_ENC_EC(ret);
  15237. #endif
  15238. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15239. return 0;
  15240. #endif
  15241. /* Use APIs. */
  15242. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  15243. if (ret != modLen)
  15244. return WC_TEST_RET_ENC_EC(ret);
  15245. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  15246. if (ret != modLen)
  15247. return WC_TEST_RET_ENC_EC(ret);
  15248. sigSz = (word32)ret;
  15249. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  15250. XMEMSET(out, 0, sizeof(out));
  15251. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15252. inLen, out, &sigSz, key, keyLen, rng);
  15253. if (ret != 0)
  15254. return WC_TEST_RET_ENC_EC(ret);
  15255. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15256. inLen, out, (word32)modLen, key, keyLen);
  15257. if (ret != 0)
  15258. return WC_TEST_RET_ENC_EC(ret);
  15259. sigSz = (word32)sizeof(out);
  15260. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  15261. in, inLen, out, &sigSz, key, keyLen, rng);
  15262. if (ret != 0)
  15263. return WC_TEST_RET_ENC_EC(ret);
  15264. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  15265. in, inLen, out, (word32)modLen, key, keyLen);
  15266. if (ret != 0)
  15267. return WC_TEST_RET_ENC_EC(ret);
  15268. /* Wrong signature type. */
  15269. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  15270. inLen, out, (word32)modLen, key, keyLen);
  15271. if (ret == 0)
  15272. return WC_TEST_RET_ENC_EC(ret);
  15273. /* check hash functions */
  15274. sigSz = (word32)sizeof(out);
  15275. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  15276. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  15277. if (ret != 0)
  15278. return WC_TEST_RET_ENC_EC(ret);
  15279. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  15280. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  15281. if (ret != 0)
  15282. return WC_TEST_RET_ENC_EC(ret);
  15283. sigSz = (word32)sizeof(out);
  15284. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  15285. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  15286. if (ret != 0)
  15287. return WC_TEST_RET_ENC_EC(ret);
  15288. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  15289. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  15290. if (ret != 0)
  15291. return WC_TEST_RET_ENC_EC(ret);
  15292. #else
  15293. (void)hash;
  15294. (void)hashEnc;
  15295. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  15296. return 0;
  15297. }
  15298. #endif /* !NO_SIG_WRAPPER */
  15299. #ifdef WC_RSA_NONBLOCK
  15300. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  15301. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  15302. {
  15303. wc_test_ret_t ret = 0;
  15304. int count;
  15305. int signSz = 0;
  15306. RsaNb nb;
  15307. byte* inlinePlain = NULL;
  15308. /* Enable non-blocking RSA mode - provide context */
  15309. ret = wc_RsaSetNonBlock(key, &nb);
  15310. if (ret != 0)
  15311. return ret;
  15312. #ifdef WC_RSA_NONBLOCK_TIME
  15313. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  15314. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  15315. if (ret != 0)
  15316. return ret;
  15317. #endif
  15318. count = 0;
  15319. do {
  15320. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  15321. count++; /* track number of would blocks */
  15322. if (ret == FP_WOULDBLOCK) {
  15323. /* do "other" work here */
  15324. }
  15325. } while (ret == FP_WOULDBLOCK);
  15326. if (ret < 0) {
  15327. return ret;
  15328. }
  15329. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  15330. printf("RSA non-block sign: %d times\n", count);
  15331. #endif
  15332. signSz = ret;
  15333. /* Test non-blocking verify */
  15334. XMEMSET(plain, 0, plainSz);
  15335. count = 0;
  15336. do {
  15337. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  15338. count++; /* track number of would blocks */
  15339. if (ret == FP_WOULDBLOCK) {
  15340. /* do "other" work here */
  15341. }
  15342. } while (ret == FP_WOULDBLOCK);
  15343. if (ret < 0) {
  15344. return ret;
  15345. }
  15346. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  15347. printf("RSA non-block verify: %d times\n", count);
  15348. #endif
  15349. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  15350. return SIG_VERIFY_E;
  15351. }
  15352. /* Test inline non-blocking verify */
  15353. count = 0;
  15354. do {
  15355. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  15356. count++; /* track number of would blocks */
  15357. if (ret == FP_WOULDBLOCK) {
  15358. /* do "other" work here */
  15359. }
  15360. } while (ret == FP_WOULDBLOCK);
  15361. if (ret < 0) {
  15362. return ret;
  15363. }
  15364. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  15365. printf("RSA non-block inline verify: %d times\n", count);
  15366. #endif
  15367. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  15368. return SIG_VERIFY_E;
  15369. }
  15370. /* Disabling non-block RSA mode */
  15371. ret = wc_RsaSetNonBlock(key, NULL);
  15372. (void)count;
  15373. return 0;
  15374. }
  15375. #endif
  15376. #if !defined(NO_ASN)
  15377. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  15378. {
  15379. wc_test_ret_t ret;
  15380. word32 inSz;
  15381. word32 inOutIdx;
  15382. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  15383. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  15384. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  15385. 0x03 };
  15386. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  15387. 0x30, 0x18, 0x30, 0x16,
  15388. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15389. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15390. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  15391. 0x30, 0x1a, 0x30, 0x18,
  15392. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15393. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  15394. 0x02, 0x1, 0x03 };
  15395. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  15396. 0x30, 0x1b, 0x30, 0x19,
  15397. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15398. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  15399. 0x02, 0x1, 0x03 };
  15400. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  15401. 0x30, 0x18, 0x30, 0x16,
  15402. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15403. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15404. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  15405. 0x30, 0x18, 0x30, 0x16,
  15406. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15407. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15408. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  15409. 0x30, 0x16, 0x30, 0x14,
  15410. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15411. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15412. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  15413. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  15414. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15415. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  15416. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  15417. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  15418. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  15419. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  15420. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15421. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  15422. 0x30, 0x17, 0x30, 0x15,
  15423. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  15424. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  15425. ret = wc_InitRsaKey(keyPub, NULL);
  15426. if (ret != 0)
  15427. return WC_TEST_RET_ENC_EC(ret);
  15428. /* Parameter Validation testing. */
  15429. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  15430. if (ret != BAD_FUNC_ARG) {
  15431. ret = WC_TEST_RET_ENC_EC(ret);
  15432. goto done;
  15433. }
  15434. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  15435. if (ret != BAD_FUNC_ARG) {
  15436. ret = WC_TEST_RET_ENC_EC(ret);
  15437. goto done;
  15438. }
  15439. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  15440. if (ret != BAD_FUNC_ARG) {
  15441. ret = WC_TEST_RET_ENC_EC(ret);
  15442. goto done;
  15443. }
  15444. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  15445. #if defined(USE_INTEGER_HEAP_MATH)
  15446. if (ret != 0)
  15447. #else
  15448. if (ret != ASN_GETINT_E)
  15449. #endif
  15450. {
  15451. ret = WC_TEST_RET_ENC_EC(ret);
  15452. goto done;
  15453. }
  15454. wc_FreeRsaKey(keyPub);
  15455. ret = wc_InitRsaKey(keyPub, NULL);
  15456. if (ret != 0)
  15457. return WC_TEST_RET_ENC_EC(ret);
  15458. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  15459. #if defined(USE_INTEGER_HEAP_MATH)
  15460. if (ret != 0)
  15461. #else
  15462. if (ret != ASN_GETINT_E)
  15463. #endif
  15464. {
  15465. ret = WC_TEST_RET_ENC_EC(ret);
  15466. goto done;
  15467. }
  15468. wc_FreeRsaKey(keyPub);
  15469. ret = wc_InitRsaKey(keyPub, NULL);
  15470. if (ret != 0)
  15471. return WC_TEST_RET_ENC_EC(ret);
  15472. /* Use API. */
  15473. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  15474. if (ret != 0) {
  15475. ret = WC_TEST_RET_ENC_EC(ret);
  15476. goto done;
  15477. }
  15478. wc_FreeRsaKey(keyPub);
  15479. ret = wc_InitRsaKey(keyPub, NULL);
  15480. if (ret != 0)
  15481. return WC_TEST_RET_ENC_EC(ret);
  15482. /* Parameter Validation testing. */
  15483. inSz = sizeof(good);
  15484. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  15485. if (ret != BAD_FUNC_ARG) {
  15486. ret = WC_TEST_RET_ENC_EC(ret);
  15487. goto done;
  15488. }
  15489. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  15490. if (ret != BAD_FUNC_ARG) {
  15491. ret = WC_TEST_RET_ENC_EC(ret);
  15492. goto done;
  15493. }
  15494. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  15495. if (ret != BAD_FUNC_ARG) {
  15496. ret = WC_TEST_RET_ENC_EC(ret);
  15497. goto done;
  15498. }
  15499. /* Use good data and offset to bad data. */
  15500. inOutIdx = 2;
  15501. inSz = sizeof(good) - inOutIdx;
  15502. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  15503. if (ret != ASN_PARSE_E) {
  15504. ret = WC_TEST_RET_ENC_EC(ret);
  15505. goto done;
  15506. }
  15507. inOutIdx = 2;
  15508. inSz = sizeof(goodAlgId) - inOutIdx;
  15509. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15510. if (ret != ASN_PARSE_E) {
  15511. ret = WC_TEST_RET_ENC_EC(ret);
  15512. goto done;
  15513. }
  15514. inOutIdx = 2;
  15515. inSz = sizeof(goodAlgId);
  15516. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15517. #ifndef WOLFSSL_NO_DECODE_EXTRA
  15518. if (ret != ASN_PARSE_E)
  15519. #else
  15520. if (ret != ASN_RSA_KEY_E)
  15521. #endif
  15522. {
  15523. ret = WC_TEST_RET_ENC_EC(ret);
  15524. goto done;
  15525. }
  15526. /* Try different bad data. */
  15527. inSz = sizeof(badAlgIdNull);
  15528. inOutIdx = 0;
  15529. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  15530. if (ret != ASN_EXPECT_0_E) {
  15531. ret = WC_TEST_RET_ENC_EC(ret);
  15532. goto done;
  15533. }
  15534. inSz = sizeof(badNotBitString);
  15535. inOutIdx = 0;
  15536. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  15537. if (ret != ASN_BITSTR_E) {
  15538. ret = WC_TEST_RET_ENC_EC(ret);
  15539. goto done;
  15540. }
  15541. inSz = sizeof(badBitStringLen);
  15542. inOutIdx = 0;
  15543. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  15544. if (ret != ASN_PARSE_E) {
  15545. ret = WC_TEST_RET_ENC_EC(ret);
  15546. goto done;
  15547. }
  15548. inSz = sizeof(badNoSeq);
  15549. inOutIdx = 0;
  15550. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  15551. if (ret != ASN_PARSE_E) {
  15552. ret = WC_TEST_RET_ENC_EC(ret);
  15553. goto done;
  15554. }
  15555. inSz = sizeof(badNoObj);
  15556. inOutIdx = 0;
  15557. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  15558. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  15559. ret = WC_TEST_RET_ENC_EC(ret);
  15560. goto done;
  15561. }
  15562. inSz = sizeof(badIntN);
  15563. inOutIdx = 0;
  15564. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  15565. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  15566. ret = WC_TEST_RET_ENC_EC(ret);
  15567. goto done;
  15568. }
  15569. inSz = sizeof(badNotIntE);
  15570. inOutIdx = 0;
  15571. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  15572. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  15573. ret = WC_TEST_RET_ENC_EC(ret);
  15574. goto done;
  15575. }
  15576. /* TODO: Shouldn't pass as the sequence length is too small. */
  15577. inSz = sizeof(badLength);
  15578. inOutIdx = 0;
  15579. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  15580. #ifndef WOLFSSL_ASN_TEMPLATE
  15581. if (ret != 0)
  15582. #else
  15583. if (ret != ASN_PARSE_E)
  15584. #endif
  15585. {
  15586. ret = WC_TEST_RET_ENC_EC(ret);
  15587. goto done;
  15588. }
  15589. /* TODO: Shouldn't ignore object id's data. */
  15590. wc_FreeRsaKey(keyPub);
  15591. ret = wc_InitRsaKey(keyPub, NULL);
  15592. if (ret != 0)
  15593. return WC_TEST_RET_ENC_EC(ret);
  15594. inSz = sizeof(badBitStrNoZero);
  15595. inOutIdx = 0;
  15596. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  15597. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  15598. ret = WC_TEST_RET_ENC_EC(ret);
  15599. goto done;
  15600. }
  15601. wc_FreeRsaKey(keyPub);
  15602. ret = wc_InitRsaKey(keyPub, NULL);
  15603. if (ret != 0)
  15604. return WC_TEST_RET_ENC_EC(ret);
  15605. /* Valid data cases. */
  15606. inSz = sizeof(good);
  15607. inOutIdx = 0;
  15608. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  15609. if (ret != 0) {
  15610. ret = WC_TEST_RET_ENC_EC(ret);
  15611. goto done;
  15612. }
  15613. if (inOutIdx != inSz) {
  15614. ret = WC_TEST_RET_ENC_NC;
  15615. goto done;
  15616. }
  15617. wc_FreeRsaKey(keyPub);
  15618. ret = wc_InitRsaKey(keyPub, NULL);
  15619. if (ret != 0)
  15620. return WC_TEST_RET_ENC_EC(ret);
  15621. inSz = sizeof(goodAlgId);
  15622. inOutIdx = 0;
  15623. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  15624. if (ret != 0) {
  15625. ret = WC_TEST_RET_ENC_EC(ret);
  15626. goto done;
  15627. }
  15628. if (inOutIdx != inSz) {
  15629. ret = WC_TEST_RET_ENC_NC;
  15630. goto done;
  15631. }
  15632. wc_FreeRsaKey(keyPub);
  15633. ret = wc_InitRsaKey(keyPub, NULL);
  15634. if (ret != 0)
  15635. return WC_TEST_RET_ENC_EC(ret);
  15636. inSz = sizeof(goodAlgIdNull);
  15637. inOutIdx = 0;
  15638. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  15639. if (ret != 0) {
  15640. ret = WC_TEST_RET_ENC_EC(ret);
  15641. goto done;
  15642. }
  15643. if (inOutIdx != inSz) {
  15644. ret = WC_TEST_RET_ENC_NC;
  15645. goto done;
  15646. }
  15647. done:
  15648. wc_FreeRsaKey(keyPub);
  15649. return ret;
  15650. }
  15651. #endif
  15652. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  15653. /* Need to create known good signatures to test with this. */
  15654. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15655. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15656. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  15657. {
  15658. byte digest[WC_MAX_DIGEST_SIZE];
  15659. wc_test_ret_t ret = 0;
  15660. const char inStr[] = TEST_STRING;
  15661. word32 inLen = (word32)TEST_STRING_SZ;
  15662. word32 outSz;
  15663. word32 plainSz;
  15664. word32 digestSz;
  15665. int i, j;
  15666. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  15667. int k, l;
  15668. #endif
  15669. #ifndef WOLFSSL_SE050
  15670. int len;
  15671. #endif
  15672. byte* plain;
  15673. int mgf[] = {
  15674. #ifndef NO_SHA
  15675. WC_MGF1SHA1,
  15676. #endif
  15677. #ifdef WOLFSSL_SHA224
  15678. WC_MGF1SHA224,
  15679. #endif
  15680. WC_MGF1SHA256,
  15681. #ifdef WOLFSSL_SHA384
  15682. WC_MGF1SHA384,
  15683. #endif
  15684. #ifdef WOLFSSL_SHA512
  15685. WC_MGF1SHA512
  15686. #endif
  15687. };
  15688. enum wc_HashType hash[] = {
  15689. #ifndef NO_SHA
  15690. WC_HASH_TYPE_SHA,
  15691. #endif
  15692. #ifdef WOLFSSL_SHA224
  15693. WC_HASH_TYPE_SHA224,
  15694. #endif
  15695. WC_HASH_TYPE_SHA256,
  15696. #ifdef WOLFSSL_SHA384
  15697. WC_HASH_TYPE_SHA384,
  15698. #endif
  15699. #ifdef WOLFSSL_SHA512
  15700. WC_HASH_TYPE_SHA512,
  15701. #endif
  15702. };
  15703. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  15704. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15705. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  15706. WC_ALLOC_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  15707. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15708. WC_ALLOC_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  15709. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15710. if (in == NULL || out == NULL || sig == NULL)
  15711. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  15712. #endif
  15713. XMEMCPY(in, inStr, inLen);
  15714. /* Test all combinations of hash and MGF. */
  15715. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  15716. /* Calculate hash of message. */
  15717. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  15718. if (ret != 0)
  15719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15720. digestSz = wc_HashGetDigestSize(hash[j]);
  15721. #ifdef WOLFSSL_SE050
  15722. /* SE050 only supports MGF matched to same hash type */
  15723. i = j;
  15724. #else
  15725. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  15726. #endif
  15727. outSz = RSA_TEST_BYTES;
  15728. do {
  15729. #if defined(WOLFSSL_ASYNC_CRYPT)
  15730. ret = wc_AsyncWait(ret, &key->asyncDev,
  15731. WC_ASYNC_FLAG_CALL_AGAIN);
  15732. #endif
  15733. if (ret >= 0) {
  15734. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  15735. hash[j], mgf[i], -1, key, rng);
  15736. }
  15737. } while (ret == WC_PENDING_E);
  15738. if (ret <= 0)
  15739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15740. outSz = (word32)ret;
  15741. XMEMCPY(sig, out, outSz);
  15742. plain = NULL;
  15743. TEST_SLEEP();
  15744. do {
  15745. #if defined(WOLFSSL_ASYNC_CRYPT)
  15746. ret = wc_AsyncWait(ret, &key->asyncDev,
  15747. WC_ASYNC_FLAG_CALL_AGAIN);
  15748. #endif
  15749. if (ret >= 0) {
  15750. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  15751. mgf[i], -1, key);
  15752. }
  15753. } while (ret == WC_PENDING_E);
  15754. if (ret <= 0)
  15755. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15756. plainSz = (word32)ret;
  15757. TEST_SLEEP();
  15758. #if defined(HAVE_SELFTEST) && \
  15759. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15760. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15761. hash[j], -1);
  15762. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15763. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15764. hash[j], -1, 0);
  15765. #else
  15766. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  15767. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  15768. #endif
  15769. if (ret != 0)
  15770. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15771. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  15772. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  15773. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  15774. if (i == k && j == l)
  15775. continue;
  15776. XMEMCPY(sig, out, outSz);
  15777. do {
  15778. #if defined(WOLFSSL_ASYNC_CRYPT)
  15779. ret = wc_AsyncWait(ret, &key->asyncDev,
  15780. WC_ASYNC_FLAG_CALL_AGAIN);
  15781. #endif
  15782. if (ret >= 0) {
  15783. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  15784. (byte**)&plain, hash[l], mgf[k], -1, key);
  15785. }
  15786. } while (ret == WC_PENDING_E);
  15787. if (ret >= 0)
  15788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15789. }
  15790. }
  15791. #endif
  15792. #ifndef WOLFSSL_SE050
  15793. } /* end mgf for loop */
  15794. #endif
  15795. }
  15796. /* SE050 generates salts internally only of hash length */
  15797. #ifndef WOLFSSL_SE050
  15798. /* Test that a salt length of zero works. */
  15799. digestSz = wc_HashGetDigestSize(hash[0]);
  15800. outSz = RSA_TEST_BYTES;
  15801. do {
  15802. #if defined(WOLFSSL_ASYNC_CRYPT)
  15803. ret = wc_AsyncWait(ret, &key->asyncDev,
  15804. WC_ASYNC_FLAG_CALL_AGAIN);
  15805. #endif
  15806. if (ret >= 0) {
  15807. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15808. mgf[0], 0, key, rng);
  15809. }
  15810. } while (ret == WC_PENDING_E);
  15811. if (ret <= 0)
  15812. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15813. outSz = (word32)ret;
  15814. TEST_SLEEP();
  15815. do {
  15816. #if defined(WOLFSSL_ASYNC_CRYPT)
  15817. ret = wc_AsyncWait(ret, &key->asyncDev,
  15818. WC_ASYNC_FLAG_CALL_AGAIN);
  15819. #endif
  15820. if (ret >= 0) {
  15821. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  15822. 0, key);
  15823. }
  15824. } while (ret == WC_PENDING_E);
  15825. if (ret <= 0)
  15826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15827. plainSz = (word32)ret;
  15828. TEST_SLEEP();
  15829. do {
  15830. #if defined(WOLFSSL_ASYNC_CRYPT)
  15831. ret = wc_AsyncWait(ret, &key->asyncDev,
  15832. WC_ASYNC_FLAG_CALL_AGAIN);
  15833. #endif
  15834. if (ret >= 0) {
  15835. #if defined(HAVE_SELFTEST) && \
  15836. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15837. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  15838. hash[0], 0);
  15839. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15840. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  15841. hash[0], 0, 0);
  15842. #else
  15843. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  15844. hash[0], 0, 0, HEAP_HINT);
  15845. #endif
  15846. }
  15847. } while (ret == WC_PENDING_E);
  15848. if (ret != 0)
  15849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15850. XMEMCPY(sig, out, outSz);
  15851. plain = NULL;
  15852. do {
  15853. #if defined(WOLFSSL_ASYNC_CRYPT)
  15854. ret = wc_AsyncWait(ret, &key->asyncDev,
  15855. WC_ASYNC_FLAG_CALL_AGAIN);
  15856. #endif
  15857. if (ret >= 0) {
  15858. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  15859. 0, key);
  15860. }
  15861. } while (ret == WC_PENDING_E);
  15862. if (ret <= 0)
  15863. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15864. plainSz = (word32)ret;
  15865. TEST_SLEEP();
  15866. #if defined(HAVE_SELFTEST) && \
  15867. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15868. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15869. hash[0], 0);
  15870. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15871. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15872. hash[0], 0, 0);
  15873. #else
  15874. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15875. 0, 0, HEAP_HINT);
  15876. #endif
  15877. if (ret != 0)
  15878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15879. /* Test bad salt lengths in various APIs. */
  15880. digestSz = wc_HashGetDigestSize(hash[0]);
  15881. outSz = RSA_TEST_BYTES;
  15882. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  15883. len = -2;
  15884. #else
  15885. len = -3;
  15886. #endif
  15887. do {
  15888. #if defined(WOLFSSL_ASYNC_CRYPT)
  15889. ret = wc_AsyncWait(ret, &key->asyncDev,
  15890. WC_ASYNC_FLAG_CALL_AGAIN);
  15891. #endif
  15892. if (ret >= 0) {
  15893. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15894. mgf[0], len, key, rng);
  15895. }
  15896. } while (ret == WC_PENDING_E);
  15897. if (ret != PSS_SALTLEN_E)
  15898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15899. do {
  15900. #if defined(WOLFSSL_ASYNC_CRYPT)
  15901. ret = wc_AsyncWait(ret, &key->asyncDev,
  15902. WC_ASYNC_FLAG_CALL_AGAIN);
  15903. #endif
  15904. if (ret >= 0) {
  15905. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  15906. mgf[0], digestSz + 1, key, rng);
  15907. }
  15908. } while (ret == WC_PENDING_E);
  15909. if (ret != PSS_SALTLEN_E)
  15910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15911. TEST_SLEEP();
  15912. do {
  15913. #if defined(WOLFSSL_ASYNC_CRYPT)
  15914. ret = wc_AsyncWait(ret, &key->asyncDev,
  15915. WC_ASYNC_FLAG_CALL_AGAIN);
  15916. #endif
  15917. if (ret >= 0) {
  15918. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  15919. mgf[0], -2, key);
  15920. }
  15921. } while (ret == WC_PENDING_E);
  15922. if (ret != PSS_SALTLEN_E)
  15923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15924. TEST_SLEEP();
  15925. do {
  15926. #if defined(WOLFSSL_ASYNC_CRYPT)
  15927. ret = wc_AsyncWait(ret, &key->asyncDev,
  15928. WC_ASYNC_FLAG_CALL_AGAIN);
  15929. #endif
  15930. if (ret >= 0) {
  15931. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  15932. digestSz + 1, key);
  15933. }
  15934. } while (ret == WC_PENDING_E);
  15935. if (ret != PSS_SALTLEN_E)
  15936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15937. TEST_SLEEP();
  15938. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  15939. len = -2;
  15940. #else
  15941. len = -3;
  15942. #endif
  15943. #if defined(HAVE_SELFTEST) && \
  15944. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15945. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15946. hash[0], len);
  15947. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15948. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15949. hash[0], len, 0);
  15950. #else
  15951. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15952. len, 0, HEAP_HINT);
  15953. #endif
  15954. if (ret != PSS_SALTLEN_E)
  15955. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15956. #ifndef WOLFSSL_PSS_LONG_SALT
  15957. len = digestSz + 1;
  15958. #else
  15959. len = plainSz - digestSz - 1;
  15960. #endif
  15961. #if defined(HAVE_SELFTEST) && \
  15962. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  15963. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15964. hash[0], len);
  15965. if (ret != PSS_SALTLEN_E)
  15966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15967. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  15968. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  15969. hash[0], len, 0);
  15970. if (ret != BAD_PADDING_E)
  15971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15972. #else
  15973. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  15974. len, 0, HEAP_HINT);
  15975. if (ret != PSS_SALTLEN_E)
  15976. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  15977. #endif
  15978. ret = 0;
  15979. #endif /* WOLFSSL_SE050 */
  15980. exit_rsa_pss:
  15981. WC_FREE_VAR(sig, HEAP_HINT);
  15982. WC_FREE_VAR(in, HEAP_HINT);
  15983. WC_FREE_VAR(out, HEAP_HINT);
  15984. return ret;
  15985. }
  15986. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  15987. #endif
  15988. #ifdef WC_RSA_NO_PADDING
  15989. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  15990. {
  15991. WC_RNG rng;
  15992. byte* tmp = NULL;
  15993. size_t bytes;
  15994. wc_test_ret_t ret;
  15995. word32 inLen = 0;
  15996. word32 idx = 0;
  15997. word32 outSz = RSA_TEST_BYTES;
  15998. word32 plainSz = RSA_TEST_BYTES;
  15999. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  16000. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  16001. !defined(NO_FILESYSTEM)
  16002. XFILE file;
  16003. #endif
  16004. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  16005. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16006. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16007. WC_ALLOC_VAR(key, RsaKey, 1, HEAP_HINT);
  16008. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16009. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16010. WOLFSSL_ENTER("rsa_no_pad_test");
  16011. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16012. if (key == NULL || out == NULL || plain == NULL)
  16013. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  16014. #endif
  16015. /* initialize stack structures */
  16016. XMEMSET(&rng, 0, sizeof(rng));
  16017. XMEMSET(key, 0, sizeof(RsaKey));
  16018. #ifdef USE_CERT_BUFFERS_1024
  16019. bytes = (size_t)sizeof_client_key_der_1024;
  16020. if (bytes < (size_t)sizeof_client_cert_der_1024)
  16021. bytes = (size_t)sizeof_client_cert_der_1024;
  16022. #elif defined(USE_CERT_BUFFERS_2048)
  16023. bytes = (size_t)sizeof_client_key_der_2048;
  16024. if (bytes < (size_t)sizeof_client_cert_der_2048)
  16025. bytes = (size_t)sizeof_client_cert_der_2048;
  16026. #else
  16027. bytes = FOURK_BUF;
  16028. #endif
  16029. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16030. if (tmp == NULL
  16031. #ifdef WOLFSSL_ASYNC_CRYPT
  16032. || out == NULL || plain == NULL
  16033. #endif
  16034. ) {
  16035. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  16036. }
  16037. #ifdef USE_CERT_BUFFERS_1024
  16038. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  16039. #elif defined(USE_CERT_BUFFERS_2048)
  16040. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  16041. #elif defined(USE_CERT_BUFFERS_3072)
  16042. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  16043. #elif defined(USE_CERT_BUFFERS_4096)
  16044. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  16045. #elif !defined(NO_FILESYSTEM)
  16046. file = XFOPEN(clientKey, "rb");
  16047. if (!file) {
  16048. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  16049. WC_TEST_RET_ENC_ERRNO);
  16050. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  16051. }
  16052. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16053. XFCLOSE(file);
  16054. if (bytes == 0)
  16055. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  16056. #else
  16057. /* No key to use. */
  16058. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  16059. #endif /* USE_CERT_BUFFERS */
  16060. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  16061. if (ret != 0) {
  16062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16063. }
  16064. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  16065. if (ret != 0) {
  16066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16067. }
  16068. /* after loading in key use tmp as the test buffer */
  16069. #ifndef HAVE_FIPS
  16070. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16071. #else
  16072. ret = wc_InitRng(&rng);
  16073. #endif
  16074. if (ret != 0) {
  16075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16076. }
  16077. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16078. inLen = wc_RsaEncryptSize(key);
  16079. outSz = inLen;
  16080. plainSz = inLen;
  16081. XMEMSET(tmp, 7, inLen);
  16082. do {
  16083. #if defined(WOLFSSL_ASYNC_CRYPT)
  16084. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16085. #endif
  16086. if (ret >= 0) {
  16087. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  16088. RSA_PRIVATE_ENCRYPT, &rng);
  16089. }
  16090. } while (ret == WC_PENDING_E);
  16091. if (ret <= 0) {
  16092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16093. }
  16094. /* encrypted result should not be the same as input */
  16095. if (XMEMCMP(out, tmp, inLen) == 0) {
  16096. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  16097. }
  16098. TEST_SLEEP();
  16099. /* decrypt with public key and compare result */
  16100. do {
  16101. #if defined(WOLFSSL_ASYNC_CRYPT)
  16102. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16103. #endif
  16104. if (ret >= 0) {
  16105. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  16106. RSA_PUBLIC_DECRYPT, &rng);
  16107. }
  16108. } while (ret == WC_PENDING_E);
  16109. if (ret <= 0) {
  16110. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16111. }
  16112. if (XMEMCMP(plain, tmp, inLen) != 0) {
  16113. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  16114. }
  16115. TEST_SLEEP();
  16116. #endif
  16117. #ifdef WC_RSA_BLINDING
  16118. ret = wc_RsaSetRNG(NULL, &rng);
  16119. if (ret != BAD_FUNC_ARG) {
  16120. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16121. }
  16122. ret = wc_RsaSetRNG(key, &rng);
  16123. if (ret < 0) {
  16124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16125. }
  16126. #endif
  16127. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  16128. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16129. do {
  16130. #if defined(WOLFSSL_ASYNC_CRYPT)
  16131. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16132. #endif
  16133. if (ret >= 0) {
  16134. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  16135. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  16136. }
  16137. } while (ret == WC_PENDING_E);
  16138. if (ret < 0) {
  16139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16140. }
  16141. TEST_SLEEP();
  16142. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  16143. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16144. do {
  16145. #if defined(WOLFSSL_ASYNC_CRYPT)
  16146. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16147. #endif
  16148. if (ret >= 0) {
  16149. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  16150. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  16151. }
  16152. } while (ret == WC_PENDING_E);
  16153. if (ret < 0) {
  16154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16155. }
  16156. if (XMEMCMP(plain, tmp, inLen) != 0) {
  16157. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  16158. }
  16159. TEST_SLEEP();
  16160. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16161. /* test some bad arguments */
  16162. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  16163. &rng);
  16164. if (ret != BAD_FUNC_ARG) {
  16165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16166. }
  16167. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  16168. &rng);
  16169. if (ret != BAD_FUNC_ARG) {
  16170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16171. }
  16172. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  16173. &rng);
  16174. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  16175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16176. }
  16177. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  16178. RSA_PUBLIC_DECRYPT, &rng);
  16179. if (ret != BAD_FUNC_ARG) {
  16180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  16181. }
  16182. /* if making it to this point of code without hitting an ERROR_OUT then
  16183. * all tests have passed */
  16184. ret = 0;
  16185. exit_rsa_nopadding:
  16186. wc_FreeRsaKey(key);
  16187. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16188. WC_FREE_VAR(key, HEAP_HINT);
  16189. WC_FREE_VAR(out, HEAP_HINT);
  16190. WC_FREE_VAR(plain, HEAP_HINT);
  16191. wc_FreeRng(&rng);
  16192. return ret;
  16193. }
  16194. #endif /* WC_RSA_NO_PADDING */
  16195. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  16196. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  16197. {
  16198. byte* tmp = NULL;
  16199. size_t bytes;
  16200. wc_test_ret_t ret;
  16201. word32 inLen = 0;
  16202. #ifndef NO_ASN
  16203. word32 idx = 0;
  16204. #endif
  16205. word32 outSz = RSA_TEST_BYTES;
  16206. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16207. word32 plainSz = RSA_TEST_BYTES;
  16208. #endif
  16209. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  16210. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  16211. XFILE file;
  16212. #endif
  16213. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16214. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16215. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16216. #endif
  16217. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16218. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16219. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16220. #endif
  16221. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16222. if (out == NULL
  16223. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16224. || plain == NULL
  16225. #endif
  16226. ) {
  16227. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  16228. }
  16229. #endif
  16230. #if defined(USE_CERT_BUFFERS_2048)
  16231. bytes = (size_t)sizeof_client_key_der_2048;
  16232. if (bytes < (size_t)sizeof_client_cert_der_2048)
  16233. bytes = (size_t)sizeof_client_cert_der_2048;
  16234. #else
  16235. bytes = FOURK_BUF;
  16236. #endif
  16237. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16238. if (tmp == NULL
  16239. #ifdef WOLFSSL_ASYNC_CRYPT
  16240. || out == NULL || plain == NULL
  16241. #endif
  16242. ) {
  16243. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  16244. }
  16245. #if defined(USE_CERT_BUFFERS_2048)
  16246. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  16247. #elif defined(USE_CERT_BUFFERS_3072)
  16248. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  16249. #elif defined(USE_CERT_BUFFERS_4096)
  16250. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  16251. #elif !defined(NO_FILESYSTEM)
  16252. file = XFOPEN(clientKey, "rb");
  16253. if (!file) {
  16254. err_sys("can't open ./certs/client-key.der, "
  16255. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  16256. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  16257. }
  16258. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16259. XFCLOSE(file);
  16260. if (bytes == 0)
  16261. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  16262. #else
  16263. /* No key to use. */
  16264. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  16265. #endif /* USE_CERT_BUFFERS */
  16266. #ifndef NO_ASN
  16267. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  16268. if (ret != 0) {
  16269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16270. }
  16271. #else
  16272. #ifdef USE_CERT_BUFFERS_2048
  16273. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  16274. if (ret != 0) {
  16275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16276. }
  16277. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  16278. if (ret != 0) {
  16279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16280. }
  16281. #ifndef NO_SIG_WRAPPER
  16282. modLen = 2048;
  16283. #endif
  16284. #else
  16285. #error Not supported yet!
  16286. #endif
  16287. #endif
  16288. key->n.dp[0] &= (mp_digit)-2;
  16289. if (ret != 0) {
  16290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16291. }
  16292. /* after loading in key use tmp as the test buffer */
  16293. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  16294. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  16295. !defined(WOLFSSL_XILINX_CRYPT)
  16296. /* The ARM64_ASM code that was FIPS validated did not return these expected
  16297. * failure codes. These tests cases were added after the assembly was
  16298. * in-lined in the module and validated, these tests will be available in
  16299. * the 140-3 module */
  16300. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  16301. inLen = 32;
  16302. outSz = wc_RsaEncryptSize(key);
  16303. XMEMSET(tmp, 7, plainSz);
  16304. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  16305. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  16306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16307. }
  16308. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  16309. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  16310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16311. }
  16312. #endif
  16313. #ifdef WC_RSA_BLINDING
  16314. ret = wc_RsaSetRNG(key, rng);
  16315. if (ret < 0) {
  16316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16317. }
  16318. #endif
  16319. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  16320. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  16321. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  16322. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  16323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16324. }
  16325. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  16326. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16327. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  16328. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  16329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  16330. }
  16331. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16332. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  16333. /* if making it to this point of code without hitting an ERROR_OUT then
  16334. * all tests have passed */
  16335. ret = 0;
  16336. exit_rsa_even_mod:
  16337. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16338. WC_FREE_VAR(out, HEAP_HINT);
  16339. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16340. WC_FREE_VAR(plain, HEAP_HINT);
  16341. #endif
  16342. (void)out;
  16343. (void)outSz;
  16344. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16345. (void)plain;
  16346. (void)plainSz;
  16347. #endif
  16348. (void)inLen;
  16349. (void)rng;
  16350. return ret;
  16351. }
  16352. #endif /* WOLFSSL_HAVE_SP_RSA */
  16353. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16354. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  16355. {
  16356. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16357. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16358. #ifdef WOLFSSL_TEST_CERT
  16359. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16360. #endif
  16361. #else
  16362. RsaKey caKey[1];
  16363. #ifdef WOLFSSL_TEST_CERT
  16364. DecodedCert decode[1];
  16365. #endif
  16366. #endif
  16367. byte* der = NULL;
  16368. wc_test_ret_t ret;
  16369. Cert* myCert = NULL;
  16370. int certSz;
  16371. size_t bytes3;
  16372. word32 idx3 = 0;
  16373. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16374. XFILE file3;
  16375. #endif
  16376. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  16377. struct tm beforeTime;
  16378. struct tm afterTime;
  16379. #endif
  16380. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  16381. (void)keypub;
  16382. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16383. if (caKey == NULL)
  16384. ERROR_OUT(MEMORY_E, exit_rsa);
  16385. #ifdef WOLFSSL_TEST_CERT
  16386. if (decode == NULL)
  16387. ERROR_OUT(MEMORY_E, exit_rsa);
  16388. #endif
  16389. #endif
  16390. XMEMSET(caKey, 0, sizeof *caKey);
  16391. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16392. if (der == NULL) {
  16393. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16394. }
  16395. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16396. if (myCert == NULL) {
  16397. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16398. }
  16399. /* self signed */
  16400. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  16401. if (ret != 0) {
  16402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16403. }
  16404. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  16405. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  16406. myCert->serialSz = (int)sizeof(mySerial);
  16407. myCert->isCA = 1;
  16408. #ifndef NO_SHA256
  16409. myCert->sigType = CTC_SHA256wRSA;
  16410. #else
  16411. myCert->sigType = CTC_SHAwRSA;
  16412. #endif
  16413. #ifdef WOLFSSL_CERT_EXT
  16414. /* add Policies */
  16415. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  16416. CTC_MAX_CERTPOL_SZ);
  16417. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  16418. CTC_MAX_CERTPOL_SZ);
  16419. myCert->certPoliciesNb = 2;
  16420. /* add SKID from the Public Key */
  16421. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  16422. if (ret != 0) {
  16423. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16424. }
  16425. /* add AKID from the Public Key */
  16426. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  16427. if (ret != 0) {
  16428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16429. }
  16430. /* add Key Usage */
  16431. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  16432. if (ret != 0) {
  16433. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16434. }
  16435. #ifdef WOLFSSL_EKU_OID
  16436. {
  16437. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  16438. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  16439. HEAP_HINT);
  16440. if (ret != 0) {
  16441. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16442. }
  16443. }
  16444. #endif /* WOLFSSL_EKU_OID */
  16445. #endif /* WOLFSSL_CERT_EXT */
  16446. do {
  16447. #if defined(WOLFSSL_ASYNC_CRYPT)
  16448. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16449. #endif
  16450. if (ret >= 0) {
  16451. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  16452. }
  16453. } while (ret == WC_PENDING_E);
  16454. if (ret < 0) {
  16455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16456. }
  16457. certSz = (word32)ret;
  16458. #ifdef WOLFSSL_TEST_CERT
  16459. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  16460. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  16461. if (ret != 0) {
  16462. FreeDecodedCert(decode);
  16463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16464. }
  16465. FreeDecodedCert(decode);
  16466. #endif
  16467. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  16468. CERT_TYPE);
  16469. if (ret != 0) {
  16470. goto exit_rsa;
  16471. }
  16472. /* Setup Certificate */
  16473. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  16474. if (ret < 0) {
  16475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16476. }
  16477. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  16478. /* Get CA Cert for testing */
  16479. #ifdef USE_CERT_BUFFERS_1024
  16480. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  16481. bytes3 = sizeof_ca_cert_der_1024;
  16482. #elif defined(USE_CERT_BUFFERS_2048)
  16483. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  16484. bytes3 = sizeof_ca_cert_der_2048;
  16485. #else
  16486. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  16487. if (!file3) {
  16488. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16489. }
  16490. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16491. XFCLOSE(file3);
  16492. if (bytes3 == 0)
  16493. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16494. #endif /* USE_CERT_BUFFERS */
  16495. #if defined(WOLFSSL_ALT_NAMES)
  16496. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  16497. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  16498. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  16499. if (ret != 0)
  16500. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16501. #endif
  16502. /* get alt names from der */
  16503. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  16504. if (ret != 0)
  16505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16506. /* get dates from der */
  16507. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  16508. if (ret != 0)
  16509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16510. #ifndef NO_ASN_TIME
  16511. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  16512. if (ret < 0)
  16513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16514. #endif
  16515. #endif /* WOLFSSL_ALT_NAMES */
  16516. #endif /* WOLFSSL_ALT_NAMES || HAVE_PKCS7 */
  16517. /* Get CA Key */
  16518. #ifdef USE_CERT_BUFFERS_1024
  16519. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  16520. bytes3 = sizeof_ca_key_der_1024;
  16521. #elif defined(USE_CERT_BUFFERS_2048)
  16522. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  16523. bytes3 = sizeof_ca_key_der_2048;
  16524. #else
  16525. file3 = XFOPEN(rsaCaKeyFile, "rb");
  16526. if (!file3) {
  16527. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16528. }
  16529. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16530. XFCLOSE(file3);
  16531. if (bytes3 == 0)
  16532. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16533. #endif /* USE_CERT_BUFFERS */
  16534. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  16535. if (ret != 0)
  16536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16537. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  16538. if (ret != 0)
  16539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16540. #ifndef NO_SHA256
  16541. myCert->sigType = CTC_SHA256wRSA;
  16542. #else
  16543. myCert->sigType = CTC_SHAwRSA;
  16544. #endif
  16545. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  16546. #ifdef WOLFSSL_CERT_EXT
  16547. /* add Policies */
  16548. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  16549. CTC_MAX_CERTPOL_SZ);
  16550. myCert->certPoliciesNb =1;
  16551. /* add SKID from the Public Key */
  16552. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  16553. if (ret != 0)
  16554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16555. /* add AKID from the CA certificate */
  16556. #if defined(USE_CERT_BUFFERS_2048)
  16557. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  16558. sizeof_ca_cert_der_2048);
  16559. #elif defined(USE_CERT_BUFFERS_1024)
  16560. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  16561. sizeof_ca_cert_der_1024);
  16562. #else
  16563. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  16564. #endif
  16565. if (ret != 0)
  16566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16567. /* add Key Usage */
  16568. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  16569. if (ret != 0)
  16570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16571. #endif /* WOLFSSL_CERT_EXT */
  16572. #if defined(USE_CERT_BUFFERS_2048)
  16573. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  16574. sizeof_ca_cert_der_2048);
  16575. #elif defined(USE_CERT_BUFFERS_1024)
  16576. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  16577. sizeof_ca_cert_der_1024);
  16578. #else
  16579. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  16580. #endif
  16581. if (ret < 0)
  16582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16583. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  16584. if (certSz < 0) {
  16585. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  16586. }
  16587. ret = 0;
  16588. do {
  16589. #if defined(WOLFSSL_ASYNC_CRYPT)
  16590. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16591. #endif
  16592. if (ret >= 0) {
  16593. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  16594. caKey, NULL, rng);
  16595. }
  16596. } while (ret == WC_PENDING_E);
  16597. if (ret < 0)
  16598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16599. certSz = (word32)ret;
  16600. #ifdef WOLFSSL_TEST_CERT
  16601. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  16602. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  16603. if (ret != 0) {
  16604. FreeDecodedCert(decode);
  16605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16606. }
  16607. FreeDecodedCert(decode);
  16608. #endif
  16609. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  16610. CERT_TYPE);
  16611. if (ret != 0) {
  16612. goto exit_rsa;
  16613. }
  16614. exit_rsa:
  16615. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16616. if (caKey != NULL) {
  16617. wc_FreeRsaKey(caKey);
  16618. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16619. }
  16620. #ifdef WOLFSSL_TEST_CERT
  16621. if (decode != NULL)
  16622. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16623. #endif
  16624. #else
  16625. wc_FreeRsaKey(caKey);
  16626. #endif
  16627. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16628. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16629. return ret;
  16630. }
  16631. #endif
  16632. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  16633. defined(WOLFSSL_CERT_GEN)
  16634. /* Make Cert / Sign example for ECC cert and RSA CA */
  16635. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  16636. {
  16637. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16638. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16639. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16640. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16641. #ifdef WOLFSSL_TEST_CERT
  16642. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16643. #endif
  16644. #else
  16645. RsaKey caKey[1];
  16646. ecc_key caEccKey[1];
  16647. ecc_key caEccKeyPub[1];
  16648. #ifdef WOLFSSL_TEST_CERT
  16649. DecodedCert decode[1];
  16650. #endif
  16651. #endif
  16652. byte* der = NULL;
  16653. Cert* myCert = NULL;
  16654. int certSz;
  16655. size_t bytes3;
  16656. word32 idx3 = 0;
  16657. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  16658. || !defined(USE_CERT_BUFFERS_256)
  16659. #ifndef NO_FILESYSTEM
  16660. XFILE file3;
  16661. #endif
  16662. #endif
  16663. wc_test_ret_t ret;
  16664. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16665. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  16666. #ifdef WOLFSSL_TEST_CERT
  16667. || (decode == NULL)
  16668. #endif
  16669. )
  16670. ERROR_OUT(MEMORY_E, exit_rsa);
  16671. #endif
  16672. XMEMSET(caKey, 0, sizeof *caKey);
  16673. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  16674. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  16675. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16676. if (der == NULL) {
  16677. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16678. }
  16679. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16680. if (myCert == NULL) {
  16681. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16682. }
  16683. /* Get CA Key */
  16684. #ifdef USE_CERT_BUFFERS_1024
  16685. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  16686. bytes3 = sizeof_ca_key_der_1024;
  16687. #elif defined(USE_CERT_BUFFERS_2048)
  16688. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  16689. bytes3 = sizeof_ca_key_der_2048;
  16690. #else
  16691. file3 = XFOPEN(rsaCaKeyFile, "rb");
  16692. if (!file3) {
  16693. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16694. }
  16695. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16696. XFCLOSE(file3);
  16697. if (bytes3 == 0)
  16698. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16699. #endif /* USE_CERT_BUFFERS */
  16700. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  16701. if (ret != 0)
  16702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16703. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  16704. if (ret != 0)
  16705. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16706. /* Get Cert Key */
  16707. #if defined(USE_CERT_BUFFERS_256)
  16708. {
  16709. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  16710. bytes3 = sizeof_ecc_key_pub_der_256;
  16711. }
  16712. #elif !defined(NO_FILESYSTEM)
  16713. {
  16714. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  16715. if (!file3) {
  16716. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16717. }
  16718. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  16719. XFCLOSE(file3);
  16720. if (bytes3 == 0)
  16721. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16722. }
  16723. #else
  16724. {
  16725. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
  16726. ERROR_OUT(ASN_PARSE_E, exit_rsa);
  16727. }
  16728. #endif
  16729. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  16730. if (ret != 0)
  16731. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16732. idx3 = 0;
  16733. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  16734. if (ret != 0)
  16735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16736. /* Setup Certificate */
  16737. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  16738. if (ret != 0)
  16739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16740. #ifndef NO_SHA256
  16741. myCert->sigType = CTC_SHA256wRSA;
  16742. #else
  16743. myCert->sigType = CTC_SHAwRSA;
  16744. #endif
  16745. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  16746. #ifdef WOLFSSL_CERT_EXT
  16747. /* add Policies */
  16748. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  16749. CTC_MAX_CERTPOL_SZ);
  16750. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  16751. CTC_MAX_CERTPOL_SZ);
  16752. myCert->certPoliciesNb = 2;
  16753. /* add SKID from the Public Key */
  16754. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  16755. if (ret != 0)
  16756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16757. /* add AKID from the CA certificate */
  16758. #if defined(USE_CERT_BUFFERS_2048)
  16759. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  16760. sizeof_ca_cert_der_2048);
  16761. #elif defined(USE_CERT_BUFFERS_1024)
  16762. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  16763. sizeof_ca_cert_der_1024);
  16764. #else
  16765. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  16766. #endif
  16767. if (ret != 0)
  16768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16769. /* add Key Usage */
  16770. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  16771. if (ret != 0)
  16772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16773. #endif /* WOLFSSL_CERT_EXT */
  16774. #if defined(USE_CERT_BUFFERS_2048)
  16775. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  16776. sizeof_ca_cert_der_2048);
  16777. #elif defined(USE_CERT_BUFFERS_1024)
  16778. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  16779. sizeof_ca_cert_der_1024);
  16780. #else
  16781. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  16782. #endif
  16783. if (ret < 0)
  16784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16785. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  16786. if (certSz < 0) {
  16787. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  16788. }
  16789. ret = 0;
  16790. do {
  16791. #if defined(WOLFSSL_ASYNC_CRYPT)
  16792. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16793. #endif
  16794. if (ret >= 0) {
  16795. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  16796. FOURK_BUF, caKey, NULL, rng);
  16797. }
  16798. } while (ret == WC_PENDING_E);
  16799. if (ret < 0)
  16800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16801. certSz = (word32)ret;
  16802. #ifdef WOLFSSL_TEST_CERT
  16803. InitDecodedCert(decode, der, certSz, 0);
  16804. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  16805. if (ret != 0) {
  16806. FreeDecodedCert(decode);
  16807. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16808. }
  16809. FreeDecodedCert(decode);
  16810. #endif
  16811. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  16812. CERT_TYPE);
  16813. if (ret != 0) {
  16814. goto exit_rsa;
  16815. }
  16816. exit_rsa:
  16817. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16818. if (caKey != NULL) {
  16819. wc_FreeRsaKey(caKey);
  16820. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16821. }
  16822. if (caEccKey != NULL) {
  16823. wc_ecc_free(caEccKey);
  16824. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16825. }
  16826. if (caEccKeyPub != NULL) {
  16827. wc_ecc_free(caEccKeyPub);
  16828. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16829. }
  16830. #ifdef WOLFSSL_TEST_CERT
  16831. if (decode != NULL)
  16832. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16833. #endif
  16834. #else
  16835. wc_FreeRsaKey(caKey);
  16836. wc_ecc_free(caEccKey);
  16837. wc_ecc_free(caEccKeyPub);
  16838. #endif
  16839. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16840. myCert = NULL;
  16841. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16842. der = NULL;
  16843. if (ret >= 0)
  16844. ret = 0;
  16845. return ret;
  16846. }
  16847. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  16848. #ifdef WOLFSSL_KEY_GEN
  16849. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  16850. {
  16851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16852. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16853. #else
  16854. RsaKey genKey[1];
  16855. #endif
  16856. wc_test_ret_t ret;
  16857. #ifndef WOLFSSL_NO_MALLOC
  16858. byte* der = NULL;
  16859. #else
  16860. byte der[1024];
  16861. #endif
  16862. #ifndef WOLFSSL_CRYPTOCELL
  16863. word32 idx = 0;
  16864. #endif
  16865. int derSz = 0;
  16866. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  16867. int keySz = 1024;
  16868. #else
  16869. int keySz = 2048;
  16870. #endif
  16871. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16872. if (! genKey)
  16873. ERROR_OUT(MEMORY_E, exit_rsa);
  16874. #endif
  16875. XMEMSET(genKey, 0, sizeof *genKey);
  16876. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  16877. if (ret != 0)
  16878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16879. #ifdef HAVE_FIPS
  16880. for (;;) {
  16881. #endif
  16882. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  16883. #if defined(WOLFSSL_ASYNC_CRYPT)
  16884. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16885. #endif
  16886. #ifdef HAVE_FIPS
  16887. if (ret == PRIME_GEN_E)
  16888. continue;
  16889. break;
  16890. }
  16891. #endif
  16892. if (ret != 0)
  16893. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16894. TEST_SLEEP();
  16895. #ifdef WOLFSSL_RSA_KEY_CHECK
  16896. ret = wc_CheckRsaKey(genKey);
  16897. if (ret != 0)
  16898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16899. #endif
  16900. #ifndef WOLFSSL_NO_MALLOC
  16901. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16902. if (der == NULL) {
  16903. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16904. }
  16905. #endif
  16906. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  16907. if (derSz < 0) {
  16908. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16909. }
  16910. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16911. PRIVATEKEY_TYPE);
  16912. if (ret != 0) {
  16913. goto exit_rsa;
  16914. }
  16915. wc_FreeRsaKey(genKey);
  16916. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  16917. if (ret != 0)
  16918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16919. #ifndef WOLFSSL_CRYPTOCELL
  16920. idx = 0;
  16921. /* The private key part of the key gen pairs from cryptocell can't be exported */
  16922. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  16923. if (ret != 0)
  16924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16925. #endif /* WOLFSSL_CRYPTOCELL */
  16926. exit_rsa:
  16927. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16928. if (genKey) {
  16929. wc_FreeRsaKey(genKey);
  16930. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16931. }
  16932. #else
  16933. wc_FreeRsaKey(genKey);
  16934. #endif
  16935. #ifndef WOLFSSL_NO_MALLOC
  16936. if (der != NULL) {
  16937. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16938. der = NULL;
  16939. }
  16940. #endif
  16941. return ret;
  16942. }
  16943. #endif
  16944. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16945. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  16946. (!defined(HAVE_FIPS) || \
  16947. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  16948. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16949. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  16950. {
  16951. wc_test_ret_t ret = 0;
  16952. word32 idx = 0;
  16953. const char inStr[] = TEST_STRING;
  16954. const word32 inLen = (word32)TEST_STRING_SZ;
  16955. const word32 outSz = RSA_TEST_BYTES;
  16956. const word32 plainSz = RSA_TEST_BYTES;
  16957. byte* res = NULL;
  16958. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  16959. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16960. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16961. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  16962. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  16963. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  16964. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16965. if (in == NULL || out == NULL || plain == NULL)
  16966. ERROR_OUT(MEMORY_E, exit_rsa);
  16967. #endif
  16968. XMEMCPY(in, inStr, inLen);
  16969. #ifndef NO_SHA
  16970. do {
  16971. #if defined(WOLFSSL_ASYNC_CRYPT)
  16972. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16973. #endif
  16974. if (ret >= 0) {
  16975. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  16976. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16977. }
  16978. } while (ret == WC_PENDING_E);
  16979. if (ret < 0)
  16980. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16981. TEST_SLEEP();
  16982. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16983. idx = (word32)ret;
  16984. do {
  16985. #if defined(WOLFSSL_ASYNC_CRYPT)
  16986. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16987. #endif
  16988. if (ret >= 0) {
  16989. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  16990. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16991. }
  16992. } while (ret == WC_PENDING_E);
  16993. if (ret < 0)
  16994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16995. if (XMEMCMP(plain, in, inLen)) {
  16996. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16997. }
  16998. TEST_SLEEP();
  16999. #endif /* NO_SHA */
  17000. #endif
  17001. #ifndef NO_SHA256
  17002. XMEMSET(plain, 0, plainSz);
  17003. do {
  17004. #if defined(WOLFSSL_ASYNC_CRYPT)
  17005. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17006. #endif
  17007. if (ret >= 0) {
  17008. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17009. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17010. }
  17011. } while (ret == WC_PENDING_E);
  17012. if (ret < 0)
  17013. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17014. TEST_SLEEP();
  17015. idx = (word32)ret;
  17016. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17017. do {
  17018. #if defined(WOLFSSL_ASYNC_CRYPT)
  17019. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17020. #endif
  17021. if (ret >= 0) {
  17022. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17023. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17024. }
  17025. } while (ret == WC_PENDING_E);
  17026. if (ret < 0)
  17027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17028. if (XMEMCMP(plain, in, inLen)) {
  17029. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17030. }
  17031. TEST_SLEEP();
  17032. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17033. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17034. do {
  17035. #if defined(WOLFSSL_ASYNC_CRYPT)
  17036. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17037. #endif
  17038. if (ret >= 0) {
  17039. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  17040. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17041. }
  17042. } while (ret == WC_PENDING_E);
  17043. if (ret < 0)
  17044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17045. if (ret != (int)inLen) {
  17046. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17047. }
  17048. if (XMEMCMP(res, in, inLen)) {
  17049. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17050. }
  17051. TEST_SLEEP();
  17052. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17053. /* check fails if not using the same optional label */
  17054. XMEMSET(plain, 0, plainSz);
  17055. do {
  17056. #if defined(WOLFSSL_ASYNC_CRYPT)
  17057. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17058. #endif
  17059. if (ret >= 0) {
  17060. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17061. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17062. }
  17063. } while (ret == WC_PENDING_E);
  17064. if (ret < 0)
  17065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17066. TEST_SLEEP();
  17067. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  17068. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17069. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  17070. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  17071. * due to label */
  17072. idx = (word32)ret;
  17073. do {
  17074. #if defined(WOLFSSL_ASYNC_CRYPT)
  17075. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17076. #endif
  17077. if (ret >= 0) {
  17078. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17079. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  17080. }
  17081. } while (ret == WC_PENDING_E);
  17082. if (ret > 0) { /* in this case decrypt should fail */
  17083. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17084. }
  17085. ret = 0;
  17086. TEST_SLEEP();
  17087. #endif /* !HAVE_CAVIUM */
  17088. /* check using optional label with encrypt/decrypt */
  17089. XMEMSET(plain, 0, plainSz);
  17090. do {
  17091. #if defined(WOLFSSL_ASYNC_CRYPT)
  17092. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17093. #endif
  17094. if (ret >= 0) {
  17095. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17096. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  17097. }
  17098. } while (ret == WC_PENDING_E);
  17099. if (ret < 0)
  17100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17101. TEST_SLEEP();
  17102. idx = (word32)ret;
  17103. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17104. do {
  17105. #if defined(WOLFSSL_ASYNC_CRYPT)
  17106. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17107. #endif
  17108. if (ret >= 0) {
  17109. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17110. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  17111. }
  17112. } while (ret == WC_PENDING_E);
  17113. if (ret < 0)
  17114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17115. if (XMEMCMP(plain, in, inLen)) {
  17116. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17117. }
  17118. TEST_SLEEP();
  17119. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17120. #ifndef NO_SHA
  17121. /* check fail using mismatch hash algorithms */
  17122. XMEMSET(plain, 0, plainSz);
  17123. do {
  17124. #if defined(WOLFSSL_ASYNC_CRYPT)
  17125. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17126. #endif
  17127. if (ret >= 0) {
  17128. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17129. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  17130. }
  17131. } while (ret == WC_PENDING_E);
  17132. if (ret < 0)
  17133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17134. TEST_SLEEP();
  17135. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  17136. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17137. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  17138. idx = (word32)ret;
  17139. do {
  17140. #if defined(WOLFSSL_ASYNC_CRYPT)
  17141. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17142. #endif
  17143. if (ret >= 0) {
  17144. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17145. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  17146. in, inLen);
  17147. }
  17148. } while (ret == WC_PENDING_E);
  17149. if (ret > 0) { /* should fail */
  17150. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17151. }
  17152. ret = 0;
  17153. TEST_SLEEP();
  17154. #endif /* !HAVE_CAVIUM */
  17155. #endif /* NO_SHA */
  17156. #endif /* NO_SHA256 */
  17157. #ifdef WOLFSSL_SHA512
  17158. /* Check valid RSA key size is used while using hash length of SHA512
  17159. If key size is less than (hash length * 2) + 2 then is invalid use
  17160. and test, since OAEP padding requires this.
  17161. BAD_FUNC_ARG is returned when this case is not met */
  17162. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  17163. XMEMSET(plain, 0, plainSz);
  17164. do {
  17165. #if defined(WOLFSSL_ASYNC_CRYPT)
  17166. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17167. #endif
  17168. if (ret >= 0) {
  17169. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17170. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  17171. }
  17172. } while (ret == WC_PENDING_E);
  17173. if (ret < 0)
  17174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17175. TEST_SLEEP();
  17176. idx = (word32)ret;
  17177. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17178. do {
  17179. #if defined(WOLFSSL_ASYNC_CRYPT)
  17180. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17181. #endif
  17182. if (ret >= 0) {
  17183. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17184. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  17185. }
  17186. } while (ret == WC_PENDING_E);
  17187. if (ret < 0)
  17188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17189. if (XMEMCMP(plain, in, inLen)) {
  17190. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17191. }
  17192. TEST_SLEEP();
  17193. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17194. }
  17195. #endif /* WOLFSSL_SHA512 */
  17196. /* check using pkcsv15 padding with _ex API */
  17197. XMEMSET(plain, 0, plainSz);
  17198. do {
  17199. #if defined(WOLFSSL_ASYNC_CRYPT)
  17200. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17201. #endif
  17202. if (ret >= 0) {
  17203. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  17204. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  17205. }
  17206. } while (ret == WC_PENDING_E);
  17207. if (ret < 0)
  17208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17209. TEST_SLEEP();
  17210. idx = (word32)ret;
  17211. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17212. do {
  17213. #if defined(WOLFSSL_ASYNC_CRYPT)
  17214. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17215. #endif
  17216. if (ret >= 0) {
  17217. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  17218. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  17219. }
  17220. } while (ret == WC_PENDING_E);
  17221. if (ret < 0)
  17222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17223. if (XMEMCMP(plain, in, inLen)) {
  17224. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17225. }
  17226. TEST_SLEEP();
  17227. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17228. exit_rsa:
  17229. WC_FREE_VAR(in, HEAP_HINT);
  17230. WC_FREE_VAR(out, HEAP_HINT);
  17231. WC_FREE_VAR(plain, HEAP_HINT);
  17232. (void)idx;
  17233. (void)inStr;
  17234. (void)res;
  17235. if (ret >= 0)
  17236. ret = 0;
  17237. return ret;
  17238. }
  17239. #endif
  17240. #endif
  17241. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  17242. {
  17243. wc_test_ret_t ret;
  17244. size_t bytes;
  17245. WC_RNG rng;
  17246. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17247. byte* tmp = NULL;
  17248. byte* der = NULL;
  17249. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17250. #else
  17251. RsaKey key[1];
  17252. byte tmp[FOURK_BUF];
  17253. #endif
  17254. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17255. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17256. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17257. #else
  17258. RsaKey keypub[1];
  17259. #endif
  17260. #endif
  17261. word32 idx = 0;
  17262. const char inStr[] = TEST_STRING;
  17263. const word32 inLen = (word32)TEST_STRING_SZ;
  17264. const word32 outSz = RSA_TEST_BYTES;
  17265. const word32 plainSz = RSA_TEST_BYTES;
  17266. byte* res = NULL;
  17267. #ifndef NO_SIG_WRAPPER
  17268. int modLen;
  17269. #endif
  17270. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17271. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  17272. !defined(NO_FILESYSTEM)
  17273. XFILE file;
  17274. #ifdef WOLFSSL_TEST_CERT
  17275. XFILE file2;
  17276. #endif
  17277. #endif
  17278. #ifdef WOLFSSL_TEST_CERT
  17279. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17280. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17281. #else
  17282. DecodedCert cert[1];
  17283. #endif
  17284. #ifndef NO_ASN_TIME
  17285. struct tm timearg;
  17286. const byte* date;
  17287. byte dateFormat;
  17288. int dateLength;
  17289. #endif
  17290. #endif
  17291. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  17292. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17293. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17294. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  17295. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17296. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17297. WOLFSSL_ENTER("rsa_test");
  17298. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17299. if (in == NULL || out == NULL || plain == NULL)
  17300. ERROR_OUT(MEMORY_E, exit_rsa);
  17301. #endif
  17302. XMEMCPY(in, inStr, inLen);
  17303. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17304. if (key == NULL)
  17305. ERROR_OUT(MEMORY_E, exit_rsa);
  17306. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17307. if (keypub == NULL)
  17308. ERROR_OUT(MEMORY_E, exit_rsa);
  17309. #endif
  17310. #ifdef WOLFSSL_TEST_CERT
  17311. if (cert == NULL)
  17312. ERROR_OUT(MEMORY_E, exit_rsa);
  17313. #endif
  17314. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17315. /* initialize stack structures */
  17316. XMEMSET(&rng, 0, sizeof(rng));
  17317. XMEMSET(key, 0, sizeof *key);
  17318. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17319. XMEMSET(keypub, 0, sizeof *keypub);
  17320. #endif
  17321. #if !defined(NO_ASN)
  17322. ret = rsa_decode_test(key);
  17323. if (ret != 0)
  17324. ERROR_OUT(ret, exit_rsa);
  17325. #endif
  17326. #ifdef USE_CERT_BUFFERS_1024
  17327. bytes = (size_t)sizeof_client_key_der_1024;
  17328. if (bytes < (size_t)sizeof_client_cert_der_1024)
  17329. bytes = (size_t)sizeof_client_cert_der_1024;
  17330. #elif defined(USE_CERT_BUFFERS_2048)
  17331. bytes = (size_t)sizeof_client_key_der_2048;
  17332. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17333. bytes = (size_t)sizeof_client_cert_der_2048;
  17334. #elif defined(USE_CERT_BUFFERS_3072)
  17335. bytes = (size_t)sizeof_client_key_der_3072;
  17336. if (bytes < (size_t)sizeof_client_cert_der_3072)
  17337. bytes = (size_t)sizeof_client_cert_der_3072;
  17338. #elif defined(USE_CERT_BUFFERS_4096)
  17339. bytes = (size_t)sizeof_client_key_der_4096;
  17340. if (bytes < (size_t)sizeof_client_cert_der_4096)
  17341. bytes = (size_t)sizeof_client_cert_der_4096;
  17342. #else
  17343. bytes = FOURK_BUF;
  17344. #endif
  17345. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17346. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17347. if (tmp == NULL)
  17348. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17349. #endif
  17350. #ifdef USE_CERT_BUFFERS_1024
  17351. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  17352. #elif defined(USE_CERT_BUFFERS_2048)
  17353. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17354. #elif defined(USE_CERT_BUFFERS_3072)
  17355. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17356. #elif defined(USE_CERT_BUFFERS_4096)
  17357. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17358. #elif !defined(NO_FILESYSTEM)
  17359. file = XFOPEN(clientKey, "rb");
  17360. if (!file) {
  17361. err_sys("can't open ./certs/client-key.der, "
  17362. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17363. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17364. }
  17365. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17366. XFCLOSE(file);
  17367. if (bytes == 0)
  17368. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17369. #else
  17370. /* No key to use. */
  17371. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17372. #endif /* USE_CERT_BUFFERS */
  17373. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  17374. if (ret != 0)
  17375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17376. #ifndef NO_ASN
  17377. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17378. if (ret != 0)
  17379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17380. #ifndef NO_SIG_WRAPPER
  17381. modLen = wc_RsaEncryptSize(key);
  17382. #endif
  17383. #else
  17384. #ifdef USE_CERT_BUFFERS_2048
  17385. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  17386. if (ret != 0)
  17387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17388. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  17389. if (ret != 0)
  17390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17391. #ifndef NO_SIG_WRAPPER
  17392. modLen = 2048;
  17393. #endif
  17394. #else
  17395. #error Not supported yet!
  17396. #endif
  17397. #endif
  17398. #ifndef WC_NO_RNG
  17399. #ifndef HAVE_FIPS
  17400. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17401. #else
  17402. ret = wc_InitRng(&rng);
  17403. #endif
  17404. if (ret != 0)
  17405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17406. #endif
  17407. #ifndef NO_SIG_WRAPPER
  17408. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  17409. if (ret != 0)
  17410. goto exit_rsa;
  17411. #endif
  17412. #ifdef WC_RSA_NONBLOCK
  17413. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  17414. if (ret != 0)
  17415. goto exit_rsa;
  17416. #endif
  17417. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17418. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17419. do {
  17420. #if defined(WOLFSSL_ASYNC_CRYPT)
  17421. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17422. #endif
  17423. if (ret >= 0) {
  17424. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  17425. }
  17426. } while (ret == WC_PENDING_E);
  17427. if (ret < 0)
  17428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17429. TEST_SLEEP();
  17430. #ifdef WC_RSA_BLINDING
  17431. {
  17432. wc_test_ret_t tmpret = ret;
  17433. ret = wc_RsaSetRNG(key, &rng);
  17434. if (ret < 0)
  17435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17436. ret = tmpret;
  17437. }
  17438. #endif
  17439. idx = (word32)ret; /* save off encrypted length */
  17440. do {
  17441. #if defined(WOLFSSL_ASYNC_CRYPT)
  17442. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17443. #endif
  17444. if (ret >= 0) {
  17445. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  17446. }
  17447. } while (ret == WC_PENDING_E);
  17448. if (ret < 0)
  17449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17450. if (XMEMCMP(plain, in, inLen)) {
  17451. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17452. }
  17453. TEST_SLEEP();
  17454. do {
  17455. #if defined(WOLFSSL_ASYNC_CRYPT)
  17456. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17457. #endif
  17458. if (ret >= 0) {
  17459. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  17460. }
  17461. } while (ret == WC_PENDING_E);
  17462. if (ret < 0)
  17463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17464. if (ret != (int)inLen) {
  17465. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17466. }
  17467. if (XMEMCMP(res, in, inLen)) {
  17468. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17469. }
  17470. TEST_SLEEP();
  17471. do {
  17472. #if defined(WOLFSSL_ASYNC_CRYPT)
  17473. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17474. #endif
  17475. if (ret >= 0) {
  17476. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  17477. }
  17478. } while (ret == WC_PENDING_E);
  17479. if (ret < 0)
  17480. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17481. TEST_SLEEP();
  17482. #elif defined(WOLFSSL_PUBLIC_MP)
  17483. {
  17484. static const byte signature_2048[] = {
  17485. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  17486. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  17487. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  17488. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  17489. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  17490. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  17491. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  17492. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  17493. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  17494. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  17495. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  17496. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  17497. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  17498. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  17499. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  17500. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  17501. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  17502. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  17503. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  17504. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  17505. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  17506. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  17507. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  17508. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  17509. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  17510. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  17511. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  17512. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  17513. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  17514. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  17515. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  17516. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  17517. };
  17518. ret = sizeof(signature_2048);
  17519. XMEMCPY(out, signature_2048, ret);
  17520. }
  17521. #endif
  17522. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  17523. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  17524. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17525. idx = (word32)ret;
  17526. XMEMSET(plain, 0, plainSz);
  17527. do {
  17528. #if defined(WOLFSSL_ASYNC_CRYPT)
  17529. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17530. #endif
  17531. if (ret >= 0) {
  17532. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  17533. #if defined(WOLFSSL_CRYPTOCELL)
  17534. /*
  17535. Cryptocell requires the input data and signature byte array to verify.
  17536. first argument must be the input data
  17537. second argument must be the length of input data
  17538. third argument must be the signature byte array or the output from
  17539. wc_RsaSSL_Sign()
  17540. fourth argument must be the length of the signature byte array
  17541. */
  17542. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  17543. #else
  17544. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  17545. #endif /* WOLFSSL_CRYPTOCELL */
  17546. #else
  17547. byte* dec = NULL;
  17548. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  17549. if (ret > 0) {
  17550. XMEMCPY(plain, dec, ret);
  17551. }
  17552. #endif
  17553. }
  17554. } while (ret == WC_PENDING_E);
  17555. if (ret < 0)
  17556. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17557. if (XMEMCMP(plain, in, (size_t)ret)) {
  17558. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17559. }
  17560. TEST_SLEEP();
  17561. #endif
  17562. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17563. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  17564. #if (!defined(HAVE_FIPS) || \
  17565. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  17566. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17567. ret = rsa_oaep_padding_test(key, &rng);
  17568. if (ret != 0)
  17569. return ret;
  17570. #endif /* !HAVE_FIPS */
  17571. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  17572. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17573. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  17574. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  17575. ret = rsa_export_key_test(key);
  17576. if (ret != 0)
  17577. return ret;
  17578. #endif
  17579. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17580. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  17581. ret = rsa_flatten_test(key);
  17582. if (ret != 0)
  17583. return ret;
  17584. #endif
  17585. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  17586. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17587. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  17588. (void)clientCert;
  17589. #endif
  17590. #ifdef WOLFSSL_TEST_CERT
  17591. #if defined(WOLFSSL_MDK_ARM)
  17592. #define sizeof(s) XSTRLEN((char *)(s))
  17593. #endif
  17594. #ifdef USE_CERT_BUFFERS_1024
  17595. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  17596. bytes = (size_t)sizeof_client_cert_der_1024;
  17597. #elif defined(USE_CERT_BUFFERS_2048)
  17598. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  17599. bytes = (size_t)sizeof_client_cert_der_2048;
  17600. #elif defined(USE_CERT_BUFFERS_3072)
  17601. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  17602. bytes = (size_t)sizeof_client_cert_der_3072;
  17603. #elif defined(USE_CERT_BUFFERS_4096)
  17604. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  17605. bytes = (size_t)sizeof_client_cert_der_4096;
  17606. #elif !defined(NO_FILESYSTEM)
  17607. file2 = XFOPEN(clientCert, "rb");
  17608. if (!file2) {
  17609. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17610. }
  17611. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  17612. XFCLOSE(file2);
  17613. if (bytes == 0)
  17614. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17615. #else
  17616. /* No certificate to use. */
  17617. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  17618. #endif
  17619. #ifdef sizeof
  17620. #undef sizeof
  17621. #endif
  17622. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  17623. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  17624. if (ret != 0) {
  17625. FreeDecodedCert(cert);
  17626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17627. }
  17628. #ifndef NO_ASN_TIME
  17629. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  17630. &dateFormat, &dateLength);
  17631. if (ret != 0) {
  17632. FreeDecodedCert(cert);
  17633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17634. }
  17635. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  17636. if (ret != 0) {
  17637. FreeDecodedCert(cert);
  17638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17639. }
  17640. #endif
  17641. FreeDecodedCert(cert);
  17642. #endif /* WOLFSSL_TEST_CERT */
  17643. #ifdef WOLFSSL_CERT_EXT
  17644. #ifdef USE_CERT_BUFFERS_1024
  17645. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17646. bytes = sizeof_client_keypub_der_1024;
  17647. #elif defined(USE_CERT_BUFFERS_2048)
  17648. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17649. bytes = sizeof_client_keypub_der_2048;
  17650. #elif defined(USE_CERT_BUFFERS_3072)
  17651. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  17652. bytes = sizeof_client_keypub_der_3072;
  17653. #elif defined(USE_CERT_BUFFERS_4096)
  17654. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  17655. bytes = sizeof_client_keypub_der_4096;
  17656. #else
  17657. file = XFOPEN(clientKeyPub, "rb");
  17658. if (!file) {
  17659. err_sys("can't open ./certs/client-keyPub.der, "
  17660. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17661. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17662. }
  17663. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17664. XFCLOSE(file);
  17665. if (bytes == 0)
  17666. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17667. #endif /* USE_CERT_BUFFERS */
  17668. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  17669. if (ret != 0)
  17670. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17671. idx = 0;
  17672. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  17673. if (ret != 0)
  17674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17675. #endif /* WOLFSSL_CERT_EXT */
  17676. #ifdef WOLFSSL_KEY_GEN
  17677. ret = rsa_keygen_test(&rng);
  17678. if (ret != 0)
  17679. goto exit_rsa;
  17680. #endif
  17681. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  17682. /* Make Cert / Sign example for RSA cert and RSA CA */
  17683. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  17684. if (ret != 0)
  17685. goto exit_rsa;
  17686. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  17687. ret = rsa_ecc_certgen_test(&rng, tmp);
  17688. if (ret != 0)
  17689. goto exit_rsa;
  17690. #endif
  17691. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  17692. {
  17693. Cert *req;
  17694. int derSz;
  17695. #ifndef WOLFSSL_SMALL_STACK
  17696. byte* der = NULL;
  17697. #endif
  17698. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17699. if (! req)
  17700. ERROR_OUT(MEMORY_E, exit_rsa);
  17701. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  17702. if (der == NULL) {
  17703. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  17704. }
  17705. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  17706. if (ret != 0)
  17707. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17708. req->version = 0;
  17709. req->isCA = 1;
  17710. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  17711. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  17712. #ifndef NO_SHA256
  17713. req->sigType = CTC_SHA256wRSA;
  17714. #else
  17715. req->sigType = CTC_SHAwRSA;
  17716. #endif
  17717. #ifdef WOLFSSL_CERT_EXT
  17718. /* add SKID from the Public Key */
  17719. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  17720. if (ret != 0)
  17721. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17722. /* add Key Usage */
  17723. ret = wc_SetKeyUsage(req, certKeyUsage2);
  17724. if (ret != 0)
  17725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17726. /* add Extended Key Usage */
  17727. ret = wc_SetExtKeyUsage(req,
  17728. "serverAuth,clientAuth,codeSigning,"
  17729. "emailProtection,timeStamping,OCSPSigning");
  17730. if (ret != 0)
  17731. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17732. #ifdef WOLFSSL_EKU_OID
  17733. {
  17734. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  17735. "2.16.840.1.111111.100.1.10.1";
  17736. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  17737. HEAP_HINT);
  17738. if (ret != 0)
  17739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17740. }
  17741. #endif /* WOLFSSL_EKU_OID */
  17742. #endif /* WOLFSSL_CERT_EXT */
  17743. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  17744. if (derSz < 0) {
  17745. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17746. }
  17747. #ifdef WOLFSSL_CERT_EXT
  17748. /* Try again with "any" flag set, will override all others */
  17749. ret = wc_SetExtKeyUsage(req, "any");
  17750. if (ret != 0)
  17751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17752. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  17753. if (derSz < 0) {
  17754. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17755. }
  17756. #endif /* WOLFSSL_CERT_EXT */
  17757. ret = 0;
  17758. do {
  17759. #if defined(WOLFSSL_ASYNC_CRYPT)
  17760. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17761. #endif
  17762. if (ret >= 0) {
  17763. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  17764. key, NULL, &rng);
  17765. }
  17766. } while (ret == WC_PENDING_E);
  17767. if (ret < 0)
  17768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  17769. derSz = (word32)ret;
  17770. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  17771. CERTREQ_TYPE);
  17772. if (ret != 0) {
  17773. goto exit_rsa;
  17774. }
  17775. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  17776. if (derSz < 0) {
  17777. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17778. }
  17779. /* Test getting the size of the buffer without providing the buffer.
  17780. * derSz is set to the "largest buffer" we are willing to allocate. */
  17781. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  17782. if (derSz < 0) {
  17783. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  17784. }
  17785. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17786. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17787. der = NULL;
  17788. }
  17789. #endif /* WOLFSSL_CERT_REQ */
  17790. #endif /* WOLFSSL_CERT_GEN */
  17791. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17792. /* Need to create known good signatures to test with this. */
  17793. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17794. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17795. ret = rsa_pss_test(&rng, key);
  17796. if (ret != 0)
  17797. goto exit_rsa;
  17798. #endif
  17799. #endif
  17800. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17801. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17802. /* New key to be loaded in rsa_even_mod_test(). */
  17803. if (key != NULL)
  17804. #endif
  17805. wc_FreeRsaKey(key);
  17806. /* New key to be loaded in rsa_even_mod_test(). */
  17807. ret = rsa_even_mod_test(&rng, key);
  17808. #endif
  17809. exit_rsa:
  17810. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17811. if (key != NULL) {
  17812. wc_FreeRsaKey(key);
  17813. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17814. }
  17815. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17816. if (keypub != NULL) {
  17817. wc_FreeRsaKey(keypub);
  17818. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17819. }
  17820. #endif
  17821. #ifdef WOLFSSL_TEST_CERT
  17822. if (cert != NULL)
  17823. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17824. #endif
  17825. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17826. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17827. #else
  17828. wc_FreeRsaKey(key);
  17829. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  17830. wc_FreeRsaKey(keypub);
  17831. #endif
  17832. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17833. wc_FreeRng(&rng);
  17834. WC_FREE_VAR(in, HEAP_HINT);
  17835. WC_FREE_VAR(out, HEAP_HINT);
  17836. WC_FREE_VAR(plain, HEAP_HINT);
  17837. (void)res;
  17838. (void)bytes;
  17839. (void)idx;
  17840. (void)in;
  17841. (void)out;
  17842. (void)plain;
  17843. (void)idx;
  17844. (void)inStr;
  17845. (void)inLen;
  17846. (void)outSz;
  17847. (void)plainSz;
  17848. /* ret can be greater then 0 with certgen but all negative values should
  17849. * be returned and treated as an error */
  17850. if (ret >= 0) {
  17851. return 0;
  17852. }
  17853. else {
  17854. return ret;
  17855. }
  17856. }
  17857. #endif /* !NO_RSA */
  17858. #ifndef NO_DH
  17859. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  17860. {
  17861. wc_test_ret_t ret = 0;
  17862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17863. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17864. #else
  17865. DhKey key[1];
  17866. #endif
  17867. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  17868. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  17869. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  17870. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  17871. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  17872. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  17873. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  17874. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  17875. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  17876. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  17877. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  17878. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  17879. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  17880. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  17881. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  17882. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  17883. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  17884. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  17885. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  17886. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  17887. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  17888. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  17889. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  17890. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  17891. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  17892. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  17893. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  17894. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  17895. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  17896. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  17897. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  17898. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  17899. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  17900. };
  17901. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  17902. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  17903. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  17904. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  17905. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  17906. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  17907. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  17908. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  17909. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  17910. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  17911. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  17912. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  17913. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  17914. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  17915. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  17916. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  17917. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  17918. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  17919. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  17920. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  17921. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  17922. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  17923. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  17924. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  17925. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  17926. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  17927. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  17928. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  17929. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  17930. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  17931. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  17932. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  17933. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  17934. };
  17935. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  17936. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  17937. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  17938. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  17939. 0x40, 0x52, 0xed, 0x41
  17940. };
  17941. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  17942. 0x00,
  17943. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  17944. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  17945. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  17946. 0x40, 0x52, 0xed, 0x41
  17947. };
  17948. byte priv[256];
  17949. byte pub[256];
  17950. word32 privSz = sizeof(priv);
  17951. word32 pubSz = sizeof(pub);
  17952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17953. if (key == NULL)
  17954. ERROR_OUT(MEMORY_E, exit_gen_test);
  17955. #endif
  17956. /* Parameter Validation testing. */
  17957. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  17958. if (ret != BAD_FUNC_ARG)
  17959. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17960. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  17961. if (ret != BAD_FUNC_ARG)
  17962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17963. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  17964. if (ret != BAD_FUNC_ARG)
  17965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17966. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  17967. if (ret != BAD_FUNC_ARG)
  17968. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17969. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  17970. if (ret != BAD_FUNC_ARG)
  17971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17972. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  17973. if (ret != BAD_FUNC_ARG)
  17974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17975. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17976. if (ret != 0)
  17977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17978. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  17979. if (ret != 0)
  17980. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17981. wc_FreeDhKey(key);
  17982. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17983. if (ret != 0)
  17984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17985. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  17986. if (ret != 0)
  17987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17988. /* Use API. */
  17989. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  17990. #if defined(WOLFSSL_ASYNC_CRYPT)
  17991. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17992. #endif
  17993. if (ret != 0)
  17994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17995. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  17996. if (ret != 0)
  17997. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  17998. wc_FreeDhKey(key);
  17999. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18000. if (ret != 0)
  18001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18002. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  18003. if (ret != 0)
  18004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18005. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  18006. if (ret != 0)
  18007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18008. #ifndef HAVE_SELFTEST
  18009. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  18010. if (ret != 0)
  18011. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18012. /* Taint the public key so the check fails. */
  18013. pub[0]++;
  18014. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  18015. if (ret != MP_CMP_E) {
  18016. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18017. }
  18018. #ifdef WOLFSSL_KEY_GEN
  18019. wc_FreeDhKey(key);
  18020. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18021. if (ret != 0)
  18022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18023. ret = wc_DhGenerateParams(rng, 2048, key);
  18024. if (ret != 0)
  18025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18026. privSz = sizeof(priv);
  18027. pubSz = sizeof(pub);
  18028. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  18029. #if defined(WOLFSSL_ASYNC_CRYPT)
  18030. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18031. #endif
  18032. if (ret != 0)
  18033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18034. #endif /* WOLFSSL_KEY_GEN */
  18035. #endif /* HAVE_SELFTEST */
  18036. ret = 0;
  18037. exit_gen_test:
  18038. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18039. if (key) {
  18040. wc_FreeDhKey(key);
  18041. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18042. }
  18043. #else
  18044. wc_FreeDhKey(key);
  18045. #endif
  18046. return ret;
  18047. }
  18048. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  18049. {
  18050. wc_test_ret_t ret = 0;
  18051. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18052. DhKey *smallKey = NULL;
  18053. #else
  18054. DhKey smallKey[1];
  18055. #endif
  18056. byte p[2] = { 1, 7 }; /* 263 in decimal */
  18057. byte g[2] = { 0, 2 };
  18058. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  18059. #ifdef WOLFSSL_DH_CONST
  18060. /* the table for constant DH lookup will round to the lowest byte size 21 */
  18061. byte priv[21];
  18062. byte pub[21];
  18063. #else
  18064. byte priv[2];
  18065. byte pub[2];
  18066. #endif
  18067. word32 privSz = sizeof(priv);
  18068. word32 pubSz = sizeof(pub);
  18069. #endif
  18070. int smallKey_inited = 0;
  18071. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18072. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  18073. return WC_TEST_RET_ENC_ERRNO;
  18074. #endif
  18075. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  18076. if (ret != 0)
  18077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18078. smallKey_inited = 1;
  18079. /* Parameter Validation testing. */
  18080. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  18081. if (ret != BAD_FUNC_ARG)
  18082. return WC_TEST_RET_ENC_EC(ret);
  18083. wc_FreeDhKey(NULL);
  18084. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  18085. if (ret != BAD_FUNC_ARG) {
  18086. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18087. }
  18088. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  18089. if (ret != BAD_FUNC_ARG) {
  18090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18091. }
  18092. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  18093. if (ret != BAD_FUNC_ARG) {
  18094. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18095. }
  18096. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  18097. if (ret != BAD_FUNC_ARG) {
  18098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18099. }
  18100. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  18101. if (ret != BAD_FUNC_ARG) {
  18102. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18103. }
  18104. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  18105. if (ret != 0)
  18106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18107. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  18108. /* Use API. */
  18109. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  18110. #if defined(WOLFSSL_ASYNC_CRYPT)
  18111. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18112. #endif
  18113. if (ret != 0) {
  18114. ret = WC_TEST_RET_ENC_EC(ret);
  18115. }
  18116. #else
  18117. (void)rng;
  18118. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  18119. ret = 0;
  18120. #endif
  18121. #endif
  18122. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  18123. {
  18124. byte priv[260];
  18125. byte pub[260];
  18126. word32 privSz = sizeof(priv);
  18127. word32 pubSz = sizeof(pub);
  18128. /* test odd ball param generation with DH */
  18129. wc_FreeDhKey(smallKey);
  18130. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  18131. if (ret != 0)
  18132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18133. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  18134. if (ret != 0)
  18135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18136. privSz = sizeof(priv);
  18137. pubSz = sizeof(pub);
  18138. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  18139. #if defined(WOLFSSL_ASYNC_CRYPT)
  18140. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18141. #endif
  18142. if (ret != 0)
  18143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  18144. }
  18145. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  18146. exit_gen_test:
  18147. if (smallKey_inited)
  18148. wc_FreeDhKey(smallKey);
  18149. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18150. if (smallKey != NULL)
  18151. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18152. #endif
  18153. return ret;
  18154. }
  18155. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18156. typedef struct dh_pubvalue_test {
  18157. const byte* data;
  18158. word32 len;
  18159. } dh_pubvalue_test;
  18160. static wc_test_ret_t dh_test_check_pubvalue(void)
  18161. {
  18162. wc_test_ret_t ret;
  18163. word32 i;
  18164. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  18165. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  18166. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  18167. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  18168. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  18169. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  18170. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  18171. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  18172. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  18173. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  18174. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  18175. const dh_pubvalue_test dh_pubval_fail[] = {
  18176. { prime, sizeof(prime) },
  18177. { pubValZero, sizeof(pubValZero) },
  18178. { pubValZeroLong, sizeof(pubValZeroLong) },
  18179. { pubValOne, sizeof(pubValOne) },
  18180. { pubValOneLong, sizeof(pubValOneLong) },
  18181. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  18182. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  18183. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  18184. { pubValTooBig0, sizeof(pubValTooBig0) },
  18185. { pubValTooBig1, sizeof(pubValTooBig1) },
  18186. { pubValTooLong, sizeof(pubValTooLong) },
  18187. };
  18188. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  18189. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  18190. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  18191. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  18192. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  18193. const dh_pubvalue_test dh_pubval_pass[] = {
  18194. { pubValTwo, sizeof(pubValTwo) },
  18195. { pubValTwoLong, sizeof(pubValTwoLong) },
  18196. { pubValGood, sizeof(pubValGood) },
  18197. { pubValGoodLen, sizeof(pubValGoodLen) },
  18198. { pubValGoodLong, sizeof(pubValGoodLong) },
  18199. };
  18200. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  18201. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  18202. dh_pubval_fail[i].len);
  18203. if (ret != MP_VAL)
  18204. return WC_TEST_RET_ENC_I(i);
  18205. }
  18206. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  18207. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  18208. dh_pubval_pass[i].len);
  18209. if (ret != 0)
  18210. return WC_TEST_RET_ENC_I(i);
  18211. }
  18212. return 0;
  18213. }
  18214. #endif
  18215. #if defined(HAVE_FFDHE)
  18216. #if defined(HAVE_FFDHE_4096)
  18217. #define MAX_DH_PRIV_SZ 39
  18218. #define MAX_DH_KEY_SZ 512
  18219. #elif defined(HAVE_FFDHE_3072)
  18220. #define MAX_DH_PRIV_SZ 34
  18221. #define MAX_DH_KEY_SZ 384
  18222. #else
  18223. #define MAX_DH_PRIV_SZ 29
  18224. #define MAX_DH_KEY_SZ 256
  18225. #endif
  18226. #ifndef WC_NO_RNG
  18227. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  18228. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  18229. #ifdef HAVE_PUBLIC_FFDHE
  18230. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  18231. #else
  18232. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  18233. #endif
  18234. {
  18235. wc_test_ret_t ret;
  18236. word32 privSz, pubSz, privSz2, pubSz2;
  18237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18238. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18239. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18240. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18241. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18242. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18243. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18244. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18245. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18246. #else
  18247. byte priv[MAX_DH_PRIV_SZ];
  18248. byte pub[MAX_DH_KEY_SZ];
  18249. byte priv2[MAX_DH_PRIV_SZ];
  18250. byte pub2[MAX_DH_KEY_SZ];
  18251. byte agree[MAX_DH_KEY_SZ];
  18252. byte agree2[MAX_DH_KEY_SZ];
  18253. DhKey key[1];
  18254. DhKey key2[1];
  18255. #endif
  18256. word32 agreeSz = MAX_DH_KEY_SZ;
  18257. word32 agreeSz2 = MAX_DH_KEY_SZ;
  18258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18259. if ((priv == NULL) ||
  18260. (pub == NULL) ||
  18261. (priv2 == NULL) ||
  18262. (pub2 == NULL) ||
  18263. (agree == NULL) ||
  18264. (agree2 == NULL) ||
  18265. (key == NULL) ||
  18266. (key2 == NULL))
  18267. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18268. #endif
  18269. pubSz = MAX_DH_KEY_SZ;
  18270. pubSz2 = MAX_DH_KEY_SZ;
  18271. #ifdef HAVE_PUBLIC_FFDHE
  18272. privSz = MAX_DH_PRIV_SZ;
  18273. privSz2 = MAX_DH_PRIV_SZ;
  18274. #else
  18275. privSz = wc_DhGetNamedKeyMinSize(name);
  18276. privSz2 = privSz;
  18277. #endif
  18278. XMEMSET(key, 0, sizeof(*key));
  18279. XMEMSET(key2, 0, sizeof(*key2));
  18280. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18281. if (ret != 0)
  18282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18283. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  18284. if (ret != 0)
  18285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18286. #ifdef HAVE_PUBLIC_FFDHE
  18287. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  18288. #else
  18289. ret = wc_DhSetNamedKey(key, name);
  18290. #endif
  18291. if (ret != 0)
  18292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18293. #ifdef HAVE_PUBLIC_FFDHE
  18294. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  18295. params->g_len);
  18296. #else
  18297. ret = wc_DhSetNamedKey(key2, name);
  18298. #endif
  18299. if (ret != 0)
  18300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18301. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  18302. #if defined(WOLFSSL_ASYNC_CRYPT)
  18303. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18304. #endif
  18305. if (ret != 0)
  18306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18307. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  18308. #if defined(WOLFSSL_ASYNC_CRYPT)
  18309. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18310. #endif
  18311. if (ret != 0)
  18312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18313. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  18314. #if defined(WOLFSSL_ASYNC_CRYPT)
  18315. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18316. #endif
  18317. if (ret != 0)
  18318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18319. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  18320. #if defined(WOLFSSL_ASYNC_CRYPT)
  18321. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18322. #endif
  18323. if (ret != 0)
  18324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18325. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  18326. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18327. }
  18328. #if (defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)) && \
  18329. !defined(HAVE_INTEL_QA)
  18330. /* Make p even */
  18331. key->p.dp[0] &= (mp_digit)-2;
  18332. if (ret != 0)
  18333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18334. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  18335. #if defined(WOLFSSL_ASYNC_CRYPT)
  18336. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18337. #endif
  18338. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  18339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18340. }
  18341. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  18342. #if defined(WOLFSSL_ASYNC_CRYPT)
  18343. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18344. #endif
  18345. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  18346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18347. }
  18348. #ifndef HAVE_SELFTEST
  18349. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  18350. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  18351. ret != ASYNC_OP_E) {
  18352. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18353. }
  18354. #endif
  18355. /* Getting here means success - set ret to 0. */
  18356. ret = 0;
  18357. #endif /* (SP DH or Fast Math) and not Intel QuickAssist */
  18358. done:
  18359. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  18360. !defined(WC_NO_RNG)
  18361. if (priv)
  18362. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18363. if (pub)
  18364. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18365. if (priv2)
  18366. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18367. if (pub2)
  18368. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18369. if (agree)
  18370. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18371. if (agree2)
  18372. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18373. if (key) {
  18374. wc_FreeDhKey(key);
  18375. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18376. }
  18377. if (key2) {
  18378. wc_FreeDhKey(key2);
  18379. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18380. }
  18381. #else
  18382. wc_FreeDhKey(key);
  18383. wc_FreeDhKey(key2);
  18384. #endif
  18385. return ret;
  18386. }
  18387. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  18388. #endif /* !WC_NO_RNG */
  18389. #endif /* HAVE_FFDHE */
  18390. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  18391. {
  18392. wc_test_ret_t ret;
  18393. word32 bytes;
  18394. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  18395. #ifndef WC_NO_RNG
  18396. WC_RNG rng;
  18397. int rngInit = 0;
  18398. #endif
  18399. int keyInit = 0, key2Init = 0;
  18400. #define DH_TEST_TMP_SIZE 1024
  18401. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  18402. #define DH_TEST_BUF_SIZE 256
  18403. #else
  18404. #define DH_TEST_BUF_SIZE 512
  18405. #endif
  18406. #ifndef WC_NO_RNG
  18407. word32 agreeSz = DH_TEST_BUF_SIZE;
  18408. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  18409. #endif
  18410. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18411. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18412. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18413. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18414. #else
  18415. DhKey key[1];
  18416. DhKey key2[1];
  18417. byte tmp[DH_TEST_TMP_SIZE];
  18418. #endif
  18419. #ifndef WC_NO_RNG
  18420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18421. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18422. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18423. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18424. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18425. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18426. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18427. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  18428. agree == NULL || agree2 == NULL) {
  18429. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18430. }
  18431. #else
  18432. byte priv[DH_TEST_BUF_SIZE];
  18433. byte pub[DH_TEST_BUF_SIZE];
  18434. byte priv2[DH_TEST_BUF_SIZE];
  18435. byte pub2[DH_TEST_BUF_SIZE];
  18436. byte agree[DH_TEST_BUF_SIZE];
  18437. byte agree2[DH_TEST_BUF_SIZE];
  18438. #endif
  18439. #endif /* !WC_NO_RNG */
  18440. WOLFSSL_ENTER("dh_test");
  18441. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18442. if (key == NULL || key2 == NULL || tmp == NULL) {
  18443. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18444. }
  18445. #endif
  18446. #ifdef USE_CERT_BUFFERS_1024
  18447. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  18448. bytes = (size_t)sizeof_dh_key_der_1024;
  18449. #elif defined(USE_CERT_BUFFERS_2048)
  18450. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  18451. bytes = (size_t)sizeof_dh_key_der_2048;
  18452. #elif defined(USE_CERT_BUFFERS_3072)
  18453. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  18454. bytes = (size_t)sizeof_dh_key_der_3072;
  18455. #elif defined(USE_CERT_BUFFERS_4096)
  18456. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  18457. bytes = (size_t)sizeof_dh_key_der_4096;
  18458. #elif defined(NO_ASN)
  18459. /* don't use file, no DER parsing */
  18460. #elif !defined(NO_FILESYSTEM)
  18461. {
  18462. XFILE file = XFOPEN(dhParamsFile, "rb");
  18463. if (! file)
  18464. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18465. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  18466. XFCLOSE(file);
  18467. if (bytes == 0)
  18468. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18469. }
  18470. #else
  18471. /* No DH key to use. */
  18472. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18473. #endif /* USE_CERT_BUFFERS */
  18474. (void)idx;
  18475. (void)tmp;
  18476. (void)bytes;
  18477. pubSz = DH_TEST_BUF_SIZE;
  18478. pubSz2 = DH_TEST_BUF_SIZE;
  18479. privSz = DH_TEST_BUF_SIZE;
  18480. privSz2 = DH_TEST_BUF_SIZE;
  18481. #ifndef WC_NO_RNG
  18482. XMEMSET(&rng, 0, sizeof(rng));
  18483. #endif
  18484. /* Use API for coverage. */
  18485. ret = wc_InitDhKey(key);
  18486. if (ret != 0)
  18487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18488. wc_FreeDhKey(key);
  18489. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18490. if (ret != 0)
  18491. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18492. keyInit = 1;
  18493. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  18494. if (ret != 0)
  18495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18496. key2Init = 1;
  18497. #ifdef NO_ASN
  18498. #ifndef WOLFSSL_SP_MATH
  18499. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  18500. if (ret != 0)
  18501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18502. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  18503. if (ret != 0)
  18504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18505. #else
  18506. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  18507. sizeof(dh2048_g));
  18508. if (ret != 0)
  18509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18510. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  18511. sizeof(dh2048_g));
  18512. if (ret != 0)
  18513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18514. #endif
  18515. #else
  18516. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18517. if (ret != 0)
  18518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18519. idx = 0;
  18520. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  18521. if (ret != 0)
  18522. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18523. #endif
  18524. #ifndef WC_NO_RNG
  18525. #ifndef HAVE_FIPS
  18526. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18527. #else
  18528. ret = wc_InitRng(&rng);
  18529. #endif
  18530. if (ret != 0)
  18531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18532. rngInit = 1;
  18533. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  18534. #if defined(WOLFSSL_ASYNC_CRYPT)
  18535. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18536. #endif
  18537. if (ret != 0)
  18538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18539. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  18540. #if defined(WOLFSSL_ASYNC_CRYPT)
  18541. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18542. #endif
  18543. if (ret != 0)
  18544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18545. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  18546. #if defined(WOLFSSL_ASYNC_CRYPT)
  18547. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  18548. #endif
  18549. if (ret != 0)
  18550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18551. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  18552. #if defined(WOLFSSL_ASYNC_CRYPT)
  18553. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  18554. #endif
  18555. if (ret != 0)
  18556. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18557. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  18558. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18559. }
  18560. #endif /* !WC_NO_RNG */
  18561. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18562. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  18563. if (ret != BAD_FUNC_ARG)
  18564. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18565. ret = wc_DhCheckPrivKey(key, priv, privSz);
  18566. if (ret != 0)
  18567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18568. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  18569. if (ret != BAD_FUNC_ARG)
  18570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18571. {
  18572. word32 pSz, qSz, gSz;
  18573. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  18574. if (ret != LENGTH_ONLY_E)
  18575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18576. }
  18577. #endif
  18578. /* Test DH key import / export */
  18579. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  18580. (!defined(HAVE_FIPS) || \
  18581. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  18582. wc_FreeDhKey(key);
  18583. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18584. if (ret != 0)
  18585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18586. #ifndef NO_ASN
  18587. {
  18588. /* DH Private - Key Export / Import */
  18589. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18590. byte *tmp2;
  18591. #else
  18592. byte tmp2[DH_TEST_TMP_SIZE];
  18593. #endif
  18594. #if defined(USE_CERT_BUFFERS_2048)
  18595. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  18596. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  18597. #else
  18598. XFILE file = XFOPEN(dhKeyFile, "rb");
  18599. if (!file)
  18600. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18601. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  18602. XFCLOSE(file);
  18603. if (bytes == 0)
  18604. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18605. #endif
  18606. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18607. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18608. if (tmp2 == NULL)
  18609. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18610. #endif
  18611. idx = 0;
  18612. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  18613. /* Import DH Private key as DER */
  18614. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18615. if (ret == 0) {
  18616. /* Export as DER */
  18617. idx = DH_TEST_TMP_SIZE;
  18618. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  18619. }
  18620. /* Verify export matches original */
  18621. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  18622. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18623. }
  18624. /* DH Public Key - Export / Import */
  18625. #if defined(USE_CERT_BUFFERS_2048)
  18626. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  18627. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  18628. #else
  18629. file = XFOPEN(dhKeyPubFile, "rb");
  18630. if (!file)
  18631. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18632. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  18633. XFCLOSE(file);
  18634. if (bytes == 0)
  18635. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  18636. #endif
  18637. /* for HAVE_WOLF_BIGINT prevent leak */
  18638. wc_FreeDhKey(key);
  18639. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  18640. idx = 0;
  18641. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  18642. /* Import DH Public key as DER */
  18643. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  18644. if (ret == 0) {
  18645. /* Export as DER */
  18646. idx = DH_TEST_TMP_SIZE;
  18647. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  18648. }
  18649. /* Verify export matches original */
  18650. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  18651. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  18652. }
  18653. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18654. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18655. #endif
  18656. }
  18657. #else
  18658. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  18659. if (ret != 0)
  18660. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18661. #endif /* !NO_ASN */
  18662. privSz = DH_TEST_BUF_SIZE;
  18663. pubSz = DH_TEST_BUF_SIZE;
  18664. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  18665. if (ret != 0)
  18666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18667. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  18668. if (ret != 0)
  18669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18670. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  18671. #ifndef WC_NO_RNG
  18672. ret = dh_generate_test(&rng);
  18673. if (ret != 0)
  18674. ERROR_OUT(ret, done);
  18675. ret = dh_fips_generate_test(&rng);
  18676. if (ret != 0)
  18677. ERROR_OUT(ret, done);
  18678. #endif /* !WC_NO_RNG */
  18679. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18680. ret = dh_test_check_pubvalue();
  18681. if (ret != 0)
  18682. ERROR_OUT(ret, done);
  18683. #endif
  18684. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  18685. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  18686. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  18687. * this will be available for testing in the 140-3 module */
  18688. #ifndef WC_NO_RNG
  18689. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  18690. #ifdef HAVE_FFDHE_2048
  18691. #ifdef HAVE_PUBLIC_FFDHE
  18692. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  18693. #else
  18694. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  18695. #endif
  18696. if (ret != 0)
  18697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18698. #endif
  18699. #ifdef HAVE_FFDHE_3072
  18700. #ifdef HAVE_PUBLIC_FFDHE
  18701. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  18702. #else
  18703. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  18704. #endif
  18705. if (ret != 0)
  18706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18707. #endif
  18708. #ifdef HAVE_FFDHE_4096
  18709. #ifdef HAVE_PUBLIC_FFDHE
  18710. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  18711. #else
  18712. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  18713. #endif
  18714. if (ret != 0)
  18715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18716. #endif
  18717. #endif /* !WC_NO_RNG */
  18718. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  18719. wc_FreeDhKey(key);
  18720. keyInit = 0;
  18721. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  18722. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  18723. /* Test Check Key */
  18724. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  18725. NULL, 0, 0, &rng);
  18726. if (ret != 0)
  18727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  18728. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  18729. #endif
  18730. done:
  18731. #ifndef WC_NO_RNG
  18732. if (rngInit)
  18733. wc_FreeRng(&rng);
  18734. #endif
  18735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18736. if (key) {
  18737. if (keyInit)
  18738. wc_FreeDhKey(key);
  18739. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18740. }
  18741. if (key2) {
  18742. if (key2Init)
  18743. wc_FreeDhKey(key2);
  18744. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18745. }
  18746. if (tmp)
  18747. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18748. if (priv)
  18749. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18750. if (pub)
  18751. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18752. if (priv2)
  18753. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18754. if (pub2)
  18755. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18756. if (agree)
  18757. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18758. if (agree2)
  18759. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18760. #else
  18761. if (keyInit)
  18762. wc_FreeDhKey(key);
  18763. if (key2Init)
  18764. wc_FreeDhKey(key2);
  18765. #endif
  18766. (void)privSz;
  18767. (void)pubSz;
  18768. (void)pubSz2;
  18769. (void)privSz2;
  18770. return ret;
  18771. #undef DH_TEST_BUF_SIZE
  18772. #undef DH_TEST_TMP_SIZE
  18773. }
  18774. #endif /* NO_DH */
  18775. #ifndef NO_DSA
  18776. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  18777. {
  18778. wc_test_ret_t ret = 0;
  18779. int answer;
  18780. word32 bytes;
  18781. word32 idx = 0;
  18782. WC_RNG rng;
  18783. int rng_inited = 0;
  18784. wc_Sha sha;
  18785. byte hash[WC_SHA_DIGEST_SIZE];
  18786. byte signature[40];
  18787. int key_inited = 0;
  18788. #ifdef WOLFSSL_KEY_GEN
  18789. byte* der = 0;
  18790. int derIn_inited = 0;
  18791. int genKey_inited = 0;
  18792. #endif
  18793. #define DSA_TEST_TMP_SIZE 1024
  18794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18795. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18796. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18797. #ifdef WOLFSSL_KEY_GEN
  18798. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18799. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18800. #endif
  18801. WOLFSSL_ENTER("dsa_test");
  18802. if ((tmp == NULL) ||
  18803. (key == NULL)
  18804. #ifdef WOLFSSL_KEY_GEN
  18805. || (derIn == NULL)
  18806. || (genKey == NULL)
  18807. #endif
  18808. ) {
  18809. ret = WC_TEST_RET_ENC_NC;
  18810. goto out;
  18811. }
  18812. #else
  18813. byte tmp[1024];
  18814. DsaKey key[1];
  18815. #ifdef WOLFSSL_KEY_GEN
  18816. DsaKey derIn[1];
  18817. DsaKey genKey[1];
  18818. #endif
  18819. #endif
  18820. #ifdef USE_CERT_BUFFERS_1024
  18821. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18822. bytes = sizeof_dsa_key_der_1024;
  18823. #elif defined(USE_CERT_BUFFERS_2048)
  18824. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18825. bytes = sizeof_dsa_key_der_2048;
  18826. #else
  18827. {
  18828. XFILE file = XFOPEN(dsaKey, "rb");
  18829. if (!file)
  18830. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  18831. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  18832. XFCLOSE(file);
  18833. if (bytes == 0)
  18834. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  18835. }
  18836. #endif /* USE_CERT_BUFFERS */
  18837. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  18838. if (ret != 0)
  18839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18840. wc_ShaUpdate(&sha, tmp, bytes);
  18841. wc_ShaFinal(&sha, hash);
  18842. wc_ShaFree(&sha);
  18843. ret = wc_InitDsaKey(key);
  18844. if (ret != 0)
  18845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18846. key_inited = 1;
  18847. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  18848. if (ret != 0)
  18849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18850. #ifndef HAVE_FIPS
  18851. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18852. #else
  18853. ret = wc_InitRng(&rng);
  18854. #endif
  18855. if (ret != 0)
  18856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18857. rng_inited = 1;
  18858. ret = wc_DsaSign(hash, signature, key, &rng);
  18859. if (ret != 0)
  18860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18861. ret = wc_DsaVerify(hash, signature, key, &answer);
  18862. if (ret != 0)
  18863. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18864. if (answer != 1)
  18865. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  18866. wc_FreeDsaKey(key);
  18867. key_inited = 0;
  18868. ret = wc_InitDsaKey_h(key, NULL);
  18869. if (ret != 0)
  18870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18871. key_inited = 1;
  18872. #ifdef WOLFSSL_KEY_GEN
  18873. {
  18874. int derSz = 0;
  18875. ret = wc_InitDsaKey(genKey);
  18876. if (ret != 0)
  18877. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18878. genKey_inited = 1;
  18879. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  18880. if (ret != 0)
  18881. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18882. ret = wc_MakeDsaKey(&rng, genKey);
  18883. if (ret != 0)
  18884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18885. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18886. if (der == NULL)
  18887. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  18888. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  18889. if (derSz < 0)
  18890. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  18891. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18892. DSA_PRIVATEKEY_TYPE);
  18893. if (ret != 0)
  18894. goto out;
  18895. ret = wc_InitDsaKey(derIn);
  18896. if (ret != 0)
  18897. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18898. derIn_inited = 1;
  18899. idx = 0;
  18900. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  18901. if (ret != 0)
  18902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  18903. }
  18904. #endif /* WOLFSSL_KEY_GEN */
  18905. out:
  18906. #ifdef WOLFSSL_KEY_GEN
  18907. if (der)
  18908. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18909. #endif
  18910. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18911. if (tmp)
  18912. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18913. if (key) {
  18914. if (key_inited)
  18915. wc_FreeDsaKey(key);
  18916. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18917. }
  18918. #ifdef WOLFSSL_KEY_GEN
  18919. if (derIn) {
  18920. if (derIn_inited)
  18921. wc_FreeDsaKey(derIn);
  18922. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18923. }
  18924. if (genKey) {
  18925. if (genKey_inited)
  18926. wc_FreeDsaKey(genKey);
  18927. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18928. }
  18929. #endif
  18930. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  18931. if (key_inited)
  18932. wc_FreeDsaKey(key);
  18933. #ifdef WOLFSSL_KEY_GEN
  18934. if (derIn_inited)
  18935. wc_FreeDsaKey(derIn);
  18936. if (genKey_inited)
  18937. wc_FreeDsaKey(genKey);
  18938. #endif
  18939. #endif
  18940. if (rng_inited)
  18941. wc_FreeRng(&rng);
  18942. return ret;
  18943. }
  18944. #endif /* NO_DSA */
  18945. #ifdef WOLFCRYPT_HAVE_SRP
  18946. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  18947. {
  18948. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  18949. WC_RNG rng;
  18950. if(NULL == buf || !size)
  18951. return WC_TEST_RET_ENC_NC;
  18952. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  18953. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  18954. wc_FreeRng(&rng);
  18955. }
  18956. return ret;
  18957. }
  18958. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  18959. {
  18960. wc_test_ret_t r;
  18961. byte clientPubKey[192]; /* A */
  18962. byte serverPubKey[192]; /* B */
  18963. word32 clientPubKeySz = 192;
  18964. word32 serverPubKeySz = 192;
  18965. byte username[] = "user";
  18966. word32 usernameSz = 4;
  18967. byte password[] = "password";
  18968. word32 passwordSz = 8;
  18969. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  18970. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  18971. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  18972. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  18973. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  18974. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  18975. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  18976. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  18977. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  18978. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  18979. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  18980. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  18981. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  18982. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  18983. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  18984. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  18985. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  18986. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  18987. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  18988. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  18989. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  18990. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  18991. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  18992. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  18993. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  18994. };
  18995. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  18996. 0x02
  18997. };
  18998. byte salt[10];
  18999. byte verifier[192];
  19000. word32 v_size = sizeof(verifier);
  19001. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  19002. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  19003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19004. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19005. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19006. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  19007. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  19008. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  19009. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  19010. if ((cli == NULL) ||
  19011. (srv == NULL) ||
  19012. (clientProof == NULL) ||
  19013. (serverProof == NULL)) {
  19014. r = WC_TEST_RET_ENC_NC;
  19015. goto out;
  19016. }
  19017. #else
  19018. Srp cli[1], srv[1];
  19019. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  19020. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  19021. #endif
  19022. /* set as 0's so if second init on srv not called SrpTerm is not on
  19023. * garbage values */
  19024. XMEMSET(srv, 0, sizeof *srv);
  19025. XMEMSET(cli, 0, sizeof *cli);
  19026. /* generating random salt */
  19027. r = generate_random_salt(salt, sizeof(salt));
  19028. /* client knows username and password. */
  19029. /* server knows N, g, salt and verifier. */
  19030. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  19031. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  19032. /* loading N, g and salt in advance to generate the verifier. */
  19033. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  19034. g, sizeof(g),
  19035. salt, sizeof(salt));
  19036. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  19037. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  19038. /* client sends username to server */
  19039. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  19040. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  19041. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  19042. g, sizeof(g),
  19043. salt, sizeof(salt));
  19044. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  19045. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  19046. /* server sends N, g, salt and B to client */
  19047. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  19048. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  19049. serverPubKey, serverPubKeySz);
  19050. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  19051. /* client sends A and M1 to server */
  19052. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  19053. serverPubKey, serverPubKeySz);
  19054. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  19055. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  19056. /* server sends M2 to client */
  19057. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  19058. wc_SrpTerm(cli);
  19059. wc_SrpTerm(srv);
  19060. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19061. out:
  19062. if (cli)
  19063. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19064. if (srv)
  19065. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19066. if (clientProof)
  19067. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19068. if (serverProof)
  19069. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19070. #endif
  19071. return r;
  19072. }
  19073. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  19074. {
  19075. wc_test_ret_t ret;
  19076. WOLFSSL_ENTER("srp_test");
  19077. #ifndef NO_SHA
  19078. ret = srp_test_digest(SRP_TYPE_SHA);
  19079. if (ret != 0)
  19080. return ret;
  19081. #endif
  19082. #ifndef NO_SHA256
  19083. ret = srp_test_digest(SRP_TYPE_SHA256);
  19084. if (ret != 0)
  19085. return ret;
  19086. #endif
  19087. #ifdef WOLFSSL_SHA384
  19088. ret = srp_test_digest(SRP_TYPE_SHA384);
  19089. if (ret != 0)
  19090. return ret;
  19091. #endif
  19092. #ifdef WOLFSSL_SHA512
  19093. ret = srp_test_digest(SRP_TYPE_SHA512);
  19094. if (ret != 0)
  19095. return ret;
  19096. #endif
  19097. return ret;
  19098. }
  19099. #endif /* WOLFCRYPT_HAVE_SRP */
  19100. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  19101. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  19102. static wc_test_ret_t openssl_aes_test(void)
  19103. {
  19104. #ifdef HAVE_AES_CBC
  19105. #ifdef WOLFSSL_AES_128
  19106. {
  19107. /* EVP_CipherUpdate test */
  19108. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  19109. {
  19110. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19111. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19112. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19113. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19114. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19115. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19116. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19117. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19118. };
  19119. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19120. "0123456789abcdef "; /* align */
  19121. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  19122. "1234567890abcdef "; /* align */
  19123. byte cipher[AES_BLOCK_SIZE * 4];
  19124. byte plain [AES_BLOCK_SIZE * 4];
  19125. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19126. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19127. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19128. #else
  19129. EVP_CIPHER_CTX en[1];
  19130. EVP_CIPHER_CTX de[1];
  19131. #endif
  19132. int outlen ;
  19133. int total = 0;
  19134. int i;
  19135. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19136. if ((en == NULL) || (de == NULL))
  19137. return MEMORY_E;
  19138. #endif
  19139. EVP_CIPHER_CTX_init(en);
  19140. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19141. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19142. return WC_TEST_RET_ENC_NC;
  19143. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19144. (byte*)cbcPlain, 9) == 0)
  19145. return WC_TEST_RET_ENC_NC;
  19146. if (outlen != 0)
  19147. return WC_TEST_RET_ENC_NC;
  19148. total += outlen;
  19149. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  19150. (byte*)&cbcPlain[9] , 9) == 0)
  19151. return WC_TEST_RET_ENC_NC;
  19152. if (outlen != 16)
  19153. return WC_TEST_RET_ENC_NC;
  19154. total += outlen;
  19155. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19156. return WC_TEST_RET_ENC_NC;
  19157. if (outlen != 16)
  19158. return WC_TEST_RET_ENC_NC;
  19159. total += outlen;
  19160. if (total != 32)
  19161. return 3408;
  19162. EVP_CIPHER_CTX_cleanup(en);
  19163. total = 0;
  19164. EVP_CIPHER_CTX_init(de);
  19165. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19166. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19167. return WC_TEST_RET_ENC_NC;
  19168. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  19169. return WC_TEST_RET_ENC_NC;
  19170. if (outlen != 0)
  19171. return WC_TEST_RET_ENC_NC;
  19172. total += outlen;
  19173. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19174. (byte*)&cipher[6], 12) == 0)
  19175. return WC_TEST_RET_ENC_NC;
  19176. if (outlen != 0)
  19177. total += outlen;
  19178. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19179. (byte*)&cipher[6+12], 14) == 0)
  19180. return WC_TEST_RET_ENC_NC;
  19181. if (outlen != 16)
  19182. return WC_TEST_RET_ENC_NC;
  19183. total += outlen;
  19184. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  19185. return WC_TEST_RET_ENC_NC;
  19186. if (outlen != 2)
  19187. return WC_TEST_RET_ENC_NC;
  19188. total += outlen;
  19189. if (total != 18)
  19190. return 3427;
  19191. if (XMEMCMP(plain, cbcPlain, 18))
  19192. return WC_TEST_RET_ENC_NC;
  19193. EVP_CIPHER_CTX_cleanup(de);
  19194. /* test with encrypting/decrypting more than 16 bytes at once */
  19195. total = 0;
  19196. EVP_CIPHER_CTX_init(en);
  19197. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19198. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19199. return WC_TEST_RET_ENC_NC;
  19200. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19201. (byte*)cbcPlain, 17) == 0)
  19202. return WC_TEST_RET_ENC_NC;
  19203. if (outlen != 16)
  19204. return WC_TEST_RET_ENC_NC;
  19205. total += outlen;
  19206. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  19207. (byte*)&cbcPlain[17] , 1) == 0)
  19208. return WC_TEST_RET_ENC_NC;
  19209. if (outlen != 0)
  19210. return WC_TEST_RET_ENC_NC;
  19211. total += outlen;
  19212. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19213. return WC_TEST_RET_ENC_NC;
  19214. if (outlen != 16)
  19215. return WC_TEST_RET_ENC_NC;
  19216. total += outlen;
  19217. if (total != 32)
  19218. return WC_TEST_RET_ENC_NC;
  19219. EVP_CIPHER_CTX_cleanup(en);
  19220. total = 0;
  19221. EVP_CIPHER_CTX_init(de);
  19222. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19223. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19224. return WC_TEST_RET_ENC_NC;
  19225. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  19226. return WC_TEST_RET_ENC_NC;
  19227. if (outlen != 16)
  19228. return WC_TEST_RET_ENC_NC;
  19229. total += outlen;
  19230. /* final call on non block size should fail */
  19231. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  19232. return WC_TEST_RET_ENC_NC;
  19233. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19234. (byte*)&cipher[17], 1) == 0)
  19235. return WC_TEST_RET_ENC_NC;
  19236. if (outlen != 0)
  19237. total += outlen;
  19238. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19239. (byte*)&cipher[17+1], 14) == 0)
  19240. return WC_TEST_RET_ENC_NC;
  19241. if (outlen != 0)
  19242. return WC_TEST_RET_ENC_NC;
  19243. total += outlen;
  19244. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  19245. return WC_TEST_RET_ENC_NC;
  19246. if (outlen != 2)
  19247. return WC_TEST_RET_ENC_NC;
  19248. total += outlen;
  19249. if (total != 18)
  19250. return WC_TEST_RET_ENC_NC;
  19251. if (XMEMCMP(plain, cbcPlain, 18))
  19252. return WC_TEST_RET_ENC_NC;
  19253. /* test byte by byte decrypt */
  19254. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  19255. plain[i] = i;
  19256. }
  19257. EVP_CIPHER_CTX_cleanup(de);
  19258. total = 0;
  19259. EVP_CIPHER_CTX_init(en);
  19260. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19261. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19262. return WC_TEST_RET_ENC_NC;
  19263. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19264. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  19265. return WC_TEST_RET_ENC_NC;
  19266. if (outlen != AES_BLOCK_SIZE * 3)
  19267. return WC_TEST_RET_ENC_NC;
  19268. total += outlen;
  19269. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19270. return WC_TEST_RET_ENC_NC;
  19271. if (outlen != AES_BLOCK_SIZE)
  19272. return WC_TEST_RET_ENC_NC;
  19273. total += outlen;
  19274. if (total != sizeof(plain))
  19275. return WC_TEST_RET_ENC_NC;
  19276. EVP_CIPHER_CTX_cleanup(en);
  19277. total = 0;
  19278. EVP_CIPHER_CTX_init(de);
  19279. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19280. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19281. return WC_TEST_RET_ENC_NC;
  19282. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  19283. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  19284. (byte*)cipher + i, 1) == 0)
  19285. return WC_TEST_RET_ENC_NC;
  19286. if (outlen > 0) {
  19287. int j;
  19288. total += outlen;
  19289. for (j = 0; j < total; j++) {
  19290. if (plain[j] != j) {
  19291. return WC_TEST_RET_ENC_NC;
  19292. }
  19293. }
  19294. }
  19295. }
  19296. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  19297. return WC_TEST_RET_ENC_NC;
  19298. total += outlen;
  19299. if (total != AES_BLOCK_SIZE * 3) {
  19300. return WC_TEST_RET_ENC_NC;
  19301. }
  19302. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  19303. if (plain[i] != i) {
  19304. return WC_TEST_RET_ENC_NC;
  19305. }
  19306. }
  19307. EVP_CIPHER_CTX_cleanup(de);
  19308. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19309. wolfSSL_EVP_CIPHER_CTX_free(en);
  19310. wolfSSL_EVP_CIPHER_CTX_free(de);
  19311. #endif
  19312. }
  19313. /* set buffers to be exact size to catch potential over read/write */
  19314. {
  19315. /* EVP_CipherUpdate test */
  19316. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  19317. {
  19318. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19319. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19320. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19321. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19322. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19323. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19324. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19325. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19326. };
  19327. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19328. "0123456789abcdef "; /* align */
  19329. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  19330. "1234567890abcdef "; /* align */
  19331. #define EVP_TEST_BUF_SZ 18
  19332. #define EVP_TEST_BUF_PAD 32
  19333. byte cipher[EVP_TEST_BUF_SZ];
  19334. byte plain [EVP_TEST_BUF_SZ];
  19335. byte padded[EVP_TEST_BUF_PAD];
  19336. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19337. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19338. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19339. #else
  19340. EVP_CIPHER_CTX en[1];
  19341. EVP_CIPHER_CTX de[1];
  19342. #endif
  19343. int outlen ;
  19344. int total = 0;
  19345. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19346. if ((en == NULL) || (de == NULL))
  19347. return MEMORY_E;
  19348. #endif
  19349. EVP_CIPHER_CTX_init(en);
  19350. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19351. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19352. return WC_TEST_RET_ENC_NC;
  19353. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  19354. return WC_TEST_RET_ENC_NC;
  19355. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19356. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  19357. return WC_TEST_RET_ENC_NC;
  19358. if (outlen != 16)
  19359. return WC_TEST_RET_ENC_NC;
  19360. total += outlen;
  19361. /* should fail here */
  19362. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  19363. return WC_TEST_RET_ENC_NC;
  19364. EVP_CIPHER_CTX_cleanup(en);
  19365. /* turn padding back on and do successful encrypt */
  19366. total = 0;
  19367. EVP_CIPHER_CTX_init(en);
  19368. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19369. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19370. return WC_TEST_RET_ENC_NC;
  19371. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  19372. return WC_TEST_RET_ENC_NC;
  19373. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  19374. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  19375. return WC_TEST_RET_ENC_NC;
  19376. if (outlen != 16)
  19377. return WC_TEST_RET_ENC_NC;
  19378. total += outlen;
  19379. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  19380. return WC_TEST_RET_ENC_NC;
  19381. total += outlen;
  19382. if (total != 32)
  19383. return WC_TEST_RET_ENC_NC;
  19384. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  19385. EVP_CIPHER_CTX_cleanup(en);
  19386. /* test out of bounds read on buffers w/o padding during decryption */
  19387. total = 0;
  19388. EVP_CIPHER_CTX_init(de);
  19389. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19390. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19391. return WC_TEST_RET_ENC_NC;
  19392. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  19393. return WC_TEST_RET_ENC_NC;
  19394. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  19395. EVP_TEST_BUF_SZ) == 0)
  19396. return WC_TEST_RET_ENC_NC;
  19397. if (outlen != 16)
  19398. return WC_TEST_RET_ENC_NC;
  19399. total += outlen;
  19400. /* should fail since not using padding */
  19401. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  19402. return WC_TEST_RET_ENC_NC;
  19403. EVP_CIPHER_CTX_cleanup(de);
  19404. total = 0;
  19405. EVP_CIPHER_CTX_init(de);
  19406. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19407. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19408. return WC_TEST_RET_ENC_NC;
  19409. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  19410. return WC_TEST_RET_ENC_NC;
  19411. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  19412. EVP_TEST_BUF_PAD) == 0)
  19413. return WC_TEST_RET_ENC_NC;
  19414. if (outlen != 16)
  19415. return WC_TEST_RET_ENC_NC;
  19416. total += outlen;
  19417. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  19418. return WC_TEST_RET_ENC_NC;
  19419. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  19420. return WC_TEST_RET_ENC_NC;
  19421. EVP_CIPHER_CTX_cleanup(de);
  19422. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19423. wolfSSL_EVP_CIPHER_CTX_free(en);
  19424. wolfSSL_EVP_CIPHER_CTX_free(de);
  19425. #endif
  19426. }
  19427. { /* evp_cipher test: EVP_aes_128_cbc */
  19428. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19429. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  19430. #else
  19431. EVP_CIPHER_CTX ctx[1];
  19432. #endif
  19433. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  19434. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  19435. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  19436. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  19437. };
  19438. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  19439. {
  19440. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  19441. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  19442. };
  19443. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19444. "0123456789abcdef "; /* align */
  19445. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  19446. "1234567890abcdef "; /* align */
  19447. byte cipher[AES_BLOCK_SIZE * 4];
  19448. byte plain [AES_BLOCK_SIZE * 4];
  19449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19450. if (ctx == NULL)
  19451. return MEMORY_E;
  19452. #endif
  19453. EVP_CIPHER_CTX_init(ctx);
  19454. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  19455. return WC_TEST_RET_ENC_NC;
  19456. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  19457. return WC_TEST_RET_ENC_NC;
  19458. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19459. return WC_TEST_RET_ENC_NC;
  19460. EVP_CIPHER_CTX_cleanup(ctx);
  19461. EVP_CIPHER_CTX_init(ctx);
  19462. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  19463. return WC_TEST_RET_ENC_NC;
  19464. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  19465. return WC_TEST_RET_ENC_NC;
  19466. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19467. return WC_TEST_RET_ENC_NC;
  19468. EVP_CIPHER_CTX_cleanup(ctx);
  19469. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19470. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  19471. #endif
  19472. } /* end evp_cipher test: EVP_aes_128_cbc*/
  19473. #endif /* WOLFSSL_AES_128 */
  19474. #endif /* HAVE_AES_CBC */
  19475. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  19476. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  19477. { /* evp_cipher test: EVP_aes_256_ecb*/
  19478. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19479. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  19480. #else
  19481. EVP_CIPHER_CTX ctx[1];
  19482. #endif
  19483. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19484. {
  19485. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19486. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19487. };
  19488. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  19489. {
  19490. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  19491. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  19492. };
  19493. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19494. {
  19495. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19496. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19497. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19498. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19499. };
  19500. byte cipher[AES_BLOCK_SIZE * 4];
  19501. byte plain [AES_BLOCK_SIZE * 4];
  19502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19503. if (ctx == NULL)
  19504. return MEMORY_E;
  19505. #endif
  19506. EVP_CIPHER_CTX_init(ctx);
  19507. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  19508. return WC_TEST_RET_ENC_NC;
  19509. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  19510. return WC_TEST_RET_ENC_NC;
  19511. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19512. return WC_TEST_RET_ENC_NC;
  19513. EVP_CIPHER_CTX_init(ctx);
  19514. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  19515. return WC_TEST_RET_ENC_NC;
  19516. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  19517. return WC_TEST_RET_ENC_NC;
  19518. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19519. return WC_TEST_RET_ENC_NC;
  19520. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19521. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  19522. #endif
  19523. } /* end evp_cipher test */
  19524. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  19525. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  19526. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  19527. {
  19528. /* Test: AES_encrypt/decrypt/set Key */
  19529. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19530. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19531. #ifdef HAVE_AES_DECRYPT
  19532. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19533. #endif
  19534. #else
  19535. AES_KEY enc[1];
  19536. #ifdef HAVE_AES_DECRYPT
  19537. AES_KEY dec[1];
  19538. #endif
  19539. #endif
  19540. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19541. {
  19542. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19543. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19544. };
  19545. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  19546. {
  19547. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  19548. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  19549. };
  19550. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19551. {
  19552. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19553. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19554. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19555. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19556. };
  19557. byte plain[sizeof(msg)];
  19558. byte cipher[sizeof(msg)];
  19559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19560. if (enc == NULL)
  19561. return MEMORY_E;
  19562. #ifdef HAVE_AES_DECRYPT
  19563. if (dec == NULL)
  19564. return MEMORY_E;
  19565. #endif
  19566. #endif
  19567. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  19568. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  19569. AES_encrypt(msg, cipher, enc);
  19570. #ifdef HAVE_AES_DECRYPT
  19571. AES_decrypt(cipher, plain, dec);
  19572. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  19573. return WC_TEST_RET_ENC_NC;
  19574. #endif /* HAVE_AES_DECRYPT */
  19575. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  19576. return WC_TEST_RET_ENC_NC;
  19577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19578. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19579. #ifdef HAVE_AES_DECRYPT
  19580. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19581. #endif
  19582. #endif
  19583. }
  19584. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  19585. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  19586. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  19587. #ifdef WOLFSSL_AES_COUNTER
  19588. {
  19589. byte plainBuff [64];
  19590. byte cipherBuff[64];
  19591. #ifdef WOLFSSL_AES_128
  19592. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  19593. {
  19594. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  19595. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  19596. };
  19597. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  19598. {
  19599. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19600. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19601. };
  19602. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  19603. {
  19604. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19605. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19606. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19607. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19608. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19609. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19610. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19611. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19612. };
  19613. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  19614. {
  19615. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  19616. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  19617. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  19618. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  19619. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  19620. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  19621. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  19622. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  19623. };
  19624. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  19625. {
  19626. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  19627. 0xc2
  19628. };
  19629. #endif
  19630. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19631. * NIST Special Publication 800-38A */
  19632. #ifdef WOLFSSL_AES_192
  19633. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  19634. {
  19635. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  19636. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  19637. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  19638. };
  19639. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  19640. {
  19641. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19642. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19643. };
  19644. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  19645. {
  19646. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19647. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19648. };
  19649. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  19650. {
  19651. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  19652. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  19653. };
  19654. #endif /* WOLFSSL_AES_192 */
  19655. #ifdef WOLFSSL_AES_256
  19656. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19657. * NIST Special Publication 800-38A */
  19658. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  19659. {
  19660. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19661. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19662. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19663. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19664. };
  19665. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  19666. {
  19667. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19668. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19669. };
  19670. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  19671. {
  19672. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19673. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19674. };
  19675. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  19676. {
  19677. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  19678. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  19679. };
  19680. #endif /* WOLFSSL_AES_256 */
  19681. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19682. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19683. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19684. #else
  19685. EVP_CIPHER_CTX en[1];
  19686. EVP_CIPHER_CTX de[1];
  19687. #endif
  19688. #ifdef WOLFSSL_AES_128
  19689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19690. EVP_CIPHER_CTX *p_en;
  19691. EVP_CIPHER_CTX *p_de;
  19692. #endif
  19693. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19694. if ((en == NULL) || (de == NULL))
  19695. return MEMORY_E;
  19696. #endif
  19697. EVP_CIPHER_CTX_init(en);
  19698. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19699. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19700. return WC_TEST_RET_ENC_NC;
  19701. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  19702. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19703. return WC_TEST_RET_ENC_NC;
  19704. EVP_CIPHER_CTX_init(de);
  19705. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19706. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19707. return WC_TEST_RET_ENC_NC;
  19708. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19709. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19710. return WC_TEST_RET_ENC_NC;
  19711. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19712. return WC_TEST_RET_ENC_NC;
  19713. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19714. return WC_TEST_RET_ENC_NC;
  19715. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19716. return WC_TEST_RET_ENC_NC;
  19717. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19718. return WC_TEST_RET_ENC_NC;
  19719. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19720. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  19721. if (p_en == NULL)
  19722. return WC_TEST_RET_ENC_ERRNO;
  19723. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  19724. if (p_de == NULL)
  19725. return WC_TEST_RET_ENC_ERRNO;
  19726. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  19727. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19728. return WC_TEST_RET_ENC_NC;
  19729. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  19730. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19731. return WC_TEST_RET_ENC_NC;
  19732. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  19733. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19734. return WC_TEST_RET_ENC_NC;
  19735. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  19736. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19737. return WC_TEST_RET_ENC_NC;
  19738. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19739. return WC_TEST_RET_ENC_NC;
  19740. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19741. return WC_TEST_RET_ENC_NC;
  19742. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  19743. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  19744. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19745. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19746. return WC_TEST_RET_ENC_NC;
  19747. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19748. return WC_TEST_RET_ENC_NC;
  19749. EVP_CIPHER_CTX_init(en);
  19750. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19751. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19752. return WC_TEST_RET_ENC_NC;
  19753. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19754. return WC_TEST_RET_ENC_NC;
  19755. EVP_CIPHER_CTX_init(de);
  19756. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19757. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19758. return WC_TEST_RET_ENC_NC;
  19759. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19760. return WC_TEST_RET_ENC_NC;
  19761. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19762. return WC_TEST_RET_ENC_NC;
  19763. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  19764. return WC_TEST_RET_ENC_NC;
  19765. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19766. return WC_TEST_RET_ENC_NC;
  19767. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19768. return WC_TEST_RET_ENC_NC;
  19769. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19770. return WC_TEST_RET_ENC_NC;
  19771. if (XMEMCMP(cipherBuff, oddCipher, 9))
  19772. return WC_TEST_RET_ENC_NC;
  19773. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19774. return WC_TEST_RET_ENC_NC;
  19775. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19776. return WC_TEST_RET_ENC_NC;
  19777. #endif /* WOLFSSL_AES_128 */
  19778. #ifdef WOLFSSL_AES_192
  19779. EVP_CIPHER_CTX_init(en);
  19780. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  19781. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19782. return WC_TEST_RET_ENC_NC;
  19783. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  19784. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19785. return WC_TEST_RET_ENC_NC;
  19786. EVP_CIPHER_CTX_init(de);
  19787. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  19788. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19789. return WC_TEST_RET_ENC_NC;
  19790. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19791. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19792. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19793. return WC_TEST_RET_ENC_NC;
  19794. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  19795. return WC_TEST_RET_ENC_NC;
  19796. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  19797. return WC_TEST_RET_ENC_NC;
  19798. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19799. return WC_TEST_RET_ENC_NC;
  19800. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19801. return WC_TEST_RET_ENC_NC;
  19802. #endif /* WOLFSSL_AES_192 */
  19803. #ifdef WOLFSSL_AES_256
  19804. EVP_CIPHER_CTX_init(en);
  19805. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  19806. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19807. return WC_TEST_RET_ENC_NC;
  19808. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  19809. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19810. return WC_TEST_RET_ENC_NC;
  19811. EVP_CIPHER_CTX_init(de);
  19812. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  19813. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19814. return WC_TEST_RET_ENC_NC;
  19815. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19816. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19817. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19818. return WC_TEST_RET_ENC_NC;
  19819. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  19820. return WC_TEST_RET_ENC_NC;
  19821. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  19822. return WC_TEST_RET_ENC_NC;
  19823. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19824. return WC_TEST_RET_ENC_NC;
  19825. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19826. return WC_TEST_RET_ENC_NC;
  19827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19828. wolfSSL_EVP_CIPHER_CTX_free(en);
  19829. wolfSSL_EVP_CIPHER_CTX_free(de);
  19830. #endif
  19831. #endif /* WOLFSSL_AES_256 */
  19832. }
  19833. #endif /* HAVE_AES_COUNTER */
  19834. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  19835. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  19836. {
  19837. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19838. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19839. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19840. #else
  19841. AES_KEY enc[1];
  19842. AES_KEY dec[1];
  19843. #endif
  19844. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  19845. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  19846. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  19847. };
  19848. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  19849. {
  19850. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  19851. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  19852. };
  19853. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  19854. {
  19855. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  19856. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  19857. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  19858. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  19859. };
  19860. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  19861. {
  19862. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19863. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19864. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19865. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  19866. };
  19867. byte cipher[AES_BLOCK_SIZE * 2];
  19868. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  19869. int num = 0;
  19870. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19871. if ((enc == NULL) || (dec == NULL))
  19872. return MEMORY_E;
  19873. #endif
  19874. XMEMCPY(iv, setIv, sizeof(setIv));
  19875. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  19876. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  19877. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  19878. &num, AES_ENCRYPT);
  19879. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  19880. return WC_TEST_RET_ENC_NC;
  19881. if (num != 15) /* should have used 15 of the 16 bytes */
  19882. return WC_TEST_RET_ENC_NC;
  19883. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  19884. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  19885. &num, AES_ENCRYPT);
  19886. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  19887. return WC_TEST_RET_ENC_NC;
  19888. if (num != 0)
  19889. return WC_TEST_RET_ENC_NC;
  19890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19891. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  19892. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  19893. #endif
  19894. }
  19895. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  19896. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  19897. return 0;
  19898. }
  19899. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  19900. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  19901. {
  19902. wc_test_ret_t ret;
  19903. EVP_MD_CTX md_ctx;
  19904. testVector a, b, c, d, e, f;
  19905. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  19906. WOLFSSL_ENTER("openssl_test");
  19907. a.inLen = 0;
  19908. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  19909. (void)a;
  19910. (void)b;
  19911. (void)c;
  19912. (void)d;
  19913. (void)e;
  19914. (void)f;
  19915. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  19916. {
  19917. byte* p;
  19918. p = (byte*)CRYPTO_malloc(10, "", 0);
  19919. if (p == NULL) {
  19920. return WC_TEST_RET_ENC_NC;
  19921. }
  19922. XMEMSET(p, 0, 10);
  19923. CRYPTO_free(p, "", 0);
  19924. }
  19925. #ifndef NO_MD5
  19926. a.input = "1234567890123456789012345678901234567890123456789012345678"
  19927. "9012345678901234567890";
  19928. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  19929. "\x7a";
  19930. a.inLen = XSTRLEN(a.input);
  19931. a.outLen = WC_MD5_DIGEST_SIZE;
  19932. EVP_MD_CTX_init(&md_ctx);
  19933. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  19934. if (ret == WOLFSSL_SUCCESS) {
  19935. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  19936. }
  19937. if (ret == WOLFSSL_SUCCESS) {
  19938. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19939. }
  19940. EVP_MD_CTX_cleanup(&md_ctx);
  19941. if (ret != WOLFSSL_SUCCESS)
  19942. return WC_TEST_RET_ENC_NC;
  19943. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  19944. return WC_TEST_RET_ENC_NC;
  19945. #endif /* NO_MD5 */
  19946. #ifndef NO_SHA
  19947. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  19948. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  19949. "aaaaaaaaaa";
  19950. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  19951. "\x53\x99\x5E\x26\xA0";
  19952. b.inLen = XSTRLEN(b.input);
  19953. b.outLen = WC_SHA_DIGEST_SIZE;
  19954. EVP_MD_CTX_init(&md_ctx);
  19955. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  19956. if (ret == WOLFSSL_SUCCESS) {
  19957. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  19958. if (ret == WOLFSSL_SUCCESS)
  19959. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19960. }
  19961. EVP_MD_CTX_cleanup(&md_ctx);
  19962. if (ret != WOLFSSL_SUCCESS)
  19963. return WC_TEST_RET_ENC_NC;
  19964. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  19965. return WC_TEST_RET_ENC_NC;
  19966. #endif /* NO_SHA */
  19967. #ifdef WOLFSSL_SHA224
  19968. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  19969. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  19970. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  19971. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  19972. e.inLen = XSTRLEN(e.input);
  19973. e.outLen = WC_SHA224_DIGEST_SIZE;
  19974. EVP_MD_CTX_init(&md_ctx);
  19975. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  19976. if (ret == WOLFSSL_SUCCESS) {
  19977. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  19978. if (ret == WOLFSSL_SUCCESS)
  19979. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19980. }
  19981. EVP_MD_CTX_cleanup(&md_ctx);
  19982. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  19983. return WC_TEST_RET_ENC_NC;
  19984. }
  19985. #endif /* WOLFSSL_SHA224 */
  19986. #ifndef NO_SHA256
  19987. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  19988. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  19989. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  19990. "\x06\xC1";
  19991. d.inLen = XSTRLEN(d.input);
  19992. d.outLen = WC_SHA256_DIGEST_SIZE;
  19993. EVP_MD_CTX_init(&md_ctx);
  19994. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  19995. if (ret == WOLFSSL_SUCCESS) {
  19996. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  19997. if (ret == WOLFSSL_SUCCESS)
  19998. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  19999. }
  20000. EVP_MD_CTX_cleanup(&md_ctx);
  20001. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  20002. return WC_TEST_RET_ENC_NC;
  20003. }
  20004. #endif /* !NO_SHA256 */
  20005. #ifdef WOLFSSL_SHA384
  20006. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20007. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20008. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  20009. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  20010. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  20011. "\x74\x60\x39";
  20012. e.inLen = XSTRLEN(e.input);
  20013. e.outLen = WC_SHA384_DIGEST_SIZE;
  20014. EVP_MD_CTX_init(&md_ctx);
  20015. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  20016. if (ret == WOLFSSL_SUCCESS) {
  20017. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  20018. if (ret == WOLFSSL_SUCCESS)
  20019. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20020. }
  20021. EVP_MD_CTX_cleanup(&md_ctx);
  20022. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  20023. return WC_TEST_RET_ENC_NC;
  20024. }
  20025. #endif /* WOLFSSL_SHA384 */
  20026. #ifdef WOLFSSL_SHA512
  20027. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20028. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20029. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  20030. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  20031. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  20032. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  20033. "\x87\x4b\xe9\x09";
  20034. f.inLen = XSTRLEN(f.input);
  20035. f.outLen = WC_SHA512_DIGEST_SIZE;
  20036. EVP_MD_CTX_init(&md_ctx);
  20037. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  20038. if (ret == WOLFSSL_SUCCESS) {
  20039. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  20040. if (ret == WOLFSSL_SUCCESS)
  20041. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20042. }
  20043. EVP_MD_CTX_cleanup(&md_ctx);
  20044. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  20045. return WC_TEST_RET_ENC_NC;
  20046. }
  20047. #if !defined(WOLFSSL_NOSHA512_224) && \
  20048. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  20049. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20050. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20051. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  20052. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  20053. f.inLen = XSTRLEN(f.input);
  20054. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  20055. EVP_MD_CTX_init(&md_ctx);
  20056. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  20057. if (ret == WOLFSSL_SUCCESS) {
  20058. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  20059. if (ret == WOLFSSL_SUCCESS)
  20060. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20061. }
  20062. EVP_MD_CTX_cleanup(&md_ctx);
  20063. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  20064. return WC_TEST_RET_ENC_NC;
  20065. }
  20066. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  20067. #if !defined(WOLFSSL_NOSHA512_256) && \
  20068. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  20069. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20070. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20071. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  20072. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  20073. "\x56\x3a";
  20074. f.inLen = XSTRLEN(f.input);
  20075. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  20076. EVP_MD_CTX_init(&md_ctx);
  20077. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  20078. if (ret == WOLFSSL_SUCCESS) {
  20079. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  20080. if (ret == WOLFSSL_SUCCESS)
  20081. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20082. }
  20083. EVP_MD_CTX_cleanup(&md_ctx);
  20084. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  20085. return WC_TEST_RET_ENC_NC;
  20086. }
  20087. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  20088. #endif /* WOLFSSL_SHA512 */
  20089. #ifdef WOLFSSL_SHA3
  20090. #ifndef WOLFSSL_NOSHA3_224
  20091. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20092. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20093. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  20094. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  20095. e.inLen = XSTRLEN(e.input);
  20096. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  20097. EVP_MD_CTX_init(&md_ctx);
  20098. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  20099. if (ret == WOLFSSL_SUCCESS) {
  20100. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  20101. if (ret == WOLFSSL_SUCCESS)
  20102. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20103. }
  20104. EVP_MD_CTX_cleanup(&md_ctx);
  20105. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  20106. return WC_TEST_RET_ENC_NC;
  20107. }
  20108. #endif /* WOLFSSL_NOSHA3_224 */
  20109. #ifndef WOLFSSL_NOSHA3_256
  20110. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20111. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20112. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  20113. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  20114. "\x1d\x18";
  20115. d.inLen = XSTRLEN(d.input);
  20116. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  20117. EVP_MD_CTX_init(&md_ctx);
  20118. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  20119. if (ret == WOLFSSL_SUCCESS) {
  20120. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  20121. if (ret == WOLFSSL_SUCCESS)
  20122. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20123. }
  20124. EVP_MD_CTX_cleanup(&md_ctx);
  20125. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  20126. return WC_TEST_RET_ENC_NC;
  20127. }
  20128. #endif /* WOLFSSL_NOSHA3_256 */
  20129. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20130. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20131. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  20132. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  20133. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  20134. "\xaa\x7f\xc7";
  20135. e.inLen = XSTRLEN(e.input);
  20136. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  20137. EVP_MD_CTX_init(&md_ctx);
  20138. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  20139. if (ret == WOLFSSL_SUCCESS) {
  20140. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  20141. if (ret == WOLFSSL_SUCCESS)
  20142. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20143. }
  20144. EVP_MD_CTX_cleanup(&md_ctx);
  20145. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  20146. return WC_TEST_RET_ENC_NC;
  20147. }
  20148. #ifndef WOLFSSL_NOSHA3_512
  20149. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  20150. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  20151. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  20152. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  20153. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  20154. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  20155. "\xf2\x1d\xd1\x85";
  20156. f.inLen = XSTRLEN(f.input);
  20157. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  20158. EVP_MD_CTX_init(&md_ctx);
  20159. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  20160. if (ret == WOLFSSL_SUCCESS) {
  20161. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  20162. if (ret == WOLFSSL_SUCCESS)
  20163. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  20164. }
  20165. EVP_MD_CTX_cleanup(&md_ctx);
  20166. if (ret != WOLFSSL_SUCCESS ||
  20167. XMEMCMP(hash, f.output, f.outLen) != 0) {
  20168. return WC_TEST_RET_ENC_NC;
  20169. }
  20170. #endif /* WOLFSSL_NOSHA3_512 */
  20171. #endif /* WOLFSSL_SHA3 */
  20172. #ifndef WC_NO_RNG
  20173. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  20174. return WC_TEST_RET_ENC_NC;
  20175. #endif
  20176. #ifndef NO_MD5
  20177. c.input = "what do ya want for nothing?";
  20178. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  20179. "\x76";
  20180. c.inLen = XSTRLEN(c.input);
  20181. c.outLen = WC_MD5_DIGEST_SIZE;
  20182. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  20183. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  20184. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  20185. hash, 0) != NULL)
  20186. #else
  20187. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  20188. hash, 0) == NULL ||
  20189. XMEMCMP(hash, c.output, c.outLen) != 0)
  20190. #endif
  20191. {
  20192. return WC_TEST_RET_ENC_NC;
  20193. }
  20194. #endif /* NO_MD5 */
  20195. #ifndef NO_DES3
  20196. { /* des test */
  20197. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  20198. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  20199. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  20200. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  20201. };
  20202. byte plain[24];
  20203. byte cipher[24];
  20204. const_DES_cblock key = {
  20205. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  20206. };
  20207. DES_cblock iv = {
  20208. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  20209. };
  20210. DES_key_schedule sched;
  20211. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20212. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  20213. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  20214. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  20215. };
  20216. DES_key_sched(&key, &sched);
  20217. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  20218. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  20219. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  20220. return WC_TEST_RET_ENC_NC;
  20221. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  20222. return WC_TEST_RET_ENC_NC;
  20223. /* test changing iv */
  20224. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  20225. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  20226. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  20227. return WC_TEST_RET_ENC_NC;
  20228. } /* end des test */
  20229. #endif /* NO_DES3 */
  20230. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  20231. if ((ret = openssl_aes_test()) != 0) {
  20232. return ret;
  20233. }
  20234. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  20235. { /* evp_cipher test: EVP_aes_128_cbc */
  20236. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20237. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  20238. #else
  20239. EVP_CIPHER_CTX ctx[1];
  20240. #endif
  20241. int idx, cipherSz, plainSz;
  20242. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  20243. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  20244. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  20245. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  20246. };
  20247. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20248. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  20249. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  20250. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  20251. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  20252. };
  20253. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  20254. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  20255. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  20256. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  20257. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  20258. };
  20259. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20260. "0123456789abcdef "; /* align */
  20261. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20262. "1234567890abcdef "; /* align */
  20263. byte cipher[AES_BLOCK_SIZE * 4];
  20264. byte plain [AES_BLOCK_SIZE * 4];
  20265. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20266. if (ctx == NULL)
  20267. return MEMORY_E;
  20268. #endif
  20269. cipherSz = 0;
  20270. EVP_CIPHER_CTX_init(ctx);
  20271. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  20272. if (ret == WOLFSSL_SUCCESS) {
  20273. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  20274. if (ret == WOLFSSL_SUCCESS)
  20275. cipherSz += idx;
  20276. }
  20277. if (ret == WOLFSSL_SUCCESS) {
  20278. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  20279. if (ret == WOLFSSL_SUCCESS)
  20280. cipherSz += idx;
  20281. }
  20282. EVP_CIPHER_CTX_cleanup(ctx);
  20283. if (ret != WOLFSSL_SUCCESS)
  20284. return WC_TEST_RET_ENC_NC;
  20285. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  20286. return WC_TEST_RET_ENC_NC;
  20287. /* check partial decrypt (not enough padding for full block) */
  20288. plainSz = 0;
  20289. EVP_CIPHER_CTX_init(ctx);
  20290. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  20291. if (ret == WOLFSSL_SUCCESS) {
  20292. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  20293. if (ret == WOLFSSL_SUCCESS)
  20294. plainSz += idx;
  20295. }
  20296. if (ret == WOLFSSL_SUCCESS) {
  20297. /* this test should fail... not enough padding for full block */
  20298. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  20299. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  20300. ret = WOLFSSL_SUCCESS;
  20301. else
  20302. ret = WC_TEST_RET_ENC_NC;
  20303. }
  20304. else
  20305. ret = WC_TEST_RET_ENC_NC;
  20306. EVP_CIPHER_CTX_cleanup(ctx);
  20307. if (ret != WOLFSSL_SUCCESS)
  20308. return ret;
  20309. plainSz = 0;
  20310. EVP_CIPHER_CTX_init(ctx);
  20311. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  20312. if (ret == WOLFSSL_SUCCESS) {
  20313. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  20314. if (ret == WOLFSSL_SUCCESS)
  20315. plainSz += idx;
  20316. }
  20317. if (ret == WOLFSSL_SUCCESS) {
  20318. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  20319. if (ret == WOLFSSL_SUCCESS)
  20320. plainSz += idx;
  20321. }
  20322. EVP_CIPHER_CTX_cleanup(ctx);
  20323. if (ret != WOLFSSL_SUCCESS)
  20324. return WC_TEST_RET_ENC_NC;
  20325. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  20326. return WC_TEST_RET_ENC_NC;
  20327. cipherSz = 0;
  20328. EVP_CIPHER_CTX_init(ctx);
  20329. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  20330. if (ret == WOLFSSL_SUCCESS) {
  20331. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  20332. if (ret == WOLFSSL_SUCCESS)
  20333. cipherSz += idx;
  20334. }
  20335. if (ret == WOLFSSL_SUCCESS) {
  20336. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  20337. if (ret == WOLFSSL_SUCCESS)
  20338. cipherSz += idx;
  20339. }
  20340. EVP_CIPHER_CTX_cleanup(ctx);
  20341. if (ret != WOLFSSL_SUCCESS)
  20342. return WC_TEST_RET_ENC_NC;
  20343. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  20344. return WC_TEST_RET_ENC_NC;
  20345. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20346. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  20347. #endif
  20348. } /* end evp_cipher test: EVP_aes_128_cbc*/
  20349. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  20350. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  20351. { /* evp_cipher test: EVP_aes_256_ecb*/
  20352. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20353. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  20354. #else
  20355. EVP_CIPHER_CTX ctx[1];
  20356. #endif
  20357. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  20358. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20359. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20360. };
  20361. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20362. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  20363. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  20364. };
  20365. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  20366. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  20367. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  20368. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  20369. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  20370. };
  20371. byte cipher[AES_BLOCK_SIZE * 4];
  20372. byte plain [AES_BLOCK_SIZE * 4];
  20373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20374. if (ctx == NULL)
  20375. return MEMORY_E;
  20376. #endif
  20377. EVP_CIPHER_CTX_init(ctx);
  20378. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  20379. if (ret == WOLFSSL_SUCCESS)
  20380. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  20381. EVP_CIPHER_CTX_cleanup(ctx);
  20382. if (ret != 16)
  20383. return WC_TEST_RET_ENC_NC;
  20384. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  20385. return WC_TEST_RET_ENC_NC;
  20386. EVP_CIPHER_CTX_init(ctx);
  20387. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  20388. if (ret == WOLFSSL_SUCCESS)
  20389. ret = EVP_Cipher(ctx, plain, cipher, 16);
  20390. EVP_CIPHER_CTX_cleanup(ctx);
  20391. if (ret != 16)
  20392. return WC_TEST_RET_ENC_NC;
  20393. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  20394. return WC_TEST_RET_ENC_NC;
  20395. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20396. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  20397. #endif
  20398. } /* end evp_cipher test */
  20399. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  20400. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  20401. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  20402. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  20403. {
  20404. /* Test: AES_encrypt/decrypt/set Key */
  20405. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20406. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  20407. #ifdef HAVE_AES_DECRYPT
  20408. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  20409. #endif
  20410. #else
  20411. AES_KEY enc[1];
  20412. #ifdef HAVE_AES_DECRYPT
  20413. AES_KEY dec[1];
  20414. #endif
  20415. #endif
  20416. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  20417. {
  20418. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20419. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20420. };
  20421. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  20422. {
  20423. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  20424. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  20425. };
  20426. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20427. {
  20428. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  20429. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  20430. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  20431. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  20432. };
  20433. byte plain[sizeof(msg)];
  20434. byte cipher[sizeof(msg)];
  20435. printf("openSSL extra test\n") ;
  20436. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20437. if (enc == NULL)
  20438. return MEMORY_E;
  20439. #ifdef HAVE_AES_DECRYPT
  20440. if (dec == NULL)
  20441. return MEMORY_E;
  20442. #endif
  20443. #endif
  20444. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  20445. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  20446. AES_encrypt(msg, cipher, enc);
  20447. #ifdef HAVE_AES_DECRYPT
  20448. AES_decrypt(cipher, plain, dec);
  20449. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  20450. return WC_TEST_RET_ENC_NC;
  20451. #endif /* HAVE_AES_DECRYPT */
  20452. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  20453. return WC_TEST_RET_ENC_NC;
  20454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20455. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  20456. #ifdef HAVE_AES_DECRYPT
  20457. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  20458. #endif
  20459. #endif
  20460. }
  20461. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  20462. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  20463. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  20464. #ifdef WOLFSSL_AES_COUNTER
  20465. {
  20466. byte plainBuff [64];
  20467. byte cipherBuff[64];
  20468. #ifdef WOLFSSL_AES_128
  20469. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  20470. {
  20471. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  20472. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  20473. };
  20474. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  20475. {
  20476. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  20477. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  20478. };
  20479. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  20480. {
  20481. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20482. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20483. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20484. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20485. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20486. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20487. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20488. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20489. };
  20490. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  20491. {
  20492. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  20493. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  20494. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  20495. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  20496. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  20497. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  20498. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  20499. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  20500. };
  20501. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  20502. {
  20503. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  20504. 0xc2
  20505. };
  20506. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20507. EVP_CIPHER_CTX *p_en;
  20508. EVP_CIPHER_CTX *p_de;
  20509. #endif
  20510. #endif /* WOLFSSL_AES_128 */
  20511. #ifdef WOLFSSL_AES_192
  20512. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  20513. * NIST Special Publication 800-38A */
  20514. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  20515. {
  20516. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  20517. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  20518. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  20519. };
  20520. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  20521. {
  20522. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  20523. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  20524. };
  20525. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  20526. {
  20527. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20528. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20529. };
  20530. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  20531. {
  20532. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  20533. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  20534. };
  20535. #endif /* WOLFSSL_AES_192 */
  20536. #ifdef WOLFSSL_AES_256
  20537. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  20538. * NIST Special Publication 800-38A */
  20539. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  20540. {
  20541. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  20542. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  20543. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  20544. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  20545. };
  20546. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  20547. {
  20548. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  20549. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  20550. };
  20551. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  20552. {
  20553. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20554. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  20555. };
  20556. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  20557. {
  20558. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  20559. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  20560. };
  20561. #endif /* WOLFSSL_AES_256 */
  20562. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20563. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20564. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20565. if ((en == NULL) || (de == NULL))
  20566. return MEMORY_E;
  20567. #else
  20568. EVP_CIPHER_CTX en[1];
  20569. EVP_CIPHER_CTX de[1];
  20570. #endif
  20571. #ifdef WOLFSSL_AES_128
  20572. EVP_CIPHER_CTX_init(en);
  20573. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  20574. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20575. return WC_TEST_RET_ENC_NC;
  20576. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  20577. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20578. return WC_TEST_RET_ENC_NC;
  20579. EVP_CIPHER_CTX_init(de);
  20580. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  20581. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20582. return WC_TEST_RET_ENC_NC;
  20583. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20584. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20585. return WC_TEST_RET_ENC_NC;
  20586. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  20587. return WC_TEST_RET_ENC_NC;
  20588. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  20589. return WC_TEST_RET_ENC_NC;
  20590. EVP_CIPHER_CTX_cleanup(en);
  20591. EVP_CIPHER_CTX_cleanup(de);
  20592. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20593. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  20594. if (p_en == NULL)
  20595. return WC_TEST_RET_ENC_ERRNO;
  20596. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  20597. if (p_de == NULL)
  20598. return WC_TEST_RET_ENC_ERRNO;
  20599. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  20600. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20601. return WC_TEST_RET_ENC_NC;
  20602. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  20603. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20604. return WC_TEST_RET_ENC_NC;
  20605. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  20606. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20607. return WC_TEST_RET_ENC_NC;
  20608. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  20609. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  20610. return WC_TEST_RET_ENC_NC;
  20611. EVP_CIPHER_CTX_cleanup(p_en);
  20612. EVP_CIPHER_CTX_cleanup(p_de);
  20613. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  20614. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  20615. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  20616. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  20617. return WC_TEST_RET_ENC_NC;
  20618. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  20619. return WC_TEST_RET_ENC_NC;
  20620. EVP_CIPHER_CTX_init(en);
  20621. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  20622. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20623. return WC_TEST_RET_ENC_NC;
  20624. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  20625. return WC_TEST_RET_ENC_NC;
  20626. EVP_CIPHER_CTX_init(de);
  20627. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  20628. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  20629. return WC_TEST_RET_ENC_NC;
  20630. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  20631. return WC_TEST_RET_ENC_NC;
  20632. if (XMEMCMP(plainBuff, ctrPlain, 9))
  20633. return WC_TEST_RET_ENC_NC;
  20634. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  20635. return WC_TEST_RET_ENC_NC;
  20636. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  20637. return WC_TEST_RET_ENC_NC;
  20638. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  20639. return WC_TEST_RET_ENC_NC;
  20640. if (XMEMCMP(plainBuff, ctrPlain, 9))
  20641. return WC_TEST_RET_ENC_NC;
  20642. if (XMEMCMP(cipherBuff, oddCipher, 9))
  20643. return WC_TEST_RET_ENC_NC;
  20644. EVP_CIPHER_CTX_cleanup(en);
  20645. EVP_CIPHER_CTX_cleanup(de);
  20646. #endif /* WOLFSSL_AES_128 */
  20647. #ifdef WOLFSSL_AES_192
  20648. EVP_CIPHER_CTX_init(en);
  20649. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  20650. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  20651. return WC_TEST_RET_ENC_NC;
  20652. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  20653. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20654. return WC_TEST_RET_ENC_NC;
  20655. EVP_CIPHER_CTX_init(de);
  20656. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  20657. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  20658. return WC_TEST_RET_ENC_NC;
  20659. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  20660. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20661. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20662. return WC_TEST_RET_ENC_NC;
  20663. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  20664. return WC_TEST_RET_ENC_NC;
  20665. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  20666. return WC_TEST_RET_ENC_NC;
  20667. EVP_CIPHER_CTX_cleanup(en);
  20668. EVP_CIPHER_CTX_cleanup(de);
  20669. #endif /* WOLFSSL_AES_192 */
  20670. #ifdef WOLFSSL_AES_256
  20671. EVP_CIPHER_CTX_init(en);
  20672. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  20673. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  20674. return WC_TEST_RET_ENC_NC;
  20675. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  20676. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20677. return WC_TEST_RET_ENC_NC;
  20678. EVP_CIPHER_CTX_init(de);
  20679. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  20680. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  20681. return WC_TEST_RET_ENC_NC;
  20682. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  20683. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  20684. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  20685. return WC_TEST_RET_ENC_NC;
  20686. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  20687. return WC_TEST_RET_ENC_NC;
  20688. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  20689. return WC_TEST_RET_ENC_NC;
  20690. EVP_CIPHER_CTX_cleanup(en);
  20691. EVP_CIPHER_CTX_cleanup(de);
  20692. #endif /* WOLFSSL_AES_256 */
  20693. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20694. wolfSSL_EVP_CIPHER_CTX_free(en);
  20695. wolfSSL_EVP_CIPHER_CTX_free(de);
  20696. #endif
  20697. }
  20698. #endif /* HAVE_AES_COUNTER */
  20699. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  20700. {
  20701. /* EVP_CipherUpdate test */
  20702. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20703. {
  20704. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20705. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20706. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20707. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20708. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20709. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20710. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20711. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20712. };
  20713. byte key[] = "0123456789abcdef "; /* align */
  20714. byte iv[] = "1234567890abcdef "; /* align */
  20715. byte cipher[AES_BLOCK_SIZE * 4];
  20716. byte plain [AES_BLOCK_SIZE * 4];
  20717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20718. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20719. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20720. #else
  20721. EVP_CIPHER_CTX en[1];
  20722. EVP_CIPHER_CTX de[1];
  20723. #endif
  20724. int outlen ;
  20725. int total = 0;
  20726. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20727. if ((en == NULL) || (de == NULL))
  20728. return MEMORY_E;
  20729. #endif
  20730. EVP_CIPHER_CTX_init(en);
  20731. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20732. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20733. return WC_TEST_RET_ENC_NC;
  20734. /* openSSL compatibility, if(inlen == 0)return 1; */
  20735. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20736. (byte*)cbcPlain, 0) != 1)
  20737. return WC_TEST_RET_ENC_NC;
  20738. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20739. return WC_TEST_RET_ENC_NC;
  20740. EVP_CIPHER_CTX_init(en);
  20741. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20742. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20743. return WC_TEST_RET_ENC_NC;
  20744. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20745. (byte*)cbcPlain, 9) == 0)
  20746. return WC_TEST_RET_ENC_NC;
  20747. if(outlen != 0)
  20748. return WC_TEST_RET_ENC_NC;
  20749. total += outlen;
  20750. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20751. (byte*)&cbcPlain[9] , 9) == 0)
  20752. return WC_TEST_RET_ENC_NC;
  20753. if(outlen != 16)
  20754. return WC_TEST_RET_ENC_NC;
  20755. total += outlen;
  20756. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20757. return WC_TEST_RET_ENC_NC;
  20758. if(outlen != 16)
  20759. return WC_TEST_RET_ENC_NC;
  20760. total += outlen;
  20761. if(total != 32)
  20762. return WC_TEST_RET_ENC_NC;
  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 WC_TEST_RET_ENC_NC;
  20791. if (XMEMCMP(plain, cbcPlain, 18))
  20792. return WC_TEST_RET_ENC_NC;
  20793. total = 0;
  20794. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20795. return WC_TEST_RET_ENC_NC;
  20796. EVP_CIPHER_CTX_init(en);
  20797. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  20798. (unsigned char*)key, (unsigned char*)iv) == 0)
  20799. return WC_TEST_RET_ENC_NC;
  20800. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  20801. return WC_TEST_RET_ENC_NC;
  20802. if(outlen != 0)
  20803. return WC_TEST_RET_ENC_NC;
  20804. total += outlen;
  20805. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  20806. return WC_TEST_RET_ENC_NC;
  20807. if(outlen != 16)
  20808. return WC_TEST_RET_ENC_NC;
  20809. total += outlen;
  20810. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20811. return WC_TEST_RET_ENC_NC;
  20812. if(outlen != 16)
  20813. return WC_TEST_RET_ENC_NC;
  20814. total += outlen;
  20815. if(total != 32)
  20816. return 3438;
  20817. total = 0;
  20818. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20819. return WC_TEST_RET_ENC_NC;
  20820. EVP_CIPHER_CTX_init(de);
  20821. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  20822. (unsigned char*)key, (unsigned char*)iv) == 0)
  20823. return WC_TEST_RET_ENC_NC;
  20824. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20825. return WC_TEST_RET_ENC_NC;
  20826. if(outlen != 0)
  20827. return WC_TEST_RET_ENC_NC;
  20828. total += outlen;
  20829. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  20830. return WC_TEST_RET_ENC_NC;
  20831. if(outlen != 0)
  20832. total += outlen;
  20833. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  20834. return WC_TEST_RET_ENC_NC;
  20835. if(outlen != 16)
  20836. return WC_TEST_RET_ENC_NC;
  20837. total += outlen;
  20838. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  20839. return WC_TEST_RET_ENC_NC;
  20840. if(outlen != 2)
  20841. return WC_TEST_RET_ENC_NC;
  20842. total += outlen;
  20843. if(total != 18)
  20844. return 3447;
  20845. if (XMEMCMP(plain, cbcPlain, 18))
  20846. return WC_TEST_RET_ENC_NC;
  20847. if (EVP_CIPHER_key_length(NULL) != 0)
  20848. return WC_TEST_RET_ENC_NC;
  20849. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  20850. return WC_TEST_RET_ENC_NC;
  20851. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  20852. return WC_TEST_RET_ENC_NC;
  20853. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  20854. return WC_TEST_RET_ENC_NC;
  20855. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20856. return WC_TEST_RET_ENC_NC;
  20857. EVP_CIPHER_CTX_init(en);
  20858. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  20859. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20860. return WC_TEST_RET_ENC_NC;
  20861. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20862. return WC_TEST_RET_ENC_NC;
  20863. EVP_CIPHER_CTX_init(en);
  20864. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  20865. (unsigned char*)key, (unsigned char*)iv) == 0)
  20866. return WC_TEST_RET_ENC_NC;
  20867. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20868. return WC_TEST_RET_ENC_NC;
  20869. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20870. return WC_TEST_RET_ENC_NC;
  20871. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20872. return WC_TEST_RET_ENC_NC;
  20873. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20874. return WC_TEST_RET_ENC_NC;
  20875. EVP_CIPHER_CTX_init(de);
  20876. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  20877. (unsigned char*)key, (unsigned char*)iv) == 0)
  20878. return WC_TEST_RET_ENC_NC;
  20879. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20880. return WC_TEST_RET_ENC_NC;
  20881. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  20882. return WC_TEST_RET_ENC_NC;
  20883. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  20884. return WC_TEST_RET_ENC_NC;
  20885. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20886. return WC_TEST_RET_ENC_NC;
  20887. EVP_CIPHER_CTX_init(en);
  20888. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  20889. (unsigned char*)key, (unsigned char*)iv);
  20890. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  20891. return WC_TEST_RET_ENC_NC;
  20892. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  20893. return WC_TEST_RET_ENC_NC;
  20894. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  20895. return WC_TEST_RET_ENC_NC;
  20896. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  20897. return WC_TEST_RET_ENC_NC;
  20898. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  20899. return WC_TEST_RET_ENC_NC;
  20900. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  20901. EVP_CIPHER_CTX_set_flags(en, 42);
  20902. if (en->flags != 42)
  20903. return WC_TEST_RET_ENC_NC;
  20904. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  20905. return WC_TEST_RET_ENC_NC;
  20906. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  20907. return WC_TEST_RET_ENC_NC;
  20908. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  20909. return WC_TEST_RET_ENC_NC;
  20910. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  20911. return WC_TEST_RET_ENC_NC;
  20912. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  20913. return WC_TEST_RET_ENC_NC;
  20914. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20915. wolfSSL_EVP_CIPHER_CTX_free(en);
  20916. wolfSSL_EVP_CIPHER_CTX_free(de);
  20917. #endif
  20918. }
  20919. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  20920. #endif /* ifndef NO_AES */
  20921. return 0;
  20922. }
  20923. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  20924. {
  20925. wc_test_ret_t ret = 0;
  20926. #if !defined(NO_SHA256) && !defined(NO_SHA)
  20927. WOLFSSL_EVP_MD_CTX* ctx;
  20928. WOLFSSL_EVP_MD_CTX* ctx2;
  20929. WOLFSSL_ENTER("openSSL_evpMD_test");
  20930. ctx = EVP_MD_CTX_create();
  20931. ctx2 = EVP_MD_CTX_create();
  20932. ret = EVP_DigestInit(ctx, EVP_sha256());
  20933. if (ret != SSL_SUCCESS) {
  20934. ret = WC_TEST_RET_ENC_NC;
  20935. goto openSSL_evpMD_test_done;
  20936. }
  20937. ret = EVP_MD_CTX_copy(ctx2, ctx);
  20938. if (ret != SSL_SUCCESS) {
  20939. ret = WC_TEST_RET_ENC_NC;
  20940. goto openSSL_evpMD_test_done;
  20941. }
  20942. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  20943. ret = WC_TEST_RET_ENC_NC;
  20944. goto openSSL_evpMD_test_done;
  20945. }
  20946. ret = EVP_DigestInit(ctx, EVP_sha1());
  20947. if (ret != SSL_SUCCESS) {
  20948. ret = WC_TEST_RET_ENC_NC;
  20949. goto openSSL_evpMD_test_done;
  20950. }
  20951. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  20952. ret = WC_TEST_RET_ENC_NC;
  20953. goto openSSL_evpMD_test_done;
  20954. }
  20955. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  20956. if (ret != SSL_SUCCESS) {
  20957. ret = WC_TEST_RET_ENC_NC;
  20958. goto openSSL_evpMD_test_done;
  20959. }
  20960. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  20961. ret = WC_TEST_RET_ENC_NC;
  20962. goto openSSL_evpMD_test_done;
  20963. }
  20964. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  20965. ret = WC_TEST_RET_ENC_NC;
  20966. goto openSSL_evpMD_test_done;
  20967. }
  20968. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  20969. ret = WC_TEST_RET_ENC_NC;
  20970. goto openSSL_evpMD_test_done;
  20971. }
  20972. if (EVP_add_digest(NULL) != 0) {
  20973. ret = WC_TEST_RET_ENC_NC;
  20974. goto openSSL_evpMD_test_done;
  20975. }
  20976. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  20977. ret = WC_TEST_RET_ENC_NC;
  20978. goto openSSL_evpMD_test_done;
  20979. }
  20980. ret = 0; /* got to success state without jumping to end with a fail */
  20981. openSSL_evpMD_test_done:
  20982. EVP_MD_CTX_destroy(ctx);
  20983. EVP_MD_CTX_destroy(ctx2);
  20984. #endif /* NO_SHA256 */
  20985. return ret;
  20986. }
  20987. #ifdef DEBUG_SIGN
  20988. static void show(const char *title, const char *p, unsigned int s) {
  20989. char* i;
  20990. printf("%s: ", title);
  20991. for (i = p;
  20992. i < p + s;
  20993. printf("%c", *i), i++);
  20994. printf("\n");
  20995. }
  20996. #else
  20997. #define show(a,b,c) WC_DO_NOTHING
  20998. #endif
  20999. #define FOURK_BUFF 4096
  21000. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  21001. {
  21002. wc_test_ret_t ret = 0;
  21003. #if !defined(NO_RSA) && !defined(NO_SHA)
  21004. byte* prvTmp;
  21005. byte* pubTmp;
  21006. int prvBytes;
  21007. int pubBytes;
  21008. RSA *prvRsa = NULL;
  21009. RSA *pubRsa = NULL;
  21010. EVP_PKEY *prvPkey = NULL;
  21011. EVP_PKEY *pubPkey = NULL;
  21012. EVP_PKEY_CTX *enc = NULL;
  21013. EVP_PKEY_CTX *dec = NULL;
  21014. byte in[] = TEST_STRING;
  21015. byte out[256];
  21016. size_t outlen;
  21017. size_t keySz;
  21018. byte plain[256];
  21019. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  21020. XFILE keyFile;
  21021. XFILE keypubFile;
  21022. char cliKey[] = "./certs/client-key.der";
  21023. char cliKeypub[] = "./certs/client-keyPub.der";
  21024. #endif
  21025. WOLFSSL_ENTER("openssl_pkey0_test");
  21026. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21027. if (prvTmp == NULL)
  21028. return WC_TEST_RET_ENC_ERRNO;
  21029. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21030. if (pubTmp == NULL) {
  21031. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21032. return WC_TEST_RET_ENC_NC;
  21033. }
  21034. #ifdef USE_CERT_BUFFERS_1024
  21035. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  21036. prvBytes = sizeof_client_key_der_1024;
  21037. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  21038. pubBytes = sizeof_client_keypub_der_1024;
  21039. #elif defined(USE_CERT_BUFFERS_2048)
  21040. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  21041. prvBytes = sizeof_client_key_der_2048;
  21042. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  21043. pubBytes = sizeof_client_keypub_der_2048;
  21044. #else
  21045. keyFile = XFOPEN(cliKey, "rb");
  21046. if (!keyFile) {
  21047. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21048. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21049. err_sys("can't open ./certs/client-key.der, "
  21050. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  21051. return WC_TEST_RET_ENC_ERRNO;
  21052. }
  21053. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  21054. XFCLOSE(keyFile);
  21055. if (prvBytes == 0) {
  21056. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21057. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21058. return WC_TEST_RET_ENC_ERRNO;
  21059. }
  21060. keypubFile = XFOPEN(cliKeypub, "rb");
  21061. if (!keypubFile) {
  21062. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21063. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21064. err_sys("can't open ./certs/client-cert.der, "
  21065. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  21066. return WC_TEST_RET_ENC_ERRNO;
  21067. }
  21068. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  21069. XFCLOSE(keypubFile);
  21070. if (pubBytes == 0) {
  21071. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21072. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21073. return WC_TEST_RET_ENC_ERRNO;
  21074. }
  21075. #endif /* USE_CERT_BUFFERS */
  21076. prvRsa = wolfSSL_RSA_new();
  21077. pubRsa = wolfSSL_RSA_new();
  21078. if((prvRsa == NULL) || (pubRsa == NULL)){
  21079. printf("error with RSA_new\n");
  21080. ret = WC_TEST_RET_ENC_ERRNO;
  21081. goto openssl_pkey0_test_done;
  21082. }
  21083. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  21084. if(ret != SSL_SUCCESS){
  21085. printf("error with RSA_LoadDer_ex\n");
  21086. ret = WC_TEST_RET_ENC_EC(ret);
  21087. goto openssl_pkey0_test_done;
  21088. }
  21089. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  21090. if(ret != SSL_SUCCESS){
  21091. printf("error with RSA_LoadDer_ex\n");
  21092. ret = WC_TEST_RET_ENC_EC(ret);
  21093. goto openssl_pkey0_test_done;
  21094. }
  21095. keySz = (size_t)RSA_size(pubRsa);
  21096. prvPkey = wolfSSL_EVP_PKEY_new();
  21097. pubPkey = wolfSSL_EVP_PKEY_new();
  21098. if((prvPkey == NULL) || (pubPkey == NULL)){
  21099. printf("error with PKEY_new\n");
  21100. ret = WC_TEST_RET_ENC_NC;
  21101. goto openssl_pkey0_test_done;
  21102. }
  21103. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  21104. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  21105. if(ret != 2){
  21106. printf("error with PKEY_set1_RSA\n");
  21107. ret = WC_TEST_RET_ENC_I(ret);
  21108. goto openssl_pkey0_test_done;
  21109. }
  21110. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  21111. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  21112. if((dec == NULL)||(enc==NULL)){
  21113. printf("error with EVP_PKEY_CTX_new\n");
  21114. ret = WC_TEST_RET_ENC_NC;
  21115. goto openssl_pkey0_test_done;
  21116. }
  21117. ret = EVP_PKEY_decrypt_init(dec);
  21118. if (ret != 1) {
  21119. printf("error with decrypt init\n");
  21120. ret = WC_TEST_RET_ENC_NC;
  21121. goto openssl_pkey0_test_done;
  21122. }
  21123. ret = EVP_PKEY_encrypt_init(enc);
  21124. if (ret != 1) {
  21125. printf("error with encrypt init\n");
  21126. ret = WC_TEST_RET_ENC_NC;
  21127. goto openssl_pkey0_test_done;
  21128. }
  21129. XMEMSET(out, 0, sizeof(out));
  21130. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  21131. if (ret != 1) {
  21132. printf("error encrypting msg\n");
  21133. ret = WC_TEST_RET_ENC_NC;
  21134. goto openssl_pkey0_test_done;
  21135. }
  21136. show("encrypted msg", out, outlen);
  21137. XMEMSET(plain, 0, sizeof(plain));
  21138. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  21139. if (ret != 1) {
  21140. printf("error decrypting msg\n");
  21141. ret = WC_TEST_RET_ENC_NC;
  21142. goto openssl_pkey0_test_done;
  21143. }
  21144. show("decrypted msg", plain, outlen);
  21145. /* RSA_PKCS1_OAEP_PADDING test */
  21146. ret = EVP_PKEY_decrypt_init(dec);
  21147. if (ret != 1) {
  21148. printf("error with decrypt init\n");
  21149. ret = WC_TEST_RET_ENC_NC;
  21150. goto openssl_pkey0_test_done;
  21151. }
  21152. ret = EVP_PKEY_encrypt_init(enc);
  21153. if (ret != 1) {
  21154. printf("error with encrypt init\n");
  21155. ret = WC_TEST_RET_ENC_NC;
  21156. goto openssl_pkey0_test_done;
  21157. }
  21158. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  21159. printf("first set rsa padding error\n");
  21160. ret = WC_TEST_RET_ENC_EC(ret);
  21161. goto openssl_pkey0_test_done;
  21162. }
  21163. #ifndef HAVE_FIPS
  21164. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  21165. printf("second set rsa padding error\n");
  21166. ret = WC_TEST_RET_ENC_EC(ret);
  21167. goto openssl_pkey0_test_done;
  21168. }
  21169. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  21170. printf("third set rsa padding error\n");
  21171. ret = WC_TEST_RET_ENC_EC(ret);
  21172. goto openssl_pkey0_test_done;
  21173. }
  21174. #endif
  21175. XMEMSET(out, 0, sizeof(out));
  21176. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  21177. if (ret != 1) {
  21178. printf("error encrypting msg\n");
  21179. ret = WC_TEST_RET_ENC_NC;
  21180. goto openssl_pkey0_test_done;
  21181. }
  21182. show("encrypted msg", out, outlen);
  21183. XMEMSET(plain, 0, sizeof(plain));
  21184. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  21185. if (ret != 1) {
  21186. printf("error decrypting msg\n");
  21187. ret = WC_TEST_RET_ENC_NC;
  21188. goto openssl_pkey0_test_done;
  21189. }
  21190. show("decrypted msg", plain, outlen);
  21191. ret = 0; /* made it to this point without error then set success */
  21192. openssl_pkey0_test_done:
  21193. wolfSSL_RSA_free(prvRsa);
  21194. wolfSSL_RSA_free(pubRsa);
  21195. EVP_PKEY_free(pubPkey);
  21196. EVP_PKEY_free(prvPkey);
  21197. EVP_PKEY_CTX_free(dec);
  21198. EVP_PKEY_CTX_free(enc);
  21199. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21200. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21201. #endif /* NO_RSA */
  21202. return ret;
  21203. }
  21204. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  21205. {
  21206. wc_test_ret_t ret = 0;
  21207. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  21208. EVP_PKEY_CTX* dec = NULL;
  21209. EVP_PKEY_CTX* enc = NULL;
  21210. EVP_PKEY* pubKey = NULL;
  21211. EVP_PKEY* prvKey = NULL;
  21212. X509* x509 = NULL;
  21213. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  21214. const unsigned char* clikey;
  21215. long cliKeySz;
  21216. size_t outlen;
  21217. int keyLenBits = 2048;
  21218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21219. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21220. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21221. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21222. if ((tmp == NULL) ||
  21223. (cipher == NULL) ||
  21224. (plain == NULL)) {
  21225. ret = WC_TEST_RET_ENC_NC;
  21226. goto openssl_pkey1_test_done;
  21227. }
  21228. #else
  21229. unsigned char tmp[FOURK_BUF];
  21230. unsigned char cipher[RSA_TEST_BYTES];
  21231. unsigned char plain[RSA_TEST_BYTES];
  21232. #endif
  21233. WOLFSSL_ENTER("openssl_pkey1_test");
  21234. #if defined(USE_CERT_BUFFERS_1024)
  21235. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  21236. cliKeySz = (long)sizeof_client_key_der_1024;
  21237. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  21238. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  21239. keyLenBits = 1024;
  21240. #elif defined(USE_CERT_BUFFERS_2048)
  21241. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  21242. cliKeySz = (long)sizeof_client_key_der_2048;
  21243. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  21244. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  21245. #elif defined(USE_CERT_BUFFERS_3072)
  21246. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  21247. cliKeySz = (long)sizeof_client_key_der_3072;
  21248. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  21249. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  21250. keyLenBits = 3072;
  21251. #elif defined(USE_CERT_BUFFERS_4096)
  21252. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  21253. cliKeySz = (long)sizeof_client_key_der_4096;
  21254. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  21255. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  21256. keyLenBits = 4096;
  21257. #else
  21258. {
  21259. XFILE f;
  21260. f = XFOPEN(clientKey, "rb");
  21261. if (!f) {
  21262. err_sys("can't open ./certs/client-key.der, "
  21263. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  21264. ret = WC_TEST_RET_ENC_ERRNO;
  21265. goto openssl_pkey1_test_done;
  21266. }
  21267. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  21268. XFCLOSE(f);
  21269. if (cliKeySz == 0)
  21270. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  21271. }
  21272. /* using existing wolfSSL api to get public and private key */
  21273. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  21274. #endif /* USE_CERT_BUFFERS */
  21275. clikey = tmp;
  21276. if ((prvKey = EVP_PKEY_new()) == NULL) {
  21277. ret = WC_TEST_RET_ENC_ERRNO;
  21278. goto openssl_pkey1_test_done;
  21279. }
  21280. EVP_PKEY_free(prvKey);
  21281. prvKey = NULL;
  21282. if (x509 == NULL) {
  21283. ret = WC_TEST_RET_ENC_NC;
  21284. goto openssl_pkey1_test_done;
  21285. }
  21286. pubKey = X509_get_pubkey(x509);
  21287. if (pubKey == NULL) {
  21288. ret = WC_TEST_RET_ENC_NC;
  21289. goto openssl_pkey1_test_done;
  21290. }
  21291. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  21292. if (prvKey == NULL) {
  21293. ret = WC_TEST_RET_ENC_NC;
  21294. goto openssl_pkey1_test_done;
  21295. }
  21296. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  21297. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  21298. ret = WC_TEST_RET_ENC_NC;
  21299. goto openssl_pkey1_test_done;
  21300. }
  21301. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  21302. ret = WC_TEST_RET_ENC_NC;
  21303. goto openssl_pkey1_test_done;
  21304. }
  21305. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  21306. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  21307. if (dec == NULL || enc == NULL) {
  21308. ret = WC_TEST_RET_ENC_ERRNO;
  21309. goto openssl_pkey1_test_done;
  21310. }
  21311. if (EVP_PKEY_decrypt_init(dec) != 1) {
  21312. ret = WC_TEST_RET_ENC_NC;
  21313. goto openssl_pkey1_test_done;
  21314. }
  21315. if (EVP_PKEY_encrypt_init(enc) != 1) {
  21316. ret = WC_TEST_RET_ENC_NC;
  21317. goto openssl_pkey1_test_done;
  21318. }
  21319. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  21320. ret = WC_TEST_RET_ENC_NC;
  21321. goto openssl_pkey1_test_done;
  21322. }
  21323. #ifndef HAVE_FIPS
  21324. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  21325. ret = WC_TEST_RET_ENC_EC(ret);
  21326. goto openssl_pkey1_test_done;
  21327. }
  21328. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  21329. ret = WC_TEST_RET_ENC_EC(ret);
  21330. goto openssl_pkey1_test_done;
  21331. }
  21332. #endif
  21333. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  21334. outlen = keyLenBits/8;
  21335. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  21336. ret = WC_TEST_RET_ENC_EC(ret);
  21337. goto openssl_pkey1_test_done;
  21338. }
  21339. XMEMSET(plain, 0, RSA_TEST_BYTES);
  21340. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  21341. ret = WC_TEST_RET_ENC_NC;
  21342. goto openssl_pkey1_test_done;
  21343. }
  21344. openssl_pkey1_test_done:
  21345. if (pubKey != NULL) {
  21346. EVP_PKEY_free(pubKey);
  21347. }
  21348. if (prvKey != NULL) {
  21349. EVP_PKEY_free(prvKey);
  21350. }
  21351. if (dec != NULL) {
  21352. EVP_PKEY_CTX_free(dec);
  21353. }
  21354. if (enc != NULL) {
  21355. EVP_PKEY_CTX_free(enc);
  21356. }
  21357. if (x509 != NULL) {
  21358. X509_free(x509);
  21359. }
  21360. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21361. if (tmp != NULL)
  21362. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21363. if (cipher != NULL)
  21364. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21365. if (plain != NULL)
  21366. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21367. #endif
  21368. #endif
  21369. return ret;
  21370. }
  21371. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  21372. {
  21373. #if !defined(NO_RSA) && !defined(NO_SHA)
  21374. byte* prvTmp;
  21375. byte* pubTmp;
  21376. int prvBytes;
  21377. int pubBytes;
  21378. RSA *prvRsa;
  21379. RSA *pubRsa;
  21380. EVP_PKEY *prvPkey;
  21381. EVP_PKEY *pubPkey;
  21382. EVP_MD_CTX* sign;
  21383. EVP_MD_CTX* verf;
  21384. char msg[] = "see spot run";
  21385. unsigned char sig[256];
  21386. unsigned int sigSz;
  21387. const void* pt;
  21388. unsigned int count;
  21389. wc_test_ret_t ret, ret1, ret2;
  21390. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  21391. XFILE keyFile;
  21392. XFILE keypubFile;
  21393. char cliKey[] = "./certs/client-key.der";
  21394. char cliKeypub[] = "./certs/client-keyPub.der";
  21395. #endif
  21396. WOLFSSL_ENTER("openssl_evpSig_test");
  21397. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21398. if (prvTmp == NULL)
  21399. return WC_TEST_RET_ENC_ERRNO;
  21400. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21401. if (pubTmp == NULL) {
  21402. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21403. return WC_TEST_RET_ENC_NC;
  21404. }
  21405. #ifdef USE_CERT_BUFFERS_1024
  21406. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  21407. prvBytes = sizeof_client_key_der_1024;
  21408. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  21409. pubBytes = sizeof_client_keypub_der_1024;
  21410. #elif defined(USE_CERT_BUFFERS_2048)
  21411. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  21412. prvBytes = sizeof_client_key_der_2048;
  21413. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  21414. pubBytes = sizeof_client_keypub_der_2048;
  21415. #else
  21416. keyFile = XFOPEN(cliKey, "rb");
  21417. if (!keyFile) {
  21418. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21419. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21420. err_sys("can't open ./certs/client-key.der, "
  21421. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  21422. return WC_TEST_RET_ENC_ERRNO;
  21423. }
  21424. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  21425. XFCLOSE(keyFile);
  21426. if (prvBytes == 0) {
  21427. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21428. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21429. return WC_TEST_RET_ENC_ERRNO;
  21430. }
  21431. keypubFile = XFOPEN(cliKeypub, "rb");
  21432. if (!keypubFile) {
  21433. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21434. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21435. err_sys("can't open ./certs/client-cert.der, "
  21436. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  21437. return WC_TEST_RET_ENC_ERRNO;
  21438. }
  21439. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  21440. XFCLOSE(keypubFile);
  21441. if (pubBytes == 0) {
  21442. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21443. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21444. return WC_TEST_RET_ENC_ERRNO;
  21445. }
  21446. #endif /* USE_CERT_BUFFERS */
  21447. prvRsa = wolfSSL_RSA_new();
  21448. pubRsa = wolfSSL_RSA_new();
  21449. if((prvRsa == NULL) || (pubRsa == NULL)){
  21450. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21451. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21452. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  21453. return WC_TEST_RET_ENC_NC;
  21454. }
  21455. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  21456. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  21457. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  21458. printf("error with RSA_LoadDer_ex\n");
  21459. return WC_TEST_RET_ENC_NC;
  21460. }
  21461. prvPkey = wolfSSL_EVP_PKEY_new();
  21462. pubPkey = wolfSSL_EVP_PKEY_new();
  21463. if((prvPkey == NULL) || (pubPkey == NULL)){
  21464. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21465. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21466. printf("error with KEY_new\n");
  21467. return WC_TEST_RET_ENC_NC;
  21468. }
  21469. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  21470. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  21471. if((ret1 != 1) || (ret2 != 1)){
  21472. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21473. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21474. printf("error with EVP_PKEY_set1_RSA\n");
  21475. return WC_TEST_RET_ENC_NC;
  21476. }
  21477. /****************** sign and verify *******************/
  21478. sign = EVP_MD_CTX_create();
  21479. verf = EVP_MD_CTX_create();
  21480. if((sign == NULL)||(verf == NULL)){
  21481. printf("error with EVP_MD_CTX_create\n");
  21482. EVP_MD_CTX_destroy(sign);
  21483. EVP_MD_CTX_destroy(verf);
  21484. return WC_TEST_RET_ENC_NC;
  21485. }
  21486. ret = EVP_SignInit(sign, EVP_sha1());
  21487. if (ret != SSL_SUCCESS){
  21488. printf("error with EVP_SignInit\n");
  21489. EVP_MD_CTX_destroy(sign);
  21490. EVP_MD_CTX_destroy(verf);
  21491. return WC_TEST_RET_ENC_NC;
  21492. }
  21493. count = sizeof(msg);
  21494. show("message = ", (char *)msg, count);
  21495. /* sign */
  21496. XMEMSET(sig, 0, sizeof(sig));
  21497. pt = (const void*)msg;
  21498. ret1 = EVP_SignUpdate(sign, pt, count);
  21499. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  21500. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  21501. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21502. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21503. EVP_MD_CTX_destroy(sign);
  21504. EVP_MD_CTX_destroy(verf);
  21505. printf("error with EVP_MD_CTX_create\n");
  21506. return WC_TEST_RET_ENC_NC;
  21507. }
  21508. show("signature = ", (char *)sig, sigSz);
  21509. /* verify */
  21510. pt = (const void*)msg;
  21511. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  21512. ret2 = EVP_VerifyUpdate(verf, pt, count);
  21513. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  21514. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21515. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21516. EVP_MD_CTX_destroy(sign);
  21517. EVP_MD_CTX_destroy(verf);
  21518. printf("error with EVP_Verify\n");
  21519. return WC_TEST_RET_ENC_NC;
  21520. }
  21521. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  21522. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21523. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21524. EVP_MD_CTX_destroy(sign);
  21525. EVP_MD_CTX_destroy(verf);
  21526. printf("error with EVP_VerifyFinal\n");
  21527. return WC_TEST_RET_ENC_NC;
  21528. }
  21529. /* expect fail without update */
  21530. EVP_VerifyInit(verf, EVP_sha1());
  21531. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  21532. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21533. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21534. EVP_MD_CTX_destroy(sign);
  21535. EVP_MD_CTX_destroy(verf);
  21536. printf("EVP_VerifyInit without update not detected\n");
  21537. return WC_TEST_RET_ENC_NC;
  21538. }
  21539. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21540. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21541. EVP_MD_CTX_destroy(sign);
  21542. EVP_MD_CTX_destroy(verf);
  21543. wolfSSL_RSA_free(prvRsa);
  21544. wolfSSL_RSA_free(pubRsa);
  21545. EVP_PKEY_free(pubPkey);
  21546. EVP_PKEY_free(prvPkey);
  21547. #endif /* NO_RSA */
  21548. return 0;
  21549. }
  21550. #endif /* OPENSSL_EXTRA */
  21551. #ifndef NO_PWDBASED
  21552. #ifdef HAVE_SCRYPT
  21553. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  21554. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  21555. {
  21556. #ifdef HAVE_FIPS
  21557. /* RFC 7914 test vector keys are too short for FIPS. */
  21558. #else
  21559. wc_test_ret_t ret;
  21560. byte derived[64];
  21561. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  21562. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  21563. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  21564. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  21565. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  21566. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  21567. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  21568. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  21569. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  21570. };
  21571. #if !defined(BENCH_EMBEDDED)
  21572. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21573. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  21574. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  21575. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  21576. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  21577. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  21578. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  21579. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  21580. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  21581. };
  21582. #endif
  21583. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  21584. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  21585. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  21586. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  21587. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  21588. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  21589. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  21590. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  21591. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  21592. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  21593. };
  21594. #endif
  21595. #ifdef SCRYPT_TEST_ALL
  21596. /* Test case is very slow.
  21597. * Use for confirmation after code change or new platform.
  21598. */
  21599. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  21600. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  21601. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  21602. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  21603. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  21604. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  21605. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  21606. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  21607. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  21608. };
  21609. #endif
  21610. WOLFSSL_ENTER("scrypt_test");
  21611. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  21612. if (ret != 0)
  21613. return WC_TEST_RET_ENC_EC(ret);
  21614. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  21615. return WC_TEST_RET_ENC_NC;
  21616. #if !defined(BENCH_EMBEDDED)
  21617. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  21618. sizeof(verify2));
  21619. if (ret != 0)
  21620. return WC_TEST_RET_ENC_EC(ret);
  21621. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  21622. return WC_TEST_RET_ENC_NC;
  21623. #endif
  21624. /* Test case with parallel overflowing */
  21625. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  21626. sizeof(verify1));
  21627. if (ret != BAD_FUNC_ARG)
  21628. return WC_TEST_RET_ENC_EC(ret);
  21629. /* Don't run these test on embedded, since they use large mallocs */
  21630. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  21631. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  21632. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  21633. if (ret != 0)
  21634. return WC_TEST_RET_ENC_EC(ret);
  21635. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  21636. return WC_TEST_RET_ENC_NC;
  21637. #ifdef SCRYPT_TEST_ALL
  21638. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  21639. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  21640. if (ret != 0)
  21641. return WC_TEST_RET_ENC_EC(ret);
  21642. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  21643. return WC_TEST_RET_ENC_NC;
  21644. #endif
  21645. #else
  21646. #ifdef SCRYPT_TEST_ALL
  21647. (void)verify4;
  21648. #endif
  21649. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  21650. #if !defined(BENCH_EMBEDDED)
  21651. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  21652. 8, 16, sizeof(verify2));
  21653. if (ret != 0)
  21654. return WC_TEST_RET_ENC_EC(ret);
  21655. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  21656. return WC_TEST_RET_ENC_NC;
  21657. #endif
  21658. #endif /* !HAVE_FIPS */
  21659. return 0;
  21660. }
  21661. #endif
  21662. #ifdef HAVE_PKCS12
  21663. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  21664. {
  21665. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  21666. 0x00, 0x00 };
  21667. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  21668. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  21669. 0x00, 0x67, 0x00, 0x00 };
  21670. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  21671. byte derived[64];
  21672. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21673. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  21674. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  21675. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  21676. };
  21677. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21678. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  21679. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  21680. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  21681. };
  21682. int id = 1;
  21683. int kLen = 24;
  21684. int iterations = 1;
  21685. wc_test_ret_t ret;
  21686. WOLFSSL_ENTER("pkcs12_test");
  21687. ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  21688. iterations, kLen, WC_SHA256, id);
  21689. if (ret < 0)
  21690. return WC_TEST_RET_ENC_EC(ret);
  21691. if (XMEMCMP(derived, verify, kLen) != 0)
  21692. return WC_TEST_RET_ENC_NC;
  21693. iterations = 1000;
  21694. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  21695. iterations, kLen, WC_SHA256, id);
  21696. if (ret < 0)
  21697. return WC_TEST_RET_ENC_EC(ret);
  21698. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  21699. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  21700. if (ret < 0)
  21701. return WC_TEST_RET_ENC_EC(ret);
  21702. if (XMEMCMP(derived, verify2, 24) != 0)
  21703. return WC_TEST_RET_ENC_NC;
  21704. return 0;
  21705. }
  21706. #endif /* HAVE_PKCS12 */
  21707. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  21708. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  21709. {
  21710. char passwd[] = "passwordpassword";
  21711. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  21712. int iterations = 2048;
  21713. int kLen = 24;
  21714. byte derived[64];
  21715. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21716. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  21717. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  21718. };
  21719. wc_test_ret_t ret;
  21720. WOLFSSL_ENTER("pbkdf2_test");
  21721. ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd),
  21722. salt, (int)sizeof(salt), iterations,
  21723. kLen, WC_SHA256, HEAP_HINT, devId);
  21724. if (ret != 0)
  21725. return ret;
  21726. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  21727. return WC_TEST_RET_ENC_NC;
  21728. return 0;
  21729. }
  21730. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  21731. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  21732. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  21733. {
  21734. char passwd[] = "password";
  21735. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  21736. int iterations = 1000;
  21737. int kLen = 16;
  21738. byte derived[16];
  21739. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21740. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  21741. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  21742. };
  21743. wc_test_ret_t ret;
  21744. WOLFSSL_ENTER("pbkdf1_test");
  21745. ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  21746. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  21747. HEAP_HINT);
  21748. if (ret != 0)
  21749. return ret;
  21750. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  21751. return WC_TEST_RET_ENC_NC;
  21752. return 0;
  21753. }
  21754. #endif /* HAVE_PBKDF2 && !NO_SHA */
  21755. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  21756. {
  21757. wc_test_ret_t ret = 0;
  21758. WOLFSSL_ENTER("pwdbased_test");
  21759. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  21760. ret = pbkdf1_test();
  21761. if (ret != 0)
  21762. return ret;
  21763. #endif
  21764. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  21765. ret = pbkdf2_test();
  21766. if (ret != 0)
  21767. return ret;
  21768. #endif
  21769. #ifdef HAVE_PKCS12
  21770. ret = pkcs12_test();
  21771. if (ret != 0)
  21772. return ret;
  21773. #endif
  21774. #ifdef HAVE_SCRYPT
  21775. ret = scrypt_test();
  21776. if (ret != 0)
  21777. return ret;
  21778. #endif
  21779. return ret;
  21780. }
  21781. #endif /* NO_PWDBASED */
  21782. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  21783. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  21784. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  21785. defined(WOLFSSL_AFALG_XILINX_RSA)
  21786. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  21787. static wc_test_ret_t hkdf_test(void)
  21788. #else
  21789. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  21790. #endif
  21791. {
  21792. wc_test_ret_t ret = 0;
  21793. #if !defined(NO_SHA) || !defined(NO_SHA256)
  21794. int L;
  21795. byte okm1[42];
  21796. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21797. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21798. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  21799. #ifndef HAVE_FIPS
  21800. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21801. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  21802. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  21803. 0xf8, 0xf9 };
  21804. #endif
  21805. #ifndef NO_SHA
  21806. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  21807. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  21808. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  21809. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  21810. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  21811. 0x49, 0x18 };
  21812. #ifndef HAVE_FIPS
  21813. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  21814. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  21815. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  21816. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  21817. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  21818. 0xf8, 0x96 };
  21819. #endif
  21820. #endif /* !NO_SHA */
  21821. #ifndef NO_SHA256
  21822. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  21823. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  21824. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  21825. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  21826. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  21827. 0x96, 0xc8 };
  21828. #ifndef HAVE_FIPS
  21829. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  21830. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  21831. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  21832. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  21833. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  21834. 0x58, 0x65 };
  21835. #endif
  21836. #endif /* !NO_SHA256 */
  21837. WOLFSSL_ENTER("hkdf_test");
  21838. XMEMSET(okm1, 0, sizeof(okm1));
  21839. L = (int)sizeof(okm1);
  21840. #ifndef NO_SHA
  21841. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  21842. okm1, L);
  21843. if (ret != 0)
  21844. return WC_TEST_RET_ENC_EC(ret);
  21845. if (XMEMCMP(okm1, res1, L) != 0)
  21846. return WC_TEST_RET_ENC_NC;
  21847. #ifndef HAVE_FIPS
  21848. /* fips can't have key size under 14 bytes, salt is key too */
  21849. L = (int)sizeof(okm1);
  21850. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  21851. info1, (word32)sizeof(info1), okm1, L);
  21852. if (ret != 0)
  21853. return WC_TEST_RET_ENC_EC(ret);
  21854. if (XMEMCMP(okm1, res2, L) != 0)
  21855. return WC_TEST_RET_ENC_NC;
  21856. #endif /* HAVE_FIPS */
  21857. #endif /* !NO_SHA */
  21858. #ifndef NO_SHA256
  21859. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  21860. okm1, L);
  21861. if (ret != 0)
  21862. return WC_TEST_RET_ENC_EC(ret);
  21863. if (XMEMCMP(okm1, res3, L) != 0)
  21864. return WC_TEST_RET_ENC_NC;
  21865. #ifndef HAVE_FIPS
  21866. /* fips can't have key size under 14 bytes, salt is key too */
  21867. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  21868. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  21869. if (ret != 0)
  21870. return WC_TEST_RET_ENC_EC(ret);
  21871. if (XMEMCMP(okm1, res4, L) != 0)
  21872. return WC_TEST_RET_ENC_NC;
  21873. #endif /* HAVE_FIPS */
  21874. #endif /* !NO_SHA256 */
  21875. #endif /* !NO_SHA || !NO_SHA256 */
  21876. return ret;
  21877. }
  21878. #endif /* HAVE_HKDF */
  21879. #ifdef WOLFSSL_WOLFSSH
  21880. typedef struct {
  21881. byte hashId;
  21882. byte keyId;
  21883. const byte* k;
  21884. word32 kSz;
  21885. const byte* h;
  21886. word32 hSz;
  21887. const byte* sessionId;
  21888. word32 sessionIdSz;
  21889. const byte* expectedKey;
  21890. word32 expectedKeySz;
  21891. } SshKdfTestVector;
  21892. /** Test Vector Set #3: SHA-256 **/
  21893. static const byte sshKdfTvSet3k[] = {
  21894. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  21895. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  21896. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  21897. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  21898. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  21899. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  21900. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  21901. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  21902. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  21903. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  21904. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  21905. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  21906. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  21907. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  21908. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  21909. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  21910. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  21911. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  21912. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  21913. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  21914. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  21915. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  21916. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  21917. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  21918. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  21919. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  21920. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  21921. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  21922. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  21923. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  21924. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  21925. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  21926. };
  21927. static const byte sshKdfTvSet3h[] = {
  21928. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  21929. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  21930. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  21931. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  21932. };
  21933. static const byte sshKdfTvSet3sid[] = {
  21934. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  21935. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  21936. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  21937. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  21938. };
  21939. static const byte sshKdfTvSet3a[] = {
  21940. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  21941. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  21942. };
  21943. static const byte sshKdfTvSet3b[] = {
  21944. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  21945. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  21946. };
  21947. static const byte sshKdfTvSet3c[] = {
  21948. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  21949. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  21950. };
  21951. static const byte sshKdfTvSet3d[] = {
  21952. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  21953. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  21954. };
  21955. static const byte sshKdfTvSet3e[] = {
  21956. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  21957. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  21958. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  21959. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  21960. };
  21961. static const byte sshKdfTvSet3f[] = {
  21962. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  21963. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  21964. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  21965. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  21966. };
  21967. static const SshKdfTestVector sshKdfTestVectors[] = {
  21968. {WC_HASH_TYPE_SHA256, 'A',
  21969. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21970. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21971. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21972. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  21973. {WC_HASH_TYPE_SHA256, 'B',
  21974. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21975. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21976. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21977. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  21978. {WC_HASH_TYPE_SHA256, 'C',
  21979. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21980. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21981. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21982. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  21983. {WC_HASH_TYPE_SHA256, 'D',
  21984. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21985. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21986. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21987. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  21988. {WC_HASH_TYPE_SHA256, 'E',
  21989. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21990. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21991. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21992. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  21993. {WC_HASH_TYPE_SHA256, 'F',
  21994. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  21995. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  21996. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  21997. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  21998. };
  21999. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  22000. {
  22001. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  22002. word32 i;
  22003. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  22004. const SshKdfTestVector* tv = NULL;
  22005. wc_test_ret_t result = 0;
  22006. WOLFSSL_ENTER("sshkdf_test");
  22007. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  22008. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  22009. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  22010. cKey, tv->expectedKeySz,
  22011. tv->k, tv->kSz, tv->h, tv->hSz,
  22012. tv->sessionId, tv->sessionIdSz);
  22013. if (result != 0) {
  22014. printf("KDF: Could not derive key.\n");
  22015. result = WC_TEST_RET_ENC_EC(result);
  22016. }
  22017. else {
  22018. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  22019. printf("KDF: Calculated Key does not match Expected Key.\n");
  22020. result = WC_TEST_RET_ENC_EC(result);
  22021. }
  22022. }
  22023. if (result != 0) break;
  22024. }
  22025. return result;
  22026. }
  22027. #endif /* WOLFSSL_WOLFSSH */
  22028. #ifdef WOLFSSL_TLS13
  22029. #define TLSV13_PSK_DHE_SZ 40
  22030. typedef struct {
  22031. enum wc_HashType hashAlg;
  22032. word32 pskSz;
  22033. word32 dheSz;
  22034. byte psk[TLSV13_PSK_DHE_SZ];
  22035. byte dhe[TLSV13_PSK_DHE_SZ];
  22036. byte hashHello1[WC_MAX_DIGEST_SIZE];
  22037. byte hashHello2[WC_MAX_DIGEST_SIZE];
  22038. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  22039. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  22040. /* Expected */
  22041. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  22042. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  22043. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  22044. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  22045. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  22046. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  22047. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  22048. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  22049. } Tls13KdfTestVector;
  22050. /* The following tests come from the CAVP test vectors we used for
  22051. * our FIPS validation. The hash values used are the components from
  22052. * the test hashed together. hashHello1 is the hash of the
  22053. * clientHelloRandom value of the test vector. hashHello2 is the hash
  22054. * of the clientHelloRandom and serverHelloRandom values from the test
  22055. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  22056. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  22057. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  22058. * hashed together. */
  22059. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  22060. { /* 1 */
  22061. WC_HASH_TYPE_SHA256, 35, 35,
  22062. { /* PSK */
  22063. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  22064. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  22065. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  22066. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  22067. 0x7f, 0x6d, 0xbe
  22068. },
  22069. { /* DHE */
  22070. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  22071. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  22072. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  22073. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  22074. 0x81, 0x26, 0x40
  22075. },
  22076. { /* Hello 1 */
  22077. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  22078. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  22079. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  22080. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  22081. },
  22082. { /* Hello 2 */
  22083. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  22084. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  22085. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  22086. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  22087. },
  22088. { /* Finished 1 */
  22089. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  22090. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  22091. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  22092. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  22093. },
  22094. { /* Finished 2 */
  22095. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  22096. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  22097. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  22098. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  22099. },
  22100. { /* Client Early Traffic Secret */
  22101. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  22102. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  22103. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  22104. },
  22105. { /* Early Exporter Master Secret */
  22106. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  22107. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  22108. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  22109. },
  22110. { /* Client Handshake Traffic Secret */
  22111. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  22112. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  22113. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  22114. },
  22115. { /* Server Handshake Traffic Secret */
  22116. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  22117. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  22118. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  22119. },
  22120. { /* Client Application Traffic Secret */
  22121. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  22122. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  22123. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  22124. },
  22125. { /* Server Application Traffic Secret */
  22126. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  22127. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  22128. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  22129. },
  22130. { /* Exporter Master Secret */
  22131. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  22132. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  22133. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  22134. },
  22135. { /* Resumption Master Secret */
  22136. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  22137. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  22138. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  22139. }
  22140. },
  22141. { /* 6 */
  22142. WC_HASH_TYPE_SHA256, 0, 33,
  22143. { 0 }, /* PSK */
  22144. { /* DHE */
  22145. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  22146. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  22147. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  22148. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  22149. 0x18
  22150. },
  22151. { /* Hello 1 */
  22152. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  22153. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  22154. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  22155. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  22156. },
  22157. { /* Hello 2 */
  22158. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  22159. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  22160. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  22161. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  22162. },
  22163. { /* Finished 1 */
  22164. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  22165. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  22166. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  22167. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  22168. },
  22169. { /* Finished 2 */
  22170. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  22171. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  22172. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  22173. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  22174. },
  22175. {
  22176. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  22177. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  22178. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  22179. },
  22180. {
  22181. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  22182. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  22183. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  22184. },
  22185. {
  22186. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  22187. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  22188. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  22189. },
  22190. {
  22191. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  22192. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  22193. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  22194. },
  22195. {
  22196. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  22197. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  22198. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  22199. },
  22200. {
  22201. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  22202. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  22203. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  22204. },
  22205. {
  22206. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  22207. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  22208. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  22209. },
  22210. {
  22211. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  22212. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  22213. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  22214. }
  22215. },
  22216. { /* 11 */
  22217. WC_HASH_TYPE_SHA256, 33, 0,
  22218. { /* PSK */
  22219. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  22220. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  22221. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  22222. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  22223. 0x12
  22224. },
  22225. { 0 }, /* DHE */
  22226. { /* Hello 1 */
  22227. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  22228. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  22229. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  22230. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  22231. },
  22232. { /* Hello 2 */
  22233. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  22234. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  22235. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  22236. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  22237. },
  22238. { /* Finished 1 */
  22239. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  22240. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  22241. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  22242. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  22243. },
  22244. { /* Finished 2 */
  22245. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  22246. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  22247. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  22248. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  22249. },
  22250. {
  22251. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  22252. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  22253. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  22254. {
  22255. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  22256. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  22257. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  22258. {
  22259. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  22260. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  22261. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  22262. {
  22263. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  22264. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  22265. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  22266. {
  22267. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  22268. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  22269. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  22270. {
  22271. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  22272. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  22273. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  22274. {
  22275. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  22276. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  22277. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  22278. {
  22279. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  22280. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  22281. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  22282. },
  22283. #ifdef WOLFSSL_SHA384
  22284. { /* 26 */
  22285. WC_HASH_TYPE_SHA384, 35, 35,
  22286. { /* PSK */
  22287. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  22288. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  22289. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  22290. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  22291. 0x68, 0xba, 0xf9
  22292. },
  22293. { /* DHE */
  22294. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  22295. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  22296. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  22297. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  22298. 0xd8, 0x66, 0xac
  22299. },
  22300. { /* Hello 1 */
  22301. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  22302. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  22303. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  22304. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  22305. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  22306. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  22307. },
  22308. { /* Hello 2 */
  22309. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  22310. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  22311. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  22312. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  22313. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  22314. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  22315. },
  22316. { /* Finished 1 */
  22317. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  22318. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  22319. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  22320. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  22321. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  22322. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  22323. },
  22324. { /* Finished 2 */
  22325. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  22326. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  22327. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  22328. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  22329. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  22330. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  22331. },
  22332. {
  22333. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  22334. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  22335. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  22336. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  22337. },
  22338. {
  22339. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  22340. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  22341. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  22342. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  22343. },
  22344. {
  22345. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  22346. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  22347. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  22348. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  22349. },
  22350. {
  22351. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  22352. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  22353. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  22354. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  22355. },
  22356. {
  22357. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  22358. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  22359. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  22360. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  22361. },
  22362. {
  22363. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  22364. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  22365. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  22366. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  22367. },
  22368. {
  22369. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  22370. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  22371. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  22372. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  22373. },
  22374. {
  22375. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  22376. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  22377. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  22378. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  22379. }
  22380. },
  22381. { /* 36 */
  22382. WC_HASH_TYPE_SHA384, 0, 33,
  22383. { 0 }, /* PSK */
  22384. { /* DHE */
  22385. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  22386. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  22387. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  22388. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  22389. 0xfd
  22390. },
  22391. { /* Hello 1 */
  22392. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  22393. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  22394. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  22395. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  22396. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  22397. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  22398. },
  22399. { /* Hello 2 */
  22400. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  22401. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  22402. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  22403. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  22404. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  22405. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  22406. },
  22407. { /* Finished 1 */
  22408. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  22409. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  22410. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  22411. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  22412. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  22413. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  22414. },
  22415. { /* Finished 2 */
  22416. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  22417. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  22418. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  22419. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  22420. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  22421. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  22422. },
  22423. {
  22424. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  22425. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  22426. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  22427. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  22428. },
  22429. {
  22430. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  22431. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  22432. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  22433. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  22434. },
  22435. {
  22436. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  22437. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  22438. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  22439. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  22440. },
  22441. {
  22442. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  22443. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  22444. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  22445. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  22446. },
  22447. {
  22448. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  22449. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  22450. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  22451. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  22452. },
  22453. {
  22454. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  22455. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  22456. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  22457. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  22458. },
  22459. {
  22460. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  22461. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  22462. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  22463. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  22464. },
  22465. {
  22466. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  22467. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  22468. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  22469. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  22470. }
  22471. },
  22472. { /* 41 */
  22473. WC_HASH_TYPE_SHA384, 33, 0,
  22474. { /* PSK */
  22475. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  22476. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  22477. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  22478. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  22479. 0x12
  22480. },
  22481. { 0 }, /* DHE */
  22482. { /* Hello 1 */
  22483. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  22484. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  22485. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  22486. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  22487. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  22488. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  22489. },
  22490. { /* Hello 2 */
  22491. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  22492. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  22493. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  22494. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  22495. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  22496. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  22497. },
  22498. { /* Finished 1 */
  22499. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  22500. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  22501. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  22502. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  22503. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  22504. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  22505. },
  22506. { /* Finished 2 */
  22507. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  22508. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  22509. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  22510. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  22511. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  22512. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  22513. },
  22514. {
  22515. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  22516. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  22517. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  22518. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  22519. },
  22520. {
  22521. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  22522. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  22523. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  22524. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  22525. },
  22526. {
  22527. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  22528. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  22529. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  22530. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  22531. },
  22532. {
  22533. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  22534. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  22535. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  22536. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  22537. },
  22538. {
  22539. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  22540. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  22541. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  22542. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  22543. },
  22544. {
  22545. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  22546. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  22547. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  22548. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  22549. },
  22550. {
  22551. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  22552. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  22553. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  22554. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  22555. },
  22556. {
  22557. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  22558. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  22559. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  22560. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  22561. }
  22562. },
  22563. #endif /* WOLFSSL_SHA384 */
  22564. };
  22565. static const char protocolLabel[] = "tls13 ";
  22566. static const char ceTrafficLabel[] = "c e traffic";
  22567. static const char eExpMasterLabel[] = "e exp master";
  22568. static const char cHsTrafficLabel[] = "c hs traffic";
  22569. static const char sHsTrafficLabel[] = "s hs traffic";
  22570. static const char cAppTrafficLabel[] = "c ap traffic";
  22571. static const char sAppTrafficLabel[] = "s ap traffic";
  22572. static const char expMasterLabel[] = "exp master";
  22573. static const char resMasterLabel[] = "res master";
  22574. static const char derivedLabel[] = "derived";
  22575. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  22576. {
  22577. wc_test_ret_t ret = 0;
  22578. word32 i;
  22579. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  22580. const Tls13KdfTestVector* tv = NULL;
  22581. WOLFSSL_ENTER("tls13_kdf_test");
  22582. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  22583. byte output[WC_MAX_DIGEST_SIZE];
  22584. byte secret[WC_MAX_DIGEST_SIZE];
  22585. byte salt[WC_MAX_DIGEST_SIZE];
  22586. byte zeroes[WC_MAX_DIGEST_SIZE];
  22587. byte hashZero[WC_MAX_DIGEST_SIZE];
  22588. int hashAlgSz;
  22589. XMEMSET(zeroes, 0, sizeof zeroes);
  22590. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  22591. if (hashAlgSz == BAD_FUNC_ARG) break;
  22592. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  22593. if (ret != 0) break;
  22594. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  22595. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  22596. tv->pskSz, tv->hashAlg);
  22597. if (ret != 0) break;
  22598. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22599. secret, hashAlgSz,
  22600. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22601. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  22602. tv->hashHello1, hashAlgSz, tv->hashAlg);
  22603. if (ret != 0) break;
  22604. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  22605. if (ret != 0) break;
  22606. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22607. secret, hashAlgSz,
  22608. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22609. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  22610. tv->hashHello1, hashAlgSz, tv->hashAlg);
  22611. if (ret != 0) break;
  22612. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  22613. if (ret != 0) break;
  22614. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  22615. secret, hashAlgSz,
  22616. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22617. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  22618. hashZero, hashAlgSz, tv->hashAlg);
  22619. if (ret != 0) break;
  22620. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  22621. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  22622. tv->dheSz, tv->hashAlg);
  22623. if (ret != 0) break;
  22624. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22625. secret, hashAlgSz,
  22626. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22627. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  22628. tv->hashHello2, hashAlgSz, tv->hashAlg);
  22629. if (ret != 0) break;
  22630. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  22631. output, hashAlgSz);
  22632. if (ret != 0) break;
  22633. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22634. secret, hashAlgSz,
  22635. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22636. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  22637. tv->hashHello2, hashAlgSz, tv->hashAlg);
  22638. if (ret != 0) break;
  22639. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  22640. if (ret != 0) break;
  22641. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  22642. secret, hashAlgSz,
  22643. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22644. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  22645. hashZero, hashAlgSz, tv->hashAlg);
  22646. if (ret != 0) break;
  22647. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  22648. zeroes, hashAlgSz, tv->hashAlg);
  22649. if (ret != 0) break;
  22650. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22651. secret, hashAlgSz,
  22652. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22653. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  22654. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22655. if (ret != 0) break;
  22656. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  22657. if (ret != 0) break;
  22658. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22659. secret, hashAlgSz,
  22660. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22661. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  22662. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22663. if (ret != 0) break;
  22664. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  22665. if (ret != 0) break;
  22666. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22667. secret, hashAlgSz,
  22668. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22669. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  22670. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  22671. if (ret != 0) break;
  22672. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  22673. if (ret != 0) break;
  22674. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  22675. secret, hashAlgSz,
  22676. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  22677. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  22678. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  22679. if (ret != 0) break;
  22680. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  22681. if (ret != 0) break;
  22682. }
  22683. return ret;
  22684. }
  22685. #endif /* WOLFSSL_TLS13 */
  22686. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  22687. * see print_fiducials() below.
  22688. */
  22689. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  22690. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  22691. {
  22692. wc_test_ret_t ret;
  22693. byte kek[128];
  22694. #ifndef NO_SHA
  22695. /* SHA-1, COUNT = 0
  22696. * shared secret length: 192
  22697. * SharedInfo length: 0
  22698. * key data length: 128
  22699. */
  22700. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  22701. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  22702. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  22703. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  22704. };
  22705. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  22706. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  22707. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  22708. };
  22709. #endif
  22710. #ifndef NO_SHA256
  22711. /* SHA-256, COUNT = 3
  22712. * shared secret length: 192
  22713. * SharedInfo length: 0
  22714. * key data length: 128
  22715. */
  22716. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  22717. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  22718. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  22719. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  22720. };
  22721. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  22722. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  22723. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  22724. };
  22725. #endif
  22726. #ifdef WOLFSSL_SHA512
  22727. /* SHA-512, COUNT = 0
  22728. * shared secret length: 192
  22729. * SharedInfo length: 0
  22730. * key data length: 128
  22731. */
  22732. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  22733. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  22734. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  22735. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  22736. };
  22737. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  22738. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  22739. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  22740. };
  22741. /* SHA-512, COUNT = 0
  22742. * shared secret length: 521
  22743. * SharedInfo length: 128
  22744. * key data length: 1024
  22745. */
  22746. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  22747. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  22748. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  22749. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  22750. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  22751. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  22752. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  22753. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  22754. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  22755. 0x5a, 0x2d
  22756. };
  22757. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  22758. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  22759. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  22760. };
  22761. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  22762. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  22763. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  22764. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  22765. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  22766. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  22767. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  22768. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  22769. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  22770. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  22771. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  22772. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  22773. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  22774. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  22775. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  22776. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  22777. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  22778. };
  22779. #endif
  22780. WOLFSSL_ENTER("x963kdf_test");
  22781. #ifndef NO_SHA
  22782. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  22783. kek, sizeof(verify));
  22784. if (ret != 0)
  22785. return WC_TEST_RET_ENC_EC(ret);
  22786. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  22787. return WC_TEST_RET_ENC_NC;
  22788. #endif
  22789. #ifndef NO_SHA256
  22790. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  22791. kek, sizeof(verify2));
  22792. if (ret != 0)
  22793. return WC_TEST_RET_ENC_EC(ret);
  22794. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  22795. return WC_TEST_RET_ENC_NC;
  22796. #endif
  22797. #ifdef WOLFSSL_SHA512
  22798. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  22799. kek, sizeof(verify3));
  22800. if (ret != 0)
  22801. return WC_TEST_RET_ENC_EC(ret);
  22802. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  22803. return WC_TEST_RET_ENC_NC;
  22804. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  22805. sizeof(info4), kek, sizeof(verify4));
  22806. if (ret != 0)
  22807. return WC_TEST_RET_ENC_EC(ret);
  22808. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  22809. return WC_TEST_RET_ENC_NC;
  22810. #endif
  22811. return 0;
  22812. }
  22813. #endif /* HAVE_X963_KDF */
  22814. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  22815. defined(HAVE_AESGCM)
  22816. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  22817. {
  22818. wc_test_ret_t ret = 0;
  22819. int rngRet = 0;
  22820. WC_RNG rng[1];
  22821. const char* start_text = "this is a test";
  22822. const char* info_text = "info";
  22823. const char* aad_text = "aad";
  22824. byte ciphertext[MAX_HPKE_LABEL_SZ];
  22825. byte plaintext[MAX_HPKE_LABEL_SZ];
  22826. void* receiverKey = NULL;
  22827. void* ephemeralKey = NULL;
  22828. #ifdef WOLFSSL_SMALL_STACK
  22829. byte *pubKey = NULL; /* public key */
  22830. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  22831. #else
  22832. byte pubKey[HPKE_Npk_MAX]; /* public key */
  22833. word16 pubKeySz = (word16)sizeof(pubKey);
  22834. #endif
  22835. rngRet = ret = wc_InitRng(rng);
  22836. if (ret != 0)
  22837. return WC_TEST_RET_ENC_EC(ret);
  22838. #ifdef WOLFSSL_SMALL_STACK
  22839. if (ret == 0) {
  22840. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  22841. DYNAMIC_TYPE_TMP_BUFFER);
  22842. if (pubKey == NULL)
  22843. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  22844. }
  22845. #endif
  22846. /* generate the keys */
  22847. if (ret == 0) {
  22848. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  22849. if (ret != 0)
  22850. ret = WC_TEST_RET_ENC_EC(ret);
  22851. }
  22852. if (ret == 0) {
  22853. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  22854. if (ret != 0)
  22855. ret = WC_TEST_RET_ENC_EC(ret);
  22856. }
  22857. /* seal */
  22858. if (ret == 0) {
  22859. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  22860. (byte*)info_text, (word32)XSTRLEN(info_text),
  22861. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  22862. (byte*)start_text, (word32)XSTRLEN(start_text),
  22863. ciphertext);
  22864. if (ret != 0)
  22865. ret = WC_TEST_RET_ENC_EC(ret);
  22866. }
  22867. /* export ephemeral key */
  22868. if (ret == 0) {
  22869. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  22870. if (ret != 0)
  22871. ret = WC_TEST_RET_ENC_EC(ret);
  22872. }
  22873. /* open with exported ephemeral key */
  22874. if (ret == 0) {
  22875. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  22876. (byte*)info_text, (word32)XSTRLEN(info_text),
  22877. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  22878. ciphertext, (word32)XSTRLEN(start_text),
  22879. plaintext);
  22880. if (ret != 0)
  22881. ret = WC_TEST_RET_ENC_EC(ret);
  22882. }
  22883. if (ret == 0) {
  22884. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  22885. if (ret != 0)
  22886. ret = WC_TEST_RET_ENC_NC;
  22887. }
  22888. if (ephemeralKey != NULL)
  22889. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  22890. if (receiverKey != NULL)
  22891. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  22892. #ifdef WOLFSSL_SMALL_STACK
  22893. if (pubKey != NULL)
  22894. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22895. #endif
  22896. if (rngRet == 0)
  22897. wc_FreeRng(rng);
  22898. return ret;
  22899. }
  22900. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  22901. {
  22902. wc_test_ret_t ret = 0;
  22903. Hpke hpke[1];
  22904. WOLFSSL_ENTER("hpke_test");
  22905. #if defined(HAVE_ECC)
  22906. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  22907. /* p256 */
  22908. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  22909. HPKE_AES_128_GCM, NULL);
  22910. if (ret != 0)
  22911. return WC_TEST_RET_ENC_EC(ret);
  22912. ret = hpke_test_single(hpke);
  22913. if (ret != 0)
  22914. return ret;
  22915. #endif
  22916. #if defined(WOLFSSL_SHA384) && \
  22917. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  22918. /* p384 */
  22919. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  22920. HPKE_AES_128_GCM, NULL);
  22921. if (ret != 0)
  22922. return WC_TEST_RET_ENC_EC(ret);
  22923. ret = hpke_test_single(hpke);
  22924. if (ret != 0)
  22925. return ret;
  22926. #endif
  22927. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  22928. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  22929. /* p521 */
  22930. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  22931. HPKE_AES_128_GCM, NULL);
  22932. if (ret != 0)
  22933. return WC_TEST_RET_ENC_EC(ret);
  22934. ret = hpke_test_single(hpke);
  22935. if (ret != 0)
  22936. return ret;
  22937. #endif
  22938. #endif
  22939. #if defined(HAVE_CURVE25519)
  22940. /* test with curve25519 and aes256 */
  22941. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  22942. HPKE_AES_256_GCM, NULL);
  22943. if (ret != 0)
  22944. return WC_TEST_RET_ENC_EC(ret);
  22945. ret = hpke_test_single(hpke);
  22946. if (ret != 0)
  22947. return ret;
  22948. #endif
  22949. return ret;
  22950. /* x448 and chacha20 are unimplemented */
  22951. }
  22952. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  22953. #if defined(WC_SRTP_KDF)
  22954. typedef struct Srtp_Kdf_Tv {
  22955. const unsigned char* key;
  22956. word32 keySz;
  22957. const unsigned char* salt;
  22958. word32 saltSz;
  22959. int kdfIdx;
  22960. const unsigned char* index;
  22961. const unsigned char* ke;
  22962. const unsigned char* ka;
  22963. const unsigned char* ks;
  22964. const unsigned char* index_c;
  22965. const unsigned char* ke_c;
  22966. const unsigned char* ka_c;
  22967. const unsigned char* ks_c;
  22968. word32 keSz;
  22969. word32 kaSz;
  22970. word32 ksSz;
  22971. } Srtp_Kdf_Tv;
  22972. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  22973. {
  22974. wc_test_ret_t ret = 0;
  22975. /* 128-bit key, kdrIdx = -1 */
  22976. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  22977. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  22978. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  22979. };
  22980. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  22981. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  22982. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  22983. };
  22984. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  22985. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  22986. };
  22987. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  22988. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  22989. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  22990. };
  22991. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  22992. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  22993. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  22994. 0xbd, 0x32, 0x6e, 0xe4
  22995. };
  22996. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  22997. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  22998. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  22999. };
  23000. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  23001. 0x56, 0xf3, 0xf1, 0x97
  23002. };
  23003. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  23004. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  23005. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  23006. };
  23007. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  23008. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  23009. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  23010. 0xc3, 0xd8, 0x77, 0x0e
  23011. };
  23012. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  23013. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  23014. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  23015. };
  23016. /* 192-bit key, kdrIdx = 0 */
  23017. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  23018. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  23019. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  23020. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  23021. };
  23022. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  23023. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  23024. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  23025. };
  23026. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  23027. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  23028. };
  23029. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  23030. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  23031. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  23032. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  23033. };
  23034. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  23035. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  23036. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  23037. 0xd1, 0xd1, 0x47, 0x08
  23038. };
  23039. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  23040. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  23041. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  23042. };
  23043. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  23044. 0x40, 0xbf, 0xd4, 0xa9
  23045. };
  23046. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  23047. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  23048. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  23049. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  23050. };
  23051. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  23052. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  23053. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  23054. 0xf3, 0x3b, 0x8e, 0x29
  23055. };
  23056. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  23057. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  23058. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  23059. };
  23060. /* 256-bit key, kdrIdx = 1 */
  23061. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  23062. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  23063. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  23064. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  23065. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  23066. };
  23067. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  23068. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  23069. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  23070. };
  23071. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  23072. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  23073. };
  23074. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  23075. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  23076. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  23077. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  23078. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  23079. };
  23080. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  23081. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  23082. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  23083. 0x65, 0xc7, 0x50, 0xc1
  23084. };
  23085. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  23086. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  23087. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  23088. };
  23089. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  23090. 0x31, 0x2d, 0x58, 0x15
  23091. };
  23092. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  23093. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  23094. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  23095. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  23096. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  23097. };
  23098. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  23099. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  23100. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  23101. 0x35, 0x78, 0xdf, 0xa4
  23102. };
  23103. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  23104. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  23105. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  23106. };
  23107. /* 128-bit key, kdrIdx = 8 */
  23108. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  23109. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  23110. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  23111. };
  23112. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  23113. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  23114. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  23115. };
  23116. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  23117. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  23118. };
  23119. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  23120. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  23121. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  23122. };
  23123. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  23124. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  23125. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  23126. 0xea, 0x31, 0x24, 0xe6
  23127. };
  23128. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  23129. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  23130. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  23131. };
  23132. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  23133. 0x4a, 0x7d, 0xaa, 0x85
  23134. };
  23135. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  23136. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  23137. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  23138. };
  23139. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  23140. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  23141. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  23142. 0x05, 0x18, 0xe3, 0xed
  23143. };
  23144. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  23145. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  23146. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  23147. };
  23148. #define SRTP_TV_CNT 4
  23149. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  23150. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  23151. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  23152. 16, 20, 14 },
  23153. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  23154. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  23155. 24, 20, 14 },
  23156. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  23157. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  23158. 32, 20, 14 },
  23159. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  23160. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  23161. 16, 20, 14 },
  23162. };
  23163. int i;
  23164. int idx;
  23165. unsigned char keyE[32];
  23166. unsigned char keyA[20];
  23167. unsigned char keyS[14];
  23168. WOLFSSL_ENTER("srtpkdf_test");
  23169. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  23170. #ifndef WOLFSSL_AES_128
  23171. if (tv[i].keySz == AES_128_KEY_SIZE) {
  23172. continue;
  23173. }
  23174. #endif
  23175. #ifndef WOLFSSL_AES_192
  23176. if (tv[i].keySz == AES_192_KEY_SIZE) {
  23177. continue;
  23178. }
  23179. #endif
  23180. #ifndef WOLFSSL_AES_256
  23181. if (tv[i].keySz == AES_256_KEY_SIZE) {
  23182. continue;
  23183. }
  23184. #endif
  23185. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23186. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23187. keyS, tv[i].ksSz);
  23188. if (ret != 0)
  23189. return WC_TEST_RET_ENC_EC(ret);
  23190. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  23191. return WC_TEST_RET_ENC_NC;
  23192. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  23193. return WC_TEST_RET_ENC_NC;
  23194. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  23195. return WC_TEST_RET_ENC_NC;
  23196. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23197. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_ENCRYPTION,
  23198. keyE, tv[i].keSz);
  23199. if (ret != 0)
  23200. return WC_TEST_RET_ENC_EC(ret);
  23201. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  23202. return WC_TEST_RET_ENC_NC;
  23203. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23204. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_MSG_AUTH,
  23205. keyA, tv[i].kaSz);
  23206. if (ret != 0)
  23207. return WC_TEST_RET_ENC_EC(ret);
  23208. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  23209. return WC_TEST_RET_ENC_NC;
  23210. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23211. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_SALT, keyS,
  23212. tv[i].ksSz);
  23213. if (ret != 0)
  23214. return WC_TEST_RET_ENC_EC(ret);
  23215. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  23216. return WC_TEST_RET_ENC_NC;
  23217. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23218. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23219. keyS, tv[i].ksSz);
  23220. if (ret != 0)
  23221. return WC_TEST_RET_ENC_EC(ret);
  23222. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  23223. return WC_TEST_RET_ENC_NC;
  23224. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  23225. return WC_TEST_RET_ENC_NC;
  23226. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  23227. return WC_TEST_RET_ENC_NC;
  23228. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23229. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c,
  23230. WC_SRTCP_LABEL_ENCRYPTION, keyE, tv[i].keSz);
  23231. if (ret != 0)
  23232. return WC_TEST_RET_ENC_EC(ret);
  23233. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  23234. return WC_TEST_RET_ENC_NC;
  23235. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23236. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_MSG_AUTH,
  23237. keyA, tv[i].kaSz);
  23238. if (ret != 0)
  23239. return WC_TEST_RET_ENC_EC(ret);
  23240. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  23241. return WC_TEST_RET_ENC_NC;
  23242. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  23243. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_SALT,
  23244. keyS, tv[i].ksSz);
  23245. if (ret != 0)
  23246. return WC_TEST_RET_ENC_EC(ret);
  23247. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  23248. return WC_TEST_RET_ENC_NC;
  23249. }
  23250. #ifdef WOLFSSL_AES_128
  23251. i = 0;
  23252. #elif defined(WOLFSSL_AES_192)
  23253. i = 1;
  23254. #else
  23255. i = 2;
  23256. #endif
  23257. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  23258. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23259. keyS, tv[i].ksSz);
  23260. if (ret != BAD_FUNC_ARG)
  23261. return WC_TEST_RET_ENC_EC(ret);
  23262. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  23263. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23264. keyS, tv[i].ksSz);
  23265. if (ret != BAD_FUNC_ARG)
  23266. return WC_TEST_RET_ENC_EC(ret);
  23267. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  23268. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23269. keyS, tv[i].ksSz);
  23270. if (ret != BAD_FUNC_ARG)
  23271. return WC_TEST_RET_ENC_EC(ret);
  23272. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  23273. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23274. keyS, tv[i].ksSz);
  23275. if (ret != BAD_FUNC_ARG)
  23276. return WC_TEST_RET_ENC_EC(ret);
  23277. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  23278. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23279. keyS, tv[i].ksSz);
  23280. if (ret != BAD_FUNC_ARG)
  23281. return WC_TEST_RET_ENC_EC(ret);
  23282. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  23283. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23284. keyS, tv[i].ksSz);
  23285. if (ret != BAD_FUNC_ARG)
  23286. return WC_TEST_RET_ENC_EC(ret);
  23287. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23288. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23289. keyS, tv[i].ksSz);
  23290. if (ret != BAD_FUNC_ARG)
  23291. return WC_TEST_RET_ENC_EC(ret);
  23292. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23293. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23294. keyS, tv[i].ksSz);
  23295. if (ret != BAD_FUNC_ARG)
  23296. return WC_TEST_RET_ENC_EC(ret);
  23297. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  23298. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23299. keyS, tv[i].ksSz);
  23300. if (ret != BAD_FUNC_ARG)
  23301. return WC_TEST_RET_ENC_EC(ret);
  23302. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  23303. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23304. keyS, tv[i].ksSz);
  23305. if (ret != BAD_FUNC_ARG)
  23306. return WC_TEST_RET_ENC_EC(ret);
  23307. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23308. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23309. keyS, tv[i].ksSz);
  23310. if (ret != BAD_FUNC_ARG)
  23311. return WC_TEST_RET_ENC_EC(ret);
  23312. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23313. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23314. keyS, tv[i].ksSz);
  23315. if (ret != BAD_FUNC_ARG)
  23316. return WC_TEST_RET_ENC_EC(ret);
  23317. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23318. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23319. keyS, tv[i].ksSz);
  23320. if (ret != BAD_FUNC_ARG)
  23321. return WC_TEST_RET_ENC_EC(ret);
  23322. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23323. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23324. keyS, tv[i].ksSz);
  23325. if (ret != BAD_FUNC_ARG)
  23326. return WC_TEST_RET_ENC_EC(ret);
  23327. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23328. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  23329. keyS, tv[i].ksSz);
  23330. if (ret != 0)
  23331. return WC_TEST_RET_ENC_EC(ret);
  23332. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23333. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  23334. keyS, tv[i].ksSz);
  23335. if (ret != 0)
  23336. return WC_TEST_RET_ENC_EC(ret);
  23337. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23338. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  23339. keyS, tv[i].ksSz);
  23340. if (ret != 0)
  23341. return WC_TEST_RET_ENC_EC(ret);
  23342. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23343. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  23344. keyS, tv[i].ksSz);
  23345. if (ret != 0)
  23346. return WC_TEST_RET_ENC_EC(ret);
  23347. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23348. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23349. NULL, tv[i].ksSz);
  23350. if (ret != 0)
  23351. return WC_TEST_RET_ENC_EC(ret);
  23352. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  23353. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  23354. NULL, tv[i].ksSz);
  23355. if (ret != 0)
  23356. return WC_TEST_RET_ENC_EC(ret);
  23357. idx = wc_SRTP_KDF_kdr_to_idx(0);
  23358. if (idx != -1)
  23359. return WC_TEST_RET_ENC_NC;
  23360. for (i = 0; i < 32; i++) {
  23361. word32 kdr = 1U << i;
  23362. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  23363. if (idx != i)
  23364. return WC_TEST_RET_ENC_NC;
  23365. }
  23366. return 0;
  23367. }
  23368. #endif
  23369. #ifdef HAVE_ECC
  23370. /* size to use for ECC key gen tests */
  23371. #ifndef ECC_KEYGEN_SIZE
  23372. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  23373. #define ECC_KEYGEN_SIZE 32
  23374. #elif defined(HAVE_ECC384)
  23375. #define ECC_KEYGEN_SIZE 48
  23376. #elif defined(HAVE_ECC224)
  23377. #define ECC_KEYGEN_SIZE 28
  23378. #elif defined(HAVE_ECC521)
  23379. #define ECC_KEYGEN_SIZE 66
  23380. #else
  23381. #error No ECC keygen size defined for test
  23382. #endif
  23383. #endif
  23384. #ifdef BENCH_EMBEDDED
  23385. #define ECC_SHARED_SIZE 128
  23386. #else
  23387. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  23388. #endif
  23389. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  23390. #define HAVE_ECC_DETERMINISTIC_K
  23391. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  23392. #else
  23393. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  23394. #endif
  23395. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  23396. #ifdef NO_ECC_SECP
  23397. #define NO_ECC_VECTOR_TEST
  23398. #endif
  23399. #ifndef NO_ECC_VECTOR_TEST
  23400. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  23401. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  23402. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  23403. #define HAVE_ECC_VECTOR_TEST
  23404. #endif
  23405. #endif
  23406. #ifdef HAVE_ECC_VECTOR_TEST
  23407. typedef struct eccVector {
  23408. const char* msg; /* SHA-1 Encoded Message */
  23409. const char* Qx;
  23410. const char* Qy;
  23411. const char* d; /* Private Key */
  23412. const char* R;
  23413. const char* S;
  23414. const char* curveName;
  23415. word32 msgLen;
  23416. word32 keySize;
  23417. #ifndef NO_ASN
  23418. const byte* r;
  23419. word32 rSz;
  23420. const byte* s;
  23421. word32 sSz;
  23422. #endif
  23423. } eccVector;
  23424. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23425. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  23426. {
  23427. wc_test_ret_t ret = 0;
  23428. int verify = 0;
  23429. word32 sigSz;
  23430. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23431. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23432. #else
  23433. ecc_key userA[1];
  23434. #endif
  23435. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  23436. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  23437. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  23438. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  23439. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  23440. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  23441. #endif
  23442. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  23443. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  23444. WC_ALLOC_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  23445. WC_ALLOC_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  23446. WC_ALLOC_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  23447. #endif
  23448. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  23449. if (sig == NULL)
  23450. ERROR_OUT(MEMORY_E, done);
  23451. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  23452. if (sigRaw == NULL || r == NULL || s == NULL)
  23453. ERROR_OUT(MEMORY_E, done);
  23454. #endif
  23455. #endif
  23456. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23457. if (userA == NULL)
  23458. ERROR_OUT(MEMORY_E, done);
  23459. #endif
  23460. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23461. if (ret != 0)
  23462. goto done;
  23463. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  23464. vector->d, vector->curveName);
  23465. if (ret != 0)
  23466. goto done;
  23467. XMEMSET(sig, 0, ECC_SIG_SIZE);
  23468. sigSz = ECC_SIG_SIZE;
  23469. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  23470. if (ret != 0)
  23471. goto done;
  23472. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  23473. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  23474. sigRawSz = ECC_SIG_SIZE;
  23475. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  23476. sigRaw, &sigRawSz);
  23477. if (ret != 0)
  23478. goto done;
  23479. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  23480. ret = WC_TEST_RET_ENC_NC;
  23481. goto done;
  23482. }
  23483. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  23484. if (ret != 0)
  23485. goto done;
  23486. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  23487. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  23488. ret = WC_TEST_RET_ENC_NC;
  23489. goto done;
  23490. }
  23491. #endif
  23492. #ifdef HAVE_ECC_VERIFY
  23493. do {
  23494. #if defined(WOLFSSL_ASYNC_CRYPT)
  23495. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23496. #endif
  23497. if (ret == 0)
  23498. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  23499. vector->msgLen, &verify, userA);
  23500. } while (ret == WC_PENDING_E);
  23501. if (ret != 0)
  23502. goto done;
  23503. TEST_SLEEP();
  23504. if (verify != 1)
  23505. ret = WC_TEST_RET_ENC_NC;
  23506. #endif
  23507. done:
  23508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23509. if (userA != NULL) {
  23510. wc_ecc_free(userA);
  23511. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23512. }
  23513. #else
  23514. wc_ecc_free(userA);
  23515. #endif
  23516. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  23517. WC_FREE_VAR(sigRaw, HEAP_HINT);
  23518. WC_FREE_VAR(r, HEAP_HINT);
  23519. WC_FREE_VAR(s, HEAP_HINT);
  23520. #endif
  23521. WC_FREE_VAR(sig, HEAP_HINT);
  23522. return ret;
  23523. }
  23524. static wc_test_ret_t ecc_test_vector(int keySize)
  23525. {
  23526. wc_test_ret_t ret;
  23527. eccVector vec;
  23528. XMEMSET(&vec, 0, sizeof(vec));
  23529. vec.keySize = (word32)keySize;
  23530. switch(keySize) {
  23531. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  23532. case 14:
  23533. return 0;
  23534. #endif /* HAVE_ECC112 */
  23535. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  23536. case 16:
  23537. return 0;
  23538. #endif /* HAVE_ECC128 */
  23539. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  23540. case 20:
  23541. return 0;
  23542. #endif /* HAVE_ECC160 */
  23543. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  23544. case 24:
  23545. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  23546. #if 1
  23547. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  23548. vec.msgLen = 20;
  23549. #else
  23550. /* This is the raw message prior to SHA-1 */
  23551. vec.msg =
  23552. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  23553. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  23554. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  23555. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  23556. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  23557. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  23558. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  23559. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  23560. vec.msgLen = 128;
  23561. #endif
  23562. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  23563. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  23564. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  23565. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  23566. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  23567. vec.curveName = "SECP192R1";
  23568. #ifndef NO_ASN
  23569. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  23570. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  23571. vec.rSz = 24;
  23572. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  23573. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  23574. vec.sSz = 24;
  23575. #endif
  23576. break;
  23577. #endif /* HAVE_ECC192 */
  23578. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  23579. case 28:
  23580. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  23581. #if 1
  23582. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  23583. vec.msgLen = 20;
  23584. #else
  23585. /* This is the raw message prior to SHA-1 */
  23586. vec.msg =
  23587. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  23588. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  23589. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  23590. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  23591. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  23592. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  23593. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  23594. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  23595. vec.msgLen = 128;
  23596. #endif
  23597. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  23598. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  23599. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  23600. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  23601. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  23602. vec.curveName = "SECP224R1";
  23603. #ifndef NO_ASN
  23604. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  23605. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  23606. "\xbc\x16\x71\xa7";
  23607. vec.rSz = 28;
  23608. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  23609. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  23610. "\x6a\xf3\xad\x5b";
  23611. vec.sSz = 28;
  23612. #endif
  23613. break;
  23614. #endif /* HAVE_ECC224 */
  23615. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  23616. case 30:
  23617. return 0;
  23618. #endif /* HAVE_ECC239 */
  23619. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  23620. case 32:
  23621. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  23622. #if 1
  23623. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  23624. vec.msgLen = 20;
  23625. #else
  23626. /* This is the raw message prior to SHA-1 */
  23627. vec.msg =
  23628. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  23629. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  23630. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  23631. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  23632. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  23633. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  23634. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  23635. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  23636. vec.msgLen = 128;
  23637. #endif
  23638. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  23639. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  23640. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  23641. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  23642. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  23643. #ifndef NO_ASN
  23644. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  23645. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  23646. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  23647. vec.rSz = 32;
  23648. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  23649. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  23650. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  23651. vec.sSz = 32;
  23652. #endif
  23653. vec.curveName = "SECP256R1";
  23654. break;
  23655. #endif /* !NO_ECC256 */
  23656. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  23657. case 40:
  23658. return 0;
  23659. #endif /* HAVE_ECC320 */
  23660. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  23661. case 48:
  23662. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  23663. #if 1
  23664. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  23665. vec.msgLen = 20;
  23666. #else
  23667. /* This is the raw message prior to SHA-1 */
  23668. vec.msg =
  23669. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  23670. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  23671. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  23672. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  23673. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  23674. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  23675. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  23676. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  23677. vec.msgLen = 128;
  23678. #endif
  23679. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  23680. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  23681. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  23682. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  23683. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  23684. vec.curveName = "SECP384R1";
  23685. #ifndef NO_ASN
  23686. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  23687. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  23688. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  23689. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  23690. vec.rSz = 48;
  23691. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  23692. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  23693. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  23694. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  23695. vec.sSz = 48;
  23696. #endif
  23697. break;
  23698. #endif /* HAVE_ECC384 */
  23699. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  23700. case 64:
  23701. return 0;
  23702. #endif /* HAVE_ECC512 */
  23703. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  23704. case 66:
  23705. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  23706. #if 1
  23707. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  23708. vec.msgLen = 20;
  23709. #else
  23710. /* This is the raw message prior to SHA-1 */
  23711. vec.msg =
  23712. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  23713. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  23714. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  23715. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  23716. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  23717. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  23718. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  23719. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  23720. vec.msgLen = 128;
  23721. #endif
  23722. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  23723. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  23724. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  23725. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  23726. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  23727. vec.curveName = "SECP521R1";
  23728. #ifndef NO_ASN
  23729. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  23730. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  23731. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  23732. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  23733. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  23734. "\xa3\xbb\x15\x21\xbe";
  23735. vec.rSz = 65;
  23736. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  23737. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  23738. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  23739. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  23740. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  23741. "\x3d\x22\xf2\x48\x0c";
  23742. vec.sSz = 65;
  23743. #endif
  23744. break;
  23745. #endif /* HAVE_ECC521 */
  23746. default:
  23747. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  23748. }; /* Switch */
  23749. ret = ecc_test_vector_item(&vec);
  23750. if (ret < 0) {
  23751. return ret;
  23752. }
  23753. return 0;
  23754. }
  23755. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23756. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23757. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23758. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23759. #if defined(HAVE_ECC256)
  23760. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  23761. {
  23762. wc_test_ret_t ret;
  23763. #ifdef WOLFSSL_SMALL_STACK
  23764. ecc_key *key = NULL;
  23765. #else
  23766. ecc_key key[1];
  23767. #endif
  23768. int key_inited = 0;
  23769. byte sig[72];
  23770. word32 sigSz;
  23771. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23772. unsigned char hash[32];
  23773. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23774. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  23775. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23776. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  23777. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23778. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  23779. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  23780. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  23781. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  23782. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  23783. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  23784. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  23785. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  23786. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  23787. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  23788. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  23789. };
  23790. #ifdef WOLFSSL_SMALL_STACK
  23791. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23792. if (key == NULL)
  23793. return MEMORY_E;
  23794. #endif
  23795. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23796. if (ret != 0) {
  23797. goto done;
  23798. }
  23799. key_inited = 1;
  23800. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  23801. if (ret != 0) {
  23802. goto done;
  23803. }
  23804. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  23805. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  23806. if (ret != 0) {
  23807. goto done;
  23808. }
  23809. ret = wc_ecc_set_deterministic(key, 1);
  23810. if (ret != 0) {
  23811. goto done;
  23812. }
  23813. sigSz = sizeof(sig);
  23814. do {
  23815. #if defined(WOLFSSL_ASYNC_CRYPT)
  23816. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23817. #endif
  23818. if (ret == 0)
  23819. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23820. } while (ret == WC_PENDING_E);
  23821. if (ret != 0) {
  23822. goto done;
  23823. }
  23824. TEST_SLEEP();
  23825. if (sigSz != sizeof(expSig)) {
  23826. ret = WC_TEST_RET_ENC_NC;
  23827. goto done;
  23828. }
  23829. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  23830. ret = WC_TEST_RET_ENC_NC;
  23831. goto done;
  23832. }
  23833. sigSz = sizeof(sig);
  23834. do {
  23835. #if defined(WOLFSSL_ASYNC_CRYPT)
  23836. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23837. #endif
  23838. if (ret == 0)
  23839. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  23840. } while (ret == WC_PENDING_E);
  23841. if (ret != 0) {
  23842. goto done;
  23843. }
  23844. TEST_SLEEP();
  23845. done:
  23846. if (key_inited)
  23847. wc_ecc_free(key);
  23848. #ifdef WOLFSSL_SMALL_STACK
  23849. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23850. #endif
  23851. return ret;
  23852. }
  23853. #endif
  23854. #ifdef WOLFSSL_PUBLIC_MP
  23855. #if defined(HAVE_ECC384)
  23856. /* KAT from RFC6979 */
  23857. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  23858. {
  23859. wc_test_ret_t ret;
  23860. #ifdef WOLFSSL_SMALL_STACK
  23861. ecc_key *key;
  23862. mp_int *r, *s, *expR, *expS;
  23863. #else
  23864. ecc_key key[1];
  23865. mp_int r[1], s[1], expR[1], expS[1];
  23866. #endif
  23867. int key_inited = 0;
  23868. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23869. unsigned char hash[32];
  23870. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23871. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  23872. "96D5724E4C70A825F872C9EA60D2EDF5";
  23873. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23874. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  23875. "DEF8F0EA9055866064A254515480BC13";
  23876. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23877. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  23878. "288B231C3AE0D4FE7344FD2533264720";
  23879. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  23880. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  23881. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  23882. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  23883. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  23884. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  23885. #ifdef WOLFSSL_SMALL_STACK
  23886. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23887. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23888. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23889. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23890. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23891. if ((key == NULL) ||
  23892. (r == NULL) ||
  23893. (s == NULL) ||
  23894. (expR == NULL) ||
  23895. (expS == NULL))
  23896. {
  23897. ret = MEMORY_E;
  23898. goto done;
  23899. }
  23900. #endif
  23901. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  23902. if (ret != MP_OKAY) {
  23903. goto done;
  23904. }
  23905. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23906. if (ret != 0) {
  23907. goto done;
  23908. }
  23909. key_inited = 1;
  23910. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  23911. if (ret != 0) {
  23912. goto done;
  23913. }
  23914. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  23915. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  23916. if (ret != 0) {
  23917. goto done;
  23918. }
  23919. ret = wc_ecc_set_deterministic(key, 1);
  23920. if (ret != 0) {
  23921. goto done;
  23922. }
  23923. do {
  23924. #if defined(WOLFSSL_ASYNC_CRYPT)
  23925. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23926. #endif
  23927. if (ret == 0)
  23928. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  23929. } while (ret == WC_PENDING_E);
  23930. if (ret != 0) {
  23931. goto done;
  23932. }
  23933. TEST_SLEEP();
  23934. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  23935. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  23936. if (mp_cmp(r, expR) != MP_EQ) {
  23937. ret = WC_TEST_RET_ENC_NC;
  23938. }
  23939. done:
  23940. if (key_inited)
  23941. wc_ecc_free(key);
  23942. #ifdef WOLFSSL_SMALL_STACK
  23943. if (key != NULL)
  23944. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23945. if (r != NULL)
  23946. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23947. if (s != NULL)
  23948. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23949. if (expR != NULL)
  23950. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23951. if (expS != NULL)
  23952. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23953. #endif
  23954. return ret;
  23955. }
  23956. #endif /* HAVE_ECC384 */
  23957. #if defined(HAVE_ECC521)
  23958. /* KAT from RFC6979 */
  23959. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  23960. {
  23961. wc_test_ret_t ret;
  23962. #ifdef WOLFSSL_SMALL_STACK
  23963. ecc_key *key;
  23964. mp_int *r, *s, *expR, *expS;
  23965. #else
  23966. ecc_key key[1];
  23967. mp_int r[1], s[1], expR[1], expS[1];
  23968. #endif
  23969. int key_inited = 0;
  23970. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  23971. unsigned char hash[32];
  23972. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  23973. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  23974. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  23975. "538";
  23976. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  23977. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  23978. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  23979. "3A4";
  23980. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  23981. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  23982. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  23983. "CF5";
  23984. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  23985. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  23986. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  23987. "1A7";
  23988. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  23989. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  23990. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  23991. "CFC";
  23992. #ifdef WOLFSSL_SMALL_STACK
  23993. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23994. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23995. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23996. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23997. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23998. if ((key == NULL) ||
  23999. (r == NULL) ||
  24000. (s == NULL) ||
  24001. (expR == NULL) ||
  24002. (expS == NULL))
  24003. {
  24004. ret = MEMORY_E;
  24005. goto done;
  24006. }
  24007. #endif
  24008. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  24009. if (ret != MP_OKAY) {
  24010. goto done;
  24011. }
  24012. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  24013. if (ret != 0) {
  24014. return WC_TEST_RET_ENC_EC(ret);
  24015. }
  24016. key_inited = 1;
  24017. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  24018. if (ret != 0) {
  24019. goto done;
  24020. }
  24021. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  24022. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  24023. if (ret != 0) {
  24024. goto done;
  24025. }
  24026. ret = wc_ecc_set_deterministic(key, 1);
  24027. if (ret != 0) {
  24028. goto done;
  24029. }
  24030. do {
  24031. #if defined(WOLFSSL_ASYNC_CRYPT)
  24032. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24033. #endif
  24034. if (ret == 0)
  24035. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  24036. } while (ret == WC_PENDING_E);
  24037. if (ret != 0) {
  24038. goto done;
  24039. }
  24040. TEST_SLEEP();
  24041. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  24042. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  24043. if (mp_cmp(r, expR) != MP_EQ) {
  24044. ret = WC_TEST_RET_ENC_NC;
  24045. }
  24046. done:
  24047. if (key_inited)
  24048. wc_ecc_free(key);
  24049. #ifdef WOLFSSL_SMALL_STACK
  24050. if (key != NULL)
  24051. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24052. if (r != NULL)
  24053. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24054. if (s != NULL)
  24055. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24056. if (expR != NULL)
  24057. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24058. if (expS != NULL)
  24059. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24060. #endif
  24061. return ret;
  24062. }
  24063. #endif /* HAVE_ECC521 */
  24064. #endif /* WOLFSSL_PUBLIC_MP */
  24065. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  24066. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  24067. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  24068. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  24069. !defined(WOLFSSL_KCAPI_ECC)
  24070. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  24071. {
  24072. wc_test_ret_t ret;
  24073. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24074. ecc_key *key = NULL;
  24075. #else
  24076. ecc_key key[1];
  24077. #endif
  24078. int key_inited = 0;
  24079. byte sig[72];
  24080. word32 sigSz;
  24081. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  24082. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  24083. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  24084. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  24085. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  24086. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  24087. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  24088. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  24089. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  24090. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  24091. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  24092. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  24093. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  24094. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  24095. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  24096. };
  24097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24098. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  24099. return MEMORY_E;
  24100. #endif
  24101. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  24102. if (ret != 0) {
  24103. goto done;
  24104. }
  24105. key_inited = 1;
  24106. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  24107. if (ret != 0) {
  24108. goto done;
  24109. }
  24110. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  24111. && (HAVE_FIPS_VERSION > 2)))
  24112. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  24113. #endif
  24114. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  24115. if (ret != 0) {
  24116. goto done;
  24117. }
  24118. sigSz = sizeof(sig);
  24119. do {
  24120. #if defined(WOLFSSL_ASYNC_CRYPT)
  24121. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24122. #endif
  24123. if (ret == 0)
  24124. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  24125. } while (ret == WC_PENDING_E);
  24126. if (ret != 0) {
  24127. goto done;
  24128. }
  24129. TEST_SLEEP();
  24130. if (sigSz != sizeof(expSig)) {
  24131. ret = WC_TEST_RET_ENC_NC;
  24132. goto done;
  24133. }
  24134. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  24135. ret = WC_TEST_RET_ENC_NC;
  24136. goto done;
  24137. }
  24138. sigSz = sizeof(sig);
  24139. do {
  24140. #if defined(WOLFSSL_ASYNC_CRYPT)
  24141. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24142. #endif
  24143. if (ret == 0)
  24144. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  24145. } while (ret == WC_PENDING_E);
  24146. if (ret != 0) {
  24147. goto done;
  24148. }
  24149. TEST_SLEEP();
  24150. done:
  24151. if (key_inited)
  24152. wc_ecc_free(key);
  24153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24154. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24155. #endif
  24156. return ret;
  24157. }
  24158. #endif
  24159. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  24160. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  24161. {
  24162. wc_test_ret_t ret;
  24163. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24164. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24165. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24166. #else
  24167. ecc_key pub_key[1], priv_key[1];
  24168. #endif
  24169. byte sharedA[32] = {0}, sharedB[32] = {0};
  24170. word32 x, z;
  24171. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  24172. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  24173. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  24174. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  24175. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  24176. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  24177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24178. if ((pub_key == NULL) ||
  24179. (priv_key == NULL)) {
  24180. ret = MEMORY_E;
  24181. goto done;
  24182. }
  24183. #endif
  24184. XMEMSET(pub_key, 0, sizeof *pub_key);
  24185. XMEMSET(priv_key, 0, sizeof *priv_key);
  24186. /* setup private and public keys */
  24187. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  24188. if (ret != 0)
  24189. goto done;
  24190. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  24191. if (ret != 0)
  24192. goto done;
  24193. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  24194. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  24195. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  24196. if (ret != 0)
  24197. goto done;
  24198. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  24199. if (ret != 0)
  24200. goto done;
  24201. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24202. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24203. !defined(HAVE_SELFTEST)
  24204. ret = wc_ecc_set_rng(priv_key, rng);
  24205. if (ret != 0)
  24206. goto done;
  24207. #else
  24208. (void)rng;
  24209. #endif
  24210. /* compute ECC Cofactor shared secret */
  24211. x = sizeof(sharedA);
  24212. do {
  24213. #if defined(WOLFSSL_ASYNC_CRYPT)
  24214. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24215. #endif
  24216. if (ret == 0)
  24217. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  24218. } while (ret == WC_PENDING_E);
  24219. if (ret != 0) {
  24220. goto done;
  24221. }
  24222. TEST_SLEEP();
  24223. /* read in expected Z */
  24224. z = sizeof(sharedB);
  24225. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  24226. if (ret != 0)
  24227. goto done;
  24228. /* compare results */
  24229. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  24230. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24231. }
  24232. done:
  24233. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24234. if (priv_key) {
  24235. wc_ecc_free(priv_key);
  24236. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24237. }
  24238. if (pub_key) {
  24239. wc_ecc_free(pub_key);
  24240. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24241. }
  24242. #else
  24243. wc_ecc_free(priv_key);
  24244. wc_ecc_free(pub_key);
  24245. #endif
  24246. return ret;
  24247. }
  24248. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  24249. #endif /* HAVE_ECC_VECTOR_TEST */
  24250. #ifdef HAVE_ECC_KEY_IMPORT
  24251. /* returns 0 on success */
  24252. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  24253. {
  24254. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24255. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24256. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  24257. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24258. #endif
  24259. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24260. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24261. #else
  24262. ecc_key key[1];
  24263. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  24264. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24265. ecc_key pub[1];
  24266. #endif
  24267. byte exportBuf[ECC_BUFSIZE];
  24268. byte tmp[ECC_BUFSIZE];
  24269. #endif
  24270. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  24271. word32 x;
  24272. word32 tmpSz;
  24273. wc_test_ret_t ret = 0;
  24274. ecc_point* pubPoint = NULL;
  24275. #ifdef HAVE_ECC_VERIFY
  24276. int verify = 0;
  24277. #endif
  24278. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24279. if ((key == NULL) ||
  24280. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  24281. (pub == NULL) ||
  24282. #endif
  24283. (exportBuf == NULL) ||
  24284. (tmp == NULL))
  24285. ERROR_OUT(MEMORY_E, done);
  24286. #endif
  24287. (void)msg;
  24288. (void)verify;
  24289. (void)exportBuf;
  24290. (void)rng;
  24291. wc_ecc_init_ex(key, HEAP_HINT, devId);
  24292. #ifndef NO_ECC256
  24293. #if defined(USE_CERT_BUFFERS_256)
  24294. {
  24295. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  24296. tmpSz = (size_t)sizeof_ecc_key_der_256;
  24297. }
  24298. #elif !defined(NO_FILESYSTEM)
  24299. {
  24300. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  24301. if (!file) {
  24302. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24303. }
  24304. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  24305. XFCLOSE(file);
  24306. if (tmpSz == 0)
  24307. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24308. }
  24309. #else
  24310. {
  24311. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
  24312. ERROR_OUT(ASN_PARSE_E, done);
  24313. }
  24314. #endif /* USE_CERT_BUFFERS_256 */
  24315. /* import private only then test with */
  24316. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  24317. if (ret == 0) {
  24318. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24319. }
  24320. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  24321. if (ret == 0) {
  24322. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24323. }
  24324. x = 0;
  24325. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  24326. if (ret != 0)
  24327. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24328. #ifdef HAVE_ECC_KEY_EXPORT
  24329. x = ECC_BUFSIZE;
  24330. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  24331. if (ret != 0)
  24332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24333. /* make private only key */
  24334. wc_ecc_free(key);
  24335. wc_ecc_init_ex(key, HEAP_HINT, devId);
  24336. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  24337. if (ret != 0)
  24338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24339. x = ECC_BUFSIZE;
  24340. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  24341. if (ret == 0) {
  24342. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24343. }
  24344. #endif /* HAVE_ECC_KEY_EXPORT */
  24345. ret = wc_ecc_make_pub(NULL, NULL);
  24346. if (ret == 0) {
  24347. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24348. }
  24349. TEST_SLEEP();
  24350. #ifndef WOLFSSL_NO_MALLOC
  24351. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  24352. if (pubPoint == NULL) {
  24353. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24354. }
  24355. #if !defined(WOLFSSL_CRYPTOCELL)
  24356. ret = wc_ecc_make_pub(key, pubPoint);
  24357. #if defined(WOLFSSL_ASYNC_CRYPT)
  24358. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  24359. #endif
  24360. if (ret != 0)
  24361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24362. #endif
  24363. TEST_SLEEP();
  24364. #ifdef HAVE_ECC_KEY_EXPORT
  24365. /* export should still fail, is private only key */
  24366. x = ECC_BUFSIZE;
  24367. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  24368. if (ret == 0) {
  24369. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24370. }
  24371. #endif /* HAVE_ECC_KEY_EXPORT */
  24372. #endif /* !WOLFSSL_NO_MALLOC */
  24373. #endif /* !NO_ECC256 */
  24374. /* create a new key since above test for loading key is not supported */
  24375. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  24376. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  24377. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  24378. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  24379. if (ret != 0)
  24380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24381. #endif
  24382. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  24383. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  24384. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  24385. tmpSz = ECC_BUFSIZE;
  24386. ret = 0;
  24387. do {
  24388. #if defined(WOLFSSL_ASYNC_CRYPT)
  24389. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24390. #endif
  24391. if (ret == 0) {
  24392. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  24393. &tmpSz, rng, key);
  24394. }
  24395. } while (ret == WC_PENDING_E);
  24396. if (ret != 0)
  24397. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24398. TEST_SLEEP();
  24399. #ifdef HAVE_ECC_VERIFY
  24400. /* try verify with private only key */
  24401. ret = 0;
  24402. do {
  24403. #if defined(WOLFSSL_ASYNC_CRYPT)
  24404. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24405. #endif
  24406. if (ret == 0) {
  24407. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  24408. (word32)XSTRLEN((const char*)msg), &verify, key);
  24409. }
  24410. } while (ret == WC_PENDING_E);
  24411. if (ret != 0)
  24412. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24413. if (verify != 1) {
  24414. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24415. }
  24416. TEST_SLEEP();
  24417. #ifdef HAVE_ECC_KEY_EXPORT
  24418. /* exporting the public part should now work */
  24419. x = ECC_BUFSIZE;
  24420. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  24421. if (ret != 0)
  24422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24423. #endif /* HAVE_ECC_KEY_EXPORT */
  24424. #endif /* HAVE_ECC_VERIFY */
  24425. #endif /* HAVE_ECC_SIGN */
  24426. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24427. /* now test private only key with creating a shared secret */
  24428. x = ECC_BUFSIZE;
  24429. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  24430. if (ret != 0)
  24431. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24432. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  24433. /* make private only key */
  24434. wc_ecc_free(key);
  24435. wc_ecc_init_ex(key, HEAP_HINT, devId);
  24436. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  24437. if (ret != 0)
  24438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24439. /* check that public export fails with private only key */
  24440. x = ECC_BUFSIZE;
  24441. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  24442. if (ret == 0) {
  24443. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24444. }
  24445. #endif /* WOLFSSL_QNX_CAAM */
  24446. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  24447. /* make public key for shared secret */
  24448. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  24449. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  24450. #ifdef HAVE_ECC_CDH
  24451. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  24452. #endif
  24453. #if defined(WOLFSSL_ASYNC_CRYPT)
  24454. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  24455. #endif
  24456. if (ret != 0)
  24457. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24458. TEST_SLEEP();
  24459. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24460. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24461. !defined(HAVE_SELFTEST)
  24462. ret = wc_ecc_set_rng(key, rng);
  24463. if (ret != 0)
  24464. goto done;
  24465. #endif
  24466. x = ECC_BUFSIZE;
  24467. do {
  24468. #if defined(WOLFSSL_ASYNC_CRYPT)
  24469. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24470. #endif
  24471. if (ret == 0) {
  24472. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  24473. }
  24474. } while (ret == WC_PENDING_E);
  24475. wc_ecc_free(pub);
  24476. if (ret != 0)
  24477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24478. TEST_SLEEP();
  24479. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  24480. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  24481. ret = 0;
  24482. done:
  24483. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  24484. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24485. if (key != NULL) {
  24486. wc_ecc_free(key);
  24487. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24488. }
  24489. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  24490. if (pub != NULL)
  24491. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24492. #endif
  24493. if (exportBuf != NULL)
  24494. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24495. if (tmp != NULL)
  24496. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24497. #else
  24498. wc_ecc_free(key);
  24499. #endif
  24500. return ret;
  24501. }
  24502. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  24503. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24504. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  24505. {
  24506. wc_test_ret_t ret;
  24507. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24508. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24509. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24510. #else
  24511. ecc_key eccKey[1];
  24512. byte tmpBuf[ECC_BUFSIZE];
  24513. #endif
  24514. word32 tmpSz;
  24515. word32 idx;
  24516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24517. if ((eccKey == NULL) || (tmpBuf == NULL))
  24518. ERROR_OUT(MEMORY_E, done);
  24519. #endif
  24520. ret = wc_ecc_init(eccKey);
  24521. if (ret != 0) {
  24522. goto done;
  24523. }
  24524. ret = wc_ecc_make_key(rng, keySize, eccKey);
  24525. #if defined(WOLFSSL_ASYNC_CRYPT)
  24526. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  24527. #endif
  24528. if (ret != 0) {
  24529. goto done;
  24530. }
  24531. tmpSz = ECC_BUFSIZE;
  24532. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  24533. wc_ecc_free(eccKey);
  24534. if (ret < 0) {
  24535. goto done;
  24536. }
  24537. tmpSz = (word32)ret;
  24538. ret = wc_ecc_init(eccKey);
  24539. if (ret != 0) {
  24540. goto done;
  24541. }
  24542. idx = 0;
  24543. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  24544. if (ret != 0) {
  24545. goto done;
  24546. }
  24547. wc_ecc_free(eccKey);
  24548. ret = wc_ecc_init(eccKey);
  24549. if (ret != 0) {
  24550. goto done;
  24551. }
  24552. idx = 0;
  24553. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  24554. if (ret != 0) {
  24555. goto done;
  24556. }
  24557. ret = 0;
  24558. done:
  24559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24560. if (eccKey != NULL) {
  24561. wc_ecc_free(eccKey);
  24562. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24563. }
  24564. if (tmpBuf != NULL)
  24565. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24566. #else
  24567. wc_ecc_free(eccKey);
  24568. #endif
  24569. return ret;
  24570. }
  24571. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  24572. #endif /* HAVE_ECC_KEY_IMPORT */
  24573. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  24574. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24575. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  24576. {
  24577. wc_test_ret_t ret = 0;
  24578. int derSz;
  24579. #ifdef HAVE_PKCS8
  24580. word32 pkcs8Sz;
  24581. #endif
  24582. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24583. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24584. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24585. #else
  24586. byte der[ECC_BUFSIZE];
  24587. ecc_key userA[1];
  24588. #endif
  24589. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24590. if ((der == NULL) || (userA == NULL))
  24591. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  24592. #endif
  24593. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24594. if (ret != 0)
  24595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24596. ret = wc_ecc_make_key(rng, keySize, userA);
  24597. #if defined(WOLFSSL_ASYNC_CRYPT)
  24598. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  24599. #endif
  24600. if (ret != 0)
  24601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24602. TEST_SLEEP();
  24603. ret = wc_ecc_check_key(userA);
  24604. if (ret != 0)
  24605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24606. TEST_SLEEP();
  24607. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  24608. if (derSz < 0) {
  24609. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24610. }
  24611. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  24612. ECC_PRIVATEKEY_TYPE);
  24613. if (ret != 0)
  24614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24615. /* test export of public key */
  24616. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  24617. if (derSz < 0) {
  24618. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24619. }
  24620. if (derSz == 0) {
  24621. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24622. }
  24623. #ifdef HAVE_COMP_KEY
  24624. /* test export of compressed public key */
  24625. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  24626. if (derSz < 0) {
  24627. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24628. }
  24629. if (derSz == 0) {
  24630. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24631. }
  24632. #endif
  24633. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  24634. if (ret != 0)
  24635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24636. #ifdef HAVE_PKCS8
  24637. /* test export of PKCS#8 unencrypted private key */
  24638. pkcs8Sz = FOURK_BUF;
  24639. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  24640. if (derSz < 0) {
  24641. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  24642. }
  24643. if (derSz == 0) {
  24644. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24645. }
  24646. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  24647. if (ret != 0) {
  24648. goto done;
  24649. }
  24650. #endif /* HAVE_PKCS8 */
  24651. done:
  24652. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24653. if (der != NULL)
  24654. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24655. if (userA != NULL) {
  24656. wc_ecc_free(userA);
  24657. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24658. }
  24659. #else
  24660. wc_ecc_free(userA);
  24661. #endif
  24662. return ret;
  24663. }
  24664. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  24665. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  24666. int curve_id, const ecc_set_type* dp)
  24667. {
  24668. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  24669. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24670. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24671. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24672. word32 y;
  24673. #endif
  24674. #ifdef HAVE_ECC_KEY_EXPORT
  24675. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  24676. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  24677. #endif
  24678. word32 x = 0;
  24679. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24680. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  24681. defined(HAVE_ECC_SIGN)
  24682. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  24683. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  24684. int i;
  24685. #ifdef HAVE_ECC_VERIFY
  24686. int verify;
  24687. #endif /* HAVE_ECC_VERIFY */
  24688. #endif /* HAVE_ECC_SIGN */
  24689. wc_test_ret_t ret;
  24690. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24691. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24692. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24693. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24694. #else
  24695. ecc_key userA[1];
  24696. ecc_key userB[1];
  24697. ecc_key pubKey[1];
  24698. #endif
  24699. #ifndef WC_NO_RNG
  24700. int curveSize;
  24701. #endif
  24702. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  24703. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24704. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24705. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24706. #endif
  24707. #ifdef HAVE_ECC_KEY_EXPORT
  24708. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  24709. #endif
  24710. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24711. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  24712. defined(HAVE_ECC_SIGN)
  24713. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  24714. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  24715. #endif
  24716. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  24717. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  24718. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24719. if (sharedA == NULL || sharedB == NULL)
  24720. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24721. #endif
  24722. #ifdef HAVE_ECC_KEY_EXPORT
  24723. if (exportBuf == NULL)
  24724. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24725. #endif
  24726. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24727. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  24728. defined(HAVE_ECC_SIGN)
  24729. if (sig == NULL || digest == NULL)
  24730. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  24731. #endif
  24732. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  24733. (void)testVerifyCount;
  24734. (void)dp;
  24735. (void)x;
  24736. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24737. if ((userA == NULL) ||
  24738. (userB == NULL) ||
  24739. (pubKey == NULL))
  24740. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24741. #endif
  24742. XMEMSET(userA, 0, sizeof *userA);
  24743. XMEMSET(userB, 0, sizeof *userB);
  24744. XMEMSET(pubKey, 0, sizeof *pubKey);
  24745. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24746. if (ret != 0)
  24747. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24748. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  24749. if (ret != 0)
  24750. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24751. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24752. if (ret != 0)
  24753. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24754. #ifdef WOLFSSL_CUSTOM_CURVES
  24755. if (dp != NULL) {
  24756. ret = wc_ecc_set_custom_curve(userA, dp);
  24757. if (ret != 0)
  24758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24759. ret = wc_ecc_set_custom_curve(userB, dp);
  24760. if (ret != 0)
  24761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24762. }
  24763. #endif
  24764. #ifndef WC_NO_RNG
  24765. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  24766. #if defined(WOLFSSL_ASYNC_CRYPT)
  24767. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  24768. #endif
  24769. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  24770. if (ret == NO_VALID_DEVID) {
  24771. ret = 0;
  24772. goto done; /* no software case */
  24773. }
  24774. #endif
  24775. if (ret == ECC_CURVE_OID_E)
  24776. goto done; /* catch case, where curve is not supported */
  24777. if (ret != 0)
  24778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24779. TEST_SLEEP();
  24780. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  24781. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  24782. if (curveSize != userA->dp->size)
  24783. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24784. }
  24785. ret = wc_ecc_check_key(userA);
  24786. if (ret != 0)
  24787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24788. TEST_SLEEP();
  24789. /* ATECC508/608 configuration may not support more than one ECDH key */
  24790. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24791. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  24792. #if defined(WOLFSSL_ASYNC_CRYPT)
  24793. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  24794. #endif
  24795. if (ret != 0)
  24796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24797. TEST_SLEEP();
  24798. /* only perform the below tests if the key size matches */
  24799. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  24800. /* Not an error, just not a key size match */
  24801. WARNING_OUT(ECC_CURVE_OID_E, done);
  24802. #ifdef HAVE_ECC_DHE
  24803. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24804. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24805. !defined(HAVE_SELFTEST)
  24806. ret = wc_ecc_set_rng(userA, rng);
  24807. if (ret != 0)
  24808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24809. ret = wc_ecc_set_rng(userB, rng);
  24810. if (ret != 0)
  24811. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24812. #endif
  24813. x = ECC_SHARED_SIZE;
  24814. do {
  24815. #if defined(WOLFSSL_ASYNC_CRYPT)
  24816. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24817. #endif
  24818. if (ret == 0)
  24819. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  24820. } while (ret == WC_PENDING_E);
  24821. if (ret != 0)
  24822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24823. TEST_SLEEP();
  24824. y = ECC_SHARED_SIZE;
  24825. do {
  24826. #if defined(WOLFSSL_ASYNC_CRYPT)
  24827. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24828. #endif
  24829. if (ret == 0)
  24830. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  24831. } while (ret == WC_PENDING_E);
  24832. if (ret != 0)
  24833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24834. if (y != x)
  24835. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24836. if (XMEMCMP(sharedA, sharedB, x))
  24837. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24838. TEST_SLEEP();
  24839. #ifdef HAVE_ECC_CDH
  24840. /* add cofactor flag */
  24841. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  24842. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  24843. x = ECC_SHARED_SIZE;
  24844. do {
  24845. #if defined(WOLFSSL_ASYNC_CRYPT)
  24846. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24847. #endif
  24848. if (ret == 0)
  24849. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  24850. } while (ret == WC_PENDING_E);
  24851. if (ret != 0)
  24852. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24853. TEST_SLEEP();
  24854. y = ECC_SHARED_SIZE;
  24855. do {
  24856. #if defined(WOLFSSL_ASYNC_CRYPT)
  24857. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24858. #endif
  24859. if (ret == 0)
  24860. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  24861. } while (ret == WC_PENDING_E);
  24862. if (ret != 0)
  24863. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24864. if (y != x)
  24865. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24866. if (XMEMCMP(sharedA, sharedB, x))
  24867. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24868. TEST_SLEEP();
  24869. /* remove cofactor flag */
  24870. wc_ecc_set_flags(userA, 0);
  24871. wc_ecc_set_flags(userB, 0);
  24872. #endif /* HAVE_ECC_CDH */
  24873. #endif /* HAVE_ECC_DHE */
  24874. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  24875. #ifdef HAVE_ECC_KEY_EXPORT
  24876. x = ECC_KEY_EXPORT_BUF_SIZE;
  24877. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  24878. if (ret != 0)
  24879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24880. #ifdef HAVE_ECC_KEY_IMPORT
  24881. #ifdef WOLFSSL_CUSTOM_CURVES
  24882. if (dp != NULL) {
  24883. ret = wc_ecc_set_custom_curve(pubKey, dp);
  24884. if (ret != 0)
  24885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24886. }
  24887. #endif
  24888. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24889. if (ret != 0)
  24890. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24891. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  24892. #ifdef HAVE_ECC_DHE
  24893. y = ECC_SHARED_SIZE;
  24894. do {
  24895. #if defined(WOLFSSL_ASYNC_CRYPT)
  24896. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24897. #endif
  24898. if (ret == 0)
  24899. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  24900. } while (ret == WC_PENDING_E);
  24901. if (ret != 0)
  24902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24903. if (XMEMCMP(sharedA, sharedB, y))
  24904. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24905. TEST_SLEEP();
  24906. #endif /* HAVE_ECC_DHE */
  24907. #ifdef HAVE_COMP_KEY
  24908. /* try compressed export / import too */
  24909. x = ECC_KEY_EXPORT_BUF_SIZE;
  24910. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  24911. if (ret != 0)
  24912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24913. wc_ecc_free(pubKey);
  24914. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24915. if (ret != 0)
  24916. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24917. #ifdef WOLFSSL_CUSTOM_CURVES
  24918. if (dp != NULL) {
  24919. ret = wc_ecc_set_custom_curve(pubKey, dp);
  24920. if (ret != 0)
  24921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24922. }
  24923. #endif
  24924. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24925. if (ret != 0)
  24926. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24927. #ifdef HAVE_ECC_DHE
  24928. y = ECC_SHARED_SIZE;
  24929. do {
  24930. #if defined(WOLFSSL_ASYNC_CRYPT)
  24931. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24932. #endif
  24933. if (ret == 0)
  24934. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  24935. } while (ret == WC_PENDING_E);
  24936. if (ret != 0)
  24937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24938. if (XMEMCMP(sharedA, sharedB, y))
  24939. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24940. TEST_SLEEP();
  24941. #endif /* HAVE_ECC_DHE */
  24942. #endif /* HAVE_COMP_KEY */
  24943. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  24944. #endif /* !WC_NO_RNG */
  24945. #endif /* HAVE_ECC_KEY_IMPORT */
  24946. #endif /* HAVE_ECC_KEY_EXPORT */
  24947. /* For KCAPI cannot sign using generated ECDH key */
  24948. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24949. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  24950. #ifdef HAVE_ECC_SIGN
  24951. /* ECC w/out Shamir has issue with all 0 digest */
  24952. /* WC_BIGINT doesn't have 0 len well on hardware */
  24953. /* Cryptocell has issues with all 0 digest */
  24954. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  24955. !defined(WOLFSSL_CRYPTOCELL)
  24956. /* test DSA sign hash with zeros */
  24957. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24958. digest[i] = 0;
  24959. }
  24960. x = ECC_SIG_SIZE;
  24961. do {
  24962. #if defined(WOLFSSL_ASYNC_CRYPT)
  24963. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24964. #endif
  24965. if (ret == 0)
  24966. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  24967. userA);
  24968. } while (ret == WC_PENDING_E);
  24969. if (ret != 0)
  24970. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24971. TEST_SLEEP();
  24972. #ifdef HAVE_ECC_VERIFY
  24973. for (i=0; i<testVerifyCount; i++) {
  24974. verify = 0;
  24975. do {
  24976. #if defined(WOLFSSL_ASYNC_CRYPT)
  24977. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24978. #endif
  24979. if (ret == 0)
  24980. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  24981. &verify, userA);
  24982. } while (ret == WC_PENDING_E);
  24983. if (ret != 0)
  24984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24985. if (verify != 1)
  24986. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24987. TEST_SLEEP();
  24988. }
  24989. #endif /* HAVE_ECC_VERIFY */
  24990. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  24991. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  24992. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24993. digest[i] = (byte)i;
  24994. }
  24995. x = ECC_SIG_SIZE;
  24996. do {
  24997. #if defined(WOLFSSL_ASYNC_CRYPT)
  24998. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24999. #endif
  25000. if (ret == 0)
  25001. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  25002. } while (ret == WC_PENDING_E);
  25003. if (ret != 0)
  25004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25005. TEST_SLEEP();
  25006. #ifdef HAVE_ECC_VERIFY
  25007. for (i=0; i<testVerifyCount; i++) {
  25008. verify = 0;
  25009. do {
  25010. #if defined(WOLFSSL_ASYNC_CRYPT)
  25011. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25012. #endif
  25013. if (ret == 0)
  25014. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  25015. } while (ret == WC_PENDING_E);
  25016. if (ret != 0)
  25017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25018. if (verify != 1)
  25019. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25020. TEST_SLEEP();
  25021. }
  25022. #endif /* HAVE_ECC_VERIFY */
  25023. #endif /* HAVE_ECC_SIGN */
  25024. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  25025. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  25026. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  25027. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  25028. !defined(WOLFSSL_KCAPI_ECC)
  25029. x = ECC_KEY_EXPORT_BUF_SIZE;
  25030. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  25031. if (ret != 0)
  25032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  25033. #elif defined(HAVE_ECC_KEY_EXPORT)
  25034. (void)exportBuf;
  25035. #endif /* HAVE_ECC_KEY_EXPORT */
  25036. done:
  25037. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25038. if (userA != NULL) {
  25039. wc_ecc_free(userA);
  25040. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25041. }
  25042. if (userB != NULL) {
  25043. wc_ecc_free(userB);
  25044. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25045. }
  25046. if (pubKey != NULL) {
  25047. wc_ecc_free(pubKey);
  25048. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25049. }
  25050. #else
  25051. wc_ecc_free(pubKey);
  25052. wc_ecc_free(userB);
  25053. wc_ecc_free(userA);
  25054. #endif
  25055. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  25056. WC_FREE_VAR(sharedA, HEAP_HINT);
  25057. WC_FREE_VAR(sharedB, HEAP_HINT);
  25058. #endif
  25059. #ifdef HAVE_ECC_KEY_EXPORT
  25060. WC_FREE_VAR(exportBuf, HEAP_HINT);
  25061. #endif
  25062. #ifdef HAVE_ECC_SIGN
  25063. WC_FREE_VAR(sig, HEAP_HINT);
  25064. WC_FREE_VAR(digest, HEAP_HINT);
  25065. #endif
  25066. (void)keySize;
  25067. (void)curve_id;
  25068. (void)rng;
  25069. return ret;
  25070. }
  25071. #undef ECC_TEST_VERIFY_COUNT
  25072. #define ECC_TEST_VERIFY_COUNT 2
  25073. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  25074. {
  25075. wc_test_ret_t ret;
  25076. WOLFSSL_MSG_EX("ecc_test_curve keySize = %d", keySize);
  25077. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  25078. NULL);
  25079. if (ret < 0) {
  25080. if (ret == ECC_CURVE_OID_E) {
  25081. /* ignore error for curves not found */
  25082. /* some curve sizes are only available with:
  25083. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  25084. and HAVE_ECC_KOBLITZ */
  25085. }
  25086. else {
  25087. printf("ecc_test_curve_size %d failed!\n", keySize);
  25088. return ret;
  25089. }
  25090. }
  25091. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  25092. #ifdef HAVE_ECC_VECTOR_TEST
  25093. ret = ecc_test_vector(keySize);
  25094. if (ret < 0) {
  25095. printf("ecc_test_vector %d failed!\n", keySize);
  25096. return ret;
  25097. }
  25098. #endif
  25099. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  25100. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  25101. ret = ecc_test_key_decode(rng, keySize);
  25102. if (ret < 0) {
  25103. if (ret == ECC_CURVE_OID_E) {
  25104. /* ignore error for curves not found */
  25105. }
  25106. else {
  25107. printf("ecc_test_key_decode %d failed!\n", keySize);
  25108. return ret;
  25109. }
  25110. }
  25111. #endif
  25112. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  25113. ret = ecc_test_key_gen(rng, keySize);
  25114. if (ret < 0) {
  25115. if (ret == ECC_CURVE_OID_E) {
  25116. /* ignore error for curves not found */
  25117. }
  25118. else {
  25119. printf("ecc_test_key_gen %d failed!\n", keySize);
  25120. return ret;
  25121. }
  25122. }
  25123. #endif
  25124. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25125. return 0;
  25126. }
  25127. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25128. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  25129. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  25130. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25131. static wc_test_ret_t ecc_point_test(void)
  25132. {
  25133. wc_test_ret_t ret;
  25134. ecc_point* point;
  25135. ecc_point* point2;
  25136. #ifdef HAVE_COMP_KEY
  25137. ecc_point* point3;
  25138. ecc_point* point4;
  25139. #endif
  25140. word32 outLen;
  25141. byte out[65];
  25142. byte der[] = { 0x04, /* = Uncompressed */
  25143. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25144. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25145. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25146. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25147. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25148. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25149. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25150. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  25151. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  25152. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  25153. byte derComp0[] = { 0x02, /* = Compressed, y even */
  25154. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25155. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25156. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25157. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  25158. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  25159. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25160. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25161. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25162. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  25163. #endif
  25164. byte altDer[] = { 0x04, /* = Uncompressed */
  25165. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25166. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25167. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25168. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25169. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25170. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25171. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25172. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  25173. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  25174. /* if curve P256 is not enabled then test should not fail */
  25175. if (curve_idx == ECC_CURVE_INVALID)
  25176. return 0;
  25177. outLen = sizeof(out);
  25178. point = wc_ecc_new_point();
  25179. if (point == NULL)
  25180. return WC_TEST_RET_ENC_ERRNO;
  25181. point2 = wc_ecc_new_point();
  25182. if (point2 == NULL) {
  25183. wc_ecc_del_point(point);
  25184. return WC_TEST_RET_ENC_NC;
  25185. }
  25186. #ifdef HAVE_COMP_KEY
  25187. point3 = wc_ecc_new_point();
  25188. if (point3 == NULL) {
  25189. wc_ecc_del_point(point2);
  25190. wc_ecc_del_point(point);
  25191. return WC_TEST_RET_ENC_NC;
  25192. }
  25193. point4 = wc_ecc_new_point();
  25194. if (point4 == NULL) {
  25195. wc_ecc_del_point(point3);
  25196. wc_ecc_del_point(point2);
  25197. wc_ecc_del_point(point);
  25198. return WC_TEST_RET_ENC_NC;
  25199. }
  25200. #endif
  25201. /* Parameter Validation testing. */
  25202. wc_ecc_del_point(NULL);
  25203. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  25204. if (ret != ECC_BAD_ARG_E) {
  25205. ret = WC_TEST_RET_ENC_EC(ret);
  25206. goto done;
  25207. }
  25208. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  25209. if (ret != ECC_BAD_ARG_E) {
  25210. ret = WC_TEST_RET_ENC_EC(ret);
  25211. goto done;
  25212. }
  25213. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  25214. if (ret != ECC_BAD_ARG_E) {
  25215. ret = WC_TEST_RET_ENC_EC(ret);
  25216. goto done;
  25217. }
  25218. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  25219. if (ret != ECC_BAD_ARG_E) {
  25220. ret = WC_TEST_RET_ENC_EC(ret);
  25221. goto done;
  25222. }
  25223. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  25224. if (ret != ECC_BAD_ARG_E) {
  25225. ret = WC_TEST_RET_ENC_EC(ret);
  25226. goto done;
  25227. }
  25228. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  25229. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  25230. ret = WC_TEST_RET_ENC_EC(ret);
  25231. goto done;
  25232. }
  25233. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  25234. if (ret != ECC_BAD_ARG_E) {
  25235. ret = WC_TEST_RET_ENC_EC(ret);
  25236. goto done;
  25237. }
  25238. outLen = 0;
  25239. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  25240. if (ret != BUFFER_E) {
  25241. ret = WC_TEST_RET_ENC_EC(ret);
  25242. goto done;
  25243. }
  25244. ret = wc_ecc_copy_point(NULL, NULL);
  25245. if (ret != ECC_BAD_ARG_E) {
  25246. ret = WC_TEST_RET_ENC_EC(ret);
  25247. goto done;
  25248. }
  25249. ret = wc_ecc_copy_point(NULL, point2);
  25250. if (ret != ECC_BAD_ARG_E) {
  25251. ret = WC_TEST_RET_ENC_EC(ret);
  25252. goto done;
  25253. }
  25254. ret = wc_ecc_copy_point(point, NULL);
  25255. if (ret != ECC_BAD_ARG_E) {
  25256. ret = WC_TEST_RET_ENC_EC(ret);
  25257. goto done;
  25258. }
  25259. ret = wc_ecc_cmp_point(NULL, NULL);
  25260. if (ret != BAD_FUNC_ARG) {
  25261. ret = WC_TEST_RET_ENC_EC(ret);
  25262. goto done;
  25263. }
  25264. ret = wc_ecc_cmp_point(NULL, point2);
  25265. if (ret != BAD_FUNC_ARG) {
  25266. ret = WC_TEST_RET_ENC_EC(ret);
  25267. goto done;
  25268. }
  25269. ret = wc_ecc_cmp_point(point, NULL);
  25270. if (ret != BAD_FUNC_ARG) {
  25271. ret = WC_TEST_RET_ENC_EC(ret);
  25272. goto done;
  25273. }
  25274. /* Use API. */
  25275. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  25276. if (ret != 0) {
  25277. ret = WC_TEST_RET_ENC_EC(ret);
  25278. goto done;
  25279. }
  25280. outLen = sizeof(out);
  25281. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  25282. if (ret != 0) {
  25283. ret = WC_TEST_RET_ENC_EC(ret);
  25284. goto done;
  25285. }
  25286. if (outLen != sizeof(der)) {
  25287. ret = WC_TEST_RET_ENC_NC;
  25288. goto done;
  25289. }
  25290. if (XMEMCMP(out, der, outLen) != 0) {
  25291. ret = WC_TEST_RET_ENC_NC;
  25292. goto done;
  25293. }
  25294. ret = wc_ecc_copy_point(point2, point);
  25295. if (ret != MP_OKAY) {
  25296. ret = WC_TEST_RET_ENC_EC(ret);
  25297. goto done;
  25298. }
  25299. ret = wc_ecc_cmp_point(point2, point);
  25300. if (ret != MP_EQ) {
  25301. ret = WC_TEST_RET_ENC_EC(ret);
  25302. goto done;
  25303. }
  25304. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  25305. if (ret != 0) {
  25306. ret = WC_TEST_RET_ENC_EC(ret);
  25307. goto done;
  25308. }
  25309. ret = wc_ecc_cmp_point(point2, point);
  25310. if (ret != MP_GT) {
  25311. ret = WC_TEST_RET_ENC_EC(ret);
  25312. goto done;
  25313. }
  25314. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  25315. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  25316. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  25317. if (ret != 0) {
  25318. ret = WC_TEST_RET_ENC_EC(ret);
  25319. goto done;
  25320. }
  25321. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  25322. if (ret != 0) {
  25323. ret = WC_TEST_RET_ENC_EC(ret);
  25324. goto done;
  25325. }
  25326. ret = wc_ecc_cmp_point(point3, point4);
  25327. if (ret != MP_EQ) {
  25328. ret = WC_TEST_RET_ENC_EC(ret);
  25329. goto done;
  25330. }
  25331. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  25332. if (ret != 0) {
  25333. ret = WC_TEST_RET_ENC_EC(ret);
  25334. goto done;
  25335. }
  25336. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  25337. if (ret != 0) {
  25338. ret = WC_TEST_RET_ENC_EC(ret);
  25339. goto done;
  25340. }
  25341. ret = wc_ecc_cmp_point(point3, point4);
  25342. if (ret != MP_EQ) {
  25343. ret = WC_TEST_RET_ENC_EC(ret);
  25344. goto done;
  25345. }
  25346. #endif
  25347. done:
  25348. #ifdef HAVE_COMP_KEY
  25349. wc_ecc_del_point(point4);
  25350. wc_ecc_del_point(point3);
  25351. #endif
  25352. wc_ecc_del_point(point2);
  25353. wc_ecc_del_point(point);
  25354. return ret;
  25355. }
  25356. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  25357. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25358. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  25359. {
  25360. wc_test_ret_t ret;
  25361. word32 sigSz;
  25362. int size;
  25363. byte out[ECC_MAX_SIG_SIZE];
  25364. byte in[] = TEST_STRING;
  25365. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  25366. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  25367. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  25368. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  25369. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  25370. };
  25371. word32 inLen = (word32)XSTRLEN((char*)in);
  25372. size = wc_ecc_sig_size(key);
  25373. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  25374. if (ret != size)
  25375. return WC_TEST_RET_ENC_NC;
  25376. sigSz = (word32)ret;
  25377. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  25378. inLen, out, &sigSz, key, sizeof(*key), rng);
  25379. if (ret != 0)
  25380. return WC_TEST_RET_ENC_EC(ret);
  25381. TEST_SLEEP();
  25382. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  25383. inLen, out, sigSz, key, sizeof(*key));
  25384. if (ret != 0)
  25385. return WC_TEST_RET_ENC_EC(ret);
  25386. TEST_SLEEP();
  25387. sigSz = (word32)sizeof(out);
  25388. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  25389. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  25390. if (ret != 0)
  25391. return WC_TEST_RET_ENC_EC(ret);
  25392. TEST_SLEEP();
  25393. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  25394. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  25395. if (ret != 0)
  25396. return WC_TEST_RET_ENC_EC(ret);
  25397. TEST_SLEEP();
  25398. return 0;
  25399. }
  25400. #endif
  25401. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  25402. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25403. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  25404. {
  25405. wc_test_ret_t ret;
  25406. int curve_id;
  25407. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25408. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25409. #else
  25410. ecc_key keyImp[1];
  25411. #endif
  25412. byte priv[32];
  25413. word32 privLen;
  25414. byte pub[65*2];
  25415. word32 pubLen, pubLenX, pubLenY;
  25416. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  25417. "ce46cbc947616d0cbaa82323818a793d";
  25418. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  25419. "922f8b30ea6e8811742ac7238fe87308";
  25420. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  25421. "2e7a493ec1153b3a95feb8a4873f8d08";
  25422. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25423. if (keyImp == NULL)
  25424. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  25425. #endif
  25426. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  25427. privLen = sizeof(priv);
  25428. ret = wc_ecc_export_private_only(key, priv, &privLen);
  25429. if (ret != 0) {
  25430. ret = WC_TEST_RET_ENC_EC(ret);
  25431. goto done;
  25432. }
  25433. pubLen = sizeof(pub);
  25434. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  25435. if (ret != 0) {
  25436. ret = WC_TEST_RET_ENC_EC(ret);
  25437. goto done;
  25438. }
  25439. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  25440. if (ret != 0) {
  25441. ret = WC_TEST_RET_ENC_EC(ret);
  25442. goto done;
  25443. }
  25444. wc_ecc_free(keyImp);
  25445. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  25446. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  25447. if (ret != 0) {
  25448. ret = WC_TEST_RET_ENC_EC(ret);
  25449. goto done;
  25450. }
  25451. wc_ecc_free(keyImp);
  25452. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  25453. curve_id = wc_ecc_get_curve_id(key->idx);
  25454. if (curve_id < 0) {
  25455. ret = WC_TEST_RET_ENC_EC(curve_id);
  25456. goto done;
  25457. }
  25458. /* test import private only */
  25459. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  25460. curve_id);
  25461. if (ret != 0) {
  25462. ret = WC_TEST_RET_ENC_EC(ret);
  25463. goto done;
  25464. }
  25465. wc_ecc_free(keyImp);
  25466. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  25467. /* test export public raw */
  25468. pubLenX = pubLenY = 32;
  25469. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  25470. if (ret != 0) {
  25471. ret = WC_TEST_RET_ENC_EC(ret);
  25472. goto done;
  25473. }
  25474. #ifndef HAVE_SELFTEST
  25475. /* test import of public */
  25476. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  25477. if (ret != 0) {
  25478. ret = WC_TEST_RET_ENC_EC(ret);
  25479. goto done;
  25480. }
  25481. #endif
  25482. wc_ecc_free(keyImp);
  25483. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  25484. /* test export private and public raw */
  25485. pubLenX = pubLenY = privLen = 32;
  25486. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  25487. priv, &privLen);
  25488. if (ret != 0) {
  25489. ret = WC_TEST_RET_ENC_EC(ret);
  25490. goto done;
  25491. }
  25492. #ifndef HAVE_SELFTEST
  25493. /* test import of private and public */
  25494. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  25495. if (ret != 0) {
  25496. ret = WC_TEST_RET_ENC_EC(ret);
  25497. goto done;
  25498. }
  25499. #endif
  25500. done:
  25501. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25502. if (keyImp != NULL) {
  25503. wc_ecc_free(keyImp);
  25504. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25505. }
  25506. #else
  25507. wc_ecc_free(keyImp);
  25508. #endif
  25509. return ret;
  25510. }
  25511. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  25512. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  25513. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25514. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  25515. {
  25516. wc_test_ret_t ret;
  25517. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25518. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25519. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25520. #else
  25521. ecc_key key2[1];
  25522. ecc_key key3[1];
  25523. #endif
  25524. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25525. if ((key2 == NULL) || (key3 == NULL))
  25526. ERROR_OUT(MEMORY_E, done);
  25527. #endif
  25528. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  25529. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  25530. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  25531. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  25532. * private key in key2.
  25533. */
  25534. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  25535. ECC_SECP256R1);
  25536. if (ret != 0)
  25537. goto done;
  25538. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  25539. * in key3.
  25540. */
  25541. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  25542. key1->dp->prime, ECC_SECP256R1);
  25543. if (ret != 0)
  25544. goto done;
  25545. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  25546. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  25547. 1);
  25548. if (ret != 0) {
  25549. ret = WC_TEST_RET_ENC_EC(ret);
  25550. goto done;
  25551. }
  25552. done:
  25553. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25554. if (key2 != NULL) {
  25555. wc_ecc_free(key2);
  25556. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25557. }
  25558. if (key3 != NULL) {
  25559. wc_ecc_free(key3);
  25560. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25561. }
  25562. #else
  25563. wc_ecc_free(key3);
  25564. wc_ecc_free(key2);
  25565. #endif
  25566. return ret;
  25567. }
  25568. #endif
  25569. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  25570. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_ATECC508A) && \
  25571. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  25572. !defined(WOLFSSL_CRYPTOCELL)
  25573. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  25574. {
  25575. wc_test_ret_t ret;
  25576. byte out[128];
  25577. word32 outLen = sizeof(out);
  25578. /* Parameter Validation testing. */
  25579. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  25580. if (ret != BAD_FUNC_ARG)
  25581. return WC_TEST_RET_ENC_EC(ret);
  25582. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  25583. if (ret != BAD_FUNC_ARG)
  25584. return WC_TEST_RET_ENC_EC(ret);
  25585. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  25586. if (ret != BAD_FUNC_ARG)
  25587. return WC_TEST_RET_ENC_EC(ret);
  25588. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  25589. if (ret != BAD_FUNC_ARG)
  25590. return WC_TEST_RET_ENC_EC(ret);
  25591. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  25592. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  25593. !defined(HAVE_SELFTEST)
  25594. ret = wc_ecc_set_rng(key, rng);
  25595. if (ret != 0)
  25596. return WC_TEST_RET_ENC_EC(ret);
  25597. #else
  25598. (void)rng;
  25599. #endif
  25600. /* Use API. */
  25601. ret = 0;
  25602. do {
  25603. #if defined(WOLFSSL_ASYNC_CRYPT)
  25604. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25605. #endif
  25606. if (ret == 0)
  25607. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  25608. } while (ret == WC_PENDING_E);
  25609. if (ret != 0)
  25610. return WC_TEST_RET_ENC_EC(ret);
  25611. TEST_SLEEP();
  25612. return 0;
  25613. }
  25614. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  25615. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  25616. {
  25617. wc_test_ret_t ret;
  25618. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25619. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25620. #else
  25621. ecc_key key[1];
  25622. #endif
  25623. #if !defined(NO_ECC_SECP) && \
  25624. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  25625. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  25626. word32 idx = 0;
  25627. #endif
  25628. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25629. if (key == NULL)
  25630. ERROR_OUT(MEMORY_E, done);
  25631. #endif
  25632. wc_ecc_init_ex(key, HEAP_HINT, devId);
  25633. /* Use API */
  25634. ret = wc_ecc_set_flags(NULL, 0);
  25635. if (ret != BAD_FUNC_ARG) {
  25636. ret = WC_TEST_RET_ENC_EC(ret);
  25637. goto done;
  25638. }
  25639. ret = wc_ecc_set_flags(key, 0);
  25640. if (ret != 0) {
  25641. ret = WC_TEST_RET_ENC_EC(ret);
  25642. goto done;
  25643. }
  25644. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  25645. #ifndef WC_NO_RNG
  25646. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  25647. #if defined(WOLFSSL_ASYNC_CRYPT)
  25648. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  25649. #endif
  25650. if (ret != 0) {
  25651. goto done;
  25652. }
  25653. #ifndef NO_SIG_WRAPPER
  25654. ret = ecc_sig_test(rng, key);
  25655. if (ret < 0)
  25656. goto done;
  25657. #endif
  25658. TEST_SLEEP();
  25659. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  25660. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25661. ret = ecc_ssh_test(key, rng);
  25662. if (ret < 0)
  25663. goto done;
  25664. #endif
  25665. wc_ecc_free(key);
  25666. #else
  25667. (void)rng;
  25668. #endif /* !WC_NO_RNG */
  25669. #if !defined(NO_ECC_SECP) && \
  25670. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  25671. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  25672. /* Use test ECC key - ensure real private "d" exists */
  25673. #if defined(USE_CERT_BUFFERS_256)
  25674. {
  25675. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  25676. sizeof_ecc_key_der_256);
  25677. }
  25678. #elif !defined(NO_FILESYSTEM)
  25679. {
  25680. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  25681. byte der[128];
  25682. word32 derSz;
  25683. if (!file) {
  25684. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  25685. }
  25686. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  25687. XFCLOSE(file);
  25688. if (derSz == 0)
  25689. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  25690. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  25691. }
  25692. #else
  25693. {
  25694. (void)idx;
  25695. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
  25696. ERROR_OUT(ASN_PARSE_E, done);
  25697. }
  25698. #endif
  25699. if (ret != 0) {
  25700. goto done;
  25701. }
  25702. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  25703. ret = ecc_exp_imp_test(key);
  25704. if (ret < 0)
  25705. goto done;
  25706. #endif
  25707. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  25708. !defined(WOLFSSL_CRYPTOCELL)
  25709. ret = ecc_mulmod_test(key);
  25710. if (ret < 0)
  25711. goto done;
  25712. #endif
  25713. #endif
  25714. #else
  25715. (void)rng;
  25716. (void)idx;
  25717. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25718. done:
  25719. wc_ecc_free(key);
  25720. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25721. if (key != NULL) {
  25722. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25723. }
  25724. #endif
  25725. return ret;
  25726. }
  25727. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  25728. #if defined(WOLFSSL_CERT_EXT) && \
  25729. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25730. static wc_test_ret_t ecc_decode_test(void)
  25731. {
  25732. wc_test_ret_t ret;
  25733. word32 inSz;
  25734. word32 inOutIdx;
  25735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25736. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25737. #else
  25738. ecc_key key[1];
  25739. #endif
  25740. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  25741. /* This is ecc_clikeypub_der_256. */
  25742. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  25743. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  25744. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  25745. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  25746. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  25747. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  25748. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  25749. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  25750. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  25751. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  25752. 0xb4 };
  25753. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  25754. 0x00, 0x04, 0x01, 0x01 };
  25755. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  25756. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25757. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  25758. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25759. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  25760. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25761. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  25762. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25763. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  25764. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  25765. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25766. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  25767. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  25768. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25769. 0x03, 0x03, 0x04, 0x01, 0x01 };
  25770. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  25771. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  25772. 0x03, 0x03, 0x00, 0x04, 0x01 };
  25773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25774. if (key == NULL)
  25775. ERROR_OUT(MEMORY_E, done);
  25776. #endif
  25777. XMEMSET(key, 0, sizeof *key);
  25778. wc_ecc_init_ex(key, HEAP_HINT, devId);
  25779. inSz = sizeof(good);
  25780. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  25781. if (ret != BAD_FUNC_ARG) {
  25782. ret = WC_TEST_RET_ENC_EC(ret);
  25783. goto done;
  25784. }
  25785. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  25786. if (ret != BAD_FUNC_ARG) {
  25787. ret = WC_TEST_RET_ENC_EC(ret);
  25788. goto done;
  25789. }
  25790. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  25791. if (ret != BAD_FUNC_ARG) {
  25792. ret = WC_TEST_RET_ENC_EC(ret);
  25793. goto done;
  25794. }
  25795. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  25796. if (ret != BAD_FUNC_ARG) {
  25797. ret = WC_TEST_RET_ENC_EC(ret);
  25798. goto done;
  25799. }
  25800. /* Change offset to produce bad input data. */
  25801. inOutIdx = 2;
  25802. inSz = sizeof(good) - inOutIdx;
  25803. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25804. if (ret != ASN_PARSE_E) {
  25805. ret = WC_TEST_RET_ENC_EC(ret);
  25806. goto done;
  25807. }
  25808. inOutIdx = 4;
  25809. inSz = sizeof(good) - inOutIdx;
  25810. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25811. if (ret != ASN_PARSE_E) {
  25812. ret = WC_TEST_RET_ENC_EC(ret);
  25813. goto done;
  25814. }
  25815. /* Bad data. */
  25816. inSz = sizeof(badNoObjId);
  25817. inOutIdx = 0;
  25818. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  25819. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  25820. ret = WC_TEST_RET_ENC_EC(ret);
  25821. goto done;
  25822. }
  25823. inSz = sizeof(badOneObjId);
  25824. inOutIdx = 0;
  25825. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  25826. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  25827. ret = WC_TEST_RET_ENC_EC(ret);
  25828. goto done;
  25829. }
  25830. inSz = sizeof(badObjId1Len);
  25831. inOutIdx = 0;
  25832. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  25833. if (ret != ASN_PARSE_E) {
  25834. ret = WC_TEST_RET_ENC_EC(ret);
  25835. goto done;
  25836. }
  25837. inSz = sizeof(badObj2d1Len);
  25838. inOutIdx = 0;
  25839. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  25840. if (ret != ASN_PARSE_E) {
  25841. ret = WC_TEST_RET_ENC_EC(ret);
  25842. goto done;
  25843. }
  25844. inSz = sizeof(badNotBitStr);
  25845. inOutIdx = 0;
  25846. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  25847. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  25848. ret = WC_TEST_RET_ENC_EC(ret);
  25849. goto done;
  25850. }
  25851. inSz = sizeof(badBitStrLen);
  25852. inOutIdx = 0;
  25853. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  25854. if (ret != ASN_PARSE_E) {
  25855. ret = WC_TEST_RET_ENC_EC(ret);
  25856. goto done;
  25857. }
  25858. inSz = sizeof(badNoBitStrZero);
  25859. inOutIdx = 0;
  25860. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  25861. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  25862. ret = WC_TEST_RET_ENC_EC(ret);
  25863. goto done;
  25864. }
  25865. inSz = sizeof(badPoint);
  25866. inOutIdx = 0;
  25867. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  25868. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  25869. ret = WC_TEST_RET_ENC_EC(ret);
  25870. goto done;
  25871. }
  25872. inSz = sizeof(good);
  25873. inOutIdx = 0;
  25874. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  25875. if (ret != 0) {
  25876. ret = WC_TEST_RET_ENC_EC(ret);
  25877. goto done;
  25878. }
  25879. done:
  25880. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25881. if (key != NULL) {
  25882. wc_ecc_free(key);
  25883. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25884. }
  25885. #else
  25886. wc_ecc_free(key);
  25887. #endif
  25888. return ret;
  25889. }
  25890. #endif /* WOLFSSL_CERT_EXT */
  25891. #ifdef WOLFSSL_CUSTOM_CURVES
  25892. static const byte eccKeyExplicitCurve[] = {
  25893. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  25894. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  25895. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  25896. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  25897. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  25898. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25899. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25900. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  25901. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  25902. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  25903. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  25904. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  25905. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  25906. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  25907. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  25908. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  25909. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  25910. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  25911. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25912. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  25913. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  25914. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  25915. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  25916. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  25917. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  25918. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  25919. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  25920. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  25921. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  25922. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  25923. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  25924. };
  25925. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  25926. {
  25927. wc_test_ret_t ret;
  25928. word32 inOutIdx;
  25929. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25930. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25931. #else
  25932. ecc_key key[1];
  25933. #endif
  25934. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  25935. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  25936. #ifndef WOLFSSL_ECC_CURVE_STATIC
  25937. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  25938. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  25939. };
  25940. #define ecc_oid_brainpoolp256r1_sz \
  25941. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  25942. #else
  25943. #define ecc_oid_brainpoolp256r1 { \
  25944. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  25945. }
  25946. #define ecc_oid_brainpoolp256r1_sz 9
  25947. #endif
  25948. #define ecc_oid_brainpoolp256r1_sum 104
  25949. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  25950. 32, /* size/bytes */
  25951. ECC_CURVE_CUSTOM, /* ID */
  25952. "BRAINPOOLP256R1", /* curve name */
  25953. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  25954. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  25955. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  25956. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  25957. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  25958. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  25959. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  25960. ecc_oid_brainpoolp256r1_sz,
  25961. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  25962. 1, /* cofactor */
  25963. };
  25964. #endif /* HAVE_ECC_BRAINPOOL */
  25965. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25966. if (! key) {
  25967. ret = MEMORY_E;
  25968. goto done;
  25969. }
  25970. #endif
  25971. XMEMSET(key, 0, sizeof *key);
  25972. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  25973. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  25974. &ecc_dp_brainpool256r1);
  25975. if (ret != 0) {
  25976. printf("ECC test for custom curve failed!\n");
  25977. goto done;
  25978. }
  25979. #endif
  25980. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  25981. {
  25982. int curve_id;
  25983. #ifdef HAVE_ECC_BRAINPOOL
  25984. curve_id = ECC_BRAINPOOLP256R1;
  25985. #else
  25986. curve_id = ECC_SECP256K1;
  25987. #endif
  25988. /* Test and demonstrate use of non-SECP curve */
  25989. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  25990. if (ret < 0) {
  25991. printf("ECC test for curve_id %d failed!\n", curve_id);
  25992. goto done;
  25993. }
  25994. }
  25995. #endif
  25996. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25997. if (ret != 0) {
  25998. ret = WC_TEST_RET_ENC_EC(ret);
  25999. goto done;
  26000. }
  26001. inOutIdx = 0;
  26002. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  26003. sizeof(eccKeyExplicitCurve));
  26004. if (ret != 0)
  26005. ret = WC_TEST_RET_ENC_EC(ret);
  26006. done:
  26007. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26008. if (key) {
  26009. wc_ecc_free(key);
  26010. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26011. }
  26012. #else
  26013. wc_ecc_free(key);
  26014. #endif
  26015. (void)rng;
  26016. return ret;
  26017. }
  26018. #endif /* WOLFSSL_CUSTOM_CURVES */
  26019. #ifdef WOLFSSL_SM2
  26020. #ifdef HAVE_ECC_VERIFY
  26021. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  26022. #ifdef WOLFSSL_SM2
  26023. #ifdef HAVE_OID_ENCODING
  26024. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  26025. #define CODED_SM2P256V1_SZ 6
  26026. #else
  26027. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  26028. #define CODED_SM2P256V1_SZ 10
  26029. #endif
  26030. #ifndef WOLFSSL_ECC_CURVE_STATIC
  26031. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  26032. #else
  26033. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  26034. #endif
  26035. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  26036. #endif /* WOLFSSL_SM2 */
  26037. #define ECC_SM2P256V1_TEST 102
  26038. static int test_sm2_verify_caseA2(void)
  26039. {
  26040. ecc_key key;
  26041. int ret, res;
  26042. mp_int r,s;
  26043. /* test key values */
  26044. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  26045. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  26046. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  26047. const ecc_set_type ecc_sm2_A2 = {
  26048. 32, /* size/bytes */
  26049. ECC_SM2P256V1_TEST, /* ID */
  26050. "SM2P256V1_TEST", /* curve name */
  26051. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  26052. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  26053. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  26054. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  26055. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  26056. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  26057. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  26058. ecc_oid_sm2p256v1, /* oid/oidSz */
  26059. ecc_oid_sm2p256v1_sz,
  26060. ECC_SM2P256V1_OID, /* oid sum */
  26061. 1, /* cofactor */
  26062. };
  26063. /* use canned hash value hash = H(ZA||M) */
  26064. const byte hash[] = {
  26065. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  26066. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  26067. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  26068. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  26069. };
  26070. /* canned r and s */
  26071. const byte rCan[] = {
  26072. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  26073. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  26074. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  26075. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  26076. };
  26077. const byte sCan[] = {
  26078. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  26079. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  26080. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  26081. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  26082. };
  26083. mp_init(&r);
  26084. mp_init(&s);
  26085. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  26086. if (ret != 0)
  26087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26088. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  26089. if (ret != 0)
  26090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26091. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  26092. if (ret != 0)
  26093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26094. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  26095. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  26096. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  26097. if (ret != 0)
  26098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26099. if (res != 1)
  26100. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26101. done:
  26102. mp_free(&r);
  26103. mp_free(&s);
  26104. wc_ecc_free(&key);
  26105. return ret;
  26106. }
  26107. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  26108. static int test_sm2_verify_case(void)
  26109. {
  26110. ecc_key key;
  26111. int ret, res;
  26112. /* test key values */
  26113. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  26114. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  26115. /* use canned hash value hash = H(ZA||M) */
  26116. const byte hash[] = {
  26117. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  26118. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  26119. 0x50,0x69,0x5B,0x20
  26120. };
  26121. const byte sig[] = {
  26122. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  26123. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  26124. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  26125. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  26126. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  26127. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  26128. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  26129. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  26130. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  26131. };
  26132. const byte badSig[] = {
  26133. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  26134. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  26135. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  26136. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  26137. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  26138. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  26139. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  26140. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  26141. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  26142. };
  26143. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  26144. if (ret != 0)
  26145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26146. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  26147. if (ret != 0)
  26148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26149. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  26150. &key);
  26151. if (ret != 0)
  26152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26153. if (res != 1)
  26154. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26155. /* now test a case that should fail */
  26156. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  26157. &res, &key);
  26158. if (ret != 0)
  26159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26160. if (res == 1)
  26161. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26162. done:
  26163. wc_ecc_free(&key);
  26164. return ret;
  26165. }
  26166. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  26167. {
  26168. const ecc_set_type* dp = wc_ecc_get_curve_params(
  26169. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  26170. int keySize = 32;
  26171. int curve_id = ECC_SM2P256V1;
  26172. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  26173. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26174. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26175. #endif
  26176. #ifdef HAVE_ECC_KEY_EXPORT
  26177. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  26178. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26179. #endif
  26180. word32 x = 0;
  26181. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  26182. word32 y;
  26183. #endif
  26184. #ifdef HAVE_ECC_SIGN
  26185. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26186. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26187. int i;
  26188. #ifdef HAVE_ECC_VERIFY
  26189. int verify;
  26190. #endif /* HAVE_ECC_VERIFY */
  26191. #endif /* HAVE_ECC_SIGN */
  26192. int ret;
  26193. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26194. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26195. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26196. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26197. #else
  26198. ecc_key userA[1];
  26199. ecc_key userB[1];
  26200. ecc_key pubKey[1];
  26201. #endif
  26202. #ifndef WC_NO_RNG
  26203. int curveSize;
  26204. #endif
  26205. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  26206. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26207. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26208. #endif
  26209. #ifdef HAVE_ECC_KEY_EXPORT
  26210. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26211. #endif
  26212. #ifdef HAVE_ECC_SIGN
  26213. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26214. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26215. #endif
  26216. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  26217. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  26218. if (sharedA == NULL || sharedB == NULL)
  26219. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26220. #endif
  26221. #ifdef HAVE_ECC_KEY_EXPORT
  26222. if (exportBuf == NULL)
  26223. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26224. #endif
  26225. #ifdef HAVE_ECC_SIGN
  26226. if (sig == NULL || digest == NULL)
  26227. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26228. #endif
  26229. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  26230. (void)testVerifyCount;
  26231. (void)dp;
  26232. (void)x;
  26233. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26234. if ((userA == NULL) ||
  26235. (userB == NULL) ||
  26236. (pubKey == NULL))
  26237. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26238. #endif
  26239. XMEMSET(userA, 0, sizeof *userA);
  26240. XMEMSET(userB, 0, sizeof *userB);
  26241. XMEMSET(pubKey, 0, sizeof *pubKey);
  26242. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26243. if (ret != 0)
  26244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26245. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  26246. if (ret != 0)
  26247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26248. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26249. if (ret != 0)
  26250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26251. #ifndef WC_NO_RNG
  26252. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  26253. if (ret == ECC_CURVE_OID_E)
  26254. goto done; /* catch case, where curve is not supported */
  26255. if (ret != 0)
  26256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26257. TEST_SLEEP();
  26258. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  26259. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  26260. if (curveSize != userA->dp->size) {
  26261. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26262. }
  26263. }
  26264. ret = wc_ecc_check_key(userA);
  26265. if (ret != 0)
  26266. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26267. TEST_SLEEP();
  26268. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  26269. if (ret != 0)
  26270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26271. /* only perform the below tests if the key size matches */
  26272. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  26273. if (ret != 0) {
  26274. ret = ECC_CURVE_OID_E;
  26275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26276. }
  26277. #ifdef HAVE_ECC_DHE
  26278. #if defined(ECC_TIMING_RESISTANT)
  26279. ret = wc_ecc_set_rng(userA, rng);
  26280. if (ret != 0)
  26281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26282. ret = wc_ecc_set_rng(userB, rng);
  26283. if (ret != 0)
  26284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26285. #endif
  26286. x = ECC_SHARED_SIZE;
  26287. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  26288. if (ret != 0)
  26289. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26290. y = ECC_SHARED_SIZE;
  26291. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  26292. if (ret != 0)
  26293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26294. if (y != x)
  26295. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26296. if (XMEMCMP(sharedA, sharedB, x))
  26297. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26298. #endif /* HAVE_ECC_DHE */
  26299. #ifdef HAVE_ECC_KEY_EXPORT
  26300. x = ECC_KEY_EXPORT_BUF_SIZE;
  26301. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  26302. if (ret != 0)
  26303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26304. #ifdef HAVE_ECC_KEY_IMPORT
  26305. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26306. if (ret != 0)
  26307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26308. #ifdef HAVE_ECC_DHE
  26309. y = ECC_SHARED_SIZE;
  26310. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  26311. if (ret != 0)
  26312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26313. if (XMEMCMP(sharedA, sharedB, y))
  26314. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26315. #endif /* HAVE_ECC_DHE */
  26316. #ifdef HAVE_COMP_KEY
  26317. /* try compressed export / import too */
  26318. x = ECC_KEY_EXPORT_BUF_SIZE;
  26319. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  26320. if (ret != 0)
  26321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26322. wc_ecc_free(pubKey);
  26323. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26324. if (ret != 0)
  26325. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26326. #endif
  26327. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26328. if (ret != 0)
  26329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26330. #ifdef HAVE_ECC_DHE
  26331. y = ECC_SHARED_SIZE;
  26332. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  26333. if (ret != 0)
  26334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26335. if (XMEMCMP(sharedA, sharedB, y))
  26336. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26337. #endif /* HAVE_ECC_DHE */
  26338. #endif /* HAVE_ECC_KEY_IMPORT */
  26339. #endif /* HAVE_ECC_KEY_EXPORT */
  26340. #endif /* !WC_NO_RNG */
  26341. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26342. !defined(WC_NO_RNG))
  26343. #ifdef HAVE_ECC_SIGN
  26344. /* ECC w/out Shamir has issue with all 0 digest */
  26345. /* WC_BIGINT doesn't have 0 len well on hardware */
  26346. /* Cryptocell has issues with all 0 digest */
  26347. #if defined(ECC_SHAMIR)
  26348. /* test DSA sign hash with zeros */
  26349. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26350. digest[i] = 0;
  26351. }
  26352. x = ECC_SIG_SIZE;
  26353. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  26354. if (ret != 0)
  26355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26356. #ifdef HAVE_ECC_VERIFY
  26357. for (i = 0; i < testVerifyCount; i++) {
  26358. verify = 0;
  26359. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  26360. userA);
  26361. if (ret != 0)
  26362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26363. if (verify != 1)
  26364. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26365. }
  26366. #endif /* HAVE_ECC_VERIFY */
  26367. #endif /* ECC_SHAMIR */
  26368. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  26369. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26370. digest[i] = (byte)i;
  26371. }
  26372. x = ECC_SIG_SIZE;
  26373. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  26374. if (ret != 0)
  26375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26376. #ifdef HAVE_ECC_VERIFY
  26377. for (i = 0; i < testVerifyCount; i++) {
  26378. verify = 0;
  26379. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  26380. userA);
  26381. if (ret != 0)
  26382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26383. if (verify != 1)
  26384. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26385. }
  26386. #endif /* HAVE_ECC_VERIFY */
  26387. #endif /* HAVE_ECC_SIGN */
  26388. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  26389. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  26390. x = ECC_KEY_EXPORT_BUF_SIZE;
  26391. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  26392. if (ret != 0)
  26393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26394. #elif defined(HAVE_ECC_KEY_EXPORT)
  26395. (void)exportBuf;
  26396. #endif /* HAVE_ECC_KEY_EXPORT */
  26397. done:
  26398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26399. if (userA != NULL) {
  26400. wc_ecc_free(userA);
  26401. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26402. }
  26403. if (userB != NULL) {
  26404. wc_ecc_free(userB);
  26405. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26406. }
  26407. if (pubKey != NULL) {
  26408. wc_ecc_free(pubKey);
  26409. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26410. }
  26411. #else
  26412. wc_ecc_free(pubKey);
  26413. wc_ecc_free(userB);
  26414. wc_ecc_free(userA);
  26415. #endif
  26416. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  26417. WC_FREE_VAR(sharedA, HEAP_HINT);
  26418. WC_FREE_VAR(sharedB, HEAP_HINT);
  26419. #endif
  26420. #ifdef HAVE_ECC_KEY_EXPORT
  26421. WC_FREE_VAR(exportBuf, HEAP_HINT);
  26422. #endif
  26423. #ifdef HAVE_ECC_SIGN
  26424. WC_FREE_VAR(sig, HEAP_HINT);
  26425. WC_FREE_VAR(digest, HEAP_HINT);
  26426. #endif
  26427. (void)keySize;
  26428. (void)curve_id;
  26429. (void)rng;
  26430. return ret;
  26431. }
  26432. #endif /* HAVE_ECC_VERIFY */
  26433. static int test_sm2_create_digest(void)
  26434. {
  26435. const byte msg[] = "message to sign";
  26436. const byte id[] = "0123456789";
  26437. const byte badId[] = "0123556789";
  26438. byte expected[] = {
  26439. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  26440. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  26441. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  26442. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  26443. };
  26444. ecc_key key;
  26445. int ret;
  26446. /* test key values */
  26447. const char qx[] =
  26448. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  26449. const char qy[] =
  26450. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  26451. const char d[] =
  26452. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  26453. byte digest[WC_SHA256_DIGEST_SIZE];
  26454. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  26455. if (ret != 0)
  26456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26457. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  26458. if (ret != 0)
  26459. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26460. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  26461. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  26462. WC_SHA256_DIGEST_SIZE, &key);
  26463. if (ret != 0)
  26464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26465. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  26466. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26467. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  26468. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  26469. WC_SHA256_DIGEST_SIZE, &key);
  26470. if (ret != 0)
  26471. goto done;
  26472. /* should be different than the previous ID used */
  26473. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  26474. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26475. done:
  26476. wc_ecc_free(&key);
  26477. return ret;
  26478. }
  26479. static int test_sm2_verify(void)
  26480. {
  26481. int ret = 0;
  26482. #ifdef HAVE_ECC_VERIFY
  26483. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  26484. ret = test_sm2_verify_caseA2();
  26485. if (ret != 0)
  26486. return ret;
  26487. #endif
  26488. ret = test_sm2_verify_case();
  26489. if (ret != 0)
  26490. return ret;
  26491. #endif /* HAVE_ECC_VERIFY */
  26492. ret = test_sm2_create_digest();
  26493. return ret;
  26494. }
  26495. #endif /* WOLFSSL_SM2 */
  26496. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  26497. /* Make Cert / Sign example for ECC cert and ECC CA */
  26498. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  26499. {
  26500. wc_test_ret_t ret;
  26501. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26502. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26503. #ifdef WOLFSSL_TEST_CERT
  26504. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26505. #endif
  26506. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26507. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26508. #else
  26509. Cert myCert[1];
  26510. #ifdef WOLFSSL_TEST_CERT
  26511. DecodedCert decode[1];
  26512. #endif
  26513. ecc_key caEccKey[1];
  26514. ecc_key certPubKey[1];
  26515. #endif
  26516. int certSz;
  26517. size_t bytes = 0;
  26518. word32 idx = 0;
  26519. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
  26520. XFILE file;
  26521. #endif
  26522. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26523. byte* der = NULL;
  26524. #else
  26525. byte der[FOURK_BUF];
  26526. #endif
  26527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26528. if ((myCert == NULL)
  26529. #ifdef WOLFSSL_TEST_CERT
  26530. || (decode == NULL)
  26531. #endif
  26532. || (caEccKey == NULL) || (certPubKey == NULL))
  26533. ERROR_OUT(MEMORY_E, exit);
  26534. #endif
  26535. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  26536. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  26537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26538. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26539. if (der == NULL) {
  26540. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26541. }
  26542. #endif
  26543. /* Get cert private key */
  26544. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  26545. /* Get Cert Key 384 */
  26546. #ifdef USE_CERT_BUFFERS_256
  26547. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  26548. bytes = sizeof_ca_ecc_key_der_384;
  26549. #elif !defined(NO_FILESYSTEM)
  26550. file = XFOPEN(eccCaKey384File, "rb");
  26551. if (!file) {
  26552. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26553. }
  26554. bytes = XFREAD(der, 1, FOURK_BUF, file);
  26555. XFCLOSE(file);
  26556. if (bytes == 0)
  26557. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26558. (void)eccCaKeyFile;
  26559. #else
  26560. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
  26561. ERROR_OUT(ASN_PARSE_E, exit);
  26562. #endif /* USE_CERT_BUFFERS_256 */
  26563. /* end if ENABLE_ECC384_CERT_GEN_TEST */
  26564. #else
  26565. /* !ENABLE_ECC384_CERT_GEN_TEST */
  26566. #ifdef USE_CERT_BUFFERS_256
  26567. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  26568. bytes = sizeof_ca_ecc_key_der_256;
  26569. #else
  26570. file = XFOPEN(eccCaKeyFile, "rb");
  26571. if (!file) {
  26572. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26573. }
  26574. bytes = XFREAD(der, 1, FOURK_BUF, file);
  26575. XFCLOSE(file);
  26576. if (bytes == 0)
  26577. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26578. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  26579. (void)eccCaKey384File;
  26580. #endif
  26581. #endif /* USE_CERT_BUFFERS_256 */
  26582. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  26583. /* Get CA Key */
  26584. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  26585. if (ret != 0)
  26586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26587. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  26588. if (ret != 0)
  26589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26590. /* Make a public key */
  26591. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  26592. if (ret != 0)
  26593. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26594. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  26595. #if defined(WOLFSSL_ASYNC_CRYPT)
  26596. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  26597. #endif
  26598. if (ret != 0)
  26599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26600. TEST_SLEEP();
  26601. /* Setup Certificate */
  26602. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  26603. if (ret != 0)
  26604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26605. #ifndef NO_SHA256
  26606. myCert->sigType = CTC_SHA256wECDSA;
  26607. #else
  26608. myCert->sigType = CTC_SHAwECDSA;
  26609. #endif
  26610. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  26611. #ifdef WOLFSSL_CERT_EXT
  26612. /* add Policies */
  26613. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  26614. CTC_MAX_CERTPOL_SZ);
  26615. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  26616. CTC_MAX_CERTPOL_SZ);
  26617. myCert->certPoliciesNb = 2;
  26618. /* add SKID from the Public Key */
  26619. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  26620. if (ret != 0)
  26621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26622. /* add AKID from the Public Key */
  26623. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  26624. if (ret != 0)
  26625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26626. /* add Key Usage */
  26627. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  26628. if (ret != 0)
  26629. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26630. #endif /* WOLFSSL_CERT_EXT */
  26631. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  26632. #if defined(USE_CERT_BUFFERS_256)
  26633. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  26634. sizeof_ca_ecc_cert_der_384);
  26635. #elif !defined(NO_FILESYSTEM)
  26636. ret = wc_SetIssuer(myCert, eccCaCert384File);
  26637. (void)eccCaCertFile;
  26638. #else
  26639. /* not testing with embedded, no file system target */
  26640. ERROR_OUT(ASN_PARSE_E, exit);
  26641. #endif /* USE_CERT_BUFFERS_256 */
  26642. #else
  26643. /* not ENABLE_ECC384_CERT_GEN_TEST */
  26644. #if defined(USE_CERT_BUFFERS_256)
  26645. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  26646. sizeof_ca_ecc_cert_der_256);
  26647. #else
  26648. ret = wc_SetIssuer(myCert, eccCaCertFile);
  26649. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  26650. (void)eccCaCert384File;
  26651. #endif
  26652. #endif
  26653. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  26654. if (ret < 0)
  26655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26656. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  26657. if (certSz < 0) {
  26658. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  26659. }
  26660. ret = 0;
  26661. do {
  26662. #if defined(WOLFSSL_ASYNC_CRYPT)
  26663. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26664. #endif
  26665. if (ret >= 0) {
  26666. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  26667. FOURK_BUF, NULL, caEccKey, rng);
  26668. }
  26669. } while (ret == WC_PENDING_E);
  26670. if (ret < 0)
  26671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26672. certSz = (word32)ret;
  26673. TEST_SLEEP();
  26674. #ifdef WOLFSSL_TEST_CERT
  26675. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  26676. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  26677. if (ret != 0) {
  26678. FreeDecodedCert(decode);
  26679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26680. }
  26681. FreeDecodedCert(decode);
  26682. #endif
  26683. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  26684. CERT_TYPE);
  26685. if (ret != 0) {
  26686. goto exit;
  26687. }
  26688. exit:
  26689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26690. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26691. #endif
  26692. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26693. if (myCert != NULL)
  26694. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26695. #ifdef WOLFSSL_TEST_CERT
  26696. if (decode != NULL)
  26697. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26698. #endif
  26699. if (caEccKey != NULL) {
  26700. wc_ecc_free(caEccKey);
  26701. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26702. }
  26703. if (certPubKey != NULL) {
  26704. wc_ecc_free(certPubKey);
  26705. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26706. }
  26707. #else
  26708. wc_ecc_free(certPubKey);
  26709. wc_ecc_free(caEccKey);
  26710. #endif
  26711. return ret;
  26712. }
  26713. #endif /* WOLFSSL_CERT_GEN */
  26714. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  26715. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  26716. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  26717. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  26718. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  26719. {
  26720. wc_test_ret_t ret = 0;
  26721. ecc_key* key;
  26722. #ifdef WC_NO_RNG
  26723. word32 idx = 0;
  26724. #endif
  26725. key = wc_ecc_key_new(HEAP_HINT);
  26726. if (key == NULL) {
  26727. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  26728. }
  26729. #ifndef WC_NO_RNG
  26730. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26731. #if defined(WOLFSSL_ASYNC_CRYPT)
  26732. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26733. #endif
  26734. if (ret != 0)
  26735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  26736. #else
  26737. /* use test ECC key */
  26738. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  26739. (word32)sizeof_ecc_key_der_256);
  26740. (void)rng;
  26741. #endif
  26742. exit:
  26743. wc_ecc_key_free(key);
  26744. return ret;
  26745. }
  26746. #endif
  26747. /* ECC Non-blocking tests for Sign and Verify */
  26748. /* Requires SP math and supports P384 or P256 */
  26749. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  26750. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  26751. defined(WOLFSSL_PUBLIC_MP)
  26752. /* ECC Private Key "d" */
  26753. static const byte p256PrivKey[] = {
  26754. /* SECP256R1 */
  26755. /* d */
  26756. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  26757. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  26758. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  26759. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  26760. };
  26761. #ifdef HAVE_ECC384
  26762. static const byte p384PrivKey[] = {
  26763. /* SECP384R1 */
  26764. /* d */
  26765. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  26766. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  26767. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  26768. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  26769. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  26770. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  26771. };
  26772. #endif /* HAVE_ECC384 */
  26773. #ifdef HAVE_ECC521
  26774. static const byte p521PrivKey[] = {
  26775. /* SECP521R1 */
  26776. /* d */
  26777. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  26778. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  26779. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  26780. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  26781. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  26782. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  26783. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  26784. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  26785. 0xa4, 0x36
  26786. };
  26787. #endif /* HAVE_ECC521 */
  26788. /* ECC public key Qx/Qy */
  26789. static const byte p256PubKey[] = {
  26790. /* SECP256R1 */
  26791. /* Qx */
  26792. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  26793. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  26794. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  26795. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  26796. /* Qy */
  26797. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  26798. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  26799. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  26800. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  26801. };
  26802. #ifdef HAVE_ECC384
  26803. static const byte p384PubKey[] = {
  26804. /* SECP384R1 */
  26805. /* Qx */
  26806. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  26807. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  26808. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  26809. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  26810. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  26811. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  26812. /* Qy */
  26813. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  26814. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  26815. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  26816. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  26817. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  26818. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  26819. };
  26820. #endif
  26821. #ifdef HAVE_ECC521
  26822. static const byte p521PubKey[] = {
  26823. /* SECP521R1 */
  26824. /* Qx */
  26825. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  26826. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  26827. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  26828. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  26829. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  26830. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  26831. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  26832. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  26833. 0x8b, 0xbc,
  26834. /* Qy */
  26835. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  26836. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  26837. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  26838. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  26839. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  26840. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  26841. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  26842. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  26843. 0x91, 0xd9
  26844. };
  26845. /* perform verify of signature and hash using public key */
  26846. /* key is public Qx + public Qy */
  26847. /* sig is r + s */
  26848. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  26849. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  26850. uint32_t curveSz, int curveId)
  26851. {
  26852. wc_test_ret_t ret;
  26853. int verify_res = 0, count = 0;
  26854. mp_int r, s;
  26855. ecc_key ecc;
  26856. ecc_nb_ctx_t nb_ctx;
  26857. /* validate arguments */
  26858. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  26859. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  26860. {
  26861. return WC_TEST_RET_ENC_NC;
  26862. }
  26863. /* Setup the ECC key */
  26864. ret = wc_ecc_init(&ecc);
  26865. if (ret < 0) {
  26866. return WC_TEST_RET_ENC_EC(ret);
  26867. }
  26868. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  26869. if (ret != MP_OKAY) {
  26870. wc_ecc_free(&ecc);
  26871. return WC_TEST_RET_ENC_EC(ret);
  26872. }
  26873. /* Setup the signature r/s variables */
  26874. ret = mp_init(&r);
  26875. if (ret != MP_OKAY) {
  26876. wc_ecc_free(&ecc);
  26877. return WC_TEST_RET_ENC_EC(ret);
  26878. }
  26879. ret = mp_init(&s);
  26880. if (ret != MP_OKAY) {
  26881. mp_clear(&r);
  26882. wc_ecc_free(&ecc);
  26883. return WC_TEST_RET_ENC_EC(ret);
  26884. }
  26885. /* Import public key x/y */
  26886. ret = wc_ecc_import_unsigned(
  26887. &ecc,
  26888. (byte*)key, /* Public "x" Coordinate */
  26889. (byte*)(key + curveSz), /* Public "y" Coordinate */
  26890. NULL, /* Private "d" (optional) */
  26891. curveId /* ECC Curve Id */
  26892. );
  26893. /* Make sure it was a public key imported */
  26894. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  26895. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  26896. }
  26897. /* Import signature r/s */
  26898. if (ret == 0) {
  26899. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  26900. if (ret < 0)
  26901. ret = WC_TEST_RET_ENC_EC(ret);
  26902. }
  26903. if (ret == 0) {
  26904. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  26905. if (ret < 0)
  26906. ret = WC_TEST_RET_ENC_EC(ret);
  26907. }
  26908. /* Verify ECC Signature */
  26909. if (ret == 0) {
  26910. do {
  26911. ret = wc_ecc_verify_hash_ex(
  26912. &r, &s, /* r/s as mp_int */
  26913. hash, hashSz, /* computed hash digest */
  26914. &verify_res, /* verification result 1=success */
  26915. &ecc
  26916. );
  26917. count++;
  26918. /* This is where real-time work could be called */
  26919. } while (ret == FP_WOULDBLOCK);
  26920. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26921. printf("ECC non-block verify: %d times\n", count);
  26922. #endif
  26923. if (ret < 0)
  26924. ret = WC_TEST_RET_ENC_EC(ret);
  26925. }
  26926. /* check verify result */
  26927. if (ret == 0 && verify_res == 0) {
  26928. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  26929. }
  26930. mp_clear(&r);
  26931. mp_clear(&s);
  26932. wc_ecc_free(&ecc);
  26933. (void)count;
  26934. return ret;
  26935. }
  26936. /* perform signature operation against hash using private key */
  26937. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  26938. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  26939. uint32_t curveSz, int curveId, WC_RNG* rng)
  26940. {
  26941. wc_test_ret_t ret;
  26942. int count = 0;
  26943. mp_int r, s;
  26944. ecc_key ecc;
  26945. ecc_nb_ctx_t nb_ctx;
  26946. /* validate arguments */
  26947. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  26948. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  26949. {
  26950. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  26951. }
  26952. /* Initialize signature result */
  26953. memset(sig, 0, curveSz*2);
  26954. /* Setup the ECC key */
  26955. ret = wc_ecc_init(&ecc);
  26956. if (ret < 0) {
  26957. return WC_TEST_RET_ENC_EC(ret);
  26958. }
  26959. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  26960. if (ret != MP_OKAY) {
  26961. wc_ecc_free(&ecc);
  26962. return WC_TEST_RET_ENC_EC(ret);
  26963. }
  26964. /* Setup the signature r/s variables */
  26965. ret = mp_init(&r);
  26966. if (ret != MP_OKAY) {
  26967. wc_ecc_free(&ecc);
  26968. return WC_TEST_RET_ENC_EC(ret);
  26969. }
  26970. ret = mp_init(&s);
  26971. if (ret != MP_OKAY) {
  26972. mp_clear(&r);
  26973. wc_ecc_free(&ecc);
  26974. return WC_TEST_RET_ENC_EC(ret);
  26975. }
  26976. /* Import private key "k" */
  26977. ret = wc_ecc_import_private_key_ex(
  26978. key, keySz, /* private key "d" */
  26979. NULL, 0, /* public (optional) */
  26980. &ecc,
  26981. curveId /* ECC Curve Id */
  26982. );
  26983. if (ret < 0)
  26984. ret = WC_TEST_RET_ENC_EC(ret);
  26985. if (ret == 0) {
  26986. do {
  26987. /* Verify ECC Signature */
  26988. ret = wc_ecc_sign_hash_ex(
  26989. hash, hashSz, /* computed hash digest */
  26990. rng, &ecc, /* random and key context */
  26991. &r, &s /* r/s as mp_int */
  26992. );
  26993. count++;
  26994. /* This is where real-time work could be called */
  26995. } while (ret == FP_WOULDBLOCK);
  26996. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  26997. printf("ECC non-block sign: %d times\n", count);
  26998. #endif
  26999. if (ret < 0)
  27000. ret = WC_TEST_RET_ENC_EC(ret);
  27001. }
  27002. if (ret == 0) {
  27003. /* export r/s */
  27004. mp_to_unsigned_bin_len(&r, sig, curveSz);
  27005. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  27006. }
  27007. mp_clear(&r);
  27008. mp_clear(&s);
  27009. wc_ecc_free(&ecc);
  27010. (void)count;
  27011. return ret;
  27012. }
  27013. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  27014. /*
  27015. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  27016. * don't have non-blocking versions of the key checking functions, yet.
  27017. */
  27018. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  27019. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  27020. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  27021. {
  27022. wc_test_ret_t ret;
  27023. ecc_key keyA;
  27024. ecc_key keyB;
  27025. ecc_nb_ctx_t nbCtxA;
  27026. ecc_nb_ctx_t nbCtxB;
  27027. byte secretA[ECC_SHARED_SIZE];
  27028. byte secretB[ECC_SHARED_SIZE];
  27029. word32 secretSzA = ECC_SHARED_SIZE;
  27030. word32 secretSzB = ECC_SHARED_SIZE;
  27031. int count = 0;
  27032. ret = wc_ecc_init(&keyA);
  27033. if (ret == 0) {
  27034. ret = wc_ecc_init(&keyB);
  27035. if (ret < 0)
  27036. ret = WC_TEST_RET_ENC_EC(ret);
  27037. }
  27038. if (ret == 0) {
  27039. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  27040. if (ret < 0)
  27041. ret = WC_TEST_RET_ENC_EC(ret);
  27042. }
  27043. if (ret == 0) {
  27044. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  27045. if (ret < 0)
  27046. ret = WC_TEST_RET_ENC_EC(ret);
  27047. }
  27048. if (ret == 0) {
  27049. do {
  27050. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  27051. count++;
  27052. } while (ret == FP_WOULDBLOCK);
  27053. if (ret < 0)
  27054. ret = WC_TEST_RET_ENC_EC(ret);
  27055. }
  27056. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  27057. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  27058. #endif
  27059. if (ret == 0) {
  27060. ret = wc_ecc_check_key(&keyA);
  27061. if (ret < 0)
  27062. ret = WC_TEST_RET_ENC_EC(ret);
  27063. }
  27064. if (ret == 0) {
  27065. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  27066. privKey, curveId);
  27067. if (ret < 0)
  27068. ret = WC_TEST_RET_ENC_EC(ret);
  27069. }
  27070. count = 0;
  27071. if (ret == 0) {
  27072. do {
  27073. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  27074. count++;
  27075. } while (ret == FP_WOULDBLOCK);
  27076. if (ret < 0)
  27077. ret = WC_TEST_RET_ENC_EC(ret);
  27078. }
  27079. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  27080. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  27081. #endif
  27082. if (ret == 0) {
  27083. do {
  27084. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  27085. } while (ret == FP_WOULDBLOCK);
  27086. if (ret < 0)
  27087. ret = WC_TEST_RET_ENC_EC(ret);
  27088. }
  27089. if (ret == 0) {
  27090. if (secretSzA != secretSzB ||
  27091. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  27092. ret = WC_TEST_RET_ENC_NC;
  27093. }
  27094. }
  27095. wc_ecc_free(&keyA);
  27096. wc_ecc_free(&keyB);
  27097. return ret;
  27098. }
  27099. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  27100. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  27101. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  27102. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  27103. WC_RNG* rng)
  27104. {
  27105. wc_test_ret_t ret = 0;
  27106. byte* sig = NULL;
  27107. word32 sigSz = curveSz * 2;
  27108. static const byte hash[] = {
  27109. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  27110. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  27111. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  27112. };
  27113. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  27114. if (sig == NULL) {
  27115. ret = WC_TEST_RET_ENC_ERRNO;
  27116. }
  27117. if (ret == 0) {
  27118. /* Sign hash using private key */
  27119. /* Note: result of an ECC sign varies for each call even with same
  27120. private key and hash. This is because a new random public key is
  27121. used for each operation. */
  27122. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  27123. &sigSz, curveSz, curveId, rng);
  27124. }
  27125. if (ret == 0) {
  27126. /* Verify generated signature is valid */
  27127. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  27128. sigSz, curveSz, curveId);
  27129. }
  27130. if (sig != NULL) {
  27131. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  27132. }
  27133. return ret;
  27134. }
  27135. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  27136. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  27137. {
  27138. wc_test_ret_t ret = 0;
  27139. word32 i;
  27140. int curveIds[3] = {0, 0, 0};
  27141. word32 curveSzs[3] = {0, 0, 0};
  27142. const byte* privKeys[3] = {NULL, NULL, NULL};
  27143. word32 privKeySzs[3] = {0, 0, 0};
  27144. const byte* pubKeys[3] = {NULL, NULL, NULL};
  27145. word32 pubKeySzs[3] = {0, 0, 0};
  27146. curveIds[0] = ECC_SECP256R1;
  27147. curveSzs[0] = 32;
  27148. privKeys[0] = p256PrivKey;
  27149. privKeySzs[0] = sizeof(p256PrivKey);
  27150. pubKeys[0] = p256PubKey;
  27151. pubKeySzs[0] = sizeof(p256PubKey);
  27152. #ifdef HAVE_ECC384
  27153. curveIds[1] = ECC_SECP384R1;
  27154. curveSzs[1] = 48;
  27155. privKeys[1] = p384PrivKey;
  27156. privKeySzs[1] = sizeof(p384PrivKey);
  27157. pubKeys[1] = p384PubKey;
  27158. pubKeySzs[1] = sizeof(p384PubKey);
  27159. #endif
  27160. #ifdef HAVE_ECC521
  27161. curveIds[2] = ECC_SECP521R1;
  27162. curveSzs[2] = 66;
  27163. privKeys[2] = p521PrivKey;
  27164. privKeySzs[2] = sizeof(p521PrivKey);
  27165. pubKeys[2] = p521PubKey;
  27166. pubKeySzs[2] = sizeof(p521PubKey);
  27167. #endif
  27168. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  27169. if (curveIds[i] == 0) {
  27170. continue;
  27171. }
  27172. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  27173. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  27174. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  27175. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  27176. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  27177. if (ret == 0) {
  27178. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  27179. pubKeys[i], rng);
  27180. }
  27181. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  27182. }
  27183. return ret;
  27184. }
  27185. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  27186. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  27187. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  27188. (HAVE_FIPS_VERSION > 2)))
  27189. static int ecc_test_raw_enc_dec(void)
  27190. {
  27191. int ret;
  27192. unsigned char r[1];
  27193. word32 rSz;
  27194. unsigned char s[1];
  27195. word32 sSz;
  27196. unsigned char rZero[] = { 0, 0, 0, 0 };
  27197. unsigned char sOne[] = { 0, 0, 1 };
  27198. unsigned char sigRaw[32];
  27199. word32 sigRawSz;
  27200. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  27201. sigRawSz = sizeof(sigRaw);
  27202. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  27203. sigRaw, &sigRawSz);
  27204. if (ret != 0) {
  27205. return WC_TEST_RET_ENC_EC(ret);
  27206. }
  27207. if (sigRawSz != sizeof(expSig)) {
  27208. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  27209. }
  27210. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  27211. return WC_TEST_RET_ENC_NC;
  27212. }
  27213. rSz = sizeof(r);
  27214. sSz = sizeof(s);
  27215. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  27216. if (ret != 0) {
  27217. return WC_TEST_RET_ENC_EC(ret);
  27218. }
  27219. if (rSz != 1) {
  27220. return WC_TEST_RET_ENC_EC((int)rSz);
  27221. }
  27222. if (sSz != 1) {
  27223. return WC_TEST_RET_ENC_EC((int)sSz);
  27224. }
  27225. if (r[0] != 0) {
  27226. return WC_TEST_RET_ENC_EC(r[0]);
  27227. }
  27228. if (s[0] != 1) {
  27229. return WC_TEST_RET_ENC_EC(s[0]);
  27230. }
  27231. return ret;
  27232. }
  27233. #endif
  27234. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  27235. {
  27236. wc_test_ret_t ret;
  27237. WC_RNG rng;
  27238. WOLFSSL_ENTER("ecc_test");
  27239. #if defined(ECC_MIN_KEY_SZ)
  27240. WOLFSSL_MSG_EX("ecc_test ECC_MIN_KEY_SZ = %d\n", ECC_MIN_KEY_SZ);
  27241. #else
  27242. WOLFSSL_MSG("ecc_test ECC_MIN_KEY_SZ not defined.");
  27243. #endif
  27244. #if defined(WOLFSSL_CERT_EXT) && \
  27245. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27246. ret = ecc_decode_test();
  27247. if (ret < 0)
  27248. return ret;
  27249. #endif
  27250. #ifndef HAVE_FIPS
  27251. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27252. #else
  27253. ret = wc_InitRng(&rng);
  27254. #endif
  27255. #ifndef WC_NO_RNG
  27256. if (ret != 0)
  27257. return WC_TEST_RET_ENC_EC(ret);
  27258. #else
  27259. (void)ret;
  27260. #endif
  27261. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  27262. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  27263. if (ret < 0) {
  27264. printf("keySize=14, Default\n");
  27265. goto done;
  27266. }
  27267. #endif /* HAVE_ECC112 */
  27268. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  27269. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  27270. if (ret < 0) {
  27271. printf("keySize=16, Default\n");
  27272. goto done;
  27273. }
  27274. #endif /* HAVE_ECC128 */
  27275. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  27276. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  27277. if (ret < 0) {
  27278. printf("keySize=20, Default\n");
  27279. goto done;
  27280. }
  27281. #endif /* HAVE_ECC160 */
  27282. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  27283. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  27284. if (ret < 0) {
  27285. printf("keySize=24, Default\n");
  27286. goto done;
  27287. }
  27288. #endif /* HAVE_ECC192 */
  27289. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  27290. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  27291. if (ret < 0) {
  27292. printf("keySize=28, Default\n");
  27293. goto done;
  27294. }
  27295. #endif /* HAVE_ECC224 */
  27296. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  27297. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  27298. if (ret < 0) {
  27299. printf("keySize=30, Default\n");
  27300. goto done;
  27301. }
  27302. #endif /* HAVE_ECC239 */
  27303. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27304. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  27305. if (ret < 0) {
  27306. printf("keySize=32, Default\n");
  27307. goto done;
  27308. }
  27309. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27310. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27311. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27312. ret = ecc_point_test();
  27313. if (ret < 0) {
  27314. goto done;
  27315. }
  27316. #endif
  27317. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  27318. ret = ecc_def_curve_test(&rng);
  27319. if (ret < 0) {
  27320. printf("Default\n");
  27321. goto done;
  27322. }
  27323. #endif
  27324. #endif /* !NO_ECC256 */
  27325. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  27326. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  27327. if (ret < 0) {
  27328. printf("keySize=40, Default\n");
  27329. goto done;
  27330. }
  27331. #endif /* HAVE_ECC320 */
  27332. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  27333. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  27334. if (ret < 0) {
  27335. printf("keySize=48, Default\n");
  27336. goto done;
  27337. }
  27338. #endif /* HAVE_ECC384 */
  27339. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  27340. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  27341. if (ret < 0) {
  27342. printf("keySize=64, Default\n");
  27343. goto done;
  27344. }
  27345. #endif /* HAVE_ECC512 */
  27346. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  27347. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  27348. if (ret < 0) {
  27349. printf("keySize=66, Default\n");
  27350. goto done;
  27351. }
  27352. #endif /* HAVE_ECC521 */
  27353. #ifdef WOLFSSL_SM2
  27354. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  27355. if (ret < 0) {
  27356. printf("SM2\n");
  27357. goto done;
  27358. }
  27359. #endif
  27360. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  27361. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  27362. (HAVE_FIPS_VERSION > 2)))
  27363. ret = ecc_test_raw_enc_dec();
  27364. if (ret != 0) {
  27365. printf("raw sig encode/decode\n");
  27366. goto done;
  27367. }
  27368. #endif
  27369. #if defined(WOLFSSL_CUSTOM_CURVES)
  27370. ret = ecc_test_custom_curves(&rng);
  27371. if (ret != 0) {
  27372. printf("Custom\n");
  27373. goto done;
  27374. }
  27375. #endif
  27376. #if defined(WOLFSSL_SM2)
  27377. ret = test_sm2_verify();
  27378. if (ret != 0) {
  27379. printf("SM2 Verify\n");
  27380. goto done;
  27381. }
  27382. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  27383. if (ret != 0) {
  27384. printf("SM2 test\n");
  27385. goto done;
  27386. }
  27387. #endif
  27388. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  27389. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  27390. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  27391. #ifdef HAVE_ECC256
  27392. ret = ecc_test_deterministic_k(&rng);
  27393. if (ret != 0) {
  27394. printf("ecc_test_deterministic_k failed!\n");
  27395. goto done;
  27396. }
  27397. #endif
  27398. #ifdef WOLFSSL_PUBLIC_MP
  27399. #if defined(HAVE_ECC384)
  27400. ret = ecc384_test_deterministic_k(&rng);
  27401. if (ret != 0) {
  27402. printf("ecc384_test_deterministic_k failed!\n");
  27403. goto done;
  27404. }
  27405. #endif
  27406. #if defined(HAVE_ECC521)
  27407. ret = ecc521_test_deterministic_k(&rng);
  27408. if (ret != 0) {
  27409. printf("ecc512_test_deterministic_k failed!\n");
  27410. goto done;
  27411. }
  27412. #endif
  27413. #endif
  27414. #endif
  27415. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  27416. !defined(WOLFSSL_KCAPI_ECC)
  27417. ret = ecc_test_sign_vectors(&rng);
  27418. if (ret != 0) {
  27419. printf("ecc_test_sign_vectors failed!\n");
  27420. goto done;
  27421. }
  27422. #endif
  27423. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  27424. defined(HAVE_ECC_DHE)
  27425. ret = ecc_test_cdh_vectors(&rng);
  27426. if (ret != 0) {
  27427. printf("ecc_test_cdh_vectors failed!\n");
  27428. goto done;
  27429. }
  27430. #endif
  27431. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27432. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  27433. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  27434. ret = ecc_test_make_pub(&rng);
  27435. if (ret != 0) {
  27436. printf("ecc_test_make_pub failed!\n");
  27437. goto done;
  27438. }
  27439. #elif defined(HAVE_ECC_KEY_IMPORT)
  27440. (void)ecc_test_make_pub; /* for compiler warning */
  27441. #endif
  27442. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  27443. ret = ecc_test_cert_gen(&rng);
  27444. if (ret != 0) {
  27445. printf("ecc_test_cert_gen failed!\n");
  27446. goto done;
  27447. }
  27448. #endif
  27449. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  27450. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  27451. defined(WOLFSSL_CUSTOM_CURVES))
  27452. ret = ecc_test_allocator(&rng);
  27453. if (ret != 0) {
  27454. printf("ecc_test_allocator failed!\n");
  27455. goto done;
  27456. }
  27457. #endif
  27458. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  27459. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  27460. ret = ecc_test_nonblock(&rng);
  27461. if (ret != 0) {
  27462. printf("ecc_test_nonblock failed!\n");
  27463. goto done;
  27464. }
  27465. #endif
  27466. done:
  27467. wc_FreeRng(&rng);
  27468. return ret;
  27469. }
  27470. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  27471. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  27472. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  27473. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  27474. {
  27475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27476. byte* plaintext;
  27477. byte* encrypted;
  27478. byte* decrypted;
  27479. #else
  27480. byte plaintext[128];
  27481. byte encrypted[128];
  27482. byte decrypted[128];
  27483. #endif
  27484. ecEncCtx* aCtx = NULL;
  27485. ecEncCtx* bCtx = NULL;
  27486. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  27487. 14, 15};
  27488. wc_test_ret_t ret = 0;
  27489. static const char message[] = "Hello wolfSSL!";
  27490. word32 plaintextLen;
  27491. word32 encryptLen = 128;
  27492. word32 decryptLen = 128;
  27493. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27494. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27495. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27496. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27497. #endif
  27498. wc_ecc_free(a);
  27499. wc_ecc_free(b);
  27500. ret = wc_ecc_init(a);
  27501. if (ret != 0)
  27502. ret = WC_TEST_RET_ENC_EC(ret);
  27503. if (ret == 0) {
  27504. ret = wc_ecc_init(b);
  27505. if (ret != 0)
  27506. ret = WC_TEST_RET_ENC_EC(ret);
  27507. }
  27508. if (ret == 0)
  27509. ret = wc_ecc_make_key(rng, 32, a);
  27510. if (ret == 0)
  27511. ret = wc_ecc_make_key(rng, 32, b);
  27512. /* create context */
  27513. if (ret == 0) {
  27514. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  27515. if (aCtx == NULL)
  27516. ret = WC_TEST_RET_ENC_NC;
  27517. }
  27518. if (ret == 0) {
  27519. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  27520. if (bCtx == NULL)
  27521. ret = WC_TEST_RET_ENC_NC;
  27522. }
  27523. /* set salt */
  27524. if (ret == 0) {
  27525. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  27526. if (ret != 0)
  27527. ret = 10472;
  27528. }
  27529. if (ret == 0) {
  27530. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  27531. if (ret != 0)
  27532. ret = 10473;
  27533. }
  27534. XMEMSET(plaintext, 0, 128);
  27535. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  27536. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  27537. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  27538. /* encrypt */
  27539. if (ret == 0) {
  27540. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  27541. &encryptLen, aCtx);
  27542. if (ret != 0)
  27543. ret = WC_TEST_RET_ENC_EC(ret);
  27544. }
  27545. /* decrypt */
  27546. if (ret == 0) {
  27547. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  27548. &decryptLen, bCtx);
  27549. if (ret != 0)
  27550. ret = WC_TEST_RET_ENC_EC(ret);
  27551. }
  27552. /* compare */
  27553. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  27554. ret = WC_TEST_RET_ENC_NC;
  27555. wc_ecc_free(a);
  27556. wc_ecc_free(b);
  27557. wc_ecc_ctx_free(aCtx);
  27558. wc_ecc_ctx_free(bCtx);
  27559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27560. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27561. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27562. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27563. #endif
  27564. return ret;
  27565. }
  27566. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  27567. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  27568. * wolfFIPS 5.3.
  27569. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  27570. */
  27571. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  27572. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  27573. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  27574. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  27575. {
  27576. wc_test_ret_t ret = 0;
  27577. #ifdef WOLFSSL_ECIES_OLD
  27578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27579. ecc_key* userA = NULL;
  27580. #else
  27581. ecc_key userA[1];
  27582. #endif
  27583. int userAInit = 0;
  27584. #endif
  27585. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27586. ecc_key* userB = NULL;
  27587. #else
  27588. ecc_key userB[1];
  27589. #endif
  27590. int userBInit = 0;
  27591. ecc_key* tmpKey;
  27592. byte plain[48];
  27593. word32 plainSz = sizeof(plain);
  27594. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  27595. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  27596. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  27597. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  27598. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  27599. };
  27600. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  27601. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  27602. 0x04,
  27603. /* X */
  27604. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  27605. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  27606. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  27607. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  27608. /* X */
  27609. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  27610. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  27611. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  27612. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  27613. };
  27614. #endif
  27615. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  27616. #ifdef WOLFSSL_ECIES_OLD
  27617. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  27618. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  27619. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  27620. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  27621. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  27622. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  27623. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  27624. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  27625. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  27626. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  27627. #elif defined(WOLFSSL_ECIES_ISO18033)
  27628. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  27629. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  27630. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  27631. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  27632. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  27633. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  27634. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  27635. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  27636. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  27637. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  27638. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  27639. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  27640. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  27641. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  27642. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  27643. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  27644. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  27645. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  27646. 0x7f
  27647. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27648. /* EC P-256 point */
  27649. 0x04,
  27650. /* X */
  27651. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  27652. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  27653. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  27654. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  27655. /* Y */
  27656. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  27657. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  27658. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  27659. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  27660. /* IV */
  27661. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27662. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27663. /* Encrypted Msg */
  27664. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  27665. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  27666. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  27667. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  27668. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  27669. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  27670. /* HMAC */
  27671. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  27672. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  27673. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  27674. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  27675. #else
  27676. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  27677. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  27678. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  27679. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  27680. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  27681. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  27682. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  27683. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  27684. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  27685. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  27686. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  27687. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  27688. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  27689. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  27690. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  27691. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  27692. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  27693. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  27694. 0x55
  27695. #endif
  27696. };
  27697. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  27698. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27699. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  27700. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  27701. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  27702. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  27703. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  27704. };
  27705. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27706. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  27707. DYNAMIC_TYPE_TMP_BUFFER);
  27708. if (userB == NULL) {
  27709. ret = WC_TEST_RET_ENC_ERRNO;
  27710. }
  27711. #ifdef WOLFSSL_ECIES_OLD
  27712. if (ret == 0) {
  27713. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  27714. DYNAMIC_TYPE_TMP_BUFFER);
  27715. if (userA == NULL) {
  27716. ret = WC_TEST_RET_ENC_ERRNO;
  27717. }
  27718. }
  27719. #endif
  27720. #endif
  27721. if (ret == 0) {
  27722. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  27723. if (ret != 0)
  27724. ret = WC_TEST_RET_ENC_EC(ret);
  27725. }
  27726. if (ret == 0) {
  27727. userBInit = 1;
  27728. #ifdef WOLFSSL_ECIES_OLD
  27729. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  27730. if (ret != 0)
  27731. ret = WC_TEST_RET_ENC_EC(ret);
  27732. }
  27733. if (ret == 0) {
  27734. userAInit = 1;
  27735. tmpKey = userA;
  27736. #else
  27737. tmpKey = NULL;
  27738. #endif
  27739. }
  27740. if (ret == 0) {
  27741. #ifdef WOLFSSL_QNX_CAAM
  27742. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  27743. sizeof(pubKey), userB, ECC_SECP256R1);
  27744. #else
  27745. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  27746. userB, ECC_SECP256R1);
  27747. #endif
  27748. if (ret != 0)
  27749. ret = WC_TEST_RET_ENC_EC(ret);
  27750. }
  27751. #ifdef WOLFSSL_ECIES_OLD
  27752. if (ret == 0) {
  27753. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  27754. ECC_SECP256R1);
  27755. if (ret != 0)
  27756. ret = WC_TEST_RET_ENC_EC(ret);
  27757. }
  27758. #endif
  27759. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27760. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27761. !defined(HAVE_SELFTEST)
  27762. if (ret == 0) {
  27763. ret = wc_ecc_set_rng(userB, rng);
  27764. if (ret != 0) {
  27765. ret = WC_TEST_RET_ENC_EC(ret);
  27766. }
  27767. }
  27768. #else
  27769. (void)rng;
  27770. #endif
  27771. if (ret == 0) {
  27772. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  27773. &plainSz, NULL);
  27774. if (ret != 0)
  27775. ret = WC_TEST_RET_ENC_EC(ret);
  27776. }
  27777. if (ret == 0) {
  27778. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27779. ret = WC_TEST_RET_ENC_NC;
  27780. }
  27781. }
  27782. if (userBInit)
  27783. wc_ecc_free(userB);
  27784. #ifdef WOLFSSL_ECIES_OLD
  27785. if (userAInit)
  27786. wc_ecc_free(userA);
  27787. #endif
  27788. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27789. if (userB != NULL) {
  27790. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27791. }
  27792. #ifdef WOLFSSL_ECIES_OLD
  27793. if (userA != NULL) {
  27794. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27795. }
  27796. #endif
  27797. #endif
  27798. return ret;
  27799. }
  27800. #endif
  27801. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  27802. byte encAlgo, byte kdfAlgo, byte macAlgo)
  27803. {
  27804. wc_test_ret_t ret = 0;
  27805. byte msg[48];
  27806. byte plain[48];
  27807. #ifdef WOLFSSL_ECIES_OLD
  27808. byte out[80];
  27809. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27810. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  27811. #else
  27812. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  27813. #endif
  27814. word32 outSz = sizeof(out);
  27815. word32 plainSz = sizeof(plain);
  27816. int i;
  27817. ecEncCtx* cliCtx = NULL;
  27818. ecEncCtx* srvCtx = NULL;
  27819. byte cliSalt[EXCHANGE_SALT_SZ];
  27820. byte srvSalt[EXCHANGE_SALT_SZ];
  27821. const byte* tmpSalt;
  27822. byte msg2[48];
  27823. byte plain2[48];
  27824. #ifdef WOLFSSL_ECIES_OLD
  27825. byte out2[80];
  27826. #elif defined(WOLFSSL_ECIES_GEN_IV)
  27827. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  27828. #else
  27829. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  27830. #endif
  27831. word32 outSz2 = sizeof(out2);
  27832. word32 plainSz2 = sizeof(plain2);
  27833. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27834. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  27835. DYNAMIC_TYPE_TMP_BUFFER);
  27836. #else
  27837. ecc_key tmpKey[1];
  27838. #endif
  27839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27840. if (tmpKey == NULL) {
  27841. ERROR_OUT(MEMORY_E, done);
  27842. }
  27843. #endif
  27844. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  27845. if (ret != 0)
  27846. goto done;
  27847. /* set message to incrementing 0,1,2,etc... */
  27848. for (i = 0; i < (int)sizeof(msg); i++)
  27849. msg[i] = i;
  27850. /* encrypt msg to B */
  27851. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  27852. if (ret != 0) {
  27853. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27854. }
  27855. #ifdef WOLFSSL_ECIES_OLD
  27856. tmpKey->dp = userA->dp;
  27857. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  27858. if (ret != 0) {
  27859. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27860. }
  27861. #endif
  27862. /* decrypt msg from A */
  27863. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  27864. if (ret != 0) {
  27865. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27866. }
  27867. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27868. ret = WC_TEST_RET_ENC_NC; goto done;
  27869. }
  27870. #ifndef WOLFSSL_ECIES_OLD
  27871. /* A decrypts msg (response) from B */
  27872. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  27873. if (ret != 0)
  27874. goto done;
  27875. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27876. ret = WC_TEST_RET_ENC_NC; goto done;
  27877. }
  27878. #endif
  27879. /* let's verify message exchange works, A is client, B is server */
  27880. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  27881. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  27882. if (cliCtx == NULL || srvCtx == NULL) {
  27883. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  27884. }
  27885. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  27886. if (ret != 0)
  27887. goto done;
  27888. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  27889. if (ret != 0)
  27890. goto done;
  27891. /* get salt to send to peer */
  27892. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  27893. if (tmpSalt == NULL) {
  27894. ret = WC_TEST_RET_ENC_NC; goto done;
  27895. }
  27896. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27897. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  27898. if (tmpSalt == NULL) {
  27899. ret = WC_TEST_RET_ENC_NC; goto done;
  27900. }
  27901. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27902. /* in actual use, we'd get the peer's salt over the transport */
  27903. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  27904. if (ret != 0)
  27905. goto done;
  27906. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  27907. if (ret != 0)
  27908. goto done;
  27909. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  27910. if (ret != 0)
  27911. goto done;
  27912. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  27913. if (ret != 0)
  27914. goto done;
  27915. /* get encrypted msg (request) to send to B */
  27916. outSz = sizeof(out);
  27917. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  27918. if (ret != 0)
  27919. goto done;
  27920. #ifndef WOLFSSL_ECIES_OLD
  27921. wc_ecc_free(tmpKey);
  27922. #endif
  27923. /* B decrypts msg (request) from A */
  27924. plainSz = sizeof(plain);
  27925. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  27926. if (ret != 0)
  27927. goto done;
  27928. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  27929. ret = WC_TEST_RET_ENC_NC; goto done;
  27930. }
  27931. /* msg2 (response) from B to A */
  27932. for (i = 0; i < (int)sizeof(msg2); i++)
  27933. msg2[i] = i + sizeof(msg2);
  27934. /* get encrypted msg (response) to send to B */
  27935. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  27936. &outSz2, srvCtx);
  27937. if (ret != 0)
  27938. goto done;
  27939. #ifdef WOLFSSL_ECIES_OLD
  27940. tmpKey->dp = userB->dp;
  27941. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  27942. if (ret != 0) {
  27943. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  27944. }
  27945. #else
  27946. wc_ecc_free(tmpKey);
  27947. #endif
  27948. /* A decrypts msg (response) from B */
  27949. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  27950. cliCtx);
  27951. if (ret != 0)
  27952. goto done;
  27953. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  27954. ret = WC_TEST_RET_ENC_NC; goto done;
  27955. }
  27956. #if defined(HAVE_COMP_KEY) && \
  27957. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  27958. /* Create new client and server contexts. */
  27959. wc_ecc_ctx_free(srvCtx);
  27960. wc_ecc_ctx_free(cliCtx);
  27961. /* let's verify message exchange works, A is client, B is server */
  27962. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  27963. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  27964. if (cliCtx == NULL || srvCtx == NULL) {
  27965. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  27966. }
  27967. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  27968. if (ret != 0)
  27969. goto done;
  27970. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  27971. if (ret != 0)
  27972. goto done;
  27973. /* get salt to send to peer */
  27974. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  27975. if (tmpSalt == NULL) {
  27976. ret = WC_TEST_RET_ENC_NC; goto done;
  27977. }
  27978. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27979. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  27980. if (tmpSalt == NULL) {
  27981. ret = WC_TEST_RET_ENC_NC; goto done;
  27982. }
  27983. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  27984. /* in actual use, we'd get the peer's salt over the transport */
  27985. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  27986. if (ret != 0)
  27987. goto done;
  27988. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  27989. if (ret != 0)
  27990. goto done;
  27991. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  27992. if (ret != 0)
  27993. goto done;
  27994. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  27995. if (ret != 0)
  27996. goto done;
  27997. /* get encrypted msg (request) to send to B - compressed public key */
  27998. outSz = sizeof(out);
  27999. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  28000. 1);
  28001. if (ret != 0)
  28002. goto done;
  28003. #ifndef WOLFSSL_ECIES_OLD
  28004. wc_ecc_free(tmpKey);
  28005. #endif
  28006. /* B decrypts msg (request) from A - out has a compressed public key */
  28007. plainSz = sizeof(plain);
  28008. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  28009. if (ret != 0)
  28010. goto done;
  28011. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  28012. ret = WC_TEST_RET_ENC_NC; goto done;
  28013. }
  28014. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  28015. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  28016. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  28017. ret = ecc_encrypt_kat(rng);
  28018. #endif
  28019. done:
  28020. /* cleanup */
  28021. wc_ecc_ctx_free(srvCtx);
  28022. wc_ecc_ctx_free(cliCtx);
  28023. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28024. if (tmpKey != NULL) {
  28025. wc_ecc_free(tmpKey);
  28026. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28027. }
  28028. #else
  28029. wc_ecc_free(tmpKey);
  28030. #endif
  28031. return ret;
  28032. }
  28033. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  28034. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  28035. {
  28036. WC_RNG rng;
  28037. wc_test_ret_t ret;
  28038. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28039. ecc_key *userA;
  28040. ecc_key *userB;
  28041. #else
  28042. ecc_key userA[1];
  28043. ecc_key userB[1];
  28044. #endif
  28045. WOLFSSL_ENTER("ecc_encrypt_test");
  28046. #ifndef HAVE_FIPS
  28047. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28048. #else
  28049. ret = wc_InitRng(&rng);
  28050. #endif
  28051. if (ret != 0)
  28052. return WC_TEST_RET_ENC_EC(ret);
  28053. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28054. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  28055. DYNAMIC_TYPE_TMP_BUFFER);
  28056. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  28057. DYNAMIC_TYPE_TMP_BUFFER);
  28058. if ((userA == NULL) || (userB == NULL)) {
  28059. ERROR_OUT(MEMORY_E, done);
  28060. }
  28061. #endif
  28062. XMEMSET(userA, 0, sizeof *userA);
  28063. XMEMSET(userB, 0, sizeof *userB);
  28064. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  28065. if (ret != 0)
  28066. goto done;
  28067. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  28068. if (ret != 0)
  28069. goto done;
  28070. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  28071. #if defined(WOLFSSL_ASYNC_CRYPT)
  28072. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  28073. #endif
  28074. if (ret != 0){
  28075. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  28076. }
  28077. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  28078. #if defined(WOLFSSL_ASYNC_CRYPT)
  28079. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  28080. #endif
  28081. if (ret != 0){
  28082. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  28083. }
  28084. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  28085. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  28086. !defined(HAVE_SELFTEST)
  28087. ret = wc_ecc_set_rng(userA, &rng);
  28088. if (ret != 0) {
  28089. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  28090. }
  28091. ret = wc_ecc_set_rng(userB, &rng);
  28092. if (ret != 0) {
  28093. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  28094. }
  28095. #endif
  28096. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  28097. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  28098. #ifdef WOLFSSL_AES_128
  28099. if (ret == 0) {
  28100. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  28101. ecHKDF_SHA256, ecHMAC_SHA256);
  28102. if (ret != 0) {
  28103. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  28104. }
  28105. }
  28106. #ifdef HAVE_X963_KDF
  28107. if (ret == 0) {
  28108. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  28109. ecKDF_X963_SHA256, ecHMAC_SHA256);
  28110. if (ret != 0) {
  28111. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  28112. }
  28113. }
  28114. if (ret == 0) {
  28115. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  28116. ecKDF_SHA256, ecHMAC_SHA256);
  28117. if (ret != 0) {
  28118. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  28119. }
  28120. }
  28121. #endif
  28122. #endif
  28123. #ifdef WOLFSSL_AES_256
  28124. if (ret == 0) {
  28125. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  28126. ecHKDF_SHA256, ecHMAC_SHA256);
  28127. if (ret != 0) {
  28128. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  28129. }
  28130. }
  28131. #endif
  28132. #endif
  28133. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  28134. #ifdef WOLFSSL_AES_128
  28135. if (ret == 0) {
  28136. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  28137. ecHKDF_SHA256, ecHMAC_SHA256);
  28138. if (ret != 0) {
  28139. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  28140. }
  28141. }
  28142. #endif
  28143. #ifdef WOLFSSL_AES_256
  28144. if (ret == 0) {
  28145. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  28146. ecHKDF_SHA256, ecHMAC_SHA256);
  28147. if (ret != 0) {
  28148. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  28149. }
  28150. }
  28151. #endif
  28152. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  28153. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  28154. if (ret == 0) {
  28155. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  28156. }
  28157. #endif
  28158. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  28159. done:
  28160. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28161. if (userA != NULL) {
  28162. wc_ecc_free(userA);
  28163. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28164. }
  28165. if (userB != NULL) {
  28166. wc_ecc_free(userB);
  28167. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28168. }
  28169. #else
  28170. wc_ecc_free(userB);
  28171. wc_ecc_free(userA);
  28172. #endif
  28173. wc_FreeRng(&rng);
  28174. return ret;
  28175. }
  28176. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  28177. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  28178. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  28179. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  28180. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  28181. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  28182. {
  28183. size_t bytes;
  28184. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28185. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28186. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28187. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28188. #else
  28189. ecc_key cliKey[1];
  28190. ecc_key servKey[1];
  28191. ecc_key tmpKey[1];
  28192. #endif
  28193. WC_RNG rng;
  28194. word32 idx = 0;
  28195. wc_test_ret_t ret;
  28196. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  28197. byte in[] = "Everyone gets Friday off. ecc p";
  28198. word32 inLen = (word32)XSTRLEN((char*)in);
  28199. byte out[256];
  28200. byte plain[256];
  28201. int verify = 0;
  28202. word32 x;
  28203. WOLFSSL_ENTER("ecc_test_buffers");
  28204. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28205. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  28206. ERROR_OUT(MEMORY_E, done);
  28207. #endif
  28208. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  28209. if (ret != 0)
  28210. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28211. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  28212. if (ret != 0)
  28213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28214. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  28215. if (ret != 0)
  28216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28217. bytes = (size_t)sizeof_ecc_clikey_der_256;
  28218. /* place client key into ecc_key struct cliKey */
  28219. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  28220. (word32)bytes);
  28221. if (ret != 0)
  28222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28223. idx = 0;
  28224. bytes = (size_t)sizeof_ecc_key_der_256;
  28225. /* place server key into ecc_key struct servKey */
  28226. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  28227. (word32)bytes);
  28228. if (ret != 0)
  28229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28230. #ifndef WC_NO_RNG
  28231. #ifndef HAVE_FIPS
  28232. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28233. #else
  28234. ret = wc_InitRng(&rng);
  28235. #endif
  28236. if (ret != 0)
  28237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28238. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  28239. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  28240. !defined(HAVE_SELFTEST)
  28241. ret = wc_ecc_set_rng(cliKey, &rng);
  28242. if (ret != 0)
  28243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28244. ret = wc_ecc_set_rng(servKey, &rng);
  28245. if (ret != 0)
  28246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28247. #endif
  28248. #endif /* !WC_NO_RNG */
  28249. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  28250. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  28251. {
  28252. word32 y;
  28253. /* test encrypt and decrypt if they're available */
  28254. x = sizeof(out);
  28255. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  28256. if (ret < 0)
  28257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28258. #ifdef WOLFSSL_ECIES_OLD
  28259. tmpKey->dp = cliKey->dp;
  28260. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  28261. if (ret != 0) {
  28262. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  28263. }
  28264. #endif
  28265. y = sizeof(plain);
  28266. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  28267. if (ret < 0)
  28268. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28269. if (XMEMCMP(plain, in, inLen))
  28270. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28271. }
  28272. #endif
  28273. x = sizeof(out);
  28274. do {
  28275. #if defined(WOLFSSL_ASYNC_CRYPT)
  28276. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28277. #endif
  28278. if (ret == 0)
  28279. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  28280. } while (ret == WC_PENDING_E);
  28281. if (ret < 0)
  28282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28283. TEST_SLEEP();
  28284. XMEMSET(plain, 0, sizeof(plain));
  28285. do {
  28286. #if defined(WOLFSSL_ASYNC_CRYPT)
  28287. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28288. #endif
  28289. if (ret == 0)
  28290. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  28291. cliKey);
  28292. } while (ret == WC_PENDING_E);
  28293. if (ret < 0)
  28294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28295. if (verify != 1)
  28296. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28297. TEST_SLEEP();
  28298. #ifdef WOLFSSL_CERT_EXT
  28299. idx = 0;
  28300. bytes = sizeof_ecc_clikeypub_der_256;
  28301. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  28302. (word32) bytes);
  28303. if (ret != 0)
  28304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28305. #endif
  28306. ret = 0;
  28307. done:
  28308. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28309. if (cliKey != NULL) {
  28310. wc_ecc_free(cliKey);
  28311. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28312. }
  28313. if (servKey != NULL) {
  28314. wc_ecc_free(servKey);
  28315. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28316. }
  28317. if (tmpKey != NULL) {
  28318. wc_ecc_free(tmpKey);
  28319. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28320. }
  28321. #else
  28322. wc_ecc_free(cliKey);
  28323. wc_ecc_free(servKey);
  28324. wc_ecc_free(tmpKey);
  28325. #endif
  28326. wc_FreeRng(&rng);
  28327. return ret;
  28328. }
  28329. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  28330. #endif /* HAVE_ECC */
  28331. #ifdef HAVE_CURVE25519
  28332. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  28333. defined(HAVE_CURVE25519_KEY_IMPORT)
  28334. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  28335. #define X25519_TEST_CNT 5
  28336. #else
  28337. #define X25519_TEST_CNT 1
  28338. #endif
  28339. static wc_test_ret_t curve25519_overflow_test(void)
  28340. {
  28341. /* secret key for party a */
  28342. byte sa[X25519_TEST_CNT][32] = {
  28343. {
  28344. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  28345. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  28346. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  28347. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  28348. },
  28349. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  28350. {
  28351. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  28352. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  28353. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  28354. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  28355. },
  28356. {
  28357. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  28358. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  28359. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  28360. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  28361. },
  28362. {
  28363. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  28364. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  28365. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  28366. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  28367. },
  28368. {
  28369. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  28370. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  28371. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  28372. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  28373. }
  28374. #endif
  28375. };
  28376. /* public key for party b */
  28377. byte pb[X25519_TEST_CNT][32] = {
  28378. {
  28379. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28380. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28381. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28382. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  28383. },
  28384. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  28385. {
  28386. /* 0xff first byte in original - invalid! */
  28387. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28388. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28389. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  28390. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  28391. },
  28392. {
  28393. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  28394. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  28395. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  28396. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  28397. },
  28398. {
  28399. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  28400. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  28401. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  28402. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  28403. },
  28404. {
  28405. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  28406. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  28407. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  28408. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  28409. }
  28410. #endif
  28411. };
  28412. /* expected shared key */
  28413. byte ss[X25519_TEST_CNT][32] = {
  28414. {
  28415. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  28416. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  28417. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  28418. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  28419. },
  28420. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  28421. {
  28422. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  28423. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  28424. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  28425. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  28426. },
  28427. {
  28428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28429. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  28432. },
  28433. {
  28434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  28438. },
  28439. {
  28440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28441. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28442. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  28444. }
  28445. #endif
  28446. };
  28447. wc_test_ret_t ret = 0;
  28448. int i;
  28449. word32 y;
  28450. byte shared[32];
  28451. curve25519_key userA;
  28452. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  28453. for (i = 0; i < X25519_TEST_CNT; i++) {
  28454. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  28455. sizeof(pb[i]), &userA) != 0) {
  28456. ret = WC_TEST_RET_ENC_I(i); break;
  28457. }
  28458. /* test against known test vector */
  28459. XMEMSET(shared, 0, sizeof(shared));
  28460. y = sizeof(shared);
  28461. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  28462. ret = WC_TEST_RET_ENC_I(i); break;
  28463. }
  28464. if (XMEMCMP(ss[i], shared, y)) {
  28465. ret = WC_TEST_RET_ENC_I(i); break;
  28466. }
  28467. }
  28468. wc_curve25519_free(&userA);
  28469. return ret;
  28470. }
  28471. /* Test the wc_curve25519_check_public API.
  28472. *
  28473. * returns 0 on success and -ve on failure.
  28474. */
  28475. static wc_test_ret_t curve25519_check_public_test(void)
  28476. {
  28477. wc_test_ret_t ret;
  28478. /* Little-endian values that will fail */
  28479. byte fail_le[][CURVE25519_KEYSIZE] = {
  28480. {
  28481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  28485. },
  28486. {
  28487. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28488. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28489. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28490. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  28491. },
  28492. {
  28493. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28494. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28495. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28496. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  28497. },
  28498. };
  28499. /* Big-endian values that will fail */
  28500. byte fail_be[][CURVE25519_KEYSIZE] = {
  28501. {
  28502. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28503. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28504. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28505. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  28506. },
  28507. {
  28508. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28509. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28510. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28511. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  28512. },
  28513. {
  28514. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28515. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28516. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28517. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  28518. },
  28519. };
  28520. /* Good or valid public value */
  28521. byte good[CURVE25519_KEYSIZE] = {
  28522. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28523. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28524. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28525. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  28526. };
  28527. int i;
  28528. /* Parameter checks */
  28529. /* NULL pointer */
  28530. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  28531. if (ret != BAD_FUNC_ARG) {
  28532. return WC_TEST_RET_ENC_EC(ret);
  28533. }
  28534. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  28535. if (ret != BAD_FUNC_ARG) {
  28536. return WC_TEST_RET_ENC_EC(ret);
  28537. }
  28538. /* Length of 0 treated differently to other invalid lengths for TLS */
  28539. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  28540. if (ret != BUFFER_E)
  28541. return WC_TEST_RET_ENC_EC(ret);
  28542. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  28543. if (ret != BUFFER_E)
  28544. return WC_TEST_RET_ENC_EC(ret);
  28545. /* Length not CURVE25519_KEYSIZE */
  28546. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  28547. if (i == CURVE25519_KEYSIZE)
  28548. continue;
  28549. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  28550. ECC_BAD_ARG_E) {
  28551. return WC_TEST_RET_ENC_I(i);
  28552. }
  28553. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  28554. ECC_BAD_ARG_E) {
  28555. return WC_TEST_RET_ENC_I(i);
  28556. }
  28557. }
  28558. /* Little-endian fail cases */
  28559. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  28560. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  28561. EC25519_LITTLE_ENDIAN) == 0) {
  28562. return WC_TEST_RET_ENC_I(i);
  28563. }
  28564. }
  28565. /* Big-endian fail cases */
  28566. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  28567. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  28568. EC25519_BIG_ENDIAN) == 0) {
  28569. return WC_TEST_RET_ENC_I(i);
  28570. }
  28571. }
  28572. /* Check a valid public value works! */
  28573. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  28574. EC25519_LITTLE_ENDIAN);
  28575. if (ret != 0) {
  28576. return WC_TEST_RET_ENC_EC(ret);
  28577. }
  28578. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  28579. EC25519_BIG_ENDIAN);
  28580. if (ret != 0) {
  28581. return WC_TEST_RET_ENC_EC(ret);
  28582. }
  28583. return 0;
  28584. }
  28585. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  28586. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  28587. defined(HAVE_CURVE25519_KEY_IMPORT)
  28588. static wc_test_ret_t curve255519_der_test(void)
  28589. {
  28590. wc_test_ret_t ret = 0;
  28591. /* certs/statickeys/x25519.der */
  28592. const byte kCurve25519PrivDer[] = {
  28593. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  28594. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  28595. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  28596. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  28597. };
  28598. /* certs/statickeys/x25519-pub.der */
  28599. const byte kCurve25519PubDer[] = {
  28600. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  28601. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  28602. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  28603. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  28604. };
  28605. curve25519_key key;
  28606. byte output[128];
  28607. word32 outputSz = 128;
  28608. word32 idx;
  28609. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  28610. if (ret != 0)
  28611. return WC_TEST_RET_ENC_EC(ret);
  28612. /* Test decode / encode of Curve25519 private key only */
  28613. if (ret == 0) {
  28614. idx = 0;
  28615. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  28616. (word32)sizeof(kCurve25519PrivDer));
  28617. if (ret < 0)
  28618. ret = WC_TEST_RET_ENC_EC(ret);
  28619. }
  28620. if (ret == 0) {
  28621. outputSz = (word32)sizeof(output);
  28622. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  28623. if (ret >= 0) {
  28624. outputSz = (word32)ret;
  28625. ret = 0;
  28626. }
  28627. else {
  28628. ret = WC_TEST_RET_ENC_EC(ret);
  28629. }
  28630. }
  28631. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  28632. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  28633. ret = WC_TEST_RET_ENC_NC;
  28634. }
  28635. /* Test decode / encode of Curve25519 public key only */
  28636. if (ret == 0) {
  28637. idx = 0;
  28638. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  28639. (word32)sizeof(kCurve25519PubDer));
  28640. if (ret < 0)
  28641. ret = WC_TEST_RET_ENC_EC(ret);
  28642. }
  28643. if (ret == 0) {
  28644. outputSz = (word32)sizeof(output);
  28645. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  28646. if (ret >= 0) {
  28647. outputSz = (word32)ret;
  28648. ret = 0;
  28649. }
  28650. else {
  28651. ret = WC_TEST_RET_ENC_EC(ret);
  28652. }
  28653. }
  28654. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  28655. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  28656. ret = WC_TEST_RET_ENC_NC;
  28657. }
  28658. wc_curve25519_free(&key);
  28659. return ret;
  28660. }
  28661. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  28662. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  28663. {
  28664. WC_RNG rng;
  28665. wc_test_ret_t ret;
  28666. #ifdef HAVE_CURVE25519_SHARED_SECRET
  28667. byte sharedA[32];
  28668. byte sharedB[32];
  28669. word32 y;
  28670. #endif
  28671. #ifdef HAVE_CURVE25519_KEY_EXPORT
  28672. byte exportBuf[32];
  28673. #endif
  28674. word32 x = 0;
  28675. curve25519_key userA, userB, pubKey;
  28676. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  28677. defined(HAVE_CURVE25519_KEY_IMPORT)
  28678. /* test vectors from
  28679. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  28680. */
  28681. /* secret key for party a */
  28682. byte sa[] = {
  28683. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  28684. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  28685. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  28686. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  28687. };
  28688. /* public key for party a */
  28689. byte pa[] = {
  28690. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  28691. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  28692. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  28693. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  28694. };
  28695. /* secret key for party b */
  28696. byte sb[] = {
  28697. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  28698. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  28699. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  28700. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  28701. };
  28702. /* public key for party b */
  28703. byte pb[] = {
  28704. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  28705. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  28706. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  28707. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  28708. };
  28709. /* expected shared key */
  28710. byte ss[] = {
  28711. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  28712. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  28713. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  28714. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  28715. };
  28716. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  28717. (void)x;
  28718. WOLFSSL_ENTER("curve25519_test");
  28719. #ifndef HAVE_FIPS
  28720. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28721. #else
  28722. ret = wc_InitRng(&rng);
  28723. #endif
  28724. if (ret != 0)
  28725. return WC_TEST_RET_ENC_EC(ret);
  28726. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  28727. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  28728. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  28729. /* make curve25519 keys */
  28730. ret = wc_curve25519_make_key(&rng, 32, &userA);
  28731. if (ret != 0)
  28732. return WC_TEST_RET_ENC_EC(ret);
  28733. ret = wc_curve25519_make_key(&rng, 32, &userB);
  28734. if (ret != 0)
  28735. return WC_TEST_RET_ENC_EC(ret);
  28736. #ifdef HAVE_CURVE25519_SHARED_SECRET
  28737. /* find shared secret key */
  28738. x = sizeof(sharedA);
  28739. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  28740. printf("wc_curve25519_shared_secret 1 failed\n");
  28741. return WC_TEST_RET_ENC_EC(ret);
  28742. }
  28743. y = sizeof(sharedB);
  28744. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  28745. printf("wc_curve25519_shared_secret 2 failed\n");
  28746. return WC_TEST_RET_ENC_EC(ret);
  28747. }
  28748. /* compare shared secret keys to test they are the same */
  28749. if (y != x)
  28750. return WC_TEST_RET_ENC_NC;
  28751. if (XMEMCMP(sharedA, sharedB, x))
  28752. return WC_TEST_RET_ENC_NC;
  28753. #endif
  28754. #ifdef HAVE_CURVE25519_KEY_EXPORT
  28755. /* export a public key and import it for another user */
  28756. x = sizeof(exportBuf);
  28757. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  28758. if (ret != 0)
  28759. return WC_TEST_RET_ENC_EC(ret);
  28760. #ifdef HAVE_CURVE25519_KEY_IMPORT
  28761. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  28762. if (ret != 0)
  28763. return WC_TEST_RET_ENC_EC(ret);
  28764. #endif
  28765. #endif
  28766. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  28767. defined(HAVE_CURVE25519_KEY_IMPORT)
  28768. /* test shared key after importing a public key */
  28769. XMEMSET(sharedB, 0, sizeof(sharedB));
  28770. y = sizeof(sharedB);
  28771. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  28772. return WC_TEST_RET_ENC_NC;
  28773. }
  28774. if (XMEMCMP(sharedA, sharedB, y))
  28775. return WC_TEST_RET_ENC_NC;
  28776. /* import RFC test vectors and compare shared key */
  28777. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  28778. &userA);
  28779. if (ret != 0)
  28780. return WC_TEST_RET_ENC_EC(ret);
  28781. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  28782. &userB);
  28783. if (ret != 0)
  28784. return WC_TEST_RET_ENC_EC(ret);
  28785. /* test against known test vector */
  28786. XMEMSET(sharedB, 0, sizeof(sharedB));
  28787. y = sizeof(sharedB);
  28788. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  28789. if (ret != 0)
  28790. return WC_TEST_RET_ENC_EC(ret);
  28791. if (XMEMCMP(ss, sharedB, y))
  28792. return WC_TEST_RET_ENC_NC;
  28793. /* test swapping roles of keys and generating same shared key */
  28794. XMEMSET(sharedB, 0, sizeof(sharedB));
  28795. y = sizeof(sharedB);
  28796. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  28797. if (ret != 0)
  28798. return WC_TEST_RET_ENC_EC(ret);
  28799. if (XMEMCMP(ss, sharedB, y))
  28800. return WC_TEST_RET_ENC_NC;
  28801. /* test with 1 generated key and 1 from known test vector */
  28802. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  28803. &userA);
  28804. if (ret != 0)
  28805. return WC_TEST_RET_ENC_EC(ret);
  28806. wc_curve25519_free(&userB);
  28807. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  28808. ret = wc_curve25519_make_key(&rng, 32, &userB);
  28809. if (ret != 0)
  28810. return WC_TEST_RET_ENC_EC(ret);
  28811. x = sizeof(sharedA);
  28812. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  28813. if (ret != 0)
  28814. return WC_TEST_RET_ENC_EC(ret);
  28815. y = sizeof(sharedB);
  28816. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  28817. if (ret != 0)
  28818. return WC_TEST_RET_ENC_EC(ret);
  28819. /* compare shared secret keys to test they are the same */
  28820. if (y != x)
  28821. return WC_TEST_RET_ENC_NC;
  28822. if (XMEMCMP(sharedA, sharedB, x))
  28823. return WC_TEST_RET_ENC_NC;
  28824. ret = curve25519_overflow_test();
  28825. if (ret != 0)
  28826. return ret;
  28827. ret = curve25519_check_public_test();
  28828. if (ret != 0)
  28829. return ret;
  28830. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  28831. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  28832. defined(HAVE_CURVE25519_KEY_IMPORT)
  28833. ret = curve255519_der_test();
  28834. if (ret != 0)
  28835. return ret;
  28836. #endif
  28837. /* clean up keys when done */
  28838. wc_curve25519_free(&pubKey);
  28839. wc_curve25519_free(&userB);
  28840. wc_curve25519_free(&userA);
  28841. wc_FreeRng(&rng);
  28842. return 0;
  28843. }
  28844. #endif /* HAVE_CURVE25519 */
  28845. #ifdef HAVE_ED25519
  28846. #ifdef WOLFSSL_TEST_CERT
  28847. static wc_test_ret_t ed25519_test_cert(void)
  28848. {
  28849. DecodedCert cert[2];
  28850. DecodedCert* serverCert = NULL;
  28851. DecodedCert* caCert = NULL;
  28852. #ifdef HAVE_ED25519_VERIFY
  28853. ed25519_key key;
  28854. ed25519_key* pubKey = NULL;
  28855. int verify;
  28856. #endif /* HAVE_ED25519_VERIFY */
  28857. wc_test_ret_t ret;
  28858. byte* tmp;
  28859. size_t bytes;
  28860. XFILE file;
  28861. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28862. if (tmp == NULL) {
  28863. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28864. }
  28865. #ifdef USE_CERT_BUFFERS_256
  28866. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  28867. bytes = sizeof_ca_ed25519_cert;
  28868. #elif !defined(NO_FILESYSTEM)
  28869. file = XFOPEN(caEd25519Cert, "rb");
  28870. if (file == NULL) {
  28871. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28872. }
  28873. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  28874. XFCLOSE(file);
  28875. if (bytes == 0)
  28876. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28877. #else
  28878. /* No certificate to use. */
  28879. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28880. #endif
  28881. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  28882. caCert = &cert[0];
  28883. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  28884. if (ret != 0)
  28885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28886. #ifdef USE_CERT_BUFFERS_256
  28887. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  28888. bytes = sizeof_server_ed25519_cert;
  28889. #elif !defined(NO_FILESYSTEM)
  28890. file = XFOPEN(serverEd25519Cert, "rb");
  28891. if (file == NULL) {
  28892. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28893. }
  28894. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  28895. XFCLOSE(file);
  28896. if (bytes == 0)
  28897. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28898. #else
  28899. /* No certificate to use. */
  28900. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28901. #endif
  28902. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  28903. serverCert = &cert[1];
  28904. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  28905. if (ret != 0)
  28906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28907. #ifdef HAVE_ED25519_VERIFY
  28908. ret = wc_ed25519_init(&key);
  28909. if (ret < 0)
  28910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28911. pubKey = &key;
  28912. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  28913. pubKey);
  28914. if (ret < 0)
  28915. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28916. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  28917. serverCert->source + serverCert->certBegin,
  28918. serverCert->sigIndex - serverCert->certBegin,
  28919. &verify, pubKey);
  28920. if (ret < 0 || verify != 1)
  28921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28922. #endif /* HAVE_ED25519_VERIFY */
  28923. done:
  28924. if (tmp != NULL)
  28925. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28926. #ifdef HAVE_ED25519_VERIFY
  28927. wc_ed25519_free(pubKey);
  28928. #endif /* HAVE_ED25519_VERIFY */
  28929. if (caCert != NULL)
  28930. FreeDecodedCert(caCert);
  28931. if (serverCert != NULL)
  28932. FreeDecodedCert(serverCert);
  28933. return ret;
  28934. }
  28935. static wc_test_ret_t ed25519_test_make_cert(void)
  28936. {
  28937. WC_RNG rng;
  28938. Cert cert;
  28939. DecodedCert decode;
  28940. ed25519_key key;
  28941. ed25519_key* privKey = NULL;
  28942. wc_test_ret_t ret = 0;
  28943. byte* tmp = NULL;
  28944. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  28945. #ifndef HAVE_FIPS
  28946. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28947. #else
  28948. ret = wc_InitRng(&rng);
  28949. #endif
  28950. if (ret != 0)
  28951. return WC_TEST_RET_ENC_EC(ret);
  28952. wc_ed25519_init(&key);
  28953. privKey = &key;
  28954. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  28955. cert.daysValid = 365 * 2;
  28956. cert.selfSigned = 1;
  28957. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  28958. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  28959. cert.isCA = 0;
  28960. #ifdef WOLFSSL_CERT_EXT
  28961. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  28962. if (ret < 0)
  28963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28964. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  28965. if (ret < 0)
  28966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28967. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  28968. if (ret < 0)
  28969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28970. #endif
  28971. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28972. if (tmp == NULL) {
  28973. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28974. }
  28975. cert.sigType = CTC_ED25519;
  28976. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  28977. if (ret < 0)
  28978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28979. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  28980. ED25519_TYPE, privKey, &rng);
  28981. if (ret < 0)
  28982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28983. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  28984. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  28985. FreeDecodedCert(&decode);
  28986. if (ret != 0)
  28987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28988. done:
  28989. if (tmp != NULL)
  28990. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28991. wc_ed25519_free(privKey);
  28992. wc_FreeRng(&rng);
  28993. return ret;
  28994. }
  28995. #endif /* WOLFSSL_TEST_CERT */
  28996. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  28997. defined(HAVE_ED25519_KEY_IMPORT)
  28998. static wc_test_ret_t ed25519ctx_test(void)
  28999. {
  29000. wc_test_ret_t ret;
  29001. byte out[ED25519_SIG_SIZE];
  29002. word32 outlen;
  29003. #ifdef HAVE_ED25519_VERIFY
  29004. int verify = 0;
  29005. #endif /* HAVE_ED25519_VERIFY */
  29006. ed25519_key key;
  29007. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  29008. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  29009. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  29010. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  29011. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  29012. };
  29013. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  29014. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  29015. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  29016. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  29017. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  29018. };
  29019. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  29020. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  29021. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  29022. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  29023. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  29024. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  29025. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  29026. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  29027. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  29028. };
  29029. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  29030. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  29031. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  29032. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  29033. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  29034. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  29035. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  29036. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  29037. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  29038. };
  29039. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  29040. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  29041. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  29042. };
  29043. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  29044. 0x66,0x6f,0x6f
  29045. };
  29046. outlen = sizeof(out);
  29047. XMEMSET(out, 0, sizeof(out));
  29048. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  29049. if (ret != 0)
  29050. return 10800;
  29051. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  29052. sizeof(pKeyCtx), &key);
  29053. if (ret == 0)
  29054. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  29055. contextCtx, sizeof(contextCtx));
  29056. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  29057. ret = WC_TEST_RET_ENC_NC;
  29058. #if defined(HAVE_ED25519_VERIFY)
  29059. /* test verify on good msg */
  29060. if (ret == 0)
  29061. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  29062. &verify, &key, contextCtx, sizeof(contextCtx));
  29063. if (ret == 0 && verify != 1)
  29064. ret = WC_TEST_RET_ENC_NC;
  29065. #endif
  29066. if (ret == 0)
  29067. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  29068. NULL, 0);
  29069. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  29070. ret = WC_TEST_RET_ENC_NC;
  29071. #if defined(HAVE_ED25519_VERIFY)
  29072. /* test verify on good msg */
  29073. if (ret == 0)
  29074. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  29075. &verify, &key, NULL, 0);
  29076. if (ret == 0 && verify != 1)
  29077. ret = WC_TEST_RET_ENC_NC;
  29078. #endif
  29079. wc_ed25519_free(&key);
  29080. return ret;
  29081. }
  29082. static wc_test_ret_t ed25519ph_test(void)
  29083. {
  29084. wc_test_ret_t ret = 0;
  29085. byte out[ED25519_SIG_SIZE];
  29086. word32 outlen;
  29087. #ifdef HAVE_ED25519_VERIFY
  29088. int verify = 0;
  29089. #endif /* HAVE_ED25519_VERIFY */
  29090. ed25519_key key;
  29091. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  29092. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  29093. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  29094. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  29095. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  29096. };
  29097. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  29098. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  29099. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  29100. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  29101. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  29102. };
  29103. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  29104. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  29105. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  29106. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  29107. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  29108. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  29109. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  29110. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  29111. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  29112. };
  29113. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  29114. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  29115. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  29116. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  29117. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  29118. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  29119. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  29120. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  29121. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  29122. };
  29123. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  29124. 0x61,0x62,0x63
  29125. };
  29126. /* SHA-512 hash of msgPh */
  29127. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  29128. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  29129. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  29130. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  29131. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  29132. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  29133. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  29134. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  29135. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  29136. };
  29137. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  29138. 0x66,0x6f,0x6f
  29139. };
  29140. outlen = sizeof(out);
  29141. XMEMSET(out, 0, sizeof(out));
  29142. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  29143. if (ret != 0)
  29144. return WC_TEST_RET_ENC_EC(ret);
  29145. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  29146. sizeof(pKeyPh), &key);
  29147. if (ret == 0)
  29148. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  29149. NULL, 0);
  29150. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  29151. ret = WC_TEST_RET_ENC_NC;
  29152. #if defined(HAVE_ED25519_VERIFY)
  29153. /* test verify on good msg */
  29154. if (ret == 0)
  29155. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  29156. &verify, &key, NULL, 0);
  29157. if (ret == 0 && verify != 1)
  29158. ret = WC_TEST_RET_ENC_NC;
  29159. #endif
  29160. if (ret == 0)
  29161. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  29162. contextPh2, sizeof(contextPh2));
  29163. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  29164. ret = WC_TEST_RET_ENC_NC;
  29165. #if defined(HAVE_ED25519_VERIFY)
  29166. /* test verify on good msg */
  29167. if (ret == 0)
  29168. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  29169. &key, contextPh2, sizeof(contextPh2));
  29170. if (ret == 0 && verify != 1)
  29171. ret = WC_TEST_RET_ENC_NC;
  29172. #endif
  29173. if (ret == 0)
  29174. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  29175. NULL, 0);
  29176. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  29177. ret = WC_TEST_RET_ENC_NC;
  29178. #if defined(HAVE_ED25519_VERIFY)
  29179. if (ret == 0)
  29180. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  29181. &verify, &key, NULL, 0);
  29182. if (ret == 0 && verify != 1)
  29183. ret = WC_TEST_RET_ENC_NC;
  29184. #endif
  29185. if (ret == 0)
  29186. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  29187. contextPh2, sizeof(contextPh2));
  29188. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  29189. ret = WC_TEST_RET_ENC_NC;
  29190. #if defined(HAVE_ED25519_VERIFY)
  29191. if (ret == 0)
  29192. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  29193. &key, contextPh2, sizeof(contextPh2));
  29194. if (ret == 0 && verify != 1)
  29195. ret = WC_TEST_RET_ENC_NC;
  29196. #endif
  29197. wc_ed25519_free(&key);
  29198. return ret;
  29199. }
  29200. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  29201. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  29202. {
  29203. wc_test_ret_t ret;
  29204. WC_RNG rng;
  29205. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  29206. defined(HAVE_ED25519_KEY_IMPORT)
  29207. byte out[ED25519_SIG_SIZE];
  29208. byte exportPKey[ED25519_KEY_SIZE];
  29209. byte exportSKey[ED25519_KEY_SIZE];
  29210. word32 exportPSz;
  29211. word32 exportSSz;
  29212. int i;
  29213. word32 outlen;
  29214. #ifdef HAVE_ED25519_VERIFY
  29215. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  29216. int j;
  29217. #endif
  29218. int verify;
  29219. #endif /* HAVE_ED25519_VERIFY */
  29220. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  29221. word32 keySz, sigSz;
  29222. ed25519_key key;
  29223. ed25519_key key2;
  29224. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  29225. defined(HAVE_ED25519_KEY_IMPORT)
  29226. /* test vectors from
  29227. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  29228. */
  29229. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  29230. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29231. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29232. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29233. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  29234. };
  29235. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  29236. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  29237. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  29238. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  29239. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  29240. };
  29241. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  29242. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  29243. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  29244. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  29245. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  29246. };
  29247. /* uncompressed test */
  29248. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  29249. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29250. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29251. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29252. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  29253. };
  29254. /* compressed prefix test */
  29255. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  29256. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29257. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29258. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29259. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  29260. };
  29261. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  29262. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  29263. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  29264. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  29265. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  29266. };
  29267. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  29268. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  29269. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  29270. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  29271. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  29272. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  29273. };
  29274. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  29275. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  29276. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  29277. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  29278. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  29279. };
  29280. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  29281. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  29282. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  29283. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  29284. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  29285. };
  29286. /* uncompressed test */
  29287. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  29288. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  29289. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  29290. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  29291. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  29292. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  29293. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  29294. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  29295. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  29296. 0xd7
  29297. };
  29298. /* compressed prefix */
  29299. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  29300. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  29301. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  29302. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  29303. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  29304. };
  29305. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  29306. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  29307. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  29308. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  29309. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  29310. };
  29311. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  29312. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  29313. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  29314. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  29315. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  29316. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  29317. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  29318. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  29319. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  29320. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  29321. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  29322. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  29323. };
  29324. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  29325. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  29326. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  29327. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  29328. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  29329. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  29330. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  29331. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  29332. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  29333. };
  29334. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  29335. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  29336. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  29337. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  29338. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  29339. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  29340. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  29341. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  29342. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  29343. };
  29344. /* uncompressed test */
  29345. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  29346. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  29347. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  29348. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  29349. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  29350. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  29351. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  29352. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  29353. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  29354. };
  29355. /* compressed prefix */
  29356. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  29357. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  29358. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  29359. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  29360. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  29361. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  29362. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  29363. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  29364. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  29365. };
  29366. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  29367. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  29368. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  29369. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  29370. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  29371. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  29372. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  29373. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  29374. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  29375. };
  29376. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  29377. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  29378. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  29379. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  29380. /* test of a 1024 byte long message */
  29381. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  29382. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  29383. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  29384. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  29385. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  29386. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  29387. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  29388. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  29389. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  29390. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  29391. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  29392. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  29393. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  29394. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  29395. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  29396. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  29397. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  29398. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  29399. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  29400. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  29401. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  29402. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  29403. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  29404. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  29405. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  29406. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  29407. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  29408. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  29409. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  29410. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  29411. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  29412. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  29413. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  29414. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  29415. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  29416. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  29417. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  29418. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  29419. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  29420. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  29421. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  29422. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  29423. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  29424. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  29425. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  29426. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  29427. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  29428. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  29429. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  29430. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  29431. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  29432. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  29433. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  29434. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  29435. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  29436. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  29437. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  29438. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  29439. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  29440. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  29441. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  29442. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  29443. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  29444. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  29445. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  29446. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  29447. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  29448. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  29449. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  29450. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  29451. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  29452. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  29453. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  29454. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  29455. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  29456. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  29457. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  29458. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  29459. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  29460. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  29461. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  29462. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  29463. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  29464. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  29465. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  29466. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  29467. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  29468. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  29469. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  29470. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  29471. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  29472. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  29473. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  29474. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  29475. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  29476. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  29477. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  29478. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  29479. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  29480. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  29481. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  29482. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  29483. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  29484. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  29485. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  29486. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  29487. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  29488. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  29489. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  29490. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  29491. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  29492. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  29493. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  29494. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  29495. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  29496. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  29497. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  29498. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  29499. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  29500. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  29501. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  29502. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  29503. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  29504. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  29505. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  29506. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  29507. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  29508. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  29509. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  29510. };
  29511. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  29512. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  29513. sizeof(msg2),
  29514. sizeof(msg3),
  29515. 0 /*sizeof(msg1)*/,
  29516. 0 /*sizeof(msg1)*/,
  29517. sizeof(msg4)
  29518. };
  29519. #ifndef NO_ASN
  29520. static const byte privateEd25519[] = {
  29521. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  29522. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  29523. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29524. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29525. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29526. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  29527. };
  29528. static const byte badPrivateEd25519[] = {
  29529. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  29530. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  29531. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29532. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29533. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29534. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  29535. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  29536. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  29537. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  29538. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  29539. 0xf7,0x07,0x51,0x1a,
  29540. 0x00 /* add additional bytes to make the pubkey bigger */
  29541. };
  29542. static const byte publicEd25519[] = {
  29543. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  29544. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  29545. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  29546. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  29547. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  29548. 0xf7,0x07,0x51,0x1a
  29549. };
  29550. /* size has been altered to catch if sanity check is done */
  29551. static const byte badPublicEd25519[] = {
  29552. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  29553. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  29554. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  29555. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  29556. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  29557. 0xf7,0x07,0x51,0x1a,
  29558. 0x00 /* add an additional byte to make the pubkey appear bigger */
  29559. };
  29560. static const byte privPubEd25519[] = {
  29561. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  29562. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  29563. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  29564. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  29565. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  29566. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  29567. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  29568. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  29569. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  29570. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  29571. 0x51,0x1a
  29572. };
  29573. word32 idx;
  29574. #endif /* NO_ASN */
  29575. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  29576. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  29577. ed25519_key key3;
  29578. #endif
  29579. WOLFSSL_ENTER("ed25519_test");
  29580. /* create ed25519 keys */
  29581. #ifndef HAVE_FIPS
  29582. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29583. #else
  29584. ret = wc_InitRng(&rng);
  29585. #endif
  29586. if (ret != 0)
  29587. return WC_TEST_RET_ENC_EC(ret);
  29588. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  29589. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  29590. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  29591. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  29592. #endif
  29593. #ifdef HAVE_ED25519_MAKE_KEY
  29594. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  29595. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  29596. #endif
  29597. /* helper functions for signature and key size */
  29598. keySz = wc_ed25519_size(&key);
  29599. sigSz = wc_ed25519_sig_size(&key);
  29600. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  29601. defined(HAVE_ED25519_KEY_IMPORT)
  29602. for (i = 0; i < 6; i++) {
  29603. outlen = sizeof(out);
  29604. XMEMSET(out, 0, sizeof(out));
  29605. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  29606. pKeySz[i], &key) != 0)
  29607. return WC_TEST_RET_ENC_I(i);
  29608. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  29609. return WC_TEST_RET_ENC_I(i);
  29610. if (XMEMCMP(out, sigs[i], 64))
  29611. return WC_TEST_RET_ENC_I(i);
  29612. #if defined(HAVE_ED25519_VERIFY)
  29613. /* test verify on good msg */
  29614. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  29615. &key) != 0 || verify != 1)
  29616. return WC_TEST_RET_ENC_I(i);
  29617. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  29618. /* test verify on good msg using streaming interface directly */
  29619. if (wc_ed25519_verify_msg_init(out, outlen,
  29620. &key, (byte)Ed25519, NULL, 0) != 0)
  29621. return WC_TEST_RET_ENC_I(i);
  29622. for (j = 0; j < msgSz[i]; j += i) {
  29623. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  29624. return WC_TEST_RET_ENC_I(i);
  29625. }
  29626. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  29627. &key) != 0 || verify != 1)
  29628. return WC_TEST_RET_ENC_I(i);
  29629. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  29630. /* test verify on bad msg */
  29631. out[outlen-1] = out[outlen-1] + 1;
  29632. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  29633. &key) == 0 || verify == 1)
  29634. return WC_TEST_RET_ENC_I(i);
  29635. #endif /* HAVE_ED25519_VERIFY */
  29636. /* test api for import/exporting keys */
  29637. exportPSz = sizeof(exportPKey);
  29638. exportSSz = sizeof(exportSKey);
  29639. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  29640. return WC_TEST_RET_ENC_I(i);
  29641. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  29642. return WC_TEST_RET_ENC_I(i);
  29643. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  29644. return WC_TEST_RET_ENC_I(i);
  29645. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  29646. exportPKey, exportPSz, &key2) != 0)
  29647. return WC_TEST_RET_ENC_I(i);
  29648. /* clear "out" buffer and test sign with imported keys */
  29649. outlen = sizeof(out);
  29650. XMEMSET(out, 0, sizeof(out));
  29651. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  29652. return WC_TEST_RET_ENC_I(i);
  29653. #if defined(HAVE_ED25519_VERIFY)
  29654. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  29655. &key2) != 0 || verify != 1)
  29656. return WC_TEST_RET_ENC_I(i);
  29657. if (XMEMCMP(out, sigs[i], 64))
  29658. return WC_TEST_RET_ENC_I(i);
  29659. #endif /* HAVE_ED25519_VERIFY */
  29660. }
  29661. ret = ed25519ctx_test();
  29662. if (ret != 0)
  29663. return ret;
  29664. ret = ed25519ph_test();
  29665. if (ret != 0)
  29666. return ret;
  29667. #ifndef NO_ASN
  29668. /* Try ASN.1 encoded private-only key and public key. */
  29669. idx = 0;
  29670. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  29671. sizeof(privateEd25519));
  29672. if (ret != 0)
  29673. return WC_TEST_RET_ENC_EC(ret);
  29674. idx = 0;
  29675. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  29676. sizeof(badPrivateEd25519)) == 0)
  29677. return WC_TEST_RET_ENC_NC;
  29678. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29679. if (ret != BAD_FUNC_ARG)
  29680. return WC_TEST_RET_ENC_EC(ret);
  29681. /* try with a buffer size that is too large */
  29682. idx = 0;
  29683. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  29684. sizeof(badPublicEd25519)) == 0)
  29685. return WC_TEST_RET_ENC_NC;
  29686. idx = 0;
  29687. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  29688. sizeof(publicEd25519));
  29689. if (ret != 0)
  29690. return WC_TEST_RET_ENC_EC(ret);
  29691. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29692. if (ret != 0)
  29693. return WC_TEST_RET_ENC_EC(ret);
  29694. if (XMEMCMP(out, sigs[0], 64))
  29695. return WC_TEST_RET_ENC_NC;
  29696. #if defined(HAVE_ED25519_VERIFY)
  29697. /* test verify on good msg */
  29698. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  29699. if (ret != 0 || verify != 1)
  29700. return WC_TEST_RET_ENC_EC(ret);
  29701. #endif /* HAVE_ED25519_VERIFY */
  29702. wc_ed25519_free(&key3);
  29703. wc_ed25519_init(&key3);
  29704. idx = 0;
  29705. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  29706. sizeof(privPubEd25519));
  29707. if (ret != 0)
  29708. return WC_TEST_RET_ENC_EC(ret);
  29709. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  29710. if (ret != 0)
  29711. return WC_TEST_RET_ENC_EC(ret);
  29712. if (XMEMCMP(out, sigs[0], 64))
  29713. return WC_TEST_RET_ENC_NC;
  29714. wc_ed25519_free(&key3);
  29715. #endif /* NO_ASN */
  29716. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  29717. /* clean up keys when done */
  29718. wc_ed25519_free(&key);
  29719. wc_ed25519_free(&key2);
  29720. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  29721. wc_FreeRng(&rng);
  29722. #endif
  29723. /* hush warnings of unused keySz and sigSz */
  29724. (void)keySz;
  29725. (void)sigSz;
  29726. #ifdef WOLFSSL_TEST_CERT
  29727. ret = ed25519_test_cert();
  29728. if (ret < 0)
  29729. return ret;
  29730. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  29731. ret = ed25519_test_make_cert();
  29732. if (ret < 0)
  29733. return ret;
  29734. #endif /* WOLFSSL_CERT_GEN */
  29735. #endif /* WOLFSSL_TEST_CERT */
  29736. return 0;
  29737. }
  29738. #endif /* HAVE_ED25519 */
  29739. #ifdef HAVE_CURVE448
  29740. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29741. defined(HAVE_CURVE448_KEY_IMPORT)
  29742. /* Test the wc_curve448_check_public API.
  29743. *
  29744. * returns 0 on success and -ve on failure.
  29745. */
  29746. static wc_test_ret_t curve448_check_public_test(void)
  29747. {
  29748. /* Little-endian values that will fail */
  29749. byte fail_le[][CURVE448_KEY_SIZE] = {
  29750. {
  29751. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29752. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29753. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29754. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29755. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29756. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29757. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29758. },
  29759. {
  29760. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29761. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29762. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29765. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29766. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29767. },
  29768. };
  29769. /* Big-endian values that will fail */
  29770. byte fail_be[][CURVE448_KEY_SIZE] = {
  29771. {
  29772. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29773. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29774. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29775. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29776. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29777. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29778. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  29779. },
  29780. {
  29781. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29782. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29785. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29786. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29787. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  29788. },
  29789. };
  29790. /* Good or valid public value */
  29791. byte good[CURVE448_KEY_SIZE] = {
  29792. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29793. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29794. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  29799. };
  29800. int i;
  29801. wc_test_ret_t ret;
  29802. /* Parameter checks */
  29803. /* NULL pointer */
  29804. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  29805. if (ret != BAD_FUNC_ARG)
  29806. return WC_TEST_RET_ENC_EC(ret);
  29807. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  29808. if (ret != BAD_FUNC_ARG)
  29809. return WC_TEST_RET_ENC_EC(ret);
  29810. /* Length of 0 treated differently to other invalid lengths for TLS */
  29811. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  29812. if (ret != BUFFER_E)
  29813. return WC_TEST_RET_ENC_EC(ret);
  29814. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  29815. if (ret != BUFFER_E)
  29816. return WC_TEST_RET_ENC_EC(ret);
  29817. /* Length not CURVE448_KEY_SIZE */
  29818. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  29819. if (i == CURVE448_KEY_SIZE)
  29820. continue;
  29821. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  29822. ECC_BAD_ARG_E) {
  29823. return WC_TEST_RET_ENC_I(i);
  29824. }
  29825. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  29826. ECC_BAD_ARG_E) {
  29827. return WC_TEST_RET_ENC_I(i);
  29828. }
  29829. }
  29830. /* Little-endian fail cases */
  29831. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  29832. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  29833. EC448_LITTLE_ENDIAN) == 0) {
  29834. return WC_TEST_RET_ENC_I(i);
  29835. }
  29836. }
  29837. /* Big-endian fail cases */
  29838. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  29839. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  29840. EC448_BIG_ENDIAN) == 0) {
  29841. return WC_TEST_RET_ENC_I(i);
  29842. }
  29843. }
  29844. /* Check a valid public value works! */
  29845. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  29846. EC448_LITTLE_ENDIAN);
  29847. if (ret != 0)
  29848. return WC_TEST_RET_ENC_EC(ret);
  29849. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  29850. if (ret != 0)
  29851. return WC_TEST_RET_ENC_EC(ret);
  29852. return 0;
  29853. }
  29854. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  29855. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  29856. {
  29857. WC_RNG rng;
  29858. wc_test_ret_t ret;
  29859. #ifdef HAVE_CURVE448_SHARED_SECRET
  29860. byte sharedA[CURVE448_KEY_SIZE];
  29861. byte sharedB[CURVE448_KEY_SIZE];
  29862. word32 y;
  29863. #endif
  29864. #ifdef HAVE_CURVE448_KEY_EXPORT
  29865. byte exportBuf[CURVE448_KEY_SIZE];
  29866. #endif
  29867. word32 x = 0;
  29868. curve448_key userA, userB, pubKey;
  29869. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29870. defined(HAVE_CURVE448_KEY_IMPORT)
  29871. /* test vectors from
  29872. https://www.rfc-editor.org/rfc/rfc7748.html
  29873. */
  29874. /* secret key for party a */
  29875. byte sa[] = {
  29876. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  29877. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  29878. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  29879. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  29880. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  29881. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  29882. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  29883. };
  29884. /* public key for party a */
  29885. byte pa[] = {
  29886. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  29887. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  29888. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  29889. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  29890. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  29891. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  29892. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  29893. };
  29894. /* secret key for party b */
  29895. byte sb[] = {
  29896. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  29897. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  29898. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  29899. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  29900. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  29901. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  29902. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  29903. };
  29904. /* public key for party b */
  29905. byte pb[] = {
  29906. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  29907. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  29908. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  29909. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  29910. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  29911. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  29912. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  29913. };
  29914. /* expected shared key */
  29915. byte ss[] = {
  29916. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  29917. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  29918. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  29919. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  29920. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  29921. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  29922. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  29923. };
  29924. #endif /* HAVE_CURVE448_SHARED_SECRET */
  29925. (void)x;
  29926. WOLFSSL_ENTER("curve448_test");
  29927. #ifndef HAVE_FIPS
  29928. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29929. #else
  29930. ret = wc_InitRng(&rng);
  29931. #endif
  29932. if (ret != 0)
  29933. return WC_TEST_RET_ENC_EC(ret);
  29934. wc_curve448_init(&userA);
  29935. wc_curve448_init(&userB);
  29936. wc_curve448_init(&pubKey);
  29937. /* make curve448 keys */
  29938. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  29939. if (ret != 0)
  29940. return WC_TEST_RET_ENC_EC(ret);
  29941. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  29942. if (ret != 0)
  29943. return WC_TEST_RET_ENC_EC(ret);
  29944. #ifdef HAVE_CURVE448_SHARED_SECRET
  29945. /* find shared secret key */
  29946. x = sizeof(sharedA);
  29947. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  29948. if (ret != 0)
  29949. return WC_TEST_RET_ENC_EC(ret);
  29950. y = sizeof(sharedB);
  29951. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  29952. if (ret != 0)
  29953. return WC_TEST_RET_ENC_EC(ret);
  29954. /* compare shared secret keys to test they are the same */
  29955. if (y != x)
  29956. return WC_TEST_RET_ENC_NC;
  29957. if (XMEMCMP(sharedA, sharedB, x))
  29958. return WC_TEST_RET_ENC_NC;
  29959. #endif
  29960. #ifdef HAVE_CURVE448_KEY_EXPORT
  29961. /* export a public key and import it for another user */
  29962. x = sizeof(exportBuf);
  29963. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  29964. if (ret != 0)
  29965. return WC_TEST_RET_ENC_EC(ret);
  29966. #ifdef HAVE_CURVE448_KEY_IMPORT
  29967. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  29968. if (ret != 0)
  29969. return WC_TEST_RET_ENC_EC(ret);
  29970. #endif
  29971. #endif
  29972. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  29973. defined(HAVE_CURVE448_KEY_IMPORT)
  29974. /* test shared key after importing a public key */
  29975. XMEMSET(sharedB, 0, sizeof(sharedB));
  29976. y = sizeof(sharedB);
  29977. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  29978. if (ret != 0)
  29979. return WC_TEST_RET_ENC_EC(ret);
  29980. if (XMEMCMP(sharedA, sharedB, y))
  29981. return WC_TEST_RET_ENC_NC;
  29982. /* import RFC test vectors and compare shared key */
  29983. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  29984. if (ret != 0)
  29985. return WC_TEST_RET_ENC_EC(ret);
  29986. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  29987. if (ret != 0)
  29988. return WC_TEST_RET_ENC_EC(ret);
  29989. /* test against known test vector */
  29990. XMEMSET(sharedB, 0, sizeof(sharedB));
  29991. y = sizeof(sharedB);
  29992. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  29993. if (ret != 0)
  29994. return WC_TEST_RET_ENC_EC(ret);
  29995. if (XMEMCMP(ss, sharedB, y))
  29996. return WC_TEST_RET_ENC_NC;
  29997. /* test swapping roles of keys and generating same shared key */
  29998. XMEMSET(sharedB, 0, sizeof(sharedB));
  29999. y = sizeof(sharedB);
  30000. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  30001. if (ret != 0)
  30002. return WC_TEST_RET_ENC_EC(ret);
  30003. if (XMEMCMP(ss, sharedB, y))
  30004. return WC_TEST_RET_ENC_NC;
  30005. /* test with 1 generated key and 1 from known test vector */
  30006. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  30007. if (ret != 0)
  30008. return WC_TEST_RET_ENC_EC(ret);
  30009. ret = wc_curve448_make_key(&rng, 56, &userB);
  30010. if (ret != 0)
  30011. return WC_TEST_RET_ENC_EC(ret);
  30012. x = sizeof(sharedA);
  30013. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  30014. if (ret != 0)
  30015. return WC_TEST_RET_ENC_EC(ret);
  30016. y = sizeof(sharedB);
  30017. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  30018. if (ret != 0)
  30019. return WC_TEST_RET_ENC_EC(ret);
  30020. /* compare shared secret keys to test they are the same */
  30021. if (y != x)
  30022. return WC_TEST_RET_ENC_NC;
  30023. if (XMEMCMP(sharedA, sharedB, x))
  30024. return WC_TEST_RET_ENC_NC;
  30025. ret = curve448_check_public_test();
  30026. if (ret != 0)
  30027. return ret;
  30028. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  30029. /* clean up keys when done */
  30030. wc_curve448_free(&pubKey);
  30031. wc_curve448_free(&userB);
  30032. wc_curve448_free(&userA);
  30033. wc_FreeRng(&rng);
  30034. return 0;
  30035. }
  30036. #endif /* HAVE_CURVE448 */
  30037. #ifdef HAVE_ED448
  30038. #ifdef WOLFSSL_TEST_CERT
  30039. static wc_test_ret_t ed448_test_cert(void)
  30040. {
  30041. DecodedCert cert[2];
  30042. DecodedCert* serverCert = NULL;
  30043. DecodedCert* caCert = NULL;
  30044. #ifdef HAVE_ED448_VERIFY
  30045. ed448_key key;
  30046. ed448_key* pubKey = NULL;
  30047. int verify;
  30048. #endif /* HAVE_ED448_VERIFY */
  30049. wc_test_ret_t ret;
  30050. byte* tmp;
  30051. size_t bytes;
  30052. XFILE file;
  30053. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30054. if (tmp == NULL) {
  30055. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30056. }
  30057. #ifdef USE_CERT_BUFFERS_256
  30058. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  30059. bytes = sizeof_ca_ed448_cert;
  30060. #elif !defined(NO_FILESYSTEM)
  30061. file = XFOPEN(caEd448Cert, "rb");
  30062. if (file == NULL) {
  30063. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30064. }
  30065. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30066. XFCLOSE(file);
  30067. if (bytes == 0)
  30068. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30069. #else
  30070. /* No certificate to use. */
  30071. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30072. #endif
  30073. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  30074. caCert = &cert[0];
  30075. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  30076. if (ret != 0)
  30077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30078. #ifdef USE_CERT_BUFFERS_256
  30079. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  30080. bytes = sizeof_server_ed448_cert;
  30081. #elif !defined(NO_FILESYSTEM)
  30082. file = XFOPEN(serverEd448Cert, "rb");
  30083. if (file == NULL) {
  30084. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30085. }
  30086. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30087. XFCLOSE(file);
  30088. if (bytes == 0)
  30089. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30090. #else
  30091. /* No certificate to use. */
  30092. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30093. #endif
  30094. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  30095. serverCert = &cert[1];
  30096. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  30097. if (ret != 0)
  30098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30099. #ifdef HAVE_ED448_VERIFY
  30100. ret = wc_ed448_init(&key);
  30101. if (ret < 0)
  30102. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30103. pubKey = &key;
  30104. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  30105. if (ret < 0)
  30106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30107. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  30108. serverCert->source + serverCert->certBegin,
  30109. serverCert->sigIndex - serverCert->certBegin,
  30110. &verify, pubKey, NULL, 0);
  30111. if (ret < 0 || verify != 1) {
  30112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30113. }
  30114. #endif /* HAVE_ED448_VERIFY */
  30115. done:
  30116. if (tmp != NULL)
  30117. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30118. #ifdef HAVE_ED448_VERIFY
  30119. wc_ed448_free(pubKey);
  30120. #endif /* HAVE_ED448_VERIFY */
  30121. if (caCert != NULL)
  30122. FreeDecodedCert(caCert);
  30123. if (serverCert != NULL)
  30124. FreeDecodedCert(serverCert);
  30125. return ret;
  30126. }
  30127. static wc_test_ret_t ed448_test_make_cert(void)
  30128. {
  30129. WC_RNG rng;
  30130. Cert cert;
  30131. DecodedCert decode;
  30132. ed448_key key;
  30133. ed448_key* privKey = NULL;
  30134. wc_test_ret_t ret = 0;
  30135. byte* tmp = NULL;
  30136. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  30137. #ifndef HAVE_FIPS
  30138. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30139. #else
  30140. ret = wc_InitRng(&rng);
  30141. #endif
  30142. if (ret != 0)
  30143. return WC_TEST_RET_ENC_EC(ret);
  30144. wc_ed448_init(&key);
  30145. privKey = &key;
  30146. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  30147. cert.daysValid = 365 * 2;
  30148. cert.selfSigned = 1;
  30149. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  30150. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  30151. cert.isCA = 0;
  30152. #ifdef WOLFSSL_CERT_EXT
  30153. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  30154. if (ret < 0)
  30155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30156. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  30157. if (ret < 0)
  30158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30159. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  30160. if (ret < 0)
  30161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30162. #endif
  30163. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30164. if (tmp == NULL) {
  30165. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30166. }
  30167. cert.sigType = CTC_ED448;
  30168. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  30169. if (ret < 0)
  30170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30171. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  30172. privKey, &rng);
  30173. if (ret < 0)
  30174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30175. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  30176. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  30177. FreeDecodedCert(&decode);
  30178. if (ret != 0)
  30179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30180. done:
  30181. if (tmp != NULL)
  30182. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30183. wc_ed448_free(privKey);
  30184. wc_FreeRng(&rng);
  30185. return ret;
  30186. }
  30187. #endif /* WOLFSSL_TEST_CERT */
  30188. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  30189. defined(HAVE_ED448_KEY_IMPORT)
  30190. static wc_test_ret_t ed448_ctx_test(void)
  30191. {
  30192. wc_test_ret_t ret;
  30193. byte out[ED448_SIG_SIZE];
  30194. word32 outlen;
  30195. #ifdef HAVE_ED448_VERIFY
  30196. int verify;
  30197. #endif /* HAVE_ED448_VERIFY */
  30198. ed448_key key;
  30199. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  30200. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  30201. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  30202. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  30203. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  30204. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  30205. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  30206. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  30207. 0x4e
  30208. };
  30209. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  30210. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  30211. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  30212. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  30213. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  30214. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  30215. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  30216. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  30217. 0x80
  30218. };
  30219. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  30220. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  30221. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  30222. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  30223. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  30224. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  30225. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  30226. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  30227. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  30228. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  30229. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  30230. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  30231. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  30232. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  30233. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  30234. 0x3c, 0x00
  30235. };
  30236. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  30237. 0x03
  30238. };
  30239. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  30240. 0x66,0x6f,0x6f
  30241. };
  30242. outlen = sizeof(out);
  30243. XMEMSET(out, 0, sizeof(out));
  30244. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  30245. if (ret != 0)
  30246. return WC_TEST_RET_ENC_EC(ret);
  30247. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  30248. sizeof(pKeyCtx), &key);
  30249. if (ret != 0)
  30250. return WC_TEST_RET_ENC_EC(ret);
  30251. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  30252. contextCtx, sizeof(contextCtx));
  30253. if (ret != 0)
  30254. return WC_TEST_RET_ENC_EC(ret);
  30255. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  30256. return WC_TEST_RET_ENC_NC;
  30257. #if defined(HAVE_ED448_VERIFY)
  30258. /* test verify on good msg */
  30259. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  30260. contextCtx, sizeof(contextCtx));
  30261. if (ret != 0 || verify != 1)
  30262. return WC_TEST_RET_ENC_EC(ret);
  30263. #endif
  30264. wc_ed448_free(&key);
  30265. return 0;
  30266. }
  30267. static wc_test_ret_t ed448ph_test(void)
  30268. {
  30269. wc_test_ret_t ret;
  30270. byte out[ED448_SIG_SIZE];
  30271. word32 outlen;
  30272. #ifdef HAVE_ED448_VERIFY
  30273. int verify;
  30274. #endif /* HAVE_ED448_VERIFY */
  30275. ed448_key key;
  30276. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  30277. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  30278. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  30279. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  30280. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  30281. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  30282. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  30283. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  30284. 0x49
  30285. };
  30286. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  30287. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  30288. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  30289. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  30290. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  30291. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  30292. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  30293. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  30294. 0x80
  30295. };
  30296. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  30297. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  30298. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  30299. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  30300. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  30301. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  30302. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  30303. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  30304. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  30305. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  30306. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  30307. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  30308. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  30309. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  30310. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  30311. 0x0f, 0x00
  30312. };
  30313. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  30314. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  30315. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  30316. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  30317. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  30318. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  30319. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  30320. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  30321. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  30322. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  30323. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  30324. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  30325. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  30326. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  30327. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  30328. 0x21, 0x00
  30329. };
  30330. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  30331. 0x61,0x62,0x63
  30332. };
  30333. /* SHA-512 hash of msgPh */
  30334. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  30335. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  30336. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  30337. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  30338. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  30339. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  30340. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  30341. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  30342. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  30343. };
  30344. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  30345. 0x66,0x6f,0x6f
  30346. };
  30347. outlen = sizeof(out);
  30348. XMEMSET(out, 0, sizeof(out));
  30349. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  30350. if (ret != 0)
  30351. return WC_TEST_RET_ENC_EC(ret);
  30352. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  30353. sizeof(pKeyPh), &key);
  30354. if (ret != 0)
  30355. return WC_TEST_RET_ENC_EC(ret);
  30356. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  30357. 0);
  30358. if (ret != 0)
  30359. return WC_TEST_RET_ENC_EC(ret);
  30360. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  30361. return WC_TEST_RET_ENC_NC;
  30362. #if defined(HAVE_ED448_VERIFY)
  30363. /* test verify on good msg */
  30364. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  30365. NULL, 0);
  30366. if (ret != 0 || verify != 1) {
  30367. return WC_TEST_RET_ENC_EC(ret);
  30368. }
  30369. #endif
  30370. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  30371. contextPh2, sizeof(contextPh2));
  30372. if (ret != 0)
  30373. return WC_TEST_RET_ENC_EC(ret);
  30374. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  30375. return WC_TEST_RET_ENC_NC;
  30376. #if defined(HAVE_ED448_VERIFY)
  30377. /* test verify on good msg */
  30378. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  30379. contextPh2, sizeof(contextPh2));
  30380. if (ret != 0 || verify != 1) {
  30381. return WC_TEST_RET_ENC_EC(ret);
  30382. }
  30383. #endif
  30384. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  30385. 0);
  30386. if (ret != 0)
  30387. return WC_TEST_RET_ENC_EC(ret);
  30388. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  30389. return WC_TEST_RET_ENC_NC;
  30390. #if defined(HAVE_ED448_VERIFY)
  30391. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  30392. &key, NULL, 0);
  30393. if (ret != 0 || verify != 1) {
  30394. return WC_TEST_RET_ENC_EC(ret);
  30395. }
  30396. #endif
  30397. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  30398. contextPh2, sizeof(contextPh2));
  30399. if (ret != 0)
  30400. return WC_TEST_RET_ENC_EC(ret);
  30401. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  30402. return WC_TEST_RET_ENC_NC;
  30403. #if defined(HAVE_ED448_VERIFY)
  30404. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  30405. &key, contextPh2, sizeof(contextPh2));
  30406. if (ret != 0 || verify != 1) {
  30407. return WC_TEST_RET_ENC_EC(ret);
  30408. }
  30409. #endif
  30410. wc_ed448_free(&key);
  30411. return 0;
  30412. }
  30413. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  30414. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  30415. {
  30416. wc_test_ret_t ret;
  30417. WC_RNG rng;
  30418. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  30419. defined(HAVE_ED448_KEY_IMPORT)
  30420. byte out[ED448_SIG_SIZE];
  30421. int i;
  30422. word32 outlen;
  30423. #ifdef HAVE_ED448_VERIFY
  30424. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  30425. int j;
  30426. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  30427. int verify;
  30428. #endif /* HAVE_ED448_VERIFY */
  30429. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  30430. word32 keySz, sigSz;
  30431. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30432. ed448_key *key = NULL;
  30433. ed448_key *key2 = NULL;
  30434. #else
  30435. ed448_key key[1];
  30436. ed448_key key2[1];
  30437. #endif
  30438. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  30439. defined(HAVE_ED448_KEY_IMPORT)
  30440. /* test vectors from
  30441. https://tools.ietf.org/html/rfc8032
  30442. */
  30443. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  30444. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  30445. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  30446. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  30447. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  30448. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  30449. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  30450. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  30451. 0x5b
  30452. };
  30453. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  30454. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  30455. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  30456. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  30457. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  30458. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  30459. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  30460. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  30461. 0x4e
  30462. };
  30463. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  30464. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  30465. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  30466. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  30467. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  30468. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  30469. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  30470. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  30471. 0x1b
  30472. };
  30473. /* uncompressed test */
  30474. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  30475. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  30476. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  30477. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  30478. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  30479. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  30480. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  30481. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  30482. 0x5b
  30483. };
  30484. /* compressed prefix test */
  30485. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  30486. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  30487. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  30488. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  30489. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  30490. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  30491. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  30492. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  30493. 0x5b
  30494. };
  30495. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  30496. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  30497. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  30498. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  30499. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  30500. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  30501. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  30502. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  30503. 0xe8
  30504. };
  30505. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  30506. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  30507. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  30508. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  30509. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  30510. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  30511. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  30512. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  30513. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  30514. 0x80
  30515. };
  30516. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  30517. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  30518. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  30519. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  30520. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  30521. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  30522. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  30523. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  30524. 0x80
  30525. };
  30526. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  30527. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  30528. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  30529. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  30530. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  30531. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  30532. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  30533. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  30534. 0x80
  30535. };
  30536. /* uncompressed test */
  30537. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  30538. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  30539. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  30540. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  30541. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  30542. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  30543. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  30544. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  30545. 0x80
  30546. };
  30547. /* compressed prefix */
  30548. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  30549. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  30550. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  30551. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  30552. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  30553. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  30554. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  30555. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  30556. 0x80
  30557. };
  30558. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  30559. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  30560. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  30561. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  30562. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  30563. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  30564. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  30565. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  30566. 0x00
  30567. };
  30568. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  30569. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  30570. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  30571. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  30572. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  30573. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  30574. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  30575. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  30576. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  30577. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  30578. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  30579. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  30580. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  30581. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  30582. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  30583. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  30584. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  30585. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  30586. 0x26, 0x00
  30587. };
  30588. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  30589. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  30590. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  30591. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  30592. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  30593. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  30594. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  30595. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  30596. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  30597. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  30598. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  30599. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  30600. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  30601. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  30602. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  30603. 0x3a, 0x00
  30604. };
  30605. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  30606. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  30607. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  30608. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  30609. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  30610. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  30611. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  30612. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  30613. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  30614. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  30615. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  30616. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  30617. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  30618. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  30619. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  30620. 0x3c, 0x00
  30621. };
  30622. /* uncompressed test */
  30623. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  30624. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  30625. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  30626. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  30627. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  30628. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  30629. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  30630. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  30631. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  30632. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  30633. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  30634. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  30635. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  30636. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  30637. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  30638. 0x26, 0x00
  30639. };
  30640. /* compressed prefix */
  30641. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  30642. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  30643. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  30644. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  30645. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  30646. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  30647. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  30648. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  30649. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  30650. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  30651. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  30652. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  30653. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  30654. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  30655. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  30656. 0x26, 0x00
  30657. };
  30658. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  30659. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  30660. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  30661. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  30662. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  30663. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  30664. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  30665. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  30666. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  30667. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  30668. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  30669. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  30670. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  30671. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  30672. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  30673. 0x1a, 0x00
  30674. };
  30675. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  30676. #define SIGSZ sizeof(sig1)
  30677. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  30678. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  30679. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  30680. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  30681. /* test of a 1023 byte long message */
  30682. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  30683. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  30684. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  30685. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  30686. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  30687. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  30688. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  30689. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  30690. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  30691. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  30692. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  30693. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  30694. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  30695. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  30696. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  30697. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  30698. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  30699. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  30700. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  30701. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  30702. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  30703. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  30704. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  30705. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  30706. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  30707. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  30708. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  30709. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  30710. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  30711. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  30712. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  30713. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  30714. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  30715. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  30716. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  30717. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  30718. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  30719. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  30720. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  30721. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  30722. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  30723. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  30724. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  30725. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  30726. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  30727. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  30728. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  30729. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  30730. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  30731. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  30732. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  30733. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  30734. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  30735. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  30736. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  30737. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  30738. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  30739. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  30740. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  30741. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  30742. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  30743. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  30744. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  30745. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  30746. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  30747. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  30748. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  30749. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  30750. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  30751. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  30752. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  30753. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  30754. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  30755. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  30756. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  30757. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  30758. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  30759. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  30760. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  30761. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  30762. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  30763. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  30764. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  30765. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  30766. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  30767. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  30768. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  30769. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  30770. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  30771. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  30772. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  30773. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  30774. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  30775. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  30776. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  30777. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  30778. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  30779. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  30780. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  30781. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  30782. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  30783. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  30784. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  30785. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  30786. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  30787. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  30788. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  30789. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  30790. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  30791. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  30792. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  30793. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  30794. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  30795. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  30796. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  30797. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  30798. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  30799. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  30800. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  30801. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  30802. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  30803. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  30804. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  30805. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  30806. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  30807. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  30808. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  30809. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  30810. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  30811. };
  30812. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  30813. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  30814. sizeof(msg2),
  30815. sizeof(msg3),
  30816. 0 /*sizeof(msg1)*/,
  30817. 0 /*sizeof(msg1)*/,
  30818. sizeof(msg4)
  30819. };
  30820. #ifndef NO_ASN
  30821. static const byte privateEd448[] = {
  30822. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  30823. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  30824. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  30825. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  30826. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  30827. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  30828. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  30829. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  30830. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  30831. 0x5b
  30832. };
  30833. static const byte publicEd448[] = {
  30834. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  30835. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  30836. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  30837. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  30838. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  30839. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  30840. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  30841. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  30842. 0xaf, 0xe8, 0x25, 0x61, 0x80
  30843. };
  30844. static const byte privPubEd448[] = {
  30845. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  30846. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  30847. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  30848. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  30849. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  30850. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  30851. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  30852. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  30853. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  30854. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  30855. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  30856. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  30857. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  30858. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  30859. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  30860. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  30861. 0xaf, 0xe8, 0x25, 0x61, 0x80
  30862. };
  30863. word32 idx;
  30864. #endif /* NO_ASN */
  30865. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  30866. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30867. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30868. ed448_key *key3 = NULL;
  30869. #else
  30870. ed448_key key3[1];
  30871. #endif
  30872. #endif
  30873. WOLFSSL_ENTER("ed448_test");
  30874. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30875. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30876. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30877. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30878. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30879. #endif
  30880. #endif
  30881. /* create ed448 keys */
  30882. #ifndef HAVE_FIPS
  30883. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30884. #else
  30885. ret = wc_InitRng(&rng);
  30886. #endif
  30887. if (ret != 0) {
  30888. XMEMSET(&rng, 0, sizeof(rng));
  30889. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30890. }
  30891. ret = wc_ed448_init(key);
  30892. if (ret < 0)
  30893. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30894. ret = wc_ed448_init(key2);
  30895. if (ret < 0)
  30896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30897. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  30898. ret = wc_ed448_init(key3);
  30899. if (ret < 0)
  30900. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30901. #endif
  30902. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  30903. if (ret < 0)
  30904. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30905. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  30906. if (ret < 0)
  30907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  30908. /* helper functions for signature and key size */
  30909. keySz = wc_ed448_size(key);
  30910. sigSz = wc_ed448_sig_size(key);
  30911. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  30912. defined(HAVE_ED448_KEY_IMPORT)
  30913. for (i = 0; i < 6; i++) {
  30914. outlen = sizeof(out);
  30915. XMEMSET(out, 0, sizeof(out));
  30916. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  30917. pKeySz[i], key) != 0)
  30918. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30919. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  30920. 0) != 0)
  30921. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30922. if (XMEMCMP(out, sigs[i], 114))
  30923. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30924. #if defined(HAVE_ED448_VERIFY)
  30925. /* test verify on good msg */
  30926. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  30927. NULL, 0) != 0 || verify != 1)
  30928. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30929. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  30930. /* test verify on good msg using streaming interface directly */
  30931. if (wc_ed448_verify_msg_init(out, outlen,
  30932. key, (byte)Ed448, NULL, 0) != 0)
  30933. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30934. for (j = 0; j < msgSz[i]; j += i) {
  30935. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  30936. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30937. }
  30938. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  30939. key) != 0 || verify != 1)
  30940. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30941. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  30942. /* test verify on bad msg */
  30943. out[outlen-2] = out[outlen-2] + 1;
  30944. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  30945. NULL, 0) == 0 || verify == 1)
  30946. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30947. #endif /* HAVE_ED448_VERIFY */
  30948. /* test api for import/exporting keys */
  30949. {
  30950. byte *exportPKey = NULL;
  30951. byte *exportSKey = NULL;
  30952. word32 exportPSz = ED448_KEY_SIZE;
  30953. word32 exportSSz = ED448_KEY_SIZE;
  30954. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30955. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30956. if ((exportPKey == NULL) || (exportSKey == NULL))
  30957. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  30958. ret = 0;
  30959. do {
  30960. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  30961. ret = WC_TEST_RET_ENC_I(i);
  30962. break;
  30963. }
  30964. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  30965. ret = WC_TEST_RET_ENC_I(i);
  30966. break;
  30967. }
  30968. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  30969. ret = WC_TEST_RET_ENC_I(i);
  30970. break;
  30971. }
  30972. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  30973. exportPKey, exportPSz, key2) != 0) {
  30974. ret = WC_TEST_RET_ENC_I(i);
  30975. break;
  30976. }
  30977. /* clear "out" buffer and test sign with imported keys */
  30978. outlen = sizeof(out);
  30979. XMEMSET(out, 0, sizeof(out));
  30980. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  30981. 0) != 0) {
  30982. ret = WC_TEST_RET_ENC_I(i);
  30983. break;
  30984. }
  30985. } while(0);
  30986. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30987. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30988. if (ret != 0)
  30989. goto out;
  30990. }
  30991. #if defined(HAVE_ED448_VERIFY)
  30992. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  30993. NULL, 0) != 0 || verify != 1)
  30994. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30995. if (XMEMCMP(out, sigs[i], SIGSZ))
  30996. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  30997. #endif /* HAVE_ED448_VERIFY */
  30998. }
  30999. ret = ed448_ctx_test();
  31000. if (ret != 0)
  31001. goto out;
  31002. ret = ed448ph_test();
  31003. if (ret != 0)
  31004. goto out;
  31005. #ifndef NO_ASN
  31006. /* Try ASN.1 encoded private-only key and public key. */
  31007. idx = 0;
  31008. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  31009. sizeof(privateEd448));
  31010. if (ret != 0)
  31011. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31012. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  31013. if (ret != BAD_FUNC_ARG)
  31014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31015. idx = 0;
  31016. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  31017. if (ret != 0)
  31018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31019. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  31020. if (ret != 0)
  31021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31022. if (XMEMCMP(out, sigs[0], SIGSZ))
  31023. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31024. #if defined(HAVE_ED448_VERIFY)
  31025. /* test verify on good msg */
  31026. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  31027. NULL, 0);
  31028. if (ret != 0 || verify != 1)
  31029. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31030. #endif /* HAVE_ED448_VERIFY */
  31031. wc_ed448_free(key3);
  31032. ret = wc_ed448_init(key3);
  31033. if (ret < 0)
  31034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31035. idx = 0;
  31036. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  31037. sizeof(privPubEd448));
  31038. if (ret != 0)
  31039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31040. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  31041. if (ret != 0)
  31042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31043. if (XMEMCMP(out, sigs[0], SIGSZ))
  31044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31045. #endif /* NO_ASN */
  31046. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  31047. ret = 0;
  31048. out:
  31049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31050. if (key) {
  31051. wc_ed448_free(key);
  31052. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31053. }
  31054. if (key2) {
  31055. wc_ed448_free(key2);
  31056. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31057. }
  31058. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  31059. if (key3) {
  31060. wc_ed448_free(key3);
  31061. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31062. }
  31063. #endif
  31064. #else
  31065. wc_ed448_free(key);
  31066. wc_ed448_free(key2);
  31067. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  31068. wc_ed448_free(key3);
  31069. #endif
  31070. #endif
  31071. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  31072. wc_FreeRng(&rng);
  31073. #endif
  31074. if (ret < 0)
  31075. return ret;
  31076. /* hush warnings of unused keySz and sigSz */
  31077. (void)keySz;
  31078. (void)sigSz;
  31079. #ifdef WOLFSSL_TEST_CERT
  31080. ret = ed448_test_cert();
  31081. if (ret < 0)
  31082. return ret;
  31083. #ifdef WOLFSSL_CERT_GEN
  31084. ret = ed448_test_make_cert();
  31085. if (ret < 0)
  31086. return ret;
  31087. #endif /* WOLFSSL_CERT_GEN */
  31088. #endif /* WOLFSSL_TEST_CERT */
  31089. return 0;
  31090. }
  31091. #endif /* HAVE_ED448 */
  31092. #ifdef WOLFSSL_HAVE_KYBER
  31093. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  31094. #ifdef WOLFSSL_KYBER512
  31095. static wc_test_ret_t kyber512_kat(void)
  31096. {
  31097. KyberKey key;
  31098. wc_test_ret_t ret;
  31099. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  31100. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  31101. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  31102. byte ss[KYBER_SS_SZ];
  31103. byte ss_dec[KYBER_SS_SZ];
  31104. const byte kyber512_rand[] = {
  31105. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  31106. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  31107. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  31108. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  31109. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  31110. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  31111. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  31112. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  31113. };
  31114. const byte kyber512enc_rand[] = {
  31115. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  31116. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  31117. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  31118. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  31119. };
  31120. const byte kyber512_pk[] = {
  31121. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  31122. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  31123. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  31124. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  31125. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  31126. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  31127. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  31128. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  31129. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  31130. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  31131. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  31132. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  31133. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  31134. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  31135. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  31136. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  31137. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  31138. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  31139. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  31140. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  31141. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  31142. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  31143. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  31144. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  31145. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  31146. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  31147. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  31148. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  31149. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  31150. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  31151. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  31152. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  31153. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  31154. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  31155. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  31156. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  31157. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  31158. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  31159. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  31160. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  31161. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  31162. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  31163. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  31164. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  31165. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  31166. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  31167. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  31168. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  31169. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  31170. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  31171. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  31172. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  31173. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  31174. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  31175. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  31176. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  31177. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  31178. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  31179. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  31180. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  31181. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  31182. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  31183. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  31184. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  31185. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  31186. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  31187. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  31188. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  31189. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  31190. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  31191. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  31192. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  31193. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  31194. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  31195. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  31196. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  31197. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  31198. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  31199. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  31200. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  31201. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  31202. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  31203. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  31204. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  31205. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  31206. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  31207. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  31208. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  31209. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  31210. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  31211. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  31212. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  31213. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  31214. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  31215. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  31216. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  31217. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31218. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31219. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31220. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  31221. };
  31222. const byte kyber512_sk[] = {
  31223. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  31224. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  31225. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  31226. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  31227. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  31228. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  31229. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  31230. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  31231. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  31232. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  31233. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  31234. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  31235. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  31236. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  31237. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  31238. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  31239. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  31240. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  31241. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  31242. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  31243. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  31244. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  31245. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  31246. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  31247. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  31248. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  31249. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  31250. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  31251. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  31252. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  31253. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  31254. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  31255. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  31256. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  31257. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  31258. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  31259. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  31260. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  31261. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  31262. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  31263. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  31264. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  31265. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  31266. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  31267. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  31268. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  31269. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  31270. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  31271. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  31272. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  31273. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  31274. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  31275. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  31276. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  31277. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  31278. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  31279. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  31280. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  31281. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  31282. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  31283. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  31284. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  31285. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  31286. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  31287. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  31288. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  31289. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  31290. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  31291. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  31292. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  31293. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  31294. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  31295. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  31296. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  31297. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  31298. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  31299. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  31300. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  31301. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  31302. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  31303. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  31304. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  31305. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  31306. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  31307. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  31308. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  31309. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  31310. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  31311. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  31312. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  31313. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  31314. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  31315. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  31316. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  31317. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  31318. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  31319. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  31320. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  31321. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  31322. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  31323. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  31324. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  31325. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  31326. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  31327. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  31328. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  31329. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  31330. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  31331. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  31332. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  31333. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  31334. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  31335. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  31336. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  31337. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  31338. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  31339. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  31340. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  31341. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  31342. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  31343. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  31344. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  31345. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  31346. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  31347. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  31348. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  31349. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  31350. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  31351. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  31352. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  31353. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  31354. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  31355. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  31356. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  31357. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  31358. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  31359. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  31360. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  31361. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  31362. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  31363. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  31364. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  31365. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  31366. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  31367. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  31368. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  31369. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  31370. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  31371. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  31372. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  31373. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  31374. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  31375. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  31376. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  31377. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  31378. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  31379. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  31380. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  31381. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  31382. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  31383. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  31384. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  31385. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  31386. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  31387. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  31388. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  31389. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  31390. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  31391. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  31392. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  31393. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  31394. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  31395. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  31396. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  31397. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  31398. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  31399. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  31400. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  31401. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  31402. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  31403. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  31404. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  31405. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  31406. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  31407. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  31408. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  31409. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  31410. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  31411. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  31412. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  31413. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  31414. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  31415. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31416. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31417. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31418. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  31419. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  31420. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  31421. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  31422. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  31423. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  31424. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  31425. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  31426. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  31427. };
  31428. const byte kyber512_ct[] = {
  31429. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  31430. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  31431. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  31432. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  31433. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  31434. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  31435. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  31436. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  31437. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  31438. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  31439. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  31440. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  31441. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  31442. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  31443. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  31444. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  31445. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  31446. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  31447. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  31448. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  31449. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  31450. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  31451. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  31452. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  31453. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  31454. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  31455. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  31456. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  31457. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  31458. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  31459. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  31460. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  31461. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  31462. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  31463. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  31464. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  31465. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  31466. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  31467. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  31468. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  31469. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  31470. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  31471. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  31472. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  31473. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  31474. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  31475. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  31476. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  31477. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  31478. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  31479. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  31480. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  31481. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  31482. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  31483. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  31484. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  31485. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  31486. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  31487. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  31488. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  31489. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  31490. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  31491. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  31492. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  31493. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  31494. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  31495. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  31496. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  31497. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  31498. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  31499. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  31500. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  31501. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  31502. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  31503. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  31504. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  31505. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  31506. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  31507. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  31508. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  31509. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  31510. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  31511. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  31512. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  31513. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  31514. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  31515. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  31516. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  31517. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  31518. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  31519. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  31520. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  31521. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  31522. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  31523. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  31524. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  31525. };
  31526. const byte kyber512_ss[] = {
  31527. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  31528. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  31529. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  31530. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  31531. };
  31532. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  31533. if (ret != 0)
  31534. return WC_TEST_RET_ENC_EC(ret);
  31535. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  31536. sizeof(kyber512_rand));
  31537. if (ret != 0)
  31538. return WC_TEST_RET_ENC_EC(ret);
  31539. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  31540. if (ret != 0)
  31541. return WC_TEST_RET_ENC_EC(ret);
  31542. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  31543. if (ret != 0)
  31544. return WC_TEST_RET_ENC_EC(ret);
  31545. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  31546. return WC_TEST_RET_ENC_NC;
  31547. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  31548. return WC_TEST_RET_ENC_NC;
  31549. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  31550. sizeof(kyber512enc_rand));
  31551. if (ret != 0)
  31552. return WC_TEST_RET_ENC_EC(ret);
  31553. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  31554. return WC_TEST_RET_ENC_NC;
  31555. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  31556. return WC_TEST_RET_ENC_NC;
  31557. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  31558. if (ret != 0)
  31559. return WC_TEST_RET_ENC_EC(ret);
  31560. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  31561. return WC_TEST_RET_ENC_NC;
  31562. wc_KyberKey_Free(&key);
  31563. return 0;
  31564. }
  31565. #endif /* WOLFSSL_KYBER512 */
  31566. #ifdef WOLFSSL_KYBER768
  31567. static wc_test_ret_t kyber768_kat(void)
  31568. {
  31569. KyberKey key;
  31570. wc_test_ret_t ret;
  31571. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  31572. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  31573. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  31574. byte ss[KYBER_SS_SZ];
  31575. byte ss_dec[KYBER_SS_SZ];
  31576. const byte kyber768_rand[] = {
  31577. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  31578. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  31579. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  31580. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  31581. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  31582. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  31583. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  31584. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  31585. };
  31586. const byte kyber768enc_rand[] = {
  31587. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  31588. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  31589. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  31590. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  31591. };
  31592. const byte kyber768_pk[] = {
  31593. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  31594. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  31595. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  31596. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  31597. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  31598. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  31599. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  31600. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  31601. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  31602. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  31603. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  31604. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  31605. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  31606. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  31607. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  31608. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  31609. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  31610. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  31611. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  31612. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  31613. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  31614. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  31615. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  31616. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  31617. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  31618. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  31619. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  31620. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  31621. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  31622. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  31623. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  31624. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  31625. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  31626. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  31627. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  31628. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  31629. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  31630. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  31631. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  31632. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  31633. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  31634. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  31635. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  31636. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  31637. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  31638. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  31639. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  31640. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  31641. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  31642. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  31643. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  31644. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  31645. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  31646. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  31647. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  31648. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  31649. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  31650. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  31651. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  31652. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  31653. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  31654. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  31655. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  31656. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  31657. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  31658. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  31659. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  31660. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  31661. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  31662. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  31663. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  31664. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  31665. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  31666. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  31667. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  31668. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  31669. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  31670. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  31671. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  31672. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  31673. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  31674. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  31675. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  31676. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  31677. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  31678. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  31679. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  31680. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  31681. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  31682. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  31683. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  31684. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  31685. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  31686. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  31687. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  31688. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  31689. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  31690. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  31691. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  31692. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  31693. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  31694. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  31695. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  31696. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  31697. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  31698. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  31699. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  31700. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  31701. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  31702. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  31703. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  31704. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  31705. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  31706. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  31707. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  31708. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  31709. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  31710. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  31711. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  31712. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  31713. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  31714. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  31715. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  31716. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  31717. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  31718. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  31719. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  31720. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  31721. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  31722. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  31723. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  31724. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  31725. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  31726. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  31727. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  31728. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  31729. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  31730. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  31731. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  31732. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  31733. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  31734. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  31735. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  31736. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  31737. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  31738. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  31739. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  31740. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  31741. };
  31742. const byte kyber768_sk[] = {
  31743. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  31744. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  31745. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  31746. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  31747. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  31748. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  31749. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  31750. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  31751. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  31752. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  31753. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  31754. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  31755. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  31756. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  31757. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  31758. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  31759. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  31760. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  31761. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  31762. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  31763. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  31764. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  31765. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  31766. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  31767. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  31768. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  31769. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  31770. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  31771. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  31772. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  31773. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  31774. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  31775. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  31776. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  31777. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  31778. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  31779. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  31780. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  31781. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  31782. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  31783. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  31784. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  31785. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  31786. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  31787. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  31788. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  31789. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  31790. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  31791. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  31792. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  31793. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  31794. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  31795. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  31796. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  31797. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  31798. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  31799. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  31800. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  31801. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  31802. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  31803. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  31804. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  31805. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  31806. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  31807. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  31808. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  31809. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  31810. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  31811. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  31812. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  31813. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  31814. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  31815. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  31816. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  31817. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  31818. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  31819. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  31820. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  31821. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  31822. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  31823. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  31824. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  31825. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  31826. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  31827. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  31828. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  31829. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  31830. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  31831. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  31832. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  31833. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  31834. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  31835. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  31836. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  31837. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  31838. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  31839. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  31840. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  31841. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  31842. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  31843. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  31844. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  31845. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  31846. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  31847. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  31848. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  31849. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  31850. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  31851. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  31852. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  31853. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  31854. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  31855. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  31856. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  31857. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  31858. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  31859. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  31860. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  31861. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  31862. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  31863. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  31864. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  31865. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  31866. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  31867. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  31868. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  31869. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  31870. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  31871. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  31872. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  31873. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  31874. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  31875. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  31876. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  31877. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  31878. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  31879. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  31880. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  31881. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  31882. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  31883. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  31884. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  31885. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  31886. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  31887. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  31888. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  31889. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  31890. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  31891. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  31892. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  31893. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  31894. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  31895. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  31896. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  31897. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  31898. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  31899. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  31900. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  31901. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  31902. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  31903. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  31904. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  31905. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  31906. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  31907. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  31908. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  31909. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  31910. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  31911. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  31912. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  31913. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  31914. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  31915. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  31916. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  31917. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  31918. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  31919. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  31920. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  31921. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  31922. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  31923. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  31924. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  31925. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  31926. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  31927. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  31928. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  31929. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  31930. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  31931. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  31932. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  31933. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  31934. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  31935. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  31936. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  31937. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  31938. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  31939. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  31940. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  31941. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  31942. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  31943. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  31944. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  31945. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  31946. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  31947. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  31948. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  31949. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  31950. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  31951. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  31952. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  31953. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  31954. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  31955. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  31956. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  31957. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  31958. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  31959. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  31960. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  31961. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  31962. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  31963. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  31964. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  31965. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  31966. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  31967. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  31968. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  31969. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  31970. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  31971. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  31972. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  31973. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  31974. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  31975. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  31976. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  31977. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  31978. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  31979. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  31980. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  31981. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  31982. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  31983. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  31984. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  31985. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  31986. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  31987. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  31988. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  31989. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  31990. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  31991. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  31992. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  31993. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  31994. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  31995. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  31996. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  31997. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  31998. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  31999. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  32000. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  32001. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  32002. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  32003. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  32004. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  32005. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  32006. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  32007. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  32008. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  32009. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  32010. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  32011. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  32012. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  32013. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  32014. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  32015. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  32016. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  32017. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  32018. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  32019. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  32020. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  32021. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  32022. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  32023. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  32024. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  32025. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  32026. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  32027. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  32028. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  32029. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  32030. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  32031. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  32032. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  32033. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  32034. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  32035. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  32036. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  32037. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  32038. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  32039. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  32040. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  32041. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  32042. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  32043. };
  32044. const byte kyber768_ct[] = {
  32045. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  32046. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  32047. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  32048. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  32049. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  32050. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  32051. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  32052. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  32053. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  32054. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  32055. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  32056. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  32057. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  32058. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  32059. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  32060. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  32061. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  32062. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  32063. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  32064. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  32065. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  32066. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  32067. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  32068. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  32069. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  32070. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  32071. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  32072. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  32073. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  32074. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  32075. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  32076. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  32077. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  32078. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  32079. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  32080. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  32081. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  32082. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  32083. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  32084. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  32085. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  32086. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  32087. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  32088. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  32089. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  32090. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  32091. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  32092. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  32093. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  32094. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  32095. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  32096. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  32097. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  32098. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  32099. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  32100. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  32101. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  32102. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  32103. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  32104. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  32105. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  32106. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  32107. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  32108. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  32109. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  32110. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  32111. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  32112. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  32113. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  32114. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  32115. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  32116. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  32117. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  32118. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  32119. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  32120. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  32121. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  32122. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  32123. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  32124. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  32125. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  32126. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  32127. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  32128. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  32129. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  32130. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  32131. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  32132. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  32133. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  32134. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  32135. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  32136. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  32137. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  32138. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  32139. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  32140. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  32141. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  32142. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  32143. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  32144. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  32145. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  32146. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  32147. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  32148. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  32149. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  32150. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  32151. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  32152. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  32153. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  32154. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  32155. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  32156. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  32157. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  32158. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  32159. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  32160. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  32161. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  32162. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  32163. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  32164. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  32165. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  32166. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  32167. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  32168. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  32169. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  32170. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  32171. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  32172. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  32173. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  32174. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  32175. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  32176. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  32177. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  32178. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  32179. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  32180. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  32181. };
  32182. const byte kyber768_ss[] = {
  32183. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  32184. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  32185. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  32186. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  32187. };
  32188. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  32189. if (ret != 0)
  32190. return WC_TEST_RET_ENC_EC(ret);
  32191. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  32192. sizeof(kyber768_rand));
  32193. if (ret != 0)
  32194. return WC_TEST_RET_ENC_EC(ret);
  32195. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  32196. if (ret != 0)
  32197. return WC_TEST_RET_ENC_EC(ret);
  32198. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  32199. if (ret != 0)
  32200. return WC_TEST_RET_ENC_EC(ret);
  32201. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  32202. return WC_TEST_RET_ENC_NC;
  32203. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  32204. return WC_TEST_RET_ENC_NC;
  32205. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  32206. sizeof(kyber768enc_rand));
  32207. if (ret != 0)
  32208. return WC_TEST_RET_ENC_EC(ret);
  32209. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  32210. return WC_TEST_RET_ENC_NC;
  32211. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  32212. return WC_TEST_RET_ENC_NC;
  32213. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  32214. if (ret != 0)
  32215. return WC_TEST_RET_ENC_EC(ret);
  32216. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  32217. return WC_TEST_RET_ENC_NC;
  32218. wc_KyberKey_Free(&key);
  32219. return 0;
  32220. }
  32221. #endif /* WOLFSSL_KYBER768 */
  32222. #ifdef WOLFSSL_KYBER1024
  32223. static wc_test_ret_t kyber1024_kat(void)
  32224. {
  32225. KyberKey key;
  32226. wc_test_ret_t ret;
  32227. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  32228. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  32229. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  32230. byte ss[KYBER_SS_SZ];
  32231. byte ss_dec[KYBER_SS_SZ];
  32232. const byte kyber1024_rand[] = {
  32233. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  32234. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  32235. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  32236. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  32237. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  32238. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  32239. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  32240. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  32241. };
  32242. const byte kyber1024enc_rand[] = {
  32243. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  32244. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  32245. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  32246. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  32247. };
  32248. const byte kyber1024_pk[] = {
  32249. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  32250. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  32251. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  32252. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  32253. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  32254. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  32255. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  32256. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  32257. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  32258. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  32259. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  32260. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  32261. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  32262. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  32263. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  32264. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  32265. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  32266. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  32267. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  32268. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  32269. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  32270. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  32271. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  32272. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  32273. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  32274. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  32275. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  32276. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  32277. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  32278. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  32279. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  32280. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  32281. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  32282. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  32283. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  32284. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  32285. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  32286. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  32287. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  32288. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  32289. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  32290. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  32291. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  32292. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  32293. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  32294. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  32295. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  32296. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  32297. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  32298. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  32299. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  32300. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  32301. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  32302. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  32303. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  32304. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  32305. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  32306. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  32307. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  32308. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  32309. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  32310. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  32311. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  32312. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  32313. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  32314. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  32315. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  32316. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  32317. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  32318. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  32319. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  32320. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  32321. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  32322. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  32323. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  32324. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  32325. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  32326. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  32327. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  32328. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  32329. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  32330. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  32331. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  32332. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  32333. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  32334. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  32335. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  32336. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  32337. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  32338. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  32339. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  32340. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  32341. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  32342. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  32343. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  32344. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  32345. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  32346. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  32347. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  32348. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  32349. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  32350. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  32351. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  32352. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  32353. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  32354. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  32355. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  32356. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  32357. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  32358. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  32359. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  32360. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  32361. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  32362. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  32363. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  32364. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  32365. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  32366. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  32367. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  32368. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  32369. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  32370. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  32371. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  32372. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  32373. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  32374. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  32375. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  32376. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  32377. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  32378. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  32379. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  32380. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  32381. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  32382. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  32383. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  32384. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  32385. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  32386. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  32387. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  32388. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  32389. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  32390. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  32391. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  32392. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  32393. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  32394. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  32395. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  32396. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  32397. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  32398. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  32399. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  32400. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  32401. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  32402. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  32403. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  32404. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  32405. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  32406. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  32407. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  32408. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  32409. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  32410. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  32411. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  32412. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  32413. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  32414. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  32415. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  32416. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  32417. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  32418. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  32419. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  32420. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  32421. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  32422. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  32423. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  32424. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  32425. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  32426. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  32427. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  32428. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  32429. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  32430. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  32431. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  32432. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  32433. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  32434. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  32435. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  32436. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  32437. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  32438. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  32439. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  32440. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  32441. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  32442. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  32443. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  32444. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  32445. };
  32446. const byte kyber1024_sk[] = {
  32447. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  32448. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  32449. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  32450. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  32451. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  32452. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  32453. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  32454. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  32455. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  32456. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  32457. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  32458. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  32459. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  32460. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  32461. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  32462. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  32463. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  32464. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  32465. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  32466. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  32467. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  32468. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  32469. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  32470. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  32471. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  32472. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  32473. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  32474. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  32475. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  32476. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  32477. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  32478. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  32479. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  32480. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  32481. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  32482. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  32483. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  32484. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  32485. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  32486. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  32487. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  32488. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  32489. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  32490. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  32491. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  32492. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  32493. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  32494. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  32495. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  32496. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  32497. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  32498. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  32499. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  32500. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  32501. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  32502. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  32503. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  32504. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  32505. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  32506. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  32507. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  32508. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  32509. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  32510. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  32511. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  32512. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  32513. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  32514. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  32515. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  32516. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  32517. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  32518. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  32519. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  32520. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  32521. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  32522. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  32523. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  32524. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  32525. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  32526. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  32527. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  32528. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  32529. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  32530. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  32531. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  32532. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  32533. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  32534. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  32535. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  32536. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  32537. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  32538. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  32539. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  32540. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  32541. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  32542. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  32543. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  32544. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  32545. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  32546. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  32547. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  32548. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  32549. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  32550. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  32551. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  32552. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  32553. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  32554. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  32555. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  32556. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  32557. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  32558. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  32559. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  32560. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  32561. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  32562. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  32563. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  32564. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  32565. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  32566. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  32567. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  32568. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  32569. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  32570. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  32571. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  32572. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  32573. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  32574. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  32575. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  32576. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  32577. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  32578. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  32579. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  32580. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  32581. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  32582. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  32583. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  32584. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  32585. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  32586. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  32587. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  32588. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  32589. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  32590. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  32591. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  32592. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  32593. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  32594. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  32595. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  32596. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  32597. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  32598. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  32599. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  32600. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  32601. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  32602. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  32603. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  32604. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  32605. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  32606. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  32607. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  32608. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  32609. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  32610. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  32611. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  32612. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  32613. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  32614. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  32615. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  32616. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  32617. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  32618. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  32619. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  32620. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  32621. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  32622. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  32623. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  32624. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  32625. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  32626. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  32627. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  32628. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  32629. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  32630. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  32631. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  32632. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  32633. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  32634. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  32635. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  32636. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  32637. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  32638. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  32639. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  32640. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  32641. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  32642. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  32643. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  32644. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  32645. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  32646. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  32647. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  32648. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  32649. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  32650. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  32651. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  32652. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  32653. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  32654. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  32655. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  32656. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  32657. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  32658. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  32659. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  32660. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  32661. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  32662. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  32663. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  32664. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  32665. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  32666. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  32667. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  32668. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  32669. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  32670. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  32671. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  32672. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  32673. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  32674. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  32675. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  32676. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  32677. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  32678. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  32679. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  32680. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  32681. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  32682. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  32683. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  32684. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  32685. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  32686. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  32687. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  32688. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  32689. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  32690. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  32691. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  32692. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  32693. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  32694. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  32695. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  32696. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  32697. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  32698. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  32699. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  32700. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  32701. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  32702. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  32703. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  32704. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  32705. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  32706. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  32707. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  32708. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  32709. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  32710. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  32711. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  32712. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  32713. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  32714. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  32715. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  32716. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  32717. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  32718. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  32719. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  32720. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  32721. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  32722. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  32723. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  32724. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  32725. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  32726. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  32727. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  32728. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  32729. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  32730. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  32731. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  32732. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  32733. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  32734. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  32735. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  32736. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  32737. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  32738. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  32739. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  32740. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  32741. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  32742. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  32743. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  32744. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  32745. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  32746. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  32747. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  32748. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  32749. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  32750. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  32751. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  32752. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  32753. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  32754. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  32755. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  32756. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  32757. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  32758. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  32759. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  32760. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  32761. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  32762. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  32763. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  32764. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  32765. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  32766. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  32767. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  32768. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  32769. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  32770. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  32771. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  32772. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  32773. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  32774. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  32775. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  32776. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  32777. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  32778. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  32779. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  32780. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  32781. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  32782. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  32783. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  32784. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  32785. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  32786. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  32787. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  32788. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  32789. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  32790. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  32791. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  32792. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  32793. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  32794. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  32795. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  32796. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  32797. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  32798. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  32799. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  32800. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  32801. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  32802. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  32803. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  32804. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  32805. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  32806. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  32807. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  32808. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  32809. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  32810. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  32811. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  32812. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  32813. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  32814. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  32815. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  32816. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  32817. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  32818. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  32819. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  32820. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  32821. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  32822. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  32823. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  32824. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  32825. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  32826. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  32827. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  32828. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  32829. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  32830. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  32831. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  32832. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  32833. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  32834. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  32835. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  32836. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  32837. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  32838. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  32839. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  32840. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  32841. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  32842. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  32843. };
  32844. const byte kyber1024_ct[] = {
  32845. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  32846. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  32847. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  32848. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  32849. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  32850. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  32851. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  32852. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  32853. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  32854. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  32855. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  32856. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  32857. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  32858. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  32859. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  32860. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  32861. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  32862. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  32863. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  32864. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  32865. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  32866. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  32867. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  32868. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  32869. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  32870. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  32871. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  32872. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  32873. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  32874. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  32875. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  32876. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  32877. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  32878. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  32879. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  32880. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  32881. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  32882. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  32883. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  32884. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  32885. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  32886. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  32887. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  32888. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  32889. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  32890. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  32891. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  32892. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  32893. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  32894. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  32895. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  32896. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  32897. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  32898. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  32899. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  32900. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  32901. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  32902. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  32903. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  32904. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  32905. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  32906. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  32907. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  32908. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  32909. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  32910. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  32911. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  32912. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  32913. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  32914. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  32915. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  32916. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  32917. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  32918. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  32919. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  32920. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  32921. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  32922. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  32923. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  32924. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  32925. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  32926. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  32927. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  32928. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  32929. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  32930. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  32931. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  32932. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  32933. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  32934. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  32935. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  32936. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  32937. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  32938. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  32939. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  32940. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  32941. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  32942. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  32943. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  32944. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  32945. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  32946. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  32947. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  32948. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  32949. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  32950. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  32951. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  32952. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  32953. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  32954. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  32955. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  32956. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  32957. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  32958. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  32959. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  32960. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  32961. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  32962. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  32963. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  32964. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  32965. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  32966. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  32967. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  32968. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  32969. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  32970. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  32971. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  32972. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  32973. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  32974. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  32975. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  32976. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  32977. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  32978. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  32979. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  32980. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  32981. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  32982. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  32983. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  32984. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  32985. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  32986. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  32987. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  32988. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  32989. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  32990. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  32991. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  32992. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  32993. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  32994. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  32995. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  32996. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  32997. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  32998. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  32999. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  33000. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  33001. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  33002. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  33003. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  33004. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  33005. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  33006. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  33007. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  33008. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  33009. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  33010. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  33011. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  33012. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  33013. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  33014. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  33015. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  33016. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  33017. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  33018. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  33019. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  33020. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  33021. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  33022. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  33023. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  33024. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  33025. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  33026. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  33027. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  33028. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  33029. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  33030. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  33031. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  33032. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  33033. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  33034. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  33035. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  33036. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  33037. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  33038. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  33039. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  33040. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  33041. };
  33042. const byte kyber1024_ss[] = {
  33043. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  33044. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  33045. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  33046. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  33047. };
  33048. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  33049. if (ret != 0)
  33050. return WC_TEST_RET_ENC_EC(ret);
  33051. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  33052. sizeof(kyber1024_rand));
  33053. if (ret != 0)
  33054. return WC_TEST_RET_ENC_EC(ret);
  33055. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  33056. if (ret != 0)
  33057. return WC_TEST_RET_ENC_EC(ret);
  33058. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  33059. if (ret != 0)
  33060. return WC_TEST_RET_ENC_EC(ret);
  33061. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  33062. return WC_TEST_RET_ENC_NC;
  33063. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  33064. return WC_TEST_RET_ENC_NC;
  33065. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  33066. sizeof(kyber1024enc_rand));
  33067. if (ret != 0)
  33068. return WC_TEST_RET_ENC_EC(ret);
  33069. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  33070. return WC_TEST_RET_ENC_NC;
  33071. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  33072. return WC_TEST_RET_ENC_NC;
  33073. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  33074. if (ret != 0)
  33075. return WC_TEST_RET_ENC_EC(ret);
  33076. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  33077. return WC_TEST_RET_ENC_NC;
  33078. wc_KyberKey_Free(&key);
  33079. return 0;
  33080. }
  33081. #endif /* WOLFSSL_KYBER1024 */
  33082. #endif /* WOLFSSL_WC_KYBER */
  33083. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  33084. {
  33085. wc_test_ret_t ret;
  33086. KyberKey key;
  33087. WC_RNG rng;
  33088. int i;
  33089. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  33090. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  33091. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  33092. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  33093. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  33094. byte ss[KYBER_SS_SZ];
  33095. byte ss_dec[KYBER_SS_SZ];
  33096. int testData[][4] = {
  33097. #ifdef WOLFSSL_KYBER512
  33098. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  33099. KYBER512_CIPHER_TEXT_SIZE },
  33100. #endif
  33101. #ifdef WOLFSSL_KYBER768
  33102. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  33103. KYBER768_CIPHER_TEXT_SIZE },
  33104. #endif
  33105. #ifdef WOLFSSL_KYBER1024
  33106. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  33107. KYBER1024_CIPHER_TEXT_SIZE },
  33108. #endif
  33109. };
  33110. WOLFSSL_ENTER("kyber_test");
  33111. #ifndef HAVE_FIPS
  33112. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  33113. #else
  33114. ret = wc_InitRng(&rng);
  33115. #endif
  33116. if (ret != 0)
  33117. return WC_TEST_RET_ENC_EC(ret);
  33118. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  33119. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  33120. if (ret != 0)
  33121. return WC_TEST_RET_ENC_I(i);
  33122. ret = wc_KyberKey_MakeKey(&key, &rng);
  33123. if (ret != 0)
  33124. return WC_TEST_RET_ENC_I(i);
  33125. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  33126. if (ret != 0)
  33127. return WC_TEST_RET_ENC_I(i);
  33128. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  33129. if (ret != 0)
  33130. return WC_TEST_RET_ENC_I(i);
  33131. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  33132. if (ret != 0)
  33133. return WC_TEST_RET_ENC_I(i);
  33134. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  33135. if (ret != 0)
  33136. return WC_TEST_RET_ENC_I(i);
  33137. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  33138. if (ret != 0)
  33139. return WC_TEST_RET_ENC_I(i);
  33140. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  33141. if (ret != 0)
  33142. return WC_TEST_RET_ENC_I(i);
  33143. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  33144. return WC_TEST_RET_ENC_I(i);
  33145. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  33146. if (ret != 0)
  33147. return WC_TEST_RET_ENC_I(i);
  33148. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  33149. if (ret != 0)
  33150. return WC_TEST_RET_ENC_I(i);
  33151. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  33152. if (ret != 0)
  33153. return WC_TEST_RET_ENC_I(i);
  33154. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  33155. return WC_TEST_RET_ENC_I(i);
  33156. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  33157. if (ret != 0)
  33158. return WC_TEST_RET_ENC_I(i);
  33159. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  33160. return WC_TEST_RET_ENC_I(i);
  33161. wc_KyberKey_Free(&key);
  33162. }
  33163. wc_FreeRng(&rng);
  33164. #ifdef WOLFSSL_WC_KYBER
  33165. #ifdef WOLFSSL_KYBER512
  33166. ret = kyber512_kat();
  33167. if (ret != 0)
  33168. return ret;
  33169. #endif
  33170. #ifdef WOLFSSL_KYBER768
  33171. ret = kyber768_kat();
  33172. if (ret != 0)
  33173. return ret;
  33174. #endif
  33175. #ifdef WOLFSSL_KYBER1024
  33176. ret = kyber1024_kat();
  33177. if (ret != 0)
  33178. return ret;
  33179. #endif
  33180. #endif /* WOLFSSL_WC_KYBER */
  33181. return 0;
  33182. }
  33183. #endif /* WOLFSSL_HAVE_KYBER */
  33184. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  33185. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  33186. void *context)
  33187. {
  33188. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  33189. * BE USED FOR TESTING PURPOSES! Production applications should
  33190. * write only to non-volatile storage. */
  33191. XMEMCPY(context, priv, privSz);
  33192. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  33193. }
  33194. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  33195. void *context)
  33196. {
  33197. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  33198. * BE USED FOR TESTING PURPOSES! */
  33199. XMEMCPY(priv, context, privSz);
  33200. return WC_XMSS_RC_READ_TO_MEMORY;
  33201. }
  33202. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  33203. {
  33204. int i = 0;
  33205. int j = 0;
  33206. XmssKey signingKey;
  33207. XmssKey verifyKey;
  33208. WC_RNG rng;
  33209. word32 pkSz = 0;
  33210. word32 skSz = 0;
  33211. word32 sigSz = 0;
  33212. word32 bufSz = 0;
  33213. unsigned char * sk = NULL;
  33214. unsigned char * old_sk = NULL;
  33215. const char * msg = "XMSS post quantum signature test";
  33216. word32 msgSz = (word32) XSTRLEN(msg);
  33217. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10
  33218. const char * param = "XMSS-SHA2_10_256";
  33219. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 20
  33220. const char * param = "XMSSMT-SHA2_20/4_256";
  33221. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 40
  33222. const char * param = "XMSSMT-SHA2_40/8_256";
  33223. #else
  33224. const char * param = "XMSSMT-SHA2_60/12_256";
  33225. #endif
  33226. byte * sig = NULL;
  33227. int ret2 = -1;
  33228. int ret = -1;
  33229. WOLFSSL_ENTER("xmss_test");
  33230. #ifndef HAVE_FIPS
  33231. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  33232. #else
  33233. ret = wc_InitRng(&rng);
  33234. #endif
  33235. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33236. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  33237. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33238. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33239. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33240. /* Set the parameter string to the signing key, and
  33241. * get sizes for secret key, pub key, and signature. */
  33242. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  33243. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33244. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  33245. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33246. if (pkSz != XMSS_SHA256_PUBLEN) {
  33247. return WC_TEST_RET_ENC_EC(pkSz);
  33248. }
  33249. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  33250. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33251. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  33252. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33253. /* Allocate signature array. */
  33254. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33255. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  33256. bufSz = sigSz;
  33257. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  33258. fprintf(stderr, "param: %s\n", param);
  33259. fprintf(stderr, "pkSz: %d\n", pkSz);
  33260. fprintf(stderr, "skSz: %d\n", skSz);
  33261. fprintf(stderr, "sigSz: %d\n", sigSz);
  33262. #endif
  33263. /* Allocate current and old secret keys.*/
  33264. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33265. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  33266. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33267. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  33268. XMEMSET(sk, 0, skSz);
  33269. XMEMSET(old_sk, 0, skSz);
  33270. XMEMSET(sig, 0, sigSz);
  33271. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  33272. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33273. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  33274. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33275. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  33276. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33277. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  33278. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33279. /* Export the pub to a verify key. */
  33280. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  33281. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33282. /* Repeat a few times to check that:
  33283. * 1. The secret key is mutated on each sign.
  33284. * 2. We can verify each new signature.
  33285. * Only do a few times, because the full signature space
  33286. * for this parameter set is huge. */
  33287. for (i = 0; i < 10; ++i) {
  33288. XMEMCPY(old_sk, sk, skSz);
  33289. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  33290. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33291. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  33292. /* Old secret key and current secret key should not match. */
  33293. ret = XMEMCMP(old_sk, sk, skSz);
  33294. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  33295. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  33296. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33297. /* Flip bits in a few places throughout the signature, stepping in multiple
  33298. * of hash size. These should all fail with -1. */
  33299. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  33300. sig[j] ^= 1;
  33301. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  33302. msgSz);
  33303. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  33304. /* Verify passed when it should have failed. */
  33305. return WC_TEST_RET_ENC_I(j);
  33306. }
  33307. /* Flip this spot back. */
  33308. sig[j] ^= 1;
  33309. }
  33310. }
  33311. /* Cleanup everything. */
  33312. if (sig != NULL) {
  33313. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33314. sig = NULL;
  33315. }
  33316. if (sk != NULL) {
  33317. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33318. sk = NULL;
  33319. }
  33320. if (old_sk != NULL) {
  33321. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33322. old_sk = NULL;
  33323. }
  33324. wc_XmssKey_Free(&signingKey);
  33325. wc_FreeRng(&rng);
  33326. return ret;
  33327. }
  33328. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  33329. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_SMALL_STACK) && \
  33330. WOLFSSL_XMSS_MIN_HEIGHT <= 10
  33331. /* A simple xmss verify only test using:
  33332. * XMSS-SHA2_10_256
  33333. * pub len: 68
  33334. * msg len: 32
  33335. * sig len: 2500
  33336. *
  33337. * These were generated with the test xmss_fast, from the unpatched
  33338. * xmss-reference repository:
  33339. * https://github.com/XMSS/xmss-reference
  33340. * */
  33341. static const byte xmss_pub[XMSS_SHA256_PUBLEN] =
  33342. {
  33343. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  33344. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  33345. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  33346. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  33347. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  33348. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  33349. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  33350. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  33351. 0xC9,0xB7,0x39,0x4E
  33352. };
  33353. static /* not const */ byte xmss_msg[32] =
  33354. {
  33355. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  33356. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  33357. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  33358. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  33359. };
  33360. /* This was actually the 5th signature produced from
  33361. * xmss_fast test in xmss-reference. */
  33362. static /* not const */ byte xmss_sig[2500] =
  33363. {
  33364. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  33365. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  33366. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  33367. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  33368. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  33369. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  33370. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  33371. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  33372. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  33373. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  33374. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  33375. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  33376. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  33377. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  33378. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  33379. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  33380. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  33381. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  33382. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  33383. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  33384. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  33385. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  33386. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  33387. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  33388. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  33389. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  33390. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  33391. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  33392. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  33393. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  33394. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  33395. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  33396. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  33397. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  33398. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  33399. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  33400. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  33401. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  33402. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  33403. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  33404. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  33405. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  33406. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  33407. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  33408. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  33409. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  33410. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  33411. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  33412. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  33413. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  33414. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  33415. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  33416. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  33417. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  33418. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  33419. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  33420. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  33421. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  33422. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  33423. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  33424. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  33425. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  33426. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  33427. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  33428. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  33429. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  33430. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  33431. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  33432. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  33433. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  33434. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  33435. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  33436. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  33437. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  33438. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  33439. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  33440. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  33441. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  33442. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  33443. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  33444. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  33445. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  33446. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  33447. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  33448. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  33449. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  33450. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  33451. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  33452. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  33453. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  33454. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  33455. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  33456. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  33457. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  33458. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  33459. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  33460. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  33461. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  33462. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  33463. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  33464. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  33465. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  33466. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  33467. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  33468. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  33469. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  33470. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  33471. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  33472. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  33473. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  33474. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  33475. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  33476. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  33477. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  33478. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  33479. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  33480. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  33481. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  33482. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  33483. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  33484. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  33485. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  33486. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  33487. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  33488. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  33489. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  33490. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  33491. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  33492. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  33493. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  33494. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  33495. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  33496. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  33497. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  33498. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  33499. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  33500. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  33501. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  33502. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  33503. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  33504. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  33505. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  33506. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  33507. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  33508. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  33509. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  33510. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  33511. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  33512. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  33513. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  33514. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  33515. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  33516. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  33517. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  33518. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  33519. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  33520. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  33521. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  33522. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  33523. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  33524. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  33525. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  33526. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  33527. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  33528. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  33529. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  33530. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  33531. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  33532. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  33533. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  33534. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  33535. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  33536. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  33537. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  33538. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  33539. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  33540. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  33541. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  33542. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  33543. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  33544. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  33545. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  33546. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  33547. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  33548. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  33549. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  33550. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  33551. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  33552. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  33553. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  33554. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  33555. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  33556. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  33557. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  33558. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  33559. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  33560. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  33561. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  33562. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  33563. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  33564. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  33565. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  33566. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  33567. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  33568. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  33569. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  33570. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  33571. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  33572. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  33573. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  33574. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  33575. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  33576. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  33577. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  33578. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  33579. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  33580. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  33581. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  33582. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  33583. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  33584. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  33585. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  33586. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  33587. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  33588. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  33589. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  33590. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  33591. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  33592. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  33593. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  33594. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  33595. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  33596. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  33597. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  33598. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  33599. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  33600. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  33601. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  33602. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  33603. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  33604. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  33605. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  33606. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  33607. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  33608. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  33609. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  33610. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  33611. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  33612. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  33613. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  33614. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  33615. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  33616. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  33617. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  33618. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  33619. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  33620. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  33621. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  33622. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  33623. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  33624. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  33625. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  33626. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  33627. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  33628. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  33629. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  33630. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  33631. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  33632. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  33633. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  33634. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  33635. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  33636. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  33637. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  33638. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  33639. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  33640. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  33641. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  33642. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  33643. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  33644. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  33645. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  33646. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  33647. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  33648. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  33649. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  33650. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  33651. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  33652. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  33653. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  33654. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  33655. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  33656. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  33657. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  33658. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  33659. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  33660. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  33661. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  33662. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  33663. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  33664. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  33665. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  33666. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  33667. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  33668. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  33669. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  33670. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  33671. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  33672. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  33673. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  33674. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  33675. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  33676. 0xB3,0x5D,0x73,0x52
  33677. };
  33678. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  33679. {
  33680. XmssKey verifyKey;
  33681. word32 pkSz = 0;
  33682. word32 sigSz = 0;
  33683. const char * param = "XMSS-SHA2_10_256";
  33684. int j = 0;
  33685. int ret2 = -1;
  33686. int ret = -1;
  33687. WOLFSSL_ENTER("xmss_test_verify_only");
  33688. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33689. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33690. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  33691. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33692. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  33693. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33694. if (pkSz != XMSS_SHA256_PUBLEN) {
  33695. return WC_TEST_RET_ENC_EC(pkSz);
  33696. }
  33697. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  33698. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33699. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  33700. fprintf(stderr, "param: %s\n", param);
  33701. fprintf(stderr, "pkSz: %d\n", pkSz);
  33702. fprintf(stderr, "sigSz: %d\n", sigSz);
  33703. #endif
  33704. if (sigSz != sizeof(xmss_sig)) {
  33705. return WC_TEST_RET_ENC_EC(sigSz);
  33706. }
  33707. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  33708. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33709. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33710. (byte *) xmss_msg, sizeof(xmss_msg));
  33711. if (ret != 0) {
  33712. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  33713. return WC_TEST_RET_ENC_EC(ret);
  33714. }
  33715. /* Flip bits in message. This should fail. */
  33716. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  33717. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33718. (byte *) xmss_msg, sizeof(xmss_msg));
  33719. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  33720. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  33721. return WC_TEST_RET_ENC_EC(ret);
  33722. }
  33723. /* Flip it back. This should pass again. */
  33724. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  33725. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33726. (byte *) xmss_msg, sizeof(xmss_msg));
  33727. if (ret != 0) {
  33728. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  33729. return WC_TEST_RET_ENC_EC(ret);
  33730. }
  33731. /* Flip bits in a few places throughout the signature, stepping in multiple
  33732. * of hash size. These should all fail with -1. */
  33733. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  33734. xmss_sig[j] ^= 1;
  33735. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  33736. (byte *) xmss_msg, sizeof(xmss_msg));
  33737. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  33738. /* Verify passed when it should have failed. */
  33739. return WC_TEST_RET_ENC_I(j);
  33740. }
  33741. /* Flip this spot back. */
  33742. xmss_sig[j] ^= 1;
  33743. }
  33744. /* Cleanup everything. */
  33745. wc_XmssKey_Free(&verifyKey);
  33746. return ret;
  33747. }
  33748. #endif /* WOLFSSL_HAVE_XMSS && !WOLFSSL_SMALL_STACK &&
  33749. * WOLFSSL_XMSS_MIN_HEIGHT <= 10 */
  33750. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  33751. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  33752. {
  33753. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  33754. * BE USED FOR TESTING PURPOSES! Production applications should
  33755. * write only to non-volatile storage. */
  33756. XMEMCPY(context, priv, privSz);
  33757. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  33758. }
  33759. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  33760. {
  33761. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  33762. * BE USED FOR TESTING PURPOSES! */
  33763. XMEMCPY(priv, context, privSz);
  33764. return WC_LMS_RC_READ_TO_MEMORY;
  33765. }
  33766. /* LMS signature sizes are a function of their parameters. This
  33767. * test has a signature of 8688 bytes. */
  33768. #define WC_TEST_LMS_SIG_LEN (8688)
  33769. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  33770. {
  33771. int i = 0;
  33772. int j = 0;
  33773. int ret = -1;
  33774. int ret2 = -1;
  33775. int sigsLeft = 0;
  33776. LmsKey signingKey;
  33777. LmsKey verifyKey;
  33778. WC_RNG rng;
  33779. word32 sigSz = 0;
  33780. const char * msg = "LMS HSS post quantum signature test";
  33781. word32 msgSz = (word32) XSTRLEN(msg);
  33782. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  33783. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  33784. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33785. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  33786. DYNAMIC_TYPE_TMP_BUFFER);
  33787. if (sig == NULL) {
  33788. return WC_TEST_RET_ENC_ERRNO;
  33789. }
  33790. #else
  33791. byte sig[WC_TEST_LMS_SIG_LEN];
  33792. #endif
  33793. WOLFSSL_ENTER("lms_test");
  33794. XMEMSET(priv, 0, sizeof(priv));
  33795. XMEMSET(old_priv, 0, sizeof(old_priv));
  33796. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  33797. #ifndef HAVE_FIPS
  33798. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  33799. #else
  33800. ret = wc_InitRng(&rng);
  33801. #endif
  33802. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33803. /* This test:
  33804. * levels: 1
  33805. * height: 5
  33806. * winternitz: 1
  33807. *
  33808. * max sigs: 2 ** (1 * 5) = 32
  33809. * signature length: 8688
  33810. */
  33811. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  33812. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33813. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  33814. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33815. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  33816. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33817. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  33818. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33819. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  33820. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33821. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  33822. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33823. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  33824. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33825. XMEMCPY(old_priv, priv, sizeof(priv));
  33826. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  33827. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33828. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  33829. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  33830. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  33831. printf("error: got %u, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  33832. return WC_TEST_RET_ENC_EC(sigSz);
  33833. }
  33834. /* 2 ** 5 should be the max number of signatures */
  33835. for (i = 0; i < 32; ++i) {
  33836. /* We should have remaining signstures. */
  33837. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  33838. if (sigsLeft == 0) {
  33839. return WC_TEST_RET_ENC_EC(sigsLeft);
  33840. }
  33841. /* Sign with key. The private key will be updated on every signature. */
  33842. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  33843. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33844. /* The updated private key should not match the old one. */
  33845. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  33846. printf("error: current priv key should not match old: %d\n", i);
  33847. return WC_TEST_RET_ENC_I(i);
  33848. }
  33849. XMEMCPY(old_priv, priv, sizeof(priv));
  33850. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  33851. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  33852. /* Flip bits in a few places throughout the signature, stepping in multiple
  33853. * of hash size. These should all fail with -1. */
  33854. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  33855. sig[j] ^= 1;
  33856. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  33857. msgSz);
  33858. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  33859. /* Verify passed when it should have failed. */
  33860. return WC_TEST_RET_ENC_I(j);
  33861. }
  33862. /* Flip this spot back. */
  33863. sig[j] ^= 1;
  33864. }
  33865. }
  33866. /* This should be the last signature. */
  33867. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  33868. if (sigsLeft != 0) {
  33869. return WC_TEST_RET_ENC_EC(sigsLeft);
  33870. }
  33871. wc_LmsKey_Free(&signingKey);
  33872. wc_LmsKey_Free(&verifyKey);
  33873. wc_FreeRng(&rng);
  33874. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33875. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33876. #endif
  33877. return ret;
  33878. }
  33879. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  33880. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK)
  33881. #if defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)
  33882. /* A simple LMS verify only test.
  33883. *
  33884. * Note: LMS signature sizes are a function of their parameters. This
  33885. * test has a signature of 1456 bytes:
  33886. * levels: 1
  33887. * height: 10
  33888. * winternitz: 8
  33889. * max sigs: 2 ** (1 * 10) = 1024
  33890. * signature length: 1456
  33891. * */
  33892. /* "wolfSSL LMS example message!" without null terminator. */
  33893. static byte lms_msg[28] =
  33894. {
  33895. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  33896. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  33897. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  33898. 0x61,0x67,0x65,0x21
  33899. };
  33900. static const byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  33901. {
  33902. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  33903. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  33904. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  33905. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  33906. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  33907. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  33908. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  33909. 0xC5,0xB4,0x8F,0x23
  33910. };
  33911. #define LMS_L1H10W8_SIGLEN (1456)
  33912. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  33913. {
  33914. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  33915. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  33916. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  33917. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  33918. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  33919. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  33920. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  33921. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  33922. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  33923. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  33924. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  33925. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  33926. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  33927. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  33928. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  33929. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  33930. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  33931. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  33932. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  33933. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  33934. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  33935. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  33936. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  33937. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  33938. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  33939. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  33940. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  33941. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  33942. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  33943. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  33944. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  33945. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  33946. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  33947. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  33948. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  33949. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  33950. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  33951. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  33952. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  33953. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  33954. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  33955. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  33956. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  33957. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  33958. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  33959. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  33960. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  33961. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  33962. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  33963. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  33964. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  33965. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  33966. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  33967. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  33968. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  33969. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  33970. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  33971. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  33972. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  33973. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  33974. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  33975. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  33976. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  33977. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  33978. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  33979. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  33980. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  33981. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  33982. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  33983. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  33984. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  33985. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  33986. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  33987. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  33988. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  33989. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  33990. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  33991. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  33992. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  33993. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  33994. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  33995. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  33996. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  33997. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  33998. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  33999. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  34000. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  34001. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  34002. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  34003. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  34004. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  34005. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  34006. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  34007. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  34008. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  34009. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  34010. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  34011. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  34012. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  34013. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  34014. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  34015. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  34016. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  34017. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  34018. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  34019. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  34020. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  34021. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  34022. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  34023. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  34024. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  34025. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  34026. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  34027. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  34028. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  34029. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  34030. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  34031. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  34032. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  34033. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  34034. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  34035. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  34036. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  34037. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  34038. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  34039. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  34040. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  34041. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  34042. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  34043. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  34044. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  34045. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  34046. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  34047. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  34048. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  34049. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  34050. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  34051. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  34052. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  34053. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  34054. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  34055. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  34056. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  34057. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  34058. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  34059. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  34060. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  34061. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  34062. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  34063. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  34064. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  34065. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  34066. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  34067. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  34068. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  34069. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  34070. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  34071. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  34072. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  34073. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  34074. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  34075. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  34076. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  34077. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  34078. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  34079. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  34080. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  34081. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  34082. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  34083. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  34084. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  34085. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  34086. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  34087. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  34088. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  34089. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  34090. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  34091. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  34092. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  34093. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  34094. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  34095. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  34096. };
  34097. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  34098. {
  34099. int ret = -1;
  34100. int ret2 = -1;
  34101. int j = 0;
  34102. LmsKey verifyKey;
  34103. word32 sigSz = 0;
  34104. word32 msgSz = sizeof(lms_msg);
  34105. word32 pubLen = 0;
  34106. int levels = 0;
  34107. int height = 0;
  34108. int winternitz = 0;
  34109. WOLFSSL_ENTER("lms_test_verify_only");
  34110. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  34111. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34112. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  34113. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34114. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  34115. HSS_MAX_PUBLIC_KEY_LEN);
  34116. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34117. /* Verify parameters, pub length, and sig length are correct. */
  34118. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  34119. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34120. if (levels != 1 || height != 10 || winternitz != 8) {
  34121. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  34122. winternitz);
  34123. return -1;
  34124. }
  34125. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubLen);
  34126. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34127. if (pubLen != HSS_MAX_PUBLIC_KEY_LEN) {
  34128. printf("error: got %u, expected %d\n", pubLen, HSS_MAX_PUBLIC_KEY_LEN);
  34129. return WC_TEST_RET_ENC_EC(pubLen);
  34130. }
  34131. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  34132. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  34133. if (sigSz != LMS_L1H10W8_SIGLEN) {
  34134. printf("error: got %u, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  34135. return WC_TEST_RET_ENC_EC(sigSz);
  34136. }
  34137. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  34138. (byte *) lms_msg, msgSz);
  34139. if (ret != 0) {
  34140. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  34141. return WC_TEST_RET_ENC_EC(ret);
  34142. }
  34143. /* Flip bits in message. This should fail. */
  34144. lms_msg[msgSz / 2] ^= 1;
  34145. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  34146. (byte *) lms_msg, msgSz);
  34147. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  34148. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  34149. return WC_TEST_RET_ENC_EC(ret);
  34150. }
  34151. /* Flip it back. This should pass again. */
  34152. lms_msg[msgSz / 2] ^= 1;
  34153. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  34154. (byte *) lms_msg, msgSz);
  34155. if (ret != 0) {
  34156. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  34157. return WC_TEST_RET_ENC_EC(ret);
  34158. }
  34159. /* Flip bits in a few places throughout the signature, stepping in multiple
  34160. * of hash size. These should all fail with -1. */
  34161. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  34162. lms_L1H10W8_sig[j] ^= 1;
  34163. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  34164. LMS_L1H10W8_SIGLEN,
  34165. (byte *) lms_msg, msgSz);
  34166. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  34167. /* Verify passed when it should have failed. */
  34168. return WC_TEST_RET_ENC_I(j);
  34169. }
  34170. /* Flip this spot back. */
  34171. lms_L1H10W8_sig[j] ^= 1;
  34172. }
  34173. wc_LmsKey_Free(&verifyKey);
  34174. return ret;
  34175. }
  34176. #endif
  34177. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK) */
  34178. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  34179. * see print_fiducials() below.
  34180. */
  34181. #ifdef WOLFCRYPT_HAVE_ECCSI
  34182. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  34183. ecc_point* pvt)
  34184. {
  34185. wc_test_ret_t ret;
  34186. byte id[1] = { 0x00 };
  34187. int valid;
  34188. word32 sz;
  34189. byte data[256];
  34190. byte hash[WC_MAX_DIGEST_SIZE];
  34191. byte hashSz;
  34192. byte sig[257];
  34193. word32 sigSz;
  34194. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  34195. if (ret != BAD_FUNC_ARG)
  34196. return WC_TEST_RET_ENC_EC(ret);
  34197. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  34198. if (ret != BAD_FUNC_ARG)
  34199. return WC_TEST_RET_ENC_EC(ret);
  34200. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  34201. if (ret != BAD_FUNC_ARG)
  34202. return WC_TEST_RET_ENC_EC(ret);
  34203. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  34204. if (ret != BAD_FUNC_ARG)
  34205. return WC_TEST_RET_ENC_EC(ret);
  34206. wc_FreeEccsiKey(NULL);
  34207. /* Create a valid key. */
  34208. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  34209. if (ret != 0)
  34210. return WC_TEST_RET_ENC_EC(ret);
  34211. ret = wc_MakeEccsiKey(NULL, NULL);
  34212. if (ret != BAD_FUNC_ARG)
  34213. return WC_TEST_RET_ENC_EC(ret);
  34214. ret = wc_MakeEccsiKey(key, NULL);
  34215. if (ret != BAD_FUNC_ARG)
  34216. return WC_TEST_RET_ENC_EC(ret);
  34217. ret = wc_MakeEccsiKey(NULL, rng);
  34218. if (ret != BAD_FUNC_ARG)
  34219. return WC_TEST_RET_ENC_EC(ret);
  34220. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  34221. NULL);
  34222. if (ret != BAD_FUNC_ARG)
  34223. return WC_TEST_RET_ENC_EC(ret);
  34224. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  34225. if (ret != BAD_FUNC_ARG)
  34226. return WC_TEST_RET_ENC_EC(ret);
  34227. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  34228. if (ret != BAD_FUNC_ARG)
  34229. return WC_TEST_RET_ENC_EC(ret);
  34230. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  34231. if (ret != BAD_FUNC_ARG)
  34232. return WC_TEST_RET_ENC_EC(ret);
  34233. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  34234. if (ret != BAD_FUNC_ARG)
  34235. return WC_TEST_RET_ENC_EC(ret);
  34236. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  34237. if (ret != BAD_FUNC_ARG)
  34238. return WC_TEST_RET_ENC_EC(ret);
  34239. /* No key set */
  34240. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  34241. if (ret != BAD_STATE_E)
  34242. return WC_TEST_RET_ENC_EC(ret);
  34243. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  34244. NULL);
  34245. if (ret != BAD_FUNC_ARG)
  34246. return WC_TEST_RET_ENC_EC(ret);
  34247. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  34248. NULL);
  34249. if (ret != BAD_FUNC_ARG)
  34250. return WC_TEST_RET_ENC_EC(ret);
  34251. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  34252. &valid);
  34253. if (ret != BAD_FUNC_ARG)
  34254. return WC_TEST_RET_ENC_EC(ret);
  34255. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  34256. &valid);
  34257. if (ret != BAD_FUNC_ARG)
  34258. return WC_TEST_RET_ENC_EC(ret);
  34259. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  34260. &valid);
  34261. if (ret != BAD_FUNC_ARG)
  34262. return WC_TEST_RET_ENC_EC(ret);
  34263. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  34264. &valid);
  34265. if (ret != BAD_FUNC_ARG)
  34266. return WC_TEST_RET_ENC_EC(ret);
  34267. /* No key set */
  34268. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  34269. &valid);
  34270. if (ret != BAD_STATE_E)
  34271. return WC_TEST_RET_ENC_EC(ret);
  34272. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  34273. if (ret != BAD_FUNC_ARG)
  34274. return WC_TEST_RET_ENC_EC(ret);
  34275. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  34276. if (ret != BAD_FUNC_ARG)
  34277. return WC_TEST_RET_ENC_EC(ret);
  34278. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  34279. if (ret != BAD_FUNC_ARG)
  34280. return WC_TEST_RET_ENC_EC(ret);
  34281. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  34282. if (ret != BAD_FUNC_ARG)
  34283. return WC_TEST_RET_ENC_EC(ret);
  34284. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  34285. if (ret != BAD_FUNC_ARG)
  34286. return WC_TEST_RET_ENC_EC(ret);
  34287. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  34288. if (ret != BAD_FUNC_ARG)
  34289. return WC_TEST_RET_ENC_EC(ret);
  34290. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  34291. if (ret != BAD_FUNC_ARG)
  34292. return WC_TEST_RET_ENC_EC(ret);
  34293. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  34294. if (ret != BAD_FUNC_ARG)
  34295. return WC_TEST_RET_ENC_EC(ret);
  34296. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  34297. if (ret != BAD_FUNC_ARG)
  34298. return WC_TEST_RET_ENC_EC(ret);
  34299. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  34300. if (ret != BAD_FUNC_ARG)
  34301. return WC_TEST_RET_ENC_EC(ret);
  34302. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  34303. if (ret != BAD_FUNC_ARG)
  34304. return WC_TEST_RET_ENC_EC(ret);
  34305. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  34306. if (ret != BAD_FUNC_ARG)
  34307. return WC_TEST_RET_ENC_EC(ret);
  34308. /* No key created so no curve information. */
  34309. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  34310. if (ret != LENGTH_ONLY_E)
  34311. return WC_TEST_RET_ENC_EC(ret);
  34312. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  34313. if (ret != BAD_FUNC_ARG)
  34314. return WC_TEST_RET_ENC_EC(ret);
  34315. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  34316. if (ret != BAD_FUNC_ARG)
  34317. return WC_TEST_RET_ENC_EC(ret);
  34318. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  34319. if (ret != BAD_FUNC_ARG)
  34320. return WC_TEST_RET_ENC_EC(ret);
  34321. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  34322. if (ret != BAD_FUNC_ARG)
  34323. return WC_TEST_RET_ENC_EC(ret);
  34324. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  34325. if (ret != BAD_FUNC_ARG)
  34326. return WC_TEST_RET_ENC_EC(ret);
  34327. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  34328. if (ret != BAD_FUNC_ARG)
  34329. return WC_TEST_RET_ENC_EC(ret);
  34330. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  34331. if (ret != BAD_FUNC_ARG)
  34332. return WC_TEST_RET_ENC_EC(ret);
  34333. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  34334. if (ret != BAD_FUNC_ARG)
  34335. return WC_TEST_RET_ENC_EC(ret);
  34336. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  34337. if (ret != BAD_FUNC_ARG)
  34338. return WC_TEST_RET_ENC_EC(ret);
  34339. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  34340. if (ret != BAD_FUNC_ARG)
  34341. return WC_TEST_RET_ENC_EC(ret);
  34342. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  34343. if (ret != BAD_FUNC_ARG)
  34344. return WC_TEST_RET_ENC_EC(ret);
  34345. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  34346. if (ret != BAD_FUNC_ARG)
  34347. return WC_TEST_RET_ENC_EC(ret);
  34348. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  34349. if (ret != BAD_FUNC_ARG)
  34350. return WC_TEST_RET_ENC_EC(ret);
  34351. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  34352. if (ret != BAD_FUNC_ARG)
  34353. return WC_TEST_RET_ENC_EC(ret);
  34354. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  34355. if (ret != BAD_FUNC_ARG)
  34356. return WC_TEST_RET_ENC_EC(ret);
  34357. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  34358. if (ret != BAD_FUNC_ARG)
  34359. return WC_TEST_RET_ENC_EC(ret);
  34360. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  34361. if (ret != BAD_FUNC_ARG)
  34362. return WC_TEST_RET_ENC_EC(ret);
  34363. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  34364. if (ret != BAD_FUNC_ARG)
  34365. return WC_TEST_RET_ENC_EC(ret);
  34366. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  34367. if (ret != BAD_FUNC_ARG)
  34368. return WC_TEST_RET_ENC_EC(ret);
  34369. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  34370. if (ret != BAD_FUNC_ARG)
  34371. return WC_TEST_RET_ENC_EC(ret);
  34372. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  34373. if (ret != BAD_FUNC_ARG)
  34374. return WC_TEST_RET_ENC_EC(ret);
  34375. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  34376. if (ret != BAD_FUNC_ARG)
  34377. return WC_TEST_RET_ENC_EC(ret);
  34378. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  34379. if (ret != BAD_FUNC_ARG)
  34380. return WC_TEST_RET_ENC_EC(ret);
  34381. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  34382. if (ret != BAD_FUNC_ARG)
  34383. return WC_TEST_RET_ENC_EC(ret);
  34384. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  34385. if (ret != BAD_FUNC_ARG)
  34386. return WC_TEST_RET_ENC_EC(ret);
  34387. ret = wc_ExportEccsiKey(NULL, data, NULL);
  34388. if (ret != BAD_FUNC_ARG)
  34389. return WC_TEST_RET_ENC_EC(ret);
  34390. ret = wc_ExportEccsiKey(key, data, NULL);
  34391. if (ret != BAD_FUNC_ARG)
  34392. return WC_TEST_RET_ENC_EC(ret);
  34393. ret = wc_ExportEccsiKey(NULL, data, &sz);
  34394. if (ret != BAD_FUNC_ARG)
  34395. return WC_TEST_RET_ENC_EC(ret);
  34396. /* No key to export */
  34397. ret = wc_ExportEccsiKey(key, NULL, &sz);
  34398. if (ret != BAD_STATE_E)
  34399. return WC_TEST_RET_ENC_EC(ret);
  34400. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  34401. if (ret != BAD_FUNC_ARG)
  34402. return WC_TEST_RET_ENC_EC(ret);
  34403. ret = wc_ImportEccsiKey(key, NULL, 0);
  34404. if (ret != BAD_FUNC_ARG)
  34405. return WC_TEST_RET_ENC_EC(ret);
  34406. ret = wc_ImportEccsiKey(NULL, data, 0);
  34407. if (ret != BAD_FUNC_ARG)
  34408. return WC_TEST_RET_ENC_EC(ret);
  34409. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  34410. if (ret != BAD_FUNC_ARG)
  34411. return WC_TEST_RET_ENC_EC(ret);
  34412. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  34413. if (ret != BAD_FUNC_ARG)
  34414. return WC_TEST_RET_ENC_EC(ret);
  34415. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  34416. if (ret != BAD_FUNC_ARG)
  34417. return WC_TEST_RET_ENC_EC(ret);
  34418. /* No key to export */
  34419. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  34420. if (ret != BAD_STATE_E)
  34421. return WC_TEST_RET_ENC_EC(ret);
  34422. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  34423. if (ret != BAD_FUNC_ARG)
  34424. return WC_TEST_RET_ENC_EC(ret);
  34425. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  34426. if (ret != BAD_FUNC_ARG)
  34427. return WC_TEST_RET_ENC_EC(ret);
  34428. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  34429. if (ret != BAD_FUNC_ARG)
  34430. return WC_TEST_RET_ENC_EC(ret);
  34431. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  34432. if (ret != BAD_FUNC_ARG)
  34433. return WC_TEST_RET_ENC_EC(ret);
  34434. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  34435. if (ret != BAD_FUNC_ARG)
  34436. return WC_TEST_RET_ENC_EC(ret);
  34437. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  34438. if (ret != BAD_FUNC_ARG)
  34439. return WC_TEST_RET_ENC_EC(ret);
  34440. /* No key to export */
  34441. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  34442. if (ret != BAD_STATE_E)
  34443. return WC_TEST_RET_ENC_EC(ret);
  34444. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  34445. if (ret != BAD_FUNC_ARG)
  34446. return WC_TEST_RET_ENC_EC(ret);
  34447. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  34448. if (ret != BAD_FUNC_ARG)
  34449. return WC_TEST_RET_ENC_EC(ret);
  34450. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  34451. if (ret != BAD_FUNC_ARG)
  34452. return WC_TEST_RET_ENC_EC(ret);
  34453. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  34454. if (ret != BAD_FUNC_ARG)
  34455. return WC_TEST_RET_ENC_EC(ret);
  34456. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  34457. if (ret != BAD_FUNC_ARG)
  34458. return WC_TEST_RET_ENC_EC(ret);
  34459. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  34460. if (ret != BAD_FUNC_ARG)
  34461. return WC_TEST_RET_ENC_EC(ret);
  34462. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  34463. if (ret != BAD_FUNC_ARG)
  34464. return WC_TEST_RET_ENC_EC(ret);
  34465. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  34466. &hashSz);
  34467. if (ret != BAD_FUNC_ARG)
  34468. return WC_TEST_RET_ENC_EC(ret);
  34469. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  34470. if (ret != BAD_FUNC_ARG)
  34471. return WC_TEST_RET_ENC_EC(ret);
  34472. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  34473. if (ret != BAD_STATE_E)
  34474. return WC_TEST_RET_ENC_EC(ret);
  34475. ret = wc_SetEccsiHash(NULL, NULL, 1);
  34476. if (ret != BAD_FUNC_ARG)
  34477. return WC_TEST_RET_ENC_EC(ret);
  34478. ret = wc_SetEccsiHash(key, NULL, 1);
  34479. if (ret != BAD_FUNC_ARG)
  34480. return WC_TEST_RET_ENC_EC(ret);
  34481. ret = wc_SetEccsiHash(NULL, hash, 1);
  34482. if (ret != BAD_FUNC_ARG)
  34483. return WC_TEST_RET_ENC_EC(ret);
  34484. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  34485. if (ret != BAD_FUNC_ARG)
  34486. return WC_TEST_RET_ENC_EC(ret);
  34487. ret = wc_SetEccsiPair(key, NULL, NULL);
  34488. if (ret != BAD_FUNC_ARG)
  34489. return WC_TEST_RET_ENC_EC(ret);
  34490. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  34491. if (ret != BAD_FUNC_ARG)
  34492. return WC_TEST_RET_ENC_EC(ret);
  34493. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  34494. if (ret != BAD_FUNC_ARG)
  34495. return WC_TEST_RET_ENC_EC(ret);
  34496. ret = wc_SetEccsiPair(key, ssk, NULL);
  34497. if (ret != BAD_FUNC_ARG)
  34498. return WC_TEST_RET_ENC_EC(ret);
  34499. ret = wc_SetEccsiPair(key, NULL, pvt);
  34500. if (ret != BAD_FUNC_ARG)
  34501. return WC_TEST_RET_ENC_EC(ret);
  34502. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  34503. if (ret != BAD_FUNC_ARG)
  34504. return WC_TEST_RET_ENC_EC(ret);
  34505. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  34506. if (ret != BAD_FUNC_ARG)
  34507. return WC_TEST_RET_ENC_EC(ret);
  34508. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  34509. if (ret != BAD_FUNC_ARG)
  34510. return WC_TEST_RET_ENC_EC(ret);
  34511. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  34512. &sigSz);
  34513. if (ret != BAD_FUNC_ARG)
  34514. return WC_TEST_RET_ENC_EC(ret);
  34515. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  34516. &sigSz);
  34517. if (ret != BAD_FUNC_ARG)
  34518. return WC_TEST_RET_ENC_EC(ret);
  34519. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  34520. &sigSz);
  34521. if (ret != BAD_FUNC_ARG)
  34522. return WC_TEST_RET_ENC_EC(ret);
  34523. /* Key not set. */
  34524. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  34525. &sigSz);
  34526. if (ret != BAD_STATE_E)
  34527. return WC_TEST_RET_ENC_EC(ret);
  34528. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  34529. if (ret != BAD_FUNC_ARG)
  34530. return WC_TEST_RET_ENC_EC(ret);
  34531. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  34532. if (ret != BAD_FUNC_ARG)
  34533. return WC_TEST_RET_ENC_EC(ret);
  34534. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  34535. if (ret != BAD_FUNC_ARG)
  34536. return WC_TEST_RET_ENC_EC(ret);
  34537. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  34538. if (ret != BAD_FUNC_ARG)
  34539. return WC_TEST_RET_ENC_EC(ret);
  34540. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  34541. &valid);
  34542. if (ret != BAD_FUNC_ARG)
  34543. return WC_TEST_RET_ENC_EC(ret);
  34544. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  34545. if (ret != BAD_FUNC_ARG)
  34546. return WC_TEST_RET_ENC_EC(ret);
  34547. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  34548. &valid);
  34549. if (ret != BAD_FUNC_ARG)
  34550. return WC_TEST_RET_ENC_EC(ret);
  34551. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  34552. &valid);
  34553. if (ret != BAD_FUNC_ARG)
  34554. return WC_TEST_RET_ENC_EC(ret);
  34555. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  34556. &valid);
  34557. if (ret != BAD_FUNC_ARG)
  34558. return WC_TEST_RET_ENC_EC(ret);
  34559. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  34560. &valid);
  34561. if (ret != BAD_STATE_E)
  34562. return WC_TEST_RET_ENC_EC(ret);
  34563. ret = wc_SetEccsiPair(key, ssk, pvt);
  34564. if (ret != 0)
  34565. return WC_TEST_RET_ENC_EC(ret);
  34566. /* Identity hash not set. */
  34567. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  34568. &sigSz);
  34569. if (ret != BAD_STATE_E)
  34570. return WC_TEST_RET_ENC_EC(ret);
  34571. wc_FreeEccsiKey(key);
  34572. return 0;
  34573. }
  34574. /* RFC 6507: Appendix A */
  34575. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  34576. {
  34577. wc_test_ret_t ret;
  34578. int verified;
  34579. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  34580. word32 msgSz = sizeof(msg);
  34581. byte hash[WC_SHA256_DIGEST_SIZE];
  34582. byte hashSz = WC_SHA256_DIGEST_SIZE;
  34583. static const byte id[] = {
  34584. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  34585. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  34586. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  34587. 0x33, 0x00
  34588. };
  34589. word32 idSz = sizeof(id);
  34590. static const byte sig[] = {
  34591. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  34592. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  34593. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  34594. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  34595. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  34596. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  34597. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  34598. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  34599. 0x04,
  34600. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  34601. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  34602. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  34603. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  34604. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  34605. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  34606. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  34607. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  34608. };
  34609. word32 sigSz = sizeof(sig);
  34610. static const byte pubData[] = {
  34611. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  34612. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  34613. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  34614. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  34615. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  34616. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  34617. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  34618. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  34619. };
  34620. static const byte expHash[] = {
  34621. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  34622. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  34623. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  34624. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  34625. };
  34626. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  34627. if (ret != 0)
  34628. return WC_TEST_RET_ENC_EC(ret);
  34629. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  34630. if (ret != 0)
  34631. return WC_TEST_RET_ENC_EC(ret);
  34632. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  34633. &hashSz);
  34634. if (ret != 0)
  34635. return WC_TEST_RET_ENC_EC(ret);
  34636. if (hashSz != sizeof(expHash))
  34637. return WC_TEST_RET_ENC_NC;
  34638. if (XMEMCMP(hash, expHash, hashSz) != 0)
  34639. return WC_TEST_RET_ENC_NC;
  34640. ret = wc_SetEccsiHash(key, hash, hashSz);
  34641. if (ret != 0)
  34642. return WC_TEST_RET_ENC_EC(ret);
  34643. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34644. &verified);
  34645. if (ret != 0)
  34646. return WC_TEST_RET_ENC_EC(ret);
  34647. if (!verified)
  34648. return WC_TEST_RET_ENC_NC;
  34649. return 0;
  34650. }
  34651. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  34652. {
  34653. wc_test_ret_t ret;
  34654. byte data[32 * 3];
  34655. word32 sz;
  34656. ecc_point* decPvt = NULL;
  34657. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34658. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  34659. DYNAMIC_TYPE_TMP_BUFFER);
  34660. if (decSsk == NULL)
  34661. return WC_TEST_RET_ENC_ERRNO;
  34662. #else
  34663. mp_int decSsk[1];
  34664. #endif
  34665. ret = mp_init(decSsk);
  34666. if (ret != 0)
  34667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34668. decPvt = wc_ecc_new_point();
  34669. if (decPvt == NULL)
  34670. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34671. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  34672. if (ret != LENGTH_ONLY_E)
  34673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34674. if (sz != 32 * 3)
  34675. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34676. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  34677. if (ret != 0)
  34678. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34679. if (sz != 32* 3)
  34680. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34681. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  34682. if (ret != 0)
  34683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34684. ret = mp_cmp(ssk, decSsk);
  34685. if (ret != MP_EQ)
  34686. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34687. ret = wc_ecc_cmp_point(pvt, decPvt);
  34688. if (ret != MP_EQ)
  34689. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34690. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  34691. if (ret != LENGTH_ONLY_E)
  34692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34693. if (sz != 32)
  34694. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34695. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  34696. if (ret != 0)
  34697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34698. if (sz != 32)
  34699. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34700. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  34701. if (ret != 0)
  34702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34703. ret = mp_cmp(ssk, decSsk);
  34704. if (ret != MP_EQ)
  34705. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34706. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  34707. if (ret != LENGTH_ONLY_E)
  34708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34709. if (sz != 32 * 2)
  34710. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34711. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  34712. if (ret != 0)
  34713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34714. if (sz != 32 * 2)
  34715. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34716. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  34717. if (ret != 0)
  34718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34719. ret = wc_ecc_cmp_point(pvt, decPvt);
  34720. if (ret != MP_EQ)
  34721. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34722. sz = sizeof(data);
  34723. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  34724. if (ret != 0)
  34725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34726. if (sz != 32 * 2 + 1)
  34727. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34728. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  34729. if (ret != 0)
  34730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34731. ret = wc_ecc_cmp_point(pvt, decPvt);
  34732. if (ret != MP_EQ)
  34733. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34734. wc_ecc_del_point(decPvt);
  34735. out:
  34736. mp_free(decSsk);
  34737. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34738. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34739. #endif
  34740. return ret;
  34741. }
  34742. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  34743. {
  34744. wc_test_ret_t ret;
  34745. byte data[32 * 3];
  34746. byte out[32 * 3];
  34747. word32 sz;
  34748. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  34749. if (ret != LENGTH_ONLY_E)
  34750. return WC_TEST_RET_ENC_EC(ret);
  34751. if (sz != 32 * 3)
  34752. return WC_TEST_RET_ENC_NC;
  34753. ret = wc_ExportEccsiKey(priv, data, &sz);
  34754. if (ret != 0)
  34755. return WC_TEST_RET_ENC_EC(ret);
  34756. ret = wc_ImportEccsiKey(priv, data, sz);
  34757. if (ret != 0)
  34758. return WC_TEST_RET_ENC_EC(ret);
  34759. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  34760. if (ret != LENGTH_ONLY_E)
  34761. return WC_TEST_RET_ENC_EC(ret);
  34762. if (sz != 32 * 3)
  34763. return WC_TEST_RET_ENC_NC;
  34764. ret = wc_ExportEccsiKey(priv, out, &sz);
  34765. if (ret != 0)
  34766. return WC_TEST_RET_ENC_EC(ret);
  34767. if (sz != 32 * 3)
  34768. return WC_TEST_RET_ENC_NC;
  34769. if (XMEMCMP(data, out, sz) != 0)
  34770. return WC_TEST_RET_ENC_NC;
  34771. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  34772. if (ret != LENGTH_ONLY_E)
  34773. return WC_TEST_RET_ENC_EC(ret);
  34774. if (sz != 32)
  34775. return WC_TEST_RET_ENC_NC;
  34776. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  34777. if (ret != 0)
  34778. return WC_TEST_RET_ENC_EC(ret);
  34779. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  34780. if (ret != 0)
  34781. return WC_TEST_RET_ENC_EC(ret);
  34782. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  34783. if (ret != LENGTH_ONLY_E)
  34784. return WC_TEST_RET_ENC_EC(ret);
  34785. if (sz != 32)
  34786. return WC_TEST_RET_ENC_NC;
  34787. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  34788. if (ret != 0)
  34789. return WC_TEST_RET_ENC_EC(ret);
  34790. if (sz != 32)
  34791. return WC_TEST_RET_ENC_NC;
  34792. if (XMEMCMP(data, out, sz) != 0)
  34793. return WC_TEST_RET_ENC_NC;
  34794. return 0;
  34795. }
  34796. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  34797. {
  34798. wc_test_ret_t ret;
  34799. byte data[32 * 2 + 1];
  34800. byte pubData[32 * 2 + 1];
  34801. word32 sz;
  34802. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  34803. if (ret != LENGTH_ONLY_E)
  34804. return WC_TEST_RET_ENC_EC(ret);
  34805. if (sz != 32 * 2)
  34806. return WC_TEST_RET_ENC_NC;
  34807. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  34808. if (ret != 0)
  34809. return WC_TEST_RET_ENC_EC(ret);
  34810. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  34811. if (ret != 0)
  34812. return WC_TEST_RET_ENC_EC(ret);
  34813. sz = sizeof(pubData);
  34814. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  34815. if (ret != 0)
  34816. return WC_TEST_RET_ENC_EC(ret);
  34817. if (sz != 32 * 2)
  34818. return WC_TEST_RET_ENC_NC;
  34819. if (XMEMCMP(data, pubData, sz) != 0)
  34820. return WC_TEST_RET_ENC_NC;
  34821. sz = sizeof(pubData);
  34822. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  34823. if (ret != 0)
  34824. return WC_TEST_RET_ENC_EC(ret);
  34825. if (sz != 32 * 2 + 1)
  34826. return WC_TEST_RET_ENC_NC;
  34827. if (pubData[0] != 0x04)
  34828. return WC_TEST_RET_ENC_NC;
  34829. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  34830. return WC_TEST_RET_ENC_NC;
  34831. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  34832. if (ret != 0)
  34833. return WC_TEST_RET_ENC_EC(ret);
  34834. return 0;
  34835. }
  34836. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  34837. mp_int* ssk, ecc_point* pvt)
  34838. {
  34839. wc_test_ret_t ret;
  34840. char mail[] = "test@wolfssl.com";
  34841. byte* id = (byte*)mail;
  34842. word32 idSz = (word32) XSTRLEN(mail);
  34843. int valid;
  34844. ret = wc_MakeEccsiKey(priv, rng);
  34845. if (ret != 0)
  34846. return WC_TEST_RET_ENC_EC(ret);
  34847. ret = eccsi_imp_exp_key_test(priv);
  34848. if (ret < 0)
  34849. return ret;
  34850. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  34851. if (ret < 0)
  34852. return ret;
  34853. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  34854. if (ret != 0)
  34855. return WC_TEST_RET_ENC_EC(ret);
  34856. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  34857. &valid);
  34858. if (ret != 0)
  34859. return WC_TEST_RET_ENC_EC(ret);
  34860. if (!valid)
  34861. return WC_TEST_RET_ENC_NC;
  34862. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  34863. if (ret != 0)
  34864. return ret;
  34865. return 0;
  34866. }
  34867. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  34868. mp_int* ssk, ecc_point* pvt)
  34869. {
  34870. wc_test_ret_t ret;
  34871. byte hashPriv[WC_MAX_DIGEST_SIZE];
  34872. byte hashPub[WC_MAX_DIGEST_SIZE];
  34873. byte hashSz;
  34874. byte sig[144];
  34875. word32 sigSz;
  34876. int verified, valid;
  34877. char mail[] = "test@wolfssl.com";
  34878. byte* id = (byte*)mail;
  34879. word32 idSz = (word32) XSTRLEN(mail);
  34880. byte msg[] = { 0x00 };
  34881. word32 msgSz = sizeof(msg);
  34882. #ifdef WOLFSSL_SHA384
  34883. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  34884. &hashSz);
  34885. if (ret != BAD_FUNC_ARG)
  34886. return WC_TEST_RET_ENC_EC(ret);
  34887. #endif
  34888. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  34889. &hashSz);
  34890. if (ret != 0)
  34891. return WC_TEST_RET_ENC_EC(ret);
  34892. if (hashSz != 32)
  34893. return WC_TEST_RET_ENC_NC;
  34894. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  34895. &hashSz);
  34896. if (ret != 0)
  34897. return WC_TEST_RET_ENC_EC(ret);
  34898. if (hashSz != 32)
  34899. return WC_TEST_RET_ENC_NC;
  34900. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  34901. return WC_TEST_RET_ENC_NC;
  34902. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  34903. if (ret != 0)
  34904. return WC_TEST_RET_ENC_EC(ret);
  34905. ret = wc_SetEccsiPair(priv, ssk, pvt);
  34906. if (ret != 0)
  34907. return WC_TEST_RET_ENC_EC(ret);
  34908. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  34909. &sigSz);
  34910. if (ret != LENGTH_ONLY_E)
  34911. return WC_TEST_RET_ENC_EC(ret);
  34912. if (sigSz != 129)
  34913. return WC_TEST_RET_ENC_NC;
  34914. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  34915. &sigSz);
  34916. if (ret != 0)
  34917. return WC_TEST_RET_ENC_EC(ret);
  34918. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34919. if (ret != 0)
  34920. return WC_TEST_RET_ENC_EC(ret);
  34921. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34922. &verified);
  34923. if (ret != 0)
  34924. return WC_TEST_RET_ENC_EC(ret);
  34925. if (!verified)
  34926. return WC_TEST_RET_ENC_NC;
  34927. /* Check that changing HS results in verification failure. */
  34928. hashPub[0] ^= 0x80;
  34929. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34930. if (ret != 0)
  34931. return WC_TEST_RET_ENC_EC(ret);
  34932. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34933. &verified);
  34934. if (ret != 0)
  34935. return WC_TEST_RET_ENC_EC(ret);
  34936. if (verified)
  34937. return WC_TEST_RET_ENC_NC;
  34938. hashPub[0] ^= 0x80;
  34939. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  34940. if (ret != 0)
  34941. return WC_TEST_RET_ENC_EC(ret);
  34942. /* Check that changing msg results in verification failure. */
  34943. msg[0] ^= 0x80;
  34944. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34945. &verified);
  34946. if (ret != 0)
  34947. return WC_TEST_RET_ENC_EC(ret);
  34948. if (verified)
  34949. return WC_TEST_RET_ENC_NC;
  34950. msg[0] ^= 0x80;
  34951. /* Check that changing signature results in verification failure. */
  34952. sig[0] ^= 0x80;
  34953. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34954. &verified);
  34955. if (ret != 0)
  34956. return WC_TEST_RET_ENC_EC(ret);
  34957. if (verified)
  34958. return WC_TEST_RET_ENC_NC;
  34959. sig[0] ^= 0x80;
  34960. /* Check that key state hasn't been invalidated. */
  34961. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34962. &verified);
  34963. if (ret != 0)
  34964. return WC_TEST_RET_ENC_EC(ret);
  34965. if (!verified)
  34966. return WC_TEST_RET_ENC_NC;
  34967. /* Check that verifying with the private key works. */
  34968. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34969. &verified);
  34970. if (ret != 0)
  34971. return WC_TEST_RET_ENC_EC(ret);
  34972. if (!verified)
  34973. return WC_TEST_RET_ENC_NC;
  34974. /* Check that the KPAK is converted from montgomery form. */
  34975. ret = eccsi_imp_exp_key_test(priv);
  34976. if (ret != 0)
  34977. return ret;
  34978. /* Check that KPAK can converted to Montgomery form again. */
  34979. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34980. &verified);
  34981. if (ret != 0)
  34982. return WC_TEST_RET_ENC_EC(ret);
  34983. if (!verified)
  34984. return WC_TEST_RET_ENC_NC;
  34985. /* Check that the KPAK is converted from montgomery form. */
  34986. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  34987. &valid);
  34988. if (ret != 0)
  34989. return WC_TEST_RET_ENC_EC(ret);
  34990. if (!valid)
  34991. return WC_TEST_RET_ENC_NC;
  34992. /* Check that KPAK can converted to Montgomery form again. */
  34993. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  34994. &verified);
  34995. if (ret != 0)
  34996. return WC_TEST_RET_ENC_EC(ret);
  34997. if (!verified)
  34998. return WC_TEST_RET_ENC_NC;
  34999. /* Check that the KPAK is converted from montgomery form. */
  35000. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  35001. if (ret != 0)
  35002. return ret;
  35003. return 0;
  35004. }
  35005. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  35006. {
  35007. wc_test_ret_t ret = 0;
  35008. WC_RNG rng;
  35009. int rng_inited = 0;
  35010. EccsiKey* priv = NULL;
  35011. EccsiKey* pub = NULL;
  35012. mp_int* ssk = NULL;
  35013. ecc_point* pvt = NULL;
  35014. WOLFSSL_ENTER("eccsi_test");
  35015. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  35016. DYNAMIC_TYPE_TMP_BUFFER);
  35017. if (priv == NULL)
  35018. ret = WC_TEST_RET_ENC_NC;
  35019. else
  35020. XMEMSET(priv, 0, sizeof(*priv));
  35021. if (ret == 0) {
  35022. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  35023. DYNAMIC_TYPE_TMP_BUFFER);
  35024. if (pub == NULL)
  35025. ret = WC_TEST_RET_ENC_NC;
  35026. else
  35027. XMEMSET(pub, 0, sizeof(*pub));
  35028. }
  35029. if (ret == 0) {
  35030. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  35031. DYNAMIC_TYPE_TMP_BUFFER);
  35032. if (ssk == NULL)
  35033. ret = WC_TEST_RET_ENC_NC;
  35034. else
  35035. XMEMSET(ssk, 0, sizeof(*ssk));
  35036. }
  35037. if (ret == 0) {
  35038. #ifndef HAVE_FIPS
  35039. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35040. #else
  35041. ret = wc_InitRng(&rng);
  35042. #endif
  35043. if (ret != 0)
  35044. ret = WC_TEST_RET_ENC_EC(ret);
  35045. else
  35046. rng_inited = 1;
  35047. }
  35048. if (ret == 0) {
  35049. pvt = wc_ecc_new_point();
  35050. if (pvt == NULL)
  35051. ret = WC_TEST_RET_ENC_NC;
  35052. }
  35053. if (ret == 0) {
  35054. ret = mp_init(ssk);
  35055. if (ret != 0)
  35056. ret = WC_TEST_RET_ENC_EC(ret);
  35057. }
  35058. if (ret == 0) {
  35059. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  35060. }
  35061. if (ret == 0) {
  35062. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  35063. if (ret != 0)
  35064. ret = WC_TEST_RET_ENC_EC(ret);
  35065. }
  35066. if (ret == 0) {
  35067. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  35068. if (ret != 0)
  35069. ret = WC_TEST_RET_ENC_EC(ret);
  35070. }
  35071. if (ret == 0) {
  35072. ret = eccsi_kat_verify_test(pub, pvt);
  35073. }
  35074. if (ret == 0) {
  35075. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  35076. }
  35077. if (ret == 0) {
  35078. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  35079. }
  35080. if (pvt != NULL)
  35081. wc_ecc_del_point(pvt);
  35082. if (rng_inited)
  35083. wc_FreeRng(&rng);
  35084. if (ssk != NULL) {
  35085. mp_free(ssk);
  35086. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35087. }
  35088. if (pub != NULL) {
  35089. wc_FreeEccsiKey(pub);
  35090. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35091. }
  35092. if (priv != NULL) {
  35093. wc_FreeEccsiKey(priv);
  35094. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35095. }
  35096. return ret;
  35097. }
  35098. #endif /* WOLFCRYPT_HAVE_ECCSI */
  35099. #ifdef WOLFCRYPT_HAVE_SAKKE
  35100. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  35101. {
  35102. wc_test_ret_t ret;
  35103. byte id[1] = { 0x00 };
  35104. int valid;
  35105. byte data[256];
  35106. word32 sz;
  35107. byte auth[257];
  35108. word16 authSz;
  35109. byte ssv[256];
  35110. word16 ssvSz;
  35111. word32 len;
  35112. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  35113. if (ret != BAD_FUNC_ARG)
  35114. return WC_TEST_RET_ENC_EC(ret);
  35115. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  35116. if (ret != BAD_FUNC_ARG)
  35117. return WC_TEST_RET_ENC_EC(ret);
  35118. wc_FreeSakkeKey(NULL);
  35119. XMEMSET(key, 0, sizeof(*key));
  35120. wc_FreeSakkeKey(key);
  35121. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  35122. if (ret != 0)
  35123. return WC_TEST_RET_ENC_EC(ret);
  35124. ret = wc_MakeSakkeKey(NULL, NULL);
  35125. if (ret != BAD_FUNC_ARG)
  35126. return WC_TEST_RET_ENC_EC(ret);
  35127. ret = wc_MakeSakkeKey(key, NULL);
  35128. if (ret != BAD_FUNC_ARG)
  35129. return WC_TEST_RET_ENC_EC(ret);
  35130. ret = wc_MakeSakkeKey(NULL, rng);
  35131. if (ret != BAD_FUNC_ARG)
  35132. return WC_TEST_RET_ENC_EC(ret);
  35133. ret = wc_MakeSakkePublicKey(NULL, NULL);
  35134. if (ret != BAD_FUNC_ARG)
  35135. return WC_TEST_RET_ENC_EC(ret);
  35136. ret = wc_MakeSakkePublicKey(key, NULL);
  35137. if (ret != BAD_FUNC_ARG)
  35138. return WC_TEST_RET_ENC_EC(ret);
  35139. ret = wc_MakeSakkePublicKey(NULL, rsk);
  35140. if (ret != BAD_FUNC_ARG)
  35141. return WC_TEST_RET_ENC_EC(ret);
  35142. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  35143. if (ret != BAD_FUNC_ARG)
  35144. return WC_TEST_RET_ENC_EC(ret);
  35145. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  35146. if (ret != BAD_FUNC_ARG)
  35147. return WC_TEST_RET_ENC_EC(ret);
  35148. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  35149. if (ret != BAD_FUNC_ARG)
  35150. return WC_TEST_RET_ENC_EC(ret);
  35151. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  35152. if (ret != BAD_FUNC_ARG)
  35153. return WC_TEST_RET_ENC_EC(ret);
  35154. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  35155. if (ret != BAD_FUNC_ARG)
  35156. return WC_TEST_RET_ENC_EC(ret);
  35157. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  35158. if (ret != BAD_FUNC_ARG)
  35159. return WC_TEST_RET_ENC_EC(ret);
  35160. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  35161. if (ret != BAD_FUNC_ARG)
  35162. return WC_TEST_RET_ENC_EC(ret);
  35163. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  35164. if (ret != BAD_FUNC_ARG)
  35165. return WC_TEST_RET_ENC_EC(ret);
  35166. ret = wc_ExportSakkeKey(key, data, NULL);
  35167. if (ret != BAD_FUNC_ARG)
  35168. return WC_TEST_RET_ENC_EC(ret);
  35169. ret = wc_ExportSakkeKey(NULL, data, &sz);
  35170. if (ret != BAD_FUNC_ARG)
  35171. return WC_TEST_RET_ENC_EC(ret);
  35172. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  35173. if (ret != BAD_FUNC_ARG)
  35174. return WC_TEST_RET_ENC_EC(ret);
  35175. ret = wc_ImportSakkeKey(key, NULL, 1);
  35176. if (ret != BAD_FUNC_ARG)
  35177. return WC_TEST_RET_ENC_EC(ret);
  35178. ret = wc_ImportSakkeKey(NULL, data, 1);
  35179. if (ret != BAD_FUNC_ARG)
  35180. return WC_TEST_RET_ENC_EC(ret);
  35181. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  35182. if (ret != BAD_FUNC_ARG)
  35183. return WC_TEST_RET_ENC_EC(ret);
  35184. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  35185. if (ret != BAD_FUNC_ARG)
  35186. return WC_TEST_RET_ENC_EC(ret);
  35187. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  35188. if (ret != BAD_FUNC_ARG)
  35189. return WC_TEST_RET_ENC_EC(ret);
  35190. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  35191. if (ret != BAD_FUNC_ARG)
  35192. return WC_TEST_RET_ENC_EC(ret);
  35193. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  35194. if (ret != BAD_FUNC_ARG)
  35195. return WC_TEST_RET_ENC_EC(ret);
  35196. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  35197. if (ret != BAD_FUNC_ARG)
  35198. return WC_TEST_RET_ENC_EC(ret);
  35199. sz = sizeof(data);
  35200. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  35201. if (ret != BAD_FUNC_ARG)
  35202. return WC_TEST_RET_ENC_EC(ret);
  35203. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  35204. if (ret != BAD_FUNC_ARG)
  35205. return WC_TEST_RET_ENC_EC(ret);
  35206. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  35207. if (ret != BAD_FUNC_ARG)
  35208. return WC_TEST_RET_ENC_EC(ret);
  35209. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  35210. if (ret != BAD_FUNC_ARG)
  35211. return WC_TEST_RET_ENC_EC(ret);
  35212. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  35213. if (ret != BAD_FUNC_ARG)
  35214. return WC_TEST_RET_ENC_EC(ret);
  35215. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  35216. if (ret != BAD_FUNC_ARG)
  35217. return WC_TEST_RET_ENC_EC(ret);
  35218. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  35219. if (ret != BAD_FUNC_ARG)
  35220. return WC_TEST_RET_ENC_EC(ret);
  35221. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  35222. if (ret != BAD_FUNC_ARG)
  35223. return WC_TEST_RET_ENC_EC(ret);
  35224. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  35225. if (ret != BAD_FUNC_ARG)
  35226. return WC_TEST_RET_ENC_EC(ret);
  35227. ret = wc_ImportSakkeRsk(key, NULL, sz);
  35228. if (ret != BAD_FUNC_ARG)
  35229. return WC_TEST_RET_ENC_EC(ret);
  35230. ret = wc_ImportSakkeRsk(NULL, data, sz);
  35231. if (ret != BAD_FUNC_ARG)
  35232. return WC_TEST_RET_ENC_EC(ret);
  35233. ret = wc_ImportSakkeRsk(key, data, 1);
  35234. if (ret != BUFFER_E)
  35235. return WC_TEST_RET_ENC_EC(ret);
  35236. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  35237. if (ret != BAD_FUNC_ARG)
  35238. return WC_TEST_RET_ENC_EC(ret);
  35239. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  35240. if (ret != BAD_FUNC_ARG)
  35241. return WC_TEST_RET_ENC_EC(ret);
  35242. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  35243. if (ret != BAD_FUNC_ARG)
  35244. return WC_TEST_RET_ENC_EC(ret);
  35245. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  35246. if (ret != BAD_FUNC_ARG)
  35247. return WC_TEST_RET_ENC_EC(ret);
  35248. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  35249. if (ret != BAD_FUNC_ARG)
  35250. return WC_TEST_RET_ENC_EC(ret);
  35251. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  35252. if (ret != BAD_FUNC_ARG)
  35253. return WC_TEST_RET_ENC_EC(ret);
  35254. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  35255. if (ret != BAD_FUNC_ARG)
  35256. return WC_TEST_RET_ENC_EC(ret);
  35257. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  35258. if (ret != LENGTH_ONLY_E)
  35259. return WC_TEST_RET_ENC_EC(ret);
  35260. len--;
  35261. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  35262. if (ret != BUFFER_E)
  35263. return WC_TEST_RET_ENC_EC(ret);
  35264. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  35265. if (ret != BAD_FUNC_ARG)
  35266. return WC_TEST_RET_ENC_EC(ret);
  35267. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  35268. if (ret != BAD_FUNC_ARG)
  35269. return WC_TEST_RET_ENC_EC(ret);
  35270. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  35271. if (ret != BAD_FUNC_ARG)
  35272. return WC_TEST_RET_ENC_EC(ret);
  35273. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  35274. if (ret != BAD_FUNC_ARG)
  35275. return WC_TEST_RET_ENC_EC(ret);
  35276. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  35277. if (ret != BAD_FUNC_ARG)
  35278. return WC_TEST_RET_ENC_EC(ret);
  35279. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  35280. if (ret != BAD_FUNC_ARG)
  35281. return WC_TEST_RET_ENC_EC(ret);
  35282. ret = wc_GetSakkeAuthSize(NULL, NULL);
  35283. if (ret != BAD_FUNC_ARG)
  35284. return WC_TEST_RET_ENC_EC(ret);
  35285. ret = wc_GetSakkeAuthSize(key, NULL);
  35286. if (ret != BAD_FUNC_ARG)
  35287. return WC_TEST_RET_ENC_EC(ret);
  35288. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  35289. if (ret != BAD_FUNC_ARG)
  35290. return WC_TEST_RET_ENC_EC(ret);
  35291. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  35292. if (ret != BAD_FUNC_ARG)
  35293. return WC_TEST_RET_ENC_EC(ret);
  35294. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  35295. if (ret != BAD_FUNC_ARG)
  35296. return WC_TEST_RET_ENC_EC(ret);
  35297. ret = wc_MakeSakkePointI(NULL, id, 1);
  35298. if (ret != BAD_FUNC_ARG)
  35299. return WC_TEST_RET_ENC_EC(ret);
  35300. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  35301. if (ret != BAD_FUNC_ARG)
  35302. return WC_TEST_RET_ENC_EC(ret);
  35303. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  35304. if (ret != BAD_FUNC_ARG)
  35305. return WC_TEST_RET_ENC_EC(ret);
  35306. ret = wc_MakeSakkePointI(key, NULL, 1);
  35307. if (ret != BAD_FUNC_ARG)
  35308. return WC_TEST_RET_ENC_EC(ret);
  35309. ret = wc_MakeSakkePointI(NULL, id, 1);
  35310. if (ret != BAD_FUNC_ARG)
  35311. return WC_TEST_RET_ENC_EC(ret);
  35312. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  35313. if (ret != BAD_FUNC_ARG)
  35314. return WC_TEST_RET_ENC_EC(ret);
  35315. ret = wc_GenerateSakkePointITable(key, data, NULL);
  35316. if (ret != BAD_FUNC_ARG)
  35317. return WC_TEST_RET_ENC_EC(ret);
  35318. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  35319. if (ret != BAD_FUNC_ARG)
  35320. return WC_TEST_RET_ENC_EC(ret);
  35321. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  35322. if (ret != LENGTH_ONLY_E)
  35323. return WC_TEST_RET_ENC_EC(ret);
  35324. len--;
  35325. ret = wc_GenerateSakkePointITable(key, data, &len);
  35326. if (ret != BUFFER_E)
  35327. return WC_TEST_RET_ENC_EC(ret);
  35328. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  35329. if (ret != BAD_FUNC_ARG)
  35330. return WC_TEST_RET_ENC_EC(ret);
  35331. ret = wc_SetSakkePointITable(key, NULL, 1);
  35332. if (ret != BAD_FUNC_ARG)
  35333. return WC_TEST_RET_ENC_EC(ret);
  35334. ret = wc_SetSakkePointITable(NULL, data, 1);
  35335. if (ret != BAD_FUNC_ARG)
  35336. return WC_TEST_RET_ENC_EC(ret);
  35337. ret = wc_SetSakkePointITable(key, data, 1);
  35338. if (ret != BUFFER_E)
  35339. return WC_TEST_RET_ENC_EC(ret);
  35340. ret = wc_ClearSakkePointITable(NULL);
  35341. if (ret != BAD_FUNC_ARG)
  35342. return WC_TEST_RET_ENC_EC(ret);
  35343. ret = wc_GetSakkePointI(NULL, data, NULL);
  35344. if (ret != BAD_FUNC_ARG)
  35345. return WC_TEST_RET_ENC_EC(ret);
  35346. ret = wc_GetSakkePointI(key, data, NULL);
  35347. if (ret != BAD_FUNC_ARG)
  35348. return WC_TEST_RET_ENC_EC(ret);
  35349. ret = wc_GetSakkePointI(NULL, data, &sz);
  35350. if (ret != BAD_FUNC_ARG)
  35351. return WC_TEST_RET_ENC_EC(ret);
  35352. sz = 1;
  35353. ret = wc_GetSakkePointI(key, data, &sz);
  35354. if (ret != BUFFER_E)
  35355. return WC_TEST_RET_ENC_EC(ret);
  35356. sz = 256;
  35357. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  35358. if (ret != BAD_FUNC_ARG)
  35359. return WC_TEST_RET_ENC_EC(ret);
  35360. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  35361. if (ret != BAD_FUNC_ARG)
  35362. return WC_TEST_RET_ENC_EC(ret);
  35363. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  35364. if (ret != BAD_FUNC_ARG)
  35365. return WC_TEST_RET_ENC_EC(ret);
  35366. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  35367. if (ret != BAD_FUNC_ARG)
  35368. return WC_TEST_RET_ENC_EC(ret);
  35369. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  35370. if (ret != BAD_FUNC_ARG)
  35371. return WC_TEST_RET_ENC_EC(ret);
  35372. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  35373. if (ret != BAD_FUNC_ARG)
  35374. return WC_TEST_RET_ENC_EC(ret);
  35375. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  35376. if (ret != BAD_FUNC_ARG)
  35377. return WC_TEST_RET_ENC_EC(ret);
  35378. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  35379. if (ret != BUFFER_E)
  35380. return WC_TEST_RET_ENC_EC(ret);
  35381. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  35382. if (ret != BUFFER_E)
  35383. return WC_TEST_RET_ENC_EC(ret);
  35384. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  35385. if (ret != BAD_FUNC_ARG)
  35386. return WC_TEST_RET_ENC_EC(ret);
  35387. ret = wc_SetSakkeIdentity(key, NULL, 1);
  35388. if (ret != BAD_FUNC_ARG)
  35389. return WC_TEST_RET_ENC_EC(ret);
  35390. ret = wc_SetSakkeIdentity(NULL, id, 1);
  35391. if (ret != BAD_FUNC_ARG)
  35392. return WC_TEST_RET_ENC_EC(ret);
  35393. ssvSz = sizeof(ssv);
  35394. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  35395. auth, NULL);
  35396. if (ret != BAD_FUNC_ARG)
  35397. return WC_TEST_RET_ENC_EC(ret);
  35398. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  35399. auth, NULL);
  35400. if (ret != BAD_FUNC_ARG)
  35401. return WC_TEST_RET_ENC_EC(ret);
  35402. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35403. auth, NULL);
  35404. if (ret != BAD_FUNC_ARG)
  35405. return WC_TEST_RET_ENC_EC(ret);
  35406. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  35407. auth, &authSz);
  35408. if (ret != BAD_FUNC_ARG)
  35409. return WC_TEST_RET_ENC_EC(ret);
  35410. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35411. auth, NULL);
  35412. if (ret != BAD_FUNC_ARG)
  35413. return WC_TEST_RET_ENC_EC(ret);
  35414. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  35415. auth, &authSz);
  35416. if (ret != BAD_FUNC_ARG)
  35417. return WC_TEST_RET_ENC_EC(ret);
  35418. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35419. auth, &authSz);
  35420. if (ret != BAD_FUNC_ARG)
  35421. return WC_TEST_RET_ENC_EC(ret);
  35422. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35423. auth, &authSz);
  35424. if (ret != BAD_STATE_E)
  35425. return WC_TEST_RET_ENC_EC(ret);
  35426. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  35427. if (ret != BAD_FUNC_ARG)
  35428. return WC_TEST_RET_ENC_EC(ret);
  35429. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  35430. if (ret != BAD_FUNC_ARG)
  35431. return WC_TEST_RET_ENC_EC(ret);
  35432. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  35433. if (ret != BAD_FUNC_ARG)
  35434. return WC_TEST_RET_ENC_EC(ret);
  35435. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  35436. if (ret != BAD_FUNC_ARG)
  35437. return WC_TEST_RET_ENC_EC(ret);
  35438. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  35439. if (ret != BAD_FUNC_ARG)
  35440. return WC_TEST_RET_ENC_EC(ret);
  35441. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  35442. if (ret != BAD_FUNC_ARG)
  35443. return WC_TEST_RET_ENC_EC(ret);
  35444. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  35445. if (ret != BAD_FUNC_ARG)
  35446. return WC_TEST_RET_ENC_EC(ret);
  35447. ssvSz = sizeof(ssv);
  35448. authSz = sizeof(auth);
  35449. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  35450. authSz);
  35451. if (ret != BAD_FUNC_ARG)
  35452. return WC_TEST_RET_ENC_EC(ret);
  35453. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  35454. authSz);
  35455. if (ret != BAD_FUNC_ARG)
  35456. return WC_TEST_RET_ENC_EC(ret);
  35457. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  35458. authSz);
  35459. if (ret != BAD_FUNC_ARG)
  35460. return WC_TEST_RET_ENC_EC(ret);
  35461. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  35462. authSz);
  35463. if (ret != BAD_FUNC_ARG)
  35464. return WC_TEST_RET_ENC_EC(ret);
  35465. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  35466. authSz);
  35467. if (ret != BAD_FUNC_ARG)
  35468. return WC_TEST_RET_ENC_EC(ret);
  35469. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  35470. authSz);
  35471. if (ret != BAD_FUNC_ARG)
  35472. return WC_TEST_RET_ENC_EC(ret);
  35473. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35474. authSz);
  35475. if (ret != BAD_FUNC_ARG)
  35476. return WC_TEST_RET_ENC_EC(ret);
  35477. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35478. authSz);
  35479. if (ret != BAD_STATE_E)
  35480. return WC_TEST_RET_ENC_EC(ret);
  35481. ret = wc_SetSakkeIdentity(key, id, 1);
  35482. if (ret != 0)
  35483. return WC_TEST_RET_ENC_EC(ret);
  35484. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35485. authSz);
  35486. if (ret != BAD_STATE_E)
  35487. return WC_TEST_RET_ENC_EC(ret);
  35488. ret = wc_SetSakkeIdentity(key, id, 0);
  35489. if (ret != 0)
  35490. return WC_TEST_RET_ENC_EC(ret);
  35491. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  35492. if (ret != 0)
  35493. return WC_TEST_RET_ENC_EC(ret);
  35494. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  35495. authSz);
  35496. if (ret != BAD_STATE_E)
  35497. return WC_TEST_RET_ENC_EC(ret);
  35498. wc_FreeSakkeKey(key);
  35499. return 0;
  35500. }
  35501. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  35502. {
  35503. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  35504. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  35505. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  35506. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  35507. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  35508. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  35509. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  35510. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  35511. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  35512. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  35513. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  35514. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  35515. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  35516. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  35517. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  35518. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  35519. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  35520. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  35521. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  35522. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  35523. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  35524. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  35525. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  35526. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  35527. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  35528. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  35529. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  35530. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  35531. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  35532. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  35533. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  35534. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  35535. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  35536. };
  35537. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  35538. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  35539. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  35540. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  35541. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  35542. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  35543. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  35544. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  35545. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  35546. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  35547. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  35548. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  35549. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  35550. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  35551. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  35552. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  35553. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  35554. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  35555. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  35556. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  35557. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  35558. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  35559. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  35560. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  35561. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  35562. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  35563. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  35564. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  35565. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  35566. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  35567. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  35568. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  35569. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  35570. };
  35571. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  35572. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  35573. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  35574. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  35575. 0x33, 0x00
  35576. };
  35577. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  35578. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  35579. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  35580. };
  35581. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  35582. 0x04,
  35583. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  35584. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  35585. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  35586. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  35587. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  35588. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  35589. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  35590. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  35591. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  35592. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  35593. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  35594. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  35595. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  35596. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  35597. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  35598. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  35599. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  35600. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  35601. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  35602. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  35603. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  35604. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  35605. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  35606. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  35607. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  35608. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  35609. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  35610. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  35611. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  35612. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  35613. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  35614. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  35615. };
  35616. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  35617. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  35618. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  35619. };
  35620. wc_test_ret_t ret;
  35621. int valid;
  35622. byte pubKey[sizeof(pubData) + 1];
  35623. word32 sz = sizeof(pubKey);
  35624. byte tmpSsv[sizeof(encSsv)];
  35625. byte* iTable = NULL;
  35626. word32 iTableLen;
  35627. byte* table = NULL;
  35628. word32 len;
  35629. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  35630. if (ret != 0)
  35631. return WC_TEST_RET_ENC_EC(ret);
  35632. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  35633. if (ret != 0)
  35634. return WC_TEST_RET_ENC_EC(ret);
  35635. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  35636. if (ret != 0)
  35637. return WC_TEST_RET_ENC_EC(ret);
  35638. if (valid != 1)
  35639. return WC_TEST_RET_ENC_NC;
  35640. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  35641. if (ret != 0)
  35642. return WC_TEST_RET_ENC_EC(ret);
  35643. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  35644. if (ret != 0)
  35645. return WC_TEST_RET_ENC_EC(ret);
  35646. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  35647. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  35648. auth, sizeof(auth));
  35649. if (ret != 0)
  35650. return WC_TEST_RET_ENC_EC(ret);
  35651. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  35652. return WC_TEST_RET_ENC_NC;
  35653. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  35654. if (ret != 0)
  35655. return WC_TEST_RET_ENC_EC(ret);
  35656. iTableLen = 0;
  35657. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  35658. if (ret != LENGTH_ONLY_E)
  35659. return WC_TEST_RET_ENC_EC(ret);
  35660. if (iTableLen != 0) {
  35661. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35662. if (iTable == NULL)
  35663. return WC_TEST_RET_ENC_ERRNO;
  35664. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  35665. if (ret != 0)
  35666. return WC_TEST_RET_ENC_EC(ret);
  35667. }
  35668. len = 0;
  35669. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  35670. if (ret != LENGTH_ONLY_E)
  35671. return WC_TEST_RET_ENC_EC(ret);
  35672. if (len > 0) {
  35673. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35674. if (table == NULL)
  35675. return WC_TEST_RET_ENC_ERRNO;
  35676. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  35677. if (ret != 0)
  35678. return WC_TEST_RET_ENC_EC(ret);
  35679. }
  35680. ret = wc_SetSakkeRsk(key, rsk, table, len);
  35681. if (ret != 0)
  35682. return WC_TEST_RET_ENC_EC(ret);
  35683. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  35684. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  35685. auth, sizeof(auth));
  35686. if (ret != 0)
  35687. return WC_TEST_RET_ENC_EC(ret);
  35688. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  35689. return WC_TEST_RET_ENC_NC;
  35690. /* Don't reference table that is about to be freed. */
  35691. ret = wc_ClearSakkePointITable(key);
  35692. if (ret != 0)
  35693. return WC_TEST_RET_ENC_EC(ret);
  35694. /* Dispose of tables */
  35695. if (iTable != NULL)
  35696. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35697. if (table != NULL)
  35698. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35699. /* Make sure the key public key is exportable - convert to Montgomery form
  35700. * in Validation.
  35701. */
  35702. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  35703. if (ret != 0)
  35704. return WC_TEST_RET_ENC_EC(ret);
  35705. if (sz != sizeof(pubData))
  35706. return WC_TEST_RET_ENC_NC;
  35707. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  35708. return WC_TEST_RET_ENC_NC;
  35709. sz = sizeof(pubData) + 1;
  35710. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  35711. if (ret != 0)
  35712. return WC_TEST_RET_ENC_EC(ret);
  35713. if (sz != sizeof(pubData) + 1)
  35714. return WC_TEST_RET_ENC_NC;
  35715. if (pubKey[0] != 0x04)
  35716. return WC_TEST_RET_ENC_NC;
  35717. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  35718. return WC_TEST_RET_ENC_NC;
  35719. return 0;
  35720. }
  35721. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  35722. {
  35723. static const byte pubData[] = {
  35724. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  35725. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  35726. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  35727. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  35728. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  35729. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  35730. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  35731. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  35732. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  35733. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  35734. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  35735. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  35736. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  35737. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  35738. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  35739. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  35740. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  35741. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  35742. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  35743. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  35744. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  35745. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  35746. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  35747. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  35748. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  35749. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  35750. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  35751. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  35752. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  35753. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  35754. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  35755. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  35756. };
  35757. static const byte id[] = {
  35758. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  35759. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  35760. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  35761. 0x33, 0x00
  35762. };
  35763. static const word32 idSz = sizeof(id);
  35764. byte ssv[] = {
  35765. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  35766. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  35767. };
  35768. static const word16 ssvSz = sizeof(ssv);
  35769. static const byte expAuth[] = {
  35770. 0x04,
  35771. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  35772. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  35773. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  35774. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  35775. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  35776. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  35777. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  35778. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  35779. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  35780. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  35781. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  35782. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  35783. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  35784. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  35785. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  35786. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  35787. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  35788. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  35789. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  35790. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  35791. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  35792. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  35793. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  35794. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  35795. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  35796. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  35797. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  35798. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  35799. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  35800. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  35801. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  35802. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  35803. };
  35804. static const byte encSsv[] = {
  35805. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  35806. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  35807. };
  35808. wc_test_ret_t ret;
  35809. byte auth[257];
  35810. word16 authSz = sizeof(auth);
  35811. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  35812. if (ret != 0)
  35813. return WC_TEST_RET_ENC_EC(ret);
  35814. ret = wc_SetSakkeIdentity(key, id, idSz);
  35815. if (ret != 0)
  35816. return WC_TEST_RET_ENC_EC(ret);
  35817. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35818. auth, &authSz);
  35819. if (ret != 0)
  35820. return WC_TEST_RET_ENC_EC(ret);
  35821. if (authSz != 257)
  35822. return WC_TEST_RET_ENC_NC;
  35823. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  35824. return WC_TEST_RET_ENC_NC;
  35825. if (XMEMCMP(auth, expAuth, authSz) != 0)
  35826. return WC_TEST_RET_ENC_NC;
  35827. return 0;
  35828. }
  35829. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  35830. WC_RNG* rng, ecc_point* rsk)
  35831. {
  35832. wc_test_ret_t ret;
  35833. byte data[440];
  35834. byte pubData[257];
  35835. word32 sz;
  35836. char mail[] = "test@wolfssl.com";
  35837. byte* id = (byte*)mail;
  35838. word32 idSz = (word32)XSTRLEN(mail);
  35839. int valid;
  35840. ecc_point* pubKey = rsk;
  35841. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  35842. if (ret != 0)
  35843. return WC_TEST_RET_ENC_EC(ret);
  35844. ret = wc_MakeSakkeKey(priv, rng);
  35845. if (ret != 0)
  35846. return WC_TEST_RET_ENC_EC(ret);
  35847. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  35848. if (ret != LENGTH_ONLY_E)
  35849. return WC_TEST_RET_ENC_EC(ret);
  35850. if (sz != 384)
  35851. return WC_TEST_RET_ENC_NC;
  35852. sz--;
  35853. ret = wc_ExportSakkeKey(priv, data, &sz);
  35854. if (ret == 0)
  35855. return WC_TEST_RET_ENC_NC;
  35856. sz++;
  35857. ret = wc_ExportSakkeKey(priv, data, &sz);
  35858. if (ret != 0)
  35859. return WC_TEST_RET_ENC_EC(ret);
  35860. if (sz != 384)
  35861. return WC_TEST_RET_ENC_NC;
  35862. ret = wc_ImportSakkeKey(key, data, sz - 1);
  35863. if (ret == 0)
  35864. return WC_TEST_RET_ENC_NC;
  35865. ret = wc_ImportSakkeKey(key, data, sz);
  35866. if (ret != 0)
  35867. return WC_TEST_RET_ENC_EC(ret);
  35868. wc_FreeSakkeKey(key);
  35869. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  35870. if (ret != 0)
  35871. return WC_TEST_RET_ENC_EC(ret);
  35872. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  35873. if (ret != LENGTH_ONLY_E)
  35874. return WC_TEST_RET_ENC_EC(ret);
  35875. if (sz != 128)
  35876. return WC_TEST_RET_ENC_NC;
  35877. sz--;
  35878. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  35879. if (ret == 0)
  35880. return WC_TEST_RET_ENC_NC;
  35881. sz++;
  35882. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  35883. if (ret != 0)
  35884. return WC_TEST_RET_ENC_EC(ret);
  35885. if (sz != 128)
  35886. return WC_TEST_RET_ENC_NC;
  35887. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  35888. if (ret == 0)
  35889. return WC_TEST_RET_ENC_NC;
  35890. ret = wc_ImportSakkePrivateKey(key, data, sz);
  35891. if (ret != 0)
  35892. return WC_TEST_RET_ENC_EC(ret);
  35893. ret = wc_MakeSakkePublicKey(key, pubKey);
  35894. if (ret != 0)
  35895. return WC_TEST_RET_ENC_EC(ret);
  35896. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  35897. if (ret != LENGTH_ONLY_E)
  35898. return WC_TEST_RET_ENC_EC(ret);
  35899. if (sz != 256)
  35900. return WC_TEST_RET_ENC_NC;
  35901. sz--;
  35902. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  35903. if (ret == 0)
  35904. return WC_TEST_RET_ENC_NC;
  35905. sz++;
  35906. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  35907. if (ret != 0)
  35908. return WC_TEST_RET_ENC_EC(ret);
  35909. if (sz != 256)
  35910. return WC_TEST_RET_ENC_NC;
  35911. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  35912. if (ret == 0)
  35913. return WC_TEST_RET_ENC_NC;
  35914. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  35915. if (ret != 0)
  35916. return WC_TEST_RET_ENC_EC(ret);
  35917. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  35918. if (ret != 0)
  35919. return WC_TEST_RET_ENC_EC(ret);
  35920. if (sz != 256)
  35921. return WC_TEST_RET_ENC_NC;
  35922. if (XMEMCMP(data, pubData, sz) != 0)
  35923. return WC_TEST_RET_ENC_NC;
  35924. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  35925. if (ret != 0)
  35926. return WC_TEST_RET_ENC_EC(ret);
  35927. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  35928. if (ret != 0)
  35929. return WC_TEST_RET_ENC_EC(ret);
  35930. if (valid != 1)
  35931. return WC_TEST_RET_ENC_NC;
  35932. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  35933. if (ret != 0)
  35934. return WC_TEST_RET_ENC_EC(ret);
  35935. if (valid != 1)
  35936. return WC_TEST_RET_ENC_NC;
  35937. sz = sizeof(data);
  35938. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  35939. if (ret != 0)
  35940. return WC_TEST_RET_ENC_EC(ret);
  35941. if (sz != 256)
  35942. return WC_TEST_RET_ENC_NC;
  35943. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  35944. if (ret != 0)
  35945. return WC_TEST_RET_ENC_EC(ret);
  35946. sz = sizeof(pubData);
  35947. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  35948. if (ret != 0)
  35949. return WC_TEST_RET_ENC_EC(ret);
  35950. if (sz != sizeof(pubData))
  35951. return WC_TEST_RET_ENC_NC;
  35952. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  35953. if (ret != 0)
  35954. return WC_TEST_RET_ENC_EC(ret);
  35955. wc_FreeSakkeKey(key);
  35956. return 0;
  35957. }
  35958. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  35959. ecc_point* rsk)
  35960. {
  35961. wc_test_ret_t ret;
  35962. byte ssv[16];
  35963. word16 ssvSz;
  35964. byte auth[257];
  35965. word16 authSz;
  35966. char mail[] = "test@wolfssl.com";
  35967. byte* id = (byte*)mail;
  35968. word32 idSz = (word32)XSTRLEN(mail);
  35969. byte pointI[256];
  35970. word32 sz;
  35971. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  35972. if (ret != LENGTH_ONLY_E)
  35973. return WC_TEST_RET_ENC_EC(ret);
  35974. if (ssvSz != 16)
  35975. return WC_TEST_RET_ENC_NC;
  35976. ssvSz += 128;
  35977. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  35978. if (ret == 0)
  35979. return WC_TEST_RET_ENC_NC;
  35980. ssvSz -= 128;
  35981. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  35982. if (ret != 0)
  35983. return WC_TEST_RET_ENC_EC(ret);
  35984. if (ssvSz != 16)
  35985. return WC_TEST_RET_ENC_NC;
  35986. ret = wc_GetSakkeAuthSize(pub, &authSz);
  35987. if (ret != 0)
  35988. return WC_TEST_RET_ENC_EC(ret);
  35989. ret = wc_SetSakkeIdentity(pub, id, idSz);
  35990. if (ret != 0)
  35991. return WC_TEST_RET_ENC_EC(ret);
  35992. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  35993. NULL, &authSz);
  35994. if (ret != LENGTH_ONLY_E)
  35995. return WC_TEST_RET_ENC_EC(ret);
  35996. if (authSz != 257)
  35997. return WC_TEST_RET_ENC_NC;
  35998. authSz--;
  35999. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  36000. auth, &authSz);
  36001. if (ret == 0)
  36002. return WC_TEST_RET_ENC_NC;
  36003. authSz++;
  36004. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  36005. auth, &authSz);
  36006. if (ret != 0)
  36007. return WC_TEST_RET_ENC_EC(ret);
  36008. if (authSz != 257)
  36009. return WC_TEST_RET_ENC_NC;
  36010. ret = wc_GetSakkePointI(pub, NULL, &sz);
  36011. if (ret != LENGTH_ONLY_E)
  36012. return WC_TEST_RET_ENC_EC(ret);
  36013. if (sz != 256)
  36014. return WC_TEST_RET_ENC_NC;
  36015. ret = wc_GetSakkePointI(pub, pointI, &sz);
  36016. if (ret != 0)
  36017. return WC_TEST_RET_ENC_EC(ret);
  36018. if (sz != 256)
  36019. return WC_TEST_RET_ENC_NC;
  36020. /* Bogus identity - make it check and regenerate I. */
  36021. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  36022. if (ret != 0)
  36023. return WC_TEST_RET_ENC_EC(ret);
  36024. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  36025. auth, &authSz);
  36026. if (ret != 0)
  36027. return WC_TEST_RET_ENC_EC(ret);
  36028. if (authSz != 257)
  36029. return WC_TEST_RET_ENC_NC;
  36030. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  36031. if (ret != 0)
  36032. return WC_TEST_RET_ENC_EC(ret);
  36033. ret = wc_SetSakkeIdentity(priv, id, idSz);
  36034. if (ret != 0)
  36035. return WC_TEST_RET_ENC_EC(ret);
  36036. authSz--;
  36037. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  36038. authSz);
  36039. if (ret == 0)
  36040. return WC_TEST_RET_ENC_NC;
  36041. authSz++;
  36042. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  36043. authSz);
  36044. if (ret != 0)
  36045. return WC_TEST_RET_ENC_EC(ret);
  36046. ssv[0] ^= 0x80;
  36047. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  36048. authSz);
  36049. if (ret != SAKKE_VERIFY_FAIL_E)
  36050. return WC_TEST_RET_ENC_EC(ret);
  36051. ssv[0] ^= 0x80;
  36052. /* Bogus identity - make it check and regenerate I. */
  36053. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  36054. if (ret != 0)
  36055. return WC_TEST_RET_ENC_EC(ret);
  36056. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  36057. authSz);
  36058. if (ret != 0)
  36059. return WC_TEST_RET_ENC_EC(ret);
  36060. return 0;
  36061. }
  36062. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  36063. {
  36064. wc_test_ret_t ret = 0;
  36065. WC_RNG rng;
  36066. int rng_inited = 0;
  36067. SakkeKey* priv = NULL;
  36068. SakkeKey* pub = NULL;
  36069. SakkeKey* key = NULL;
  36070. ecc_point* rsk = NULL;
  36071. WOLFSSL_ENTER("sakke_test");
  36072. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  36073. DYNAMIC_TYPE_TMP_BUFFER);
  36074. if (priv == NULL)
  36075. ret = WC_TEST_RET_ENC_NC;
  36076. else
  36077. XMEMSET(priv, 0, sizeof(*priv));
  36078. if (ret == 0) {
  36079. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  36080. DYNAMIC_TYPE_TMP_BUFFER);
  36081. if (pub == NULL)
  36082. ret = WC_TEST_RET_ENC_NC;
  36083. else
  36084. XMEMSET(pub, 0, sizeof(*pub));
  36085. }
  36086. if (ret == 0) {
  36087. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  36088. DYNAMIC_TYPE_TMP_BUFFER);
  36089. if (key == NULL)
  36090. ret = WC_TEST_RET_ENC_NC;
  36091. else
  36092. XMEMSET(key, 0, sizeof(*key));
  36093. }
  36094. if (ret == 0) {
  36095. #ifndef HAVE_FIPS
  36096. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36097. #else
  36098. ret = wc_InitRng(&rng);
  36099. #endif
  36100. if (ret == 0)
  36101. rng_inited = 1;
  36102. else
  36103. ret = WC_TEST_RET_ENC_EC(ret);
  36104. }
  36105. if (ret == 0) {
  36106. rsk = wc_ecc_new_point();
  36107. if (rsk == NULL)
  36108. ret = WC_TEST_RET_ENC_NC;
  36109. }
  36110. if (ret == 0) {
  36111. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  36112. if (ret != 0)
  36113. ret = WC_TEST_RET_ENC_EC(ret);
  36114. }
  36115. if (ret == 0) {
  36116. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  36117. if (ret != 0)
  36118. ret = WC_TEST_RET_ENC_EC(ret);
  36119. }
  36120. if (ret == 0) {
  36121. ret = sakke_api_test(&rng, key, rsk);
  36122. }
  36123. if (ret == 0) {
  36124. ret = sakke_kat_derive_test(pub, rsk);
  36125. }
  36126. if (ret == 0) {
  36127. ret = sakke_kat_encapsulate_test(pub);
  36128. }
  36129. if (ret == 0) {
  36130. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  36131. }
  36132. if (ret == 0) {
  36133. ret = sakke_op_test(priv, pub, &rng, rsk);
  36134. }
  36135. if (rsk != NULL) {
  36136. wc_ecc_forcezero_point(rsk);
  36137. wc_ecc_del_point(rsk);
  36138. }
  36139. if (rng_inited)
  36140. wc_FreeRng(&rng);
  36141. if (key != NULL)
  36142. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36143. if (pub != NULL) {
  36144. wc_FreeSakkeKey(pub);
  36145. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36146. }
  36147. if (priv != NULL) {
  36148. wc_FreeSakkeKey(priv);
  36149. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36150. }
  36151. return ret;
  36152. }
  36153. #endif /* WOLFCRYPT_HAVE_SAKKE */
  36154. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  36155. typedef struct CMAC_Test_Case {
  36156. int type;
  36157. int partial;
  36158. const byte* m;
  36159. word32 mSz;
  36160. const byte* k;
  36161. word32 kSz;
  36162. const byte* t;
  36163. word32 tSz;
  36164. } CMAC_Test_Case;
  36165. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  36166. {
  36167. #ifdef WOLFSSL_AES_128
  36168. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  36169. {
  36170. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  36171. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  36172. };
  36173. #define KLEN_128 (sizeof(k128))
  36174. #endif
  36175. #ifdef WOLFSSL_AES_192
  36176. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  36177. {
  36178. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  36179. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  36180. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  36181. };
  36182. #define KLEN_192 (sizeof(k192))
  36183. #endif
  36184. #ifdef WOLFSSL_AES_256
  36185. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  36186. {
  36187. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  36188. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  36189. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  36190. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  36191. };
  36192. #define KLEN_256 (sizeof(k256))
  36193. #endif
  36194. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  36195. {
  36196. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  36197. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  36198. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  36199. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  36200. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  36201. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  36202. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  36203. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  36204. };
  36205. #define MLEN_0 (0)
  36206. #define MLEN_128 (128/8)
  36207. #define MLEN_320 (320/8)
  36208. #define MLEN_319 (MLEN_320 - 1)
  36209. #define MLEN_512 (512/8)
  36210. #ifdef WOLFSSL_AES_128
  36211. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  36212. {
  36213. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  36214. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  36215. };
  36216. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  36217. {
  36218. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  36219. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  36220. };
  36221. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  36222. {
  36223. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  36224. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  36225. };
  36226. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  36227. {
  36228. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  36229. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  36230. };
  36231. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  36232. {
  36233. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  36234. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  36235. };
  36236. #endif
  36237. #ifdef WOLFSSL_AES_192
  36238. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  36239. {
  36240. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  36241. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  36242. };
  36243. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  36244. {
  36245. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  36246. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  36247. };
  36248. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  36249. {
  36250. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  36251. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  36252. };
  36253. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  36254. {
  36255. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  36256. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  36257. };
  36258. #endif
  36259. #ifdef WOLFSSL_AES_256
  36260. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  36261. {
  36262. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  36263. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  36264. };
  36265. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  36266. {
  36267. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  36268. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  36269. };
  36270. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  36271. {
  36272. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  36273. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  36274. };
  36275. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  36276. {
  36277. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  36278. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  36279. };
  36280. #endif
  36281. const CMAC_Test_Case testCases[] =
  36282. {
  36283. #ifdef WOLFSSL_AES_128
  36284. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  36285. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  36286. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  36287. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  36288. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  36289. #endif
  36290. #ifdef WOLFSSL_AES_192
  36291. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  36292. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  36293. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  36294. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  36295. #endif
  36296. #ifdef WOLFSSL_AES_256
  36297. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  36298. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  36299. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  36300. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  36301. #endif
  36302. #ifdef WOLFSSL_AES_128
  36303. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  36304. #endif
  36305. };
  36306. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36307. Cmac *cmac;
  36308. #else
  36309. Cmac cmac[1];
  36310. #endif
  36311. byte tag[AES_BLOCK_SIZE];
  36312. const CMAC_Test_Case* tc;
  36313. word32 i, tagSz;
  36314. wc_test_ret_t ret;
  36315. WOLFSSL_ENTER("cmac_test");
  36316. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36317. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  36318. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36319. #endif
  36320. for (i = 0, tc = testCases;
  36321. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  36322. i++, tc++) {
  36323. XMEMSET(tag, 0, sizeof(tag));
  36324. tagSz = AES_BLOCK_SIZE;
  36325. #if !defined(HAVE_FIPS) || \
  36326. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  36327. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  36328. #else
  36329. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  36330. #endif
  36331. if (ret != 0)
  36332. {
  36333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36334. }
  36335. if (tc->partial) {
  36336. ret = wc_CmacUpdate(cmac, tc->m,
  36337. tc->mSz/2 - tc->partial);
  36338. if (ret != 0)
  36339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36340. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  36341. tc->mSz/2 + tc->partial);
  36342. if (ret != 0)
  36343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36344. }
  36345. else {
  36346. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  36347. if (ret != 0)
  36348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36349. }
  36350. ret = wc_CmacFinal(cmac, tag, &tagSz);
  36351. if (ret != 0)
  36352. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36353. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  36354. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36355. XMEMSET(tag, 0, sizeof(tag));
  36356. tagSz = sizeof(tag);
  36357. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  36358. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  36359. tc->k, tc->kSz, NULL, devId);
  36360. #else
  36361. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  36362. tc->k, tc->kSz);
  36363. #endif
  36364. if (ret != 0)
  36365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36366. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  36367. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36368. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  36369. ret = wc_AesCmacVerify_ex(cmac, tc->t, tc->tSz, tc->m, tc->mSz,
  36370. tc->k, tc->kSz, HEAP_HINT, devId);
  36371. #else
  36372. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  36373. tc->k, tc->kSz);
  36374. #endif
  36375. if (ret != 0)
  36376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36377. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  36378. /* Test that keyless generate with init is the same */
  36379. XMEMSET(tag, 0, sizeof(tag));
  36380. tagSz = sizeof(tag);
  36381. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  36382. if (ret != 0) {
  36383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36384. }
  36385. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  36386. NULL, 0, HEAP_HINT, devId);
  36387. if (ret != 0) {
  36388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36389. }
  36390. #endif
  36391. }
  36392. ret = 0;
  36393. out:
  36394. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36395. if (cmac)
  36396. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  36397. #endif
  36398. return ret;
  36399. }
  36400. #endif /* NO_AES && WOLFSSL_CMAC */
  36401. #if defined(WOLFSSL_SIPHASH)
  36402. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  36403. /* Test vectors from:
  36404. * https://github.com/veorq/SipHash/blob/master/vectors.h
  36405. */
  36406. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  36407. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  36408. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  36409. };
  36410. static const unsigned char siphash_msg[64] = {
  36411. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  36412. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  36413. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  36414. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  36415. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  36416. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  36417. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  36418. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  36419. };
  36420. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  36421. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  36422. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  36423. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  36424. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  36425. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  36426. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  36427. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  36428. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  36429. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  36430. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  36431. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  36432. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  36433. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  36434. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  36435. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  36436. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  36437. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  36438. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  36439. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  36440. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  36441. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  36442. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  36443. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  36444. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  36445. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  36446. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  36447. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  36448. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  36449. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  36450. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  36451. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  36452. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  36453. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  36454. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  36455. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  36456. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  36457. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  36458. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  36459. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  36460. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  36461. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  36462. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  36463. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  36464. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  36465. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  36466. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  36467. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  36468. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  36469. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  36470. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  36471. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  36472. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  36473. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  36474. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  36475. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  36476. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  36477. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  36478. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  36479. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  36480. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  36481. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  36482. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  36483. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  36484. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  36485. };
  36486. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  36487. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  36488. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  36489. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  36490. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  36491. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  36492. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  36493. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  36494. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  36495. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  36496. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  36497. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  36498. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  36499. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  36500. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  36501. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  36502. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  36503. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  36504. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  36505. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  36506. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  36507. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  36508. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  36509. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  36510. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  36511. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  36512. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  36513. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  36514. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  36515. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  36516. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  36517. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  36518. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  36519. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  36520. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  36521. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  36522. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  36523. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  36524. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  36525. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  36526. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  36527. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  36528. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  36529. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  36530. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  36531. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  36532. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  36533. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  36534. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  36535. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  36536. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  36537. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  36538. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  36539. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  36540. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  36541. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  36542. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  36543. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  36544. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  36545. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  36546. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  36547. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  36548. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  36549. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  36550. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  36551. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  36552. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  36553. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  36554. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  36555. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  36556. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  36557. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  36558. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  36559. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  36560. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  36561. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  36562. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  36563. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  36564. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  36565. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  36566. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  36567. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  36568. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  36569. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  36570. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  36571. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  36572. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  36573. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  36574. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  36575. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  36576. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  36577. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  36578. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  36579. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  36580. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  36581. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  36582. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  36583. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  36584. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  36585. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  36586. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  36587. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  36588. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  36589. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  36590. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  36591. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  36592. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  36593. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  36594. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  36595. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  36596. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  36597. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  36598. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  36599. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  36600. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  36601. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  36602. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  36603. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  36604. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  36605. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  36606. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  36607. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  36608. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  36609. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  36610. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  36611. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  36612. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  36613. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  36614. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  36615. };
  36616. #endif
  36617. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  36618. {
  36619. wc_test_ret_t ret = 0;
  36620. int i;
  36621. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  36622. unsigned char res[SIPHASH_MAC_SIZE_16];
  36623. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  36624. SipHash siphash;
  36625. WOLFSSL_ENTER("siphash_test (1)");
  36626. for (i = 0; i < 64; i++) {
  36627. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36628. if (ret != 0)
  36629. return WC_TEST_RET_ENC_I(i);
  36630. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  36631. if (ret != 0)
  36632. return WC_TEST_RET_ENC_I(i);
  36633. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  36634. if (ret != 0)
  36635. return WC_TEST_RET_ENC_I(i);
  36636. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  36637. return WC_TEST_RET_ENC_I(i);
  36638. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  36639. if (ret != 0)
  36640. return WC_TEST_RET_ENC_I(i);
  36641. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  36642. return WC_TEST_RET_ENC_I(i);
  36643. }
  36644. for (i = 0; i < 64; i++) {
  36645. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  36646. if (ret != 0)
  36647. return WC_TEST_RET_ENC_I(i);
  36648. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  36649. if (ret != 0)
  36650. return WC_TEST_RET_ENC_I(i);
  36651. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  36652. if (ret != 0)
  36653. return WC_TEST_RET_ENC_I(i);
  36654. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  36655. return WC_TEST_RET_ENC_I(i);
  36656. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  36657. if (ret != 0)
  36658. return WC_TEST_RET_ENC_I(i);
  36659. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  36660. return WC_TEST_RET_ENC_I(i);
  36661. }
  36662. #else
  36663. WOLFSSL_ENTER("siphash_test (1)");
  36664. #endif
  36665. /* Testing bad parameters. */
  36666. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  36667. if (ret != BAD_FUNC_ARG)
  36668. return WC_TEST_RET_ENC_EC(ret);
  36669. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  36670. if (ret != BAD_FUNC_ARG)
  36671. return WC_TEST_RET_ENC_EC(ret);
  36672. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  36673. if (ret != BAD_FUNC_ARG)
  36674. return WC_TEST_RET_ENC_EC(ret);
  36675. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  36676. if (ret != BAD_FUNC_ARG)
  36677. return WC_TEST_RET_ENC_EC(ret);
  36678. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36679. if (ret != 0)
  36680. return WC_TEST_RET_ENC_EC(ret);
  36681. ret = wc_SipHashUpdate(NULL, NULL, 0);
  36682. if (ret != BAD_FUNC_ARG)
  36683. return WC_TEST_RET_ENC_EC(ret);
  36684. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  36685. if (ret != BAD_FUNC_ARG)
  36686. return WC_TEST_RET_ENC_EC(ret);
  36687. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  36688. if (ret != BAD_FUNC_ARG)
  36689. return WC_TEST_RET_ENC_EC(ret);
  36690. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  36691. if (ret != BAD_FUNC_ARG)
  36692. return WC_TEST_RET_ENC_EC(ret);
  36693. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  36694. if (ret != BAD_FUNC_ARG)
  36695. return WC_TEST_RET_ENC_EC(ret);
  36696. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  36697. if (ret != BAD_FUNC_ARG)
  36698. return WC_TEST_RET_ENC_EC(ret);
  36699. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  36700. if (ret != BAD_FUNC_ARG)
  36701. return WC_TEST_RET_ENC_EC(ret);
  36702. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  36703. if (ret != BAD_FUNC_ARG)
  36704. return WC_TEST_RET_ENC_EC(ret);
  36705. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  36706. if (ret != BAD_FUNC_ARG)
  36707. return WC_TEST_RET_ENC_EC(ret);
  36708. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  36709. if (ret != BAD_FUNC_ARG)
  36710. return WC_TEST_RET_ENC_EC(ret);
  36711. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  36712. if (ret != BAD_FUNC_ARG)
  36713. return WC_TEST_RET_ENC_EC(ret);
  36714. /* Test cache with multiple non blocksize bytes */
  36715. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36716. if (ret != 0)
  36717. return WC_TEST_RET_ENC_EC(ret);
  36718. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  36719. if (ret != 0)
  36720. return WC_TEST_RET_ENC_EC(ret);
  36721. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  36722. if (ret != 0)
  36723. return WC_TEST_RET_ENC_EC(ret);
  36724. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  36725. if (ret != 0)
  36726. return WC_TEST_RET_ENC_EC(ret);
  36727. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  36728. if (ret != 0)
  36729. return WC_TEST_RET_ENC_EC(ret);
  36730. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  36731. if (ret != 0)
  36732. return WC_TEST_RET_ENC_EC(ret);
  36733. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  36734. if (ret != 0)
  36735. return WC_TEST_RET_ENC_EC(ret);
  36736. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  36737. return WC_TEST_RET_ENC_NC;
  36738. return 0;
  36739. }
  36740. #endif /* WOLFSSL_SIPHASH */
  36741. #ifdef HAVE_LIBZ
  36742. static const byte sample_text[] =
  36743. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  36744. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  36745. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  36746. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  36747. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  36748. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  36749. "\n"
  36750. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  36751. "four loko you probably haven't heard of them high life. Messenger bag\n"
  36752. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  36753. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  36754. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  36755. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  36756. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  36757. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  36758. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  36759. "four loko whatever street art yr farm-to-table.\n"
  36760. "\n"
  36761. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  36762. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  36763. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  36764. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  36765. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  36766. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  36767. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  36768. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  36769. "locavore.\n"
  36770. "\n"
  36771. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  36772. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  36773. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  36774. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  36775. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  36776. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  36777. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  36778. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  36779. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  36780. "neutra PBR selvage.\n"
  36781. "\n"
  36782. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  36783. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  36784. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  36785. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  36786. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  36787. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  36788. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  36789. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  36790. "mollit magna, sriracha sartorial helvetica.\n"
  36791. "\n"
  36792. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  36793. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  36794. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  36795. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  36796. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  36797. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  36798. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  36799. "\n"
  36800. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  36801. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  36802. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  36803. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  36804. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  36805. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  36806. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  36807. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  36808. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  36809. "\n"
  36810. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  36811. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  36812. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  36813. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  36814. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  36815. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  36816. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  36817. "bag dolor terry richardson sapiente.\n";
  36818. static const byte sample_text_gz[] = {
  36819. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  36820. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  36821. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  36822. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  36823. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  36824. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  36825. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  36826. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  36827. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  36828. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  36829. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  36830. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  36831. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  36832. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  36833. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  36834. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  36835. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  36836. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  36837. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  36838. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  36839. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  36840. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  36841. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  36842. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  36843. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  36844. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  36845. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  36846. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  36847. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  36848. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  36849. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  36850. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  36851. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  36852. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  36853. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  36854. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  36855. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  36856. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  36857. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  36858. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  36859. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  36860. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  36861. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  36862. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  36863. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  36864. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  36865. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  36866. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  36867. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  36868. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  36869. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  36870. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  36871. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  36872. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  36873. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  36874. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  36875. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  36876. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  36877. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  36878. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  36879. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  36880. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  36881. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  36882. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  36883. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  36884. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  36885. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  36886. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  36887. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  36888. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  36889. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  36890. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  36891. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  36892. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  36893. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  36894. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  36895. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  36896. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  36897. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  36898. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  36899. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  36900. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  36901. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  36902. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  36903. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  36904. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  36905. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  36906. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  36907. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  36908. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  36909. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  36910. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  36911. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  36912. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  36913. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  36914. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  36915. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  36916. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  36917. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  36918. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  36919. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  36920. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  36921. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  36922. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  36923. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  36924. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  36925. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  36926. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  36927. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  36928. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  36929. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  36930. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  36931. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  36932. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  36933. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  36934. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  36935. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  36936. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  36937. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  36938. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  36939. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  36940. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  36941. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  36942. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  36943. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  36944. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  36945. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  36946. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  36947. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  36948. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  36949. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  36950. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  36951. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  36952. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  36953. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  36954. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  36955. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  36956. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  36957. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  36958. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  36959. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  36960. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  36961. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  36962. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  36963. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  36964. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  36965. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  36966. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  36967. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  36968. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  36969. };
  36970. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  36971. {
  36972. wc_test_ret_t ret = 0;
  36973. word32 dSz = sizeof(sample_text);
  36974. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  36975. byte *c;
  36976. byte *d;
  36977. WOLFSSL_ENTER("compress_test");
  36978. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36979. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36980. if (c == NULL || d == NULL) {
  36981. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  36982. }
  36983. /* follow calloc and initialize to 0 */
  36984. XMEMSET(c, 0, cSz);
  36985. XMEMSET(d, 0, dSz);
  36986. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  36987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36988. }
  36989. cSz = (word32)ret;
  36990. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  36991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  36992. }
  36993. dSz = (word32)ret;
  36994. if (XMEMCMP(d, sample_text, dSz) != 0) {
  36995. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  36996. }
  36997. /* GZIP tests */
  36998. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  36999. XMEMSET(c, 0, cSz);
  37000. XMEMSET(d, 0, dSz);
  37001. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  37002. if (ret < 0)
  37003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  37004. cSz = (word32)ret;
  37005. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  37006. if (ret < 0)
  37007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  37008. if (XMEMCMP(d, sample_text, dSz) != 0) {
  37009. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  37010. }
  37011. /* Try with gzip generated output */
  37012. XMEMSET(d, 0, dSz);
  37013. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  37014. LIBZ_WINBITS_GZIP);
  37015. if (ret < 0)
  37016. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  37017. dSz = (word32)ret;
  37018. if (XMEMCMP(d, sample_text, dSz) != 0) {
  37019. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  37020. }
  37021. ret = 0; /* success */
  37022. exit:
  37023. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37024. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37025. return ret;
  37026. }
  37027. #endif /* HAVE_LIBZ */
  37028. #ifdef HAVE_PKCS7
  37029. /* External Debugging/Testing Note:
  37030. *
  37031. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  37032. * additional testing. To dump bundles to files DER encoded files, please
  37033. * define:
  37034. *
  37035. * #define PKCS7_OUTPUT_TEST_BUNDLES
  37036. */
  37037. /* Loads certs and keys for use with PKCS7 tests, from either files
  37038. * or buffers.
  37039. *
  37040. * rsaClientCertBuf - output buffer for RSA client cert
  37041. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  37042. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  37043. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  37044. *
  37045. * rsaServerCertBuf - output buffer for RSA server cert
  37046. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  37047. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  37048. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  37049. *
  37050. * rsaCaCertBuf - output buffer for RSA CA cert
  37051. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  37052. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  37053. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  37054. *
  37055. * eccClientCertBuf - output buffer for ECC cert
  37056. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  37057. * eccClientPrivKeyBuf - output buffer for ECC private key
  37058. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  37059. *
  37060. * Returns 0 on success, negative on error
  37061. */
  37062. static wc_test_ret_t pkcs7_load_certs_keys(
  37063. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  37064. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  37065. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  37066. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  37067. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  37068. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  37069. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  37070. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  37071. {
  37072. #ifndef NO_FILESYSTEM
  37073. XFILE certFile;
  37074. XFILE keyFile;
  37075. (void)certFile;
  37076. (void)keyFile;
  37077. #endif
  37078. #ifndef NO_RSA
  37079. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  37080. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  37081. return BAD_FUNC_ARG;
  37082. #endif
  37083. #ifdef HAVE_ECC
  37084. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  37085. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  37086. return BAD_FUNC_ARG;
  37087. #endif
  37088. /* RSA */
  37089. #ifndef NO_RSA
  37090. #ifdef USE_CERT_BUFFERS_1024
  37091. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  37092. return WC_TEST_RET_ENC_NC;
  37093. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  37094. sizeof_client_cert_der_1024);
  37095. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  37096. if (rsaServerCertBuf != NULL) {
  37097. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  37098. return WC_TEST_RET_ENC_NC;
  37099. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  37100. sizeof_server_cert_der_1024);
  37101. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  37102. }
  37103. if (rsaCaCertBuf != NULL) {
  37104. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  37105. return WC_TEST_RET_ENC_NC;
  37106. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  37107. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  37108. }
  37109. #elif defined(USE_CERT_BUFFERS_2048)
  37110. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  37111. return WC_TEST_RET_ENC_NC;
  37112. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  37113. sizeof_client_cert_der_2048);
  37114. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  37115. if (rsaServerCertBuf != NULL) {
  37116. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  37117. return WC_TEST_RET_ENC_NC;
  37118. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  37119. sizeof_server_cert_der_2048);
  37120. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  37121. }
  37122. if (rsaCaCertBuf != NULL) {
  37123. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  37124. return WC_TEST_RET_ENC_NC;
  37125. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  37126. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  37127. }
  37128. #else
  37129. certFile = XFOPEN(clientCert, "rb");
  37130. if (!certFile)
  37131. return WC_TEST_RET_ENC_ERRNO;
  37132. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  37133. *rsaClientCertBufSz, certFile);
  37134. XFCLOSE(certFile);
  37135. if (*rsaClientCertBufSz == 0)
  37136. return WC_TEST_RET_ENC_ERRNO;
  37137. if (rsaServerCertBuf != NULL) {
  37138. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  37139. if (!certFile)
  37140. return WC_TEST_RET_ENC_ERRNO;
  37141. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  37142. *rsaServerCertBufSz, certFile);
  37143. XFCLOSE(certFile);
  37144. if (*rsaServerCertBufSz == 0)
  37145. return WC_TEST_RET_ENC_ERRNO;
  37146. }
  37147. if (rsaCaCertBuf != NULL) {
  37148. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  37149. if (!certFile)
  37150. return WC_TEST_RET_ENC_ERRNO;
  37151. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  37152. certFile);
  37153. XFCLOSE(certFile);
  37154. if (*rsaCaCertBufSz == 0)
  37155. return WC_TEST_RET_ENC_ERRNO;
  37156. }
  37157. #endif
  37158. #ifdef USE_CERT_BUFFERS_1024
  37159. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  37160. return WC_TEST_RET_ENC_NC;
  37161. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  37162. sizeof_client_key_der_1024);
  37163. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  37164. if (rsaServerPrivKeyBuf != NULL) {
  37165. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  37166. return WC_TEST_RET_ENC_NC;
  37167. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  37168. sizeof_server_key_der_1024);
  37169. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  37170. }
  37171. if (rsaCaPrivKeyBuf != NULL) {
  37172. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  37173. return WC_TEST_RET_ENC_NC;
  37174. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  37175. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  37176. }
  37177. #elif defined(USE_CERT_BUFFERS_2048)
  37178. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  37179. return WC_TEST_RET_ENC_NC;
  37180. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  37181. sizeof_client_key_der_2048);
  37182. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  37183. if (rsaServerPrivKeyBuf != NULL) {
  37184. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  37185. return WC_TEST_RET_ENC_NC;
  37186. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  37187. sizeof_server_key_der_2048);
  37188. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  37189. }
  37190. if (rsaCaPrivKeyBuf != NULL) {
  37191. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  37192. return WC_TEST_RET_ENC_NC;
  37193. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  37194. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  37195. }
  37196. #else
  37197. keyFile = XFOPEN(clientKey, "rb");
  37198. if (!keyFile)
  37199. return WC_TEST_RET_ENC_ERRNO;
  37200. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  37201. *rsaClientPrivKeyBufSz, keyFile);
  37202. XFCLOSE(keyFile);
  37203. if (*rsaClientPrivKeyBufSz == 0)
  37204. return WC_TEST_RET_ENC_ERRNO;
  37205. if (rsaServerPrivKeyBuf != NULL) {
  37206. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  37207. if (!keyFile)
  37208. return WC_TEST_RET_ENC_ERRNO;
  37209. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  37210. *rsaServerPrivKeyBufSz, keyFile);
  37211. XFCLOSE(keyFile);
  37212. if (*rsaServerPrivKeyBufSz == 0)
  37213. return WC_TEST_RET_ENC_ERRNO;
  37214. }
  37215. if (rsaCaPrivKeyBuf != NULL) {
  37216. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  37217. if (!keyFile)
  37218. return WC_TEST_RET_ENC_ERRNO;
  37219. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  37220. *rsaCaPrivKeyBufSz, keyFile);
  37221. XFCLOSE(keyFile);
  37222. if (*rsaCaPrivKeyBufSz == 0)
  37223. return WC_TEST_RET_ENC_ERRNO;
  37224. }
  37225. #endif /* USE_CERT_BUFFERS */
  37226. #endif /* NO_RSA */
  37227. /* ECC */
  37228. #ifdef HAVE_ECC
  37229. #ifdef USE_CERT_BUFFERS_256
  37230. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  37231. return WC_TEST_RET_ENC_NC;
  37232. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  37233. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  37234. #else
  37235. certFile = XFOPEN(eccClientCert, "rb");
  37236. if (!certFile)
  37237. return WC_TEST_RET_ENC_ERRNO;
  37238. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  37239. *eccClientCertBufSz, certFile);
  37240. XFCLOSE(certFile);
  37241. if (*eccClientCertBufSz == 0)
  37242. return WC_TEST_RET_ENC_ERRNO;
  37243. #endif /* USE_CERT_BUFFERS_256 */
  37244. #ifdef USE_CERT_BUFFERS_256
  37245. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  37246. return WC_TEST_RET_ENC_NC;
  37247. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  37248. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  37249. #else
  37250. keyFile = XFOPEN(eccClientKey, "rb");
  37251. if (!keyFile)
  37252. return WC_TEST_RET_ENC_ERRNO;
  37253. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  37254. *eccClientPrivKeyBufSz, keyFile);
  37255. XFCLOSE(keyFile);
  37256. if (*eccClientPrivKeyBufSz == 0)
  37257. return WC_TEST_RET_ENC_ERRNO;
  37258. #endif /* USE_CERT_BUFFERS_256 */
  37259. #endif /* HAVE_ECC */
  37260. #ifdef NO_RSA
  37261. (void)rsaClientCertBuf;
  37262. (void)rsaClientCertBufSz;
  37263. (void)rsaClientPrivKeyBuf;
  37264. (void)rsaClientPrivKeyBufSz;
  37265. (void)rsaServerCertBuf;
  37266. (void)rsaServerCertBufSz;
  37267. (void)rsaServerPrivKeyBuf;
  37268. (void)rsaServerPrivKeyBufSz;
  37269. (void)rsaCaCertBuf;
  37270. (void)rsaCaCertBufSz;
  37271. (void)rsaCaPrivKeyBuf;
  37272. (void)rsaCaPrivKeyBufSz;
  37273. #endif
  37274. #ifndef HAVE_ECC
  37275. (void)eccClientCertBuf;
  37276. (void)eccClientCertBufSz;
  37277. (void)eccClientPrivKeyBuf;
  37278. (void)eccClientPrivKeyBufSz;
  37279. #endif
  37280. #ifndef NO_FILESYSTEM
  37281. (void)certFile;
  37282. (void)keyFile;
  37283. #endif
  37284. return 0;
  37285. }
  37286. typedef struct {
  37287. const byte* content;
  37288. word32 contentSz;
  37289. int contentOID;
  37290. int encryptOID;
  37291. int keyWrapOID;
  37292. int keyAgreeOID;
  37293. byte* cert;
  37294. size_t certSz;
  37295. byte* privateKey;
  37296. word32 privateKeySz;
  37297. byte* optionalUkm;
  37298. word32 optionalUkmSz;
  37299. int ktriOptions; /* KTRI options flags */
  37300. int kariOptions; /* KARI options flags */
  37301. /* KEKRI specific */
  37302. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  37303. word32 secretKeySz; /* size of secretKey, bytes */
  37304. const byte* secretKeyId; /* key identifier */
  37305. word32 secretKeyIdSz; /* size of key identifier, bytes */
  37306. void* timePtr; /* time_t pointer */
  37307. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  37308. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  37309. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  37310. word32 otherAttrSz; /* size of otherAttr, bytes */
  37311. int kekriOptions; /* KEKRI options flags */
  37312. /* PWRI specific */
  37313. const char* password;
  37314. word32 passwordSz;
  37315. const byte* salt;
  37316. word32 saltSz;
  37317. int kdfOID;
  37318. int hashOID;
  37319. int kdfIterations;
  37320. int pwriOptions; /* PWRI options flags */
  37321. /* ORI specific */
  37322. int isOri;
  37323. int oriOptions; /* ORI options flags */
  37324. const char* outFileName;
  37325. } pkcs7EnvelopedVector;
  37326. static const byte asnDataOid[] = {
  37327. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  37328. };
  37329. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  37330. * and giving wolfCrypt the value for oriOID and oriValue to place in
  37331. * OtherRecipientInfo.
  37332. *
  37333. * Returns 0 on success, negative upon error. */
  37334. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  37335. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  37336. void* ctx)
  37337. {
  37338. int i;
  37339. /* make sure buffers are large enough */
  37340. if (*oriValueSz < (2 + cekSz))
  37341. return WC_TEST_RET_ENC_NC;
  37342. if (*oriTypeSz < sizeof(asnDataOid))
  37343. return WC_TEST_RET_ENC_NC;
  37344. /* our simple encryption algorithm will be take the bitwise complement */
  37345. oriValue[0] = 0x04; /*ASN OCTET STRING */
  37346. oriValue[1] = (byte)cekSz; /* length */
  37347. for (i = 0; i < (int)cekSz; i++) {
  37348. oriValue[2 + i] = ~cek[i];
  37349. }
  37350. *oriValueSz = 2 + cekSz;
  37351. /* set oriType to ASN.1 encoded data OID */
  37352. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  37353. *oriTypeSz = sizeof(asnDataOid);
  37354. (void)pkcs7;
  37355. (void)ctx;
  37356. return 0;
  37357. }
  37358. /* ORI decrypt callback, responsible for providing a decrypted content
  37359. * encryption key (CEK) placed into decryptedKey and size placed into
  37360. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  37361. * in decrypting the encrypted CEK.
  37362. *
  37363. * Returns 0 on success, negative upon error. */
  37364. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  37365. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  37366. word32* decryptedKeySz, void* ctx)
  37367. {
  37368. int i;
  37369. /* make sure oriType matches what we expect */
  37370. if (oriTypeSz != sizeof(asnDataOid))
  37371. return WC_TEST_RET_ENC_NC;
  37372. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  37373. return WC_TEST_RET_ENC_NC;
  37374. /* make sure decrypted buffer is large enough */
  37375. if (*decryptedKeySz < oriValueSz)
  37376. return WC_TEST_RET_ENC_NC;
  37377. /* decrypt encrypted CEK using simple bitwise complement,
  37378. only for example */
  37379. for (i = 0; i < (int)oriValueSz - 2; i++) {
  37380. decryptedKey[i] = ~oriValue[2 + i];
  37381. }
  37382. *decryptedKeySz = oriValueSz - 2;
  37383. (void)pkcs7;
  37384. (void)ctx;
  37385. return 0;
  37386. }
  37387. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  37388. /* returns 0 on success */
  37389. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  37390. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  37391. byte* in, int inSz, byte* out, void* usrCtx)
  37392. {
  37393. wc_test_ret_t ret;
  37394. int keyId = -1, keySz;
  37395. word32 keyIdSz = 8;
  37396. const byte* key;
  37397. byte keyIdRaw[8];
  37398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37399. Aes *aes;
  37400. #else
  37401. Aes aes[1];
  37402. #endif
  37403. /* looking for KEY ID
  37404. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  37405. */
  37406. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  37407. /* 0x06, 0x0B do not pass in tag and length */
  37408. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  37409. 0x01, 0x09, 0x10, 0x02, 0x25
  37410. };
  37411. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  37412. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37413. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37414. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37415. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  37416. };
  37417. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  37418. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  37419. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  37420. };
  37421. /* test user context passed in */
  37422. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  37423. return WC_TEST_RET_ENC_NC;
  37424. }
  37425. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37426. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  37427. return WC_TEST_RET_ENC_ERRNO;
  37428. #endif
  37429. /* if needing to find keyIdSz can call with NULL */
  37430. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  37431. &keyIdSz);
  37432. if (ret != LENGTH_ONLY_E) {
  37433. printf("Unexpected error %d when getting keyIdSz\n", ret);
  37434. printf("Possibly no KEY ID attribute set\n");
  37435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37436. }
  37437. else {
  37438. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  37439. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  37440. &keyIdSz);
  37441. if (ret < 0) {
  37442. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37443. }
  37444. if (keyIdSz < 3) {
  37445. printf("keyIdSz is smaller than expected\n");
  37446. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37447. }
  37448. if (keyIdSz > 2 + sizeof(int)) {
  37449. printf("example case was only expecting a keyId of int size\n");
  37450. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37451. }
  37452. /* keyIdRaw[0] OCTET TAG */
  37453. /* keyIdRaw[1] Length */
  37454. #ifdef BIG_ENDIAN_ORDER
  37455. if (keyIdRaw[1] == 0x01) {
  37456. keyId = 1;
  37457. }
  37458. #else
  37459. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  37460. #endif
  37461. }
  37462. /* Use keyID here if found to select key and decrypt in HSM or in this
  37463. * example just select key and do software decryption */
  37464. if (keyId == 1) {
  37465. key = altKey;
  37466. keySz = sizeof(altKey);
  37467. }
  37468. else {
  37469. key = defKey;
  37470. keySz = sizeof(defKey);
  37471. }
  37472. switch (encryptOID) {
  37473. #ifdef WOLFSSL_AES_256
  37474. case AES256CBCb:
  37475. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  37476. WARNING_OUT(BAD_FUNC_ARG, out);
  37477. break;
  37478. #endif
  37479. #ifdef WOLFSSL_AES_128
  37480. case AES128CBCb:
  37481. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  37482. ERROR_OUT(BAD_FUNC_ARG, out);
  37483. break;
  37484. #endif
  37485. default:
  37486. printf("Unsupported content cipher type for example");
  37487. ERROR_OUT(ALGO_ID_E, out);
  37488. };
  37489. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  37490. if (ret == 0) {
  37491. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  37492. if (ret == 0)
  37493. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  37494. wc_AesFree(aes);
  37495. }
  37496. out:
  37497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37498. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37499. #endif
  37500. (void)aad;
  37501. (void)aadSz;
  37502. (void)authTag;
  37503. (void)authTagSz;
  37504. return (int)ret;
  37505. }
  37506. #endif /* !NO_AES && HAVE_AES_CBC */
  37507. #define PKCS7_BUF_SIZE 2048
  37508. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  37509. byte* rsaPrivKey, word32 rsaPrivKeySz,
  37510. byte* eccCert, word32 eccCertSz,
  37511. byte* eccPrivKey, word32 eccPrivKeySz)
  37512. {
  37513. wc_test_ret_t ret = 0;
  37514. int testSz = 0, i;
  37515. int envelopedSz, decodedSz;
  37516. byte *enveloped = NULL;
  37517. byte *decoded = NULL;
  37518. PKCS7* pkcs7 = NULL;
  37519. #ifdef ECC_TIMING_RESISTANT
  37520. WC_RNG rng;
  37521. #endif
  37522. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  37523. XFILE pkcs7File;
  37524. #endif
  37525. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  37526. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  37527. 0x72,0x6c,0x64
  37528. };
  37529. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  37530. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  37531. byte optionalUkm[] = {
  37532. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  37533. };
  37534. #endif /* NO_AES */
  37535. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  37536. !defined(NO_SHA)
  37537. /* encryption key for kekri recipient types */
  37538. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  37539. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37540. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  37541. };
  37542. /* encryption key identifier */
  37543. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  37544. 0x02,0x02,0x03,0x04
  37545. };
  37546. #endif
  37547. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  37548. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  37549. #ifndef HAVE_FIPS
  37550. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  37551. #else
  37552. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  37553. #endif
  37554. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  37555. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  37556. };
  37557. #endif
  37558. #define MAX_TESTVECTORS_LEN 13
  37559. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  37560. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  37561. if (testSz == MAX_TESTVECTORS_LEN) { \
  37562. ret = WC_TEST_RET_ENC_NC; \
  37563. goto out; \
  37564. } \
  37565. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  37566. }
  37567. pkcs7EnvelopedVector *testVectors = NULL;
  37568. #ifdef ECC_TIMING_RESISTANT
  37569. XMEMSET(&rng, 0, sizeof(rng));
  37570. #endif
  37571. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  37572. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37573. if (testVectors == NULL) {
  37574. ret = WC_TEST_RET_ENC_ERRNO;
  37575. goto out;
  37576. }
  37577. {
  37578. /* key transport key encryption technique */
  37579. #ifndef NO_RSA
  37580. #ifndef NO_DES3
  37581. ADD_PKCS7ENVELOPEDVECTOR(
  37582. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  37583. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  37584. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37585. "pkcs7envelopedDataDES3.der");
  37586. #endif
  37587. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  37588. #ifdef WOLFSSL_AES_128
  37589. ADD_PKCS7ENVELOPEDVECTOR(
  37590. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  37591. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  37592. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37593. "pkcs7envelopedDataAES128CBC.der");
  37594. #endif
  37595. #ifdef WOLFSSL_AES_192
  37596. ADD_PKCS7ENVELOPEDVECTOR(
  37597. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  37598. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  37599. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37600. "pkcs7envelopedDataAES192CBC.der");
  37601. #endif
  37602. #ifdef WOLFSSL_AES_256
  37603. ADD_PKCS7ENVELOPEDVECTOR(
  37604. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  37605. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  37606. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37607. "pkcs7envelopedDataAES256CBC.der");
  37608. /* explicitly using SKID for SubjectKeyIdentifier */
  37609. ADD_PKCS7ENVELOPEDVECTOR(
  37610. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  37611. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  37612. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37613. "pkcs7envelopedDataAES256CBC_SKID.der");
  37614. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  37615. ADD_PKCS7ENVELOPEDVECTOR(
  37616. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  37617. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  37618. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  37619. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  37620. #endif
  37621. #endif /* !NO_AES && HAVE_AES_CBC */
  37622. #endif
  37623. /* key agreement key encryption technique*/
  37624. #ifdef HAVE_ECC
  37625. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  37626. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37627. ADD_PKCS7ENVELOPEDVECTOR(
  37628. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  37629. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37630. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  37631. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37632. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  37633. #endif
  37634. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  37635. ADD_PKCS7ENVELOPEDVECTOR(
  37636. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  37637. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37638. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  37639. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37640. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  37641. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  37642. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  37643. ADD_PKCS7ENVELOPEDVECTOR(
  37644. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  37645. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37646. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  37647. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37648. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  37649. /* with optional user keying material (ukm) */
  37650. ADD_PKCS7ENVELOPEDVECTOR(
  37651. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  37652. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  37653. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  37654. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37655. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  37656. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  37657. #endif /* !NO_AES && HAVE_AES_CBC */
  37658. #endif
  37659. /* kekri (KEKRecipientInfo) recipient types */
  37660. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  37661. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37662. ADD_PKCS7ENVELOPEDVECTOR(
  37663. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  37664. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  37665. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  37666. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  37667. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  37668. #endif
  37669. #endif /* !NO_AES && HAVE_AES_CBC */
  37670. /* pwri (PasswordRecipientInfo) recipient types */
  37671. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  37672. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  37673. ADD_PKCS7ENVELOPEDVECTOR(
  37674. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  37675. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  37676. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  37677. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  37678. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  37679. #endif
  37680. #endif
  37681. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  37682. /* ori (OtherRecipientInfo) recipient types */
  37683. ADD_PKCS7ENVELOPEDVECTOR(
  37684. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  37685. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  37686. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  37687. #endif
  37688. };
  37689. #undef MAX_TESTVECTORS_LEN
  37690. #undef ADD_PKCS7ENVELOPEDVECTOR
  37691. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37692. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37693. if ((! enveloped) || (! decoded)) {
  37694. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37695. }
  37696. #ifdef ECC_TIMING_RESISTANT
  37697. #ifndef HAVE_FIPS
  37698. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37699. #else
  37700. ret = wc_InitRng(&rng);
  37701. #endif
  37702. if (ret != 0)
  37703. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37704. #endif
  37705. for (i = 0; i < testSz; i++) {
  37706. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  37707. #ifdef WOLFSSL_ASYNC_CRYPT
  37708. INVALID_DEVID /* async PKCS7 is not supported */
  37709. #else
  37710. devId
  37711. #endif
  37712. );
  37713. if (pkcs7 == NULL) {
  37714. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37715. }
  37716. if (testVectors[i].secretKey != NULL) {
  37717. /* KEKRI recipient type */
  37718. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37719. if (ret != 0)
  37720. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37721. pkcs7->content = (byte*)testVectors[i].content;
  37722. pkcs7->contentSz = testVectors[i].contentSz;
  37723. pkcs7->contentOID = testVectors[i].contentOID;
  37724. pkcs7->encryptOID = testVectors[i].encryptOID;
  37725. pkcs7->ukm = testVectors[i].optionalUkm;
  37726. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37727. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  37728. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  37729. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  37730. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  37731. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  37732. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  37733. if (ret < 0) {
  37734. wc_PKCS7_Free(pkcs7);
  37735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37736. }
  37737. /* set key, for decryption */
  37738. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  37739. testVectors[i].secretKeySz);
  37740. if (ret != 0) {
  37741. wc_PKCS7_Free(pkcs7);
  37742. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37743. }
  37744. } else if (testVectors[i].password != NULL) {
  37745. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  37746. /* PWRI recipient type */
  37747. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37748. if (ret != 0)
  37749. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37750. pkcs7->content = (byte*)testVectors[i].content;
  37751. pkcs7->contentSz = testVectors[i].contentSz;
  37752. pkcs7->contentOID = testVectors[i].contentOID;
  37753. pkcs7->encryptOID = testVectors[i].encryptOID;
  37754. pkcs7->ukm = testVectors[i].optionalUkm;
  37755. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37756. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  37757. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  37758. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  37759. testVectors[i].kdfOID,
  37760. testVectors[i].hashOID, testVectors[i].kdfIterations,
  37761. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  37762. if (ret < 0) {
  37763. wc_PKCS7_Free(pkcs7);
  37764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37765. }
  37766. /* set password, for decryption */
  37767. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  37768. testVectors[i].passwordSz);
  37769. if (ret < 0) {
  37770. wc_PKCS7_Free(pkcs7);
  37771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37772. }
  37773. #endif /* ! NO_PWDBASED && ! NO_SHA */
  37774. } else if (testVectors[i].isOri == 1) {
  37775. /* ORI recipient type */
  37776. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37777. if (ret != 0)
  37778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37779. pkcs7->content = (byte*)testVectors[i].content;
  37780. pkcs7->contentSz = testVectors[i].contentSz;
  37781. pkcs7->contentOID = testVectors[i].contentOID;
  37782. pkcs7->encryptOID = testVectors[i].encryptOID;
  37783. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  37784. testVectors[i].oriOptions);
  37785. if (ret < 0) {
  37786. wc_PKCS7_Free(pkcs7);
  37787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37788. }
  37789. /* set decrypt callback for decryption */
  37790. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  37791. if (ret < 0) {
  37792. wc_PKCS7_Free(pkcs7);
  37793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37794. }
  37795. } else {
  37796. /* KTRI or KARI recipient types */
  37797. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  37798. if (ret != 0)
  37799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37800. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  37801. (word32)testVectors[i].certSz);
  37802. if (ret != 0) {
  37803. wc_PKCS7_Free(pkcs7);
  37804. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37805. }
  37806. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  37807. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  37808. pkcs7->privateKey = testVectors[i].privateKey;
  37809. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  37810. pkcs7->content = (byte*)testVectors[i].content;
  37811. pkcs7->contentSz = testVectors[i].contentSz;
  37812. pkcs7->contentOID = testVectors[i].contentOID;
  37813. pkcs7->encryptOID = testVectors[i].encryptOID;
  37814. pkcs7->ukm = testVectors[i].optionalUkm;
  37815. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  37816. /* set SubjectIdentifier type for KTRI types */
  37817. if (testVectors[i].ktriOptions & CMS_SKID) {
  37818. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  37819. if (ret != 0) {
  37820. wc_PKCS7_Free(pkcs7);
  37821. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37822. }
  37823. } else if (testVectors[i].ktriOptions &
  37824. CMS_ISSUER_AND_SERIAL_NUMBER) {
  37825. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  37826. CMS_ISSUER_AND_SERIAL_NUMBER);
  37827. if (ret != 0) {
  37828. wc_PKCS7_Free(pkcs7);
  37829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37830. }
  37831. }
  37832. }
  37833. #ifdef ECC_TIMING_RESISTANT
  37834. pkcs7->rng = &rng;
  37835. #endif
  37836. /* encode envelopedData */
  37837. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  37838. PKCS7_BUF_SIZE);
  37839. if (envelopedSz <= 0) {
  37840. wc_PKCS7_Free(pkcs7);
  37841. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  37842. }
  37843. /* decode envelopedData */
  37844. pkcs7->contentOID = 0;
  37845. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  37846. decoded, PKCS7_BUF_SIZE);
  37847. if (pkcs7->contentOID != testVectors[i].contentOID ||
  37848. decodedSz <= 0) {
  37849. wc_PKCS7_Free(pkcs7);
  37850. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37851. }
  37852. /* test decode result */
  37853. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  37854. wc_PKCS7_Free(pkcs7);
  37855. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37856. }
  37857. #ifndef NO_PKCS7_STREAM
  37858. { /* test reading byte by byte */
  37859. int z;
  37860. for (z = 0; z < envelopedSz; z++) {
  37861. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  37862. decoded, PKCS7_BUF_SIZE);
  37863. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  37864. printf("unexpected error %d\n", decodedSz);
  37865. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  37866. }
  37867. }
  37868. /* test decode result */
  37869. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  37870. printf("stream read compare failed\n");
  37871. wc_PKCS7_Free(pkcs7);
  37872. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37873. }
  37874. }
  37875. #endif
  37876. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  37877. /* output pkcs7 envelopedData for external testing */
  37878. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  37879. if (!pkcs7File) {
  37880. wc_PKCS7_Free(pkcs7);
  37881. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37882. }
  37883. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  37884. XFCLOSE(pkcs7File);
  37885. if (ret != envelopedSz) {
  37886. wc_PKCS7_Free(pkcs7);
  37887. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37888. } else {
  37889. /* reset ret to 0 for success */
  37890. ret = 0;
  37891. }
  37892. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  37893. wc_PKCS7_Free(pkcs7);
  37894. pkcs7 = NULL;
  37895. }
  37896. #ifdef ECC_TIMING_RESISTANT
  37897. wc_FreeRng(&rng);
  37898. #endif
  37899. (void)eccCert;
  37900. (void)eccCertSz;
  37901. (void)eccPrivKey;
  37902. (void)eccPrivKeySz;
  37903. (void)rsaCert;
  37904. (void)rsaCertSz;
  37905. (void)rsaPrivKey;
  37906. (void)rsaPrivKeySz;
  37907. out:
  37908. if (testVectors)
  37909. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37910. if (enveloped)
  37911. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37912. if (decoded)
  37913. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37914. return ret;
  37915. }
  37916. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  37917. {
  37918. wc_test_ret_t ret = 0;
  37919. byte* rsaCert = NULL;
  37920. byte* rsaPrivKey = NULL;
  37921. word32 rsaCertSz = 0;
  37922. word32 rsaPrivKeySz = 0;
  37923. byte* eccCert = NULL;
  37924. byte* eccPrivKey = NULL;
  37925. word32 eccCertSz = 0;
  37926. word32 eccPrivKeySz = 0;
  37927. WOLFSSL_ENTER("pkcs7enveloped_test");
  37928. #ifndef NO_RSA
  37929. /* read client RSA cert and key in DER format */
  37930. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37931. if (rsaCert == NULL)
  37932. return WC_TEST_RET_ENC_ERRNO;
  37933. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37934. if (rsaPrivKey == NULL) {
  37935. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37936. return WC_TEST_RET_ENC_NC;
  37937. }
  37938. rsaCertSz = FOURK_BUF;
  37939. rsaPrivKeySz = FOURK_BUF;
  37940. #endif /* NO_RSA */
  37941. #ifdef HAVE_ECC
  37942. /* read client ECC cert and key in DER format */
  37943. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37944. if (eccCert == NULL) {
  37945. #ifndef NO_RSA
  37946. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37947. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37948. #endif
  37949. return WC_TEST_RET_ENC_NC;
  37950. }
  37951. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37952. if (eccPrivKey == NULL) {
  37953. #ifndef NO_RSA
  37954. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37955. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37956. #endif
  37957. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37958. return WC_TEST_RET_ENC_NC;
  37959. }
  37960. eccCertSz = FOURK_BUF;
  37961. eccPrivKeySz = FOURK_BUF;
  37962. #endif /* HAVE_ECC */
  37963. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  37964. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  37965. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  37966. eccPrivKey, &eccPrivKeySz);
  37967. if (ret < 0) {
  37968. #ifndef NO_RSA
  37969. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37970. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37971. #endif
  37972. #ifdef HAVE_ECC
  37973. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37974. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37975. #endif
  37976. return WC_TEST_RET_ENC_EC(ret);
  37977. }
  37978. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  37979. rsaPrivKey, (word32)rsaPrivKeySz,
  37980. eccCert, (word32)eccCertSz,
  37981. eccPrivKey, (word32)eccPrivKeySz);
  37982. #ifndef NO_RSA
  37983. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37984. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37985. #endif
  37986. #ifdef HAVE_ECC
  37987. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37988. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37989. #endif
  37990. return ret;
  37991. }
  37992. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  37993. typedef struct {
  37994. const byte* content;
  37995. word32 contentSz;
  37996. int contentOID;
  37997. int encryptOID;
  37998. int keyWrapOID;
  37999. int keyAgreeOID;
  38000. byte* cert;
  38001. size_t certSz;
  38002. byte* privateKey;
  38003. word32 privateKeySz;
  38004. PKCS7Attrib* authAttribs;
  38005. word32 authAttribsSz;
  38006. PKCS7Attrib* unauthAttribs;
  38007. word32 unauthAttribsSz;
  38008. /* KARI / KTRI specific */
  38009. byte* optionalUkm;
  38010. word32 optionalUkmSz;
  38011. int ktriOptions; /* KTRI options flags */
  38012. int kariOptions; /* KARI options flags */
  38013. /* KEKRI specific */
  38014. byte* secretKey; /* key, only for kekri RecipientInfo types */
  38015. word32 secretKeySz; /* size of secretKey, bytes */
  38016. byte* secretKeyId; /* key identifier */
  38017. word32 secretKeyIdSz; /* size of key identifier, bytes */
  38018. void* timePtr; /* time_t pointer */
  38019. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  38020. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  38021. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  38022. word32 otherAttrSz; /* size of otherAttr, bytes */
  38023. int kekriOptions; /* KEKRI options flags */
  38024. /* PWRI specific */
  38025. char* password; /* password */
  38026. word32 passwordSz; /* password size, bytes */
  38027. byte* salt; /* KDF salt */
  38028. word32 saltSz; /* KDF salt size, bytes */
  38029. int kdfOID; /* KDF OID */
  38030. int hashOID; /* KDF hash algorithm OID */
  38031. int kdfIterations; /* KDF iterations */
  38032. int kekEncryptOID; /* KEK encryption algorithm OID */
  38033. int pwriOptions; /* PWRI options flags */
  38034. /* ORI specific */
  38035. int isOri;
  38036. int oriOptions; /* ORI options flags */
  38037. const char* outFileName;
  38038. } pkcs7AuthEnvelopedVector;
  38039. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  38040. byte* rsaPrivKey, word32 rsaPrivKeySz,
  38041. byte* eccCert, word32 eccCertSz,
  38042. byte* eccPrivKey, word32 eccPrivKeySz)
  38043. {
  38044. wc_test_ret_t ret = 0;
  38045. int testSz = 0, i;
  38046. int envelopedSz, decodedSz;
  38047. byte *enveloped = NULL;
  38048. byte *decoded = NULL;
  38049. WC_RNG rng;
  38050. PKCS7* pkcs7;
  38051. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38052. XFILE pkcs7File;
  38053. #endif
  38054. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  38055. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38056. 0x72,0x6c,0x64
  38057. };
  38058. byte senderNonce[PKCS7_NONCE_SZ + 2];
  38059. #ifdef HAVE_ECC
  38060. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  38061. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  38062. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  38063. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  38064. 0x09, 0x05 };
  38065. PKCS7Attrib attribs[] =
  38066. {
  38067. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  38068. sizeof(senderNonce) }
  38069. };
  38070. #endif
  38071. #endif
  38072. #endif
  38073. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  38074. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  38075. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  38076. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  38077. };
  38078. #endif /* NO_AES */
  38079. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  38080. /* encryption key for kekri recipient types */
  38081. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  38082. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38083. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  38084. };
  38085. /* encryption key identifier */
  38086. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  38087. 0x02,0x02,0x03,0x04
  38088. };
  38089. #endif
  38090. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  38091. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  38092. #ifndef HAVE_FIPS
  38093. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  38094. #else
  38095. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  38096. #endif
  38097. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  38098. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  38099. };
  38100. #endif
  38101. #define MAX_TESTVECTORS_LEN 20
  38102. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  38103. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  38104. if (testSz == MAX_TESTVECTORS_LEN) { \
  38105. ret = WC_TEST_RET_ENC_NC; \
  38106. goto out; \
  38107. } \
  38108. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  38109. sizeof _this_vector); \
  38110. }
  38111. pkcs7AuthEnvelopedVector *testVectors = NULL;
  38112. XMEMSET(&rng, 0, sizeof(rng));
  38113. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  38114. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38115. if (testVectors == NULL) {
  38116. ret = WC_TEST_RET_ENC_ERRNO;
  38117. goto out;
  38118. }
  38119. {
  38120. /* key transport key encryption technique */
  38121. #ifndef NO_RSA
  38122. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  38123. #ifdef WOLFSSL_AES_128
  38124. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38125. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  38126. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  38127. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  38128. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  38129. #endif
  38130. #ifdef WOLFSSL_AES_192
  38131. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38132. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  38133. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  38134. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  38135. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  38136. #endif
  38137. #ifdef WOLFSSL_AES_256
  38138. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38139. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  38140. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  38141. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  38142. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  38143. /* test with contentType set to FirmwarePkgData */
  38144. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38145. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  38146. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  38147. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  38148. 0, 0, 0, 0, 0, 0, 0, 0,
  38149. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  38150. /* explicitly using SKID for SubjectKeyIdentifier */
  38151. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38152. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  38153. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  38154. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  38155. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  38156. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  38157. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38158. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  38159. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  38160. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  38161. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  38162. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  38163. #endif
  38164. #else /* NO_AES || !HAVE_AESGCM */
  38165. (void)rsaCert;
  38166. (void)rsaCertSz;
  38167. (void)rsaPrivKey;
  38168. (void)rsaPrivKeySz;
  38169. #endif /* NO_AES || !HAVE_AESGCM */
  38170. #endif
  38171. /* key agreement key encryption technique*/
  38172. #ifdef HAVE_ECC
  38173. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  38174. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  38175. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38176. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  38177. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38178. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  38179. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  38180. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  38181. #endif
  38182. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  38183. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38184. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38185. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38186. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  38187. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  38188. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  38189. /* with authenticated attributes */
  38190. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38191. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38192. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38193. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  38194. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  38195. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  38196. 0, 0, 0,
  38197. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  38198. /* with unauthenticated attributes */
  38199. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38200. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38201. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38202. eccPrivKeySz, NULL, 0, attribs,
  38203. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  38204. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  38205. 0, 0, 0,
  38206. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  38207. /* with authenticated AND unauthenticated attributes */
  38208. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38209. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38210. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38211. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  38212. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  38213. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  38214. 0, 0, 0, 0, 0, 0,
  38215. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  38216. /* with authenticated AND unauthenticated attributes AND
  38217. * contentType of FirmwarePkgData */
  38218. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38219. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  38220. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38221. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  38222. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  38223. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  38224. 0, 0, 0, 0, 0, 0,
  38225. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  38226. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  38227. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  38228. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38229. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38230. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38231. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  38232. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  38233. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  38234. /* with optional user keying material (ukm) */
  38235. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38236. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  38237. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  38238. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  38239. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  38240. 0, 0, 0, 0, 0, 0,
  38241. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  38242. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  38243. #endif /* NO_AES */
  38244. #endif
  38245. /* kekri (KEKRecipientInfo) recipient types */
  38246. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  38247. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  38248. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38249. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  38250. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  38251. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  38252. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  38253. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  38254. #endif
  38255. #endif
  38256. /* pwri (PasswordRecipientInfo) recipient types */
  38257. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  38258. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  38259. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38260. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  38261. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  38262. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  38263. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  38264. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  38265. #endif
  38266. #endif
  38267. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  38268. #ifdef WOLFSSL_AES_128
  38269. /* ori (OtherRecipientInfo) recipient types */
  38270. ADD_PKCS7AUTHENVELOPEDVECTOR(
  38271. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  38272. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  38273. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  38274. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  38275. #endif
  38276. #endif
  38277. }
  38278. #undef MAX_TESTVECTORS_LEN
  38279. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  38280. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38281. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38282. if ((! enveloped) || (! decoded)) {
  38283. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38284. }
  38285. /* generate senderNonce */
  38286. {
  38287. #ifndef HAVE_FIPS
  38288. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38289. #else
  38290. ret = wc_InitRng(&rng);
  38291. #endif
  38292. if (ret != 0)
  38293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38294. senderNonce[0] = 0x04;
  38295. senderNonce[1] = PKCS7_NONCE_SZ;
  38296. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  38297. if (ret != 0) {
  38298. wc_FreeRng(&rng);
  38299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38300. }
  38301. }
  38302. for (i = 0; i < testSz; i++) {
  38303. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  38304. #ifdef WOLFSSL_ASYNC_CRYPT
  38305. INVALID_DEVID /* async PKCS7 is not supported */
  38306. #else
  38307. devId
  38308. #endif
  38309. );
  38310. if (pkcs7 == NULL) {
  38311. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38312. }
  38313. if (testVectors[i].secretKey != NULL) {
  38314. /* KEKRI recipient type */
  38315. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  38316. if (ret != 0)
  38317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38318. pkcs7->content = (byte*)testVectors[i].content;
  38319. pkcs7->contentSz = testVectors[i].contentSz;
  38320. pkcs7->contentOID = testVectors[i].contentOID;
  38321. pkcs7->encryptOID = testVectors[i].encryptOID;
  38322. pkcs7->ukm = testVectors[i].optionalUkm;
  38323. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  38324. pkcs7->authAttribs = testVectors[i].authAttribs;
  38325. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  38326. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  38327. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  38328. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  38329. testVectors[i].secretKey, testVectors[i].secretKeySz,
  38330. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  38331. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  38332. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  38333. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  38334. if (ret < 0) {
  38335. wc_PKCS7_Free(pkcs7);
  38336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38337. }
  38338. /* set key, for decryption */
  38339. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  38340. testVectors[i].secretKeySz);
  38341. if (ret != 0) {
  38342. wc_PKCS7_Free(pkcs7);
  38343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38344. }
  38345. } else if (testVectors[i].password != NULL) {
  38346. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  38347. /* PWRI recipient type */
  38348. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  38349. if (ret != 0)
  38350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38351. pkcs7->content = (byte*)testVectors[i].content;
  38352. pkcs7->contentSz = testVectors[i].contentSz;
  38353. pkcs7->contentOID = testVectors[i].contentOID;
  38354. pkcs7->encryptOID = testVectors[i].encryptOID;
  38355. pkcs7->ukm = testVectors[i].optionalUkm;
  38356. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  38357. pkcs7->authAttribs = testVectors[i].authAttribs;
  38358. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  38359. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  38360. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  38361. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  38362. (byte*)testVectors[i].password,
  38363. testVectors[i].passwordSz, testVectors[i].salt,
  38364. testVectors[i].saltSz, testVectors[i].kdfOID,
  38365. testVectors[i].hashOID, testVectors[i].kdfIterations,
  38366. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  38367. if (ret < 0) {
  38368. wc_PKCS7_Free(pkcs7);
  38369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38370. }
  38371. /* set password, for decryption */
  38372. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  38373. testVectors[i].passwordSz);
  38374. if (ret < 0) {
  38375. wc_PKCS7_Free(pkcs7);
  38376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38377. }
  38378. #endif /* ! NO_PWDBASED && ! NO_SHA */
  38379. } else if (testVectors[i].isOri == 1) {
  38380. /* ORI recipient type */
  38381. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  38382. if (ret != 0)
  38383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38384. pkcs7->content = (byte*)testVectors[i].content;
  38385. pkcs7->contentSz = testVectors[i].contentSz;
  38386. pkcs7->contentOID = testVectors[i].contentOID;
  38387. pkcs7->encryptOID = testVectors[i].encryptOID;
  38388. pkcs7->authAttribs = testVectors[i].authAttribs;
  38389. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  38390. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  38391. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  38392. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  38393. testVectors[i].oriOptions);
  38394. if (ret < 0) {
  38395. wc_PKCS7_Free(pkcs7);
  38396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38397. }
  38398. /* set decrypt callback for decryption */
  38399. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  38400. if (ret < 0) {
  38401. wc_PKCS7_Free(pkcs7);
  38402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38403. }
  38404. } else {
  38405. /* KTRI or KARI recipient types */
  38406. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  38407. (word32)testVectors[i].certSz);
  38408. if (ret != 0) {
  38409. wc_PKCS7_Free(pkcs7);
  38410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38411. }
  38412. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  38413. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  38414. pkcs7->privateKey = testVectors[i].privateKey;
  38415. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  38416. pkcs7->content = (byte*)testVectors[i].content;
  38417. pkcs7->contentSz = testVectors[i].contentSz;
  38418. pkcs7->contentOID = testVectors[i].contentOID;
  38419. pkcs7->encryptOID = testVectors[i].encryptOID;
  38420. pkcs7->ukm = testVectors[i].optionalUkm;
  38421. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  38422. pkcs7->authAttribs = testVectors[i].authAttribs;
  38423. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  38424. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  38425. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  38426. /* set SubjectIdentifier type for KTRI types */
  38427. if (testVectors[i].ktriOptions & CMS_SKID) {
  38428. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  38429. if (ret != 0) {
  38430. wc_PKCS7_Free(pkcs7);
  38431. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38432. }
  38433. } else if (testVectors[i].ktriOptions &
  38434. CMS_ISSUER_AND_SERIAL_NUMBER) {
  38435. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  38436. CMS_ISSUER_AND_SERIAL_NUMBER);
  38437. if (ret != 0) {
  38438. wc_PKCS7_Free(pkcs7);
  38439. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38440. }
  38441. }
  38442. }
  38443. #ifdef ECC_TIMING_RESISTANT
  38444. pkcs7->rng = &rng;
  38445. #endif
  38446. /* encode envelopedData */
  38447. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  38448. PKCS7_BUF_SIZE);
  38449. if (envelopedSz <= 0) {
  38450. wc_PKCS7_Free(pkcs7);
  38451. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  38452. }
  38453. #ifndef NO_PKCS7_STREAM
  38454. { /* test reading byte by byte */
  38455. int z;
  38456. for (z = 0; z < envelopedSz; z++) {
  38457. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  38458. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  38459. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  38460. printf("unexpected error %d\n", decodedSz);
  38461. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  38462. }
  38463. }
  38464. /* test decode result */
  38465. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  38466. printf("stream read compare failed\n");
  38467. wc_PKCS7_Free(pkcs7);
  38468. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38469. }
  38470. }
  38471. #endif
  38472. /* decode envelopedData */
  38473. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  38474. envelopedSz, decoded,
  38475. PKCS7_BUF_SIZE);
  38476. if (decodedSz <= 0) {
  38477. wc_PKCS7_Free(pkcs7);
  38478. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  38479. }
  38480. /* test decode result */
  38481. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  38482. wc_PKCS7_Free(pkcs7);
  38483. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38484. }
  38485. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38486. /* output pkcs7 envelopedData for external testing */
  38487. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  38488. if (!pkcs7File) {
  38489. wc_PKCS7_Free(pkcs7);
  38490. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38491. }
  38492. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  38493. XFCLOSE(pkcs7File);
  38494. if (ret != envelopedSz) {
  38495. wc_PKCS7_Free(pkcs7);
  38496. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38497. } else {
  38498. /* reset ret to 0 for success */
  38499. ret = 0;
  38500. }
  38501. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  38502. wc_PKCS7_Free(pkcs7);
  38503. pkcs7 = NULL;
  38504. }
  38505. wc_FreeRng(&rng);
  38506. (void)eccCert;
  38507. (void)eccCertSz;
  38508. (void)eccPrivKey;
  38509. (void)eccPrivKeySz;
  38510. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  38511. (void)secretKey;
  38512. (void)secretKeyId;
  38513. #endif
  38514. #ifdef NO_RSA
  38515. (void)rsaCert;
  38516. (void)rsaCertSz;
  38517. (void)rsaPrivKey;
  38518. (void)rsaPrivKeySz;
  38519. #endif
  38520. out:
  38521. if (testVectors)
  38522. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38523. if (enveloped)
  38524. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38525. if (decoded)
  38526. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38527. return ret;
  38528. }
  38529. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  38530. {
  38531. wc_test_ret_t ret = 0;
  38532. byte* rsaCert = NULL;
  38533. byte* rsaPrivKey = NULL;
  38534. word32 rsaCertSz = 0;
  38535. word32 rsaPrivKeySz = 0;
  38536. byte* eccCert = NULL;
  38537. byte* eccPrivKey = NULL;
  38538. word32 eccCertSz = 0;
  38539. word32 eccPrivKeySz = 0;
  38540. WOLFSSL_ENTER("pkcs7authenveloped_test");
  38541. #ifndef NO_RSA
  38542. /* read client RSA cert and key in DER format */
  38543. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38544. if (rsaCert == NULL)
  38545. return WC_TEST_RET_ENC_ERRNO;
  38546. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38547. if (rsaPrivKey == NULL) {
  38548. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38549. return WC_TEST_RET_ENC_NC;
  38550. }
  38551. rsaCertSz = FOURK_BUF;
  38552. rsaPrivKeySz = FOURK_BUF;
  38553. #endif /* NO_RSA */
  38554. #ifdef HAVE_ECC
  38555. /* read client ECC cert and key in DER format */
  38556. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38557. if (eccCert == NULL) {
  38558. #ifndef NO_RSA
  38559. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38560. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38561. #endif
  38562. return WC_TEST_RET_ENC_NC;
  38563. }
  38564. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38565. if (eccPrivKey == NULL) {
  38566. #ifndef NO_RSA
  38567. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38568. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38569. #endif
  38570. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38571. return WC_TEST_RET_ENC_NC;
  38572. }
  38573. eccCertSz = FOURK_BUF;
  38574. eccPrivKeySz = FOURK_BUF;
  38575. #endif /* HAVE_ECC */
  38576. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  38577. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  38578. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  38579. eccPrivKey, &eccPrivKeySz);
  38580. if (ret < 0) {
  38581. #ifndef NO_RSA
  38582. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38583. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38584. #endif
  38585. #ifdef HAVE_ECC
  38586. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38587. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38588. #endif
  38589. return WC_TEST_RET_ENC_EC(ret);
  38590. }
  38591. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  38592. rsaPrivKey, (word32)rsaPrivKeySz,
  38593. eccCert, (word32)eccCertSz,
  38594. eccPrivKey, (word32)eccPrivKeySz);
  38595. #ifndef NO_RSA
  38596. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38597. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38598. #endif
  38599. #ifdef HAVE_ECC
  38600. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38601. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38602. #endif
  38603. return ret;
  38604. }
  38605. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  38606. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  38607. static const byte p7DefKey[] = {
  38608. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38609. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38610. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38611. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  38612. };
  38613. static const byte p7AltKey[] = {
  38614. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  38615. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  38616. };
  38617. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  38618. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  38619. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  38620. {
  38621. wc_test_ret_t ret;
  38622. if (cek == NULL || out == NULL)
  38623. return BAD_FUNC_ARG;
  38624. /* test case sanity checks */
  38625. if (keyIdSz != 1) {
  38626. return WC_TEST_RET_ENC_NC;
  38627. }
  38628. if (keyId[0] != 0x00) {
  38629. return WC_TEST_RET_ENC_NC;
  38630. }
  38631. if (type != (int)PKCS7_KEKRI) {
  38632. return WC_TEST_RET_ENC_NC;
  38633. }
  38634. switch (keyWrapAlgo) {
  38635. case AES256_WRAP:
  38636. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  38637. out, outSz, NULL);
  38638. if (ret <= 0)
  38639. return (int)ret;
  38640. break;
  38641. default:
  38642. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  38643. return BAD_KEYWRAP_ALG_E;
  38644. };
  38645. (void)pkcs7;
  38646. (void)direction;
  38647. (void)orginKey; /* used with KAKRI */
  38648. (void)orginKeySz;
  38649. return (int)ret;
  38650. }
  38651. /* returns key size on success */
  38652. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  38653. {
  38654. wc_test_ret_t ret;
  38655. word32 atrSz;
  38656. byte atr[256];
  38657. /* Additionally can look for fwWrappedFirmwareKey
  38658. * 1.2.840.113529.1.9.16.1.16 */
  38659. const unsigned char fwWrappedFirmwareKey[] = {
  38660. /* 0x06, 0x0B */
  38661. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38662. 0x01, 0x09, 0x10, 0x02, 0x27
  38663. };
  38664. /* find keyID in fwWrappedFirmwareKey */
  38665. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  38666. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  38667. if (ret == LENGTH_ONLY_E) {
  38668. XMEMSET(atr, 0, sizeof(atr));
  38669. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  38670. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  38671. /* keyIdRaw[0] OCTET TAG */
  38672. /* keyIdRaw[1] Length */
  38673. if (ret > 0) {
  38674. PKCS7* envPkcs7;
  38675. envPkcs7 = wc_PKCS7_New(NULL, 0);
  38676. if (envPkcs7 == NULL) {
  38677. return MEMORY_E;
  38678. }
  38679. wc_PKCS7_Init(envPkcs7, NULL, 0);
  38680. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  38681. if (ret == 0) {
  38682. /* expecting FIRMWARE_PKG_DATA content */
  38683. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  38684. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  38685. key, keySz);
  38686. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  38687. /* the contentOID should have been set to the inner
  38688. * FIRMWARE_PKG_DATA content */
  38689. ret = BAD_STATE_E;
  38690. }
  38691. }
  38692. wc_PKCS7_Free(envPkcs7);
  38693. }
  38694. }
  38695. return ret;
  38696. }
  38697. /* create a KEKRI enveloped data
  38698. * return size on success */
  38699. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  38700. word32 outSz)
  38701. {
  38702. wc_test_ret_t ret;
  38703. PKCS7* pkcs7;
  38704. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  38705. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  38706. if (pkcs7 == NULL)
  38707. return WC_TEST_RET_ENC_ERRNO;
  38708. pkcs7->content = in;
  38709. pkcs7->contentSz = inSz;
  38710. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  38711. pkcs7->encryptOID = AES256CBCb;
  38712. pkcs7->ukm = NULL;
  38713. pkcs7->ukmSz = 0;
  38714. /* add recipient (KEKRI type) */
  38715. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  38716. sizeof(p7DefKey), (byte*)keyId,
  38717. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  38718. if (ret < 0) {
  38719. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  38720. wc_PKCS7_Free(pkcs7);
  38721. return WC_TEST_RET_ENC_EC(ret);
  38722. }
  38723. /* encode envelopedData, returns size */
  38724. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  38725. if (ret <= 0) {
  38726. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  38727. wc_PKCS7_Free(pkcs7);
  38728. return WC_TEST_RET_ENC_EC(ret);
  38729. }
  38730. wc_PKCS7_Free(pkcs7);
  38731. return ret;
  38732. }
  38733. /*
  38734. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  38735. * returns size of buffer output on success
  38736. */
  38737. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  38738. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  38739. byte* key, word32 keySz)
  38740. {
  38741. wc_test_ret_t ret;
  38742. int attribNum = 1;
  38743. PKCS7* pkcs7;
  38744. /* KEY ID
  38745. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  38746. */
  38747. const unsigned char fwDecryptKeyID[] = {
  38748. 0x06, 0x0B,
  38749. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38750. 0x01, 0x09, 0x10, 0x02, 0x25
  38751. };
  38752. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  38753. const unsigned char fwWrappedFirmwareKey[] = {
  38754. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38755. 0x01, 0x09, 0x10, 0x02, 0x27
  38756. };
  38757. byte keyID[] = { 0x04, 0x01, 0x00 };
  38758. byte env[256];
  38759. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  38760. PKCS7Attrib attribs[] =
  38761. {
  38762. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  38763. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  38764. };
  38765. keyID[2] = keyHint;
  38766. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  38767. if (keyHint == 0) {
  38768. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  38769. sizeof(env));
  38770. if (ret <= 0) {
  38771. return ret;
  38772. }
  38773. attribs[1].valueSz = (int)ret;
  38774. attribNum++;
  38775. }
  38776. /* init PKCS7 */
  38777. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  38778. if (pkcs7 == NULL)
  38779. return WC_TEST_RET_ENC_ERRNO;
  38780. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  38781. if (ret != 0) {
  38782. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  38783. wc_PKCS7_Free(pkcs7);
  38784. return WC_TEST_RET_ENC_EC(ret);
  38785. }
  38786. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  38787. if (ret != 0) {
  38788. wc_PKCS7_Free(pkcs7);
  38789. return WC_TEST_RET_ENC_EC(ret);
  38790. }
  38791. /* encode Signed Encrypted FirmwarePkgData */
  38792. if (encryptKeySz == 16) {
  38793. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  38794. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  38795. (byte*)data, sizeof(data), NULL, 0,
  38796. attribs, attribNum, out, *outSz);
  38797. }
  38798. else {
  38799. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  38800. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  38801. (byte*)data, sizeof(data), NULL, 0,
  38802. attribs, attribNum, out, *outSz);
  38803. }
  38804. if (ret <= 0) {
  38805. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  38806. "ret = %d\n", ret);
  38807. wc_PKCS7_Free(pkcs7);
  38808. return WC_TEST_RET_ENC_EC(ret);
  38809. } else {
  38810. *outSz = (int)ret;
  38811. }
  38812. wc_PKCS7_Free(pkcs7);
  38813. return ret;
  38814. }
  38815. /* test verification and decryption of PKCS7 bundle
  38816. * return 0 on success
  38817. */
  38818. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  38819. {
  38820. wc_test_ret_t ret = 0;
  38821. int usrCtx = 1; /* test value to pass as user context to callback */
  38822. PKCS7* pkcs7 = NULL;
  38823. byte* sid = NULL;
  38824. word32 sidSz;
  38825. byte key[256];
  38826. word32 keySz = sizeof(key);
  38827. byte *decoded = NULL;
  38828. int decodedSz = FOURK_BUF/2;
  38829. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  38830. #ifdef NO_SHA
  38831. #ifdef USE_CERT_BUFFERS_1024
  38832. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  38833. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  38834. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  38835. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  38836. #else
  38837. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  38838. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  38839. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  38840. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  38841. #endif
  38842. #else /* !NO_SHA */
  38843. #ifdef USE_CERT_BUFFERS_1024
  38844. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  38845. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  38846. 0x10, 0x69, 0x59, 0xec,
  38847. #else
  38848. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  38849. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  38850. 0xD7, 0x85, 0x65, 0xC0
  38851. #endif
  38852. #endif /* !NO_SHA */
  38853. };
  38854. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38855. if (decoded == NULL) {
  38856. ret = MEMORY_E;
  38857. goto out;
  38858. }
  38859. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  38860. if (pkcs7 == NULL) {
  38861. ret = MEMORY_E;
  38862. goto out;
  38863. }
  38864. /* Test verify */
  38865. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  38866. if (ret != 0)
  38867. goto out;
  38868. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  38869. if (ret != 0)
  38870. goto out;
  38871. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  38872. if (ret != 0)
  38873. goto out;
  38874. /* Get size of SID and print it out */
  38875. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  38876. if (ret != LENGTH_ONLY_E)
  38877. goto out;
  38878. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38879. if (sid == NULL) {
  38880. ret = MEMORY_E;
  38881. goto out;
  38882. }
  38883. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  38884. if (ret != 0)
  38885. goto out;
  38886. ret = XMEMCMP(sid, expectedSid, sidSz);
  38887. if (ret != 0) {
  38888. ret = PKCS7_NO_SIGNER_E; /* close enough */
  38889. goto out;
  38890. }
  38891. /* get expected fwWrappedFirmwareKey */
  38892. if (keyHint == 0) {
  38893. ret = getFirmwareKey(pkcs7, key, keySz);
  38894. if (ret < 0)
  38895. goto out;
  38896. pkcs7->encryptionKey = key;
  38897. pkcs7->encryptionKeySz = (int)ret;
  38898. }
  38899. else {
  38900. decodedSz = PKCS7_BUF_SIZE;
  38901. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  38902. if (ret != 0)
  38903. goto out;
  38904. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  38905. if (ret != 0)
  38906. goto out;
  38907. }
  38908. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  38909. pkcs7->contentSz, decoded, decodedSz);
  38910. if (decodedSz < 0) {
  38911. ret = decodedSz;
  38912. goto out;
  38913. }
  38914. ret = 0;
  38915. out:
  38916. if (decoded)
  38917. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38918. if (pkcs7)
  38919. wc_PKCS7_Free(pkcs7);
  38920. if (sid)
  38921. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38922. return ret;
  38923. }
  38924. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  38925. {
  38926. wc_test_ret_t ret = 0;
  38927. word32 derSz;
  38928. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38929. WOLFSSL_ENTER("pkcs7callback_test");
  38930. if (! derBuf)
  38931. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38932. /* Doing default generation and verify */
  38933. derSz = FOURK_BUF;
  38934. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  38935. certSz, key, keySz);
  38936. if (ret <= 0) {
  38937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38938. }
  38939. ret = verifyBundle(derBuf, derSz, 0);
  38940. if (ret != 0)
  38941. ERROR_OUT(ret, out);
  38942. /* test choosing other key with keyID */
  38943. derSz = FOURK_BUF;
  38944. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  38945. cert, certSz, key, keySz);
  38946. if (ret <= 0) {
  38947. ERROR_OUT(ret, out);
  38948. }
  38949. ret = verifyBundle(derBuf, derSz, 1);
  38950. if (ret != 0)
  38951. ERROR_OUT(ret, out);
  38952. /* test fail case with wrong keyID */
  38953. derSz = FOURK_BUF;
  38954. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  38955. cert, certSz, key, keySz);
  38956. if (ret <= 0) {
  38957. ERROR_OUT(ret, out);
  38958. }
  38959. ret = verifyBundle(derBuf, derSz, 1);
  38960. if (ret == 0) {
  38961. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38962. }
  38963. ret = 0;
  38964. out:
  38965. if (derBuf)
  38966. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38967. return ret;
  38968. }
  38969. #endif /* !NO_AES && HAVE_AES_CBC */
  38970. #ifndef NO_PKCS7_ENCRYPTED_DATA
  38971. typedef struct {
  38972. const byte* content;
  38973. word32 contentSz;
  38974. int contentOID;
  38975. int encryptOID;
  38976. byte* encryptionKey;
  38977. word32 encryptionKeySz;
  38978. PKCS7Attrib* attribs;
  38979. word32 attribsSz;
  38980. const char* outFileName;
  38981. } pkcs7EncryptedVector;
  38982. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  38983. {
  38984. wc_test_ret_t ret = 0;
  38985. int i, testSz;
  38986. int encryptedSz, decodedSz, attribIdx;
  38987. PKCS7* pkcs7;
  38988. byte *encrypted;
  38989. byte *decoded;
  38990. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  38991. XFILE pkcs7File;
  38992. #endif
  38993. PKCS7Attrib* expectedAttrib;
  38994. PKCS7DecodedAttrib* decodedAttrib;
  38995. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  38996. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38997. 0x72,0x6c,0x64
  38998. };
  38999. #ifndef NO_DES3
  39000. byte desKey[] = {
  39001. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  39002. };
  39003. byte des3Key[] = {
  39004. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  39005. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  39006. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  39007. };
  39008. #endif
  39009. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39010. #ifdef WOLFSSL_AES_128
  39011. byte aes128Key[] = {
  39012. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39013. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39014. };
  39015. #endif
  39016. #ifdef WOLFSSL_AES_192
  39017. byte aes192Key[] = {
  39018. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39019. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39020. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39021. };
  39022. #endif
  39023. #ifdef WOLFSSL_AES_256
  39024. byte aes256Key[] = {
  39025. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39026. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39027. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39028. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39029. };
  39030. #endif
  39031. #ifdef WOLFSSL_AES_256
  39032. /* Attribute example from RFC 4134, Section 7.2
  39033. * OID = 1.2.5555
  39034. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  39035. static const byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  39036. static const byte genAttr[] = { 0x04, 47,
  39037. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  39038. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  39039. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  39040. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  39041. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  39042. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  39043. static const byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  39044. static const byte genAttr2[] = { 0x04, 47,
  39045. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  39046. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  39047. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  39048. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  39049. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  39050. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  39051. PKCS7Attrib attribs[] =
  39052. {
  39053. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  39054. };
  39055. PKCS7Attrib multiAttribs[] =
  39056. {
  39057. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  39058. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  39059. };
  39060. #endif
  39061. #endif /* NO_AES */
  39062. const pkcs7EncryptedVector testVectors[] =
  39063. {
  39064. #ifndef NO_DES3
  39065. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  39066. NULL, 0, "pkcs7encryptedDataDES3.der"},
  39067. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  39068. NULL, 0, "pkcs7encryptedDataDES.der"},
  39069. #endif /* NO_DES3 */
  39070. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39071. #ifdef WOLFSSL_AES_128
  39072. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  39073. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  39074. #endif
  39075. #ifdef WOLFSSL_AES_192
  39076. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  39077. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  39078. #endif
  39079. #ifdef WOLFSSL_AES_256
  39080. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  39081. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  39082. /* test with optional unprotected attributes */
  39083. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  39084. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39085. "pkcs7encryptedDataAES256CBC_attribs.der"},
  39086. /* test with multiple optional unprotected attributes */
  39087. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  39088. sizeof(aes256Key), multiAttribs,
  39089. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  39090. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  39091. /* test with contentType set to FirmwarePkgData */
  39092. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  39093. sizeof(aes256Key), NULL, 0,
  39094. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  39095. #endif
  39096. #endif /* !NO_AES && HAVE_AES_CBC */
  39097. };
  39098. WOLFSSL_ENTER("pkcs7encrypted_test");
  39099. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39100. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39101. if ((! encrypted) || (! decoded)) {
  39102. ERROR_OUT(MEMORY_E, out);
  39103. }
  39104. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  39105. for (i = 0; i < testSz; i++) {
  39106. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39107. if (pkcs7 == NULL) {
  39108. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39109. }
  39110. pkcs7->content = (byte*)testVectors[i].content;
  39111. pkcs7->contentSz = testVectors[i].contentSz;
  39112. pkcs7->contentOID = testVectors[i].contentOID;
  39113. pkcs7->encryptOID = testVectors[i].encryptOID;
  39114. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  39115. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  39116. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  39117. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  39118. /* encode encryptedData */
  39119. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  39120. PKCS7_BUF_SIZE);
  39121. if (encryptedSz <= 0) {
  39122. wc_PKCS7_Free(pkcs7);
  39123. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  39124. }
  39125. /* decode encryptedData */
  39126. #ifndef NO_PKCS7_STREAM
  39127. { /* test reading byte by byte */
  39128. int z;
  39129. for (z = 0; z < encryptedSz; z++) {
  39130. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  39131. decoded, PKCS7_BUF_SIZE);
  39132. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  39133. printf("unexpected error %d\n", decodedSz);
  39134. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  39135. }
  39136. }
  39137. /* test decode result */
  39138. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  39139. printf("stream read failed\n");
  39140. wc_PKCS7_Free(pkcs7);
  39141. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39142. }
  39143. }
  39144. #endif
  39145. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  39146. decoded, PKCS7_BUF_SIZE);
  39147. if (decodedSz <= 0){
  39148. wc_PKCS7_Free(pkcs7);
  39149. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  39150. }
  39151. /* test decode result */
  39152. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  39153. wc_PKCS7_Free(pkcs7);
  39154. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39155. }
  39156. /* verify decoded unprotected attributes */
  39157. if (pkcs7->decodedAttrib != NULL) {
  39158. decodedAttrib = pkcs7->decodedAttrib;
  39159. attribIdx = 1;
  39160. while (decodedAttrib != NULL) {
  39161. /* expected attribute, stored list is reversed */
  39162. expectedAttrib = &(pkcs7->unprotectedAttribs
  39163. [pkcs7->unprotectedAttribsSz - attribIdx]);
  39164. /* verify oid */
  39165. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  39166. decodedAttrib->oidSz) != 0) {
  39167. wc_PKCS7_Free(pkcs7);
  39168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39169. }
  39170. /* verify value */
  39171. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  39172. decodedAttrib->valueSz) != 0) {
  39173. wc_PKCS7_Free(pkcs7);
  39174. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39175. }
  39176. decodedAttrib = decodedAttrib->next;
  39177. attribIdx++;
  39178. }
  39179. }
  39180. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39181. /* output pkcs7 envelopedData for external testing */
  39182. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  39183. if (!pkcs7File) {
  39184. wc_PKCS7_Free(pkcs7);
  39185. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39186. }
  39187. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  39188. if (ret < 0)
  39189. ret = WC_TEST_RET_ENC_ERRNO;
  39190. else
  39191. ret = 0;
  39192. XFCLOSE(pkcs7File);
  39193. #endif
  39194. wc_PKCS7_Free(pkcs7);
  39195. }
  39196. out:
  39197. if (encrypted)
  39198. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39199. if (decoded)
  39200. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39201. return ret;
  39202. }
  39203. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  39204. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39205. typedef struct {
  39206. const byte* content;
  39207. word32 contentSz;
  39208. int contentOID;
  39209. const char* outFileName;
  39210. } pkcs7CompressedVector;
  39211. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  39212. {
  39213. wc_test_ret_t ret = 0;
  39214. int i, testSz;
  39215. int compressedSz, decodedSz;
  39216. PKCS7* pkcs7;
  39217. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39218. byte *compressed;
  39219. byte *decoded;
  39220. #else
  39221. byte compressed[PKCS7_BUF_SIZE];
  39222. byte decoded[PKCS7_BUF_SIZE];
  39223. #endif
  39224. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39225. XFILE pkcs7File;
  39226. #endif
  39227. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39228. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39229. 0x72,0x6c,0x64
  39230. };
  39231. const pkcs7CompressedVector testVectors[] =
  39232. {
  39233. {data, (word32)sizeof(data), DATA,
  39234. "pkcs7compressedData_data_zlib.der"},
  39235. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  39236. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  39237. };
  39238. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39239. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39240. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39241. if ((! compressed) || (! decoded)) {
  39242. ERROR_OUT(MEMORY_E, out);
  39243. }
  39244. #endif
  39245. WOLFSSL_ENTER("pkcs7compressed_test");
  39246. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  39247. for (i = 0; i < testSz; i++) {
  39248. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39249. if (pkcs7 == NULL) {
  39250. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39251. }
  39252. pkcs7->content = (byte*)testVectors[i].content;
  39253. pkcs7->contentSz = testVectors[i].contentSz;
  39254. pkcs7->contentOID = testVectors[i].contentOID;
  39255. /* encode compressedData */
  39256. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  39257. PKCS7_BUF_SIZE);
  39258. if (compressedSz <= 0) {
  39259. wc_PKCS7_Free(pkcs7);
  39260. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  39261. }
  39262. /* decode compressedData */
  39263. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  39264. compressedSz, decoded,
  39265. PKCS7_BUF_SIZE);
  39266. if (decodedSz <= 0){
  39267. wc_PKCS7_Free(pkcs7);
  39268. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  39269. }
  39270. /* test decode result */
  39271. if (XMEMCMP(decoded, testVectors[i].content,
  39272. testVectors[i].contentSz) != 0) {
  39273. wc_PKCS7_Free(pkcs7);
  39274. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39275. }
  39276. /* make sure content type is the same */
  39277. if (testVectors[i].contentOID != pkcs7->contentOID) {
  39278. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39279. }
  39280. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39281. /* output pkcs7 compressedData for external testing */
  39282. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  39283. if (!pkcs7File) {
  39284. wc_PKCS7_Free(pkcs7);
  39285. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39286. }
  39287. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  39288. if (ret < 0)
  39289. ret = WC_TEST_RET_ENC_ERRNO;
  39290. else
  39291. ret = 0;
  39292. XFCLOSE(pkcs7File);
  39293. #endif
  39294. wc_PKCS7_Free(pkcs7);
  39295. }
  39296. out:
  39297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39298. if (compressed)
  39299. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39300. if (decoded)
  39301. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39302. #endif
  39303. return ret;
  39304. } /* pkcs7compressed_test() */
  39305. #undef PKCS7_BUF_SIZE
  39306. #endif /* HAVE_LIBZ */
  39307. typedef struct {
  39308. const byte* content;
  39309. word32 contentSz;
  39310. int hashOID;
  39311. int signOID;
  39312. byte* privateKey;
  39313. word32 privateKeySz;
  39314. byte* cert;
  39315. size_t certSz;
  39316. byte* caCert;
  39317. size_t caCertSz;
  39318. PKCS7Attrib* signedAttribs;
  39319. word32 signedAttribsSz;
  39320. const char* outFileName;
  39321. int contentOID;
  39322. const byte* contentType;
  39323. word32 contentTypeSz;
  39324. int sidType;
  39325. int encryptOID; /* for single-shot encrypt alg OID */
  39326. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  39327. const byte* encryptKey; /* for single-shot, encryptedData */
  39328. word32 encryptKeySz; /* for single-shot, encryptedData */
  39329. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  39330. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  39331. word16 detachedSignature; /* generate detached signature (0:1) */
  39332. } pkcs7SignedVector;
  39333. static wc_test_ret_t pkcs7signed_run_vectors(
  39334. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  39335. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  39336. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  39337. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  39338. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  39339. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  39340. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  39341. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  39342. {
  39343. wc_test_ret_t ret;
  39344. int testSz = 0, i;
  39345. int encodedSz;
  39346. byte* out = NULL;
  39347. word32 outSz;
  39348. WC_RNG rng;
  39349. PKCS7* pkcs7 = NULL;
  39350. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39351. XFILE file;
  39352. #endif
  39353. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39354. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39355. 0x72,0x6c,0x64
  39356. };
  39357. static const byte transIdOid[] =
  39358. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  39359. 0x09, 0x07 };
  39360. static const byte messageTypeOid[] =
  39361. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  39362. 0x09, 0x02 };
  39363. static const byte senderNonceOid[] =
  39364. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  39365. 0x09, 0x05 };
  39366. #ifndef NO_SHA
  39367. byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  39368. #else
  39369. byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  39370. #endif
  39371. static const byte messageType[] = { 0x13, 2, '1', '9' };
  39372. byte senderNonce[PKCS7_NONCE_SZ + 2];
  39373. PKCS7Attrib attribs[] =
  39374. {
  39375. { transIdOid, sizeof(transIdOid), transId,
  39376. sizeof(transId) - 1 }, /* take off the null */
  39377. { messageTypeOid, sizeof(messageTypeOid), messageType,
  39378. sizeof(messageType) },
  39379. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  39380. sizeof(senderNonce) }
  39381. };
  39382. /* for testing custom contentType, FirmwarePkgData */
  39383. static const byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  39384. 0x48, 0x86, 0xF7, 0x0D,
  39385. 0x01, 0x09, 0x10, 0x01, 0x10 };
  39386. #define MAX_TESTVECTORS_LEN 20
  39387. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  39388. const pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  39389. if (testSz == MAX_TESTVECTORS_LEN) { \
  39390. ret = WC_TEST_RET_ENC_NC; \
  39391. goto out; \
  39392. } \
  39393. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  39394. sizeof _this_vector); \
  39395. }
  39396. pkcs7SignedVector *testVectors = NULL;
  39397. XMEMSET(&rng, 0, sizeof(rng));
  39398. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  39399. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39400. if (testVectors == NULL) {
  39401. ret = WC_TEST_RET_ENC_ERRNO;
  39402. goto out;
  39403. }
  39404. {
  39405. #ifndef NO_RSA
  39406. #ifndef NO_SHA
  39407. /* RSA with SHA */
  39408. ADD_PKCS7SIGNEDVECTOR(
  39409. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  39410. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39411. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39412. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  39413. 0, 0);
  39414. /* RSA with SHA, no signed attributes */
  39415. ADD_PKCS7SIGNEDVECTOR(
  39416. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  39417. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  39418. NULL, 0, NULL, 0,
  39419. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39420. NULL, 0, 0);
  39421. #endif
  39422. #ifdef WOLFSSL_SHA224
  39423. /* RSA with SHA224 */
  39424. ADD_PKCS7SIGNEDVECTOR(
  39425. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  39426. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39427. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39428. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39429. NULL, 0, 0);
  39430. #endif
  39431. #ifndef NO_SHA256
  39432. /* RSA with SHA256 */
  39433. ADD_PKCS7SIGNEDVECTOR(
  39434. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39435. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39436. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39437. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39438. NULL, 0, 0);
  39439. /* RSA with SHA256, detached signature */
  39440. ADD_PKCS7SIGNEDVECTOR(
  39441. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39442. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39443. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39444. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  39445. NULL, 0, NULL, 0, 1);
  39446. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  39447. ADD_PKCS7SIGNEDVECTOR(
  39448. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39449. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39450. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39451. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  39452. NULL, 0, NULL, 0, 0);
  39453. /* RSA with SHA256 and custom contentType */
  39454. ADD_PKCS7SIGNEDVECTOR(
  39455. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39456. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39457. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39458. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  39459. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  39460. NULL, 0, 0);
  39461. /* RSA with SHA256 and FirmwarePkgData contentType */
  39462. ADD_PKCS7SIGNEDVECTOR(
  39463. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39464. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39465. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39466. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  39467. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39468. /* RSA with SHA256 using server cert and ca cert */
  39469. ADD_PKCS7SIGNEDVECTOR(
  39470. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  39471. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  39472. rsaCaCertBuf, rsaCaCertBufSz,
  39473. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39474. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  39475. NULL, 0, NULL, 0, 0);
  39476. #endif
  39477. #if defined(WOLFSSL_SHA384)
  39478. /* RSA with SHA384 */
  39479. ADD_PKCS7SIGNEDVECTOR(
  39480. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  39481. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39482. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39483. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39484. NULL, 0, 0);
  39485. #endif
  39486. #if defined(WOLFSSL_SHA512)
  39487. /* RSA with SHA512 */
  39488. ADD_PKCS7SIGNEDVECTOR(
  39489. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  39490. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39491. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39492. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39493. NULL, 0, 0);
  39494. #endif
  39495. #endif /* NO_RSA */
  39496. #ifdef HAVE_ECC
  39497. #ifndef NO_SHA
  39498. /* ECDSA with SHA */
  39499. ADD_PKCS7SIGNEDVECTOR(
  39500. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  39501. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39502. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39503. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39504. NULL, 0, 0);
  39505. /* ECDSA with SHA, no signed attributes */
  39506. ADD_PKCS7SIGNEDVECTOR(
  39507. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  39508. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  39509. NULL, 0, NULL, 0,
  39510. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39511. NULL, 0, 0);
  39512. #endif
  39513. #ifdef WOLFSSL_SHA224
  39514. /* ECDSA with SHA224 */
  39515. ADD_PKCS7SIGNEDVECTOR(
  39516. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  39517. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39518. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39519. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39520. NULL, 0, 0);
  39521. #endif
  39522. #ifndef NO_SHA256
  39523. /* ECDSA with SHA256 */
  39524. ADD_PKCS7SIGNEDVECTOR(
  39525. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39526. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39527. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39528. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39529. NULL, 0, 0);
  39530. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  39531. ADD_PKCS7SIGNEDVECTOR(
  39532. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39533. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39534. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39535. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  39536. NULL, 0, NULL, 0, 0);
  39537. /* ECDSA with SHA256 and custom contentType */
  39538. ADD_PKCS7SIGNEDVECTOR(
  39539. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39540. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39541. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39542. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  39543. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  39544. NULL, 0, 0);
  39545. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  39546. ADD_PKCS7SIGNEDVECTOR(
  39547. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39548. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39549. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39550. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  39551. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39552. #endif
  39553. #ifdef WOLFSSL_SHA384
  39554. /* ECDSA with SHA384 */
  39555. ADD_PKCS7SIGNEDVECTOR(
  39556. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  39557. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39558. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39559. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39560. NULL, 0, 0);
  39561. #endif
  39562. #ifdef WOLFSSL_SHA512
  39563. /* ECDSA with SHA512 */
  39564. ADD_PKCS7SIGNEDVECTOR(
  39565. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  39566. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39567. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39568. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  39569. NULL, 0, 0);
  39570. #endif
  39571. #endif /* HAVE_ECC */
  39572. };
  39573. #undef MAX_TESTVECTORS_LEN
  39574. #undef ADD_PKCS7SIGNEDVECTOR
  39575. outSz = FOURK_BUF;
  39576. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39577. if (out == NULL)
  39578. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39579. XMEMSET(out, 0, outSz);
  39580. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  39581. if (ret < 0)
  39582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39583. #ifndef HAVE_FIPS
  39584. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  39585. #else
  39586. ret = wc_InitRng(&rng);
  39587. #endif
  39588. if (ret != 0)
  39589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39590. for (i = 0; i < testSz; i++) {
  39591. if (pkcs7)
  39592. wc_PKCS7_Free(pkcs7);
  39593. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39594. if (pkcs7 == NULL)
  39595. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39596. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  39597. (word32)testVectors[i].certSz);
  39598. if (ret != 0)
  39599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39600. /* load CA certificate, if present */
  39601. if (testVectors[i].caCert != NULL) {
  39602. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  39603. (word32)testVectors[i].caCertSz);
  39604. if (ret != 0)
  39605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39606. }
  39607. pkcs7->rng = &rng;
  39608. pkcs7->content = (byte*)testVectors[i].content;
  39609. pkcs7->contentSz = testVectors[i].contentSz;
  39610. pkcs7->contentOID = testVectors[i].contentOID;
  39611. pkcs7->hashOID = testVectors[i].hashOID;
  39612. pkcs7->encryptOID = testVectors[i].signOID;
  39613. pkcs7->privateKey = testVectors[i].privateKey;
  39614. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  39615. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  39616. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  39617. /* optional custom contentType, default is DATA,
  39618. overrides contentOID if set */
  39619. if (testVectors[i].contentType != NULL) {
  39620. ret = wc_PKCS7_SetContentType(pkcs7,
  39621. (byte *)testVectors[i].contentType,
  39622. testVectors[i].contentTypeSz);
  39623. if (ret != 0)
  39624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39625. }
  39626. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  39627. default is IssuerAndSerialNumber */
  39628. if (testVectors[i].sidType == CMS_SKID) {
  39629. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  39630. if (ret != 0)
  39631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39632. }
  39633. /* generate senderNonce */
  39634. {
  39635. senderNonce[0] = 0x04;
  39636. senderNonce[1] = PKCS7_NONCE_SZ;
  39637. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  39638. if (ret != 0)
  39639. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39640. }
  39641. /* generate transactionID (used with SCEP) */
  39642. {
  39643. #ifndef NO_SHA
  39644. wc_Sha sha;
  39645. byte digest[WC_SHA_DIGEST_SIZE];
  39646. #else
  39647. wc_Sha256 sha;
  39648. byte digest[WC_SHA256_DIGEST_SIZE];
  39649. #endif
  39650. int j,k;
  39651. transId[0] = 0x13;
  39652. transId[1] = sizeof(digest) * 2;
  39653. #ifndef NO_SHA
  39654. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  39655. if (ret != 0)
  39656. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39657. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  39658. wc_ShaFinal(&sha, digest);
  39659. wc_ShaFree(&sha);
  39660. #else
  39661. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  39662. if (ret != 0)
  39663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39664. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  39665. wc_Sha256Final(&sha, digest);
  39666. wc_Sha256Free(&sha);
  39667. #endif
  39668. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  39669. #if defined(WOLF_C89)
  39670. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  39671. #else
  39672. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  39673. #endif
  39674. }
  39675. }
  39676. /* enable detached signature generation, if set */
  39677. if (testVectors[i].detachedSignature == 1) {
  39678. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  39679. if (ret != 0)
  39680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39681. }
  39682. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  39683. if (encodedSz < 0)
  39684. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  39685. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39686. /* write PKCS#7 to output file for more testing */
  39687. file = XFOPEN(testVectors[i].outFileName, "wb");
  39688. if (!file) {
  39689. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39690. }
  39691. ret = (int)XFWRITE(out, 1, encodedSz, file);
  39692. XFCLOSE(file);
  39693. if (ret != (int)encodedSz)
  39694. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39695. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  39696. wc_PKCS7_Free(pkcs7);
  39697. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  39698. if (pkcs7 == NULL)
  39699. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39700. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  39701. if (testVectors[i].detachedSignature == 1) {
  39702. /* set content for verifying detached signatures */
  39703. pkcs7->content = (byte*)testVectors[i].content;
  39704. pkcs7->contentSz = testVectors[i].contentSz;
  39705. }
  39706. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  39707. if (ret < 0)
  39708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39709. /* verify contentType extracted successfully for custom content types */
  39710. if (testVectors[i].contentTypeSz > 0) {
  39711. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  39712. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39713. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  39714. pkcs7->contentTypeSz) != 0) {
  39715. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39716. }
  39717. }
  39718. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  39719. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39720. {
  39721. /* check getting signed attributes */
  39722. #ifndef NO_SHA
  39723. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  39724. #else
  39725. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  39726. #endif
  39727. const byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  39728. int oidSz = (int)sizeof(transIdOid) - 2;
  39729. int bufSz = 0;
  39730. if (testVectors[i].signedAttribs != NULL) {
  39731. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  39732. NULL, (word32*)&bufSz);
  39733. if (ret != LENGTH_ONLY_E)
  39734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39735. ret = 0;
  39736. }
  39737. if (bufSz > (int)sizeof(buf))
  39738. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39739. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  39740. buf, (word32*)&bufSz);
  39741. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  39742. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  39743. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39744. }
  39745. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39746. file = XFOPEN("./pkcs7cert.der", "wb");
  39747. if (!file)
  39748. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39749. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  39750. if (ret < 0)
  39751. ret = WC_TEST_RET_ENC_ERRNO;
  39752. else
  39753. ret = 0;
  39754. XFCLOSE(file);
  39755. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  39756. }
  39757. out:
  39758. if (pkcs7 != NULL)
  39759. wc_PKCS7_Free(pkcs7);
  39760. if (out != NULL)
  39761. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39762. if (testVectors != NULL)
  39763. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39764. wc_FreeRng(&rng);
  39765. if (ret > 0)
  39766. return 0;
  39767. (void)rsaClientCertBuf;
  39768. (void)rsaClientCertBufSz;
  39769. (void)rsaClientPrivKeyBuf;
  39770. (void)rsaClientPrivKeyBufSz;
  39771. (void)rsaServerCertBuf;
  39772. (void)rsaServerCertBufSz;
  39773. (void)rsaServerPrivKeyBuf;
  39774. (void)rsaServerPrivKeyBufSz;
  39775. (void)rsaCaCertBuf;
  39776. (void)rsaCaCertBufSz;
  39777. (void)rsaCaPrivKeyBuf;
  39778. (void)rsaCaPrivKeyBufSz;
  39779. (void)eccClientCertBuf;
  39780. (void)eccClientCertBufSz;
  39781. (void)eccClientPrivKeyBuf;
  39782. (void)eccClientPrivKeyBufSz;
  39783. return ret;
  39784. }
  39785. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  39786. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  39787. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  39788. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  39789. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  39790. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  39791. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  39792. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  39793. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  39794. {
  39795. wc_test_ret_t ret;
  39796. int testSz = 0, i;
  39797. int encodedSz;
  39798. byte* out = NULL;
  39799. word32 outSz;
  39800. WC_RNG rng;
  39801. PKCS7* pkcs7 = NULL;
  39802. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39803. XFILE file;
  39804. #endif
  39805. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  39806. !defined(NO_PKCS7_ENCRYPTED_DATA)
  39807. byte* encryptedTmp = NULL;
  39808. int encryptedTmpSz;
  39809. #endif
  39810. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39811. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39812. 0x72,0x6c,0x64
  39813. };
  39814. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39815. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39816. static const byte aes256Key[] = {
  39817. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39818. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39819. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39820. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39821. };
  39822. #endif
  39823. static const byte messageTypeOid[] =
  39824. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  39825. 0x09, 0x02 };
  39826. static const byte messageType[] = { 0x13, 2, '1', '9' };
  39827. PKCS7Attrib attribs[] =
  39828. {
  39829. { messageTypeOid, sizeof(messageTypeOid), messageType,
  39830. sizeof(messageType) },
  39831. };
  39832. #define MAX_TESTVECTORS_LEN 19
  39833. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  39834. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  39835. if (testSz == MAX_TESTVECTORS_LEN) { \
  39836. ret = WC_TEST_RET_ENC_NC; \
  39837. goto out; \
  39838. } \
  39839. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  39840. sizeof _this_vector); \
  39841. }
  39842. pkcs7SignedVector *testVectors = NULL;
  39843. XMEMSET(&rng, 0, sizeof(rng));
  39844. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  39845. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39846. if (testVectors == NULL) {
  39847. ret = WC_TEST_RET_ENC_ERRNO;
  39848. goto out;
  39849. }
  39850. {
  39851. #ifndef NO_RSA
  39852. #ifndef NO_SHA256
  39853. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  39854. ADD_PKCS7SIGNEDVECTOR(
  39855. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39856. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39857. NULL, 0,
  39858. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  39859. 0, 0, NULL, 0, NULL, 0, 0);
  39860. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  39861. ADD_PKCS7SIGNEDVECTOR(
  39862. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39863. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39864. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39865. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  39866. NULL, 0, NULL, 0, 0);
  39867. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  39868. ADD_PKCS7SIGNEDVECTOR(
  39869. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39870. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39871. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39872. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  39873. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  39874. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  39875. ADD_PKCS7SIGNEDVECTOR(
  39876. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  39877. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  39878. rsaCaCertBuf, rsaCaCertBufSz,
  39879. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39880. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  39881. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39882. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39883. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39884. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  39885. ADD_PKCS7SIGNEDVECTOR(
  39886. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39887. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39888. NULL, 0,
  39889. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  39890. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  39891. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  39892. ADD_PKCS7SIGNEDVECTOR(
  39893. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39894. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39895. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39896. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  39897. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  39898. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39899. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  39900. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39901. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  39902. ADD_PKCS7SIGNEDVECTOR(
  39903. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39904. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39905. NULL, 0,
  39906. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  39907. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39908. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  39909. ADD_PKCS7SIGNEDVECTOR(
  39910. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39911. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39912. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39913. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  39914. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39915. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39916. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  39917. no attribs */
  39918. ADD_PKCS7SIGNEDVECTOR(
  39919. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39920. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39921. NULL, 0,
  39922. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  39923. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  39924. 0, 0);
  39925. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  39926. attribs */
  39927. ADD_PKCS7SIGNEDVECTOR(
  39928. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  39929. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  39930. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39931. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  39932. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  39933. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39934. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  39935. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  39936. #endif /* NO_SHA256 */
  39937. #endif /* NO_RSA */
  39938. #ifdef HAVE_ECC
  39939. #ifndef NO_SHA256
  39940. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  39941. ADD_PKCS7SIGNEDVECTOR(
  39942. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39943. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39944. NULL, 0,
  39945. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39946. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39947. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  39948. ADD_PKCS7SIGNEDVECTOR(
  39949. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39950. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39951. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39952. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39953. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  39954. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  39955. ADD_PKCS7SIGNEDVECTOR(
  39956. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39957. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39958. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39959. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  39960. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  39961. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  39962. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  39963. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  39964. ADD_PKCS7SIGNEDVECTOR(
  39965. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39966. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39967. NULL, 0,
  39968. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39969. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  39970. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  39971. ADD_PKCS7SIGNEDVECTOR(
  39972. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39973. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39974. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39975. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39976. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  39977. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  39978. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  39979. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  39980. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  39981. ADD_PKCS7SIGNEDVECTOR(
  39982. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39983. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39984. NULL, 0,
  39985. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  39986. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39987. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  39988. ADD_PKCS7SIGNEDVECTOR(
  39989. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39990. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  39991. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  39992. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  39993. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  39994. #ifndef NO_PKCS7_ENCRYPTED_DATA
  39995. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  39996. no attribs */
  39997. ADD_PKCS7SIGNEDVECTOR(
  39998. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  39999. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  40000. NULL, 0,
  40001. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  40002. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  40003. 0, 0);
  40004. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  40005. attribs */
  40006. ADD_PKCS7SIGNEDVECTOR(
  40007. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  40008. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  40009. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  40010. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  40011. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  40012. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  40013. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  40014. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  40015. #endif /* NO_SHA256 */
  40016. #endif /* HAVE_ECC */
  40017. };
  40018. #undef MAX_TESTVECTORS_LEN
  40019. #undef ADD_PKCS7SIGNEDVECTOR
  40020. outSz = FOURK_BUF;
  40021. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40022. if (out == NULL)
  40023. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40024. XMEMSET(out, 0, outSz);
  40025. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  40026. if (ret < 0)
  40027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40028. #ifndef HAVE_FIPS
  40029. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40030. #else
  40031. ret = wc_InitRng(&rng);
  40032. #endif
  40033. if (ret != 0)
  40034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40035. for (i = 0; i < testSz; i++) {
  40036. if (pkcs7)
  40037. wc_PKCS7_Free(pkcs7);
  40038. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  40039. if (pkcs7 == NULL)
  40040. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40041. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  40042. (word32)testVectors[i].certSz);
  40043. if (ret != 0)
  40044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40045. /* load CA certificate, if present */
  40046. if (testVectors[i].caCert != NULL) {
  40047. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  40048. (word32)testVectors[i].caCertSz);
  40049. if (ret != 0)
  40050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40051. }
  40052. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  40053. default is IssuerAndSerialNumber */
  40054. if (testVectors[i].sidType == CMS_SKID) {
  40055. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  40056. if (ret != 0)
  40057. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40058. }
  40059. if (testVectors[i].encCompFlag == 0) {
  40060. /* encode Signed FirmwarePkgData */
  40061. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  40062. testVectors[i].privateKey, testVectors[i].privateKeySz,
  40063. testVectors[i].signOID, testVectors[i].hashOID,
  40064. (byte*)testVectors[i].content, testVectors[i].contentSz,
  40065. testVectors[i].signedAttribs,
  40066. testVectors[i].signedAttribsSz, out, outSz);
  40067. if (encodedSz < 0)
  40068. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  40069. #ifndef NO_PKCS7_ENCRYPTED_DATA
  40070. } else if (testVectors[i].encCompFlag == 1) {
  40071. /* encode Signed Encrypted FirmwarePkgData */
  40072. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  40073. (byte *)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  40074. testVectors[i].privateKey, testVectors[i].privateKeySz,
  40075. testVectors[i].encryptOID, testVectors[i].signOID,
  40076. testVectors[i].hashOID, (byte*)testVectors[i].content,
  40077. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  40078. testVectors[i].unprotectedAttribsSz,
  40079. testVectors[i].signedAttribs,
  40080. testVectors[i].signedAttribsSz, out, outSz);
  40081. if (encodedSz <= 0)
  40082. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  40083. #endif
  40084. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  40085. } else if (testVectors[i].encCompFlag == 2) {
  40086. /* encode Signed Compressed FirmwarePkgData */
  40087. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  40088. testVectors[i].privateKey, testVectors[i].privateKeySz,
  40089. testVectors[i].signOID, testVectors[i].hashOID,
  40090. (byte*)testVectors[i].content, testVectors[i].contentSz,
  40091. testVectors[i].signedAttribs,
  40092. testVectors[i].signedAttribsSz, out, outSz);
  40093. if (encodedSz <= 0)
  40094. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  40095. #ifndef NO_PKCS7_ENCRYPTED_DATA
  40096. } else if (testVectors[i].encCompFlag == 3) {
  40097. /* encode Signed Encrypted Compressed FirmwarePkgData */
  40098. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  40099. (byte*)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  40100. testVectors[i].privateKey, testVectors[i].privateKeySz,
  40101. testVectors[i].encryptOID, testVectors[i].signOID,
  40102. testVectors[i].hashOID, (byte*)testVectors[i].content,
  40103. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  40104. testVectors[i].unprotectedAttribsSz,
  40105. testVectors[i].signedAttribs,
  40106. testVectors[i].signedAttribsSz, out, outSz);
  40107. if (encodedSz <= 0)
  40108. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  40109. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  40110. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  40111. } else {
  40112. /* unsupported SignedData single-shot combination */
  40113. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40114. }
  40115. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40116. /* write PKCS#7 to output file for more testing */
  40117. file = XFOPEN(testVectors[i].outFileName, "wb");
  40118. if (!file)
  40119. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40120. ret = (int)XFWRITE(out, 1, encodedSz, file);
  40121. XFCLOSE(file);
  40122. file = NULL;
  40123. if (ret != (int)encodedSz)
  40124. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40125. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  40126. wc_PKCS7_Free(pkcs7);
  40127. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  40128. if (pkcs7 == NULL)
  40129. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40130. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  40131. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  40132. if (ret < 0)
  40133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40134. #ifndef NO_PKCS7_STREAM
  40135. {
  40136. word32 z;
  40137. for (z = 0; z < outSz && ret != 0; z++) {
  40138. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  40139. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  40140. printf("unexpected error %d\n", ret);
  40141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40142. }
  40143. }
  40144. }
  40145. #endif
  40146. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  40147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40148. if (testVectors[i].encCompFlag == 0) {
  40149. /* verify decoded content matches expected */
  40150. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  40151. XMEMCMP(pkcs7->content, testVectors[i].content,
  40152. pkcs7->contentSz)) {
  40153. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40154. }
  40155. }
  40156. #ifndef NO_PKCS7_ENCRYPTED_DATA
  40157. else if (testVectors[i].encCompFlag == 1) {
  40158. /* decrypt inner encryptedData */
  40159. pkcs7->encryptionKey = (byte *)testVectors[i].encryptKey;
  40160. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  40161. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  40162. pkcs7->contentSz, out, outSz);
  40163. if (ret < 0)
  40164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40165. /* compare decrypted to expected */
  40166. if (((word32)ret != testVectors[i].contentSz) ||
  40167. XMEMCMP(out, testVectors[i].content, ret))
  40168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40169. }
  40170. #endif
  40171. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  40172. else if (testVectors[i].encCompFlag == 2) {
  40173. /* decompress inner compressedData */
  40174. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  40175. pkcs7->contentSz, out, outSz);
  40176. if (ret < 0)
  40177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40178. /* compare decompressed to expected */
  40179. if (((word32)ret != testVectors[i].contentSz) ||
  40180. XMEMCMP(out, testVectors[i].content, ret))
  40181. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40182. }
  40183. #ifndef NO_PKCS7_ENCRYPTED_DATA
  40184. else if (testVectors[i].encCompFlag == 3) {
  40185. encryptedTmpSz = FOURK_BUF;
  40186. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  40187. DYNAMIC_TYPE_TMP_BUFFER);
  40188. if (encryptedTmp == NULL)
  40189. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40190. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  40191. /* decrypt inner encryptedData */
  40192. pkcs7->encryptionKey = (byte*)testVectors[i].encryptKey;
  40193. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  40194. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  40195. pkcs7->contentSz, encryptedTmp,
  40196. encryptedTmpSz);
  40197. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  40198. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40199. /* decompress inner compressedData */
  40200. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  40201. encryptedTmpSz, out, outSz);
  40202. if (ret < 0)
  40203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40204. /* compare decompressed to expected */
  40205. if (((word32)ret != testVectors[i].contentSz) ||
  40206. XMEMCMP(out, testVectors[i].content, ret))
  40207. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40208. }
  40209. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  40210. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  40211. }
  40212. out:
  40213. if (pkcs7 != NULL)
  40214. wc_PKCS7_Free(pkcs7);
  40215. if (out != NULL)
  40216. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40217. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  40218. !defined(NO_PKCS7_ENCRYPTED_DATA)
  40219. if (encryptedTmp != NULL)
  40220. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40221. #endif
  40222. if (testVectors != NULL)
  40223. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40224. wc_FreeRng(&rng);
  40225. if (ret > 0)
  40226. return 0;
  40227. (void)eccClientCertBuf;
  40228. (void)eccClientCertBufSz;
  40229. (void)eccClientPrivKeyBuf;
  40230. (void)eccClientPrivKeyBufSz;
  40231. (void)rsaClientCertBuf;
  40232. (void)rsaClientCertBufSz;
  40233. (void)rsaClientPrivKeyBuf;
  40234. (void)rsaClientPrivKeyBufSz;
  40235. (void)rsaServerCertBuf;
  40236. (void)rsaServerCertBufSz;
  40237. (void)rsaServerPrivKeyBuf;
  40238. (void)rsaServerPrivKeyBufSz;
  40239. (void)rsaCaCertBuf;
  40240. (void)rsaCaCertBufSz;
  40241. (void)rsaCaPrivKeyBuf;
  40242. (void)rsaCaPrivKeyBufSz;
  40243. return ret;
  40244. }
  40245. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  40246. {
  40247. wc_test_ret_t ret = 0;
  40248. byte* rsaClientCertBuf = NULL;
  40249. byte* rsaServerCertBuf = NULL;
  40250. byte* rsaCaCertBuf = NULL;
  40251. byte* eccClientCertBuf = NULL;
  40252. byte* rsaClientPrivKeyBuf = NULL;
  40253. byte* rsaServerPrivKeyBuf = NULL;
  40254. byte* rsaCaPrivKeyBuf = NULL;
  40255. byte* eccClientPrivKeyBuf = NULL;
  40256. word32 rsaClientCertBufSz = 0;
  40257. word32 rsaServerCertBufSz = 0;
  40258. word32 rsaCaCertBufSz = 0;
  40259. word32 eccClientCertBufSz = 0;
  40260. word32 rsaClientPrivKeyBufSz = 0;
  40261. word32 rsaServerPrivKeyBufSz = 0;
  40262. word32 rsaCaPrivKeyBufSz = 0;
  40263. word32 eccClientPrivKeyBufSz = 0;
  40264. WOLFSSL_ENTER("pkcs7signed_test");
  40265. #ifndef NO_RSA
  40266. /* read client RSA cert and key in DER format */
  40267. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40268. DYNAMIC_TYPE_TMP_BUFFER);
  40269. if (rsaClientCertBuf == NULL)
  40270. ret = WC_TEST_RET_ENC_NC;
  40271. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40272. DYNAMIC_TYPE_TMP_BUFFER);
  40273. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  40274. ret = WC_TEST_RET_ENC_ERRNO;
  40275. }
  40276. rsaClientCertBufSz = FOURK_BUF;
  40277. rsaClientPrivKeyBufSz = FOURK_BUF;
  40278. /* read server RSA cert and key in DER format */
  40279. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40280. DYNAMIC_TYPE_TMP_BUFFER);
  40281. if (ret == 0 && rsaServerCertBuf == NULL)
  40282. ret = WC_TEST_RET_ENC_NC;
  40283. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40284. DYNAMIC_TYPE_TMP_BUFFER);
  40285. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  40286. ret = WC_TEST_RET_ENC_ERRNO;
  40287. }
  40288. rsaServerCertBufSz = FOURK_BUF;
  40289. rsaServerPrivKeyBufSz = FOURK_BUF;
  40290. /* read CA RSA cert and key in DER format, for use with server cert */
  40291. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40292. DYNAMIC_TYPE_TMP_BUFFER);
  40293. if (ret == 0 && rsaCaCertBuf == NULL)
  40294. ret = WC_TEST_RET_ENC_NC;
  40295. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40296. DYNAMIC_TYPE_TMP_BUFFER);
  40297. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  40298. ret = WC_TEST_RET_ENC_ERRNO;
  40299. }
  40300. rsaCaCertBufSz = FOURK_BUF;
  40301. rsaCaPrivKeyBufSz = FOURK_BUF;
  40302. #endif /* NO_RSA */
  40303. #ifdef HAVE_ECC
  40304. /* read client ECC cert and key in DER format */
  40305. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40306. DYNAMIC_TYPE_TMP_BUFFER);
  40307. if (ret == 0 && eccClientCertBuf == NULL) {
  40308. ret = WC_TEST_RET_ENC_ERRNO;
  40309. }
  40310. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  40311. DYNAMIC_TYPE_TMP_BUFFER);
  40312. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  40313. ret = WC_TEST_RET_ENC_ERRNO;
  40314. }
  40315. eccClientCertBufSz = FOURK_BUF;
  40316. eccClientPrivKeyBufSz = FOURK_BUF;
  40317. #endif /* HAVE_ECC */
  40318. if (ret >= 0)
  40319. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  40320. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  40321. rsaServerCertBuf, &rsaServerCertBufSz,
  40322. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  40323. rsaCaCertBuf, &rsaCaCertBufSz,
  40324. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  40325. eccClientCertBuf, &eccClientCertBufSz,
  40326. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  40327. if (ret < 0) {
  40328. ret = WC_TEST_RET_ENC_EC(ret);
  40329. }
  40330. if (ret >= 0)
  40331. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  40332. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  40333. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  40334. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  40335. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  40336. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  40337. eccClientCertBuf, (word32)eccClientCertBufSz,
  40338. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  40339. if (ret >= 0)
  40340. ret = pkcs7signed_run_SingleShotVectors(
  40341. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  40342. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  40343. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  40344. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  40345. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  40346. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  40347. eccClientCertBuf, (word32)eccClientCertBufSz,
  40348. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  40349. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  40350. if (ret >= 0)
  40351. ret = pkcs7callback_test(
  40352. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  40353. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  40354. #endif
  40355. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40356. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40357. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40358. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40359. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40360. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40361. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40362. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40363. return ret;
  40364. }
  40365. #endif /* HAVE_PKCS7 */
  40366. #if defined(WOLFSSL_PUBLIC_MP) && \
  40367. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40368. defined(USE_FAST_MATH))
  40369. /* Maximum number of bytes in a number to test. */
  40370. #define MP_MAX_TEST_BYTE_LEN 32
  40371. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  40372. {
  40373. byte d[MP_MAX_TEST_BYTE_LEN];
  40374. wc_test_ret_t ret;
  40375. (void)heap;
  40376. do {
  40377. ret = wc_RNG_GenerateBlock(rng, d, len);
  40378. if (ret != 0)
  40379. return ret;
  40380. ret = mp_read_unsigned_bin(n, d, len);
  40381. if (ret != 0)
  40382. return ret;
  40383. } while (mp_iszero(n));
  40384. return 0;
  40385. }
  40386. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  40387. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  40388. {
  40389. int i, j;
  40390. mp_digit rem;
  40391. mp_digit rem2;
  40392. wc_test_ret_t ret;
  40393. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40394. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  40395. for (i = 0; i < 10; i++) {
  40396. for (j = 1; j < 10; j++) {
  40397. ret = randNum(a, j, rng, NULL);
  40398. if (ret != 0)
  40399. return WC_TEST_RET_ENC_EC(ret);
  40400. ret = mp_div_3(a, r, &rem);
  40401. if (ret != 0)
  40402. return WC_TEST_RET_ENC_EC(ret);
  40403. ret = mp_mul_d(r, 3, r);
  40404. if (ret != 0)
  40405. return WC_TEST_RET_ENC_EC(ret);
  40406. ret = mp_add_d(r, rem, r);
  40407. if (ret != 0)
  40408. return WC_TEST_RET_ENC_EC(ret);
  40409. ret = mp_cmp(r, a);
  40410. if (ret != MP_EQ)
  40411. return WC_TEST_RET_ENC_NC;
  40412. }
  40413. }
  40414. ret = mp_div_3(a, r, &rem);
  40415. if (ret != 0)
  40416. return WC_TEST_RET_ENC_NC;
  40417. ret = mp_div_3(a, a, NULL);
  40418. if (ret != 0)
  40419. return WC_TEST_RET_ENC_NC;
  40420. ret = mp_cmp(r, a);
  40421. if (ret != MP_EQ)
  40422. return WC_TEST_RET_ENC_NC;
  40423. #endif
  40424. #if defined(WOLFSSL_SP_MATH_ALL)
  40425. ret = mp_div_d(a, 10, r, &rem);
  40426. if (ret != 0)
  40427. return WC_TEST_RET_ENC_EC(ret);
  40428. ret = mp_div_d(a, 10, a, NULL);
  40429. if (ret != 0)
  40430. return WC_TEST_RET_ENC_EC(ret);
  40431. ret = mp_cmp(r, a);
  40432. if (ret != MP_EQ)
  40433. return WC_TEST_RET_ENC_NC;
  40434. ret = mp_div_d(a, 12, r, &rem);
  40435. if (ret != 0)
  40436. return WC_TEST_RET_ENC_EC(ret);
  40437. ret = mp_div_d(a, 12, a, NULL);
  40438. if (ret != 0)
  40439. return WC_TEST_RET_ENC_EC(ret);
  40440. ret = mp_cmp(r, a);
  40441. if (ret != MP_EQ)
  40442. return WC_TEST_RET_ENC_NC;
  40443. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  40444. if (ret != 0)
  40445. return WC_TEST_RET_ENC_EC(ret);
  40446. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  40447. if (ret != 0)
  40448. return WC_TEST_RET_ENC_EC(ret);
  40449. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  40450. if (ret != 0)
  40451. return WC_TEST_RET_ENC_EC(ret);
  40452. ret = mp_cmp(r, a);
  40453. if (ret != MP_EQ)
  40454. return WC_TEST_RET_ENC_NC;
  40455. if (rem != rem2)
  40456. return WC_TEST_RET_ENC_NC;
  40457. #endif
  40458. (void)a;
  40459. (void)r;
  40460. (void)rng;
  40461. (void)i;
  40462. (void)j;
  40463. (void)rem;
  40464. (void)rem2;
  40465. (void)ret;
  40466. return 0;
  40467. }
  40468. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  40469. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  40470. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  40471. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  40472. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  40473. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  40474. {
  40475. wc_test_ret_t ret;
  40476. int i, j;
  40477. int size;
  40478. char str[30];
  40479. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  40480. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  40481. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  40482. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  40483. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  40484. for (i = 0; i < 10; i++) {
  40485. for (j = 2; j < 12; j++) {
  40486. ret = randNum(a, j, rng, NULL);
  40487. if (ret != 0)
  40488. return WC_TEST_RET_ENC_EC(ret);
  40489. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  40490. if (ret != MP_OKAY)
  40491. return WC_TEST_RET_ENC_EC(ret);
  40492. ret = mp_toradix(a, str, MP_RADIX_DEC);
  40493. if (ret != MP_OKAY)
  40494. return WC_TEST_RET_ENC_EC(ret);
  40495. if ((int)XSTRLEN(str) != size - 1)
  40496. return WC_TEST_RET_ENC_NC;
  40497. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  40498. if (ret != MP_OKAY)
  40499. return WC_TEST_RET_ENC_EC(ret);
  40500. ret = mp_cmp(a, r);
  40501. if (ret != MP_EQ)
  40502. return WC_TEST_RET_ENC_NC;
  40503. }
  40504. }
  40505. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  40506. if (ret != MP_VAL)
  40507. return WC_TEST_RET_ENC_EC(ret);
  40508. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  40509. if (ret != MP_VAL)
  40510. return WC_TEST_RET_ENC_EC(ret);
  40511. ret = mp_read_radix(r, empty2, MP_RADIX_DEC);
  40512. if (ret != MP_OKAY)
  40513. return WC_TEST_RET_ENC_EC(ret);
  40514. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  40515. if (ret != MP_OKAY)
  40516. return WC_TEST_RET_ENC_EC(ret);
  40517. if (!mp_iszero(r))
  40518. return WC_TEST_RET_ENC_NC;
  40519. mp_set(r, 1);
  40520. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  40521. if (ret != MP_OKAY)
  40522. return WC_TEST_RET_ENC_EC(ret);
  40523. if (!mp_iszero(r))
  40524. return WC_TEST_RET_ENC_NC;
  40525. mp_zero(a);
  40526. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  40527. if (ret != 0)
  40528. return WC_TEST_RET_ENC_EC(ret);
  40529. if (size != 2)
  40530. return WC_TEST_RET_ENC_NC;
  40531. ret = mp_toradix(a, str, MP_RADIX_DEC);
  40532. if (ret != 0)
  40533. return WC_TEST_RET_ENC_EC(ret);
  40534. if ((int)XSTRLEN(str) != size - 1)
  40535. return WC_TEST_RET_ENC_NC;
  40536. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  40537. if (ret != 0)
  40538. return WC_TEST_RET_ENC_EC(ret);
  40539. if (!mp_iszero(r))
  40540. return WC_TEST_RET_ENC_NC;
  40541. return 0;
  40542. }
  40543. #endif
  40544. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  40545. defined(HAVE_ECC))
  40546. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  40547. {
  40548. wc_test_ret_t ret;
  40549. int i, j;
  40550. int size;
  40551. char str[30];
  40552. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  40553. static char longStr[2 * sizeof(a->dp) + 2];
  40554. #endif
  40555. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  40556. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  40557. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  40558. for (i = 0; i < 10; i++) {
  40559. for (j = 2; j < 12; j++) {
  40560. ret = randNum(a, j, rng, NULL);
  40561. if (ret != 0)
  40562. return WC_TEST_RET_ENC_EC(ret);
  40563. mp_radix_size(a, MP_RADIX_HEX, &size);
  40564. mp_toradix(a, str, MP_RADIX_HEX);
  40565. if ((int)XSTRLEN(str) != size - 1)
  40566. return WC_TEST_RET_ENC_NC;
  40567. mp_read_radix(r, str, MP_RADIX_HEX);
  40568. ret = mp_cmp(a, r);
  40569. if (ret != MP_EQ)
  40570. return WC_TEST_RET_ENC_NC;
  40571. }
  40572. }
  40573. ret = mp_read_radix(r, empty2, MP_RADIX_HEX);
  40574. if (ret != MP_OKAY)
  40575. return WC_TEST_RET_ENC_EC(ret);
  40576. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  40577. if (ret != MP_VAL)
  40578. return WC_TEST_RET_ENC_EC(ret);
  40579. mp_set(r, 1);
  40580. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  40581. if (ret != MP_OKAY)
  40582. return WC_TEST_RET_ENC_EC(ret);
  40583. if (!mp_iszero(r))
  40584. return WC_TEST_RET_ENC_NC;
  40585. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  40586. /* Fixed MP data size - string can be too long. */
  40587. longStr[0] = '8';
  40588. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  40589. longStr[sizeof(longStr)-1] = '\0';
  40590. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  40591. if (ret != MP_VAL)
  40592. return WC_TEST_RET_ENC_EC(ret);
  40593. #endif
  40594. mp_zero(a);
  40595. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  40596. if (ret != 0)
  40597. return WC_TEST_RET_ENC_EC(ret);
  40598. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  40599. if (size != 3)
  40600. #else
  40601. if (size != 2)
  40602. #endif
  40603. return WC_TEST_RET_ENC_NC;
  40604. ret = mp_toradix(a, str, MP_RADIX_HEX);
  40605. if (ret != 0)
  40606. return WC_TEST_RET_ENC_EC(ret);
  40607. if ((int)XSTRLEN(str) != size - 1)
  40608. return WC_TEST_RET_ENC_NC;
  40609. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  40610. if (ret != 0)
  40611. return WC_TEST_RET_ENC_EC(ret);
  40612. if (!mp_iszero(r))
  40613. return WC_TEST_RET_ENC_NC;
  40614. #ifdef WOLFSSL_SP_MATH
  40615. ret = mp_toradix(a, str, 8);
  40616. if (ret != MP_VAL)
  40617. return WC_TEST_RET_ENC_EC(ret);
  40618. ret = mp_radix_size(a, 8, &size);
  40619. if (ret != MP_VAL)
  40620. return WC_TEST_RET_ENC_EC(ret);
  40621. #endif
  40622. return 0;
  40623. }
  40624. #endif
  40625. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  40626. {
  40627. int i;
  40628. wc_test_ret_t ret;
  40629. ret = randNum(a, 4, rng, NULL);
  40630. if (ret != 0)
  40631. return WC_TEST_RET_ENC_EC(ret);
  40632. for (i = 0; i < 4; i++) {
  40633. mp_copy(r1, a);
  40634. ret = mp_lshd(r1, i);
  40635. if (ret != MP_OKAY)
  40636. return WC_TEST_RET_ENC_EC(ret);
  40637. #ifndef WOLFSSL_SP_MATH
  40638. mp_rshd(r1, i);
  40639. #else
  40640. mp_rshb(r1, i * SP_WORD_SIZE);
  40641. #endif
  40642. ret = mp_cmp(a, r1);
  40643. if (ret != MP_EQ)
  40644. return WC_TEST_RET_ENC_NC;
  40645. }
  40646. #ifndef WOLFSSL_SP_MATH
  40647. for (i = 0; i < DIGIT_BIT+1; i++) {
  40648. ret = mp_mul_2d(a, i, r1);
  40649. if (ret != MP_OKAY)
  40650. return WC_TEST_RET_ENC_EC(ret);
  40651. mp_rshb(r1, i);
  40652. ret = mp_cmp(a, r1);
  40653. if (ret != MP_EQ)
  40654. return WC_TEST_RET_ENC_NC;
  40655. }
  40656. #endif
  40657. return 0;
  40658. }
  40659. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  40660. {
  40661. int i, j;
  40662. wc_test_ret_t ret;
  40663. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  40664. mp_zero(a);
  40665. mp_set_bit(a, i);
  40666. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  40667. return WC_TEST_RET_ENC_NC;
  40668. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  40669. mp_zero(r1);
  40670. mp_set_bit(r1, i);
  40671. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  40672. if (ret != MP_OKAY)
  40673. return WC_TEST_RET_ENC_EC(ret);
  40674. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  40675. if (ret != MP_OKAY)
  40676. return WC_TEST_RET_ENC_EC(ret);
  40677. ret = mp_cmp(a, r1);
  40678. if (ret != MP_EQ)
  40679. return WC_TEST_RET_ENC_NC;
  40680. }
  40681. }
  40682. mp_zero(r1);
  40683. ret = mp_add_d(r1, 1, r1);
  40684. if (ret != MP_OKAY)
  40685. return WC_TEST_RET_ENC_EC(ret);
  40686. if (r1->used != 1)
  40687. return WC_TEST_RET_ENC_NC;
  40688. ret = mp_sub_d(r1, 1, r1);
  40689. if (ret != MP_OKAY)
  40690. return WC_TEST_RET_ENC_EC(ret);
  40691. if (r1->used != 0)
  40692. return WC_TEST_RET_ENC_NC;
  40693. return 0;
  40694. }
  40695. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  40696. {
  40697. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  40698. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  40699. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  40700. };
  40701. byte out[24];
  40702. int i, j, k;
  40703. const byte* p;
  40704. wc_test_ret_t ret;
  40705. for (i = 0; i < (int)sizeof(in); i++) {
  40706. p = in + sizeof(in) - i;
  40707. ret = mp_read_unsigned_bin(a, p, i);
  40708. if (ret != 0)
  40709. return WC_TEST_RET_ENC_EC(ret);
  40710. for (j = i; j < (int)sizeof(out); j++) {
  40711. XMEMSET(out, 0xff, sizeof(out));
  40712. ret = mp_to_unsigned_bin_len(a, out, j);
  40713. if (ret != 0)
  40714. return WC_TEST_RET_ENC_EC(ret);
  40715. for (k = 0; k < j - i; k++) {
  40716. if (out[k] != 0)
  40717. return WC_TEST_RET_ENC_NC;
  40718. }
  40719. for (; k < j; k++) {
  40720. if (out[k] != p[k - (j - i)])
  40721. return WC_TEST_RET_ENC_NC;
  40722. }
  40723. }
  40724. }
  40725. /* Length too small. */
  40726. ret = mp_to_unsigned_bin_len(a, out, 1);
  40727. if (ret != MP_VAL)
  40728. return WC_TEST_RET_ENC_EC(ret);
  40729. ret = mp_read_unsigned_bin(a, NULL, 0);
  40730. if (ret != 0)
  40731. return WC_TEST_RET_ENC_EC(ret);
  40732. if (!mp_iszero(a))
  40733. return WC_TEST_RET_ENC_NC;
  40734. return 0;
  40735. }
  40736. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  40737. static wc_test_ret_t mp_test_set_int(mp_int* a)
  40738. {
  40739. #if SP_ULONG_BITS == 64
  40740. unsigned long n = 0xfedcba9876543210UL;
  40741. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  40742. byte out[8] = { 0 };
  40743. #elif SP_ULONG_BITS == 32
  40744. unsigned long n = 0xfedcba98UL;
  40745. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  40746. byte out[4] = { 0 };
  40747. #elif SP_ULONG_BITS == 16
  40748. unsigned long n = 0xfedc;
  40749. byte exp[2] = { 0xfe, 0xdc };
  40750. byte out[2] = { 0 };
  40751. #elif SP_ULONG_BITS == 8
  40752. unsigned long n = 0xfe;
  40753. byte exp[1] = { 0xfe };
  40754. byte out[1] = { 0 };
  40755. #endif
  40756. wc_test_ret_t ret;
  40757. ret = mp_set_int(a, n);
  40758. if (ret != 0)
  40759. return WC_TEST_RET_ENC_EC(ret);
  40760. ret = mp_unsigned_bin_size(a);
  40761. if (ret != sizeof(exp))
  40762. return WC_TEST_RET_ENC_NC;
  40763. ret = mp_to_unsigned_bin(a, out);
  40764. if (ret != 0)
  40765. return WC_TEST_RET_ENC_EC(ret);
  40766. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  40767. return WC_TEST_RET_ENC_NC;
  40768. return 0;
  40769. }
  40770. #endif
  40771. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  40772. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  40773. {
  40774. byte buffer[16];
  40775. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  40776. char hexStr[] = "abcdef0123456789";
  40777. #ifndef WOLFSSL_SP_INT_NEGATIVE
  40778. char negStr[] = "-1234";
  40779. #endif
  40780. #endif
  40781. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  40782. defined(HAVE_COMP_KEY)
  40783. char decStr[] = "0987654321";
  40784. #endif
  40785. wc_test_ret_t ret;
  40786. #ifdef WOLFSSL_SP_MATH_ALL
  40787. mp_digit rho;
  40788. int size;
  40789. #endif
  40790. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  40791. int result;
  40792. #endif
  40793. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  40794. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  40795. mp_digit rd;
  40796. #endif
  40797. (void)rng;
  40798. (void)r;
  40799. ret = mp_init(NULL);
  40800. if (ret != MP_VAL)
  40801. return WC_TEST_RET_ENC_EC(ret);
  40802. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  40803. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  40804. if (ret != MP_OKAY)
  40805. return WC_TEST_RET_ENC_EC(ret);
  40806. #endif
  40807. mp_free(NULL);
  40808. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  40809. ret = mp_grow(NULL, 1);
  40810. if (ret != MP_VAL)
  40811. return WC_TEST_RET_ENC_EC(ret);
  40812. #ifdef WOLFSSL_SP_MATH
  40813. ret = mp_grow(a, SP_INT_DIGITS + 1);
  40814. if (ret != MP_MEM)
  40815. return WC_TEST_RET_ENC_EC(ret);
  40816. #endif
  40817. #endif
  40818. mp_clear(NULL);
  40819. ret = mp_abs(NULL, NULL);
  40820. if (ret != MP_VAL)
  40821. return WC_TEST_RET_ENC_EC(ret);
  40822. ret = mp_abs(a, NULL);
  40823. if (ret != MP_VAL)
  40824. return WC_TEST_RET_ENC_EC(ret);
  40825. ret = mp_abs(NULL, b);
  40826. if (ret != MP_VAL)
  40827. return WC_TEST_RET_ENC_EC(ret);
  40828. ret = mp_unsigned_bin_size(NULL);
  40829. if (ret != 0)
  40830. return WC_TEST_RET_ENC_EC(ret);
  40831. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  40832. if (ret != MP_VAL)
  40833. return WC_TEST_RET_ENC_EC(ret);
  40834. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  40835. if (ret != MP_VAL)
  40836. return WC_TEST_RET_ENC_EC(ret);
  40837. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  40838. if (ret != MP_VAL)
  40839. return WC_TEST_RET_ENC_EC(ret);
  40840. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  40841. if (ret != MP_VAL)
  40842. return WC_TEST_RET_ENC_EC(ret);
  40843. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  40844. ret = mp_read_radix(NULL, NULL, 16);
  40845. if (ret != MP_VAL)
  40846. return WC_TEST_RET_ENC_EC(ret);
  40847. ret = mp_read_radix(a, NULL, 16);
  40848. if (ret != MP_VAL)
  40849. return WC_TEST_RET_ENC_EC(ret);
  40850. ret = mp_read_radix(NULL, hexStr, 16);
  40851. if (ret != MP_VAL)
  40852. return WC_TEST_RET_ENC_EC(ret);
  40853. #ifndef WOLFSSL_SP_INT_NEGATIVE
  40854. ret = mp_read_radix(a, negStr, 16);
  40855. if (ret != MP_VAL)
  40856. return WC_TEST_RET_ENC_EC(ret);
  40857. #ifdef WOLFSSL_SP_MATH_ALL
  40858. ret = mp_read_radix(a, negStr, 10);
  40859. if (ret != MP_VAL)
  40860. return WC_TEST_RET_ENC_EC(ret);
  40861. #endif /* WOLFSSL_SP_MATH_ALL */
  40862. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  40863. #endif
  40864. #ifndef WOLFSSL_SP_MATH_ALL
  40865. /* Radix 10 only supported with ALL. */
  40866. ret = mp_read_radix(a, decStr, 10);
  40867. if (ret != MP_VAL)
  40868. return WC_TEST_RET_ENC_EC(ret);
  40869. #endif
  40870. /* Radix 8 not supported SP_INT. */
  40871. ret = mp_read_radix(a, "0123", 8);
  40872. if (ret != MP_VAL)
  40873. return WC_TEST_RET_ENC_EC(ret);
  40874. ret = mp_count_bits(NULL);
  40875. if (ret != 0)
  40876. return WC_TEST_RET_ENC_EC(ret);
  40877. ret = mp_is_bit_set(NULL, 0);
  40878. if (ret != 0)
  40879. return WC_TEST_RET_ENC_EC(ret);
  40880. ret = mp_leading_bit(NULL);
  40881. if (ret != 0)
  40882. return WC_TEST_RET_ENC_EC(ret);
  40883. mp_zero(a);
  40884. ret = mp_leading_bit(a);
  40885. if (ret != 0)
  40886. return WC_TEST_RET_ENC_EC(ret);
  40887. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  40888. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  40889. !defined(NO_RSA)
  40890. ret = mp_set_bit(NULL, 1);
  40891. if (ret != MP_VAL)
  40892. return WC_TEST_RET_ENC_EC(ret);
  40893. #endif
  40894. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  40895. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40896. ret = mp_to_unsigned_bin(NULL, NULL);
  40897. if (ret != MP_VAL)
  40898. return WC_TEST_RET_ENC_EC(ret);
  40899. ret = mp_to_unsigned_bin(a, NULL);
  40900. if (ret != MP_VAL)
  40901. return WC_TEST_RET_ENC_EC(ret);
  40902. ret = mp_to_unsigned_bin(NULL, buffer);
  40903. if (ret != MP_VAL)
  40904. return WC_TEST_RET_ENC_EC(ret);
  40905. #endif
  40906. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  40907. if (ret != MP_VAL)
  40908. return WC_TEST_RET_ENC_EC(ret);
  40909. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  40910. if (ret != MP_VAL)
  40911. return WC_TEST_RET_ENC_EC(ret);
  40912. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  40913. if (ret != MP_VAL)
  40914. return WC_TEST_RET_ENC_EC(ret);
  40915. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  40916. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40917. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  40918. if (ret != MP_VAL)
  40919. return WC_TEST_RET_ENC_EC(ret);
  40920. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  40921. if (ret != MP_VAL)
  40922. return WC_TEST_RET_ENC_EC(ret);
  40923. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  40924. if (ret != MP_VAL)
  40925. return WC_TEST_RET_ENC_EC(ret);
  40926. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  40927. if (ret != MP_OKAY)
  40928. return WC_TEST_RET_ENC_EC(ret);
  40929. #endif
  40930. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  40931. ret = mp_copy(NULL, NULL);
  40932. if (ret != MP_VAL)
  40933. return WC_TEST_RET_ENC_EC(ret);
  40934. ret = mp_copy(a, NULL);
  40935. if (ret != MP_VAL)
  40936. return WC_TEST_RET_ENC_EC(ret);
  40937. ret = mp_copy(NULL, b);
  40938. if (ret != MP_VAL)
  40939. return WC_TEST_RET_ENC_EC(ret);
  40940. #endif
  40941. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  40942. ret = sp_2expt(NULL, 1);
  40943. if (ret != MP_VAL)
  40944. return WC_TEST_RET_ENC_EC(ret);
  40945. #endif
  40946. ret = mp_set(NULL, 0);
  40947. if (ret != MP_VAL)
  40948. return WC_TEST_RET_ENC_EC(ret);
  40949. ret = mp_cmp_d(NULL, 0);
  40950. if (ret != MP_LT)
  40951. return WC_TEST_RET_ENC_EC(ret);
  40952. ret = mp_cmp(NULL, NULL);
  40953. if (ret != MP_EQ)
  40954. return WC_TEST_RET_ENC_NC;
  40955. ret = mp_cmp(a, NULL);
  40956. if (ret != MP_GT)
  40957. return WC_TEST_RET_ENC_NC;
  40958. ret = mp_cmp(NULL, b);
  40959. if (ret != MP_LT)
  40960. return WC_TEST_RET_ENC_NC;
  40961. #ifdef WOLFSSL_SP_MATH_ALL
  40962. mp_rshd(NULL, 1);
  40963. #endif
  40964. mp_zero(NULL);
  40965. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  40966. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  40967. ret = mp_lshd(NULL, 0);
  40968. if (ret != MP_VAL)
  40969. return WC_TEST_RET_ENC_EC(ret);
  40970. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  40971. if (ret != MP_VAL)
  40972. return WC_TEST_RET_ENC_EC(ret);
  40973. #endif
  40974. #if defined(WOLFSSL_SP_MATH_ALL)
  40975. ret = mp_div(NULL, NULL, a, b);
  40976. if (ret != MP_VAL)
  40977. return WC_TEST_RET_ENC_EC(ret);
  40978. ret = mp_div(a, NULL, a, b);
  40979. if (ret != MP_VAL)
  40980. return WC_TEST_RET_ENC_EC(ret);
  40981. ret = mp_div(NULL, b, a, b);
  40982. if (ret != MP_VAL)
  40983. return WC_TEST_RET_ENC_EC(ret);
  40984. ret = mp_div(a, b, NULL, NULL);
  40985. if (ret != MP_VAL)
  40986. return WC_TEST_RET_ENC_EC(ret);
  40987. #endif
  40988. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  40989. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  40990. ret = mp_mod(NULL, NULL, NULL);
  40991. if (ret != MP_VAL)
  40992. return WC_TEST_RET_ENC_EC(ret);
  40993. ret = mp_mod(a, NULL, NULL);
  40994. if (ret != MP_VAL)
  40995. return WC_TEST_RET_ENC_EC(ret);
  40996. ret = mp_mod(NULL, b, NULL);
  40997. if (ret != MP_VAL)
  40998. return WC_TEST_RET_ENC_EC(ret);
  40999. ret = mp_mod(NULL, NULL, r);
  41000. if (ret != MP_VAL)
  41001. return WC_TEST_RET_ENC_EC(ret);
  41002. ret = mp_mod(a, b, NULL);
  41003. if (ret != MP_VAL)
  41004. return WC_TEST_RET_ENC_EC(ret);
  41005. ret = mp_mod(a, NULL, r);
  41006. if (ret != MP_VAL)
  41007. return WC_TEST_RET_ENC_EC(ret);
  41008. ret = mp_mod(NULL, b, r);
  41009. if (ret != MP_VAL)
  41010. return WC_TEST_RET_ENC_EC(ret);
  41011. #endif
  41012. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  41013. ret = mp_set_int(NULL, 0);
  41014. if (ret != MP_VAL)
  41015. return WC_TEST_RET_ENC_EC(ret);
  41016. #endif
  41017. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  41018. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  41019. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  41020. if (ret != MP_VAL)
  41021. return WC_TEST_RET_ENC_EC(ret);
  41022. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  41023. if (ret != MP_VAL)
  41024. return WC_TEST_RET_ENC_EC(ret);
  41025. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  41026. if (ret != MP_VAL)
  41027. return WC_TEST_RET_ENC_EC(ret);
  41028. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  41029. if (ret != MP_VAL)
  41030. return WC_TEST_RET_ENC_EC(ret);
  41031. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  41032. if (ret != MP_VAL)
  41033. return WC_TEST_RET_ENC_EC(ret);
  41034. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  41035. if (ret != MP_VAL)
  41036. return WC_TEST_RET_ENC_EC(ret);
  41037. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  41038. if (ret != MP_VAL)
  41039. return WC_TEST_RET_ENC_EC(ret);
  41040. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  41041. if (ret != MP_VAL)
  41042. return WC_TEST_RET_ENC_EC(ret);
  41043. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  41044. if (ret != MP_VAL)
  41045. return WC_TEST_RET_ENC_EC(ret);
  41046. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  41047. if (ret != MP_VAL)
  41048. return WC_TEST_RET_ENC_EC(ret);
  41049. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  41050. if (ret != MP_VAL)
  41051. return WC_TEST_RET_ENC_EC(ret);
  41052. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  41053. if (ret != MP_VAL)
  41054. return WC_TEST_RET_ENC_EC(ret);
  41055. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  41056. if (ret != MP_VAL)
  41057. return WC_TEST_RET_ENC_EC(ret);
  41058. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  41059. if (ret != MP_VAL)
  41060. return WC_TEST_RET_ENC_EC(ret);
  41061. ret = mp_exptmod_nct(a, a, a, NULL);
  41062. if (ret != MP_VAL)
  41063. return WC_TEST_RET_ENC_EC(ret);
  41064. ret = mp_exptmod_nct(a, a, NULL, a);
  41065. if (ret != MP_VAL)
  41066. return WC_TEST_RET_ENC_EC(ret);
  41067. ret = mp_exptmod_nct(a, NULL, a, a);
  41068. if (ret != MP_VAL)
  41069. return WC_TEST_RET_ENC_EC(ret);
  41070. ret = mp_exptmod_nct(NULL, a, a, a);
  41071. if (ret != MP_VAL)
  41072. return WC_TEST_RET_ENC_EC(ret);
  41073. #endif
  41074. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  41075. !defined(WC_NO_RNG)
  41076. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  41077. if (ret != MP_VAL)
  41078. return WC_TEST_RET_ENC_EC(ret);
  41079. ret = mp_rand_prime(a, 32, NULL, NULL);
  41080. if (ret != MP_VAL)
  41081. return WC_TEST_RET_ENC_EC(ret);
  41082. ret = mp_rand_prime(NULL, 32, rng, NULL);
  41083. if (ret != MP_VAL)
  41084. return WC_TEST_RET_ENC_EC(ret);
  41085. ret = mp_rand_prime(a, 0, rng, NULL);
  41086. if (ret != MP_VAL)
  41087. return WC_TEST_RET_ENC_EC(ret);
  41088. #endif
  41089. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  41090. ret = mp_mul(NULL, NULL, NULL);
  41091. if (ret != MP_VAL)
  41092. return WC_TEST_RET_ENC_EC(ret);
  41093. ret = mp_mul(a, NULL, NULL);
  41094. if (ret != MP_VAL)
  41095. return WC_TEST_RET_ENC_EC(ret);
  41096. ret = mp_mul(NULL, b, NULL);
  41097. if (ret != MP_VAL)
  41098. return WC_TEST_RET_ENC_EC(ret);
  41099. ret = mp_mul(NULL, NULL, r);
  41100. if (ret != MP_VAL)
  41101. return WC_TEST_RET_ENC_EC(ret);
  41102. ret = mp_mul(a, b, NULL);
  41103. if (ret != MP_VAL)
  41104. return WC_TEST_RET_ENC_EC(ret);
  41105. ret = mp_mul(a, NULL, r);
  41106. if (ret != MP_VAL)
  41107. return WC_TEST_RET_ENC_EC(ret);
  41108. ret = mp_mul(NULL, b, r);
  41109. if (ret != MP_VAL)
  41110. return WC_TEST_RET_ENC_EC(ret);
  41111. #endif
  41112. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  41113. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  41114. ret = mp_sqr(NULL, NULL);
  41115. if (ret != MP_VAL)
  41116. return WC_TEST_RET_ENC_EC(ret);
  41117. ret = mp_sqr(a, NULL);
  41118. if (ret != MP_VAL)
  41119. return WC_TEST_RET_ENC_EC(ret);
  41120. ret = mp_sqr(NULL, r);
  41121. if (ret != MP_VAL)
  41122. return WC_TEST_RET_ENC_EC(ret);
  41123. #endif
  41124. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  41125. ret = mp_sqrmod(NULL, NULL, NULL);
  41126. if (ret != MP_VAL)
  41127. return WC_TEST_RET_ENC_EC(ret);
  41128. ret = mp_sqrmod(a, NULL, NULL);
  41129. if (ret != MP_VAL)
  41130. return WC_TEST_RET_ENC_EC(ret);
  41131. ret = mp_sqrmod(NULL, a, NULL);
  41132. if (ret != MP_VAL)
  41133. return WC_TEST_RET_ENC_EC(ret);
  41134. ret = mp_sqrmod(NULL, NULL, a);
  41135. if (ret != MP_VAL)
  41136. return WC_TEST_RET_ENC_EC(ret);
  41137. ret = mp_sqrmod(a, b, NULL);
  41138. if (ret != MP_VAL)
  41139. return WC_TEST_RET_ENC_EC(ret);
  41140. ret = mp_sqrmod(a, NULL, b);
  41141. if (ret != MP_VAL)
  41142. return WC_TEST_RET_ENC_EC(ret);
  41143. ret = mp_sqrmod(NULL, a, b);
  41144. if (ret != MP_VAL)
  41145. return WC_TEST_RET_ENC_EC(ret);
  41146. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  41147. if (ret != MP_VAL)
  41148. return WC_TEST_RET_ENC_EC(ret);
  41149. ret = mp_mulmod(a, NULL, NULL, NULL);
  41150. if (ret != MP_VAL)
  41151. return WC_TEST_RET_ENC_EC(ret);
  41152. ret = mp_mulmod(NULL, a, NULL, NULL);
  41153. if (ret != MP_VAL)
  41154. return WC_TEST_RET_ENC_EC(ret);
  41155. ret = mp_mulmod(NULL, NULL, a, NULL);
  41156. if (ret != MP_VAL)
  41157. return WC_TEST_RET_ENC_EC(ret);
  41158. ret = mp_mulmod(NULL, NULL, NULL, a);
  41159. if (ret != MP_VAL)
  41160. return WC_TEST_RET_ENC_EC(ret);
  41161. ret = mp_mulmod(a, b, b, NULL);
  41162. if (ret != MP_VAL)
  41163. return WC_TEST_RET_ENC_EC(ret);
  41164. ret = mp_mulmod(a, b, NULL, a);
  41165. if (ret != MP_VAL)
  41166. return WC_TEST_RET_ENC_EC(ret);
  41167. ret = mp_mulmod(a, NULL, b, a);
  41168. if (ret != MP_VAL)
  41169. return WC_TEST_RET_ENC_EC(ret);
  41170. ret = mp_mulmod(NULL, b, b, a);
  41171. if (ret != MP_VAL)
  41172. return WC_TEST_RET_ENC_EC(ret);
  41173. #endif
  41174. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  41175. !defined(NO_RSA) || !defined(NO_DSA)
  41176. ret = mp_add_d(NULL, 1, NULL);
  41177. if (ret != MP_VAL)
  41178. return WC_TEST_RET_ENC_EC(ret);
  41179. ret = mp_add_d(a, 1, NULL);
  41180. if (ret != MP_VAL)
  41181. return WC_TEST_RET_ENC_EC(ret);
  41182. ret = mp_add_d(NULL, 1, b);
  41183. if (ret != MP_VAL)
  41184. return WC_TEST_RET_ENC_EC(ret);
  41185. #endif
  41186. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  41187. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  41188. ret = mp_sub_d(NULL, 1, NULL);
  41189. if (ret != MP_VAL)
  41190. return WC_TEST_RET_ENC_EC(ret);
  41191. ret = mp_sub_d(a, 1, NULL);
  41192. if (ret != MP_VAL)
  41193. return WC_TEST_RET_ENC_EC(ret);
  41194. ret = mp_sub_d(NULL, 1, b);
  41195. if (ret != MP_VAL)
  41196. return WC_TEST_RET_ENC_EC(ret);
  41197. #endif
  41198. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  41199. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  41200. ret = mp_div_d(NULL, 0, NULL, NULL);
  41201. if (ret != MP_VAL)
  41202. return WC_TEST_RET_ENC_EC(ret);
  41203. ret = mp_div_d(a, 0, NULL, NULL);
  41204. if (ret != MP_VAL)
  41205. return WC_TEST_RET_ENC_EC(ret);
  41206. ret = mp_div_d(NULL, 1, NULL, NULL);
  41207. if (ret != MP_VAL)
  41208. return WC_TEST_RET_ENC_EC(ret);
  41209. #endif
  41210. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  41211. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  41212. ret = mp_mod_d(NULL, 0, NULL);
  41213. if (ret != MP_VAL)
  41214. return WC_TEST_RET_ENC_EC(ret);
  41215. ret = mp_mod_d(a, 0, NULL);
  41216. if (ret != MP_VAL)
  41217. return WC_TEST_RET_ENC_EC(ret);
  41218. ret = mp_mod_d(NULL, 0, &rd);
  41219. if (ret != MP_VAL)
  41220. return WC_TEST_RET_ENC_EC(ret);
  41221. #endif
  41222. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  41223. ret = mp_gcd(NULL, NULL, NULL);
  41224. if (ret != MP_VAL)
  41225. return WC_TEST_RET_ENC_EC(ret);
  41226. ret = mp_gcd(a, NULL, NULL);
  41227. if (ret != MP_VAL)
  41228. return WC_TEST_RET_ENC_EC(ret);
  41229. ret = mp_gcd(NULL, a, NULL);
  41230. if (ret != MP_VAL)
  41231. return WC_TEST_RET_ENC_EC(ret);
  41232. ret = mp_gcd(NULL, NULL, a);
  41233. if (ret != MP_VAL)
  41234. return WC_TEST_RET_ENC_EC(ret);
  41235. ret = mp_gcd(a, b, NULL);
  41236. if (ret != MP_VAL)
  41237. return WC_TEST_RET_ENC_EC(ret);
  41238. ret = mp_gcd(a, NULL, b);
  41239. if (ret != MP_VAL)
  41240. return WC_TEST_RET_ENC_EC(ret);
  41241. ret = mp_gcd(NULL, a, b);
  41242. if (ret != MP_VAL)
  41243. return WC_TEST_RET_ENC_EC(ret);
  41244. #endif
  41245. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  41246. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  41247. if (ret != MP_VAL)
  41248. return WC_TEST_RET_ENC_EC(ret);
  41249. ret = mp_div_2_mod_ct(a, NULL, NULL);
  41250. if (ret != MP_VAL)
  41251. return WC_TEST_RET_ENC_EC(ret);
  41252. ret = mp_div_2_mod_ct(NULL, b, NULL);
  41253. if (ret != MP_VAL)
  41254. return WC_TEST_RET_ENC_EC(ret);
  41255. ret = mp_div_2_mod_ct(NULL, NULL, a);
  41256. if (ret != MP_VAL)
  41257. return WC_TEST_RET_ENC_EC(ret);
  41258. ret = mp_div_2_mod_ct(a, b, NULL);
  41259. if (ret != MP_VAL)
  41260. return WC_TEST_RET_ENC_EC(ret);
  41261. ret = mp_div_2_mod_ct(a, b, NULL);
  41262. if (ret != MP_VAL)
  41263. return WC_TEST_RET_ENC_EC(ret);
  41264. ret = mp_div_2_mod_ct(NULL, b, a);
  41265. if (ret != MP_VAL)
  41266. return WC_TEST_RET_ENC_EC(ret);
  41267. ret = mp_div_2(NULL, NULL);
  41268. if (ret != MP_VAL)
  41269. return WC_TEST_RET_ENC_EC(ret);
  41270. ret = mp_div_2(a, NULL);
  41271. if (ret != MP_VAL)
  41272. return WC_TEST_RET_ENC_EC(ret);
  41273. ret = mp_div_2(NULL, a);
  41274. if (ret != MP_VAL)
  41275. return WC_TEST_RET_ENC_EC(ret);
  41276. #endif
  41277. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  41278. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  41279. ret = mp_invmod(NULL, NULL, NULL);
  41280. if (ret != MP_VAL)
  41281. return WC_TEST_RET_ENC_EC(ret);
  41282. ret = mp_invmod(a, NULL, NULL);
  41283. if (ret != MP_VAL)
  41284. return WC_TEST_RET_ENC_EC(ret);
  41285. ret = mp_invmod(NULL, b, NULL);
  41286. if (ret != MP_VAL)
  41287. return WC_TEST_RET_ENC_EC(ret);
  41288. ret = mp_invmod(NULL, NULL, a);
  41289. if (ret != MP_VAL)
  41290. return WC_TEST_RET_ENC_EC(ret);
  41291. ret = mp_invmod(a, b, NULL);
  41292. if (ret != MP_VAL)
  41293. return WC_TEST_RET_ENC_EC(ret);
  41294. ret = mp_invmod(a, NULL, a);
  41295. if (ret != MP_VAL)
  41296. return WC_TEST_RET_ENC_EC(ret);
  41297. ret = mp_invmod(NULL, b, a);
  41298. if (ret != MP_VAL)
  41299. return WC_TEST_RET_ENC_EC(ret);
  41300. #endif
  41301. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  41302. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  41303. if (ret != MP_VAL)
  41304. return WC_TEST_RET_ENC_EC(ret);
  41305. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  41306. if (ret != MP_VAL)
  41307. return WC_TEST_RET_ENC_EC(ret);
  41308. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  41309. if (ret != MP_VAL)
  41310. return WC_TEST_RET_ENC_EC(ret);
  41311. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  41312. if (ret != MP_VAL)
  41313. return WC_TEST_RET_ENC_EC(ret);
  41314. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  41315. if (ret != MP_VAL)
  41316. return WC_TEST_RET_ENC_EC(ret);
  41317. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  41318. if (ret != MP_VAL)
  41319. return WC_TEST_RET_ENC_EC(ret);
  41320. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  41321. if (ret != MP_VAL)
  41322. return WC_TEST_RET_ENC_EC(ret);
  41323. #endif
  41324. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  41325. ret = mp_lcm(NULL, NULL, NULL);
  41326. if (ret != MP_VAL)
  41327. return WC_TEST_RET_ENC_EC(ret);
  41328. ret = mp_lcm(a, NULL, NULL);
  41329. if (ret != MP_VAL)
  41330. return WC_TEST_RET_ENC_EC(ret);
  41331. ret = mp_lcm(NULL, b, NULL);
  41332. if (ret != MP_VAL)
  41333. return WC_TEST_RET_ENC_EC(ret);
  41334. ret = mp_lcm(NULL, NULL, a);
  41335. if (ret != MP_VAL)
  41336. return WC_TEST_RET_ENC_EC(ret);
  41337. ret = mp_lcm(a, b, NULL);
  41338. if (ret != MP_VAL)
  41339. return WC_TEST_RET_ENC_EC(ret);
  41340. ret = mp_lcm(a, NULL, a);
  41341. if (ret != MP_VAL)
  41342. return WC_TEST_RET_ENC_EC(ret);
  41343. ret = mp_lcm(NULL, b, a);
  41344. if (ret != MP_VAL)
  41345. return WC_TEST_RET_ENC_EC(ret);
  41346. #endif
  41347. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  41348. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  41349. if (ret != MP_VAL)
  41350. return WC_TEST_RET_ENC_EC(ret);
  41351. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  41352. if (ret != MP_VAL)
  41353. return WC_TEST_RET_ENC_EC(ret);
  41354. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  41355. if (ret != MP_VAL)
  41356. return WC_TEST_RET_ENC_EC(ret);
  41357. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  41358. if (ret != MP_VAL)
  41359. return WC_TEST_RET_ENC_EC(ret);
  41360. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  41361. if (ret != MP_VAL)
  41362. return WC_TEST_RET_ENC_EC(ret);
  41363. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  41364. if (ret != MP_VAL)
  41365. return WC_TEST_RET_ENC_EC(ret);
  41366. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  41367. if (ret != MP_VAL)
  41368. return WC_TEST_RET_ENC_EC(ret);
  41369. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  41370. if (ret != MP_VAL)
  41371. return WC_TEST_RET_ENC_EC(ret);
  41372. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  41373. if (ret != MP_VAL)
  41374. return WC_TEST_RET_ENC_EC(ret);
  41375. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  41376. if (ret != MP_VAL)
  41377. return WC_TEST_RET_ENC_EC(ret);
  41378. ret = mp_exptmod(a, NULL, NULL, NULL);
  41379. if (ret != MP_VAL)
  41380. return WC_TEST_RET_ENC_EC(ret);
  41381. ret = mp_exptmod(NULL, b, NULL, NULL);
  41382. if (ret != MP_VAL)
  41383. return WC_TEST_RET_ENC_EC(ret);
  41384. ret = mp_exptmod(NULL, NULL, b, NULL);
  41385. if (ret != MP_VAL)
  41386. return WC_TEST_RET_ENC_EC(ret);
  41387. ret = mp_exptmod(NULL, NULL, NULL, a);
  41388. if (ret != MP_VAL)
  41389. return WC_TEST_RET_ENC_EC(ret);
  41390. ret = mp_exptmod(a, b, b, NULL);
  41391. if (ret != MP_VAL)
  41392. return WC_TEST_RET_ENC_EC(ret);
  41393. ret = mp_exptmod(a, b, NULL, a);
  41394. if (ret != MP_VAL)
  41395. return WC_TEST_RET_ENC_EC(ret);
  41396. ret = mp_exptmod(a, NULL, b, a);
  41397. if (ret != MP_VAL)
  41398. return WC_TEST_RET_ENC_EC(ret);
  41399. ret = mp_exptmod(NULL, b, b, a);
  41400. if (ret != MP_VAL)
  41401. return WC_TEST_RET_ENC_EC(ret);
  41402. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  41403. if (ret != MP_VAL)
  41404. return WC_TEST_RET_ENC_EC(ret);
  41405. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  41406. if (ret != MP_VAL)
  41407. return WC_TEST_RET_ENC_EC(ret);
  41408. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  41409. if (ret != MP_VAL)
  41410. return WC_TEST_RET_ENC_EC(ret);
  41411. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  41412. if (ret != MP_VAL)
  41413. return WC_TEST_RET_ENC_EC(ret);
  41414. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  41415. if (ret != MP_VAL)
  41416. return WC_TEST_RET_ENC_EC(ret);
  41417. ret = mp_exptmod_nct(a, b, b, NULL);
  41418. if (ret != MP_VAL)
  41419. return WC_TEST_RET_ENC_EC(ret);
  41420. ret = mp_exptmod_nct(a, b, NULL, a);
  41421. if (ret != MP_VAL)
  41422. return WC_TEST_RET_ENC_EC(ret);
  41423. ret = mp_exptmod_nct(a, NULL, b, a);
  41424. if (ret != MP_VAL)
  41425. return WC_TEST_RET_ENC_EC(ret);
  41426. ret = mp_exptmod_nct(NULL, b, b, a);
  41427. if (ret != MP_VAL)
  41428. return WC_TEST_RET_ENC_EC(ret);
  41429. #endif
  41430. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  41431. ret = mp_cnt_lsb(NULL);
  41432. if (ret != 0)
  41433. return WC_TEST_RET_ENC_EC(ret);
  41434. #endif
  41435. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  41436. ret = mp_prime_is_prime(NULL, 1, NULL);
  41437. if (ret != MP_VAL)
  41438. return WC_TEST_RET_ENC_EC(ret);
  41439. ret = mp_prime_is_prime(a, 1, NULL);
  41440. if (ret != MP_VAL)
  41441. return WC_TEST_RET_ENC_EC(ret);
  41442. ret = mp_prime_is_prime(NULL, 1, &result);
  41443. if (ret != MP_VAL)
  41444. return WC_TEST_RET_ENC_EC(ret);
  41445. ret = mp_prime_is_prime(a, 0, &result);
  41446. if (ret != MP_VAL)
  41447. return WC_TEST_RET_ENC_EC(ret);
  41448. ret = mp_prime_is_prime(a, 1024, &result);
  41449. if (ret != MP_VAL)
  41450. return WC_TEST_RET_ENC_EC(ret);
  41451. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  41452. if (ret != MP_VAL)
  41453. return WC_TEST_RET_ENC_EC(ret);
  41454. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  41455. if (ret != MP_VAL)
  41456. return WC_TEST_RET_ENC_EC(ret);
  41457. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  41458. if (ret != MP_VAL)
  41459. return WC_TEST_RET_ENC_EC(ret);
  41460. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  41461. if (ret != MP_VAL)
  41462. return WC_TEST_RET_ENC_EC(ret);
  41463. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  41464. if (ret != MP_VAL)
  41465. return WC_TEST_RET_ENC_EC(ret);
  41466. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  41467. if (ret != MP_VAL)
  41468. return WC_TEST_RET_ENC_EC(ret);
  41469. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  41470. if (ret != MP_VAL)
  41471. return WC_TEST_RET_ENC_EC(ret);
  41472. #endif
  41473. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  41474. ret = mp_exch(NULL, NULL);
  41475. if (ret != MP_VAL)
  41476. return WC_TEST_RET_ENC_EC(ret);
  41477. ret = mp_exch(a, NULL);
  41478. if (ret != MP_VAL)
  41479. return WC_TEST_RET_ENC_EC(ret);
  41480. ret = mp_exch(NULL, b);
  41481. if (ret != MP_VAL)
  41482. return WC_TEST_RET_ENC_EC(ret);
  41483. #endif
  41484. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  41485. defined(WOLFSSL_SP_MATH_ALL)
  41486. ret = mp_mul_d(NULL, 1, NULL);
  41487. if (ret != MP_VAL)
  41488. return WC_TEST_RET_ENC_EC(ret);
  41489. ret = mp_mul_d(a, 1, NULL);
  41490. if (ret != MP_VAL)
  41491. return WC_TEST_RET_ENC_EC(ret);
  41492. ret = mp_mul_d(NULL, 1, b);
  41493. if (ret != MP_VAL)
  41494. return WC_TEST_RET_ENC_EC(ret);
  41495. #endif
  41496. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  41497. ret = mp_add(NULL, NULL, NULL);
  41498. if (ret != MP_VAL)
  41499. return WC_TEST_RET_ENC_EC(ret);
  41500. ret = mp_add(a, NULL, NULL);
  41501. if (ret != MP_VAL)
  41502. return WC_TEST_RET_ENC_EC(ret);
  41503. ret = mp_add(NULL, b, NULL);
  41504. if (ret != MP_VAL)
  41505. return WC_TEST_RET_ENC_EC(ret);
  41506. ret = mp_add(NULL, NULL, r);
  41507. if (ret != MP_VAL)
  41508. return WC_TEST_RET_ENC_EC(ret);
  41509. ret = mp_add(a, b, NULL);
  41510. if (ret != MP_VAL)
  41511. return WC_TEST_RET_ENC_EC(ret);
  41512. ret = mp_add(a, NULL, r);
  41513. if (ret != MP_VAL)
  41514. return WC_TEST_RET_ENC_EC(ret);
  41515. ret = mp_add(NULL, b, r);
  41516. if (ret != MP_VAL)
  41517. return WC_TEST_RET_ENC_EC(ret);
  41518. #endif
  41519. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  41520. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  41521. ret = mp_sub(NULL, NULL, NULL);
  41522. if (ret != MP_VAL)
  41523. return WC_TEST_RET_ENC_EC(ret);
  41524. ret = mp_sub(a, NULL, NULL);
  41525. if (ret != MP_VAL)
  41526. return WC_TEST_RET_ENC_EC(ret);
  41527. ret = mp_sub(NULL, b, NULL);
  41528. if (ret != MP_VAL)
  41529. return WC_TEST_RET_ENC_EC(ret);
  41530. ret = mp_sub(NULL, NULL, r);
  41531. if (ret != MP_VAL)
  41532. return WC_TEST_RET_ENC_EC(ret);
  41533. ret = mp_sub(a, b, NULL);
  41534. if (ret != MP_VAL)
  41535. return WC_TEST_RET_ENC_EC(ret);
  41536. ret = mp_sub(a, NULL, r);
  41537. if (ret != MP_VAL)
  41538. return WC_TEST_RET_ENC_EC(ret);
  41539. ret = mp_sub(NULL, b, r);
  41540. if (ret != MP_VAL)
  41541. return WC_TEST_RET_ENC_EC(ret);
  41542. #endif
  41543. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  41544. defined(WOLFSSL_CUSTOM_CURVES))
  41545. ret = mp_addmod(NULL, NULL, NULL, NULL);
  41546. if (ret != MP_VAL)
  41547. return WC_TEST_RET_ENC_EC(ret);
  41548. ret = mp_addmod(a, NULL, NULL, NULL);
  41549. if (ret != MP_VAL)
  41550. return WC_TEST_RET_ENC_EC(ret);
  41551. ret = mp_addmod(NULL, b, NULL, NULL);
  41552. if (ret != MP_VAL)
  41553. return WC_TEST_RET_ENC_EC(ret);
  41554. ret = mp_addmod(NULL, NULL, b, NULL);
  41555. if (ret != MP_VAL)
  41556. return WC_TEST_RET_ENC_EC(ret);
  41557. ret = mp_addmod(NULL, NULL, NULL, a);
  41558. if (ret != MP_VAL)
  41559. return WC_TEST_RET_ENC_EC(ret);
  41560. ret = mp_addmod(a, b, b, NULL);
  41561. if (ret != MP_VAL)
  41562. return WC_TEST_RET_ENC_EC(ret);
  41563. ret = mp_addmod(a, b, NULL, a);
  41564. if (ret != MP_VAL)
  41565. return WC_TEST_RET_ENC_EC(ret);
  41566. ret = mp_addmod(a, NULL, b, a);
  41567. if (ret != MP_VAL)
  41568. return WC_TEST_RET_ENC_EC(ret);
  41569. ret = mp_addmod(NULL, b, b, a);
  41570. if (ret != MP_VAL)
  41571. return WC_TEST_RET_ENC_EC(ret);
  41572. #endif
  41573. #ifdef WOLFSSL_SP_MATH_ALL
  41574. ret = mp_submod(NULL, NULL, NULL, NULL);
  41575. if (ret != MP_VAL)
  41576. return WC_TEST_RET_ENC_EC(ret);
  41577. ret = mp_submod(a, NULL, NULL, NULL);
  41578. if (ret != MP_VAL)
  41579. return WC_TEST_RET_ENC_EC(ret);
  41580. ret = mp_submod(NULL, b, NULL, NULL);
  41581. if (ret != MP_VAL)
  41582. return WC_TEST_RET_ENC_EC(ret);
  41583. ret = mp_submod(NULL, NULL, b, NULL);
  41584. if (ret != MP_VAL)
  41585. return WC_TEST_RET_ENC_EC(ret);
  41586. ret = mp_submod(NULL, NULL, NULL, a);
  41587. if (ret != MP_VAL)
  41588. return WC_TEST_RET_ENC_EC(ret);
  41589. ret = mp_submod(a, b, b, NULL);
  41590. if (ret != MP_VAL)
  41591. return WC_TEST_RET_ENC_EC(ret);
  41592. ret = mp_submod(a, b, NULL, a);
  41593. if (ret != MP_VAL)
  41594. return WC_TEST_RET_ENC_EC(ret);
  41595. ret = mp_submod(a, NULL, b, a);
  41596. if (ret != MP_VAL)
  41597. return WC_TEST_RET_ENC_EC(ret);
  41598. ret = mp_submod(NULL, b, b, a);
  41599. if (ret != MP_VAL)
  41600. return WC_TEST_RET_ENC_EC(ret);
  41601. #endif
  41602. #ifdef WOLFSSL_SP_MATH_ALL
  41603. ret = mp_div_2d(NULL, 1, a, b);
  41604. if (ret != MP_VAL)
  41605. return WC_TEST_RET_ENC_EC(ret);
  41606. ret = mp_mod_2d(NULL, 1, NULL);
  41607. if (ret != MP_VAL)
  41608. return WC_TEST_RET_ENC_EC(ret);
  41609. ret = mp_mod_2d(a, 1, NULL);
  41610. if (ret != MP_VAL)
  41611. return WC_TEST_RET_ENC_EC(ret);
  41612. ret = mp_mod_2d(NULL, 1, b);
  41613. if (ret != MP_VAL)
  41614. return WC_TEST_RET_ENC_EC(ret);
  41615. ret = mp_mul_2d(NULL, 1, NULL);
  41616. if (ret != MP_VAL)
  41617. return WC_TEST_RET_ENC_EC(ret);
  41618. ret = mp_mul_2d(a, 1, NULL);
  41619. if (ret != MP_VAL)
  41620. return WC_TEST_RET_ENC_EC(ret);
  41621. ret = mp_mul_2d(NULL, 1, b);
  41622. if (ret != MP_VAL)
  41623. return WC_TEST_RET_ENC_EC(ret);
  41624. #endif
  41625. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  41626. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  41627. ret = mp_montgomery_reduce(NULL, NULL, 1);
  41628. if (ret != MP_VAL)
  41629. return WC_TEST_RET_ENC_EC(ret);
  41630. ret = mp_montgomery_reduce(a, NULL, 1);
  41631. if (ret != MP_VAL)
  41632. return WC_TEST_RET_ENC_EC(ret);
  41633. ret = mp_montgomery_reduce(NULL, b, 1);
  41634. if (ret != MP_VAL)
  41635. return WC_TEST_RET_ENC_EC(ret);
  41636. mp_zero(b);
  41637. ret = mp_montgomery_reduce(a, b, 1);
  41638. if (ret != MP_VAL)
  41639. return WC_TEST_RET_ENC_EC(ret);
  41640. #endif
  41641. #ifdef WOLFSSL_SP_MATH_ALL
  41642. ret = mp_montgomery_setup(NULL, NULL);
  41643. if (ret != MP_VAL)
  41644. return WC_TEST_RET_ENC_EC(ret);
  41645. ret = mp_montgomery_setup(a, NULL);
  41646. if (ret != MP_VAL)
  41647. return WC_TEST_RET_ENC_EC(ret);
  41648. ret = mp_montgomery_setup(NULL, &rho);
  41649. if (ret != MP_VAL)
  41650. return WC_TEST_RET_ENC_EC(ret);
  41651. ret = mp_montgomery_calc_normalization(NULL, NULL);
  41652. if (ret != MP_VAL)
  41653. return WC_TEST_RET_ENC_EC(ret);
  41654. ret = mp_montgomery_calc_normalization(a, NULL);
  41655. if (ret != MP_VAL)
  41656. return WC_TEST_RET_ENC_EC(ret);
  41657. ret = mp_montgomery_calc_normalization(NULL, b);
  41658. if (ret != MP_VAL)
  41659. return WC_TEST_RET_ENC_EC(ret);
  41660. #endif
  41661. ret = mp_unsigned_bin_size(NULL);
  41662. if (ret != 0)
  41663. return WC_TEST_RET_ENC_EC(ret);
  41664. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  41665. ret = mp_tohex(NULL, NULL);
  41666. if (ret != MP_VAL)
  41667. return WC_TEST_RET_ENC_EC(ret);
  41668. ret = mp_tohex(a, NULL);
  41669. if (ret != MP_VAL)
  41670. return WC_TEST_RET_ENC_EC(ret);
  41671. ret = mp_tohex(NULL, hexStr);
  41672. if (ret != MP_VAL)
  41673. return WC_TEST_RET_ENC_EC(ret);
  41674. #endif
  41675. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  41676. ret = mp_todecimal(NULL, NULL);
  41677. if (ret != MP_VAL)
  41678. return WC_TEST_RET_ENC_EC(ret);
  41679. ret = mp_todecimal(a, NULL);
  41680. if (ret != MP_VAL)
  41681. return WC_TEST_RET_ENC_EC(ret);
  41682. ret = mp_todecimal(NULL, decStr);
  41683. if (ret != MP_VAL)
  41684. return WC_TEST_RET_ENC_EC(ret);
  41685. #endif
  41686. #ifdef WOLFSSL_SP_MATH_ALL
  41687. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  41688. if (ret != MP_VAL)
  41689. return WC_TEST_RET_ENC_EC(ret);
  41690. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  41691. if (ret != MP_VAL)
  41692. return WC_TEST_RET_ENC_EC(ret);
  41693. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  41694. if (ret != MP_VAL)
  41695. return WC_TEST_RET_ENC_EC(ret);
  41696. ret = mp_toradix(a, hexStr, 3);
  41697. if (ret != MP_VAL)
  41698. return WC_TEST_RET_ENC_EC(ret);
  41699. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  41700. if (ret != MP_VAL)
  41701. return WC_TEST_RET_ENC_EC(ret);
  41702. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  41703. if (ret != MP_VAL)
  41704. return WC_TEST_RET_ENC_EC(ret);
  41705. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  41706. if (ret != MP_VAL)
  41707. return WC_TEST_RET_ENC_EC(ret);
  41708. ret = mp_radix_size(a, 3, &size);
  41709. if (ret != MP_VAL)
  41710. return WC_TEST_RET_ENC_EC(ret);
  41711. #endif
  41712. return 0;
  41713. }
  41714. #endif
  41715. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  41716. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  41717. {
  41718. int i, j;
  41719. wc_test_ret_t ret;
  41720. mp_zero(a);
  41721. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41722. if (mp_is_bit_set(a, i))
  41723. return WC_TEST_RET_ENC_NC;
  41724. for (j = 0; j < i; j++) {
  41725. if (!mp_is_bit_set(a, j))
  41726. return WC_TEST_RET_ENC_NC;
  41727. }
  41728. if (mp_set_bit(a, i) != 0)
  41729. return WC_TEST_RET_ENC_NC;
  41730. if (!mp_is_bit_set(a, i))
  41731. return WC_TEST_RET_ENC_NC;
  41732. }
  41733. mp_zero(a);
  41734. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41735. if (mp_is_bit_set(a, i))
  41736. return WC_TEST_RET_ENC_NC;
  41737. }
  41738. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  41739. mp_zero(a);
  41740. if (mp_set_bit(a, i) != 0)
  41741. return WC_TEST_RET_ENC_NC;
  41742. for (j = 0; j < i; j++) {
  41743. if (mp_is_bit_set(a, j))
  41744. return WC_TEST_RET_ENC_NC;
  41745. }
  41746. if (!mp_is_bit_set(a, i))
  41747. return WC_TEST_RET_ENC_NC;
  41748. }
  41749. #ifdef WOLFSSL_KEY_GEN
  41750. for (i = 0; i < DIGIT_BIT * 2; i++) {
  41751. mp_set(a, 1);
  41752. ret = mp_2expt(a, i);
  41753. if (ret != 0)
  41754. return WC_TEST_RET_ENC_EC(ret);
  41755. for (j = 0; j < i; j++) {
  41756. if (mp_is_bit_set(a, j))
  41757. return WC_TEST_RET_ENC_NC;
  41758. }
  41759. if (!mp_is_bit_set(a, i))
  41760. return WC_TEST_RET_ENC_NC;
  41761. }
  41762. #endif
  41763. #ifdef WOLFSSL_SP_MATH
  41764. mp_zero(a);
  41765. for (j = 1; j <= 3; j++) {
  41766. i = SP_INT_MAX_BITS - j;
  41767. if (mp_is_bit_set(a, i))
  41768. return WC_TEST_RET_ENC_NC;
  41769. if (mp_set_bit(a, i) != 0)
  41770. return WC_TEST_RET_ENC_NC;
  41771. if (!mp_is_bit_set(a, i))
  41772. return WC_TEST_RET_ENC_NC;
  41773. #ifdef WOLFSSL_KEY_GEN
  41774. ret = mp_2expt(a, i);
  41775. if (ret != 0)
  41776. return WC_TEST_RET_ENC_EC(ret);
  41777. if (!mp_is_bit_set(a, i))
  41778. return WC_TEST_RET_ENC_NC;
  41779. #endif
  41780. }
  41781. mp_zero(a);
  41782. for (j = 0; j <= 3; j++) {
  41783. i = SP_INT_MAX_BITS + j;
  41784. if (mp_is_bit_set(a, i))
  41785. return WC_TEST_RET_ENC_NC;
  41786. if (mp_set_bit(a, i) != MP_VAL)
  41787. return WC_TEST_RET_ENC_NC;
  41788. #ifdef WOLFSSL_KEY_GEN
  41789. ret = mp_2expt(a, i);
  41790. if (ret != MP_VAL)
  41791. return WC_TEST_RET_ENC_EC(ret);
  41792. #endif
  41793. }
  41794. #endif
  41795. (void)ret;
  41796. return 0;
  41797. }
  41798. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  41799. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  41800. {
  41801. wc_test_ret_t ret;
  41802. mp_zero(a);
  41803. mp_zero(b);
  41804. ret = mp_cmp_d(a, 0);
  41805. if (ret != MP_EQ)
  41806. return WC_TEST_RET_ENC_EC(ret);
  41807. ret = mp_cmp_d(a, 1);
  41808. if (ret != MP_LT)
  41809. return WC_TEST_RET_ENC_EC(ret);
  41810. ret = mp_cmp(a, b);
  41811. if (ret != MP_EQ)
  41812. return WC_TEST_RET_ENC_NC;
  41813. mp_set(a, 1);
  41814. ret = mp_cmp_d(a, 0);
  41815. if (ret != MP_GT)
  41816. return WC_TEST_RET_ENC_EC(ret);
  41817. ret = mp_cmp_d(a, 1);
  41818. if (ret != MP_EQ)
  41819. return WC_TEST_RET_ENC_EC(ret);
  41820. ret = mp_cmp_d(a, 2);
  41821. if (ret != MP_LT)
  41822. return WC_TEST_RET_ENC_EC(ret);
  41823. ret = mp_cmp(a, b);
  41824. if (ret != MP_GT)
  41825. return WC_TEST_RET_ENC_NC;
  41826. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  41827. ret = mp_cmp_d(b, -1);
  41828. if (ret != MP_GT)
  41829. return WC_TEST_RET_ENC_EC(ret);
  41830. ret = mp_cmp(a, b);
  41831. if (ret != MP_LT)
  41832. return WC_TEST_RET_ENC_NC;
  41833. ret = mp_cmp(b, a);
  41834. if (ret != MP_GT)
  41835. return WC_TEST_RET_ENC_NC;
  41836. ret = mp_cmp(b, b);
  41837. if (ret != MP_EQ)
  41838. return WC_TEST_RET_ENC_NC;
  41839. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  41840. defined(WOLFSSL_SP_INT_NEGATIVE)
  41841. mp_read_radix(a, "-1", MP_RADIX_HEX);
  41842. mp_read_radix(a, "1", MP_RADIX_HEX);
  41843. ret = mp_cmp(a, b);
  41844. if (ret != MP_LT)
  41845. return WC_TEST_RET_ENC_NC;
  41846. ret = mp_cmp(b, a);
  41847. if (ret != MP_GT)
  41848. return WC_TEST_RET_ENC_NC;
  41849. mp_read_radix(b, "-2", MP_RADIX_HEX);
  41850. ret = mp_cmp(a, b);
  41851. if (ret != MP_GT)
  41852. return WC_TEST_RET_ENC_NC;
  41853. ret = mp_cmp(b, a);
  41854. if (ret != MP_LT)
  41855. return WC_TEST_RET_ENC_NC;
  41856. mp_read_radix(a, "-2", MP_RADIX_HEX);
  41857. ret = mp_cmp(a, b);
  41858. if (ret != MP_EQ)
  41859. return WC_TEST_RET_ENC_NC;
  41860. #endif
  41861. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  41862. defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
  41863. mp_zero(a);
  41864. mp_zero(b);
  41865. ret = mp_cmp_ct(a, b, 1);
  41866. if (ret != MP_EQ)
  41867. return WC_TEST_RET_ENC_EC(ret);
  41868. ret = mp_cmp_ct(a, a, a->used);
  41869. if (ret != MP_EQ)
  41870. return WC_TEST_RET_ENC_EC(ret);
  41871. #ifdef WOLFSSL_SP_MATH_ALL
  41872. ret = mp_cmp_ct(a, NULL, a->used);
  41873. if (ret != MP_GT)
  41874. return WC_TEST_RET_ENC_EC(ret);
  41875. ret = mp_cmp_ct(NULL, a, a->used);
  41876. if (ret != MP_LT)
  41877. return WC_TEST_RET_ENC_EC(ret);
  41878. #endif
  41879. mp_read_radix(a, "1", MP_RADIX_HEX);
  41880. ret = mp_cmp_ct(a, b, 1);
  41881. if (ret != MP_GT)
  41882. return WC_TEST_RET_ENC_EC(ret);
  41883. ret = mp_cmp_ct(b, a, 1);
  41884. if (ret != MP_LT)
  41885. return WC_TEST_RET_ENC_EC(ret);
  41886. mp_read_radix(a, "0123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  41887. ret = mp_cmp_ct(a, b, a->used);
  41888. if (ret != MP_GT)
  41889. return WC_TEST_RET_ENC_EC(ret);
  41890. ret = mp_cmp_ct(b, a, a->used);
  41891. if (ret != MP_LT)
  41892. return WC_TEST_RET_ENC_EC(ret);
  41893. mp_read_radix(b, "1123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  41894. ret = mp_cmp_ct(b, a, a->used);
  41895. if (ret != MP_GT)
  41896. return WC_TEST_RET_ENC_EC(ret);
  41897. ret = mp_cmp_ct(a, b, a->used);
  41898. if (ret != MP_LT)
  41899. return WC_TEST_RET_ENC_EC(ret);
  41900. mp_read_radix(b, "0123456789abcdef0123456789abcdf0", MP_RADIX_HEX);
  41901. ret = mp_cmp_ct(b, a, a->used);
  41902. if (ret != MP_GT)
  41903. return WC_TEST_RET_ENC_EC(ret);
  41904. ret = mp_cmp_ct(a, b, a->used);
  41905. if (ret != MP_LT)
  41906. return WC_TEST_RET_ENC_EC(ret);
  41907. mp_read_radix(b, "0123456789abcdf0", MP_RADIX_HEX);
  41908. ret = mp_cmp_ct(a, b, a->used);
  41909. if (ret != MP_GT)
  41910. return WC_TEST_RET_ENC_EC(ret);
  41911. ret = mp_cmp_ct(b, a, a->used);
  41912. if (ret != MP_LT)
  41913. return WC_TEST_RET_ENC_EC(ret);
  41914. #endif
  41915. return 0;
  41916. }
  41917. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  41918. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  41919. {
  41920. wc_test_ret_t ret;
  41921. int i, j, k;
  41922. #ifndef WOLFSSL_SP_MATH
  41923. for (i = 0; i < 10; i++) {
  41924. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  41925. ret = randNum(a, j, rng, NULL);
  41926. if (ret != MP_OKAY)
  41927. return WC_TEST_RET_ENC_EC(ret);
  41928. mp_copy(a, b);
  41929. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  41930. ret = mp_mul_2d(a, k, a);
  41931. if (ret != MP_OKAY)
  41932. return WC_TEST_RET_ENC_EC(ret);
  41933. mp_rshb(a, k);
  41934. ret = mp_cmp(a, b);
  41935. if (ret != MP_EQ)
  41936. return WC_TEST_RET_ENC_NC;
  41937. }
  41938. }
  41939. }
  41940. #endif
  41941. for (i = 0; i < 10; i++) {
  41942. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  41943. ret = randNum(a, j, rng, NULL);
  41944. if (ret != MP_OKAY)
  41945. return WC_TEST_RET_ENC_EC(ret);
  41946. mp_copy(a, b);
  41947. for (k = 0; k < 10; k++) {
  41948. ret = mp_lshd(a, k);
  41949. if (ret != MP_OKAY)
  41950. return WC_TEST_RET_ENC_EC(ret);
  41951. #ifndef WOLFSSL_SP_MATH
  41952. mp_rshd(a, k);
  41953. #else
  41954. mp_rshb(a, k * SP_WORD_SIZE);
  41955. #endif
  41956. ret = mp_cmp(a, b);
  41957. if (ret != MP_EQ)
  41958. return WC_TEST_RET_ENC_NC;
  41959. }
  41960. }
  41961. }
  41962. #ifndef WOLFSSL_SP_MATH
  41963. mp_zero(a);
  41964. mp_rshd(a, 1);
  41965. if (!mp_iszero(a))
  41966. return WC_TEST_RET_ENC_NC;
  41967. mp_set(a, 1);
  41968. mp_rshd(a, 1);
  41969. if (!mp_iszero(a))
  41970. return WC_TEST_RET_ENC_NC;
  41971. mp_set(a, 1);
  41972. mp_rshd(a, 2);
  41973. if (!mp_iszero(a))
  41974. return WC_TEST_RET_ENC_NC;
  41975. #endif
  41976. return 0;
  41977. }
  41978. #endif
  41979. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  41980. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  41981. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  41982. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  41983. WC_RNG* rng)
  41984. {
  41985. wc_test_ret_t ret;
  41986. int i, j, k;
  41987. mp_zero(a);
  41988. mp_zero(d);
  41989. ret = mp_div(a, d, r, rem);
  41990. if (ret != MP_VAL)
  41991. return WC_TEST_RET_ENC_EC(ret);
  41992. mp_set(d, 1);
  41993. ret = mp_div(a, d, r, rem);
  41994. if (ret != MP_OKAY)
  41995. return WC_TEST_RET_ENC_EC(ret);
  41996. if (!mp_iszero(r))
  41997. return WC_TEST_RET_ENC_EC(ret);
  41998. if (!mp_iszero(rem))
  41999. return WC_TEST_RET_ENC_EC(ret);
  42000. mp_set(a, 1);
  42001. ret = mp_div(a, d, r, rem);
  42002. if (ret != MP_OKAY)
  42003. return WC_TEST_RET_ENC_EC(ret);
  42004. if (!mp_isone(r))
  42005. return WC_TEST_RET_ENC_EC(ret);
  42006. if (!mp_iszero(rem))
  42007. return WC_TEST_RET_ENC_EC(ret);
  42008. for (i = 0; i < 100; i++) {
  42009. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  42010. ret = randNum(d, j, rng, NULL);
  42011. if (ret != MP_OKAY)
  42012. return WC_TEST_RET_ENC_EC(ret);
  42013. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  42014. ret = randNum(a, k, rng, NULL);
  42015. if (ret != MP_OKAY)
  42016. return WC_TEST_RET_ENC_EC(ret);
  42017. ret = mp_div(a, d, NULL, rem);
  42018. if (ret != MP_OKAY)
  42019. return WC_TEST_RET_ENC_EC(ret);
  42020. ret = mp_div(a, d, r, NULL);
  42021. if (ret != MP_OKAY)
  42022. return WC_TEST_RET_ENC_EC(ret);
  42023. ret = mp_div(a, d, r, rem);
  42024. if (ret != MP_OKAY)
  42025. return WC_TEST_RET_ENC_EC(ret);
  42026. mp_mul(r, d, r);
  42027. mp_add(r, rem, r);
  42028. if (mp_cmp(r, a) != MP_EQ)
  42029. return WC_TEST_RET_ENC_NC;
  42030. }
  42031. }
  42032. }
  42033. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  42034. if (ret != MP_OKAY)
  42035. return WC_TEST_RET_ENC_EC(ret);
  42036. mp_add(d, d, a);
  42037. mp_set(rem, 1);
  42038. mp_div(a, d, NULL, rem);
  42039. if (ret != MP_OKAY)
  42040. return WC_TEST_RET_ENC_EC(ret);
  42041. if (!mp_iszero(rem))
  42042. return WC_TEST_RET_ENC_EC(ret);
  42043. mp_set(r, 1);
  42044. mp_div(a, d, r, NULL);
  42045. if (ret != MP_OKAY)
  42046. return WC_TEST_RET_ENC_EC(ret);
  42047. if (mp_cmp_d(r, 2) != MP_EQ)
  42048. return WC_TEST_RET_ENC_EC(ret);
  42049. mp_set(r, 1);
  42050. mp_set(rem, 1);
  42051. mp_div(a, d, r, rem);
  42052. if (ret != MP_OKAY)
  42053. return WC_TEST_RET_ENC_EC(ret);
  42054. if (mp_cmp_d(r, 2) != MP_EQ)
  42055. return WC_TEST_RET_ENC_EC(ret);
  42056. if (!mp_iszero(rem))
  42057. return WC_TEST_RET_ENC_EC(ret);
  42058. mp_set(a, 0xfe);
  42059. mp_lshd(a, 3);
  42060. mp_add_d(a, 0xff, a);
  42061. mp_set(d, 0xfe);
  42062. mp_lshd(d, 2);
  42063. ret = mp_div(a, d, r, rem);
  42064. if (ret != MP_OKAY)
  42065. return WC_TEST_RET_ENC_EC(ret);
  42066. mp_mul(r, d, d);
  42067. mp_add(rem, d, d);
  42068. if (mp_cmp(a, d) != MP_EQ)
  42069. return WC_TEST_RET_ENC_NC;
  42070. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  42071. * a digit. So mp_div must detect and handle.
  42072. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  42073. */
  42074. mp_zero(a);
  42075. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  42076. mp_zero(d);
  42077. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  42078. mp_add_d(d, 1, d);
  42079. ret = mp_div(a, d, r, rem);
  42080. if (ret != MP_OKAY)
  42081. return WC_TEST_RET_ENC_EC(ret);
  42082. /* Make sure [d | d] / d is handled. */
  42083. mp_zero(a);
  42084. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  42085. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  42086. mp_zero(d);
  42087. mp_set_bit(d, DIGIT_BIT - 1);
  42088. ret = mp_div(a, d, r, rem);
  42089. if (ret != MP_OKAY)
  42090. return WC_TEST_RET_ENC_EC(ret);
  42091. mp_zero(a);
  42092. mp_set_bit(a, DIGIT_BIT);
  42093. mp_set_bit(a, 0);
  42094. mp_zero(d);
  42095. if (mp_cmp(r, a) != MP_EQ)
  42096. return WC_TEST_RET_ENC_NC;
  42097. if (mp_cmp(rem, d) != MP_EQ)
  42098. return WC_TEST_RET_ENC_NC;
  42099. return 0;
  42100. }
  42101. #endif
  42102. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  42103. !defined(WC_NO_RNG)
  42104. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  42105. {
  42106. wc_test_ret_t ret;
  42107. int res;
  42108. ret = mp_rand_prime(a, 1, rng, NULL);
  42109. #if defined(WOLFSSL_SP_MATH_ALL)
  42110. if (ret != 0)
  42111. return WC_TEST_RET_ENC_EC(ret);
  42112. #else
  42113. if (ret != MP_VAL)
  42114. return WC_TEST_RET_ENC_NC;
  42115. #endif
  42116. #ifndef WOLFSSL_SP_MATH
  42117. ret = mp_rand_prime(a, -5, rng, NULL);
  42118. if (ret != 0 || (a->dp[0] & 3) != 3)
  42119. return WC_TEST_RET_ENC_NC;
  42120. #endif
  42121. ret = mp_prime_is_prime(a, 1, &res);
  42122. if (ret != MP_OKAY)
  42123. return WC_TEST_RET_ENC_EC(ret);
  42124. #ifndef WOLFSSL_SP_MATH
  42125. if (res != MP_YES)
  42126. return WC_TEST_RET_ENC_EC(res);
  42127. #else
  42128. if (res != MP_NO)
  42129. return WC_TEST_RET_ENC_EC(res);
  42130. #endif
  42131. ret = mp_prime_is_prime(a, 0, &res);
  42132. if (ret != MP_VAL)
  42133. return WC_TEST_RET_ENC_EC(ret);
  42134. ret = mp_prime_is_prime(a, -1, &res);
  42135. if (ret != MP_VAL)
  42136. return WC_TEST_RET_ENC_EC(ret);
  42137. ret = mp_prime_is_prime(a, 257, &res);
  42138. if (ret != MP_VAL)
  42139. return WC_TEST_RET_ENC_EC(ret);
  42140. mp_set(a, 1);
  42141. ret = mp_prime_is_prime(a, 1, &res);
  42142. if (ret != MP_OKAY)
  42143. return WC_TEST_RET_ENC_EC(ret);
  42144. if (res != MP_NO)
  42145. return WC_TEST_RET_ENC_EC(res);
  42146. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  42147. if (ret != MP_OKAY)
  42148. return WC_TEST_RET_ENC_EC(ret);
  42149. if (res != MP_NO)
  42150. return WC_TEST_RET_ENC_EC(res);
  42151. mp_set(a, 2);
  42152. ret = mp_prime_is_prime(a, 1, &res);
  42153. if (ret != MP_OKAY)
  42154. return WC_TEST_RET_ENC_EC(ret);
  42155. if (res != MP_YES)
  42156. return WC_TEST_RET_ENC_EC(res);
  42157. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  42158. if (ret != MP_OKAY)
  42159. return WC_TEST_RET_ENC_EC(ret);
  42160. if (res != MP_YES)
  42161. return WC_TEST_RET_ENC_EC(res);
  42162. mp_set(a, 0xfb);
  42163. ret = mp_prime_is_prime(a, 1, &res);
  42164. if (ret != MP_OKAY)
  42165. return WC_TEST_RET_ENC_EC(ret);
  42166. if (res != MP_YES)
  42167. return WC_TEST_RET_ENC_EC(res);
  42168. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  42169. if (ret != MP_OKAY)
  42170. return WC_TEST_RET_ENC_EC(ret);
  42171. if (res != MP_YES)
  42172. return WC_TEST_RET_ENC_EC(res);
  42173. mp_set(a, 0x6);
  42174. ret = mp_prime_is_prime(a, 1, &res);
  42175. if (ret != MP_OKAY)
  42176. return WC_TEST_RET_ENC_EC(ret);
  42177. if (res != MP_NO)
  42178. return WC_TEST_RET_ENC_EC(res);
  42179. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  42180. if (ret != MP_OKAY)
  42181. return WC_TEST_RET_ENC_EC(ret);
  42182. if (res != MP_NO)
  42183. return WC_TEST_RET_ENC_EC(res);
  42184. mp_set_int(a, 0x655 * 0x65b);
  42185. ret = mp_prime_is_prime(a, 10, &res);
  42186. if (ret != MP_OKAY)
  42187. return WC_TEST_RET_ENC_EC(ret);
  42188. if (res != MP_NO)
  42189. return WC_TEST_RET_ENC_EC(res);
  42190. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  42191. if (ret != MP_OKAY)
  42192. return WC_TEST_RET_ENC_EC(ret);
  42193. if (res != MP_NO)
  42194. return WC_TEST_RET_ENC_EC(res);
  42195. return 0;
  42196. }
  42197. #endif
  42198. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  42199. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  42200. WC_RNG* rng)
  42201. {
  42202. wc_test_ret_t ret;
  42203. int i;
  42204. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  42205. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  42206. { 12, 56, 168 }, { 56, 12, 168 }
  42207. };
  42208. (void)exp;
  42209. mp_set(a, 0);
  42210. mp_set(b, 1);
  42211. ret = mp_lcm(a, a, r);
  42212. if (ret != MP_VAL)
  42213. return WC_TEST_RET_ENC_EC(ret);
  42214. ret = mp_lcm(a, b, r);
  42215. if (ret != MP_VAL)
  42216. return WC_TEST_RET_ENC_EC(ret);
  42217. ret = mp_lcm(b, a, r);
  42218. if (ret != MP_VAL)
  42219. return WC_TEST_RET_ENC_EC(ret);
  42220. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  42221. mp_set(a, kat[i][0]);
  42222. mp_set(b, kat[i][1]);
  42223. ret = mp_lcm(a, b, r);
  42224. if (ret != MP_OKAY)
  42225. return WC_TEST_RET_ENC_EC(ret);
  42226. mp_set(exp, kat[i][2]);
  42227. ret = mp_cmp(r, exp);
  42228. if (ret != MP_EQ)
  42229. return WC_TEST_RET_ENC_NC;
  42230. }
  42231. (void)rng;
  42232. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  42233. !defined(WC_NO_RNG)
  42234. ret = mp_rand_prime(a, 20, rng, NULL);
  42235. if (ret != MP_OKAY)
  42236. return WC_TEST_RET_ENC_EC(ret);
  42237. ret = mp_rand_prime(b, 20, rng, NULL);
  42238. if (ret != MP_OKAY)
  42239. return WC_TEST_RET_ENC_EC(ret);
  42240. ret = mp_mul(a, b, exp);
  42241. if (ret != MP_OKAY)
  42242. return WC_TEST_RET_ENC_EC(ret);
  42243. ret = mp_lcm(a, b, r);
  42244. if (ret != MP_OKAY)
  42245. return WC_TEST_RET_ENC_EC(ret);
  42246. ret = mp_cmp(r, exp);
  42247. if (ret != MP_EQ)
  42248. return WC_TEST_RET_ENC_NC;
  42249. ret = mp_lcm(b, a, r);
  42250. if (ret != MP_OKAY)
  42251. return WC_TEST_RET_ENC_EC(ret);
  42252. ret = mp_cmp(r, exp);
  42253. if (ret != MP_EQ)
  42254. return WC_TEST_RET_ENC_NC;
  42255. #endif
  42256. mp_set(a, 11);
  42257. mp_zero(b);
  42258. ret = mp_gcd(a, b, r);
  42259. if (ret != MP_OKAY)
  42260. return WC_TEST_RET_ENC_EC(ret);
  42261. ret = mp_cmp_d(r, 11);
  42262. if (ret != MP_EQ)
  42263. return WC_TEST_RET_ENC_EC(ret);
  42264. ret = mp_gcd(b, a, r);
  42265. if (ret != MP_OKAY)
  42266. return WC_TEST_RET_ENC_EC(ret);
  42267. ret = mp_cmp_d(r, 11);
  42268. if (ret != MP_EQ)
  42269. return WC_TEST_RET_ENC_EC(ret);
  42270. ret = mp_gcd(b, b, r);
  42271. if (ret != MP_VAL)
  42272. return WC_TEST_RET_ENC_EC(ret);
  42273. return 0;
  42274. }
  42275. #endif
  42276. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  42277. defined(WOLFSSL_SP_MATH_ALL)
  42278. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  42279. {
  42280. wc_test_ret_t ret;
  42281. int i;
  42282. int j;
  42283. mp_set(a, 10);
  42284. ret = mp_mod_2d(a, 0, r);
  42285. if (ret != MP_OKAY)
  42286. return WC_TEST_RET_ENC_EC(ret);
  42287. if (!mp_iszero(r))
  42288. return WC_TEST_RET_ENC_NC;
  42289. ret = mp_mod_2d(a, 1, r);
  42290. if (ret != MP_OKAY)
  42291. return WC_TEST_RET_ENC_EC(ret);
  42292. if (!mp_iszero(r))
  42293. return WC_TEST_RET_ENC_NC;
  42294. ret = mp_mod_2d(a, 2, r);
  42295. if (ret != MP_OKAY)
  42296. return WC_TEST_RET_ENC_EC(ret);
  42297. ret = mp_cmp_d(r, 2);
  42298. if (ret != 0)
  42299. return WC_TEST_RET_ENC_EC(ret);
  42300. for (i = 2; i < 20; i++) {
  42301. ret = randNum(a, i, rng, NULL);
  42302. if (ret != 0)
  42303. return WC_TEST_RET_ENC_EC(ret);
  42304. for (j = 1; j <= mp_count_bits(a); j++) {
  42305. /* Get top part */
  42306. ret = mp_div_2d(a, j, t, NULL);
  42307. if (ret != 0)
  42308. return WC_TEST_RET_ENC_EC(ret);
  42309. ret = mp_mul_2d(t, j, t);
  42310. if (ret != 0)
  42311. return WC_TEST_RET_ENC_EC(ret);
  42312. /* Get bottom part */
  42313. ret = mp_mod_2d(a, j, r);
  42314. if (ret != 0)
  42315. return WC_TEST_RET_ENC_EC(ret);
  42316. /* Reassemble */
  42317. ret = mp_add(t, r, r);
  42318. if (ret != 0)
  42319. return WC_TEST_RET_ENC_EC(ret);
  42320. ret = mp_cmp(a, r);
  42321. if (ret != MP_EQ)
  42322. return WC_TEST_RET_ENC_NC;
  42323. }
  42324. }
  42325. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  42326. /* Test negative value being moded. */
  42327. for (j = 0; j < 20; j++) {
  42328. ret = randNum(a, 2, rng, NULL);
  42329. if (ret != 0)
  42330. return WC_TEST_RET_ENC_EC(ret);
  42331. a->sign = MP_NEG;
  42332. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  42333. ret = mp_mod_2d(a, i, r);
  42334. if (ret != 0)
  42335. return WC_TEST_RET_ENC_EC(ret);
  42336. mp_zero(t);
  42337. ret = mp_set_bit(t, i);
  42338. if (ret != 0)
  42339. return WC_TEST_RET_ENC_EC(ret);
  42340. ret = mp_mod(a, t, t);
  42341. if (ret != 0)
  42342. return WC_TEST_RET_ENC_EC(ret);
  42343. ret = mp_cmp(r, t);
  42344. if (ret != MP_EQ)
  42345. return WC_TEST_RET_ENC_NC;
  42346. }
  42347. }
  42348. #endif
  42349. return 0;
  42350. }
  42351. #endif
  42352. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  42353. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  42354. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  42355. {
  42356. wc_test_ret_t ret;
  42357. mp_digit r;
  42358. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  42359. mp_digit rem;
  42360. int i;
  42361. #endif
  42362. (void)rng;
  42363. ret = mp_set(a, 1);
  42364. if (ret != MP_OKAY)
  42365. return WC_TEST_RET_ENC_EC(ret);
  42366. ret = mp_mod_d(a, 0, &r);
  42367. if (ret != MP_VAL)
  42368. return WC_TEST_RET_ENC_EC(ret);
  42369. mp_zero(a);
  42370. ret = mp_mod_d(a, 1, &r);
  42371. if (ret != MP_OKAY)
  42372. return WC_TEST_RET_ENC_EC(ret);
  42373. ret = mp_mod_d(a, 3, &r);
  42374. if (ret != MP_OKAY)
  42375. return WC_TEST_RET_ENC_EC(ret);
  42376. ret = mp_mod_d(a, 5, &r);
  42377. if (ret != MP_OKAY)
  42378. return WC_TEST_RET_ENC_EC(ret);
  42379. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  42380. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  42381. ret = randNum(a, i, rng, NULL);
  42382. if (ret != MP_OKAY)
  42383. return WC_TEST_RET_ENC_EC(ret);
  42384. ret = mp_mod_d(a, 3, &r);
  42385. if (ret != MP_OKAY)
  42386. return WC_TEST_RET_ENC_EC(ret);
  42387. ret = mp_div_d(a, 3, a, &rem);
  42388. if (ret != MP_OKAY)
  42389. return WC_TEST_RET_ENC_EC(ret);
  42390. if (r != rem)
  42391. return WC_TEST_RET_ENC_NC;
  42392. }
  42393. #endif
  42394. return 0;
  42395. }
  42396. #endif
  42397. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  42398. WC_RNG* rng)
  42399. {
  42400. wc_test_ret_t ret;
  42401. int i;
  42402. for (i = 1; i < 16; i++) {
  42403. ret = randNum(a, i, rng, NULL);
  42404. if (ret != 0)
  42405. return WC_TEST_RET_ENC_EC(ret);
  42406. ret = mp_mul(a, a, r1);
  42407. if (ret != 0)
  42408. return WC_TEST_RET_ENC_EC(ret);
  42409. ret = mp_sqr(a, r2);
  42410. if (ret != 0)
  42411. return WC_TEST_RET_ENC_EC(ret);
  42412. ret = mp_cmp(r1, r2);
  42413. if (ret != MP_EQ)
  42414. return WC_TEST_RET_ENC_NC;
  42415. }
  42416. ret = mp_set(b, 0);
  42417. if (ret != MP_OKAY)
  42418. return WC_TEST_RET_ENC_EC(ret);
  42419. ret = mp_mul(a, b, r1);
  42420. if (ret != MP_OKAY)
  42421. return WC_TEST_RET_ENC_EC(ret);
  42422. if (!mp_iszero(r1))
  42423. return WC_TEST_RET_ENC_EC(ret);
  42424. ret = mp_sqr(b, r1);
  42425. if (ret != MP_OKAY)
  42426. return WC_TEST_RET_ENC_EC(ret);
  42427. if (!mp_iszero(r1))
  42428. return WC_TEST_RET_ENC_NC;
  42429. #ifdef WOLFSSL_SP_MATH_ALL
  42430. ret = mp_set(a, 1);
  42431. if (ret != MP_OKAY)
  42432. return WC_TEST_RET_ENC_EC(ret);
  42433. i = (SP_INT_DIGITS / 2) + 1;
  42434. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  42435. if (ret != MP_OKAY)
  42436. return WC_TEST_RET_ENC_EC(ret);
  42437. ret = mp_set(b, 1);
  42438. if (ret != MP_OKAY)
  42439. return WC_TEST_RET_ENC_EC(ret);
  42440. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  42441. if (ret != MP_OKAY)
  42442. return WC_TEST_RET_ENC_EC(ret);
  42443. ret = mp_mul(a, b, r1);
  42444. if (ret != MP_OKAY)
  42445. return WC_TEST_RET_ENC_EC(ret);
  42446. ret = mp_mul(a, a, r1);
  42447. if (ret == MP_OKAY)
  42448. return WC_TEST_RET_ENC_NC;
  42449. ret = mp_sqr(a, r1);
  42450. if (ret == MP_OKAY)
  42451. return WC_TEST_RET_ENC_NC;
  42452. ret = mp_sqr(b, r1);
  42453. if (ret != MP_OKAY)
  42454. return WC_TEST_RET_ENC_EC(ret);
  42455. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  42456. (defined(HAVE_ECC) && defined(FP_ECC))
  42457. ret = mp_mulmod(a, b, b, r1);
  42458. if (ret != MP_OKAY)
  42459. return WC_TEST_RET_ENC_EC(ret);
  42460. ret = mp_mulmod(a, a, b, r1);
  42461. if (ret == MP_OKAY)
  42462. return WC_TEST_RET_ENC_NC;
  42463. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  42464. ret = mp_sqrmod(a, b, r1);
  42465. if (ret == MP_OKAY)
  42466. return WC_TEST_RET_ENC_NC;
  42467. ret = mp_sqrmod(b, a, r1);
  42468. if (ret != MP_OKAY)
  42469. return WC_TEST_RET_ENC_EC(ret);
  42470. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  42471. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  42472. #endif /* WOLFSSL_SP_MATH_ALL */
  42473. return 0;
  42474. }
  42475. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  42476. defined(OPENSSL_EXTRA)
  42477. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  42478. {
  42479. wc_test_ret_t ret;
  42480. mp_set(a, 0);
  42481. mp_set(m, 1);
  42482. ret = mp_invmod(a, m, r);
  42483. if (ret != MP_VAL)
  42484. return WC_TEST_RET_ENC_EC(ret);
  42485. ret = mp_invmod(m, a, r);
  42486. if (ret != MP_VAL)
  42487. return WC_TEST_RET_ENC_EC(ret);
  42488. mp_set(a, 2);
  42489. mp_set(m, 4);
  42490. ret = mp_invmod(a, m, r);
  42491. if (ret != MP_VAL)
  42492. return WC_TEST_RET_ENC_EC(ret);
  42493. mp_set(a, 3);
  42494. mp_set(m, 6);
  42495. ret = mp_invmod(a, m, r);
  42496. if (ret != MP_VAL)
  42497. return WC_TEST_RET_ENC_EC(ret);
  42498. mp_set(a, 5*9);
  42499. mp_set(m, 6*9);
  42500. ret = mp_invmod(a, m, r);
  42501. if (ret != MP_VAL)
  42502. return WC_TEST_RET_ENC_EC(ret);
  42503. mp_set(a, 1);
  42504. mp_set(m, 4);
  42505. ret = mp_invmod(a, m, r);
  42506. if (ret != MP_OKAY)
  42507. return WC_TEST_RET_ENC_EC(ret);
  42508. if (!mp_isone(r))
  42509. return WC_TEST_RET_ENC_NC;
  42510. mp_set(a, 3);
  42511. mp_set(m, 4);
  42512. ret = mp_invmod(a, m, r);
  42513. if (ret != MP_OKAY)
  42514. return WC_TEST_RET_ENC_EC(ret);
  42515. ret = mp_cmp_d(r, 3);
  42516. if (ret != 0)
  42517. return WC_TEST_RET_ENC_EC(ret);
  42518. mp_set(a, 3);
  42519. mp_set(m, 5);
  42520. ret = mp_invmod(a, m, r);
  42521. if (ret != MP_OKAY)
  42522. return WC_TEST_RET_ENC_EC(ret);
  42523. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  42524. /* Maximum 'a' */
  42525. mp_set(a, 0);
  42526. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  42527. mp_sub_d(a, 1, a);
  42528. /* Modulus too big. */
  42529. mp_set(m, 0);
  42530. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  42531. ret = mp_invmod(a, m, r);
  42532. if (ret != MP_VAL)
  42533. return WC_TEST_RET_ENC_EC(ret);
  42534. /* Maximum modulus - even. */
  42535. mp_set(m, 0);
  42536. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  42537. ret = mp_invmod(a, m, r);
  42538. if (ret != MP_OKAY)
  42539. return WC_TEST_RET_ENC_EC(ret);
  42540. #endif
  42541. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  42542. mp_read_radix(a, "-3", 16);
  42543. ret = mp_invmod(a, m, r);
  42544. if (ret != MP_OKAY)
  42545. return WC_TEST_RET_ENC_EC(ret);
  42546. #endif
  42547. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  42548. mp_set(a, 0);
  42549. mp_set(m, 3);
  42550. ret = mp_invmod_mont_ct(a, m, r, 1);
  42551. if (ret != MP_VAL)
  42552. return WC_TEST_RET_ENC_EC(ret);
  42553. mp_set(a, 1);
  42554. mp_set(m, 0);
  42555. ret = mp_invmod_mont_ct(a, m, r, 1);
  42556. if (ret != MP_VAL)
  42557. return WC_TEST_RET_ENC_EC(ret);
  42558. mp_set(a, 1);
  42559. mp_set(m, 1);
  42560. ret = mp_invmod_mont_ct(a, m, r, 1);
  42561. if (ret != MP_VAL)
  42562. return WC_TEST_RET_ENC_EC(ret);
  42563. mp_set(a, 1);
  42564. mp_set(m, 2);
  42565. ret = mp_invmod_mont_ct(a, m, r, 1);
  42566. if (ret != MP_VAL)
  42567. return WC_TEST_RET_ENC_EC(ret);
  42568. mp_set(a, 1);
  42569. mp_set(m, 3);
  42570. ret = mp_invmod_mont_ct(a, m, r, 1);
  42571. if (ret != MP_OKAY)
  42572. return WC_TEST_RET_ENC_EC(ret);
  42573. #endif
  42574. return 0;
  42575. }
  42576. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  42577. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  42578. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  42579. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  42580. {
  42581. wc_test_ret_t ret;
  42582. mp_set(b, 0x2);
  42583. mp_set(e, 0x3);
  42584. mp_set(m, 0x0);
  42585. ret = mp_exptmod_ex(b, e, 1, m, r);
  42586. if (ret != MP_VAL)
  42587. return WC_TEST_RET_ENC_EC(ret);
  42588. ret = mp_exptmod_nct(b, e, m, r);
  42589. if (ret != MP_VAL)
  42590. return WC_TEST_RET_ENC_EC(ret);
  42591. mp_set(b, 0x2);
  42592. mp_set(e, 0x3);
  42593. mp_set(m, 0x1);
  42594. ret = mp_exptmod_ex(b, e, 1, m, r);
  42595. if (ret != MP_OKAY)
  42596. return WC_TEST_RET_ENC_EC(ret);
  42597. if (!mp_iszero(r))
  42598. return WC_TEST_RET_ENC_NC;
  42599. ret = mp_exptmod_nct(b, e, m, r);
  42600. if (ret != MP_OKAY)
  42601. return WC_TEST_RET_ENC_EC(ret);
  42602. if (!mp_iszero(r))
  42603. return WC_TEST_RET_ENC_NC;
  42604. mp_set(b, 0x2);
  42605. mp_set(e, 0x0);
  42606. mp_set(m, 0x7);
  42607. ret = mp_exptmod_ex(b, e, 1, m, r);
  42608. if (ret != MP_OKAY)
  42609. return WC_TEST_RET_ENC_EC(ret);
  42610. if (!mp_isone(r))
  42611. return WC_TEST_RET_ENC_NC;
  42612. ret = mp_exptmod_nct(b, e, m, r);
  42613. if (ret != MP_OKAY)
  42614. return WC_TEST_RET_ENC_EC(ret);
  42615. if (!mp_isone(r))
  42616. return WC_TEST_RET_ENC_NC;
  42617. mp_set(b, 0x0);
  42618. mp_set(e, 0x3);
  42619. mp_set(m, 0x7);
  42620. ret = mp_exptmod_ex(b, e, 1, m, r);
  42621. if (ret != MP_OKAY)
  42622. return WC_TEST_RET_ENC_EC(ret);
  42623. if (!mp_iszero(r))
  42624. return WC_TEST_RET_ENC_NC;
  42625. ret = mp_exptmod_nct(b, e, m, r);
  42626. if (ret != MP_OKAY)
  42627. return WC_TEST_RET_ENC_EC(ret);
  42628. if (!mp_iszero(r))
  42629. return WC_TEST_RET_ENC_NC;
  42630. mp_set(b, 0x10);
  42631. mp_set(e, 0x3);
  42632. mp_set(m, 0x7);
  42633. ret = mp_exptmod_ex(b, e, 1, m, r);
  42634. if (ret != MP_OKAY)
  42635. return WC_TEST_RET_ENC_EC(ret);
  42636. ret = mp_exptmod_nct(b, e, m, r);
  42637. if (ret != MP_OKAY)
  42638. return WC_TEST_RET_ENC_EC(ret);
  42639. mp_set(b, 0x7);
  42640. mp_set(e, 0x3);
  42641. mp_set(m, 0x7);
  42642. ret = mp_exptmod_ex(b, e, 1, m, r);
  42643. if (ret != MP_OKAY)
  42644. return WC_TEST_RET_ENC_EC(ret);
  42645. if (!mp_iszero(r))
  42646. return WC_TEST_RET_ENC_NC;
  42647. ret = mp_exptmod_nct(b, e, m, r);
  42648. if (ret != MP_OKAY)
  42649. return WC_TEST_RET_ENC_EC(ret);
  42650. if (!mp_iszero(r))
  42651. return WC_TEST_RET_ENC_NC;
  42652. #ifndef WOLFSSL_SP_MATH
  42653. mp_set(b, 0x01);
  42654. mp_mul_2d(b, DIGIT_BIT, b);
  42655. mp_add_d(b, 1, b);
  42656. mp_set(e, 0x3);
  42657. mp_copy(b, m);
  42658. ret = mp_exptmod_ex(b, e, 1, m, r);
  42659. if (ret != MP_OKAY)
  42660. return WC_TEST_RET_ENC_EC(ret);
  42661. if (!mp_iszero(r))
  42662. return WC_TEST_RET_ENC_NC;
  42663. ret = mp_exptmod_nct(b, e, m, r);
  42664. if (ret != MP_OKAY)
  42665. return WC_TEST_RET_ENC_EC(ret);
  42666. if (!mp_iszero(r))
  42667. return WC_TEST_RET_ENC_NC;
  42668. #endif
  42669. mp_set(b, 0x2);
  42670. mp_set(e, 0x3);
  42671. mp_set(m, 0x7);
  42672. ret = mp_exptmod_ex(b, e, 1, m, r);
  42673. if (ret != MP_OKAY)
  42674. return WC_TEST_RET_ENC_EC(ret);
  42675. ret = mp_exptmod_nct(b, e, m, r);
  42676. if (ret != MP_OKAY)
  42677. return WC_TEST_RET_ENC_EC(ret);
  42678. #ifdef WOLFSSL_SP_MATH_ALL
  42679. mp_set(b, 0x2);
  42680. mp_set(e, 0x3);
  42681. mp_set(m, 0x01);
  42682. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  42683. mp_add_d(m, 0x01, m);
  42684. ret = mp_exptmod_ex(b, e, 1, m, r);
  42685. if (ret != MP_VAL)
  42686. return WC_TEST_RET_ENC_EC(ret);
  42687. ret = mp_exptmod_nct(b, e, m, r);
  42688. if (ret != MP_VAL)
  42689. return WC_TEST_RET_ENC_EC(ret);
  42690. #endif
  42691. return 0;
  42692. }
  42693. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  42694. * OPENSSL_EXTRA */
  42695. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  42696. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  42697. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  42698. {
  42699. wc_test_ret_t ret;
  42700. mp_digit mp;
  42701. static int exp[] = { 7, 8, 16, 27, 32, 64,
  42702. 127, 128, 255, 256,
  42703. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  42704. 383, 384, 2033, 2048
  42705. #endif
  42706. };
  42707. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  42708. 0x01, 0x9f, 0x13, 0xbd,
  42709. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  42710. 0x1f, 0x13d, 0x45, 0x615
  42711. #endif
  42712. };
  42713. int bits[] = { 256, 384,
  42714. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  42715. 2048,
  42716. #endif
  42717. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  42718. 3072
  42719. #endif
  42720. };
  42721. int i;
  42722. int j;
  42723. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  42724. if (exp[i] >= DIGIT_BIT)
  42725. continue;
  42726. mp_zero(m);
  42727. ret = mp_set_bit(m, exp[i]);
  42728. if (ret != MP_OKAY)
  42729. return WC_TEST_RET_ENC_EC(ret);
  42730. ret = mp_sub_d(m, sub[i], m);
  42731. if (ret != MP_OKAY)
  42732. return WC_TEST_RET_ENC_EC(ret);
  42733. ret = mp_montgomery_setup(m, &mp);
  42734. if (ret != MP_OKAY)
  42735. return WC_TEST_RET_ENC_EC(ret);
  42736. ret = mp_montgomery_calc_normalization(n, m);
  42737. if (ret != MP_OKAY)
  42738. return WC_TEST_RET_ENC_EC(ret);
  42739. for (j = 0; j < 10; j++) {
  42740. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  42741. if (ret != 0)
  42742. return WC_TEST_RET_ENC_EC(ret);
  42743. ret = mp_mod(a, m, a);
  42744. if (ret != 0)
  42745. return WC_TEST_RET_ENC_EC(ret);
  42746. /* r = a * a */
  42747. ret = mp_sqrmod(a, m, r);
  42748. if (ret != MP_OKAY)
  42749. return WC_TEST_RET_ENC_EC(ret);
  42750. /* Convert to Montgomery form = a*n */
  42751. ret = mp_mulmod(a, n, m, a);
  42752. if (ret != MP_OKAY)
  42753. return WC_TEST_RET_ENC_EC(ret);
  42754. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  42755. ret = mp_sqr(a, a);
  42756. if (ret != MP_OKAY)
  42757. return WC_TEST_RET_ENC_EC(ret);
  42758. ret = mp_montgomery_reduce(a, m, mp);
  42759. if (ret != MP_OKAY)
  42760. return WC_TEST_RET_ENC_EC(ret);
  42761. ret = mp_montgomery_reduce(a, m, mp);
  42762. if (ret != MP_OKAY)
  42763. return WC_TEST_RET_ENC_EC(ret);
  42764. if (mp_cmp(a, r) != MP_EQ)
  42765. return WC_TEST_RET_ENC_NC;
  42766. }
  42767. }
  42768. /* Force carries. */
  42769. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  42770. /* a = 2^(bits*2) - 1 */
  42771. mp_zero(a);
  42772. mp_set_bit(a, bits[i] * 2);
  42773. mp_sub_d(a, 1, a);
  42774. /* m = 2^(bits) - 1 */
  42775. mp_zero(m);
  42776. mp_set_bit(m, bits[i]);
  42777. mp_sub_d(m, 1, m);
  42778. mp = 1;
  42779. /* result = r = 2^(bits) - 1 */
  42780. mp_zero(r);
  42781. mp_set_bit(r, bits[i]);
  42782. mp_sub_d(r, 1, r);
  42783. ret = mp_montgomery_reduce(a, m, mp);
  42784. if (ret != MP_OKAY)
  42785. return WC_TEST_RET_ENC_EC(ret);
  42786. /* Result is m or 0 if reduced to range of modulus. */
  42787. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  42788. return WC_TEST_RET_ENC_NC;
  42789. }
  42790. return 0;
  42791. }
  42792. #endif
  42793. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  42794. {
  42795. WC_RNG rng;
  42796. int rng_inited = 0;
  42797. wc_test_ret_t ret;
  42798. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  42799. int i, j;
  42800. #ifndef WOLFSSL_SP_MATH
  42801. int k;
  42802. #endif
  42803. mp_digit d = 0;
  42804. #endif
  42805. #ifdef WOLFSSL_SMALL_STACK
  42806. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42807. DYNAMIC_TYPE_TMP_BUFFER),
  42808. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42809. DYNAMIC_TYPE_TMP_BUFFER),
  42810. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42811. DYNAMIC_TYPE_TMP_BUFFER),
  42812. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42813. DYNAMIC_TYPE_TMP_BUFFER),
  42814. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  42815. DYNAMIC_TYPE_TMP_BUFFER);
  42816. if ((a == NULL) ||
  42817. (b == NULL) ||
  42818. (r1 == NULL) ||
  42819. (r2 == NULL) ||
  42820. (p == NULL))
  42821. {
  42822. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  42823. }
  42824. #else
  42825. mp_int a[1], b[1], r1[1], r2[1], p[1];
  42826. #endif
  42827. WOLFSSL_ENTER("mp_test");
  42828. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  42829. if (ret != 0)
  42830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42831. #ifdef WOLFSSL_SP_MATH_ALL
  42832. mp_init_copy(p, a);
  42833. #else
  42834. ret = mp_init(p);
  42835. if (ret != 0)
  42836. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42837. #endif
  42838. #ifndef HAVE_FIPS
  42839. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42840. #else
  42841. ret = wc_InitRng(&rng);
  42842. #endif
  42843. if (ret != 0)
  42844. goto done;
  42845. rng_inited = 1;
  42846. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  42847. mp_set_int(a, 0);
  42848. if (a->used != 0 || a->dp[0] != 0)
  42849. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42850. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  42851. for (i = 0; i < 4 * j; i++) {
  42852. /* New values to use. */
  42853. ret = randNum(p, j, &rng, NULL);
  42854. if (ret != 0)
  42855. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42856. ret = randNum(a, j, &rng, NULL);
  42857. if (ret != 0)
  42858. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42859. ret = randNum(b, j, &rng, NULL);
  42860. if (ret != 0)
  42861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42862. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  42863. if (ret != 0)
  42864. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42865. d &= MP_MASK;
  42866. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  42867. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  42868. /* Ensure sqrmod produce same result as mulmod. */
  42869. ret = mp_sqrmod(a, p, r1);
  42870. if (ret != 0)
  42871. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42872. ret = mp_mulmod(a, a, p, r2);
  42873. if (ret != 0)
  42874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42875. if (mp_cmp(r1, r2) != 0) {
  42876. WOLFSSL_MSG("Fail: mp_mulmod result does not match mp_sqrmod!");
  42877. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42878. }
  42879. #endif
  42880. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  42881. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  42882. !defined(WOLFSSL_SP_INT_NEGATIVE))
  42883. ret = mp_addmod(a, b, p, r1);
  42884. if (ret != 0)
  42885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42886. ret = mp_submod(r1, b, p, r2);
  42887. if (ret != 0)
  42888. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42889. ret = mp_mod(a, p, r1);
  42890. if (ret != 0)
  42891. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42892. if (mp_cmp(r1, r2) != MP_EQ)
  42893. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42894. #else
  42895. /* Ensure add with mod produce same result as sub with mod. */
  42896. ret = mp_addmod(a, b, p, r1);
  42897. if (ret != 0)
  42898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42899. b->sign ^= 1;
  42900. ret = mp_submod(a, b, p, r2);
  42901. if (ret != 0)
  42902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42903. if (mp_cmp(r1, r2) != 0)
  42904. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42905. #endif
  42906. #endif
  42907. /* Ensure add digit produce same result as sub digit. */
  42908. ret = mp_add_d(a, d, r1);
  42909. if (ret != 0)
  42910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42911. ret = mp_sub_d(r1, d, r2);
  42912. if (ret != 0)
  42913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42914. if (mp_cmp(a, r2) != 0)
  42915. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42916. /* Invert - if p is even it will use the slow impl.
  42917. * - if p and a are even it will fail.
  42918. */
  42919. ret = mp_invmod(a, p, r1);
  42920. if (ret != 0 && ret != MP_VAL)
  42921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42922. #ifndef WOLFSSL_SP_MATH
  42923. /* Shift up and down number all bits in a digit. */
  42924. for (k = 0; k < DIGIT_BIT; k++) {
  42925. mp_mul_2d(a, k, r1);
  42926. mp_div_2d(r1, k, r2, p);
  42927. if (mp_cmp(a, r2) != 0)
  42928. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42929. if (!mp_iszero(p))
  42930. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42931. mp_rshb(r1, k);
  42932. if (mp_cmp(a, r1) != 0)
  42933. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42934. }
  42935. #endif
  42936. }
  42937. }
  42938. /* Test adding and subtracting zero from zero. */
  42939. mp_zero(a);
  42940. ret = mp_add_d(a, 0, r1);
  42941. if (ret != 0)
  42942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42943. if (!mp_iszero(r1)) {
  42944. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42945. }
  42946. ret = mp_sub_d(a, 0, r2);
  42947. if (ret != 0)
  42948. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  42949. if (!mp_iszero(r2)) {
  42950. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42951. }
  42952. #if DIGIT_BIT >= 32
  42953. /* Check that setting a 32-bit digit works. */
  42954. d &= 0xffffffffU;
  42955. mp_set_int(a, d);
  42956. if (a->used != 1 || a->dp[0] != d)
  42957. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42958. #endif
  42959. /* Check setting a bit and testing a bit works. */
  42960. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  42961. mp_zero(a);
  42962. mp_set_bit(a, i);
  42963. if (!mp_is_bit_set(a, i))
  42964. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  42965. }
  42966. #endif
  42967. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  42968. mp_zero(a);
  42969. i = mp_cnt_lsb(a);
  42970. if (i != 0)
  42971. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42972. mp_set(a, 1);
  42973. i = mp_cnt_lsb(a);
  42974. if (i != 0)
  42975. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42976. mp_set(a, 32);
  42977. i = mp_cnt_lsb(a);
  42978. if (i != 5)
  42979. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42980. mp_zero(a);
  42981. mp_set_bit(a, 129);
  42982. i = mp_cnt_lsb(a);
  42983. if (i != 129)
  42984. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  42985. #endif
  42986. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  42987. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  42988. goto done;
  42989. #endif
  42990. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  42991. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  42992. goto done;
  42993. #endif
  42994. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  42995. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42996. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  42997. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  42998. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  42999. goto done;
  43000. #endif
  43001. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  43002. defined(HAVE_ECC))
  43003. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  43004. goto done;
  43005. #endif
  43006. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  43007. goto done;
  43008. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  43009. goto done;
  43010. if ((ret = mp_test_read_to_bin(a)) != 0)
  43011. goto done;
  43012. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43013. if ((ret = mp_test_set_int(a)) != 0)
  43014. goto done;
  43015. #endif
  43016. if ((ret = mp_test_cmp(a, r1)) != 0)
  43017. goto done;
  43018. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43019. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  43020. goto done;
  43021. #endif
  43022. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43023. if ((ret = mp_test_set_is_bit(a)) != 0)
  43024. goto done;
  43025. #endif
  43026. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  43027. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  43028. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  43029. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  43030. goto done;
  43031. #endif
  43032. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  43033. !defined(WC_NO_RNG)
  43034. if ((ret = mp_test_prime(a, &rng)) != 0)
  43035. goto done;
  43036. #endif
  43037. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  43038. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  43039. goto done;
  43040. #endif
  43041. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  43042. defined(WOLFSSL_SP_MATH_ALL)
  43043. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  43044. goto done;
  43045. #endif
  43046. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  43047. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  43048. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  43049. goto done;
  43050. #endif
  43051. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  43052. goto done;
  43053. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  43054. defined(OPENSSL_EXTRA)
  43055. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  43056. goto done;
  43057. #endif
  43058. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  43059. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  43060. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  43061. goto done;
  43062. #endif
  43063. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  43064. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43065. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  43066. goto done;
  43067. #endif
  43068. done:
  43069. #ifdef WOLFSSL_SMALL_STACK
  43070. if (p) {
  43071. mp_clear(p);
  43072. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43073. }
  43074. if (r2) {
  43075. mp_clear(r2);
  43076. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43077. }
  43078. if (r1) {
  43079. mp_clear(r1);
  43080. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43081. }
  43082. if (b) {
  43083. mp_clear(b);
  43084. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43085. }
  43086. if (a) {
  43087. mp_clear(a);
  43088. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43089. }
  43090. #else
  43091. mp_clear(p);
  43092. mp_clear(r2);
  43093. mp_clear(r1);
  43094. mp_clear(b);
  43095. mp_clear(a);
  43096. #endif
  43097. if (rng_inited)
  43098. wc_FreeRng(&rng);
  43099. return ret;
  43100. }
  43101. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  43102. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  43103. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  43104. typedef struct pairs_t {
  43105. const unsigned char* coeff;
  43106. int coeffSz;
  43107. int exp;
  43108. } pairs_t;
  43109. /*
  43110. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  43111. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  43112. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  43113. */
  43114. static const unsigned char c192a[] =
  43115. {
  43116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  43117. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  43118. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  43119. };
  43120. static const unsigned char c0a[] =
  43121. {
  43122. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  43123. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  43124. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  43125. };
  43126. static const pairs_t ecPairsA[] =
  43127. {
  43128. {c192a, sizeof(c192a), 192},
  43129. {c0a, sizeof(c0a), 0}
  43130. };
  43131. static const int kA[] = {173, 293};
  43132. static const unsigned char controlPrime[] = {
  43133. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  43134. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  43135. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  43136. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  43137. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  43138. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  43139. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  43140. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  43141. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  43142. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  43143. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  43144. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  43145. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  43146. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  43147. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  43148. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  43149. };
  43150. static const unsigned char testOne[] = { 1 };
  43151. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  43152. {
  43153. wc_test_ret_t ret;
  43154. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43155. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43156. if (ki == NULL)
  43157. return MEMORY_E;
  43158. #else
  43159. mp_int ki[1];
  43160. #endif
  43161. ret = mp_init(ki);
  43162. if (ret != 0)
  43163. ret = WC_TEST_RET_ENC_EC(ret);
  43164. if (ret == 0) {
  43165. ret = mp_set(ki, k);
  43166. if (ret != 0)
  43167. ret = WC_TEST_RET_ENC_EC(ret);
  43168. }
  43169. if (ret == 0) {
  43170. ret = mp_sub_d(p1, 1, p2);
  43171. if (ret != 0)
  43172. ret = WC_TEST_RET_ENC_EC(ret);
  43173. }
  43174. if (ret == 0) {
  43175. ret = mp_mul(p2, ki, p2);
  43176. if (ret != 0)
  43177. ret = WC_TEST_RET_ENC_EC(ret);
  43178. }
  43179. if (ret == 0) {
  43180. ret = mp_add_d(p2, 1, p2);
  43181. if (ret != 0)
  43182. ret = WC_TEST_RET_ENC_EC(ret);
  43183. }
  43184. mp_clear(ki);
  43185. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43186. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43187. #endif
  43188. return ret;
  43189. }
  43190. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  43191. const pairs_t* ecPairs, int ecPairsSz,
  43192. const int* k)
  43193. {
  43194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43195. mp_int *x = NULL, *y = NULL;
  43196. #else
  43197. mp_int x[1], y[1];
  43198. #endif
  43199. wc_test_ret_t ret;
  43200. int i;
  43201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43202. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  43203. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  43204. ret = MEMORY_E;
  43205. goto out;
  43206. }
  43207. #endif
  43208. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  43209. if (ret != 0) {
  43210. ret = WC_TEST_RET_ENC_EC(ret);
  43211. goto out;
  43212. }
  43213. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  43214. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  43215. if (ret != 0) {
  43216. ret = WC_TEST_RET_ENC_EC(ret);
  43217. break;
  43218. }
  43219. /* p1 = 2^exp */
  43220. ret = mp_2expt(y, ecPairs[i].exp);
  43221. if (ret != 0) {
  43222. ret = WC_TEST_RET_ENC_EC(ret);
  43223. break;
  43224. }
  43225. /* p1 = p1 * m */
  43226. ret = mp_mul(x, y, x);
  43227. if (ret != 0) {
  43228. ret = WC_TEST_RET_ENC_EC(ret);
  43229. break;
  43230. }
  43231. /* p1 += */
  43232. ret = mp_add(p1, x, p1);
  43233. if (ret != 0) {
  43234. ret = WC_TEST_RET_ENC_EC(ret);
  43235. break;
  43236. }
  43237. mp_zero(x);
  43238. mp_zero(y);
  43239. }
  43240. if (ret == 0)
  43241. ret = GenerateNextP(p1, p2, k[0]);
  43242. if (ret == 0)
  43243. ret = GenerateNextP(p1, p3, k[1]);
  43244. out:
  43245. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43246. if (x != NULL) {
  43247. mp_clear(x);
  43248. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43249. }
  43250. if (y != NULL) {
  43251. mp_clear(y);
  43252. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43253. }
  43254. #else
  43255. mp_clear(x);
  43256. mp_clear(y);
  43257. #endif
  43258. return ret;
  43259. }
  43260. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  43261. {
  43262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43263. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  43264. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  43265. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  43266. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43267. #else
  43268. mp_int n[1],
  43269. p1[1],
  43270. p2[1],
  43271. p3[1];
  43272. #endif
  43273. wc_test_ret_t ret;
  43274. int isPrime = 0;
  43275. WC_RNG rng;
  43276. WOLFSSL_ENTER("prime_test");
  43277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43278. if ((n == NULL) ||
  43279. (p1 == NULL) ||
  43280. (p2 == NULL) ||
  43281. (p3 == NULL))
  43282. ERROR_OUT(MEMORY_E, out);
  43283. #endif
  43284. ret = wc_InitRng(&rng);
  43285. if (ret != 0)
  43286. ret = WC_TEST_RET_ENC_EC(ret);
  43287. if (ret == 0) {
  43288. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  43289. if (ret != 0)
  43290. ret = WC_TEST_RET_ENC_EC(ret);
  43291. }
  43292. if (ret == 0)
  43293. ret = GenerateP(p1, p2, p3,
  43294. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  43295. if (ret == 0) {
  43296. ret = mp_mul(p1, p2, n);
  43297. if (ret != 0)
  43298. ret = WC_TEST_RET_ENC_EC(ret);
  43299. }
  43300. if (ret == 0) {
  43301. ret = mp_mul(n, p3, n);
  43302. if (ret != 0)
  43303. ret = WC_TEST_RET_ENC_EC(ret);
  43304. }
  43305. if (ret != 0)
  43306. ERROR_OUT(ret, out);
  43307. /* Check the old prime test using the number that false positives.
  43308. * This test result should indicate as not prime. */
  43309. ret = mp_prime_is_prime(n, 40, &isPrime);
  43310. if (ret != 0)
  43311. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43312. if (isPrime)
  43313. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43314. /* This test result should fail. It should indicate the value as prime. */
  43315. ret = mp_prime_is_prime(n, 8, &isPrime);
  43316. if (ret != 0)
  43317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43318. if (!isPrime)
  43319. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43320. /* This test result should indicate the value as not prime. */
  43321. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  43322. if (ret != 0)
  43323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43324. if (isPrime)
  43325. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43326. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  43327. if (ret != 0)
  43328. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43329. /* This test result should indicate the value as prime. */
  43330. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  43331. if (ret != 0)
  43332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43333. if (!isPrime)
  43334. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43335. /* This test result should indicate the value as prime. */
  43336. isPrime = -1;
  43337. ret = mp_prime_is_prime(n, 8, &isPrime);
  43338. if (ret != 0)
  43339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43340. if (!isPrime)
  43341. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43342. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  43343. if (ret != 0)
  43344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43345. /* This test result should indicate the value as not prime. */
  43346. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  43347. if (ret != 0)
  43348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43349. if (isPrime)
  43350. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43351. ret = mp_prime_is_prime(n, 8, &isPrime);
  43352. if (ret != 0)
  43353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43354. if (isPrime)
  43355. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43356. ret = 0;
  43357. out:
  43358. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43359. if (n != NULL) {
  43360. mp_clear(n);
  43361. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43362. }
  43363. if (p1 != NULL) {
  43364. mp_clear(p1);
  43365. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43366. }
  43367. if (p2 != NULL) {
  43368. mp_clear(p2);
  43369. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43370. }
  43371. if (p3 != NULL) {
  43372. mp_clear(p3);
  43373. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43374. }
  43375. #else
  43376. mp_clear(p3);
  43377. mp_clear(p2);
  43378. mp_clear(p1);
  43379. mp_clear(n);
  43380. #endif
  43381. wc_FreeRng(&rng);
  43382. return ret;
  43383. }
  43384. #endif /* WOLFSSL_PUBLIC_MP */
  43385. #if defined(ASN_BER_TO_DER) && \
  43386. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  43387. defined(OPENSSL_EXTRA_X509_SMALL))
  43388. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  43389. typedef struct berDerTestData {
  43390. const byte *in;
  43391. word32 inSz;
  43392. const byte *out;
  43393. word32 outSz;
  43394. } berDerTestData;
  43395. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  43396. {
  43397. wc_test_ret_t ret;
  43398. int i;
  43399. word32 len = 0, l;
  43400. byte out[32];
  43401. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  43402. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  43403. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  43404. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  43405. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  43406. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  43407. };
  43408. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  43409. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  43410. 0x30, 0x80,
  43411. 0x02, 0x01, 0x01,
  43412. 0x30, 0x80,
  43413. 0x24, 0x80,
  43414. 0x04, 0x01, 0x01,
  43415. 0x04, 0x02, 0x02, 0x03,
  43416. 0x00, 0x00,
  43417. 0x06, 0x01, 0x01,
  43418. 0x00, 0x00,
  43419. 0x31, 0x80,
  43420. 0x06, 0x01, 0x01,
  43421. 0x00, 0x00,
  43422. 0x00, 0x00,
  43423. };
  43424. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  43425. 0x30, 0x12,
  43426. 0x02, 0x01, 0x01,
  43427. 0x30, 0x08,
  43428. 0x04, 0x03, 0x01, 0x02, 0x03,
  43429. 0x06, 0x01, 0x01,
  43430. 0x31, 0x03,
  43431. 0x06, 0x01, 0x01
  43432. };
  43433. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  43434. berDerTestData testData[] = {
  43435. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  43436. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  43437. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  43438. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  43439. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  43440. };
  43441. WOLFSSL_ENTER("berder_test");
  43442. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  43443. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  43444. if (ret != LENGTH_ONLY_E)
  43445. return WC_TEST_RET_ENC_I(i);
  43446. if (len != testData[i].outSz)
  43447. return WC_TEST_RET_ENC_I(i);
  43448. len = testData[i].outSz;
  43449. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  43450. if (ret != 0)
  43451. return WC_TEST_RET_ENC_I(i);
  43452. if (XMEMCMP(out, testData[i].out, len) != 0)
  43453. return WC_TEST_RET_ENC_I(i);
  43454. for (l = 1; l < testData[i].inSz; l++) {
  43455. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  43456. if (ret != ASN_PARSE_E)
  43457. return WC_TEST_RET_ENC_EC(ret);
  43458. len = testData[i].outSz;
  43459. ret = wc_BerToDer(testData[i].in, l, out, &len);
  43460. if (ret != ASN_PARSE_E)
  43461. return WC_TEST_RET_ENC_EC(ret);
  43462. }
  43463. for (l = 0; l < testData[i].outSz-1; l++) {
  43464. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  43465. if (ret != BUFFER_E)
  43466. return WC_TEST_RET_ENC_EC(ret);
  43467. }
  43468. }
  43469. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  43470. if (ret != BAD_FUNC_ARG)
  43471. return WC_TEST_RET_ENC_EC(ret);
  43472. ret = wc_BerToDer(out, 4, NULL, NULL);
  43473. if (ret != BAD_FUNC_ARG)
  43474. return WC_TEST_RET_ENC_EC(ret);
  43475. ret = wc_BerToDer(NULL, 4, NULL, &len);
  43476. if (ret != BAD_FUNC_ARG)
  43477. return WC_TEST_RET_ENC_EC(ret);
  43478. ret = wc_BerToDer(NULL, 4, out, NULL);
  43479. if (ret != BAD_FUNC_ARG)
  43480. return WC_TEST_RET_ENC_EC(ret);
  43481. ret = wc_BerToDer(out, 4, out, NULL);
  43482. if (ret != BAD_FUNC_ARG)
  43483. return WC_TEST_RET_ENC_EC(ret);
  43484. ret = wc_BerToDer(NULL, 4, out, &len);
  43485. if (ret != BAD_FUNC_ARG)
  43486. return WC_TEST_RET_ENC_EC(ret);
  43487. for (l = 1; l < sizeof(good4_out); l++) {
  43488. len = l;
  43489. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  43490. if (ret != BUFFER_E)
  43491. return WC_TEST_RET_ENC_EC(ret);
  43492. }
  43493. return 0;
  43494. }
  43495. #endif
  43496. #ifdef DEBUG_WOLFSSL
  43497. static THREAD_LS_T int log_cnt = 0;
  43498. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  43499. {
  43500. (void)logLevel;
  43501. (void)logMessage;
  43502. log_cnt++;
  43503. }
  43504. #endif /* DEBUG_WOLFSSL */
  43505. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  43506. {
  43507. wc_test_ret_t ret;
  43508. #ifdef DEBUG_WOLFSSL
  43509. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  43510. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  43511. byte b[256];
  43512. int i;
  43513. WOLFSSL_ENTER("logging_test (debug)");
  43514. for (i = 0; i < (int)sizeof(b); i++)
  43515. b[i] = i;
  43516. ret = wolfSSL_Debugging_ON();
  43517. if (ret != 0)
  43518. return WC_TEST_RET_ENC_EC(ret);
  43519. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  43520. if (ret != 0)
  43521. return WC_TEST_RET_ENC_EC(ret);
  43522. WOLFSSL_MSG(msg);
  43523. WOLFSSL_BUFFER(a, sizeof(a));
  43524. WOLFSSL_BUFFER(b, sizeof(b));
  43525. WOLFSSL_BUFFER(NULL, 0);
  43526. WOLFSSL_ERROR(MEMORY_E);
  43527. WOLFSSL_ERROR_MSG(msg);
  43528. /* turn off logs */
  43529. wolfSSL_Debugging_OFF();
  43530. /* capture log count */
  43531. i = log_cnt;
  43532. /* validate no logs are output when disabled */
  43533. WOLFSSL_MSG(msg);
  43534. WOLFSSL_BUFFER(a, sizeof(a));
  43535. WOLFSSL_BUFFER(b, sizeof(b));
  43536. WOLFSSL_BUFFER(NULL, 0);
  43537. WOLFSSL_ERROR(MEMORY_E);
  43538. WOLFSSL_ERROR_MSG(msg);
  43539. /* check the logs were disabled */
  43540. if (i != log_cnt)
  43541. return WC_TEST_RET_ENC_NC;
  43542. /* restore callback and leave logging enabled */
  43543. wolfSSL_SetLoggingCb(NULL);
  43544. wolfSSL_Debugging_ON();
  43545. /* suppress unused args */
  43546. (void)a;
  43547. (void)b;
  43548. #else
  43549. WOLFSSL_ENTER("logging_test");
  43550. ret = wolfSSL_Debugging_ON();
  43551. if (ret != NOT_COMPILED_IN)
  43552. return WC_TEST_RET_ENC_EC(ret);
  43553. wolfSSL_Debugging_OFF();
  43554. ret = wolfSSL_SetLoggingCb(NULL);
  43555. if (ret != NOT_COMPILED_IN)
  43556. return WC_TEST_RET_ENC_EC(ret);
  43557. #endif /* DEBUG_WOLFSSL */
  43558. return 0;
  43559. }
  43560. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  43561. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  43562. #else
  43563. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  43564. #endif
  43565. {
  43566. #ifdef WOLFSSL_PTHREADS
  43567. wolfSSL_Mutex m;
  43568. #endif
  43569. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  43570. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  43571. wc_test_ret_t ret;
  43572. #endif
  43573. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  43574. #ifndef WOLFSSL_STATIC_MEMORY
  43575. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  43576. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (1)");
  43577. #else
  43578. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  43579. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  43580. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (2)");
  43581. if (mm != NULL) {
  43582. ret = wc_InitMutex(mm);
  43583. if (ret != 0) {
  43584. WOLFSSL_MSG("Init Mutex failed");
  43585. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  43586. return WC_TEST_RET_ENC_EC(ret);
  43587. }
  43588. }
  43589. #endif
  43590. if (mm == NULL)
  43591. return WC_TEST_RET_ENC_ERRNO;
  43592. wc_FreeMutex(mm);
  43593. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  43594. #endif
  43595. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  43596. #ifdef WOLFSSL_PTHREADS
  43597. ret = wc_InitMutex(&m);
  43598. if (ret != 0)
  43599. return WC_TEST_RET_ENC_EC(ret);
  43600. ret = wc_LockMutex(&m);
  43601. if (ret != 0)
  43602. return WC_TEST_RET_ENC_EC(ret);
  43603. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  43604. /* trying to free a locked mutex is not portable behavior with pthread */
  43605. /* Attempting to destroy a locked mutex results in undefined behavior */
  43606. ret = wc_FreeMutex(&m);
  43607. if (ret != BAD_MUTEX_E)
  43608. return WC_TEST_RET_ENC_EC(ret);
  43609. #endif
  43610. ret = wc_UnLockMutex(&m);
  43611. if (ret != 0)
  43612. return WC_TEST_RET_ENC_EC(ret);
  43613. ret = wc_FreeMutex(&m);
  43614. if (ret != 0)
  43615. return WC_TEST_RET_ENC_EC(ret);
  43616. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  43617. /* Trying to use a pthread after free'ing is not portable behavior */
  43618. ret = wc_LockMutex(&m);
  43619. if (ret != BAD_MUTEX_E)
  43620. return WC_TEST_RET_ENC_EC(ret);
  43621. ret = wc_UnLockMutex(&m);
  43622. if (ret != BAD_MUTEX_E)
  43623. return WC_TEST_RET_ENC_EC(ret);
  43624. #endif
  43625. #endif
  43626. return 0;
  43627. }
  43628. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  43629. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  43630. !defined(WOLFSSL_STATIC_MEMORY)
  43631. static wc_test_ret_t malloc_cnt = 0;
  43632. static wc_test_ret_t realloc_cnt = 0;
  43633. static wc_test_ret_t free_cnt = 0;
  43634. #ifdef WOLFSSL_DEBUG_MEMORY
  43635. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  43636. {
  43637. (void) func;
  43638. (void) line;
  43639. #else
  43640. static void *my_Malloc_cb(size_t size)
  43641. {
  43642. #endif
  43643. malloc_cnt++;
  43644. #ifndef WOLFSSL_NO_MALLOC
  43645. return malloc(size);
  43646. #else
  43647. WOLFSSL_MSG("No malloc available");
  43648. (void)size;
  43649. return NULL;
  43650. #endif
  43651. }
  43652. #ifdef WOLFSSL_DEBUG_MEMORY
  43653. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  43654. {
  43655. (void) func;
  43656. (void) line;
  43657. #else
  43658. static void my_Free_cb(void *ptr)
  43659. {
  43660. #endif
  43661. free_cnt++;
  43662. #ifndef WOLFSSL_NO_MALLOC
  43663. free(ptr);
  43664. #else
  43665. WOLFSSL_MSG("No free available");
  43666. (void)ptr;
  43667. #endif
  43668. }
  43669. #ifdef WOLFSSL_DEBUG_MEMORY
  43670. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  43671. {
  43672. (void) func;
  43673. (void) line;
  43674. #else
  43675. static void *my_Realloc_cb(void *ptr, size_t size)
  43676. {
  43677. #endif
  43678. realloc_cnt++;
  43679. #ifndef WOLFSSL_NO_MALLOC
  43680. return realloc(ptr, size);
  43681. #else
  43682. WOLFSSL_MSG("No realloc available");
  43683. (void)ptr;
  43684. (void)size;
  43685. return NULL;
  43686. #endif
  43687. }
  43688. #endif /* !WOLFSSL_NO_MALLOC */
  43689. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  43690. {
  43691. wc_test_ret_t ret = 0;
  43692. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  43693. !defined(WOLFSSL_STATIC_MEMORY)
  43694. byte* b = NULL;
  43695. #endif
  43696. wolfSSL_Malloc_cb mc;
  43697. wolfSSL_Free_cb fc;
  43698. wolfSSL_Realloc_cb rc;
  43699. WOLFSSL_ENTER("memcb_test");
  43700. /* Save existing memory callbacks */
  43701. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  43702. if (ret != 0)
  43703. return WC_TEST_RET_ENC_EC(ret);
  43704. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  43705. !defined(WOLFSSL_STATIC_MEMORY)
  43706. /* test realloc */
  43707. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43708. if (b == NULL) {
  43709. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  43710. }
  43711. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43712. b = NULL;
  43713. /* Use API. */
  43714. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  43715. (wolfSSL_Free_cb)my_Free_cb,
  43716. (wolfSSL_Realloc_cb)my_Realloc_cb);
  43717. if (ret != 0) {
  43718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  43719. }
  43720. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43721. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43722. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43723. #ifndef WOLFSSL_STATIC_MEMORY
  43724. #ifndef WOLFSSL_CHECK_MEM_ZERO
  43725. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  43726. #else
  43727. /* Checking zeroized memory means realloc is a malloc and free. */
  43728. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  43729. #endif
  43730. #else
  43731. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  43732. #endif
  43733. ret = WC_TEST_RET_ENC_NC;
  43734. #endif /* !WOLFSSL_NO_MALLOC */
  43735. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  43736. !defined(WOLFSSL_STATIC_MEMORY)
  43737. exit_memcb:
  43738. /* reset malloc/free/realloc counts */
  43739. malloc_cnt = 0;
  43740. free_cnt = 0;
  43741. realloc_cnt = 0;
  43742. #endif
  43743. /* restore memory callbacks */
  43744. wolfSSL_SetAllocators(mc, fc, rc);
  43745. return ret;
  43746. }
  43747. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  43748. #if defined(WOLFSSL_CAAM_BLOB)
  43749. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  43750. {
  43751. wc_test_ret_t ret = 0;
  43752. byte out[112];
  43753. byte blob[112];
  43754. word32 outSz;
  43755. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  43756. {
  43757. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  43758. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  43759. };
  43760. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  43761. {
  43762. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  43763. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  43764. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  43765. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  43766. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  43767. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  43768. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  43769. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  43770. };
  43771. WOLFSSL_ENTER("blob_test");
  43772. XMEMSET(blob, 0, sizeof(blob));
  43773. XMEMSET(out, 0, sizeof(out));
  43774. outSz = sizeof(blob);
  43775. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  43776. if (ret != 0)
  43777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43778. blob[outSz - 2] += 1;
  43779. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43780. if (ret == 0) { /* should fail with altered blob */
  43781. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43782. }
  43783. XMEMSET(blob, 0, sizeof(blob));
  43784. outSz = sizeof(blob);
  43785. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  43786. if (ret != 0)
  43787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43788. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43789. if (ret != 0)
  43790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43791. if (XMEMCMP(out, iv, sizeof(iv))) {
  43792. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43793. }
  43794. XMEMSET(blob, 0, sizeof(blob));
  43795. outSz = sizeof(blob);
  43796. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  43797. if (ret != 0)
  43798. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43799. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  43800. if (ret != 0)
  43801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  43802. if (XMEMCMP(out, text, sizeof(text))) {
  43803. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  43804. }
  43805. exit_blob:
  43806. return ret;
  43807. }
  43808. #endif /* WOLFSSL_CAAM_BLOB */
  43809. #ifdef WOLF_CRYPTO_CB
  43810. /* Example custom context for crypto callback */
  43811. typedef struct {
  43812. int exampleVar; /* flag for testing if only crypt is enabled. */
  43813. } myCryptoDevCtx;
  43814. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  43815. /* Testing rsa cb when CB_ONLY_RSA is enabled
  43816. * When CB_ONLY_RSA is enabled, software imple. is not available.
  43817. *
  43818. * ctx callback ctx
  43819. * returen 0 on success, otherwise return negative
  43820. */
  43821. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  43822. {
  43823. wc_test_ret_t ret = 0;
  43824. #if !defined(NO_RSA)
  43825. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43826. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  43827. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43828. byte* tmp = NULL;
  43829. #else
  43830. RsaKey key[1];
  43831. byte tmp[FOURK_BUF];
  43832. #endif
  43833. size_t bytes;
  43834. const word32 inLen = (word32)TEST_STRING_SZ;
  43835. word32 idx = 0;
  43836. word32 sigSz;
  43837. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  43838. byte out[RSA_TEST_BYTES];
  43839. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  43840. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  43841. !defined(NO_FILESYSTEM)
  43842. XFILE file;
  43843. #endif
  43844. #ifdef WOLFSSL_KEY_GEN
  43845. WC_RNG rng;
  43846. #endif
  43847. #ifdef USE_CERT_BUFFERS_1024
  43848. bytes = (size_t)sizeof_client_key_der_1024;
  43849. if (bytes < (size_t)sizeof_client_cert_der_1024)
  43850. bytes = (size_t)sizeof_client_cert_der_1024;
  43851. #elif defined(USE_CERT_BUFFERS_2048)
  43852. bytes = (size_t)sizeof_client_key_der_2048;
  43853. if (bytes < (size_t)sizeof_client_cert_der_2048)
  43854. bytes = (size_t)sizeof_client_cert_der_2048;
  43855. #elif defined(USE_CERT_BUFFERS_3072)
  43856. bytes = (size_t)sizeof_client_key_der_3072;
  43857. if (bytes < (size_t)sizeof_client_cert_der_3072)
  43858. bytes = (size_t)sizeof_client_cert_der_3072;
  43859. #elif defined(USE_CERT_BUFFERS_4096)
  43860. bytes = (size_t)sizeof_client_key_der_4096;
  43861. if (bytes < (size_t)sizeof_client_cert_der_4096)
  43862. bytes = (size_t)sizeof_client_cert_der_4096;
  43863. #else
  43864. bytes = FOURK_BUF;
  43865. #endif
  43866. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43867. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43868. if (tmp == NULL)
  43869. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  43870. #endif
  43871. #ifdef USE_CERT_BUFFERS_1024
  43872. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  43873. #elif defined(USE_CERT_BUFFERS_2048)
  43874. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  43875. #elif defined(USE_CERT_BUFFERS_3072)
  43876. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  43877. #elif defined(USE_CERT_BUFFERS_4096)
  43878. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  43879. #elif !defined(NO_FILESYSTEM)
  43880. file = XFOPEN(clientKey, "rb");
  43881. if (!file) {
  43882. ret = WC_TEST_RET_ENC_ERRNO;
  43883. err_sys("can't open ./certs/client-key.der, "
  43884. "Please run from wolfSSL home dir", ret);
  43885. ERROR_OUT(ret, exit_onlycb);
  43886. }
  43887. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  43888. XFCLOSE(file);
  43889. if (bytes == 0)
  43890. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  43891. #endif
  43892. #ifdef WOLFSSL_KEY_GEN
  43893. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  43894. * wc_MakeRsaKey() -> rsa cb ->
  43895. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  43896. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  43897. */
  43898. ctx->exampleVar = 99;
  43899. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  43900. if (ret != 0)
  43901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43902. /* wc_MakeRsaKey() -> rsa cb ->
  43903. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  43904. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  43905. */
  43906. ctx->exampleVar = 1;
  43907. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  43908. if (ret != NO_VALID_DEVID) {
  43909. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43910. } else
  43911. /* reset return code */
  43912. ret = 0;
  43913. #endif
  43914. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  43915. if (ret != 0)
  43916. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43917. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  43918. if (ret != 0)
  43919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43920. sigSz = (word32)wc_RsaEncryptSize(key);
  43921. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  43922. if (ret == 0) {
  43923. /* wc_SignatureGenerate() -> rsa cb ->
  43924. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  43925. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  43926. */
  43927. ctx->exampleVar = 99;
  43928. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  43929. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  43930. if (ret != 0)
  43931. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43932. }
  43933. if (ret == 0) {
  43934. /* wc_SignatureGenerate() -> rsa cb ->
  43935. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  43936. * wc_SignatureGenerate(INVALID_DEVID) expects to
  43937. * return NO_VALID_DEVID(failure)
  43938. */
  43939. ctx->exampleVar = 1;
  43940. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  43941. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  43942. if (ret != NO_VALID_DEVID) {
  43943. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  43944. } else
  43945. /* reset return code */
  43946. ret = 0;
  43947. }
  43948. exit_onlycb:
  43949. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43950. if (key != NULL) {
  43951. wc_FreeRsaKey(key);
  43952. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43953. }
  43954. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43955. #else
  43956. wc_FreeRsaKey(key);
  43957. #endif
  43958. #endif
  43959. return ret;
  43960. }
  43961. #endif
  43962. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  43963. /* Testing rsa cb when CB_ONLY_ECC is enabled
  43964. * When CB_ONLY_ECC is enabled, software imple. is not available.
  43965. *
  43966. * ctx callback ctx
  43967. * returen 0 on success, otherwise return negative
  43968. */
  43969. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  43970. {
  43971. wc_test_ret_t ret = 0;
  43972. #if defined(HAVE_ECC)
  43973. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43974. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  43975. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43976. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  43977. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43978. byte* out = (byte*)XMALLOC(sizeof(byte),
  43979. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43980. #ifdef OPENSSL_EXTRA
  43981. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  43982. DYNAMIC_TYPE_TMP_BUFFER);
  43983. #endif
  43984. #else
  43985. ecc_key key[1];
  43986. ecc_key pub[1];
  43987. byte out[256];
  43988. #ifdef OPENSSL_EXTRA
  43989. unsigned char check[256];
  43990. #endif
  43991. #endif
  43992. #ifdef OPENSSL_EXTRA
  43993. EVP_PKEY* privKey = NULL;
  43994. EVP_PKEY* pubKey = NULL;
  43995. #ifdef USE_CERT_BUFFERS_256
  43996. ecc_key* pkey;
  43997. const unsigned char* cp;
  43998. #endif
  43999. EVP_MD_CTX mdCtx;
  44000. const char testData[] = "Hi There";
  44001. size_t checkSz = -1;
  44002. const unsigned char* p;
  44003. const unsigned char check_v[256] = {
  44004. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  44005. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  44006. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  44007. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  44008. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  44009. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  44010. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  44011. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  44012. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  44013. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  44014. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  44015. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  44016. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  44017. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  44018. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  44019. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  44020. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  44021. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  44022. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  44023. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  44024. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  44025. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  44026. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  44027. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  44028. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  44029. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  44030. };
  44031. #endif
  44032. WC_RNG rng;
  44033. EncryptedInfo encInfo;
  44034. int keyFormat = 0;
  44035. word32 keyIdx = 0;
  44036. byte in[] = "Everyone gets Friday off. ecc p";
  44037. word32 inLen = (word32)XSTRLEN((char*)in);
  44038. word32 outLen;
  44039. int verify;
  44040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  44041. if (key == NULL || pub == NULL) {
  44042. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  44043. }
  44044. #endif
  44045. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  44046. if (ret != 0)
  44047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44048. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  44049. ctx->exampleVar = 99;
  44050. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  44051. if (ret != 0)
  44052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44053. ctx->exampleVar = 1;
  44054. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  44055. if (ret != NO_VALID_DEVID) {
  44056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44057. } else
  44058. /* reset return code */
  44059. ret = 0;
  44060. #ifdef USE_CERT_BUFFERS_256
  44061. if (ret == 0) {
  44062. /* load ECC private key and perform private transform */
  44063. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  44064. key, sizeof_ecc_key_der_256);
  44065. }
  44066. if (ret != 0)
  44067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44068. /* wc_CryptoCb_EccSign cb test, no actual testing */
  44069. ctx->exampleVar = 99;
  44070. if (ret == 0) {
  44071. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  44072. }
  44073. if (ret != 0)
  44074. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44075. ctx->exampleVar = 1;
  44076. if (ret == 0) {
  44077. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  44078. }
  44079. if (ret != NO_VALID_DEVID) {
  44080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44081. }
  44082. else
  44083. ret = 0;
  44084. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  44085. ctx->exampleVar = 99;
  44086. if (ret == 0) {
  44087. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  44088. }
  44089. if (ret != 0)
  44090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44091. ctx->exampleVar = 1;
  44092. if (ret == 0) {
  44093. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  44094. }
  44095. if (ret != NO_VALID_DEVID) {
  44096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44097. }
  44098. else
  44099. ret = 0;
  44100. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  44101. /* make public key for shared secret */
  44102. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  44103. ctx->exampleVar = 99;
  44104. if (ret == 0) {
  44105. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  44106. }
  44107. if (ret != 0)
  44108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44109. ctx->exampleVar = 1;
  44110. if (ret == 0) {
  44111. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  44112. }
  44113. if (ret != NO_VALID_DEVID) {
  44114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44115. }
  44116. else
  44117. ret = 0;
  44118. #ifdef OPENSSL_EXTRA
  44119. (void)pkey;
  44120. cp = ecc_clikey_der_256;
  44121. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  44122. sizeof_ecc_clikey_der_256);
  44123. if (privKey == NULL) {
  44124. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44125. }
  44126. pkey = (ecc_key*)privKey->ecc->internal;
  44127. pkey->devId = devId;
  44128. p = ecc_clikeypub_der_256;
  44129. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  44130. if (pubKey == NULL) {
  44131. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44132. }
  44133. pkey = (ecc_key*)pubKey->ecc->internal;
  44134. pkey->devId = devId;
  44135. /* sign */
  44136. EVP_MD_CTX_init(&mdCtx);
  44137. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  44138. if (ret != WOLFSSL_SUCCESS) {
  44139. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44140. }
  44141. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  44142. (unsigned int)XSTRLEN(testData));
  44143. if (ret != WOLFSSL_SUCCESS) {
  44144. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44145. }
  44146. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  44147. if (ret != WOLFSSL_SUCCESS) {
  44148. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44149. }
  44150. ctx->exampleVar = 99;
  44151. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  44152. /* just called crypt callback as dummy
  44153. * EVP_DigestSignFinal returns 0 internally.
  44154. */
  44155. if (ret != 0)
  44156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44157. ctx->exampleVar = 1;
  44158. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  44159. /* just called crypt callback as dummy
  44160. * EVP_DigestSignFinal returns 0 internally.
  44161. */
  44162. if (ret != 0)
  44163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44164. /* restore checkSz for verify */
  44165. checkSz = 71;
  44166. ret = EVP_MD_CTX_cleanup(&mdCtx);
  44167. if (ret != SSL_SUCCESS) {
  44168. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44169. }
  44170. /* verify */
  44171. EVP_MD_CTX_init(&mdCtx);
  44172. if (ret == SSL_SUCCESS) {
  44173. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  44174. }
  44175. if (ret != WOLFSSL_SUCCESS) {
  44176. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44177. }
  44178. if (ret == WOLFSSL_SUCCESS) {
  44179. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  44180. (unsigned int)XSTRLEN(testData));
  44181. }
  44182. if (ret != WOLFSSL_SUCCESS) {
  44183. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44184. }
  44185. ctx->exampleVar = 99;
  44186. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  44187. /* just called crypt callback as dummy
  44188. * EVP_DigestSignFinal returns 0 internally.
  44189. */
  44190. if (ret != 0)
  44191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  44192. ctx->exampleVar = 1;
  44193. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  44194. /* just called crypt callback as dummy
  44195. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  44196. */
  44197. if (ret != -1) {
  44198. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44199. }
  44200. ret = EVP_MD_CTX_cleanup(&mdCtx);
  44201. if (ret != SSL_SUCCESS) {
  44202. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  44203. } else
  44204. ret = 0;
  44205. #endif
  44206. #else
  44207. (void)verify;
  44208. (void)outLen;
  44209. (void)inLen;
  44210. (void)out;
  44211. (void)pub;
  44212. #ifdef OPENSSL_EXTRA
  44213. (void)privKey;
  44214. (void)pubKey;
  44215. (void)mdCtx;
  44216. (void)check;
  44217. (void)checkSz;
  44218. (void)p;
  44219. #endif
  44220. #endif
  44221. (void)keyFormat;
  44222. (void)encInfo;
  44223. exit_onlycb:
  44224. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  44225. if (key != NULL) {
  44226. wc_ecc_free(key);
  44227. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44228. }
  44229. if (pub != NULL) {
  44230. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44231. }
  44232. if (out != NULL) {
  44233. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44234. }
  44235. #ifdef OPENSSL_EXTRA
  44236. if (check) {
  44237. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44238. }
  44239. #endif
  44240. #else
  44241. wc_ecc_free(key);
  44242. #ifdef OPENSSL_EXTRA
  44243. if (privKey)
  44244. EVP_PKEY_free(privKey);
  44245. if (pubKey)
  44246. EVP_PKEY_free(pubKey);
  44247. #endif
  44248. #endif
  44249. #endif /* HAVE_ECC */
  44250. return ret;
  44251. }
  44252. #endif
  44253. /* Example crypto dev callback function that calls software version */
  44254. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  44255. {
  44256. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  44257. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  44258. if (info == NULL)
  44259. return BAD_FUNC_ARG;
  44260. #ifdef DEBUG_WOLFSSL
  44261. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  44262. #endif
  44263. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  44264. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  44265. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  44266. /* if RNG only supports crypto callback, just use seed */
  44267. ret = wc_GenerateSeed(&info->rng.rng->seed,
  44268. info->rng.out, info->rng.sz);
  44269. #elif !defined(WC_NO_RNG)
  44270. /* set devId to invalid, so software is used */
  44271. info->rng.rng->devId = INVALID_DEVID;
  44272. ret = wc_RNG_GenerateBlock(info->rng.rng,
  44273. info->rng.out, info->rng.sz);
  44274. /* reset devId */
  44275. info->rng.rng->devId = devIdArg;
  44276. #endif
  44277. }
  44278. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  44279. #ifndef WC_NO_RNG
  44280. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  44281. word32* seedWord32 = (word32*)seed;
  44282. word32 len;
  44283. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  44284. while (info->seed.sz > 0) {
  44285. len = (word32)sizeof(seed);
  44286. if (info->seed.sz < len)
  44287. len = info->seed.sz;
  44288. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  44289. info->seed.seed += len;
  44290. info->seed.sz -= len;
  44291. (*seedWord32)++;
  44292. }
  44293. ret = 0;
  44294. #endif
  44295. }
  44296. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  44297. #ifdef DEBUG_WOLFSSL
  44298. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  44299. #endif
  44300. #ifndef NO_RSA
  44301. if (info->pk.type == WC_PK_TYPE_RSA) {
  44302. /* set devId to invalid, so software is used */
  44303. info->pk.rsa.key->devId = INVALID_DEVID;
  44304. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  44305. #ifdef DEBUG_WOLFSSL
  44306. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44307. #endif
  44308. if (myCtx->exampleVar == 99) {
  44309. info->pk.rsa.key->devId = devIdArg;
  44310. return 0;
  44311. }
  44312. #endif
  44313. switch (info->pk.rsa.type) {
  44314. case RSA_PUBLIC_ENCRYPT:
  44315. case RSA_PUBLIC_DECRYPT:
  44316. /* perform software based RSA public op */
  44317. ret = wc_RsaFunction(
  44318. info->pk.rsa.in, info->pk.rsa.inLen,
  44319. info->pk.rsa.out, info->pk.rsa.outLen,
  44320. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  44321. break;
  44322. case RSA_PRIVATE_ENCRYPT:
  44323. case RSA_PRIVATE_DECRYPT:
  44324. /* perform software based RSA private op */
  44325. ret = wc_RsaFunction(
  44326. info->pk.rsa.in, info->pk.rsa.inLen,
  44327. info->pk.rsa.out, info->pk.rsa.outLen,
  44328. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  44329. break;
  44330. }
  44331. /* reset devId */
  44332. info->pk.rsa.key->devId = devIdArg;
  44333. }
  44334. #ifdef WOLFSSL_KEY_GEN
  44335. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  44336. info->pk.rsakg.key->devId = INVALID_DEVID;
  44337. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  44338. #ifdef DEBUG_WOLFSSL
  44339. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44340. #endif
  44341. if (myCtx->exampleVar == 99) {
  44342. info->pk.rsakg.key->devId = devIdArg;
  44343. return 0;
  44344. }
  44345. #endif
  44346. #ifdef HAVE_FIPS
  44347. for (;;) {
  44348. #endif
  44349. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  44350. info->pk.rsakg.e, info->pk.rsakg.rng);
  44351. #ifdef HAVE_FIPS
  44352. if (ret == PRIME_GEN_E)
  44353. continue;
  44354. break;
  44355. }
  44356. #endif
  44357. /* reset devId */
  44358. info->pk.rsakg.key->devId = devIdArg;
  44359. }
  44360. #endif
  44361. #endif /* !NO_RSA */
  44362. #ifdef HAVE_ECC
  44363. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  44364. /* set devId to invalid, so software is used */
  44365. info->pk.eckg.key->devId = INVALID_DEVID;
  44366. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44367. #ifdef DEBUG_WOLFSSL
  44368. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44369. #endif
  44370. if (myCtx->exampleVar == 99) {
  44371. info->pk.eckg.key->devId = devIdArg;
  44372. return 0;
  44373. }
  44374. #endif
  44375. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  44376. info->pk.eckg.key, info->pk.eckg.curveId);
  44377. /* reset devId */
  44378. info->pk.eckg.key->devId = devIdArg;
  44379. }
  44380. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  44381. /* set devId to invalid, so software is used */
  44382. info->pk.eccsign.key->devId = INVALID_DEVID;
  44383. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44384. #ifdef DEBUG_WOLFSSL
  44385. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44386. #endif
  44387. if (myCtx->exampleVar == 99) {
  44388. info->pk.eccsign.key->devId = devIdArg;
  44389. return 0;
  44390. }
  44391. #endif
  44392. ret = wc_ecc_sign_hash(
  44393. info->pk.eccsign.in, info->pk.eccsign.inlen,
  44394. info->pk.eccsign.out, info->pk.eccsign.outlen,
  44395. info->pk.eccsign.rng, info->pk.eccsign.key);
  44396. /* reset devId */
  44397. info->pk.eccsign.key->devId = devIdArg;
  44398. }
  44399. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  44400. /* set devId to invalid, so software is used */
  44401. info->pk.eccverify.key->devId = INVALID_DEVID;
  44402. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44403. #ifdef DEBUG_WOLFSSL
  44404. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44405. #endif
  44406. if (myCtx->exampleVar == 99) {
  44407. info->pk.eccverify.key->devId = devIdArg;
  44408. return 0;
  44409. }
  44410. #endif
  44411. ret = wc_ecc_verify_hash(
  44412. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  44413. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  44414. info->pk.eccverify.res, info->pk.eccverify.key);
  44415. /* reset devId */
  44416. info->pk.eccverify.key->devId = devIdArg;
  44417. }
  44418. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  44419. /* set devId to invalid, so software is used */
  44420. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  44421. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44422. #ifdef DEBUG_WOLFSSL
  44423. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  44424. #endif
  44425. if (myCtx->exampleVar == 99) {
  44426. info->pk.ecdh.private_key->devId = devIdArg;
  44427. return 0;
  44428. }
  44429. #endif
  44430. ret = wc_ecc_shared_secret(
  44431. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  44432. info->pk.ecdh.out, info->pk.ecdh.outlen);
  44433. /* reset devId */
  44434. info->pk.ecdh.private_key->devId = devIdArg;
  44435. }
  44436. #endif /* HAVE_ECC */
  44437. #ifdef HAVE_CURVE25519
  44438. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  44439. /* set devId to invalid, so software is used */
  44440. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  44441. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  44442. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  44443. /* reset devId */
  44444. info->pk.curve25519kg.key->devId = devIdArg;
  44445. }
  44446. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  44447. /* set devId to invalid, so software is used */
  44448. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  44449. ret = wc_curve25519_shared_secret_ex(
  44450. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  44451. info->pk.curve25519.out, info->pk.curve25519.outlen,
  44452. info->pk.curve25519.endian);
  44453. /* reset devId */
  44454. info->pk.curve25519.private_key->devId = devIdArg;
  44455. }
  44456. #endif /* HAVE_CURVE25519 */
  44457. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  44458. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  44459. /* set devId to invalid, so software is used */
  44460. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  44461. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  44462. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  44463. /* reset devId */
  44464. info->pk.ed25519kg.key->devId = devIdArg;
  44465. }
  44466. #ifdef HAVE_ED25519_SIGN
  44467. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  44468. /* set devId to invalid, so software is used */
  44469. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  44470. ret = wc_ed25519_sign_msg_ex(
  44471. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  44472. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  44473. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  44474. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  44475. /* reset devId */
  44476. info->pk.ed25519sign.key->devId = devIdArg;
  44477. }
  44478. #endif
  44479. #ifdef HAVE_ED25519_VERIFY
  44480. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  44481. /* set devId to invalid, so software is used */
  44482. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  44483. ret = wc_ed25519_verify_msg_ex(
  44484. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  44485. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  44486. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  44487. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  44488. info->pk.ed25519verify.contextLen);
  44489. /* reset devId */
  44490. info->pk.ed25519verify.key->devId = devIdArg;
  44491. }
  44492. #endif
  44493. #endif /* HAVE_ED25519 */
  44494. }
  44495. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  44496. #if !defined(NO_AES) || !defined(NO_DES3)
  44497. #ifdef HAVE_AESGCM
  44498. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  44499. if (info->cipher.enc) {
  44500. /* set devId to invalid, so software is used */
  44501. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  44502. ret = wc_AesGcmEncrypt(
  44503. info->cipher.aesgcm_enc.aes,
  44504. info->cipher.aesgcm_enc.out,
  44505. info->cipher.aesgcm_enc.in,
  44506. info->cipher.aesgcm_enc.sz,
  44507. info->cipher.aesgcm_enc.iv,
  44508. info->cipher.aesgcm_enc.ivSz,
  44509. info->cipher.aesgcm_enc.authTag,
  44510. info->cipher.aesgcm_enc.authTagSz,
  44511. info->cipher.aesgcm_enc.authIn,
  44512. info->cipher.aesgcm_enc.authInSz);
  44513. /* reset devId */
  44514. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  44515. }
  44516. else {
  44517. /* set devId to invalid, so software is used */
  44518. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  44519. ret = wc_AesGcmDecrypt(
  44520. info->cipher.aesgcm_dec.aes,
  44521. info->cipher.aesgcm_dec.out,
  44522. info->cipher.aesgcm_dec.in,
  44523. info->cipher.aesgcm_dec.sz,
  44524. info->cipher.aesgcm_dec.iv,
  44525. info->cipher.aesgcm_dec.ivSz,
  44526. info->cipher.aesgcm_dec.authTag,
  44527. info->cipher.aesgcm_dec.authTagSz,
  44528. info->cipher.aesgcm_dec.authIn,
  44529. info->cipher.aesgcm_dec.authInSz);
  44530. /* reset devId */
  44531. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  44532. }
  44533. }
  44534. #endif /* HAVE_AESGCM */
  44535. #ifdef HAVE_AES_CBC
  44536. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  44537. if (info->cipher.enc) {
  44538. /* set devId to invalid, so software is used */
  44539. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  44540. ret = wc_AesCbcEncrypt(
  44541. info->cipher.aescbc.aes,
  44542. info->cipher.aescbc.out,
  44543. info->cipher.aescbc.in,
  44544. info->cipher.aescbc.sz);
  44545. /* reset devId */
  44546. info->cipher.aescbc.aes->devId = devIdArg;
  44547. }
  44548. else {
  44549. /* set devId to invalid, so software is used */
  44550. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  44551. ret = wc_AesCbcDecrypt(
  44552. info->cipher.aescbc.aes,
  44553. info->cipher.aescbc.out,
  44554. info->cipher.aescbc.in,
  44555. info->cipher.aescbc.sz);
  44556. /* reset devId */
  44557. info->cipher.aescbc.aes->devId = devIdArg;
  44558. }
  44559. }
  44560. #endif /* HAVE_AES_CBC */
  44561. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  44562. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  44563. if (info->cipher.enc) {
  44564. /* set devId to invalid, so software is used */
  44565. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  44566. ret = wc_AesEcbEncrypt(
  44567. info->cipher.aesecb.aes,
  44568. info->cipher.aesecb.out,
  44569. info->cipher.aesecb.in,
  44570. info->cipher.aesecb.sz);
  44571. /* reset devId */
  44572. info->cipher.aesecb.aes->devId = devIdArg;
  44573. }
  44574. else {
  44575. /* set devId to invalid, so software is used */
  44576. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  44577. ret = wc_AesEcbDecrypt(
  44578. info->cipher.aesecb.aes,
  44579. info->cipher.aesecb.out,
  44580. info->cipher.aesecb.in,
  44581. info->cipher.aesecb.sz);
  44582. /* reset devId */
  44583. info->cipher.aesecb.aes->devId = devIdArg;
  44584. }
  44585. }
  44586. #endif /* HAVE_AES_ECB */
  44587. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  44588. !defined(HAVE_SELFTEST)
  44589. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  44590. /* set devId to invalid, so software is used */
  44591. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  44592. ret = wc_AesCtrEncrypt(
  44593. info->cipher.aesctr.aes,
  44594. info->cipher.aesctr.out,
  44595. info->cipher.aesctr.in,
  44596. info->cipher.aesctr.sz);
  44597. /* reset devId */
  44598. info->cipher.aesctr.aes->devId = devIdArg;
  44599. }
  44600. #endif /* WOLFSSL_AES_COUNTER */
  44601. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  44602. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  44603. if (info->cipher.enc) {
  44604. /* set devId to invalid, so software is used */
  44605. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  44606. ret = wc_AesCcmEncrypt(
  44607. info->cipher.aesccm_enc.aes,
  44608. info->cipher.aesccm_enc.out,
  44609. info->cipher.aesccm_enc.in,
  44610. info->cipher.aesccm_enc.sz,
  44611. info->cipher.aesccm_enc.nonce,
  44612. info->cipher.aesccm_enc.nonceSz,
  44613. info->cipher.aesccm_enc.authTag,
  44614. info->cipher.aesccm_enc.authTagSz,
  44615. info->cipher.aesccm_enc.authIn,
  44616. info->cipher.aesccm_enc.authInSz);
  44617. /* reset devId */
  44618. info->cipher.aesccm_enc.aes->devId = devIdArg;
  44619. }
  44620. else {
  44621. /* set devId to invalid, so software is used */
  44622. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  44623. ret = wc_AesCcmDecrypt(
  44624. info->cipher.aesccm_dec.aes,
  44625. info->cipher.aesccm_dec.out,
  44626. info->cipher.aesccm_dec.in,
  44627. info->cipher.aesccm_dec.sz,
  44628. info->cipher.aesccm_dec.nonce,
  44629. info->cipher.aesccm_dec.nonceSz,
  44630. info->cipher.aesccm_dec.authTag,
  44631. info->cipher.aesccm_dec.authTagSz,
  44632. info->cipher.aesccm_dec.authIn,
  44633. info->cipher.aesccm_dec.authInSz);
  44634. /* reset devId */
  44635. info->cipher.aesccm_dec.aes->devId = devIdArg;
  44636. }
  44637. }
  44638. #endif
  44639. #ifndef NO_DES3
  44640. if (info->cipher.type == WC_CIPHER_DES3) {
  44641. if (info->cipher.enc) {
  44642. /* set devId to invalid, so software is used */
  44643. info->cipher.des3.des->devId = INVALID_DEVID;
  44644. ret = wc_Des3_CbcEncrypt(
  44645. info->cipher.des3.des,
  44646. info->cipher.des3.out,
  44647. info->cipher.des3.in,
  44648. info->cipher.des3.sz);
  44649. /* reset devId */
  44650. info->cipher.des3.des->devId = devIdArg;
  44651. }
  44652. else {
  44653. /* set devId to invalid, so software is used */
  44654. info->cipher.des3.des->devId = INVALID_DEVID;
  44655. ret = wc_Des3_CbcDecrypt(
  44656. info->cipher.des3.des,
  44657. info->cipher.des3.out,
  44658. info->cipher.des3.in,
  44659. info->cipher.des3.sz);
  44660. /* reset devId */
  44661. info->cipher.des3.des->devId = devIdArg;
  44662. }
  44663. }
  44664. #endif /* !NO_DES3 */
  44665. #endif /* !NO_AES || !NO_DES3 */
  44666. }
  44667. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  44668. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  44669. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  44670. #if !defined(NO_SHA)
  44671. if (info->hash.type == WC_HASH_TYPE_SHA) {
  44672. if (info->hash.sha1 == NULL)
  44673. return NOT_COMPILED_IN;
  44674. /* set devId to invalid, so software is used */
  44675. info->hash.sha1->devId = INVALID_DEVID;
  44676. if (info->hash.in != NULL) {
  44677. ret = wc_ShaUpdate(
  44678. info->hash.sha1,
  44679. info->hash.in,
  44680. info->hash.inSz);
  44681. }
  44682. if (info->hash.digest != NULL) {
  44683. ret = wc_ShaFinal(
  44684. info->hash.sha1,
  44685. info->hash.digest);
  44686. }
  44687. /* reset devId */
  44688. info->hash.sha1->devId = devIdArg;
  44689. }
  44690. else
  44691. #endif
  44692. #if !defined(NO_SHA256)
  44693. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  44694. if (info->hash.sha256 == NULL)
  44695. return NOT_COMPILED_IN;
  44696. /* set devId to invalid, so software is used */
  44697. info->hash.sha256->devId = INVALID_DEVID;
  44698. if (info->hash.in != NULL) {
  44699. ret = wc_Sha256Update(
  44700. info->hash.sha256,
  44701. info->hash.in,
  44702. info->hash.inSz);
  44703. }
  44704. if (info->hash.digest != NULL) {
  44705. ret = wc_Sha256Final(
  44706. info->hash.sha256,
  44707. info->hash.digest);
  44708. }
  44709. /* reset devId */
  44710. info->hash.sha256->devId = devIdArg;
  44711. }
  44712. else
  44713. #endif
  44714. #ifdef WOLFSSL_SHA384
  44715. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  44716. if (info->hash.sha384 == NULL)
  44717. return NOT_COMPILED_IN;
  44718. #ifndef NO_SHA2_CRYPTO_CB
  44719. /* set devId to invalid, so software is used */
  44720. info->hash.sha384->devId = INVALID_DEVID;
  44721. #endif
  44722. if (info->hash.in != NULL) {
  44723. ret = wc_Sha384Update(
  44724. info->hash.sha384,
  44725. info->hash.in,
  44726. info->hash.inSz);
  44727. }
  44728. if (info->hash.digest != NULL) {
  44729. ret = wc_Sha384Final(
  44730. info->hash.sha384,
  44731. info->hash.digest);
  44732. }
  44733. #ifndef NO_SHA2_CRYPTO_CB
  44734. /* reset devId */
  44735. info->hash.sha384->devId = devIdArg;
  44736. #endif
  44737. }
  44738. else
  44739. #endif
  44740. #ifdef WOLFSSL_SHA512
  44741. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  44742. if (info->hash.sha512 == NULL)
  44743. return NOT_COMPILED_IN;
  44744. #ifndef NO_SHA2_CRYPTO_CB
  44745. /* set devId to invalid, so software is used */
  44746. info->hash.sha512->devId = INVALID_DEVID;
  44747. #endif
  44748. if (info->hash.in != NULL) {
  44749. ret = wc_Sha512Update(
  44750. info->hash.sha512,
  44751. info->hash.in,
  44752. info->hash.inSz);
  44753. }
  44754. if (info->hash.digest != NULL) {
  44755. ret = wc_Sha512Final(
  44756. info->hash.sha512,
  44757. info->hash.digest);
  44758. }
  44759. #ifndef NO_SHA2_CRYPTO_CB
  44760. /* reset devId */
  44761. info->hash.sha512->devId = devIdArg;
  44762. #endif
  44763. }
  44764. else
  44765. #endif
  44766. {
  44767. }
  44768. }
  44769. #endif /* !NO_SHA || !NO_SHA256 */
  44770. #ifndef NO_HMAC
  44771. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  44772. if (info->hmac.hmac == NULL)
  44773. return NOT_COMPILED_IN;
  44774. /* set devId to invalid, so software is used */
  44775. info->hmac.hmac->devId = INVALID_DEVID;
  44776. if (info->hmac.in != NULL) {
  44777. ret = wc_HmacUpdate(
  44778. info->hmac.hmac,
  44779. info->hmac.in,
  44780. info->hmac.inSz);
  44781. }
  44782. else if (info->hmac.digest != NULL) {
  44783. ret = wc_HmacFinal(
  44784. info->hmac.hmac,
  44785. info->hmac.digest);
  44786. }
  44787. /* reset devId */
  44788. info->hmac.hmac->devId = devIdArg;
  44789. }
  44790. #endif
  44791. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  44792. else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
  44793. if (info->cmac.cmac == NULL) {
  44794. return NOT_COMPILED_IN;
  44795. }
  44796. /* set devId to invalid so software is used */
  44797. info->cmac.cmac->devId = INVALID_DEVID;
  44798. /* Handle one-shot cases */
  44799. if (info->cmac.key != NULL && info->cmac.in != NULL
  44800. && info->cmac.out != NULL) {
  44801. ret = wc_AesCmacGenerate(info->cmac.out,
  44802. info->cmac.outSz,
  44803. info->cmac.in,
  44804. info->cmac.inSz,
  44805. info->cmac.key,
  44806. info->cmac.keySz);
  44807. /* Sequentially handle incremental cases */
  44808. } else {
  44809. if (info->cmac.key != NULL) {
  44810. ret = wc_InitCmac(info->cmac.cmac,
  44811. info->cmac.key,
  44812. info->cmac.keySz,
  44813. info->cmac.type,
  44814. NULL);
  44815. }
  44816. if ((ret == 0) && (info->cmac.in != NULL)) {
  44817. ret = wc_CmacUpdate(info->cmac.cmac,
  44818. info->cmac.in,
  44819. info->cmac.inSz);
  44820. }
  44821. if ((ret ==0) && (info->cmac.out != NULL)) {
  44822. ret = wc_CmacFinal(info->cmac.cmac,
  44823. info->cmac.out,
  44824. info->cmac.outSz);
  44825. }
  44826. }
  44827. /* reset devId */
  44828. info->cmac.cmac->devId = devIdArg;
  44829. }
  44830. #endif /* WOLFSSL_CMAC && !(NO_AES) && WOLFSSL_AES_DIRECT */
  44831. (void)devIdArg;
  44832. (void)myCtx;
  44833. return ret;
  44834. }
  44835. #ifdef WOLF_CRYPTO_CB_FIND
  44836. static int myCryptoCbFind(int currentId, int algoType)
  44837. {
  44838. /* can have algo specific overrides here
  44839. switch (algoType) {
  44840. i.e.
  44841. WC_ALGO_TYPE_CMAC
  44842. WC_ALGO_TYPE_SEED
  44843. WC_ALGO_TYPE_HMAC
  44844. WC_ALGO_TYPE_HASH
  44845. WC_ALGO_TYPE_CIPHER
  44846. WC_ALGO_TYPE_PK
  44847. }
  44848. */
  44849. (void)algoType;
  44850. if (currentId == INVALID_DEVID) {
  44851. /* can override invalid devid found with 1 */
  44852. }
  44853. return currentId;
  44854. }
  44855. #endif /* WOLF_CRYPTO_CB_FIND */
  44856. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  44857. {
  44858. wc_test_ret_t ret = 0;
  44859. int origDevId = devId;
  44860. myCryptoDevCtx myCtx;
  44861. WOLFSSL_ENTER("cryptocb_test");
  44862. /* example data for callback */
  44863. myCtx.exampleVar = 1;
  44864. /* set devId to something other than INVALID_DEVID */
  44865. devId = 1;
  44866. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  44867. #ifdef WOLF_CRYPTO_CB_FIND
  44868. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  44869. #endif /* WOLF_CRYPTO_CB_FIND */
  44870. #ifndef WC_NO_RNG
  44871. if (ret == 0)
  44872. ret = random_test();
  44873. #endif /* WC_NO_RNG */
  44874. #if !defined(NO_RSA)
  44875. PRIVATE_KEY_UNLOCK();
  44876. if (ret == 0)
  44877. ret = rsa_test();
  44878. PRIVATE_KEY_LOCK();
  44879. #endif
  44880. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  44881. PRIVATE_KEY_UNLOCK();
  44882. if (ret == 0)
  44883. ret = rsa_onlycb_test(&myCtx);
  44884. PRIVATE_KEY_LOCK();
  44885. #endif
  44886. #if defined(HAVE_ECC)
  44887. PRIVATE_KEY_UNLOCK();
  44888. if (ret == 0)
  44889. ret = ecc_test();
  44890. PRIVATE_KEY_LOCK();
  44891. #endif
  44892. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  44893. PRIVATE_KEY_UNLOCK();
  44894. if (ret == 0)
  44895. ret = ecc_onlycb_test(&myCtx);
  44896. PRIVATE_KEY_LOCK();
  44897. #endif
  44898. #ifdef HAVE_ED25519
  44899. if (ret == 0)
  44900. ret = ed25519_test();
  44901. #endif
  44902. #ifdef HAVE_CURVE25519
  44903. if (ret == 0)
  44904. ret = curve25519_test();
  44905. #endif
  44906. #ifndef NO_AES
  44907. #ifdef HAVE_AESGCM
  44908. if (ret == 0)
  44909. ret = aesgcm_test();
  44910. #endif
  44911. #ifdef HAVE_AES_CBC
  44912. if (ret == 0)
  44913. ret = aes_test();
  44914. #endif
  44915. #ifdef WOLFSSL_AES_XTS
  44916. if (ret == 0)
  44917. ret = aes_xts_test();
  44918. #endif
  44919. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  44920. if (ret == 0)
  44921. ret = aesccm_test();
  44922. #endif
  44923. #endif /* !NO_AES */
  44924. #ifndef NO_DES3
  44925. if (ret == 0)
  44926. ret = des3_test();
  44927. #endif /* !NO_DES3 */
  44928. #ifndef NO_SHA
  44929. if (ret == 0)
  44930. ret = sha_test();
  44931. #endif
  44932. #ifndef NO_SHA256
  44933. if (ret == 0)
  44934. ret = sha256_test();
  44935. #endif
  44936. #ifdef WOLFSSL_SHA384
  44937. if (ret == 0)
  44938. ret = sha384_test();
  44939. #endif
  44940. #ifdef WOLFSSL_SHA512
  44941. if (ret == 0)
  44942. ret = sha512_test();
  44943. #endif
  44944. #ifndef NO_HMAC
  44945. #ifndef NO_SHA
  44946. if (ret == 0)
  44947. ret = hmac_sha_test();
  44948. #endif
  44949. #ifndef NO_SHA256
  44950. if (ret == 0)
  44951. ret = hmac_sha256_test();
  44952. #endif
  44953. #endif
  44954. #ifndef NO_PWDBASED
  44955. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  44956. if (ret == 0)
  44957. ret = pbkdf2_test();
  44958. #endif
  44959. #endif
  44960. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  44961. if (ret == 0)
  44962. ret = cmac_test();
  44963. #endif
  44964. /* restore devId */
  44965. devId = origDevId;
  44966. return ret;
  44967. }
  44968. #endif /* WOLF_CRYPTO_CB */
  44969. #ifdef WOLFSSL_CERT_PIV
  44970. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  44971. {
  44972. wc_test_ret_t ret;
  44973. wc_CertPIV piv;
  44974. /* Template for Identiv PIV cert, nonce and signature */
  44975. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  44976. 0x0A, 0x0B,
  44977. 0x53, 0x09, /* NIST PIV Cert */
  44978. 0x70, 0x02, /* Certificate */
  44979. 0x30, 0x00,
  44980. 0x71, 0x01, 0x05, /* Cert Info */
  44981. 0xFE, 0x00, /* Error Detection */
  44982. 0x0B, 0x01, 0x00, /* Nonce */
  44983. 0x0C, 0x01, 0x00, /* Signed Nonce */
  44984. };
  44985. /* PIV certificate data including certificate, info and error detection. */
  44986. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  44987. 0x53, 0x09, /* NIST PIV Cert */
  44988. 0x70, 0x02, /* Certificate */
  44989. 0x30, 0x00,
  44990. 0x71, 0x01, 0x04, /* Cert Info */
  44991. 0xFE, 0x00, /* Error Detection */
  44992. };
  44993. WOLFSSL_ENTER("certpiv_test");
  44994. XMEMSET(&piv, 0, sizeof(piv));
  44995. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  44996. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  44997. if (ret != 0) {
  44998. return WC_TEST_RET_ENC_EC(ret);
  44999. }
  45000. if (!piv.isIdentiv) {
  45001. return WC_TEST_RET_ENC_NC;
  45002. }
  45003. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  45004. return WC_TEST_RET_ENC_NC;
  45005. }
  45006. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  45007. return WC_TEST_RET_ENC_NC;
  45008. }
  45009. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  45010. return WC_TEST_RET_ENC_NC;
  45011. }
  45012. if (!piv.isX509) {
  45013. return WC_TEST_RET_ENC_NC;
  45014. }
  45015. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  45016. return WC_TEST_RET_ENC_NC;
  45017. }
  45018. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  45019. return WC_TEST_RET_ENC_NC;
  45020. }
  45021. XMEMSET(&piv, 0, sizeof(piv));
  45022. /* Test with NIST PIV format */
  45023. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  45024. if (ret != 0) {
  45025. return WC_TEST_RET_ENC_EC(ret);
  45026. }
  45027. if (piv.isIdentiv) {
  45028. return WC_TEST_RET_ENC_NC;
  45029. }
  45030. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  45031. return WC_TEST_RET_ENC_NC;
  45032. }
  45033. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  45034. return WC_TEST_RET_ENC_NC;
  45035. }
  45036. if ((piv.compression != 0)) {
  45037. return WC_TEST_RET_ENC_NC;
  45038. }
  45039. if (!piv.isX509) {
  45040. return WC_TEST_RET_ENC_NC;
  45041. }
  45042. return ret;
  45043. }
  45044. #endif /* WOLFSSL_CERT_PIV */
  45045. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  45046. static time_t time_cb(time_t* t)
  45047. {
  45048. if (t != NULL) {
  45049. *t = 99;
  45050. }
  45051. return 99;
  45052. }
  45053. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  45054. {
  45055. time_t t;
  45056. wc_test_ret_t ret;
  45057. WOLFSSL_ENTER("time_test");
  45058. ret = wc_SetTimeCb(time_cb);
  45059. if (ret != 0)
  45060. return WC_TEST_RET_ENC_EC(ret);
  45061. t = wc_Time(NULL);
  45062. if (t != 99)
  45063. return WC_TEST_RET_ENC_NC;
  45064. ret = wc_GetTime(&t, sizeof(time_t));
  45065. if (ret != 0)
  45066. return WC_TEST_RET_ENC_EC(ret);
  45067. if (t != 99)
  45068. return WC_TEST_RET_ENC_NC;
  45069. ret = wc_SetTimeCb(NULL);
  45070. if (ret != 0)
  45071. return WC_TEST_RET_ENC_EC(ret);
  45072. return 0;
  45073. }
  45074. #endif
  45075. #ifdef WOLFSSL_AES_SIV
  45076. typedef struct {
  45077. const byte key[33];
  45078. word32 keySz;
  45079. const byte nonce[49];
  45080. word32 nonceSz;
  45081. const byte assoc[81];
  45082. word32 assocSz;
  45083. const byte plaintext[83];
  45084. word32 plaintextSz;
  45085. const byte siv[AES_BLOCK_SIZE+1];
  45086. const byte ciphertext[82];
  45087. word32 ciphertextSz;
  45088. } AesSivTestVector;
  45089. #define AES_SIV_TEST_VECTORS 7
  45090. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  45091. {
  45092. /* These test vectors come from chrony 4.1's SIV unit tests. */
  45093. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  45094. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45095. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45096. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  45097. "", 0,
  45098. "", 0,
  45099. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  45100. "", 0
  45101. },
  45102. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45103. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45104. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  45105. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  45106. "", 0,
  45107. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  45108. "", 0
  45109. },
  45110. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45111. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45112. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  45113. "", 0,
  45114. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  45115. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  45116. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  45117. },
  45118. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45119. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45120. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  45121. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  45122. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  45123. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  45124. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  45125. },
  45126. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45127. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45128. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  45129. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  45130. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  45131. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  45132. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  45133. },
  45134. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45135. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45136. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  45137. "\xd5", 17,
  45138. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  45139. "\xa0", 17,
  45140. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  45141. "\x08", 17,
  45142. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  45143. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  45144. "\xbd", 17
  45145. },
  45146. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  45147. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  45148. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  45149. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  45150. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  45151. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  45152. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  45153. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  45154. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  45155. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  45156. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  45157. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  45158. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  45159. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  45160. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  45161. "\x31", 81,
  45162. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  45163. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  45164. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  45165. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  45166. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  45167. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  45168. "\xa1", 81
  45169. }};
  45170. int i;
  45171. byte computedCiphertext[82];
  45172. byte computedPlaintext[82];
  45173. byte siv[AES_BLOCK_SIZE];
  45174. wc_test_ret_t ret = 0;
  45175. WOLFSSL_ENTER("aes_siv_test");
  45176. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  45177. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  45178. testVectors[i].assoc, testVectors[i].assocSz,
  45179. testVectors[i].nonce, testVectors[i].nonceSz,
  45180. testVectors[i].plaintext,
  45181. testVectors[i].plaintextSz, siv,
  45182. computedCiphertext);
  45183. if (ret != 0) {
  45184. return WC_TEST_RET_ENC_EC(ret);
  45185. }
  45186. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  45187. if (ret != 0) {
  45188. return WC_TEST_RET_ENC_NC;
  45189. }
  45190. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  45191. testVectors[i].ciphertextSz);
  45192. if (ret != 0) {
  45193. return WC_TEST_RET_ENC_NC;
  45194. }
  45195. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  45196. testVectors[i].assoc, testVectors[i].assocSz,
  45197. testVectors[i].nonce, testVectors[i].nonceSz,
  45198. computedCiphertext, testVectors[i].plaintextSz,
  45199. siv, computedPlaintext);
  45200. if (ret != 0) {
  45201. return WC_TEST_RET_ENC_EC(ret);
  45202. }
  45203. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  45204. testVectors[i].plaintextSz);
  45205. if (ret != 0) {
  45206. return WC_TEST_RET_ENC_NC;
  45207. }
  45208. }
  45209. return 0;
  45210. }
  45211. #endif
  45212. #undef ERROR_OUT
  45213. static const int fiducial4 = WC_TEST_RET_LN;
  45214. /* print the fiducial line numbers assigned above, allowing confirmation of
  45215. * source code version match when in doubt.
  45216. */
  45217. static void print_fiducials(void) {
  45218. printf(" [fiducial line numbers: %d %d %d %d]\n",
  45219. fiducial1, fiducial2, fiducial3, fiducial4);
  45220. }
  45221. #else
  45222. #ifndef NO_MAIN_DRIVER
  45223. int main(void) { return 0; }
  45224. #endif
  45225. #endif /* NO_CRYPT_TEST */