test.c 1.6 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifndef NO_CRYPT_TEST
  29. #include <wolfssl/version.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfssl/wolfcrypt/wc_port.h>
  32. #include <wolfssl/wolfcrypt/mem_track.h>
  33. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  34. #include <wolfssl/ssl.h>
  35. #define err_sys err_sys_remap /* remap err_sys */
  36. #include <wolfssl/test.h>
  37. #undef err_sys
  38. #endif
  39. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  40. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  41. #include <stdint.h>
  42. #endif
  43. #ifdef HAVE_STACK_SIZE_VERBOSE
  44. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  45. static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  46. #else
  47. static ssize_t max_relative_stack = -1;
  48. #endif
  49. #endif
  50. const byte const_byte_array[] = "A+Gd\0\0\0";
  51. #define CBPTR_EXPECTED 'A'
  52. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  53. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  54. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  55. #else
  56. static ssize_t max_relative_heap_allocs = -1;
  57. #endif
  58. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  59. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  60. #else
  61. static ssize_t max_relative_heap_bytes = -1;
  62. #endif
  63. #define PRINT_HEAP_CHECKPOINT() { \
  64. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  65. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  66. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  67. (long int)_rha, \
  68. _rha == 1 ? "" : "s", \
  69. (long int)_rhb); \
  70. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  71. return err_sys("heap allocs exceed designated max.", \
  72. WC_TEST_RET_ENC_NC); \
  73. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  74. return err_sys("heap bytes exceed designated max.", \
  75. WC_TEST_RET_ENC_NC); \
  76. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  77. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  78. }
  79. #else
  80. #define PRINT_HEAP_CHECKPOINT()
  81. #endif /* WOLFSSL_TRACK_MEMORY_VERBOSE && !WOLFSSL_STATIC_MEMORY */
  82. #ifdef USE_FLAT_TEST_H
  83. #ifdef HAVE_CONFIG_H
  84. #include "test_paths.h"
  85. #endif
  86. #include "test.h"
  87. #else
  88. #ifdef HAVE_CONFIG_H
  89. #include "wolfcrypt/test/test_paths.h"
  90. #endif
  91. #include "wolfcrypt/test/test.h"
  92. #endif
  93. /* printf mappings */
  94. #ifndef WOLFSSL_LOG_PRINTF
  95. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  96. #include <mqx.h>
  97. #include <stdlib.h>
  98. /* see wc_port.h for fio.h and nio.h includes */
  99. #elif defined(FREESCALE_KSDK_BM)
  100. #include "fsl_debug_console.h"
  101. #undef printf
  102. #define printf PRINTF
  103. #elif defined(WOLFSSL_APACHE_MYNEWT)
  104. #include <assert.h>
  105. #include <string.h>
  106. #include "sysinit/sysinit.h"
  107. #include "os/os.h"
  108. #ifdef ARCH_sim
  109. #include "mcu/mcu_sim.h"
  110. #endif
  111. #include "os/os_time.h"
  112. #elif defined(WOLFSSL_ESPIDF)
  113. #include <time.h>
  114. #include <sys/time.h>
  115. #include <esp_log.h>
  116. #elif defined(WOLFSSL_ZEPHYR)
  117. #include <stdio.h>
  118. #define printf printk
  119. #elif defined(MICRIUM)
  120. #include <os.h>
  121. #if (OS_VERSION < 50000)
  122. #include <bsp_ser.h>
  123. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  124. #undef printf
  125. #define printf BSP_Ser_Printf
  126. #else
  127. #include <stdio.h>
  128. #endif
  129. #elif defined(WOLFSSL_PB)
  130. #include <stdarg.h>
  131. int wolfssl_pb_print(const char*, ...);
  132. #undef printf
  133. #define printf wolfssl_pb_print
  134. #elif defined(WOLFSSL_TELIT_M2MB)
  135. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  136. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  137. /* remap printf */
  138. #undef printf
  139. #define printf M2M_LOG_INFO
  140. /* OS requires occasional sleep() */
  141. #ifndef TEST_SLEEP_MS
  142. #define TEST_SLEEP_MS 50
  143. #endif
  144. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  145. /* don't use file system for these tests, since ./certs dir isn't loaded */
  146. #undef NO_FILESYSTEM
  147. #define NO_FILESYSTEM
  148. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  149. !defined(THREADX_NO_DC_PRINTF)
  150. #ifndef NETOS
  151. /* since just testing, use THREADX log printf instead (NETOS prototypes
  152. * this elsewhere) */
  153. int dc_log_printf(char*, ...);
  154. #endif
  155. #undef printf
  156. #define printf dc_log_printf
  157. #elif defined(ANDROID)
  158. #ifdef XMALLOC_USER
  159. #include <stdlib.h> /* we're using malloc / free direct here */
  160. #endif
  161. #ifndef STRING_USER
  162. #include <stdio.h>
  163. #endif
  164. #include <android/log.h>
  165. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  166. #ifndef NO_FILESYSTEM
  167. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  168. #endif
  169. #else
  170. #define printf(...) \
  171. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  172. #define fprintf(fp, ...) \
  173. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  174. #endif
  175. #elif defined(WOLFSSL_DEOS)
  176. #include <printx.h>
  177. #undef printf
  178. #define printf printx
  179. #else
  180. #ifdef XMALLOC_USER
  181. #include <stdlib.h> /* we're using malloc / free direct here */
  182. #endif
  183. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  184. #include <stdio.h>
  185. #endif
  186. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  187. #undef printf
  188. #define printf(...) ({})
  189. #endif
  190. /* enable way for customer to override test/bench printf */
  191. #ifdef XPRINTF
  192. #undef printf
  193. #define printf XPRINTF
  194. #elif !defined(printf)
  195. /* arrange for printf() to flush after every message -- this assures
  196. * redirected output (to a log file) records progress right up to the
  197. * moment of a crash/abort(); otherwise anything queued in stdout would
  198. * be lost.
  199. */
  200. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  201. #endif
  202. #endif
  203. #endif /* !WOLFSSL_LOG_PRINTF */
  204. #include <wolfssl/wolfcrypt/memory.h>
  205. #include <wolfssl/wolfcrypt/wc_port.h>
  206. #include <wolfssl/wolfcrypt/logging.h>
  207. #include <wolfssl/wolfcrypt/types.h>
  208. #include <wolfssl/wolfcrypt/asn.h>
  209. #include <wolfssl/wolfcrypt/md2.h>
  210. #include <wolfssl/wolfcrypt/md5.h>
  211. #include <wolfssl/wolfcrypt/md4.h>
  212. #include <wolfssl/wolfcrypt/sha.h>
  213. #include <wolfssl/wolfcrypt/sha256.h>
  214. #include <wolfssl/wolfcrypt/sha512.h>
  215. #include <wolfssl/wolfcrypt/rc2.h>
  216. #include <wolfssl/wolfcrypt/arc4.h>
  217. #if !defined(WC_NO_RNG)
  218. #include <wolfssl/wolfcrypt/random.h>
  219. #endif
  220. #include <wolfssl/wolfcrypt/wolfmath.h>
  221. #include <wolfssl/wolfcrypt/coding.h>
  222. #include <wolfssl/wolfcrypt/signature.h>
  223. #include <wolfssl/wolfcrypt/rsa.h>
  224. #include <wolfssl/wolfcrypt/des3.h>
  225. #include <wolfssl/wolfcrypt/aes.h>
  226. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  227. #include <wolfssl/wolfcrypt/cmac.h>
  228. #include <wolfssl/wolfcrypt/siphash.h>
  229. #include <wolfssl/wolfcrypt/poly1305.h>
  230. #include <wolfssl/wolfcrypt/camellia.h>
  231. #include <wolfssl/wolfcrypt/hmac.h>
  232. #include <wolfssl/wolfcrypt/kdf.h>
  233. #include <wolfssl/wolfcrypt/dh.h>
  234. #include <wolfssl/wolfcrypt/dsa.h>
  235. #include <wolfssl/wolfcrypt/srp.h>
  236. #include <wolfssl/wolfcrypt/chacha.h>
  237. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  238. #include <wolfssl/wolfcrypt/pwdbased.h>
  239. #include <wolfssl/wolfcrypt/ripemd.h>
  240. #include <wolfssl/wolfcrypt/error-crypt.h>
  241. #ifdef HAVE_ECC
  242. #include <wolfssl/wolfcrypt/ecc.h>
  243. #endif
  244. #ifdef WOLFSSL_SM2
  245. #include <wolfssl/wolfcrypt/sm2.h>
  246. #endif
  247. #ifdef HAVE_HPKE
  248. #include <wolfssl/wolfcrypt/hpke.h>
  249. #endif
  250. #ifdef HAVE_CURVE25519
  251. #include <wolfssl/wolfcrypt/curve25519.h>
  252. #endif
  253. #ifdef HAVE_ED25519
  254. #include <wolfssl/wolfcrypt/ed25519.h>
  255. #endif
  256. #ifdef HAVE_CURVE448
  257. #include <wolfssl/wolfcrypt/curve448.h>
  258. #endif
  259. #ifdef HAVE_ED448
  260. #include <wolfssl/wolfcrypt/ed448.h>
  261. #endif
  262. #ifdef WOLFSSL_HAVE_KYBER
  263. #include <wolfssl/wolfcrypt/kyber.h>
  264. #ifdef WOLFSSL_WC_KYBER
  265. #include <wolfssl/wolfcrypt/wc_kyber.h>
  266. #endif
  267. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  268. #include <wolfssl/wolfcrypt/ext_kyber.h>
  269. #endif
  270. #endif
  271. #ifdef WOLFCRYPT_HAVE_ECCSI
  272. #include <wolfssl/wolfcrypt/eccsi.h>
  273. #endif
  274. #ifdef WOLFCRYPT_HAVE_SAKKE
  275. #include <wolfssl/wolfcrypt/sakke.h>
  276. #endif
  277. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  278. #include <wolfssl/wolfcrypt/blake2.h>
  279. #endif
  280. #ifdef WOLFSSL_SHA3
  281. #include <wolfssl/wolfcrypt/sha3.h>
  282. #endif
  283. #ifdef WOLFSSL_SM3
  284. #include <wolfssl/wolfcrypt/sm3.h>
  285. #endif
  286. #ifdef WOLFSSL_SM4
  287. #include <wolfssl/wolfcrypt/sm4.h>
  288. #endif
  289. #ifdef HAVE_LIBZ
  290. #include <wolfssl/wolfcrypt/compress.h>
  291. #endif
  292. #ifdef HAVE_PKCS7
  293. #include <wolfssl/wolfcrypt/pkcs7.h>
  294. #endif
  295. #ifdef HAVE_FIPS
  296. #include <wolfssl/wolfcrypt/fips_test.h>
  297. #endif
  298. #ifdef HAVE_SELFTEST
  299. #include <wolfssl/wolfcrypt/selftest.h>
  300. #endif
  301. #ifdef WOLFSSL_ASYNC_CRYPT
  302. #include <wolfssl/wolfcrypt/async.h>
  303. #endif
  304. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  305. #include <wolfssl/wolfcrypt/logging.h>
  306. #endif
  307. #ifdef WOLFSSL_CAAM
  308. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  309. #endif
  310. #ifdef WOLF_CRYPTO_CB
  311. #include <wolfssl/wolfcrypt/cryptocb.h>
  312. #ifdef HAVE_INTEL_QA_SYNC
  313. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  314. #endif
  315. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  316. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  317. #endif
  318. #endif
  319. #ifdef _MSC_VER
  320. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  321. #pragma warning(disable: 4996)
  322. #endif
  323. #ifdef OPENSSL_EXTRA
  324. #ifndef WOLFCRYPT_ONLY
  325. #include <wolfssl/openssl/evp.h>
  326. #include <wolfssl/openssl/hmac.h>
  327. #endif
  328. #include <wolfssl/openssl/rand.h>
  329. #include <wolfssl/openssl/aes.h>
  330. #include <wolfssl/openssl/des.h>
  331. #endif
  332. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  333. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  334. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  335. #define USE_CERT_BUFFERS_2048
  336. #endif
  337. #if !defined(USE_CERT_BUFFERS_256)
  338. #define USE_CERT_BUFFERS_256
  339. #endif
  340. #endif
  341. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  342. #define ENABLE_ECC384_CERT_GEN_TEST
  343. #endif
  344. #include <wolfssl/certs_test.h>
  345. #ifdef DEVKITPRO
  346. #include <wiiuse/wpad.h>
  347. #endif
  348. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  349. /* FIPS build has replaced ecc.h. */
  350. #define wc_ecc_key_get_priv(key) (&((key)->k))
  351. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  352. #endif
  353. #ifdef WOLFSSL_STATIC_MEMORY
  354. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  355. #else
  356. #define HEAP_HINT NULL
  357. #endif /* WOLFSSL_STATIC_MEMORY */
  358. /* these cases do not have intermediate hashing support */
  359. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  360. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  361. #define NO_INTM_HASH_TEST
  362. #endif
  363. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  364. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  365. defined(WOLFSSL_SECO_CAAM)
  366. #define HASH_SIZE_LIMIT
  367. #endif
  368. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  369. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  370. static void initDefaultName(void);
  371. #endif
  372. /* for async devices */
  373. #ifdef WOLFSSL_CAAM_DEVID
  374. static int devId = WOLFSSL_CAAM_DEVID;
  375. #else
  376. static int devId = INVALID_DEVID;
  377. #endif
  378. #ifdef HAVE_WNR
  379. const char* wnrConfigFile = "wnr-example.conf";
  380. #endif
  381. #define TEST_STRING "Everyone gets Friday off."
  382. #define TEST_STRING_SZ 25
  383. typedef struct testVector {
  384. const char* input;
  385. const char* output;
  386. size_t inLen;
  387. size_t outLen;
  388. } testVector;
  389. #ifndef WOLFSSL_TEST_SUBROUTINE
  390. #define WOLFSSL_TEST_SUBROUTINE
  391. #endif
  392. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  393. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  394. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  395. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  396. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  397. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  398. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  399. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  400. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  401. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  402. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  403. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  404. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  405. #if !defined(WOLFSSL_NOSHA512_224) && \
  406. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  407. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  408. #endif
  409. #if !defined(WOLFSSL_NOSHA512_256) && \
  410. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  411. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  412. #endif
  413. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  414. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  415. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  416. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  417. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  418. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  419. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  420. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  421. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  422. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  423. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  424. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  425. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  426. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  427. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  428. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  429. defined(WOLFSSL_AFALG_XILINX_RSA)
  430. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  431. static wc_test_ret_t hkdf_test(void);
  432. #else
  433. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  434. #endif
  435. #endif
  436. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  437. #ifdef WOLFSSL_TLS13
  438. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  439. #endif
  440. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  441. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  442. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  443. #ifdef WC_RC2
  444. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  445. #endif
  446. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  449. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  452. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  453. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  454. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  455. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  456. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  457. #if defined(WOLFSSL_SIPHASH)
  458. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  459. #endif
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  461. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  462. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  464. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  465. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  467. #ifdef WOLFSSL_SM4
  468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  469. #endif
  470. #ifdef WC_RSA_NO_PADDING
  471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  472. #endif
  473. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  474. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  475. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  476. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  477. #ifndef WC_NO_RNG
  478. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  479. #endif /* WC_NO_RNG */
  480. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  481. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  482. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  483. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  484. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  485. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  486. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  487. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  488. #endif
  489. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  490. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  491. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  493. #ifdef HAVE_ECC
  494. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  495. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  496. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  497. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  498. #endif
  499. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  500. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  501. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  502. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  503. /* skip for ATECC508/608A, cannot import private key buffers */
  504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  505. #endif
  506. #endif
  507. #ifdef HAVE_CURVE25519
  508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  509. #endif
  510. #ifdef HAVE_ED25519
  511. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  512. #endif
  513. #ifdef HAVE_CURVE448
  514. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  515. #endif
  516. #ifdef HAVE_ED448
  517. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  518. #endif
  519. #ifdef WOLFSSL_HAVE_KYBER
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  521. #endif
  522. #ifdef WOLFCRYPT_HAVE_ECCSI
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  524. #endif
  525. #ifdef WOLFCRYPT_HAVE_SAKKE
  526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  527. #endif
  528. #ifdef HAVE_BLAKE2
  529. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  530. #endif
  531. #ifdef HAVE_BLAKE2S
  532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  533. #endif
  534. #ifdef HAVE_LIBZ
  535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  536. #endif
  537. #ifdef HAVE_PKCS7
  538. #ifndef NO_PKCS7_ENCRYPTED_DATA
  539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  540. #endif
  541. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  542. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  543. #endif
  544. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  545. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  546. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  548. #endif
  549. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  551. word32 keySz);
  552. #endif
  553. #endif
  554. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  555. !defined(NO_FILESYSTEM)
  556. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  557. #endif
  558. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  559. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  561. #endif
  562. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  563. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  564. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  565. #endif
  566. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  567. #if defined(WOLFSSL_PUBLIC_MP) && \
  568. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  569. defined(USE_FAST_MATH))
  570. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  571. #endif
  572. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  573. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  574. #endif
  575. #if defined(ASN_BER_TO_DER) && \
  576. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  577. defined(OPENSSL_EXTRA_X509_SMALL))
  578. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  579. #endif
  580. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  581. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  582. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  583. #endif
  584. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  585. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  586. #else
  587. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  588. #endif
  589. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  590. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  591. #endif
  592. #ifdef WOLFSSL_CAAM_BLOB
  593. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  594. #endif
  595. #ifdef WOLF_CRYPTO_CB
  596. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  597. #endif
  598. #ifdef WOLFSSL_CERT_PIV
  599. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  600. #endif
  601. #ifdef WOLFSSL_AES_SIV
  602. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  603. #endif
  604. /* General big buffer size for many tests. */
  605. #define FOURK_BUF 4096
  606. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  607. static void render_error_message(const char* msg, wc_test_ret_t es)
  608. {
  609. (void)msg;
  610. (void)es;
  611. #ifdef WOLFSSL_LINUXKM
  612. #define err_sys_printf lkm_printf
  613. #else
  614. #define err_sys_printf printf
  615. #endif
  616. switch (WC_TEST_RET_DEC_TAG(es)) {
  617. case WC_TEST_RET_TAG_NC:
  618. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  619. break;
  620. case WC_TEST_RET_TAG_EC:
  621. #ifdef NO_ERROR_STRINGS
  622. err_sys_printf("%s error L=%d code=%d\n", msg,
  623. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  624. #else
  625. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  626. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  627. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  628. #endif
  629. break;
  630. case WC_TEST_RET_TAG_ERRNO:
  631. {
  632. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  633. * flavor that always returns a non-null char pointer that must be used
  634. * directly, and a POSIX flavor that returns an error int, and iff success,
  635. * stores an error string in the supplied buffer. this is all most
  636. * infelicitous...
  637. */
  638. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  639. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  640. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  641. (defined(__USE_XOPEN2K) && \
  642. defined(_POSIX_C_SOURCE) && \
  643. (_POSIX_C_SOURCE >= 200112L)))
  644. char errno_buf[64], *errno_string;
  645. /* precisely mirror the gate used in glibc string.h */
  646. #if defined __USE_XOPEN2K && !defined __USE_GNU
  647. if (strerror_r(WC_TEST_RET_DEC_I(es),
  648. errno_buf, sizeof(errno_buf)) != 0)
  649. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  650. errno_string = errno_buf;
  651. #else
  652. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  653. errno_buf, sizeof(errno_buf));
  654. #endif
  655. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  656. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  657. errno_string);
  658. #else /* can't figure out how to strerror_r(), or don't want error strings */
  659. err_sys_printf("%s error L=%d errno=%d\n", msg,
  660. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  661. #endif
  662. break;
  663. }
  664. case WC_TEST_RET_TAG_I:
  665. err_sys_printf("%s error L=%d i=%d\n", msg,
  666. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  667. break;
  668. }
  669. #undef err_sys_printf
  670. }
  671. static void print_fiducials(void);
  672. #ifdef HAVE_STACK_SIZE
  673. static THREAD_RETURN err_sys(const char* msg, int es)
  674. #else
  675. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  676. #endif
  677. {
  678. render_error_message(msg, es);
  679. print_fiducials();
  680. #ifdef WOLFSSL_LINUXKM
  681. EXIT_TEST(es);
  682. #else
  683. EXIT_TEST(-1);
  684. #endif
  685. }
  686. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  687. /* func_args from test.h, so don't have to pull in other stuff */
  688. typedef struct func_args {
  689. int argc;
  690. char** argv;
  691. wc_test_ret_t return_code;
  692. } func_args;
  693. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  694. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  695. static void myFipsCb(int ok, int err, const char* hash)
  696. {
  697. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  698. printf("message = %s\n", wc_GetErrorString(err));
  699. printf("hash = %s\n", hash);
  700. if (err == IN_CORE_FIPS_E) {
  701. printf("In core integrity hash check failure, copy above hash\n");
  702. printf("into verifyCore[] in fips_test.c and rebuild\n");
  703. }
  704. }
  705. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  706. #ifdef WOLFSSL_STATIC_MEMORY
  707. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  708. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  709. #elif defined(BENCH_EMBEDDED)
  710. static byte gTestMemory[14000];
  711. #elif defined(WOLFSSL_CERT_EXT)
  712. static byte gTestMemory[140000];
  713. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  714. !defined(ALT_ECC_SIZE)
  715. static byte gTestMemory[160000];
  716. #else
  717. static byte gTestMemory[80000];
  718. #endif
  719. #endif
  720. #ifdef WOLFSSL_PB
  721. static int wolfssl_pb_print(const char* msg, ...)
  722. {
  723. int ret;
  724. va_list args;
  725. char tmpBuf[80];
  726. va_start(args, msg);
  727. ret = vsprint(tmpBuf, msg, args);
  728. va_end(args);
  729. fnDumpStringToSystemLog(tmpBuf);
  730. return ret;
  731. }
  732. #endif /* WOLFSSL_PB */
  733. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  734. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  735. /* Enable support for RNG with crypto callback */
  736. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  737. {
  738. int rc = CRYPTOCB_UNAVAILABLE;
  739. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  740. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  741. }
  742. (void)ctx;
  743. (void)thisDevId;
  744. return rc;
  745. }
  746. #endif
  747. /* optional macro to add sleep between tests */
  748. #ifndef TEST_SLEEP
  749. #define TEST_SLEEP()
  750. #else
  751. #define TEST_PASS test_pass
  752. #include <stdarg.h> /* for var args */
  753. static WC_INLINE void test_pass(const char* fmt, ...)
  754. {
  755. va_list args;
  756. va_start(args, fmt);
  757. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  758. va_end(args);
  759. PRINT_HEAP_CHECKPOINT();
  760. TEST_SLEEP();
  761. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  762. }
  763. #endif
  764. /* set test pass output to printf if not overriden */
  765. #ifndef TEST_PASS
  766. /* redirect to printf */
  767. #define TEST_PASS(...) { \
  768. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  769. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  770. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  771. } \
  772. PRINT_HEAP_CHECKPOINT(); \
  773. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  774. }
  775. #endif
  776. #ifdef TEST_ALWAYS_RUN_TO_END
  777. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  778. #elif !defined(TEST_FAIL)
  779. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  780. #endif
  781. #ifdef HAVE_STACK_SIZE
  782. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  783. #else
  784. wc_test_ret_t wolfcrypt_test(void* args)
  785. #endif
  786. {
  787. wc_test_ret_t ret;
  788. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  789. long heap_baselineAllocs, heap_baselineBytes;
  790. #endif
  791. #ifdef TEST_ALWAYS_RUN_TO_END
  792. int last_failed_test_ret = 0;
  793. #endif
  794. STACK_SIZE_INIT();
  795. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  796. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  797. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  798. (void)wolfCrypt_heap_peakBytes_checkpoint();
  799. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  800. #endif
  801. printf("------------------------------------------------------------------------------\n");
  802. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  803. printf("------------------------------------------------------------------------------\n");
  804. if (args) {
  805. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  806. int ch;
  807. #endif
  808. ((func_args*)args)->return_code = -1; /* error state */
  809. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  810. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  811. switch(ch) {
  812. case 's':
  813. #ifdef HAVE_STACK_SIZE_VERBOSE
  814. max_relative_stack = (ssize_t)atoi(myoptarg);
  815. break;
  816. #else
  817. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  818. #endif
  819. case 'm':
  820. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  821. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  822. break;
  823. #else
  824. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  825. #endif
  826. case 'a':
  827. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  828. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  829. break;
  830. #else
  831. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  832. #endif
  833. case 'h':
  834. return err_sys("\
  835. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  836. [-a max_relative_heap_allocs] [-h]\n", 0);
  837. default:
  838. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  839. }
  840. }
  841. #endif
  842. }
  843. #ifdef WOLFSSL_STATIC_MEMORY
  844. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  845. WOLFMEM_GENERAL, 1) != 0) {
  846. printf("unable to load static memory.\n");
  847. return(EXIT_FAILURE);
  848. }
  849. #endif
  850. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  851. wolfSSL_Debugging_ON();
  852. #endif
  853. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  854. wc_SetLoggingHeap(HEAP_HINT);
  855. #endif
  856. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  857. wolfCrypt_SetCb_fips(myFipsCb);
  858. #endif
  859. #if !defined(NO_BIG_INT)
  860. if (CheckCtcSettings() != 1) {
  861. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  862. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  863. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  864. }
  865. #if defined(USE_FAST_MATH) && \
  866. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  867. if (CheckFastMathSettings() != 1)
  868. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  869. WC_TEST_RET_ENC_NC);
  870. #endif /* USE_FAST_MATH */
  871. #endif /* !NO_BIG_INT */
  872. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  873. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  874. initDefaultName();
  875. #endif
  876. #ifdef WOLFSSL_ASYNC_CRYPT
  877. ret = wolfAsync_DevOpen(&devId);
  878. if (ret < 0) {
  879. printf("Async device open failed\nRunning without async\n");
  880. }
  881. #else
  882. (void)devId;
  883. #endif /* WOLFSSL_ASYNC_CRYPT */
  884. #ifdef WOLF_CRYPTO_CB
  885. #ifdef HAVE_INTEL_QA_SYNC
  886. devId = wc_CryptoCb_InitIntelQa();
  887. if (INVALID_DEVID == devId) {
  888. printf("Couldn't init the Intel QA\n");
  889. }
  890. #endif
  891. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  892. devId = wc_CryptoCb_InitOcteon();
  893. if (INVALID_DEVID == devId) {
  894. printf("Couldn't init the Cavium Octeon\n");
  895. }
  896. #endif
  897. #endif
  898. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  899. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  900. if (devId == INVALID_DEVID) {
  901. /* for testing RNG with crypto callback register function */
  902. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  903. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  904. }
  905. #endif
  906. #ifdef HAVE_SELFTEST
  907. if ( (ret = wolfCrypt_SelfTest()) != 0)
  908. TEST_FAIL("CAVP selftest failed!\n", ret);
  909. else
  910. TEST_PASS("CAVP selftest passed!\n");
  911. #endif
  912. if ( (ret = error_test()) != 0)
  913. TEST_FAIL("error test failed!\n", ret);
  914. else
  915. TEST_PASS("error test passed!\n");
  916. if ( (ret = memory_test()) != 0)
  917. TEST_FAIL("MEMORY test failed!\n", ret);
  918. else
  919. TEST_PASS("MEMORY test passed!\n");
  920. #ifndef NO_CODING
  921. if ( (ret = base64_test()) != 0)
  922. TEST_FAIL("base64 test failed!\n", ret);
  923. else
  924. TEST_PASS("base64 test passed!\n");
  925. #ifdef WOLFSSL_BASE16
  926. if ( (ret = base16_test()) != 0)
  927. TEST_FAIL("base16 test failed!\n", ret);
  928. else
  929. TEST_PASS("base16 test passed!\n");
  930. #endif
  931. #endif /* !NO_CODING */
  932. #ifndef NO_ASN
  933. if ( (ret = asn_test()) != 0)
  934. TEST_FAIL("asn test failed!\n", ret);
  935. else
  936. TEST_PASS("asn test passed!\n");
  937. #endif
  938. #ifndef WC_NO_RNG
  939. if ( (ret = random_test()) != 0)
  940. TEST_FAIL("RANDOM test failed!\n", ret);
  941. else
  942. TEST_PASS("RANDOM test passed!\n");
  943. #endif /* WC_NO_RNG */
  944. #ifndef NO_MD5
  945. if ( (ret = md5_test()) != 0)
  946. TEST_FAIL("MD5 test failed!\n", ret);
  947. else
  948. TEST_PASS("MD5 test passed!\n");
  949. #endif
  950. #ifdef WOLFSSL_MD2
  951. if ( (ret = md2_test()) != 0)
  952. TEST_FAIL("MD2 test failed!\n", ret);
  953. else
  954. TEST_PASS("MD2 test passed!\n");
  955. #endif
  956. #ifndef NO_MD4
  957. if ( (ret = md4_test()) != 0)
  958. TEST_FAIL("MD4 test failed!\n", ret);
  959. else
  960. TEST_PASS("MD4 test passed!\n");
  961. #endif
  962. #ifndef NO_SHA
  963. if ( (ret = sha_test()) != 0)
  964. TEST_FAIL("SHA test failed!\n", ret);
  965. else
  966. TEST_PASS("SHA test passed!\n");
  967. #endif
  968. #ifdef WOLFSSL_SHA224
  969. if ( (ret = sha224_test()) != 0)
  970. TEST_FAIL("SHA-224 test failed!\n", ret);
  971. else
  972. TEST_PASS("SHA-224 test passed!\n");
  973. #endif
  974. #ifndef NO_SHA256
  975. if ( (ret = sha256_test()) != 0)
  976. TEST_FAIL("SHA-256 test failed!\n", ret);
  977. else
  978. TEST_PASS("SHA-256 test passed!\n");
  979. #endif
  980. #ifdef WOLFSSL_SHA384
  981. if ( (ret = sha384_test()) != 0)
  982. TEST_FAIL("SHA-384 test failed!\n", ret);
  983. else
  984. TEST_PASS("SHA-384 test passed!\n");
  985. #endif
  986. #ifdef WOLFSSL_SHA512
  987. if ((ret = sha512_test()) != 0) {
  988. TEST_FAIL("SHA-512 test failed!\n", ret);
  989. }
  990. else {
  991. TEST_PASS("SHA-512 test passed!\n");
  992. }
  993. #if !defined(WOLFSSL_NOSHA512_224) && \
  994. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  995. if ((ret = sha512_224_test()) != 0) {
  996. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  997. }
  998. else
  999. TEST_PASS("SHA-512/224 test passed!\n");
  1000. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1001. #if !defined(WOLFSSL_NOSHA512_256) && \
  1002. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1003. if ((ret = sha512_256_test()) != 0) {
  1004. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1005. }
  1006. else
  1007. TEST_PASS("SHA-512/256 test passed!\n");
  1008. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1009. #endif /* WOLFSSL_SHA512 */
  1010. #ifdef WOLFSSL_SHA3
  1011. if ( (ret = sha3_test()) != 0)
  1012. TEST_FAIL("SHA-3 test failed!\n", ret);
  1013. else
  1014. TEST_PASS("SHA-3 test passed!\n");
  1015. #endif
  1016. #ifdef WOLFSSL_SHAKE128
  1017. if ( (ret = shake128_test()) != 0)
  1018. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1019. else
  1020. TEST_PASS("SHAKE128 test passed!\n");
  1021. #endif
  1022. #ifdef WOLFSSL_SHAKE256
  1023. if ( (ret = shake256_test()) != 0)
  1024. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1025. else
  1026. TEST_PASS("SHAKE256 test passed!\n");
  1027. #endif
  1028. #ifdef WOLFSSL_SM3
  1029. if ( (ret = sm3_test()) != 0)
  1030. return err_sys("SM-3 test failed!\n", ret);
  1031. else
  1032. TEST_PASS("SM-3 test passed!\n");
  1033. #endif
  1034. #ifndef NO_HASH_WRAPPER
  1035. if ( (ret = hash_test()) != 0)
  1036. TEST_FAIL("Hash test failed!\n", ret);
  1037. else
  1038. TEST_PASS("Hash test passed!\n");
  1039. #endif
  1040. #ifdef WOLFSSL_RIPEMD
  1041. if ( (ret = ripemd_test()) != 0)
  1042. TEST_FAIL("RIPEMD test failed!\n", ret);
  1043. else
  1044. TEST_PASS("RIPEMD test passed!\n");
  1045. #endif
  1046. #ifdef HAVE_BLAKE2
  1047. if ( (ret = blake2b_test()) != 0)
  1048. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1049. else
  1050. TEST_PASS("BLAKE2b test passed!\n");
  1051. #endif
  1052. #ifdef HAVE_BLAKE2S
  1053. if ( (ret = blake2s_test()) != 0)
  1054. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1055. else
  1056. TEST_PASS("BLAKE2s test passed!\n");
  1057. #endif
  1058. #ifndef NO_HMAC
  1059. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1060. && (HAVE_FIPS_VERSION >= 5))
  1061. if ( (ret = hmac_md5_test()) != 0)
  1062. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1063. else
  1064. TEST_PASS("HMAC-MD5 test passed!\n");
  1065. #endif
  1066. #ifndef NO_SHA
  1067. if ( (ret = hmac_sha_test()) != 0)
  1068. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1069. else
  1070. TEST_PASS("HMAC-SHA test passed!\n");
  1071. #endif
  1072. #ifdef WOLFSSL_SHA224
  1073. if ( (ret = hmac_sha224_test()) != 0)
  1074. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1075. else
  1076. TEST_PASS("HMAC-SHA224 test passed!\n");
  1077. #endif
  1078. #ifndef NO_SHA256
  1079. if ( (ret = hmac_sha256_test()) != 0)
  1080. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1081. else
  1082. TEST_PASS("HMAC-SHA256 test passed!\n");
  1083. #endif
  1084. #ifdef WOLFSSL_SHA384
  1085. if ( (ret = hmac_sha384_test()) != 0)
  1086. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1087. else
  1088. TEST_PASS("HMAC-SHA384 test passed!\n");
  1089. #endif
  1090. #ifdef WOLFSSL_SHA512
  1091. if ( (ret = hmac_sha512_test()) != 0)
  1092. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1093. else
  1094. TEST_PASS("HMAC-SHA512 test passed!\n");
  1095. #endif
  1096. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1097. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1098. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1099. if ( (ret = hmac_sha3_test()) != 0)
  1100. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1101. else
  1102. TEST_PASS("HMAC-SHA3 test passed!\n");
  1103. #endif
  1104. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1105. PRIVATE_KEY_UNLOCK();
  1106. if ( (ret = hkdf_test()) != 0)
  1107. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1108. else
  1109. TEST_PASS("HMAC-KDF test passed!\n");
  1110. PRIVATE_KEY_LOCK();
  1111. #endif
  1112. #endif /* !NO_HMAC */
  1113. #ifdef WOLFSSL_WOLFSSH
  1114. PRIVATE_KEY_UNLOCK();
  1115. if ( (ret = sshkdf_test()) != 0)
  1116. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1117. else
  1118. TEST_PASS("SSH-KDF test passed!\n");
  1119. PRIVATE_KEY_LOCK();
  1120. #endif /* WOLFSSL_WOLFSSH */
  1121. #ifdef WOLFSSL_TLS13
  1122. PRIVATE_KEY_UNLOCK();
  1123. if ( (ret = tls13_kdf_test()) != 0)
  1124. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1125. else
  1126. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1127. PRIVATE_KEY_LOCK();
  1128. #endif /* WOLFSSL_TLS13 */
  1129. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1130. if ( (ret = x963kdf_test()) != 0)
  1131. TEST_FAIL("X963-KDF test failed!\n", ret);
  1132. else
  1133. TEST_PASS("X963-KDF test passed!\n");
  1134. #endif
  1135. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1136. if ( (ret = hpke_test()) != 0)
  1137. TEST_FAIL("HPKE test failed!\n", ret);
  1138. else
  1139. TEST_PASS("HPKE test passed!\n");
  1140. #endif
  1141. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1142. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  1143. if ( (ret = gmac_test()) != 0)
  1144. TEST_FAIL("GMAC test failed!\n", ret);
  1145. else
  1146. TEST_PASS("GMAC test passed!\n");
  1147. #endif
  1148. #ifdef WC_RC2
  1149. if ( (ret = rc2_test()) != 0)
  1150. TEST_FAIL("RC2 test failed!\n", ret);
  1151. else
  1152. TEST_PASS("RC2 test passed!\n");
  1153. #endif
  1154. #ifndef NO_RC4
  1155. if ( (ret = arc4_test()) != 0)
  1156. TEST_FAIL("ARC4 test failed!\n", ret);
  1157. else
  1158. TEST_PASS("ARC4 test passed!\n");
  1159. #endif
  1160. #ifdef HAVE_CHACHA
  1161. if ( (ret = chacha_test()) != 0)
  1162. TEST_FAIL("Chacha test failed!\n", ret);
  1163. else
  1164. TEST_PASS("Chacha test passed!\n");
  1165. #endif
  1166. #ifdef HAVE_XCHACHA
  1167. if ( (ret = XChaCha_test()) != 0)
  1168. TEST_FAIL("XChacha test failed!\n", ret);
  1169. else
  1170. TEST_PASS("XChacha test passed!\n");
  1171. #endif
  1172. #ifdef HAVE_POLY1305
  1173. if ( (ret = poly1305_test()) != 0)
  1174. TEST_FAIL("POLY1305 test failed!\n", ret);
  1175. else
  1176. TEST_PASS("POLY1305 test passed!\n");
  1177. #endif
  1178. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1179. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1180. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1181. else
  1182. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1183. #endif
  1184. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1185. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1186. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1187. else
  1188. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1189. #endif
  1190. #ifndef NO_DES3
  1191. if ( (ret = des_test()) != 0)
  1192. TEST_FAIL("DES test failed!\n", ret);
  1193. else
  1194. TEST_PASS("DES test passed!\n");
  1195. #endif
  1196. #ifndef NO_DES3
  1197. if ( (ret = des3_test()) != 0)
  1198. TEST_FAIL("DES3 test failed!\n", ret);
  1199. else
  1200. TEST_PASS("DES3 test passed!\n");
  1201. #endif
  1202. #ifndef NO_AES
  1203. if ( (ret = aes_test()) != 0)
  1204. TEST_FAIL("AES test failed!\n", ret);
  1205. else
  1206. TEST_PASS("AES test passed!\n");
  1207. #ifdef WOLFSSL_AES_192
  1208. if ( (ret = aes192_test()) != 0)
  1209. TEST_FAIL("AES192 test failed!\n", ret);
  1210. else
  1211. TEST_PASS("AES192 test passed!\n");
  1212. #endif
  1213. #ifdef WOLFSSL_AES_256
  1214. if ( (ret = aes256_test()) != 0)
  1215. TEST_FAIL("AES256 test failed!\n", ret);
  1216. else
  1217. TEST_PASS("AES256 test passed!\n");
  1218. #endif
  1219. #ifdef WOLFSSL_AES_OFB
  1220. if ( (ret = aesofb_test()) != 0)
  1221. TEST_FAIL("AES-OFB test failed!\n", ret);
  1222. else
  1223. TEST_PASS("AESOFB test passed!\n");
  1224. #endif
  1225. #ifdef HAVE_AESGCM
  1226. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1227. if ( (ret = aesgcm_test()) != 0)
  1228. TEST_FAIL("AES-GCM test failed!\n", ret);
  1229. #endif
  1230. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1231. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1232. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1233. if ((ret = aesgcm_default_test()) != 0) {
  1234. TEST_FAIL("AES-GCM test failed!\n", ret);
  1235. }
  1236. #endif
  1237. if (ret == 0) {
  1238. TEST_PASS("AES-GCM test passed!\n");
  1239. }
  1240. #endif
  1241. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1242. if ( (ret = aesccm_test()) != 0)
  1243. TEST_FAIL("AES-CCM test failed!\n", ret);
  1244. else
  1245. TEST_PASS("AES-CCM test passed!\n");
  1246. #endif
  1247. #ifdef HAVE_AES_KEYWRAP
  1248. if ( (ret = aeskeywrap_test()) != 0)
  1249. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1250. else
  1251. TEST_PASS("AES Key Wrap test passed!\n");
  1252. #endif
  1253. #ifdef WOLFSSL_AES_SIV
  1254. if ( (ret = aes_siv_test()) != 0)
  1255. TEST_FAIL("AES-SIV test failed!\n", ret);
  1256. else
  1257. TEST_PASS("AES-SIV test passed!\n");
  1258. #endif
  1259. #endif
  1260. #ifdef HAVE_CAMELLIA
  1261. if ( (ret = camellia_test()) != 0)
  1262. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1263. else
  1264. TEST_PASS("CAMELLIA test passed!\n");
  1265. #endif
  1266. #ifdef WOLFSSL_SM4
  1267. if ( (ret = sm4_test()) != 0)
  1268. return err_sys("SM-4 test failed!\n", ret);
  1269. else
  1270. TEST_PASS("SM-4 test passed!\n");
  1271. #endif
  1272. #if !defined(NO_RSA)
  1273. #ifdef WC_RSA_NO_PADDING
  1274. if ( (ret = rsa_no_pad_test()) != 0)
  1275. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1276. else
  1277. TEST_PASS("RSA NOPAD test passed!\n");
  1278. #endif
  1279. if ( (ret = rsa_test()) != 0)
  1280. TEST_FAIL("RSA test failed!\n", ret);
  1281. else
  1282. TEST_PASS("RSA test passed!\n");
  1283. #endif
  1284. #ifndef NO_DH
  1285. PRIVATE_KEY_UNLOCK();
  1286. if ( (ret = dh_test()) != 0)
  1287. TEST_FAIL("DH test failed!\n", ret);
  1288. else
  1289. TEST_PASS("DH test passed!\n");
  1290. PRIVATE_KEY_LOCK();
  1291. #endif
  1292. #ifndef NO_DSA
  1293. if ( (ret = dsa_test()) != 0)
  1294. TEST_FAIL("DSA test failed!\n", ret);
  1295. else
  1296. TEST_PASS("DSA test passed!\n");
  1297. #endif
  1298. #ifdef WOLFCRYPT_HAVE_SRP
  1299. if ( (ret = srp_test()) != 0)
  1300. TEST_FAIL("SRP test failed!\n", ret);
  1301. else
  1302. TEST_PASS("SRP test passed!\n");
  1303. #endif
  1304. #ifndef NO_PWDBASED
  1305. if ( (ret = pwdbased_test()) != 0)
  1306. TEST_FAIL("PWDBASED test failed!\n", ret);
  1307. else
  1308. TEST_PASS("PWDBASED test passed!\n");
  1309. #endif
  1310. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1311. if ( (ret = openssl_test()) != 0)
  1312. TEST_FAIL("OPENSSL test failed!\n", ret);
  1313. else
  1314. TEST_PASS("OPENSSL test passed!\n");
  1315. if ( (ret = openSSL_evpMD_test()) != 0)
  1316. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1317. else
  1318. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1319. if ( (ret = openssl_pkey0_test()) != 0)
  1320. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1321. else
  1322. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1323. if ( (ret = openssl_pkey1_test()) != 0)
  1324. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1325. else
  1326. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1327. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1328. if ( (ret = openssl_evpSig_test()) != 0)
  1329. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1330. else
  1331. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1332. #endif
  1333. #endif
  1334. #if defined(HAVE_ECC)
  1335. PRIVATE_KEY_UNLOCK();
  1336. if ( (ret = ecc_test()) != 0)
  1337. TEST_FAIL("ECC test failed!\n", ret);
  1338. else
  1339. TEST_PASS("ECC test passed!\n");
  1340. PRIVATE_KEY_LOCK();
  1341. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1342. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1343. if ( (ret = ecc_encrypt_test()) != 0)
  1344. TEST_FAIL("ECC Enc test failed!\n", ret);
  1345. else
  1346. TEST_PASS("ECC Enc test passed!\n");
  1347. #endif
  1348. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1349. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1350. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1351. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1352. /* skip for ATECC508/608A, cannot import private key buffers */
  1353. if ( (ret = ecc_test_buffers()) != 0)
  1354. TEST_FAIL("ECC buffer test failed!\n", ret);
  1355. else
  1356. TEST_PASS("ECC buffer test passed!\n");
  1357. #endif
  1358. #endif
  1359. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1360. !defined(NO_FILESYSTEM)
  1361. if ( (ret = cert_test()) != 0)
  1362. TEST_FAIL("CERT test failed!\n", ret);
  1363. else
  1364. TEST_PASS("CERT test passed!\n");
  1365. #endif
  1366. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1367. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1368. if ( (ret = certext_test()) != 0)
  1369. TEST_FAIL("CERT EXT test failed!\n", ret);
  1370. else
  1371. TEST_PASS("CERT EXT test passed!\n");
  1372. #endif
  1373. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1374. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1375. if ( (ret = decodedCertCache_test()) != 0)
  1376. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1377. else
  1378. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1379. #endif
  1380. #ifdef HAVE_CURVE25519
  1381. if ( (ret = curve25519_test()) != 0)
  1382. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1383. else
  1384. TEST_PASS("CURVE25519 test passed!\n");
  1385. #endif
  1386. #ifdef HAVE_ED25519
  1387. if ( (ret = ed25519_test()) != 0)
  1388. TEST_FAIL("ED25519 test failed!\n", ret);
  1389. else
  1390. TEST_PASS("ED25519 test passed!\n");
  1391. #endif
  1392. #ifdef HAVE_CURVE448
  1393. if ( (ret = curve448_test()) != 0)
  1394. TEST_FAIL("CURVE448 test failed!\n", ret);
  1395. else
  1396. TEST_PASS("CURVE448 test passed!\n");
  1397. #endif
  1398. #ifdef HAVE_ED448
  1399. if ( (ret = ed448_test()) != 0)
  1400. TEST_FAIL("ED448 test failed!\n", ret);
  1401. else
  1402. TEST_PASS("ED448 test passed!\n");
  1403. #endif
  1404. #ifdef WOLFSSL_HAVE_KYBER
  1405. if ( (ret = kyber_test()) != 0)
  1406. TEST_FAIL("KYBER test failed!\n", ret);
  1407. else
  1408. TEST_PASS("KYBER test passed!\n");
  1409. #endif
  1410. #ifdef WOLFCRYPT_HAVE_ECCSI
  1411. if ( (ret = eccsi_test()) != 0)
  1412. TEST_FAIL("ECCSI test failed!\n", ret);
  1413. else
  1414. TEST_PASS("ECCSI test passed!\n");
  1415. #endif
  1416. #ifdef WOLFCRYPT_HAVE_SAKKE
  1417. if ( (ret = sakke_test()) != 0)
  1418. TEST_FAIL("SAKKE test failed!\n", ret);
  1419. else
  1420. TEST_PASS("SAKKE test passed!\n");
  1421. #endif
  1422. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1423. if ( (ret = cmac_test()) != 0)
  1424. TEST_FAIL("CMAC test failed!\n", ret);
  1425. else
  1426. TEST_PASS("CMAC test passed!\n");
  1427. #endif
  1428. #if defined(WOLFSSL_SIPHASH)
  1429. if ( (ret = siphash_test()) != 0)
  1430. TEST_FAIL("SipHash test failed!\n", ret);
  1431. else
  1432. TEST_PASS("SipHash test passed!\n");
  1433. #endif
  1434. #ifdef HAVE_LIBZ
  1435. if ( (ret = compress_test()) != 0)
  1436. TEST_FAIL("COMPRESS test failed!\n", ret);
  1437. else
  1438. TEST_PASS("COMPRESS test passed!\n");
  1439. #endif
  1440. #ifdef HAVE_PKCS7
  1441. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1442. if ( (ret = pkcs7encrypted_test()) != 0)
  1443. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  1444. else
  1445. TEST_PASS("PKCS7encrypted test passed!\n");
  1446. #endif
  1447. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1448. if ( (ret = pkcs7compressed_test()) != 0)
  1449. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  1450. else
  1451. TEST_PASS("PKCS7compressed test passed!\n");
  1452. #endif
  1453. if ( (ret = pkcs7signed_test()) != 0)
  1454. TEST_FAIL("PKCS7signed test failed!\n", ret);
  1455. else
  1456. TEST_PASS("PKCS7signed test passed!\n");
  1457. if ( (ret = pkcs7enveloped_test()) != 0)
  1458. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  1459. else
  1460. TEST_PASS("PKCS7enveloped test passed!\n");
  1461. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1462. if ( (ret = pkcs7authenveloped_test()) != 0)
  1463. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  1464. else
  1465. TEST_PASS("PKCS7authenveloped test passed!\n");
  1466. #endif
  1467. #endif
  1468. #if defined(WOLFSSL_PUBLIC_MP) && \
  1469. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  1470. defined(USE_FAST_MATH))
  1471. if ( (ret = mp_test()) != 0)
  1472. TEST_FAIL("mp test failed!\n", ret);
  1473. else
  1474. TEST_PASS("mp test passed!\n");
  1475. #endif
  1476. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1477. if ( (ret = prime_test()) != 0)
  1478. TEST_FAIL("prime test failed!\n", ret);
  1479. else
  1480. TEST_PASS("prime test passed!\n");
  1481. #endif
  1482. #if defined(ASN_BER_TO_DER) && \
  1483. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1484. defined(OPENSSL_EXTRA_X509_SMALL))
  1485. if ( (ret = berder_test()) != 0)
  1486. TEST_FAIL("ber-der test failed!\n", ret);
  1487. else
  1488. TEST_PASS("ber-der test passed!\n");
  1489. #endif
  1490. if ( (ret = logging_test()) != 0)
  1491. TEST_FAIL("logging test failed!\n", ret);
  1492. else
  1493. TEST_PASS("logging test passed!\n");
  1494. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1495. if ( (ret = time_test()) != 0)
  1496. TEST_FAIL("time test failed!\n", ret);
  1497. else
  1498. TEST_PASS("time test passed!\n");
  1499. #endif
  1500. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  1501. if ((ret = wolfcrypt_mutex_test()) != 0)
  1502. #else
  1503. if ((ret = mutex_test()) != 0)
  1504. #endif
  1505. TEST_FAIL("mutex test failed!\n", ret);
  1506. else
  1507. TEST_PASS("mutex test passed!\n");
  1508. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1509. if ( (ret = memcb_test()) != 0)
  1510. TEST_FAIL("memcb test failed!\n", ret);
  1511. else
  1512. TEST_PASS("memcb test passed!\n");
  1513. #endif
  1514. #ifdef WOLFSSL_CAAM_BLOB
  1515. if ( (ret = blob_test()) != 0)
  1516. TEST_FAIL("blob test failed!\n", ret);
  1517. else
  1518. TEST_PASS("blob test passed!\n");
  1519. #endif
  1520. #if defined(WOLF_CRYPTO_CB) && \
  1521. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1522. defined(WOLFSSL_QNX_CAAM))
  1523. if ( (ret = cryptocb_test()) != 0)
  1524. TEST_FAIL("crypto callback test failed!\n", ret);
  1525. else
  1526. TEST_PASS("crypto callback test passed!\n");
  1527. #endif
  1528. #ifdef WOLFSSL_CERT_PIV
  1529. if ( (ret = certpiv_test()) != 0)
  1530. TEST_FAIL("cert piv test failed!\n", ret);
  1531. else
  1532. TEST_PASS("cert piv test passed!\n");
  1533. #endif
  1534. #ifdef WOLF_CRYPTO_CB
  1535. #ifdef HAVE_INTEL_QA_SYNC
  1536. wc_CryptoCb_CleanupIntelQa(&devId);
  1537. #endif
  1538. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1539. wc_CryptoCb_CleanupOcteon(&devId);
  1540. #endif
  1541. #endif
  1542. #ifdef WOLFSSL_ASYNC_CRYPT
  1543. wolfAsync_DevClose(&devId);
  1544. #endif
  1545. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1546. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1547. wc_ecc_fp_free();
  1548. #endif
  1549. #ifdef TEST_ALWAYS_RUN_TO_END
  1550. if (last_failed_test_ret != 0)
  1551. ret = last_failed_test_ret;
  1552. #endif
  1553. if (args)
  1554. ((func_args*)args)->return_code = ret;
  1555. TEST_PASS("Test complete\n");
  1556. EXIT_TEST(ret);
  1557. }
  1558. #ifndef NO_MAIN_DRIVER
  1559. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1560. int myoptind = 0;
  1561. char* myoptarg = NULL;
  1562. #endif
  1563. /* so overall tests can pull in test function */
  1564. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1565. wc_test_ret_t wolf_test_task(void)
  1566. #else
  1567. #ifndef NO_MAIN_FUNCTION
  1568. int main(int argc, char** argv)
  1569. {
  1570. return (int)wolfcrypt_test_main(argc, argv);
  1571. }
  1572. #endif
  1573. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  1574. #endif
  1575. {
  1576. wc_test_ret_t ret;
  1577. func_args args;
  1578. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1579. /* set dummy wallclock time. */
  1580. struct timeval utctime;
  1581. struct timezone tz;
  1582. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1583. utctime.tv_usec = 0;
  1584. tz.tz_minuteswest = 0;
  1585. tz.tz_dsttime = 0;
  1586. settimeofday(&utctime, &tz);
  1587. #endif
  1588. #ifdef WOLFSSL_APACHE_MYNEWT
  1589. #ifdef ARCH_sim
  1590. mcu_sim_parse_args(argc, argv);
  1591. #endif
  1592. sysinit();
  1593. /* set dummy wallclock time. */
  1594. struct os_timeval utctime;
  1595. struct os_timezone tz;
  1596. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1597. utctime.tv_usec = 0;
  1598. tz.tz_minuteswest = 0;
  1599. tz.tz_dsttime = 0;
  1600. os_settimeofday(&utctime, &tz);
  1601. #endif
  1602. #ifdef WOLFSSL_ZEPHYR
  1603. /* set dummy wallclock time. */
  1604. struct timespec utctime;
  1605. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1606. utctime.tv_nsec = 0;
  1607. clock_settime(CLOCK_REALTIME, &utctime);
  1608. #endif
  1609. #ifdef DEVKITPRO
  1610. void *framebuffer;
  1611. GXRModeObj *rmode = NULL;
  1612. VIDEO_Init();
  1613. WPAD_Init();
  1614. rmode = VIDEO_GetPreferredMode(NULL);
  1615. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1616. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1617. #pragma GCC diagnostic pop
  1618. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1619. VIDEO_Configure(rmode);
  1620. VIDEO_SetNextFramebuffer(framebuffer);
  1621. VIDEO_SetBlack(FALSE);
  1622. VIDEO_Flush();
  1623. VIDEO_WaitVSync();
  1624. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1625. #endif
  1626. #ifdef HAVE_WNR
  1627. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  1628. err_sys("Whitewood netRandom global config failed",
  1629. WC_TEST_RET_ENC_EC(ret));
  1630. return WC_TEST_RET_ENC_EC(ret);
  1631. }
  1632. #endif
  1633. #ifndef WOLFSSL_ESPIDF
  1634. args.argc = argc;
  1635. args.argv = argv;
  1636. #endif
  1637. if ((ret = wolfCrypt_Init()) != 0) {
  1638. printf("wolfCrypt_Init failed %d\n", (int)ret);
  1639. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  1640. }
  1641. #ifdef HAVE_WC_INTROSPECTION
  1642. printf("Math: %s\n", wc_GetMathInfo());
  1643. #endif
  1644. #ifdef WC_RNG_SEED_CB
  1645. wc_SetSeed_Cb(wc_GenerateSeed);
  1646. #endif
  1647. #ifdef HAVE_STACK_SIZE
  1648. StackSizeCheck(&args, wolfcrypt_test);
  1649. #else
  1650. wolfcrypt_test(&args);
  1651. #endif
  1652. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1653. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  1654. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  1655. }
  1656. #ifdef HAVE_WNR
  1657. if ((ret = wc_FreeNetRandom()) < 0)
  1658. err_sys("Failed to free netRandom context",
  1659. WC_TEST_RET_ENC_EC(ret));
  1660. #endif /* HAVE_WNR */
  1661. #ifdef DOLPHIN_EMULATOR
  1662. /* Returning from main panics the emulator. Just hang
  1663. * and let the user force quit the emulator window. */
  1664. printf("args.return_code: %d\n", args.return_code);
  1665. printf("Testing complete. You may close the window now\n");
  1666. while (1);
  1667. #endif
  1668. #ifdef WOLFSSL_ESPIDF
  1669. /* ESP_LOGI to print takes up a lot less memory than printf */
  1670. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1671. #endif
  1672. /* everything else will use printf */
  1673. #if !defined(WOLFSSL_ESPIDF)
  1674. /* gate this for target platforms wishing to avoid printf reference */
  1675. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  1676. #endif
  1677. return args.return_code;
  1678. } /* wolfcrypt_test_main or wolf_test_task */
  1679. #endif /* NO_MAIN_DRIVER */
  1680. /* helper to save DER, convert to PEM and save PEM */
  1681. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1682. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1683. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1684. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1685. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  1686. #else
  1687. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  1688. #endif
  1689. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  1690. const char* fileDer, const char* filePem, int pemType, int calling_line)
  1691. {
  1692. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1693. wc_test_ret_t ret;
  1694. XFILE derFile;
  1695. derFile = XFOPEN(fileDer, "wb");
  1696. if (!derFile) {
  1697. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  1698. }
  1699. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1700. XFCLOSE(derFile);
  1701. if (ret != derSz) {
  1702. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  1703. }
  1704. #endif
  1705. #ifdef WOLFSSL_DER_TO_PEM
  1706. if (filePem) {
  1707. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1708. XFILE pemFile;
  1709. #endif
  1710. byte* pem;
  1711. int pemSz;
  1712. /* calculate PEM size */
  1713. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1714. if (pemSz < 0) {
  1715. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  1716. }
  1717. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1718. if (pem == NULL) {
  1719. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  1720. }
  1721. /* Convert to PEM */
  1722. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1723. if (pemSz < 0) {
  1724. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1725. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  1726. }
  1727. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1728. pemFile = XFOPEN(filePem, "wb");
  1729. if (!pemFile) {
  1730. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1731. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  1732. }
  1733. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1734. XFCLOSE(pemFile);
  1735. if (ret != pemSz) {
  1736. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1737. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  1738. }
  1739. #endif
  1740. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1741. }
  1742. #endif /* WOLFSSL_DER_TO_PEM */
  1743. /* suppress unused variable warnings */
  1744. (void)der;
  1745. (void)derSz;
  1746. (void)filePem;
  1747. (void)fileDer;
  1748. (void)pemType;
  1749. (void)calling_line;
  1750. return 0;
  1751. }
  1752. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1753. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  1754. {
  1755. const char* errStr;
  1756. char out[WOLFSSL_MAX_ERROR_SZ];
  1757. const char* unknownStr = wc_GetErrorString(0);
  1758. #ifdef NO_ERROR_STRINGS
  1759. /* Ensure a valid error code's string matches an invalid code's.
  1760. * The string is that error strings are not available.
  1761. */
  1762. errStr = wc_GetErrorString(OPEN_RAN_E);
  1763. wc_ErrorString(OPEN_RAN_E, out);
  1764. if (XSTRCMP(errStr, unknownStr) != 0)
  1765. return WC_TEST_RET_ENC_NC;
  1766. if (XSTRCMP(out, unknownStr) != 0)
  1767. return WC_TEST_RET_ENC_NC;
  1768. #else
  1769. int i;
  1770. int j = 0;
  1771. /* Values that are not or no longer error codes. */
  1772. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1773. -163, -164, -165, -166, -167, -168, -169, -233,
  1774. 0 };
  1775. /* Check that all errors have a string and it's the same through the two
  1776. * APIs. Check that the values that are not errors map to the unknown
  1777. * string.
  1778. */
  1779. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1780. errStr = wc_GetErrorString(i);
  1781. wc_ErrorString(i, out);
  1782. if (i != missing[j]) {
  1783. if (XSTRCMP(errStr, unknownStr) == 0)
  1784. return WC_TEST_RET_ENC_NC;
  1785. if (XSTRCMP(out, unknownStr) == 0)
  1786. return WC_TEST_RET_ENC_NC;
  1787. if (XSTRCMP(errStr, out) != 0)
  1788. return WC_TEST_RET_ENC_NC;
  1789. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1790. return WC_TEST_RET_ENC_NC;
  1791. }
  1792. else {
  1793. j++;
  1794. if (XSTRCMP(errStr, unknownStr) != 0)
  1795. return WC_TEST_RET_ENC_NC;
  1796. if (XSTRCMP(out, unknownStr) != 0)
  1797. return WC_TEST_RET_ENC_NC;
  1798. }
  1799. }
  1800. /* Check if the next possible value has been given a string. */
  1801. errStr = wc_GetErrorString(i);
  1802. wc_ErrorString(i, out);
  1803. if (XSTRCMP(errStr, unknownStr) != 0)
  1804. return WC_TEST_RET_ENC_NC;
  1805. if (XSTRCMP(out, unknownStr) != 0)
  1806. return WC_TEST_RET_ENC_NC;
  1807. #endif
  1808. return 0;
  1809. }
  1810. #ifndef NO_CODING
  1811. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  1812. {
  1813. wc_test_ret_t ret;
  1814. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1815. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1816. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1817. byte out[128];
  1818. word32 outLen;
  1819. #ifdef WOLFSSL_BASE64_ENCODE
  1820. byte data[3];
  1821. word32 dataLen;
  1822. byte longData[79] = { 0 };
  1823. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1824. #endif
  1825. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1826. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1827. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1828. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1829. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1830. byte goodChar[] =
  1831. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1832. "abcdefghijklmnopqrstuvwxyz"
  1833. "0123456789+/;";
  1834. byte charTest[] = "A+Gd\0\0\0";
  1835. int i;
  1836. /* Good Base64 encodings. */
  1837. outLen = sizeof(out);
  1838. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1839. if (ret != 0)
  1840. return WC_TEST_RET_ENC_EC(ret);
  1841. outLen = sizeof(out);
  1842. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1843. if (ret != 0)
  1844. return WC_TEST_RET_ENC_EC(ret);
  1845. outLen = sizeof(goodChar);
  1846. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1847. if (ret != 0)
  1848. return WC_TEST_RET_ENC_EC(ret);
  1849. if (outLen != 64 / 4 * 3)
  1850. return WC_TEST_RET_ENC_NC;
  1851. outLen = sizeof(out);
  1852. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1853. if (ret != 0)
  1854. return WC_TEST_RET_ENC_EC(ret);
  1855. /* Bad parameters. */
  1856. outLen = 1;
  1857. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1858. if (ret != BAD_FUNC_ARG)
  1859. return WC_TEST_RET_ENC_EC(ret);
  1860. outLen = sizeof(out);
  1861. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1862. if (ret != ASN_INPUT_E)
  1863. return WC_TEST_RET_ENC_EC(ret);
  1864. outLen = sizeof(out);
  1865. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1866. if (ret != ASN_INPUT_E)
  1867. return WC_TEST_RET_ENC_EC(ret);
  1868. /* Bad character at each offset 0-3. */
  1869. for (i = 0; i < 4; i++) {
  1870. outLen = sizeof(out);
  1871. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1872. if (ret != ASN_INPUT_E)
  1873. return WC_TEST_RET_ENC_I(i);
  1874. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1875. if (ret != ASN_INPUT_E)
  1876. return WC_TEST_RET_ENC_I(i);
  1877. }
  1878. /* Invalid character less than 0x2b */
  1879. for (i = 1; i < 0x2b; i++) {
  1880. outLen = sizeof(out);
  1881. charTest[0] = (byte)i;
  1882. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1883. if (ret != ASN_INPUT_E)
  1884. return WC_TEST_RET_ENC_I(i);
  1885. }
  1886. /* Bad characters in range 0x2b - 0x7a. */
  1887. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1888. outLen = sizeof(out);
  1889. charTest[0] = badChar[i];
  1890. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1891. if (ret != ASN_INPUT_E)
  1892. return WC_TEST_RET_ENC_I(i);
  1893. }
  1894. /* Invalid character greater than 0x7a */
  1895. for (i = 0x7b; i < 0x100; i++) {
  1896. outLen = sizeof(out);
  1897. charTest[0] = (byte)i;
  1898. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1899. if (ret != ASN_INPUT_E)
  1900. return WC_TEST_RET_ENC_I(i);
  1901. }
  1902. #ifdef WOLFSSL_BASE64_ENCODE
  1903. /* Decode and encode all symbols - non-alphanumeric. */
  1904. dataLen = sizeof(data);
  1905. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1906. if (ret != 0)
  1907. return WC_TEST_RET_ENC_EC(ret);
  1908. outLen = sizeof(out);
  1909. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1910. if (ret != LENGTH_ONLY_E)
  1911. return WC_TEST_RET_ENC_EC(ret);
  1912. outLen = sizeof(out);
  1913. ret = Base64_Encode(data, dataLen, out, &outLen);
  1914. if (ret != 0)
  1915. return WC_TEST_RET_ENC_EC(ret);
  1916. outLen = 7;
  1917. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1918. if (ret != BUFFER_E)
  1919. return WC_TEST_RET_ENC_EC(ret);
  1920. outLen = sizeof(out);
  1921. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1922. if (ret != LENGTH_ONLY_E)
  1923. return WC_TEST_RET_ENC_EC(ret);
  1924. outLen = sizeof(out);
  1925. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1926. if (ret != 0)
  1927. return WC_TEST_RET_ENC_EC(ret);
  1928. outLen = sizeof(out);
  1929. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1930. if (ret != 0)
  1931. return WC_TEST_RET_ENC_EC(ret);
  1932. /* Data that results in an encoding longer than one line. */
  1933. outLen = sizeof(out);
  1934. dataLen = sizeof(longData);
  1935. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1936. if (ret != 0)
  1937. return WC_TEST_RET_ENC_EC(ret);
  1938. outLen = sizeof(out);
  1939. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1940. if (ret != 0)
  1941. return WC_TEST_RET_ENC_EC(ret);
  1942. outLen = sizeof(out);
  1943. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1944. if (ret != 0)
  1945. return WC_TEST_RET_ENC_EC(ret);
  1946. #endif
  1947. return 0;
  1948. }
  1949. #ifdef WOLFSSL_BASE16
  1950. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  1951. {
  1952. wc_test_ret_t ret;
  1953. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1954. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1955. byte encoded[40];
  1956. word32 encodedLen;
  1957. byte plain[40];
  1958. word32 len;
  1959. /* length returned includes null termination */
  1960. encodedLen = sizeof(encoded);
  1961. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1962. if (ret != 0)
  1963. return WC_TEST_RET_ENC_EC(ret);
  1964. len = (word32)XSTRLEN((char*)encoded);
  1965. if (len != encodedLen - 1)
  1966. return WC_TEST_RET_ENC_NC;
  1967. len = sizeof(plain);
  1968. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1969. if (ret != 0)
  1970. return WC_TEST_RET_ENC_EC(ret);
  1971. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1972. return WC_TEST_RET_ENC_NC;
  1973. if (encodedLen != sizeof(encodedTestData) ||
  1974. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1975. return WC_TEST_RET_ENC_NC;
  1976. }
  1977. return 0;
  1978. }
  1979. #endif /* WOLFSSL_BASE16 */
  1980. #endif /* !NO_CODING */
  1981. #ifndef NO_ASN
  1982. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  1983. {
  1984. wc_test_ret_t ret;
  1985. /* ASN1 encoded date buffer */
  1986. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1987. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1988. byte format;
  1989. int length;
  1990. const byte* datePart;
  1991. #ifndef NO_ASN_TIME
  1992. struct tm timearg;
  1993. time_t now;
  1994. #endif
  1995. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1996. &length);
  1997. if (ret != 0)
  1998. return WC_TEST_RET_ENC_EC(ret);
  1999. #ifndef NO_ASN_TIME
  2000. /* Parameter Validation tests. */
  2001. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2002. return WC_TEST_RET_ENC_EC(ret);
  2003. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2004. return WC_TEST_RET_ENC_EC(ret);
  2005. now = 0;
  2006. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2007. return WC_TEST_RET_ENC_EC(ret);
  2008. }
  2009. if (now == 0) {
  2010. printf("RTC/Time not set!\n");
  2011. return WC_TEST_RET_ENC_NC;
  2012. }
  2013. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2014. if (ret != 0)
  2015. return WC_TEST_RET_ENC_EC(ret);
  2016. #endif /* !NO_ASN_TIME */
  2017. return 0;
  2018. }
  2019. #endif /* !NO_ASN */
  2020. #ifdef WOLFSSL_MD2
  2021. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2022. {
  2023. wc_test_ret_t ret = 0;
  2024. Md2 md2;
  2025. byte hash[MD2_DIGEST_SIZE];
  2026. testVector a, b, c, d, e, f, g;
  2027. testVector test_md2[7];
  2028. int times = sizeof(test_md2) / sizeof(testVector), i;
  2029. a.input = "";
  2030. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2031. "\x27\x73";
  2032. a.inLen = XSTRLEN(a.input);
  2033. a.outLen = MD2_DIGEST_SIZE;
  2034. b.input = "a";
  2035. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2036. "\xb5\xd1";
  2037. b.inLen = XSTRLEN(b.input);
  2038. b.outLen = MD2_DIGEST_SIZE;
  2039. c.input = "abc";
  2040. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2041. "\xd6\xbb";
  2042. c.inLen = XSTRLEN(c.input);
  2043. c.outLen = MD2_DIGEST_SIZE;
  2044. d.input = "message digest";
  2045. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2046. "\x06\xb0";
  2047. d.inLen = XSTRLEN(d.input);
  2048. d.outLen = MD2_DIGEST_SIZE;
  2049. e.input = "abcdefghijklmnopqrstuvwxyz";
  2050. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2051. "\x94\x0b";
  2052. e.inLen = XSTRLEN(e.input);
  2053. e.outLen = MD2_DIGEST_SIZE;
  2054. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2055. "6789";
  2056. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2057. "\x38\xcd";
  2058. f.inLen = XSTRLEN(f.input);
  2059. f.outLen = MD2_DIGEST_SIZE;
  2060. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2061. "9012345678901234567890";
  2062. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2063. "\xef\xd8";
  2064. g.inLen = XSTRLEN(g.input);
  2065. g.outLen = MD2_DIGEST_SIZE;
  2066. test_md2[0] = a;
  2067. test_md2[1] = b;
  2068. test_md2[2] = c;
  2069. test_md2[3] = d;
  2070. test_md2[4] = e;
  2071. test_md2[5] = f;
  2072. test_md2[6] = g;
  2073. wc_InitMd2(&md2);
  2074. for (i = 0; i < times; ++i) {
  2075. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2076. wc_Md2Final(&md2, hash);
  2077. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2078. return WC_TEST_RET_ENC_I(i);
  2079. }
  2080. for (i = 0; i < times; ++i) {
  2081. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2082. if (ret != 0) {
  2083. return WC_TEST_RET_ENC_I(i);
  2084. }
  2085. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2086. return WC_TEST_RET_ENC_I(i);
  2087. }
  2088. }
  2089. return 0;
  2090. }
  2091. #endif
  2092. #ifndef NO_MD5
  2093. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2094. {
  2095. wc_test_ret_t ret = 0;
  2096. wc_Md5 md5, md5Copy;
  2097. byte hash[WC_MD5_DIGEST_SIZE];
  2098. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2099. testVector a, b, c, d, e, f;
  2100. testVector test_md5[6];
  2101. int times = sizeof(test_md5) / sizeof(testVector), i;
  2102. a.input = "";
  2103. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2104. "\x7e";
  2105. a.inLen = XSTRLEN(a.input);
  2106. a.outLen = WC_MD5_DIGEST_SIZE;
  2107. b.input = "abc";
  2108. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2109. "\x72";
  2110. b.inLen = XSTRLEN(b.input);
  2111. b.outLen = WC_MD5_DIGEST_SIZE;
  2112. c.input = "message digest";
  2113. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2114. "\xd0";
  2115. c.inLen = XSTRLEN(c.input);
  2116. c.outLen = WC_MD5_DIGEST_SIZE;
  2117. d.input = "abcdefghijklmnopqrstuvwxyz";
  2118. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2119. "\x3b";
  2120. d.inLen = XSTRLEN(d.input);
  2121. d.outLen = WC_MD5_DIGEST_SIZE;
  2122. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2123. "6789";
  2124. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2125. "\x9f";
  2126. e.inLen = XSTRLEN(e.input);
  2127. e.outLen = WC_MD5_DIGEST_SIZE;
  2128. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2129. "9012345678901234567890";
  2130. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2131. "\x7a";
  2132. f.inLen = XSTRLEN(f.input);
  2133. f.outLen = WC_MD5_DIGEST_SIZE;
  2134. test_md5[0] = a;
  2135. test_md5[1] = b;
  2136. test_md5[2] = c;
  2137. test_md5[3] = d;
  2138. test_md5[4] = e;
  2139. test_md5[5] = f;
  2140. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2141. if (ret != 0)
  2142. return WC_TEST_RET_ENC_EC(ret);
  2143. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2144. if (ret != 0) {
  2145. wc_Md5Free(&md5);
  2146. return WC_TEST_RET_ENC_EC(ret);
  2147. }
  2148. for (i = 0; i < times; ++i) {
  2149. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2150. (word32)test_md5[i].inLen);
  2151. if (ret != 0)
  2152. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2153. ret = wc_Md5GetHash(&md5, hashcopy);
  2154. if (ret != 0)
  2155. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2156. ret = wc_Md5Copy(&md5, &md5Copy);
  2157. if (ret != 0)
  2158. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2159. ret = wc_Md5Final(&md5, hash);
  2160. if (ret != 0)
  2161. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2162. wc_Md5Free(&md5Copy);
  2163. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2164. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2165. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2166. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2167. }
  2168. #ifndef NO_LARGE_HASH_TEST
  2169. /* BEGIN LARGE HASH TEST */ {
  2170. byte large_input[1024];
  2171. const char* large_digest =
  2172. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2173. for (i = 0; i < (int)sizeof(large_input); i++) {
  2174. large_input[i] = (byte)(i & 0xFF);
  2175. }
  2176. times = 100;
  2177. #ifdef WOLFSSL_PIC32MZ_HASH
  2178. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2179. #endif
  2180. for (i = 0; i < times; ++i) {
  2181. ret = wc_Md5Update(&md5, (byte*)large_input,
  2182. (word32)sizeof(large_input));
  2183. if (ret != 0)
  2184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2185. }
  2186. ret = wc_Md5Final(&md5, hash);
  2187. if (ret != 0)
  2188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2189. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2190. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2191. } /* END LARGE HASH TEST */
  2192. #endif /* NO_LARGE_HASH_TEST */
  2193. exit:
  2194. wc_Md5Free(&md5);
  2195. wc_Md5Free(&md5Copy);
  2196. return ret;
  2197. }
  2198. #endif /* NO_MD5 */
  2199. #ifndef NO_MD4
  2200. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2201. {
  2202. Md4 md4;
  2203. byte hash[MD4_DIGEST_SIZE];
  2204. testVector a, b, c, d, e, f, g;
  2205. testVector test_md4[7];
  2206. int times = sizeof(test_md4) / sizeof(testVector), i;
  2207. a.input = "";
  2208. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2209. "\xc0";
  2210. a.inLen = XSTRLEN(a.input);
  2211. a.outLen = MD4_DIGEST_SIZE;
  2212. b.input = "a";
  2213. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2214. "\x24";
  2215. b.inLen = XSTRLEN(b.input);
  2216. b.outLen = MD4_DIGEST_SIZE;
  2217. c.input = "abc";
  2218. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2219. "\x9d";
  2220. c.inLen = XSTRLEN(c.input);
  2221. c.outLen = MD4_DIGEST_SIZE;
  2222. d.input = "message digest";
  2223. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2224. "\x4b";
  2225. d.inLen = XSTRLEN(d.input);
  2226. d.outLen = MD4_DIGEST_SIZE;
  2227. e.input = "abcdefghijklmnopqrstuvwxyz";
  2228. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2229. "\xa9";
  2230. e.inLen = XSTRLEN(e.input);
  2231. e.outLen = MD4_DIGEST_SIZE;
  2232. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2233. "6789";
  2234. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2235. "\xe4";
  2236. f.inLen = XSTRLEN(f.input);
  2237. f.outLen = MD4_DIGEST_SIZE;
  2238. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2239. "9012345678901234567890";
  2240. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2241. "\x36";
  2242. g.inLen = XSTRLEN(g.input);
  2243. g.outLen = MD4_DIGEST_SIZE;
  2244. test_md4[0] = a;
  2245. test_md4[1] = b;
  2246. test_md4[2] = c;
  2247. test_md4[3] = d;
  2248. test_md4[4] = e;
  2249. test_md4[5] = f;
  2250. test_md4[6] = g;
  2251. wc_InitMd4(&md4);
  2252. for (i = 0; i < times; ++i) {
  2253. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2254. wc_Md4Final(&md4, hash);
  2255. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2256. return WC_TEST_RET_ENC_I(i);
  2257. }
  2258. return 0;
  2259. }
  2260. #endif /* NO_MD4 */
  2261. #ifndef NO_SHA
  2262. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2263. {
  2264. wc_test_ret_t ret = 0;
  2265. wc_Sha sha, shaCopy;
  2266. byte hash[WC_SHA_DIGEST_SIZE];
  2267. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2268. testVector a, b, c, d, e;
  2269. testVector test_sha[5];
  2270. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2271. a.input = "";
  2272. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2273. "\x90\xaf\xd8\x07\x09";
  2274. a.inLen = XSTRLEN(a.input);
  2275. a.outLen = WC_SHA_DIGEST_SIZE;
  2276. b.input = "abc";
  2277. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2278. "\x6C\x9C\xD0\xD8\x9D";
  2279. b.inLen = XSTRLEN(b.input);
  2280. b.outLen = WC_SHA_DIGEST_SIZE;
  2281. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2282. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2283. "\xE5\xE5\x46\x70\xF1";
  2284. c.inLen = XSTRLEN(c.input);
  2285. c.outLen = WC_SHA_DIGEST_SIZE;
  2286. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2287. "aaaaaa";
  2288. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2289. "\x2A\x25\xEC\x64\x4D";
  2290. d.inLen = XSTRLEN(d.input);
  2291. d.outLen = WC_SHA_DIGEST_SIZE;
  2292. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2293. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2294. "aaaaaaaaaa";
  2295. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2296. "\x53\x99\x5E\x26\xA0";
  2297. e.inLen = XSTRLEN(e.input);
  2298. e.outLen = WC_SHA_DIGEST_SIZE;
  2299. test_sha[0] = a;
  2300. test_sha[1] = b;
  2301. test_sha[2] = c;
  2302. test_sha[3] = d;
  2303. test_sha[4] = e;
  2304. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2305. if (ret != 0)
  2306. return WC_TEST_RET_ENC_EC(ret);
  2307. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2308. if (ret != 0) {
  2309. wc_ShaFree(&sha);
  2310. return WC_TEST_RET_ENC_EC(ret);
  2311. }
  2312. for (i = 0; i < times; ++i) {
  2313. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2314. (word32)test_sha[i].inLen);
  2315. if (ret != 0)
  2316. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2317. ret = wc_ShaGetHash(&sha, hashcopy);
  2318. if (ret != 0)
  2319. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2320. ret = wc_ShaCopy(&sha, &shaCopy);
  2321. if (ret != 0)
  2322. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2323. ret = wc_ShaFinal(&sha, hash);
  2324. if (ret != 0)
  2325. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2326. wc_ShaFree(&shaCopy);
  2327. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2328. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2329. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2330. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2331. }
  2332. #ifndef NO_LARGE_HASH_TEST
  2333. /* BEGIN LARGE HASH TEST */ {
  2334. byte large_input[1024];
  2335. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2336. defined(HASH_SIZE_LIMIT)
  2337. const char* large_digest =
  2338. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2339. "\x15\x8c\x6d\xb6";
  2340. #else
  2341. const char* large_digest =
  2342. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2343. "\xc5\xd9\x0a\xac";
  2344. #endif
  2345. for (i = 0; i < (int)sizeof(large_input); i++) {
  2346. large_input[i] = (byte)(i & 0xFF);
  2347. }
  2348. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2349. defined(HASH_SIZE_LIMIT)
  2350. times = 20;
  2351. #else
  2352. times = 100;
  2353. #endif
  2354. #ifdef WOLFSSL_PIC32MZ_HASH
  2355. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2356. #endif
  2357. for (i = 0; i < times; ++i) {
  2358. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2359. (word32)sizeof(large_input));
  2360. if (ret != 0)
  2361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2362. }
  2363. ret = wc_ShaFinal(&sha, hash);
  2364. if (ret != 0)
  2365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2366. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2367. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2368. } /* END LARGE HASH TEST */
  2369. #endif /* NO_LARGE_HASH_TEST */
  2370. exit:
  2371. wc_ShaFree(&sha);
  2372. wc_ShaFree(&shaCopy);
  2373. return ret;
  2374. }
  2375. #endif /* NO_SHA */
  2376. #ifdef WOLFSSL_RIPEMD
  2377. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  2378. {
  2379. RipeMd ripemd;
  2380. wc_test_ret_t ret;
  2381. byte hash[RIPEMD_DIGEST_SIZE];
  2382. testVector a, b, c, d;
  2383. testVector test_ripemd[4];
  2384. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2385. a.input = "abc";
  2386. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2387. "\xb0\x87\xf1\x5a\x0b\xfc";
  2388. a.inLen = XSTRLEN(a.input);
  2389. a.outLen = RIPEMD_DIGEST_SIZE;
  2390. b.input = "message digest";
  2391. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2392. "\x5f\xfa\x21\x59\x5f\x36";
  2393. b.inLen = XSTRLEN(b.input);
  2394. b.outLen = RIPEMD_DIGEST_SIZE;
  2395. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2396. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2397. "\xf4\x9a\xda\x62\xeb\x2b";
  2398. c.inLen = XSTRLEN(c.input);
  2399. c.outLen = RIPEMD_DIGEST_SIZE;
  2400. d.input = "12345678901234567890123456789012345678901234567890123456"
  2401. "789012345678901234567890";
  2402. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2403. "\x82\xbf\x63\x32\x6b\xfb";
  2404. d.inLen = XSTRLEN(d.input);
  2405. d.outLen = RIPEMD_DIGEST_SIZE;
  2406. test_ripemd[0] = a;
  2407. test_ripemd[1] = b;
  2408. test_ripemd[2] = c;
  2409. test_ripemd[3] = d;
  2410. ret = wc_InitRipeMd(&ripemd);
  2411. if (ret != 0) {
  2412. return WC_TEST_RET_ENC_EC(ret);
  2413. }
  2414. for (i = 0; i < times; ++i) {
  2415. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2416. (word32)test_ripemd[i].inLen);
  2417. if (ret != 0) {
  2418. return WC_TEST_RET_ENC_I(i);
  2419. }
  2420. ret = wc_RipeMdFinal(&ripemd, hash);
  2421. if (ret != 0) {
  2422. return WC_TEST_RET_ENC_I(i);
  2423. }
  2424. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2425. return WC_TEST_RET_ENC_I(i);
  2426. }
  2427. return 0;
  2428. }
  2429. #endif /* WOLFSSL_RIPEMD */
  2430. #ifdef HAVE_BLAKE2
  2431. #define BLAKE2B_TESTS 3
  2432. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2433. {
  2434. {
  2435. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2436. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2437. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2438. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2439. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2440. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2441. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2442. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2443. },
  2444. {
  2445. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2446. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2447. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2448. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2449. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2450. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2451. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2452. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2453. },
  2454. {
  2455. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2456. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2457. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2458. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2459. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2460. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2461. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2462. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2463. }
  2464. };
  2465. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  2466. {
  2467. Blake2b b2b;
  2468. byte digest[64];
  2469. byte input[64];
  2470. int i, ret;
  2471. for (i = 0; i < (int)sizeof(input); i++)
  2472. input[i] = (byte)i;
  2473. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2474. ret = wc_InitBlake2b(&b2b, 64);
  2475. if (ret != 0)
  2476. return WC_TEST_RET_ENC_I(i);
  2477. ret = wc_Blake2bUpdate(&b2b, input, i);
  2478. if (ret != 0)
  2479. return WC_TEST_RET_ENC_I(i);
  2480. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2481. if (ret != 0)
  2482. return WC_TEST_RET_ENC_I(i);
  2483. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2484. return WC_TEST_RET_ENC_I(i);
  2485. }
  2486. }
  2487. return 0;
  2488. }
  2489. #endif /* HAVE_BLAKE2 */
  2490. #ifdef HAVE_BLAKE2S
  2491. #define BLAKE2S_TESTS 3
  2492. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2493. {
  2494. {
  2495. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2496. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2497. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2498. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2499. },
  2500. {
  2501. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2502. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2503. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2504. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2505. },
  2506. {
  2507. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2508. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2509. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2510. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2511. }
  2512. };
  2513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  2514. {
  2515. Blake2s b2s;
  2516. byte digest[32];
  2517. byte input[64];
  2518. int i, ret;
  2519. for (i = 0; i < (int)sizeof(input); i++)
  2520. input[i] = (byte)i;
  2521. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2522. ret = wc_InitBlake2s(&b2s, 32);
  2523. if (ret != 0)
  2524. return WC_TEST_RET_ENC_I(i);
  2525. ret = wc_Blake2sUpdate(&b2s, input, i);
  2526. if (ret != 0)
  2527. return WC_TEST_RET_ENC_I(i);
  2528. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2529. if (ret != 0)
  2530. return WC_TEST_RET_ENC_I(i);
  2531. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2532. return WC_TEST_RET_ENC_I(i);
  2533. }
  2534. }
  2535. return 0;
  2536. }
  2537. #endif /* HAVE_BLAKE2S */
  2538. #ifdef WOLFSSL_SHA224
  2539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  2540. {
  2541. wc_Sha224 sha, shaCopy;
  2542. byte hash[WC_SHA224_DIGEST_SIZE];
  2543. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2544. wc_test_ret_t ret = 0;
  2545. testVector a, b, c;
  2546. testVector test_sha[3];
  2547. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2548. a.input = "";
  2549. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2550. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2551. a.inLen = XSTRLEN(a.input);
  2552. a.outLen = WC_SHA224_DIGEST_SIZE;
  2553. b.input = "abc";
  2554. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2555. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2556. b.inLen = XSTRLEN(b.input);
  2557. b.outLen = WC_SHA224_DIGEST_SIZE;
  2558. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2559. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2560. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2561. c.inLen = XSTRLEN(c.input);
  2562. c.outLen = WC_SHA224_DIGEST_SIZE;
  2563. test_sha[0] = a;
  2564. test_sha[1] = b;
  2565. test_sha[2] = c;
  2566. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2567. if (ret != 0)
  2568. return WC_TEST_RET_ENC_EC(ret);
  2569. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2570. if (ret != 0) {
  2571. wc_Sha224Free(&sha);
  2572. return WC_TEST_RET_ENC_EC(ret);
  2573. }
  2574. for (i = 0; i < times; ++i) {
  2575. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2576. (word32)test_sha[i].inLen);
  2577. if (ret != 0)
  2578. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2579. ret = wc_Sha224GetHash(&sha, hashcopy);
  2580. if (ret != 0)
  2581. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2582. ret = wc_Sha224Copy(&sha, &shaCopy);
  2583. if (ret != 0)
  2584. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2585. ret = wc_Sha224Final(&sha, hash);
  2586. if (ret != 0)
  2587. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2588. wc_Sha224Free(&shaCopy);
  2589. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2590. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2591. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2592. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2593. }
  2594. exit:
  2595. wc_Sha224Free(&sha);
  2596. wc_Sha224Free(&shaCopy);
  2597. return ret;
  2598. }
  2599. #endif
  2600. #ifndef NO_SHA256
  2601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  2602. {
  2603. wc_Sha256 sha, shaCopy;
  2604. byte hash[WC_SHA256_DIGEST_SIZE];
  2605. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2606. wc_test_ret_t ret = 0;
  2607. testVector a, b, c;
  2608. testVector test_sha[3];
  2609. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2610. a.input = "";
  2611. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2612. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2613. "\xb8\x55";
  2614. a.inLen = XSTRLEN(a.input);
  2615. a.outLen = WC_SHA256_DIGEST_SIZE;
  2616. b.input = "abc";
  2617. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2618. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2619. "\x15\xAD";
  2620. b.inLen = XSTRLEN(b.input);
  2621. b.outLen = WC_SHA256_DIGEST_SIZE;
  2622. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2623. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2624. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2625. "\x06\xC1";
  2626. c.inLen = XSTRLEN(c.input);
  2627. c.outLen = WC_SHA256_DIGEST_SIZE;
  2628. test_sha[0] = a;
  2629. test_sha[1] = b;
  2630. test_sha[2] = c;
  2631. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2632. if (ret != 0)
  2633. return WC_TEST_RET_ENC_EC(ret);
  2634. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2635. if (ret != 0) {
  2636. wc_Sha256Free(&sha);
  2637. return WC_TEST_RET_ENC_EC(ret);
  2638. }
  2639. for (i = 0; i < times; ++i) {
  2640. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2641. (word32)test_sha[i].inLen);
  2642. if (ret != 0) {
  2643. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2644. }
  2645. ret = wc_Sha256GetHash(&sha, hashcopy);
  2646. if (ret != 0)
  2647. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2648. ret = wc_Sha256Copy(&sha, &shaCopy);
  2649. if (ret != 0)
  2650. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2651. ret = wc_Sha256Final(&sha, hash);
  2652. if (ret != 0)
  2653. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2654. wc_Sha256Free(&shaCopy);
  2655. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2656. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2657. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2658. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2659. }
  2660. #ifndef NO_LARGE_HASH_TEST
  2661. /* BEGIN LARGE HASH TEST */ {
  2662. byte large_input[1024];
  2663. #ifdef HASH_SIZE_LIMIT
  2664. const char* large_digest =
  2665. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2666. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2667. #else
  2668. const char* large_digest =
  2669. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2670. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2671. #endif
  2672. for (i = 0; i < (int)sizeof(large_input); i++) {
  2673. large_input[i] = (byte)(i & 0xFF);
  2674. }
  2675. #ifdef HASH_SIZE_LIMIT
  2676. times = 20;
  2677. #else
  2678. times = 100;
  2679. #endif
  2680. #ifdef WOLFSSL_PIC32MZ_HASH
  2681. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2682. #endif
  2683. for (i = 0; i < times; ++i) {
  2684. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2685. (word32)sizeof(large_input));
  2686. if (ret != 0)
  2687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2688. }
  2689. ret = wc_Sha256Final(&sha, hash);
  2690. if (ret != 0)
  2691. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2692. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2693. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2694. } /* END LARGE HASH TEST */
  2695. #endif /* NO_LARGE_HASH_TEST */
  2696. exit:
  2697. wc_Sha256Free(&sha);
  2698. wc_Sha256Free(&shaCopy);
  2699. return ret;
  2700. }
  2701. #endif
  2702. #ifdef WOLFSSL_SHA512
  2703. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  2704. {
  2705. /*
  2706. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  2707. */
  2708. wc_Sha512 sha, shaCopy;
  2709. byte hash[WC_SHA512_DIGEST_SIZE];
  2710. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2711. wc_test_ret_t ret = 0;
  2712. testVector a, b, c;
  2713. testVector test_sha[3];
  2714. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2715. a.input = "";
  2716. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2717. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2718. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2719. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2720. "\xf9\x27\xda\x3e";
  2721. a.inLen = XSTRLEN(a.input);
  2722. a.outLen = WC_SHA512_DIGEST_SIZE;
  2723. b.input = "abc";
  2724. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2725. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2726. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2727. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2728. "\xa5\x4c\xa4\x9f";
  2729. b.inLen = XSTRLEN(b.input);
  2730. b.outLen = WC_SHA512_DIGEST_SIZE;
  2731. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2732. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2733. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2734. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2735. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2736. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2737. "\x87\x4b\xe9\x09";
  2738. c.inLen = XSTRLEN(c.input);
  2739. c.outLen = WC_SHA512_DIGEST_SIZE;
  2740. test_sha[0] = a;
  2741. test_sha[1] = b;
  2742. test_sha[2] = c;
  2743. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2744. if (ret != 0)
  2745. return WC_TEST_RET_ENC_EC(ret);
  2746. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2747. if (ret != 0) {
  2748. wc_Sha512Free(&sha);
  2749. return WC_TEST_RET_ENC_EC(ret);
  2750. }
  2751. for (i = 0; i < times; ++i) {
  2752. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2753. (word32)test_sha[i].inLen);
  2754. if (ret != 0)
  2755. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2756. ret = wc_Sha512GetHash(&sha, hashcopy);
  2757. if (ret != 0)
  2758. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2759. ret = wc_Sha512Copy(&sha, &shaCopy);
  2760. if (ret != 0)
  2761. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2762. ret = wc_Sha512Final(&sha, hash);
  2763. if (ret != 0)
  2764. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2765. wc_Sha512Free(&shaCopy);
  2766. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2767. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2768. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2769. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2770. }
  2771. #ifndef NO_LARGE_HASH_TEST
  2772. /* BEGIN LARGE HASH TEST */ {
  2773. byte large_input[1024];
  2774. #ifdef HASH_SIZE_LIMIT
  2775. const char* large_digest =
  2776. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2777. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2778. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2779. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2780. #else
  2781. const char* large_digest =
  2782. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2783. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2784. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2785. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2786. #endif
  2787. for (i = 0; i < (int)sizeof(large_input); i++) {
  2788. large_input[i] = (byte)(i & 0xFF);
  2789. }
  2790. #ifdef HASH_SIZE_LIMIT
  2791. times = 20;
  2792. #else
  2793. times = 100;
  2794. #endif
  2795. for (i = 0; i < times; ++i) {
  2796. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2797. (word32)sizeof(large_input));
  2798. if (ret != 0)
  2799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2800. }
  2801. ret = wc_Sha512Final(&sha, hash);
  2802. if (ret != 0)
  2803. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2804. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2805. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2806. #ifndef NO_UNALIGNED_MEMORY_TEST
  2807. /* Unaligned memory access test */
  2808. for (i = 1; i < 16; i++) {
  2809. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2810. (word32)sizeof(large_input) - i);
  2811. if (ret != 0)
  2812. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2813. ret = wc_Sha512Final(&sha, hash);
  2814. }
  2815. #endif
  2816. } /* END LARGE HASH TEST */
  2817. #endif /* NO_LARGE_HASH_TEST */
  2818. exit:
  2819. wc_Sha512Free(&sha);
  2820. wc_Sha512Free(&shaCopy);
  2821. return ret;
  2822. }
  2823. #if !defined(WOLFSSL_NOSHA512_224) && \
  2824. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2825. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  2826. {
  2827. /*
  2828. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  2829. **
  2830. ** NIST SHA512/224 Document Example:
  2831. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  2832. */
  2833. wc_Sha512 sha, shaCopy;
  2834. byte hash[WC_SHA512_224_DIGEST_SIZE];
  2835. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  2836. wc_test_ret_t ret = 0;
  2837. testVector a, b, c;
  2838. testVector test_sha[3];
  2839. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2840. a.input = "";
  2841. a.output = "\x6e\xd0\xdd\x02"
  2842. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  2843. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  2844. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  2845. a.inLen = XSTRLEN(a.input);
  2846. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  2847. /*
  2848. ** See page 1 in above document for the SHA512/224 "abc" test:
  2849. */
  2850. b.input = "abc";
  2851. /*
  2852. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  2853. */
  2854. b.output = "\x46\x34\x27\x0f"
  2855. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  2856. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  2857. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  2858. b.inLen = XSTRLEN(b.input);
  2859. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  2860. /*
  2861. ** See page 4 in above for the 2-block test:
  2862. */
  2863. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2864. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2865. /*
  2866. ** see page 9 in above document for the 2-block test message digest:
  2867. */
  2868. c.output = "\x23\xfe\xc5\xbb"
  2869. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  2870. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  2871. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  2872. c.inLen = XSTRLEN(c.input);
  2873. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  2874. test_sha[0] = a;
  2875. test_sha[1] = b;
  2876. test_sha[2] = c;
  2877. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  2878. if (ret != 0)
  2879. return WC_TEST_RET_ENC_EC(ret);
  2880. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  2881. if (ret != 0) {
  2882. wc_Sha512_224Free(&sha);
  2883. return WC_TEST_RET_ENC_EC(ret);
  2884. }
  2885. for (i = 0; i < times; ++i) {
  2886. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  2887. (word32)test_sha[i].inLen);
  2888. if (ret != 0)
  2889. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2890. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  2891. if (ret != 0)
  2892. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2893. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  2894. if (ret != 0)
  2895. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2896. ret = wc_Sha512_224Final(&sha, hash);
  2897. if (ret != 0)
  2898. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2899. wc_Sha512_224Free(&shaCopy);
  2900. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  2901. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2902. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  2903. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2904. }
  2905. #ifndef NO_LARGE_HASH_TEST
  2906. /* BEGIN LARGE HASH TEST */ {
  2907. byte large_input[1024];
  2908. #ifdef HASH_SIZE_LIMIT
  2909. const char* large_digest =
  2910. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  2911. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  2912. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  2913. "\x14\x6c\xcf\x6a";
  2914. #else
  2915. const char* large_digest =
  2916. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  2917. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  2918. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  2919. "\xe2\x4e\x85\x05";
  2920. #endif
  2921. for (i = 0; i < (int)sizeof(large_input); i++) {
  2922. large_input[i] = (byte)(i & 0xFF);
  2923. }
  2924. #ifdef HASH_SIZE_LIMIT
  2925. times = 20;
  2926. #else
  2927. times = 100;
  2928. #endif
  2929. for (i = 0; i < times; ++i) {
  2930. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  2931. (word32)sizeof(large_input));
  2932. if (ret != 0)
  2933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2934. }
  2935. ret = wc_Sha512_224Final(&sha, hash);
  2936. if (ret != 0)
  2937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2938. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  2939. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2940. #ifndef NO_UNALIGNED_MEMORY_TEST
  2941. /* Unaligned memory access test */
  2942. for (i = 1; i < 16; i++) {
  2943. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  2944. (word32)sizeof(large_input) - i);
  2945. if (ret != 0)
  2946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2947. ret = wc_Sha512_224Final(&sha, hash);
  2948. }
  2949. #endif
  2950. } /* END LARGE HASH TEST */
  2951. #endif /* NO_LARGE_HASH_TEST */
  2952. exit:
  2953. wc_Sha512_224Free(&sha);
  2954. wc_Sha512_224Free(&shaCopy);
  2955. return ret;
  2956. } /* sha512_224_test */
  2957. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  2958. #if !defined(WOLFSSL_NOSHA512_256) && \
  2959. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2960. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  2961. {
  2962. /*
  2963. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  2964. ** NIST SHA512/256 Document Example:
  2965. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  2966. */
  2967. wc_Sha512 sha, shaCopy;
  2968. byte hash[WC_SHA512_256_DIGEST_SIZE];
  2969. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  2970. wc_test_ret_t ret = 0;
  2971. testVector a, b, c;
  2972. testVector test_sha[3];
  2973. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2974. a.input = "";
  2975. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  2976. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  2977. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  2978. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  2979. a.inLen = XSTRLEN(a.input);
  2980. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  2981. /*
  2982. ** See page 1 of above document for "abc" example:
  2983. */
  2984. b.input = "abc";
  2985. /*
  2986. ** See page 4 of above document for "abc" example digest:
  2987. */
  2988. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  2989. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  2990. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  2991. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  2992. b.inLen = XSTRLEN(b.input);
  2993. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  2994. /*
  2995. ** See page 4 of above document for Two Block Message Sample:
  2996. */
  2997. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2998. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2999. /*
  3000. ** See page 10 of above document for Two Block Message Sample digest:
  3001. */
  3002. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3003. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3004. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3005. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3006. c.inLen = XSTRLEN(c.input);
  3007. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3008. test_sha[0] = a;
  3009. test_sha[1] = b;
  3010. test_sha[2] = c;
  3011. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3012. if (ret != 0)
  3013. return WC_TEST_RET_ENC_EC(ret);
  3014. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3015. if (ret != 0) {
  3016. wc_Sha512_256Free(&sha);
  3017. return WC_TEST_RET_ENC_EC(ret);
  3018. }
  3019. for (i = 0; i < times; ++i) {
  3020. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3021. (word32)test_sha[i].inLen);
  3022. if (ret != 0)
  3023. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3024. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3025. if (ret != 0)
  3026. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3027. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3028. if (ret != 0)
  3029. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3030. ret = wc_Sha512_256Final(&sha, hash);
  3031. if (ret != 0)
  3032. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3033. wc_Sha512_256Free(&shaCopy);
  3034. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3035. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3036. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3037. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3038. }
  3039. #ifndef NO_LARGE_HASH_TEST
  3040. /* BEGIN LARGE HASH TEST */ {
  3041. byte large_input[1024];
  3042. #ifdef HASH_SIZE_LIMIT
  3043. const char* large_digest =
  3044. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3045. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3046. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3047. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3048. #else
  3049. const char* large_digest =
  3050. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3051. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3052. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3053. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3054. #endif
  3055. for (i = 0; i < (int)sizeof(large_input); i++) {
  3056. large_input[i] = (byte)(i & 0xFF);
  3057. }
  3058. #ifdef HASH_SIZE_LIMIT
  3059. times = 20;
  3060. #else
  3061. times = 100;
  3062. #endif
  3063. for (i = 0; i < times; ++i) {
  3064. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3065. (word32)sizeof(large_input));
  3066. if (ret != 0)
  3067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3068. }
  3069. ret = wc_Sha512_256Final(&sha, hash);
  3070. if (ret != 0)
  3071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3072. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3073. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3074. #ifndef NO_UNALIGNED_MEMORY_TEST
  3075. /* Unaligned memory access test */
  3076. for (i = 1; i < 16; i++) {
  3077. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3078. (word32)sizeof(large_input) - i);
  3079. if (ret != 0)
  3080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3081. ret = wc_Sha512_256Final(&sha, hash);
  3082. }
  3083. #endif
  3084. } /* END LARGE HASH TEST */
  3085. #endif /* NO_LARGE_HASH_TEST */
  3086. exit:
  3087. wc_Sha512_256Free(&sha);
  3088. wc_Sha512_256Free(&shaCopy);
  3089. return ret;
  3090. } /* sha512_256_test */
  3091. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3092. #endif /* WOLFSSL_SHA512 */
  3093. #ifdef WOLFSSL_SHA384
  3094. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3095. {
  3096. wc_Sha384 sha, shaCopy;
  3097. byte hash[WC_SHA384_DIGEST_SIZE];
  3098. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3099. wc_test_ret_t ret = 0;
  3100. testVector a, b, c;
  3101. testVector test_sha[3];
  3102. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3103. a.input = "";
  3104. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3105. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3106. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3107. "\x98\xb9\x5b";
  3108. a.inLen = XSTRLEN(a.input);
  3109. a.outLen = WC_SHA384_DIGEST_SIZE;
  3110. b.input = "abc";
  3111. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3112. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3113. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3114. "\xc8\x25\xa7";
  3115. b.inLen = XSTRLEN(b.input);
  3116. b.outLen = WC_SHA384_DIGEST_SIZE;
  3117. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3118. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3119. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3120. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3121. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3122. "\x74\x60\x39";
  3123. c.inLen = XSTRLEN(c.input);
  3124. c.outLen = WC_SHA384_DIGEST_SIZE;
  3125. test_sha[0] = a;
  3126. test_sha[1] = b;
  3127. test_sha[2] = c;
  3128. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3129. if (ret != 0)
  3130. return WC_TEST_RET_ENC_EC(ret);
  3131. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3132. if (ret != 0) {
  3133. wc_Sha384Free(&sha);
  3134. return WC_TEST_RET_ENC_EC(ret);
  3135. }
  3136. for (i = 0; i < times; ++i) {
  3137. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3138. (word32)test_sha[i].inLen);
  3139. if (ret != 0)
  3140. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3141. ret = wc_Sha384GetHash(&sha, hashcopy);
  3142. if (ret != 0)
  3143. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3144. ret = wc_Sha384Copy(&sha, &shaCopy);
  3145. if (ret != 0)
  3146. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3147. ret = wc_Sha384Final(&sha, hash);
  3148. if (ret != 0)
  3149. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3150. wc_Sha384Free(&shaCopy);
  3151. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3152. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3153. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3154. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3155. }
  3156. #ifndef NO_LARGE_HASH_TEST
  3157. /* BEGIN LARGE HASH TEST */ {
  3158. byte large_input[1024];
  3159. #ifdef HASH_SIZE_LIMIT
  3160. const char* large_digest =
  3161. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3162. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3163. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  3164. #else
  3165. const char* large_digest =
  3166. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  3167. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  3168. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  3169. #endif
  3170. for (i = 0; i < (int)sizeof(large_input); i++) {
  3171. large_input[i] = (byte)(i & 0xFF);
  3172. }
  3173. #ifdef HASH_SIZE_LIMIT
  3174. times = 20;
  3175. #else
  3176. times = 100;
  3177. #endif
  3178. for (i = 0; i < times; ++i) {
  3179. ret = wc_Sha384Update(&sha, (byte*)large_input,
  3180. (word32)sizeof(large_input));
  3181. if (ret != 0)
  3182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3183. }
  3184. ret = wc_Sha384Final(&sha, hash);
  3185. if (ret != 0)
  3186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3187. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  3188. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3189. } /* END LARGE HASH TEST */
  3190. #endif /* NO_LARGE_HASH_TEST */
  3191. exit:
  3192. wc_Sha384Free(&sha);
  3193. wc_Sha384Free(&shaCopy);
  3194. return ret;
  3195. }
  3196. #endif /* WOLFSSL_SHA384 */
  3197. #ifdef WOLFSSL_SHA3
  3198. #ifndef WOLFSSL_NOSHA3_224
  3199. static wc_test_ret_t sha3_224_test(void)
  3200. {
  3201. wc_Sha3 sha;
  3202. byte hash[WC_SHA3_224_DIGEST_SIZE];
  3203. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  3204. testVector a, b, c;
  3205. testVector test_sha[3];
  3206. wc_test_ret_t ret = 0;
  3207. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3208. a.input = "";
  3209. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  3210. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  3211. a.inLen = XSTRLEN(a.input);
  3212. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  3213. b.input = "abc";
  3214. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  3215. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  3216. b.inLen = XSTRLEN(b.input);
  3217. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  3218. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3219. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  3220. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  3221. c.inLen = XSTRLEN(c.input);
  3222. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  3223. test_sha[0] = a;
  3224. test_sha[1] = b;
  3225. test_sha[2] = c;
  3226. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  3227. if (ret != 0)
  3228. return WC_TEST_RET_ENC_EC(ret);
  3229. for (i = 0; i < times; ++i) {
  3230. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  3231. (word32)test_sha[i].inLen);
  3232. if (ret != 0)
  3233. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3234. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  3235. if (ret != 0)
  3236. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3237. ret = wc_Sha3_224_Final(&sha, hash);
  3238. if (ret != 0)
  3239. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3240. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  3241. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3242. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  3243. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3244. }
  3245. #ifndef NO_LARGE_HASH_TEST
  3246. /* BEGIN LARGE HASH TEST */ {
  3247. byte large_input[1024];
  3248. const char* large_digest =
  3249. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  3250. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  3251. for (i = 0; i < (int)sizeof(large_input); i++) {
  3252. large_input[i] = (byte)(i & 0xFF);
  3253. }
  3254. times = 100;
  3255. for (i = 0; i < times; ++i) {
  3256. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  3257. (word32)sizeof(large_input));
  3258. if (ret != 0)
  3259. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3260. }
  3261. ret = wc_Sha3_224_Final(&sha, hash);
  3262. if (ret != 0)
  3263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3264. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  3265. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3266. } /* END LARGE HASH TEST */
  3267. #endif /* NO_LARGE_HASH_TEST */
  3268. exit:
  3269. wc_Sha3_224_Free(&sha);
  3270. return ret;
  3271. }
  3272. #endif /* WOLFSSL_NOSHA3_224 */
  3273. #ifndef WOLFSSL_NOSHA3_256
  3274. static wc_test_ret_t sha3_256_test(void)
  3275. {
  3276. wc_Sha3 sha;
  3277. byte hash[WC_SHA3_256_DIGEST_SIZE];
  3278. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  3279. testVector a, b, c;
  3280. testVector test_sha[3];
  3281. wc_test_ret_t ret = 0;
  3282. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3283. byte large_input[1024];
  3284. const char* large_digest =
  3285. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  3286. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  3287. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3288. /* test vector with hash of empty string */
  3289. const char* Keccak256EmptyOut =
  3290. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  3291. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  3292. #endif
  3293. /*
  3294. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  3295. */
  3296. a.input = "";
  3297. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  3298. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  3299. "\x43\x4a";
  3300. a.inLen = XSTRLEN(a.input);
  3301. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  3302. b.input = "abc";
  3303. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  3304. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  3305. "\x15\x32";
  3306. b.inLen = XSTRLEN(b.input);
  3307. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  3308. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3309. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  3310. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  3311. "\x33\x76";
  3312. c.inLen = XSTRLEN(c.input);
  3313. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  3314. test_sha[0] = a;
  3315. test_sha[1] = b;
  3316. test_sha[2] = c;
  3317. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  3318. if (ret != 0)
  3319. return WC_TEST_RET_ENC_EC(ret);
  3320. for (i = 0; i < times; ++i) {
  3321. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  3322. (word32)test_sha[i].inLen);
  3323. if (ret != 0)
  3324. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3325. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  3326. if (ret != 0)
  3327. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3328. ret = wc_Sha3_256_Final(&sha, hash);
  3329. if (ret != 0)
  3330. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3331. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  3332. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3333. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  3334. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3335. }
  3336. #ifndef NO_LARGE_HASH_TEST
  3337. /* BEGIN LARGE HASH TEST */ {
  3338. for (i = 0; i < (int)sizeof(large_input); i++) {
  3339. large_input[i] = (byte)(i & 0xFF);
  3340. }
  3341. times = 100;
  3342. for (i = 0; i < times; ++i) {
  3343. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  3344. (word32)sizeof(large_input));
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3347. }
  3348. ret = wc_Sha3_256_Final(&sha, hash);
  3349. if (ret != 0)
  3350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3351. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  3352. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3353. } /* END LARGE HASH TEST */
  3354. #endif /* NO_LARGE_HASH_TEST */
  3355. /* this is a software only variant of SHA3 not supported by external hardware devices */
  3356. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3357. /* Test for Keccak256 */
  3358. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  3359. if (ret != 0) {
  3360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3361. }
  3362. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  3363. if (ret != 0) {
  3364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3365. }
  3366. ret = wc_Sha3_256_Final(&sha, hash);
  3367. if (ret != 0) {
  3368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3369. }
  3370. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  3371. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3372. }
  3373. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  3374. exit:
  3375. wc_Sha3_256_Free(&sha);
  3376. return ret;
  3377. }
  3378. #endif /* WOLFSSL_NOSHA3_256 */
  3379. #ifndef WOLFSSL_NOSHA3_384
  3380. static wc_test_ret_t sha3_384_test(void)
  3381. {
  3382. wc_Sha3 sha;
  3383. byte hash[WC_SHA3_384_DIGEST_SIZE];
  3384. byte buf[64];
  3385. #ifndef NO_INTM_HASH_TEST
  3386. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  3387. #endif
  3388. testVector a, b, c;
  3389. testVector test_sha[3];
  3390. wc_test_ret_t ret;
  3391. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3392. /*
  3393. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  3394. */
  3395. a.input = "";
  3396. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  3397. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  3398. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  3399. "\xd5\xf0\x04";
  3400. a.inLen = XSTRLEN(a.input);
  3401. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  3402. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3403. /* NIST test vector with a length that is a multiple of 4 */
  3404. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  3405. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  3406. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  3407. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  3408. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  3409. "\x19\x87\x22";
  3410. b.inLen = XSTRLEN(b.input);
  3411. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3412. #else
  3413. b.input = "abc";
  3414. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  3415. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  3416. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  3417. "\x37\x6d\x25";
  3418. b.inLen = XSTRLEN(b.input);
  3419. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3420. #endif
  3421. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3422. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  3423. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  3424. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  3425. "\x65\x7c\x22";
  3426. c.inLen = XSTRLEN(c.input);
  3427. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  3428. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3429. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  3430. #else
  3431. test_sha[0] = a;
  3432. #endif
  3433. test_sha[1] = b;
  3434. test_sha[2] = c;
  3435. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  3436. if (ret != 0)
  3437. return WC_TEST_RET_ENC_EC(ret);
  3438. for (i = 0; i < times; ++i) {
  3439. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  3440. ret = wc_Sha3_384_Update(&sha, buf,
  3441. (word32)test_sha[i].inLen);
  3442. if (ret != 0)
  3443. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3444. #ifndef NO_INTM_HASH_TEST
  3445. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  3446. if (ret != 0)
  3447. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3448. #endif
  3449. ret = wc_Sha3_384_Final(&sha, hash);
  3450. if (ret != 0)
  3451. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3452. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  3453. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3454. #ifndef NO_INTM_HASH_TEST
  3455. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  3456. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3457. #endif
  3458. }
  3459. #ifndef NO_LARGE_HASH_TEST
  3460. /* BEGIN LARGE HASH TEST */ {
  3461. byte large_input[1024];
  3462. const char* large_digest =
  3463. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3464. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3465. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3466. for (i = 0; i < (int)sizeof(large_input); i++) {
  3467. large_input[i] = (byte)(i & 0xFF);
  3468. }
  3469. times = 100;
  3470. for (i = 0; i < times; ++i) {
  3471. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3472. (word32)sizeof(large_input));
  3473. if (ret != 0)
  3474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3475. }
  3476. ret = wc_Sha3_384_Final(&sha, hash);
  3477. if (ret != 0)
  3478. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3479. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3480. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3481. } /* END LARGE HASH TEST */
  3482. #endif /* NO_LARGE_HASH_TEST */
  3483. exit:
  3484. wc_Sha3_384_Free(&sha);
  3485. return ret;
  3486. }
  3487. #endif /* WOLFSSL_NOSHA3_384 */
  3488. #ifndef WOLFSSL_NOSHA3_512
  3489. static wc_test_ret_t sha3_512_test(void)
  3490. {
  3491. wc_Sha3 sha;
  3492. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3493. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3494. testVector a, b, c;
  3495. testVector test_sha[3];
  3496. wc_test_ret_t ret;
  3497. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3498. /*
  3499. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  3500. */
  3501. a.input = "";
  3502. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3503. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3504. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3505. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3506. "\x28\x1d\xcd\x26";
  3507. a.inLen = XSTRLEN(a.input);
  3508. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3509. b.input = "abc";
  3510. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3511. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3512. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3513. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3514. "\x4e\xec\x53\xf0";
  3515. b.inLen = XSTRLEN(b.input);
  3516. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3517. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3518. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3519. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3520. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3521. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3522. "\x39\xd6\xe7\x5e";
  3523. c.inLen = XSTRLEN(c.input);
  3524. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3525. test_sha[0] = a;
  3526. test_sha[1] = b;
  3527. test_sha[2] = c;
  3528. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3529. if (ret != 0)
  3530. return WC_TEST_RET_ENC_EC(ret);
  3531. for (i = 0; i < times; ++i) {
  3532. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3533. (word32)test_sha[i].inLen);
  3534. if (ret != 0)
  3535. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3536. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3537. if (ret != 0)
  3538. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3539. ret = wc_Sha3_512_Final(&sha, hash);
  3540. if (ret != 0)
  3541. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3542. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3543. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3544. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3545. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3546. }
  3547. #ifndef NO_LARGE_HASH_TEST
  3548. /* BEGIN LARGE HASH TEST */ {
  3549. byte large_input[1024];
  3550. const char* large_digest =
  3551. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3552. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3553. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3554. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3555. for (i = 0; i < (int)sizeof(large_input); i++) {
  3556. large_input[i] = (byte)(i & 0xFF);
  3557. }
  3558. times = 100;
  3559. for (i = 0; i < times; ++i) {
  3560. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3561. (word32)sizeof(large_input));
  3562. if (ret != 0)
  3563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3564. }
  3565. ret = wc_Sha3_512_Final(&sha, hash);
  3566. if (ret != 0)
  3567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3568. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3569. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3570. } /* END LARGE HASH TEST */
  3571. #endif /* NO_LARGE_HASH_TEST */
  3572. exit:
  3573. wc_Sha3_512_Free(&sha);
  3574. return ret;
  3575. }
  3576. #endif /* WOLFSSL_NOSHA3_512 */
  3577. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  3578. {
  3579. wc_test_ret_t ret;
  3580. (void)ret;
  3581. #ifndef WOLFSSL_NOSHA3_224
  3582. if ((ret = sha3_224_test()) != 0)
  3583. return ret;
  3584. #endif
  3585. #ifndef WOLFSSL_NOSHA3_256
  3586. if ((ret = sha3_256_test()) != 0)
  3587. return ret;
  3588. #endif
  3589. #ifndef WOLFSSL_NOSHA3_384
  3590. if ((ret = sha3_384_test()) != 0)
  3591. return ret;
  3592. #endif
  3593. #ifndef WOLFSSL_NOSHA3_512
  3594. if ((ret = sha3_512_test()) != 0)
  3595. return ret;
  3596. #endif
  3597. return 0;
  3598. }
  3599. #endif /* WOLFSSL_SHA3 */
  3600. #ifdef WOLFSSL_SHAKE128
  3601. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3602. size_t large_input_buf_size)
  3603. {
  3604. testVector a, b, c, d, e;
  3605. testVector test_sha[5];
  3606. wc_test_ret_t ret = 0;
  3607. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3608. static const char large_digest[] =
  3609. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3610. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3611. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3612. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3613. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3614. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3615. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3616. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3617. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3618. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3619. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3620. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3621. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3622. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3623. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3624. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3625. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3626. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3627. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3628. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3629. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3630. byte hash[sizeof(large_digest) - 1];
  3631. /*
  3632. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3633. */
  3634. a.input = "";
  3635. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3636. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3637. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3638. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3639. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3640. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3641. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3642. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3643. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3644. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3645. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3646. "\xb0\xfc\xa9";
  3647. a.inLen = XSTRLEN(a.input);
  3648. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3649. b.input = "abc";
  3650. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3651. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3652. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3653. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3654. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3655. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3656. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3657. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3658. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3659. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3660. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3661. "\x47\x58\x4e";
  3662. b.inLen = XSTRLEN(b.input);
  3663. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3664. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3665. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3666. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3667. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3668. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3669. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3670. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3671. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3672. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3673. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3674. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3675. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3676. "\xb4\xc9\x54";
  3677. c.inLen = XSTRLEN(c.input);
  3678. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3679. /* Taken from NIST CAVP test vectors - full rate output. */
  3680. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3681. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3682. "\x85\xe0";
  3683. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3684. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3685. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3686. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3687. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3688. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3689. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3690. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3691. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3692. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3693. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3694. "\x55\x4d\x09";
  3695. d.inLen = 32;
  3696. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3697. /* Taken from NIST CAVP test vectors - more than one output block. */
  3698. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3699. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3700. "\x47\xe4";
  3701. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3702. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3703. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3704. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3705. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3706. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3707. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3708. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3709. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3710. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3711. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3712. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3713. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3714. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3715. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3716. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3717. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3718. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3719. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3720. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3721. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3722. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3723. "\xd9\xe9\xe6\xf9\xff\xa5";
  3724. e.inLen = 32;
  3725. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3726. test_sha[0] = a;
  3727. test_sha[1] = b;
  3728. test_sha[2] = c;
  3729. test_sha[3] = d;
  3730. test_sha[4] = e;
  3731. for (i = 0; i < times; ++i) {
  3732. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3733. if (ret != 0)
  3734. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3735. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3736. (word32)test_sha[i].inLen);
  3737. if (ret != 0)
  3738. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3739. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3740. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3741. if (ret != 0)
  3742. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3743. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3744. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3745. }
  3746. #ifndef NO_LARGE_HASH_TEST
  3747. /* BEGIN LARGE HASH TEST */ {
  3748. for (i = 0; i < (int)large_input_buf_size; i++) {
  3749. large_input_buf[i] = (byte)(i & 0xFF);
  3750. }
  3751. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3752. if (ret != 0)
  3753. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3754. /* Absorb is non-incremental. */
  3755. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  3756. (word32)large_input_buf_size);
  3757. if (ret != 0)
  3758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3759. /* Able to squeeze out blocks incrementally. */
  3760. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3761. if (ret != 0)
  3762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3763. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3764. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3765. if (ret != 0)
  3766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3767. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3768. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3769. } /* END LARGE HASH TEST */
  3770. #endif /* NO_LARGE_HASH_TEST */
  3771. exit:
  3772. return ret;
  3773. }
  3774. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  3775. {
  3776. wc_Shake sha;
  3777. byte hash[250];
  3778. testVector a, b, c, d, e;
  3779. testVector test_sha[5];
  3780. wc_test_ret_t ret = 0;
  3781. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3782. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  3783. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3784. byte *large_input = NULL;
  3785. #else
  3786. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  3787. #endif
  3788. static const char large_digest[] =
  3789. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3790. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3791. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3792. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3793. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3794. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3795. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3796. "\xfa\x1b";
  3797. /*
  3798. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3799. */
  3800. a.input = "";
  3801. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3802. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3803. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3804. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3805. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3806. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3807. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3808. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3809. a.inLen = XSTRLEN(a.input);
  3810. a.outLen = 114;
  3811. b.input = "abc";
  3812. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3813. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3814. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3815. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3816. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3817. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3818. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3819. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3820. b.inLen = XSTRLEN(b.input);
  3821. b.outLen = 114;
  3822. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3823. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3824. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3825. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3826. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3827. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3828. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3829. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3830. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3831. c.inLen = XSTRLEN(c.input);
  3832. c.outLen = 114;
  3833. /* Taken from NIST CAVP test vectors - full rate output. */
  3834. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3835. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3836. "\x85\xe0";
  3837. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3838. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3839. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3840. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3841. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3842. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3843. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3844. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3845. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3846. "\xa1";
  3847. d.inLen = 32;
  3848. d.outLen = 136;
  3849. /* Taken from NIST CAVP test vectors - more than one output block. */
  3850. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3851. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3852. "\x47\xe4";
  3853. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3854. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3855. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3856. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3857. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3858. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3859. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3860. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3861. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3862. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3863. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3864. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3865. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3866. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3867. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3868. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3869. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3870. e.inLen = 32;
  3871. e.outLen = 250;
  3872. test_sha[0] = a;
  3873. test_sha[1] = b;
  3874. test_sha[2] = c;
  3875. test_sha[3] = d;
  3876. test_sha[4] = e;
  3877. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3878. if (ret != 0)
  3879. return WC_TEST_RET_ENC_EC(ret);
  3880. for (i = 0; i < times; ++i) {
  3881. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3882. (word32)test_sha[i].inLen);
  3883. if (ret != 0)
  3884. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3885. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3886. if (ret != 0)
  3887. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3888. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3889. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3890. }
  3891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3892. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  3893. DYNAMIC_TYPE_TMP_BUFFER);
  3894. if (large_input == NULL)
  3895. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  3896. #endif
  3897. #ifndef NO_LARGE_HASH_TEST
  3898. /* BEGIN LARGE HASH TEST */ {
  3899. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  3900. large_input[i] = (byte)(i & 0xFF);
  3901. }
  3902. times = 100;
  3903. for (i = 0; i < times; ++i) {
  3904. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3905. SHAKE128_LARGE_INPUT_BUFSIZ);
  3906. if (ret != 0)
  3907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3908. }
  3909. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3910. if (ret != 0)
  3911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3912. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  3913. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3914. } /* END LARGE HASH TEST */
  3915. #endif /* NO_LARGE_HASH_TEST */
  3916. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  3917. exit:
  3918. wc_Shake128_Free(&sha);
  3919. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3920. if (large_input != NULL)
  3921. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3922. #endif
  3923. return ret;
  3924. }
  3925. #endif
  3926. #ifdef WOLFSSL_SHAKE256
  3927. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3928. size_t large_input_buf_size)
  3929. {
  3930. testVector a, b, c, d, e;
  3931. testVector test_sha[5];
  3932. wc_test_ret_t ret = 0;
  3933. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3934. static const char large_digest[] =
  3935. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3936. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3937. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3938. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3939. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3940. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3941. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3942. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3943. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3944. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3945. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3946. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3947. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3948. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3949. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3950. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3951. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3952. byte hash[sizeof(large_digest) - 1];
  3953. /*
  3954. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  3955. */
  3956. a.input = "";
  3957. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3958. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3959. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3960. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3961. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3962. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3963. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3964. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3965. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3966. "\xdd";
  3967. a.inLen = XSTRLEN(a.input);
  3968. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3969. b.input = "abc";
  3970. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3971. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3972. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3973. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3974. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3975. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3976. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3977. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3978. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3979. "\x29";
  3980. b.inLen = XSTRLEN(b.input);
  3981. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3982. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3983. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3984. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3985. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3986. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3987. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3988. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3989. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3990. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3991. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3992. "\x8d";
  3993. c.inLen = XSTRLEN(c.input);
  3994. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3995. /* Taken from NIST CAVP test vectors - full rate output. */
  3996. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3997. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3998. "\x85\xe0";
  3999. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4000. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4001. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4002. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4003. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4004. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4005. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4006. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4007. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4008. "\xc2";
  4009. d.inLen = 32;
  4010. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4011. /* Taken from NIST CAVP test vectors - more than one output block. */
  4012. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4013. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4014. "\x47\xe4";
  4015. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4016. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4017. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4018. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4019. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4020. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4021. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4022. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4023. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4024. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4025. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4026. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4027. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4028. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4029. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4030. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4031. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4032. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4033. "\x68\x44";
  4034. e.inLen = 32;
  4035. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4036. test_sha[0] = a;
  4037. test_sha[1] = b;
  4038. test_sha[2] = c;
  4039. test_sha[3] = d;
  4040. test_sha[4] = e;
  4041. for (i = 0; i < times; ++i) {
  4042. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4043. if (ret != 0)
  4044. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4045. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4046. (word32)test_sha[i].inLen);
  4047. if (ret != 0)
  4048. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4049. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4050. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4051. if (ret != 0)
  4052. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4053. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4054. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4055. }
  4056. #ifndef NO_LARGE_HASH_TEST
  4057. /* BEGIN LARGE HASH TEST */ {
  4058. for (i = 0; i < (int)large_input_buf_size; i++) {
  4059. large_input_buf[i] = (byte)(i & 0xFF);
  4060. }
  4061. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4062. if (ret != 0)
  4063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4064. /* Absorb is non-incremental. */
  4065. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4066. (word32)large_input_buf_size);
  4067. if (ret != 0)
  4068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4069. /* Able to squeeze out blocks incrementally. */
  4070. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4071. if (ret != 0)
  4072. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4073. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4074. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4075. if (ret != 0)
  4076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4077. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4078. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4079. } /* END LARGE HASH TEST */
  4080. #endif /* NO_LARGE_HASH_TEST */
  4081. exit:
  4082. return ret;
  4083. }
  4084. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4085. {
  4086. wc_Shake sha;
  4087. byte hash[250];
  4088. testVector a, b, c, d, e;
  4089. testVector test_sha[5];
  4090. wc_test_ret_t ret = 0;
  4091. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4092. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4093. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4094. byte *large_input = NULL;
  4095. #else
  4096. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4097. #endif
  4098. const char* large_digest =
  4099. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4100. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4101. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4102. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4103. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4104. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4105. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4106. "\xea\x26";
  4107. /*
  4108. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4109. */
  4110. a.input = "";
  4111. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4112. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4113. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4114. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4115. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4116. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4117. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4118. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4119. a.inLen = XSTRLEN(a.input);
  4120. a.outLen = 114;
  4121. b.input = "abc";
  4122. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4123. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4124. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4125. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4126. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4127. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4128. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4129. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4130. b.inLen = XSTRLEN(b.input);
  4131. b.outLen = 114;
  4132. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4133. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4134. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4135. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4136. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4137. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4138. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4139. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4140. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4141. c.inLen = XSTRLEN(c.input);
  4142. c.outLen = 114;
  4143. /* Taken from NIST CAVP test vectors - full rate output. */
  4144. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4145. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4146. "\x85\xe0";
  4147. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4148. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4149. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4150. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4151. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4152. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4153. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4154. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4155. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4156. "\xc2";
  4157. d.inLen = 32;
  4158. d.outLen = 136;
  4159. /* Taken from NIST CAVP test vectors - more than one output block. */
  4160. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4161. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4162. "\x47\xe4";
  4163. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4164. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4165. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4166. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4167. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4168. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4169. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4170. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4171. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4172. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4173. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4174. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4175. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4176. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4177. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4178. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4179. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  4180. e.inLen = 32;
  4181. e.outLen = 250;
  4182. test_sha[0] = a;
  4183. test_sha[1] = b;
  4184. test_sha[2] = c;
  4185. test_sha[3] = d;
  4186. test_sha[4] = e;
  4187. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  4188. if (ret != 0)
  4189. return WC_TEST_RET_ENC_EC(ret);
  4190. for (i = 0; i < times; ++i) {
  4191. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  4192. (word32)test_sha[i].inLen);
  4193. if (ret != 0)
  4194. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4195. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  4196. if (ret != 0)
  4197. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4198. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4199. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4200. }
  4201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4202. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  4203. DYNAMIC_TYPE_TMP_BUFFER);
  4204. if (large_input == NULL)
  4205. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4206. #endif
  4207. #ifndef NO_LARGE_HASH_TEST
  4208. /* BEGIN LARGE HASH TEST */ {
  4209. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  4210. large_input[i] = (byte)(i & 0xFF);
  4211. }
  4212. times = 100;
  4213. for (i = 0; i < times; ++i) {
  4214. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  4215. SHAKE256_LARGE_INPUT_BUFSIZ);
  4216. if (ret != 0)
  4217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4218. }
  4219. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  4220. if (ret != 0)
  4221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4222. if (XMEMCMP(hash, large_digest, 114) != 0)
  4223. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4224. } /* END LARGE HASH TEST */
  4225. #endif /* NO_LARGE_HASH_TEST */
  4226. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  4227. exit:
  4228. wc_Shake256_Free(&sha);
  4229. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4230. if (large_input != NULL)
  4231. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4232. #endif
  4233. return ret;
  4234. }
  4235. #endif
  4236. #ifdef WOLFSSL_SM3
  4237. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  4238. {
  4239. wc_Sm3 sm3, sm3Copy;
  4240. byte hash[WC_SM3_DIGEST_SIZE];
  4241. byte hashGet[WC_SM3_DIGEST_SIZE];
  4242. byte hashCopy[WC_SM3_DIGEST_SIZE];
  4243. wc_test_ret_t ret = 0;
  4244. testVector a, b, c;
  4245. testVector test_sm3[3];
  4246. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  4247. a.input = "";
  4248. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  4249. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  4250. "\xaa\x2b";
  4251. a.inLen = XSTRLEN(a.input);
  4252. a.outLen = WC_SM3_DIGEST_SIZE;
  4253. b.input = "abc";
  4254. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  4255. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  4256. "\xa8\xe0";
  4257. b.inLen = XSTRLEN(b.input);
  4258. b.outLen = WC_SM3_DIGEST_SIZE;
  4259. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4260. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  4261. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  4262. "\x8c\x05";
  4263. c.inLen = XSTRLEN(c.input);
  4264. c.outLen = WC_SM3_DIGEST_SIZE;
  4265. test_sm3[0] = a;
  4266. test_sm3[1] = b;
  4267. test_sm3[2] = c;
  4268. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  4269. if (ret != 0)
  4270. return WC_TEST_RET_ENC_EC(ret);
  4271. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  4272. if (ret != 0) {
  4273. wc_Sm3Free(&sm3);
  4274. return WC_TEST_RET_ENC_EC(ret);
  4275. }
  4276. /* Test all the KATs. */
  4277. for (i = 0; i < times; ++i) {
  4278. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  4279. (word32)test_sm3[i].inLen);
  4280. if (ret != 0) {
  4281. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4282. }
  4283. /* Get the final hash but leave ready for more updates. */
  4284. ret = wc_Sm3GetHash(&sm3, hashGet);
  4285. if (ret != 0)
  4286. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4287. /* Make a copy of the hash. */
  4288. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  4289. if (ret != 0)
  4290. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4291. /* Get the final hash with original. */
  4292. ret = wc_Sm3Final(&sm3, hash);
  4293. if (ret != 0)
  4294. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4295. /* Get the final hash with copy. */
  4296. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  4297. if (ret != 0)
  4298. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4299. /* Dispose of copy. */
  4300. wc_Sm3Free(&sm3Copy);
  4301. /* Check hashes match expected. */
  4302. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  4303. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4304. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  4305. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4306. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  4307. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4308. }
  4309. #ifndef NO_LARGE_HASH_TEST
  4310. {
  4311. word32 sz;
  4312. byte large_input[1024];
  4313. #ifdef HASH_SIZE_LIMIT
  4314. const char* large_digest =
  4315. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  4316. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  4317. times = 20;
  4318. #else
  4319. const char* large_digest =
  4320. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  4321. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  4322. times = 100;
  4323. #endif
  4324. /* Set large input to something. */
  4325. for (i = 0; i < (int)sizeof(large_input); i++) {
  4326. large_input[i] = (byte)(i & 0xFF);
  4327. }
  4328. /* Hash a large number of times. */
  4329. for (i = 0; i < times; ++i) {
  4330. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  4331. (word32)sizeof(large_input));
  4332. if (ret != 0)
  4333. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4334. }
  4335. /* Calculate hash and compare to expected. */
  4336. ret = wc_Sm3Final(&sm3, hash);
  4337. if (ret != 0)
  4338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4339. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4340. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4341. /* Check updating with various sizes works. */
  4342. for (sz = 1; sz <= 64; sz++) {
  4343. /* Hash a large number of times. */
  4344. for (i = 0; i < times; ++i) {
  4345. word32 o;
  4346. /* Update sz bytes at a time from large input buffer. */
  4347. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  4348. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  4349. if (ret != 0)
  4350. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  4351. }
  4352. /* Check for left-overs. */
  4353. if (o < (word32)sizeof(large_input)) {
  4354. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  4355. (word32)sizeof(large_input) - o);
  4356. if (ret != 0)
  4357. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4358. }
  4359. }
  4360. /* Calculate hash and compare to expected. */
  4361. ret = wc_Sm3Final(&sm3, hash);
  4362. if (ret != 0)
  4363. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4364. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4365. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4366. }
  4367. }
  4368. #endif /* NO_LARGE_HASH_TEST */
  4369. exit:
  4370. wc_Sm3Free(&sm3);
  4371. wc_Sm3Free(&sm3Copy);
  4372. return ret;
  4373. }
  4374. #endif
  4375. #ifndef NO_HASH_WRAPPER
  4376. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  4377. {
  4378. wc_HashAlg hash;
  4379. int ret, exp_ret;
  4380. int i, j;
  4381. int digestSz;
  4382. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  4383. byte out[WC_MAX_DIGEST_SIZE];
  4384. byte hashOut[WC_MAX_DIGEST_SIZE];
  4385. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4386. enum wc_HashType hashType;
  4387. #endif
  4388. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  4389. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  4390. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  4391. WC_HASH_TYPE_SHA3_224,
  4392. WC_HASH_TYPE_SHA3_256,
  4393. WC_HASH_TYPE_SHA3_384,
  4394. WC_HASH_TYPE_SHA3_512 };
  4395. enum wc_HashType typesNoImpl[] = {
  4396. #ifdef NO_MD5
  4397. WC_HASH_TYPE_MD5,
  4398. #endif
  4399. #ifdef NO_SHA
  4400. WC_HASH_TYPE_SHA,
  4401. #endif
  4402. #ifndef WOLFSSL_SHA224
  4403. WC_HASH_TYPE_SHA224,
  4404. #endif
  4405. #ifdef NO_SHA256
  4406. WC_HASH_TYPE_SHA256,
  4407. #endif
  4408. #ifndef WOLFSSL_SHA384
  4409. WC_HASH_TYPE_SHA384,
  4410. #endif
  4411. #ifndef WOLFSSL_SHA512
  4412. WC_HASH_TYPE_SHA512,
  4413. #endif
  4414. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  4415. WC_HASH_TYPE_SHA3_224,
  4416. #endif
  4417. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  4418. WC_HASH_TYPE_SHA3_256,
  4419. #endif
  4420. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  4421. WC_HASH_TYPE_SHA3_384,
  4422. #endif
  4423. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  4424. WC_HASH_TYPE_SHA3_512,
  4425. #endif
  4426. WC_HASH_TYPE_NONE
  4427. };
  4428. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  4429. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  4430. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  4431. WC_HASH_TYPE_BLAKE2B,
  4432. WC_HASH_TYPE_NONE };
  4433. /* Parameter Validation testing. */
  4434. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  4435. if (ret != BAD_FUNC_ARG)
  4436. return WC_TEST_RET_ENC_EC(ret);
  4437. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4438. if (ret != BAD_FUNC_ARG)
  4439. return WC_TEST_RET_ENC_EC(ret);
  4440. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4441. if (ret != BAD_FUNC_ARG)
  4442. return WC_TEST_RET_ENC_EC(ret);
  4443. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  4444. if (ret != BAD_FUNC_ARG)
  4445. return WC_TEST_RET_ENC_EC(ret);
  4446. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  4447. if (ret != BAD_FUNC_ARG)
  4448. return WC_TEST_RET_ENC_EC(ret);
  4449. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  4450. if (ret != BAD_FUNC_ARG)
  4451. return WC_TEST_RET_ENC_EC(ret);
  4452. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  4453. if (ret != BAD_FUNC_ARG)
  4454. return WC_TEST_RET_ENC_EC(ret);
  4455. /* Try invalid hash algorithms. */
  4456. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  4457. ret = wc_HashInit(&hash, typesBad[i]);
  4458. if (ret != BAD_FUNC_ARG)
  4459. return WC_TEST_RET_ENC_I(i);
  4460. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  4461. if (ret != BAD_FUNC_ARG)
  4462. return WC_TEST_RET_ENC_I(i);
  4463. ret = wc_HashFinal(&hash, typesBad[i], out);
  4464. if (ret != BAD_FUNC_ARG)
  4465. return WC_TEST_RET_ENC_I(i);
  4466. wc_HashFree(&hash, typesBad[i]);
  4467. }
  4468. /* Try valid hash algorithms. */
  4469. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  4470. exp_ret = 0;
  4471. if (typesGood[i] == typesNoImpl[j]) {
  4472. /* Recognized but no implementation compiled in. */
  4473. exp_ret = HASH_TYPE_E;
  4474. j++;
  4475. }
  4476. ret = wc_HashInit(&hash, typesGood[i]);
  4477. if (ret != exp_ret)
  4478. return WC_TEST_RET_ENC_I(i);
  4479. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  4480. if (ret != exp_ret)
  4481. return WC_TEST_RET_ENC_I(i);
  4482. ret = wc_HashFinal(&hash, typesGood[i], out);
  4483. if (ret != exp_ret)
  4484. return WC_TEST_RET_ENC_I(i);
  4485. wc_HashFree(&hash, typesGood[i]);
  4486. digestSz = wc_HashGetDigestSize(typesGood[i]);
  4487. if (exp_ret < 0 && digestSz != exp_ret)
  4488. return WC_TEST_RET_ENC_I(i);
  4489. if (exp_ret == 0 && digestSz < 0)
  4490. return WC_TEST_RET_ENC_I(i);
  4491. if (exp_ret == 0) {
  4492. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  4493. digestSz - 1);
  4494. if (ret != BUFFER_E)
  4495. return WC_TEST_RET_ENC_I(i);
  4496. }
  4497. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  4498. if (ret != exp_ret)
  4499. return WC_TEST_RET_ENC_I(i);
  4500. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  4501. return WC_TEST_RET_ENC_I(i);
  4502. ret = wc_HashGetBlockSize(typesGood[i]);
  4503. if (exp_ret < 0 && ret != exp_ret)
  4504. return WC_TEST_RET_ENC_I(i);
  4505. if (exp_ret == 0 && ret < 0)
  4506. return WC_TEST_RET_ENC_I(i);
  4507. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4508. ret = wc_HashGetOID(typesGood[i]);
  4509. if (ret == BAD_FUNC_ARG ||
  4510. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  4511. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  4512. return WC_TEST_RET_ENC_I(i);
  4513. }
  4514. hashType = wc_OidGetHash(ret);
  4515. if (exp_ret == 0 && hashType != typesGood[i])
  4516. return WC_TEST_RET_ENC_I(i);
  4517. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4518. }
  4519. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  4520. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  4521. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  4522. return WC_TEST_RET_ENC_I(i);
  4523. }
  4524. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4525. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  4526. #ifdef WOLFSSL_MD2
  4527. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4528. return WC_TEST_RET_ENC_EC(ret);
  4529. #else
  4530. if (ret != HASH_TYPE_E)
  4531. return WC_TEST_RET_ENC_EC(ret);
  4532. #endif
  4533. hashType = wc_OidGetHash(646); /* Md2h */
  4534. #ifdef WOLFSSL_MD2
  4535. if (hashType != WC_HASH_TYPE_MD2)
  4536. return WC_TEST_RET_ENC_NC;
  4537. #else
  4538. if (hashType != WC_HASH_TYPE_NONE)
  4539. return WC_TEST_RET_ENC_NC;
  4540. #endif
  4541. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  4542. #ifndef NO_MD5
  4543. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4544. return WC_TEST_RET_ENC_EC(ret);
  4545. #else
  4546. if (ret != HASH_TYPE_E)
  4547. return WC_TEST_RET_ENC_EC(ret);
  4548. #endif
  4549. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  4550. if (ret != BAD_FUNC_ARG)
  4551. return WC_TEST_RET_ENC_EC(ret);
  4552. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  4553. if (ret != BAD_FUNC_ARG)
  4554. return WC_TEST_RET_ENC_EC(ret);
  4555. hashType = wc_OidGetHash(0);
  4556. if (hashType != WC_HASH_TYPE_NONE)
  4557. return WC_TEST_RET_ENC_NC;
  4558. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4559. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  4560. #ifdef WOLFSSL_MD2
  4561. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4562. return WC_TEST_RET_ENC_EC(ret);
  4563. #else
  4564. if (ret != HASH_TYPE_E)
  4565. return WC_TEST_RET_ENC_EC(ret);
  4566. #endif
  4567. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  4568. #ifdef WOLFSSL_MD2
  4569. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4570. return WC_TEST_RET_ENC_EC(ret);
  4571. #else
  4572. if (ret != HASH_TYPE_E)
  4573. return WC_TEST_RET_ENC_EC(ret);
  4574. #endif
  4575. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  4576. #ifndef NO_MD4
  4577. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4578. return WC_TEST_RET_ENC_EC(ret);
  4579. #else
  4580. if (ret != HASH_TYPE_E)
  4581. return WC_TEST_RET_ENC_EC(ret);
  4582. #endif
  4583. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  4584. #ifndef NO_MD4
  4585. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4586. return WC_TEST_RET_ENC_EC(ret);
  4587. #else
  4588. if (ret != HASH_TYPE_E)
  4589. return WC_TEST_RET_ENC_EC(ret);
  4590. #endif
  4591. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  4592. #if !defined(NO_MD5) && !defined(NO_SHA)
  4593. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4594. return WC_TEST_RET_ENC_EC(ret);
  4595. #else
  4596. if (ret != HASH_TYPE_E)
  4597. return WC_TEST_RET_ENC_EC(ret);
  4598. #endif
  4599. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  4600. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4601. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4602. return WC_TEST_RET_ENC_EC(ret);
  4603. #else
  4604. if (ret != HASH_TYPE_E)
  4605. return WC_TEST_RET_ENC_EC(ret);
  4606. #endif
  4607. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  4608. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4609. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4610. return WC_TEST_RET_ENC_EC(ret);
  4611. #else
  4612. if (ret != HASH_TYPE_E)
  4613. return WC_TEST_RET_ENC_EC(ret);
  4614. #endif
  4615. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  4616. if (ret != BAD_FUNC_ARG)
  4617. return WC_TEST_RET_ENC_EC(ret);
  4618. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  4619. if (ret != BAD_FUNC_ARG)
  4620. return WC_TEST_RET_ENC_EC(ret);
  4621. #if !defined(NO_CERTS) && !defined(NO_ASN)
  4622. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  4623. ret = wc_GetCTC_HashOID(MD2);
  4624. if (ret == 0)
  4625. return WC_TEST_RET_ENC_EC(ret);
  4626. #endif
  4627. #ifndef NO_MD5
  4628. ret = wc_GetCTC_HashOID(WC_MD5);
  4629. if (ret == 0)
  4630. return WC_TEST_RET_ENC_EC(ret);
  4631. #endif
  4632. #ifndef NO_SHA
  4633. ret = wc_GetCTC_HashOID(WC_SHA);
  4634. if (ret == 0)
  4635. return WC_TEST_RET_ENC_EC(ret);
  4636. #endif
  4637. #ifdef WOLFSSL_SHA224
  4638. ret = wc_GetCTC_HashOID(WC_SHA224);
  4639. if (ret == 0)
  4640. return WC_TEST_RET_ENC_EC(ret);
  4641. #endif
  4642. #ifndef NO_SHA256
  4643. ret = wc_GetCTC_HashOID(WC_SHA256);
  4644. if (ret == 0)
  4645. return WC_TEST_RET_ENC_EC(ret);
  4646. #endif
  4647. #ifdef WOLFSSL_SHA384
  4648. ret = wc_GetCTC_HashOID(WC_SHA384);
  4649. if (ret == 0)
  4650. return WC_TEST_RET_ENC_EC(ret);
  4651. #endif
  4652. #ifdef WOLFSSL_SHA512
  4653. ret = wc_GetCTC_HashOID(WC_SHA512);
  4654. if (ret == 0)
  4655. return WC_TEST_RET_ENC_EC(ret);
  4656. #endif
  4657. ret = wc_GetCTC_HashOID(-1);
  4658. if (ret != 0)
  4659. return WC_TEST_RET_ENC_EC(ret);
  4660. #endif
  4661. return 0;
  4662. }
  4663. #endif /* !NO_HASH_WRAPPER */
  4664. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4665. defined(HAVE_FIPS_VERSION) && \
  4666. (HAVE_FIPS_VERSION >= 5))
  4667. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  4668. {
  4669. Hmac hmac;
  4670. byte hash[WC_MD5_DIGEST_SIZE];
  4671. const char* keys[]=
  4672. {
  4673. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4674. "Jefe",
  4675. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4676. };
  4677. testVector a, b, c;
  4678. testVector test_hmac[3];
  4679. wc_test_ret_t ret;
  4680. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4681. a.input = "Hi There";
  4682. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4683. "\x9d";
  4684. a.inLen = XSTRLEN(a.input);
  4685. a.outLen = WC_MD5_DIGEST_SIZE;
  4686. b.input = "what do ya want for nothing?";
  4687. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4688. "\x38";
  4689. b.inLen = XSTRLEN(b.input);
  4690. b.outLen = WC_MD5_DIGEST_SIZE;
  4691. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4692. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4693. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4694. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4695. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4696. "\xf6";
  4697. c.inLen = XSTRLEN(c.input);
  4698. c.outLen = WC_MD5_DIGEST_SIZE;
  4699. test_hmac[0] = a;
  4700. test_hmac[1] = b;
  4701. test_hmac[2] = c;
  4702. for (i = 0; i < times; ++i) {
  4703. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4704. if (i == 1) {
  4705. continue; /* cavium can't handle short keys, fips not allowed */
  4706. }
  4707. #endif
  4708. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  4709. if (ret != 0)
  4710. return WC_TEST_RET_ENC_EC(ret);
  4711. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4712. (word32)XSTRLEN(keys[i]));
  4713. if (ret != 0)
  4714. return WC_TEST_RET_ENC_EC(ret);
  4715. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4716. (word32)test_hmac[i].inLen);
  4717. if (ret != 0)
  4718. return WC_TEST_RET_ENC_EC(ret);
  4719. ret = wc_HmacFinal(&hmac, hash);
  4720. if (ret != 0)
  4721. return WC_TEST_RET_ENC_EC(ret);
  4722. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4723. return WC_TEST_RET_ENC_I(i);
  4724. wc_HmacFree(&hmac);
  4725. }
  4726. #ifndef HAVE_FIPS
  4727. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  4728. return WC_TEST_RET_ENC_EC(ret);
  4729. #endif
  4730. return 0;
  4731. }
  4732. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4733. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4734. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  4735. {
  4736. Hmac hmac;
  4737. byte hash[WC_SHA_DIGEST_SIZE];
  4738. const char* keys[]=
  4739. {
  4740. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4741. "\x0b\x0b\x0b",
  4742. "Jefe",
  4743. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4744. "\xAA\xAA\xAA"
  4745. };
  4746. testVector a, b, c;
  4747. testVector test_hmac[3];
  4748. wc_test_ret_t ret;
  4749. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4750. a.input = "Hi There";
  4751. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4752. "\x8e\xf1\x46\xbe\x00";
  4753. a.inLen = XSTRLEN(a.input);
  4754. a.outLen = WC_SHA_DIGEST_SIZE;
  4755. b.input = "what do ya want for nothing?";
  4756. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4757. "\x9c\x25\x9a\x7c\x79";
  4758. b.inLen = XSTRLEN(b.input);
  4759. b.outLen = WC_SHA_DIGEST_SIZE;
  4760. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4761. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4762. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4763. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4764. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4765. "\x4f\x63\xf1\x75\xd3";
  4766. c.inLen = XSTRLEN(c.input);
  4767. c.outLen = WC_SHA_DIGEST_SIZE;
  4768. test_hmac[0] = a;
  4769. test_hmac[1] = b;
  4770. test_hmac[2] = c;
  4771. for (i = 0; i < times; ++i) {
  4772. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4773. if (i == 1)
  4774. continue; /* cavium can't handle short keys, fips not allowed */
  4775. #endif
  4776. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  4777. return WC_TEST_RET_ENC_EC(ret);
  4778. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4779. (word32)XSTRLEN(keys[i]));
  4780. if (ret != 0)
  4781. return WC_TEST_RET_ENC_EC(ret);
  4782. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4783. (word32)test_hmac[i].inLen);
  4784. if (ret != 0)
  4785. return WC_TEST_RET_ENC_EC(ret);
  4786. ret = wc_HmacFinal(&hmac, hash);
  4787. if (ret != 0)
  4788. return WC_TEST_RET_ENC_EC(ret);
  4789. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4790. return WC_TEST_RET_ENC_I(i);
  4791. wc_HmacFree(&hmac);
  4792. }
  4793. #ifndef HAVE_FIPS
  4794. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  4795. return WC_TEST_RET_ENC_EC(ret);
  4796. #endif
  4797. return 0;
  4798. }
  4799. #endif
  4800. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4801. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  4802. {
  4803. Hmac hmac;
  4804. byte hash[WC_SHA224_DIGEST_SIZE];
  4805. const char* keys[]=
  4806. {
  4807. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4808. "\x0b\x0b\x0b",
  4809. "Jefe",
  4810. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4811. "\xAA\xAA\xAA",
  4812. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4813. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4814. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4815. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4816. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4817. };
  4818. testVector a, b, c, d;
  4819. testVector test_hmac[4];
  4820. wc_test_ret_t ret;
  4821. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4822. a.input = "Hi There";
  4823. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4824. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4825. a.inLen = XSTRLEN(a.input);
  4826. a.outLen = WC_SHA224_DIGEST_SIZE;
  4827. b.input = "what do ya want for nothing?";
  4828. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4829. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4830. b.inLen = XSTRLEN(b.input);
  4831. b.outLen = WC_SHA224_DIGEST_SIZE;
  4832. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4833. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4834. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4835. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4836. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4837. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4838. c.inLen = XSTRLEN(c.input);
  4839. c.outLen = WC_SHA224_DIGEST_SIZE;
  4840. d.input = "Big Key Input";
  4841. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4842. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4843. d.inLen = XSTRLEN(d.input);
  4844. d.outLen = WC_SHA224_DIGEST_SIZE;
  4845. test_hmac[0] = a;
  4846. test_hmac[1] = b;
  4847. test_hmac[2] = c;
  4848. test_hmac[3] = d;
  4849. for (i = 0; i < times; ++i) {
  4850. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4851. if (i == 1)
  4852. continue; /* cavium can't handle short keys, fips not allowed */
  4853. #endif
  4854. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  4855. return WC_TEST_RET_ENC_EC(ret);
  4856. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4857. (word32)XSTRLEN(keys[i]));
  4858. if (ret != 0)
  4859. return WC_TEST_RET_ENC_EC(ret);
  4860. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4861. (word32)test_hmac[i].inLen);
  4862. if (ret != 0)
  4863. return WC_TEST_RET_ENC_EC(ret);
  4864. ret = wc_HmacFinal(&hmac, hash);
  4865. if (ret != 0)
  4866. return WC_TEST_RET_ENC_EC(ret);
  4867. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4868. return WC_TEST_RET_ENC_I(i);
  4869. wc_HmacFree(&hmac);
  4870. }
  4871. #ifndef HAVE_FIPS
  4872. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  4873. return WC_TEST_RET_ENC_EC(ret);
  4874. #endif
  4875. return 0;
  4876. }
  4877. #endif
  4878. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4879. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  4880. {
  4881. Hmac hmac;
  4882. byte hash[WC_SHA256_DIGEST_SIZE];
  4883. const char* keys[]=
  4884. {
  4885. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4886. "\x0b\x0b\x0b",
  4887. "Jefe",
  4888. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4889. "\xAA\xAA\xAA",
  4890. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4891. "\xAA\xAA\xAA",
  4892. };
  4893. testVector a, b, c, d;
  4894. testVector test_hmac[4];
  4895. wc_test_ret_t ret;
  4896. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4897. a.input = "Hi There";
  4898. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4899. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4900. "\xcf\xf7";
  4901. a.inLen = XSTRLEN(a.input);
  4902. a.outLen = WC_SHA256_DIGEST_SIZE;
  4903. b.input = "what do ya want for nothing?";
  4904. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4905. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4906. "\x38\x43";
  4907. b.inLen = XSTRLEN(b.input);
  4908. b.outLen = WC_SHA256_DIGEST_SIZE;
  4909. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4910. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4911. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4912. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4913. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4914. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4915. "\x65\xfe";
  4916. c.inLen = XSTRLEN(c.input);
  4917. c.outLen = WC_SHA256_DIGEST_SIZE;
  4918. d.input = 0;
  4919. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4920. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4921. "\x3e\x46";
  4922. d.inLen = 0;
  4923. d.outLen = WC_SHA256_DIGEST_SIZE;
  4924. test_hmac[0] = a;
  4925. test_hmac[1] = b;
  4926. test_hmac[2] = c;
  4927. test_hmac[3] = d;
  4928. for (i = 0; i < times; ++i) {
  4929. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4930. if (i == 1)
  4931. continue; /* cavium can't handle short keys, fips not allowed */
  4932. #endif
  4933. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4934. if (i == 3)
  4935. continue; /* QuickAssist can't handle empty HMAC */
  4936. #endif
  4937. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4938. return WC_TEST_RET_ENC_I(i);
  4939. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4940. (word32)XSTRLEN(keys[i]));
  4941. if (ret != 0)
  4942. return WC_TEST_RET_ENC_I(i);
  4943. if (test_hmac[i].input != NULL) {
  4944. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4945. (word32)test_hmac[i].inLen);
  4946. if (ret != 0)
  4947. return WC_TEST_RET_ENC_I(i);
  4948. }
  4949. ret = wc_HmacFinal(&hmac, hash);
  4950. if (ret != 0)
  4951. return WC_TEST_RET_ENC_I(i);
  4952. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4953. return WC_TEST_RET_ENC_I(i);
  4954. wc_HmacFree(&hmac);
  4955. }
  4956. #ifndef HAVE_FIPS
  4957. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  4958. return WC_TEST_RET_ENC_EC(ret);
  4959. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  4960. return WC_TEST_RET_ENC_EC(ret);
  4961. #endif
  4962. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  4963. return WC_TEST_RET_ENC_EC(ret);
  4964. return 0;
  4965. }
  4966. #endif
  4967. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4968. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  4969. {
  4970. Hmac hmac;
  4971. byte hash[WC_SHA384_DIGEST_SIZE];
  4972. const char* keys[]=
  4973. {
  4974. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4975. "\x0b\x0b\x0b",
  4976. "Jefe",
  4977. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4978. "\xAA\xAA\xAA",
  4979. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4980. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4981. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4982. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4983. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4984. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4985. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4986. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4987. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4988. };
  4989. testVector a, b, c, d;
  4990. testVector test_hmac[4];
  4991. wc_test_ret_t ret;
  4992. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4993. a.input = "Hi There";
  4994. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4995. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4996. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4997. "\xfa\x9c\xb6";
  4998. a.inLen = XSTRLEN(a.input);
  4999. a.outLen = WC_SHA384_DIGEST_SIZE;
  5000. b.input = "what do ya want for nothing?";
  5001. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5002. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5003. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5004. "\xb2\x16\x49";
  5005. b.inLen = XSTRLEN(b.input);
  5006. b.outLen = WC_SHA384_DIGEST_SIZE;
  5007. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5008. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5009. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5010. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5011. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5012. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5013. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5014. "\xa3\x4f\x27";
  5015. c.inLen = XSTRLEN(c.input);
  5016. c.outLen = WC_SHA384_DIGEST_SIZE;
  5017. d.input = "Big Key Input";
  5018. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  5019. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  5020. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  5021. "\x57\x41\x69";
  5022. d.inLen = XSTRLEN(d.input);
  5023. d.outLen = WC_SHA384_DIGEST_SIZE;
  5024. test_hmac[0] = a;
  5025. test_hmac[1] = b;
  5026. test_hmac[2] = c;
  5027. test_hmac[3] = d;
  5028. for (i = 0; i < times; ++i) {
  5029. #if defined(HAVE_FIPS)
  5030. if (i == 1)
  5031. continue; /* fips not allowed */
  5032. #endif
  5033. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5034. return WC_TEST_RET_ENC_EC(ret);
  5035. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5036. (word32)XSTRLEN(keys[i]));
  5037. if (ret != 0)
  5038. return WC_TEST_RET_ENC_EC(ret);
  5039. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5040. (word32)test_hmac[i].inLen);
  5041. if (ret != 0)
  5042. return WC_TEST_RET_ENC_EC(ret);
  5043. ret = wc_HmacFinal(&hmac, hash);
  5044. if (ret != 0)
  5045. return WC_TEST_RET_ENC_EC(ret);
  5046. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5047. return WC_TEST_RET_ENC_I(i);
  5048. wc_HmacFree(&hmac);
  5049. }
  5050. #ifndef HAVE_FIPS
  5051. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5052. return WC_TEST_RET_ENC_EC(ret);
  5053. #endif
  5054. return 0;
  5055. }
  5056. #endif
  5057. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5058. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5059. {
  5060. Hmac hmac;
  5061. byte hash[WC_SHA512_DIGEST_SIZE];
  5062. const char* keys[]=
  5063. {
  5064. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5065. "\x0b\x0b\x0b",
  5066. "Jefe",
  5067. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5068. "\xAA\xAA\xAA",
  5069. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5070. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5071. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5072. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5073. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5074. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5075. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5076. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5077. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5078. };
  5079. testVector a, b, c, d;
  5080. testVector test_hmac[4];
  5081. wc_test_ret_t ret;
  5082. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5083. a.input = "Hi There";
  5084. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5085. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5086. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5087. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  5088. "\x3a\x12\x68\x54";
  5089. a.inLen = XSTRLEN(a.input);
  5090. a.outLen = WC_SHA512_DIGEST_SIZE;
  5091. b.input = "what do ya want for nothing?";
  5092. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  5093. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  5094. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  5095. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  5096. "\x38\xbc\xe7\x37";
  5097. b.inLen = XSTRLEN(b.input);
  5098. b.outLen = WC_SHA512_DIGEST_SIZE;
  5099. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5100. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5101. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5102. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5103. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  5104. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  5105. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  5106. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  5107. "\xe1\x32\x92\xfb";
  5108. c.inLen = XSTRLEN(c.input);
  5109. c.outLen = WC_SHA512_DIGEST_SIZE;
  5110. d.input = "Big Key Input";
  5111. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  5112. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  5113. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  5114. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  5115. "\x1e\x18\xfe\xfa";
  5116. d.inLen = XSTRLEN(d.input);
  5117. d.outLen = WC_SHA512_DIGEST_SIZE;
  5118. test_hmac[0] = a;
  5119. test_hmac[1] = b;
  5120. test_hmac[2] = c;
  5121. test_hmac[3] = d;
  5122. for (i = 0; i < times; ++i) {
  5123. #if defined(HAVE_FIPS)
  5124. if (i == 1)
  5125. continue; /* fips not allowed */
  5126. #endif
  5127. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5128. return WC_TEST_RET_ENC_EC(ret);
  5129. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  5130. (word32)XSTRLEN(keys[i]));
  5131. if (ret != 0)
  5132. return WC_TEST_RET_ENC_EC(ret);
  5133. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5134. (word32)test_hmac[i].inLen);
  5135. if (ret != 0)
  5136. return WC_TEST_RET_ENC_EC(ret);
  5137. ret = wc_HmacFinal(&hmac, hash);
  5138. if (ret != 0)
  5139. return WC_TEST_RET_ENC_EC(ret);
  5140. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  5141. return WC_TEST_RET_ENC_I(i);
  5142. wc_HmacFree(&hmac);
  5143. }
  5144. #ifndef HAVE_FIPS
  5145. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  5146. return WC_TEST_RET_ENC_EC(ret);
  5147. #endif
  5148. return 0;
  5149. }
  5150. #endif
  5151. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  5152. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  5153. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  5154. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  5155. {
  5156. Hmac hmac;
  5157. byte hash[WC_SHA3_512_DIGEST_SIZE];
  5158. const char* key[4] =
  5159. {
  5160. "Jefe",
  5161. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5162. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5163. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  5164. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  5165. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5166. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5167. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5168. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5169. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5170. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5171. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5172. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5173. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5174. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5175. };
  5176. const char* input[4] =
  5177. {
  5178. "what do ya want for nothing?",
  5179. "Hi There",
  5180. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5181. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5182. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5183. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5184. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  5185. "Big Key Input"
  5186. };
  5187. const int hashType[4] =
  5188. {
  5189. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  5190. };
  5191. const int hashSz[4] =
  5192. {
  5193. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  5194. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  5195. };
  5196. const char* output[16] =
  5197. {
  5198. /* key = jefe, input = what do ya want for nothing? */
  5199. /* HMAC-SHA3-224 */
  5200. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  5201. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  5202. /* HMAC-SHA3-256 */
  5203. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  5204. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  5205. /* HMAC-SHA3-384 */
  5206. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  5207. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  5208. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  5209. /* HMAC-SHA3-512 */
  5210. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  5211. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  5212. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  5213. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  5214. /* key = 0b..., input = Hi There */
  5215. /* HMAC-SHA3-224 */
  5216. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  5217. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  5218. /* HMAC-SHA3-256 */
  5219. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  5220. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  5221. /* HMAC-SHA3-384 */
  5222. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  5223. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  5224. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  5225. /* HMAC-SHA3-512 */
  5226. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  5227. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  5228. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  5229. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  5230. /* key = aa..., output = dd... */
  5231. /* HMAC-SHA3-224 */
  5232. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  5233. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  5234. /* HMAC-SHA3-256 */
  5235. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  5236. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  5237. /* HMAC-SHA3-384 */
  5238. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  5239. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  5240. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  5241. /* HMAC-SHA3-512 */
  5242. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  5243. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  5244. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  5245. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  5246. /* key = big key, input = Big Key Input */
  5247. /* HMAC-SHA3-224 */
  5248. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  5249. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  5250. /* HMAC-SHA3-256 */
  5251. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  5252. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  5253. /* HMAC-SHA3-384 */
  5254. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  5255. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  5256. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  5257. /* HMAC-SHA3-512 */
  5258. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  5259. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  5260. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  5261. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  5262. };
  5263. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  5264. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  5265. ret;
  5266. #ifdef HAVE_FIPS
  5267. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  5268. * short. Skip it in FIPS builds. */
  5269. i = 1;
  5270. #endif
  5271. for (; i < iMax; i++) {
  5272. for (j = 0; j < jMax; j++) {
  5273. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5274. return WC_TEST_RET_ENC_EC(ret);
  5275. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  5276. (word32)XSTRLEN(key[i]));
  5277. if (ret != 0)
  5278. return WC_TEST_RET_ENC_EC(ret);
  5279. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  5280. (word32)XSTRLEN(input[i]));
  5281. if (ret != 0)
  5282. return WC_TEST_RET_ENC_EC(ret);
  5283. ret = wc_HmacFinal(&hmac, hash);
  5284. if (ret != 0)
  5285. return WC_TEST_RET_ENC_EC(ret);
  5286. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  5287. return WC_TEST_RET_ENC_NC;
  5288. wc_HmacFree(&hmac);
  5289. if (i > 0)
  5290. continue;
  5291. #ifndef HAVE_FIPS
  5292. ret = wc_HmacSizeByType(hashType[j]);
  5293. if (ret != hashSz[j])
  5294. return WC_TEST_RET_ENC_EC(ret);
  5295. #endif
  5296. }
  5297. }
  5298. return 0;
  5299. }
  5300. #endif
  5301. #ifdef WC_RC2
  5302. typedef struct rc2TestVector {
  5303. const char* input;
  5304. const char* output;
  5305. const char* key; /* Key, variable up to 128 bytes */
  5306. const char* iv; /* IV, 8-bytes */
  5307. int inLen;
  5308. int outLen;
  5309. int keyLen;
  5310. int effectiveKeyBits; /* Up to 1024 bits supported */
  5311. } rc2TestVector;
  5312. static wc_test_ret_t rc2_ecb_test(void)
  5313. {
  5314. wc_test_ret_t ret = 0;
  5315. byte cipher[RC2_BLOCK_SIZE];
  5316. byte plain[RC2_BLOCK_SIZE];
  5317. rc2TestVector a, b, c, d, e, f, g, h;
  5318. rc2TestVector test_rc2[8];
  5319. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  5320. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5321. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  5322. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5323. a.inLen = RC2_BLOCK_SIZE;
  5324. a.outLen = RC2_BLOCK_SIZE;
  5325. a.keyLen = 8;
  5326. a.effectiveKeyBits = 63;
  5327. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5328. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  5329. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5330. b.inLen = RC2_BLOCK_SIZE;
  5331. b.outLen = RC2_BLOCK_SIZE;
  5332. b.keyLen = 8;
  5333. b.effectiveKeyBits = 64;
  5334. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  5335. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  5336. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5337. c.inLen = RC2_BLOCK_SIZE;
  5338. c.outLen = RC2_BLOCK_SIZE;
  5339. c.keyLen = 8;
  5340. c.effectiveKeyBits = 64;
  5341. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5342. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  5343. d.key = "\x88";
  5344. d.inLen = RC2_BLOCK_SIZE;
  5345. d.outLen = RC2_BLOCK_SIZE;
  5346. d.keyLen = 1;
  5347. d.effectiveKeyBits = 64;
  5348. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5349. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  5350. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5351. e.inLen = RC2_BLOCK_SIZE;
  5352. e.outLen = RC2_BLOCK_SIZE;
  5353. e.keyLen = 7;
  5354. e.effectiveKeyBits = 64;
  5355. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5356. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  5357. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5358. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5359. f.inLen = RC2_BLOCK_SIZE;
  5360. f.outLen = RC2_BLOCK_SIZE;
  5361. f.keyLen = 16;
  5362. f.effectiveKeyBits = 64;
  5363. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5364. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  5365. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5366. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5367. g.inLen = RC2_BLOCK_SIZE;
  5368. g.outLen = RC2_BLOCK_SIZE;
  5369. g.keyLen = 16;
  5370. g.effectiveKeyBits = 128;
  5371. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5372. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  5373. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5374. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5375. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5376. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5377. "\x1e";
  5378. h.inLen = RC2_BLOCK_SIZE;
  5379. h.outLen = RC2_BLOCK_SIZE;
  5380. h.keyLen = 33;
  5381. h.effectiveKeyBits = 129;
  5382. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  5383. test_rc2[0] = a;
  5384. test_rc2[1] = b;
  5385. test_rc2[2] = c;
  5386. test_rc2[3] = d;
  5387. test_rc2[4] = e;
  5388. test_rc2[5] = f;
  5389. test_rc2[6] = g;
  5390. test_rc2[7] = h;
  5391. for (i = 0; i < times; ++i) {
  5392. Rc2 enc;
  5393. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  5394. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  5395. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  5396. NULL, test_rc2[i].effectiveKeyBits);
  5397. if (ret != 0) {
  5398. return WC_TEST_RET_ENC_EC(ret);
  5399. }
  5400. /* ECB encrypt */
  5401. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  5402. (word32)test_rc2[i].outLen);
  5403. if (ret != 0) {
  5404. return WC_TEST_RET_ENC_EC(ret);
  5405. }
  5406. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  5407. return WC_TEST_RET_ENC_NC;
  5408. }
  5409. /* ECB decrypt */
  5410. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  5411. if (ret != 0) {
  5412. return WC_TEST_RET_ENC_EC(ret);
  5413. }
  5414. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  5415. return WC_TEST_RET_ENC_NC;
  5416. }
  5417. }
  5418. return 0;
  5419. }
  5420. static wc_test_ret_t rc2_cbc_test(void)
  5421. {
  5422. wc_test_ret_t ret = 0;
  5423. byte cipher[128];
  5424. byte plain[128];
  5425. rc2TestVector a, b, c, d, e, f, g, h, i;
  5426. rc2TestVector test_rc2[9];
  5427. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  5428. /* key length = 7, effective key bits = 63 */
  5429. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5430. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5431. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  5432. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  5433. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5434. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5435. a.inLen = RC2_BLOCK_SIZE*2;
  5436. a.outLen = RC2_BLOCK_SIZE*2;
  5437. a.keyLen = 8;
  5438. a.effectiveKeyBits = 63;
  5439. /* key length = 8, effective key bits = 64, all 0xFF */
  5440. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  5441. "\xff\xff\xff\xff\xff\xff\xff\xff";
  5442. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  5443. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  5444. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5445. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5446. b.inLen = RC2_BLOCK_SIZE*2;
  5447. b.outLen = RC2_BLOCK_SIZE*2;
  5448. b.keyLen = 8;
  5449. b.effectiveKeyBits = 64;
  5450. /* key length = 8, effective key bits = 64 */
  5451. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  5452. "\x10\x00\x00\x00\x00\x00\x00\x01";
  5453. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  5454. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  5455. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5456. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5457. c.inLen = RC2_BLOCK_SIZE*2;
  5458. c.outLen = RC2_BLOCK_SIZE*2;
  5459. c.keyLen = 8;
  5460. c.effectiveKeyBits = 64;
  5461. /* key length = 1, effective key bits = 64 */
  5462. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5463. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5464. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  5465. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  5466. d.key = "\x88";
  5467. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5468. d.inLen = RC2_BLOCK_SIZE*2;
  5469. d.outLen = RC2_BLOCK_SIZE*2;
  5470. d.keyLen = 1;
  5471. d.effectiveKeyBits = 64;
  5472. /* key length = 7, effective key bits = 64 */
  5473. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5474. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5475. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  5476. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  5477. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5478. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5479. e.inLen = RC2_BLOCK_SIZE*2;
  5480. e.outLen = RC2_BLOCK_SIZE*2;
  5481. e.keyLen = 7;
  5482. e.effectiveKeyBits = 64;
  5483. /* key length = 16, effective key bits = 64 */
  5484. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5485. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5486. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  5487. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  5488. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5489. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5490. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5491. f.inLen = RC2_BLOCK_SIZE*2;
  5492. f.outLen = RC2_BLOCK_SIZE*2;
  5493. f.keyLen = 16;
  5494. f.effectiveKeyBits = 64;
  5495. /* key length = 16, effective bits = 128 */
  5496. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5497. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5498. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  5499. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  5500. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5501. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5502. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5503. g.inLen = RC2_BLOCK_SIZE*2;
  5504. g.outLen = RC2_BLOCK_SIZE*2;
  5505. g.keyLen = 16;
  5506. g.effectiveKeyBits = 128;
  5507. /* key length = 33, effective bits = 129 */
  5508. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5509. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5510. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  5511. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  5512. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5513. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5514. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5515. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5516. "\x1e";
  5517. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5518. h.inLen = RC2_BLOCK_SIZE*2;
  5519. h.outLen = RC2_BLOCK_SIZE*2;
  5520. h.keyLen = 33;
  5521. h.effectiveKeyBits = 129;
  5522. /* key length = 10, effective bits = 40 */
  5523. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  5524. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  5525. "\x11\x22\x33\x44\x55\x66\x77\x88"
  5526. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  5527. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  5528. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  5529. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  5530. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  5531. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  5532. "\x3E\x96";
  5533. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  5534. i.inLen = RC2_BLOCK_SIZE*4;
  5535. i.outLen = RC2_BLOCK_SIZE*4;
  5536. i.keyLen = 10;
  5537. i.effectiveKeyBits = 40;
  5538. test_rc2[0] = a;
  5539. test_rc2[1] = b;
  5540. test_rc2[2] = c;
  5541. test_rc2[3] = d;
  5542. test_rc2[4] = e;
  5543. test_rc2[5] = f;
  5544. test_rc2[6] = g;
  5545. test_rc2[7] = h;
  5546. test_rc2[8] = i;
  5547. for (j = 0; j < times; ++j) {
  5548. Rc2 rc2;
  5549. XMEMSET(cipher, 0, sizeof(cipher));
  5550. XMEMSET(plain, 0, sizeof(plain));
  5551. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  5552. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  5553. if (ret != 0) {
  5554. return WC_TEST_RET_ENC_EC(ret);
  5555. }
  5556. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  5557. test_rc2[j].inLen);
  5558. if (ret != 0) {
  5559. return WC_TEST_RET_ENC_EC(ret);
  5560. }
  5561. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  5562. return WC_TEST_RET_ENC_NC;
  5563. }
  5564. /* reset IV for decrypt, since overriden by encrypt operation */
  5565. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  5566. if (ret != 0) {
  5567. return WC_TEST_RET_ENC_EC(ret);
  5568. }
  5569. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  5570. if (ret != 0) {
  5571. return WC_TEST_RET_ENC_EC(ret);
  5572. }
  5573. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  5574. return WC_TEST_RET_ENC_NC;
  5575. }
  5576. }
  5577. return 0;
  5578. }
  5579. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  5580. {
  5581. wc_test_ret_t ret = 0;
  5582. ret = rc2_ecb_test();
  5583. if (ret != 0) {
  5584. return ret;
  5585. }
  5586. return rc2_cbc_test();
  5587. }
  5588. #endif
  5589. #ifndef NO_RC4
  5590. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  5591. {
  5592. byte cipher[16];
  5593. byte plain[16];
  5594. wc_test_ret_t ret;
  5595. const char* keys[] =
  5596. {
  5597. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5598. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5599. "\x00\x00\x00\x00\x00\x00\x00\x00",
  5600. "\xef\x01\x23\x45"
  5601. };
  5602. testVector a, b, c, d;
  5603. testVector test_arc4[4];
  5604. int times = sizeof(test_arc4) / sizeof(testVector), i;
  5605. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  5606. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  5607. a.inLen = 8;
  5608. a.outLen = 8;
  5609. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5610. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  5611. b.inLen = 8;
  5612. b.outLen = 8;
  5613. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5614. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  5615. c.inLen = 8;
  5616. c.outLen = 8;
  5617. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  5618. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  5619. d.inLen = 10;
  5620. d.outLen = 10;
  5621. test_arc4[0] = a;
  5622. test_arc4[1] = b;
  5623. test_arc4[2] = c;
  5624. test_arc4[3] = d;
  5625. for (i = 0; i < times; ++i) {
  5626. Arc4 enc;
  5627. Arc4 dec;
  5628. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  5629. if (i == 3)
  5630. keylen = 4;
  5631. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  5632. if (ret != 0)
  5633. return WC_TEST_RET_ENC_EC(ret);
  5634. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  5635. if (ret != 0)
  5636. return WC_TEST_RET_ENC_EC(ret);
  5637. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  5638. if (ret != 0)
  5639. return WC_TEST_RET_ENC_EC(ret);
  5640. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  5641. if (ret != 0)
  5642. return WC_TEST_RET_ENC_EC(ret);
  5643. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5644. (word32)test_arc4[i].outLen);
  5645. if (ret != 0)
  5646. return WC_TEST_RET_ENC_EC(ret);
  5647. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5648. if (ret != 0)
  5649. return WC_TEST_RET_ENC_EC(ret);
  5650. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5651. return WC_TEST_RET_ENC_I(i);
  5652. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5653. return WC_TEST_RET_ENC_I(i);
  5654. wc_Arc4Free(&enc);
  5655. wc_Arc4Free(&dec);
  5656. }
  5657. return 0;
  5658. }
  5659. #endif
  5660. #ifdef HAVE_CHACHA
  5661. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  5662. {
  5663. ChaCha enc;
  5664. ChaCha dec;
  5665. byte cipher[128];
  5666. byte plain[128];
  5667. byte sliver[64];
  5668. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5669. word32 keySz = 32;
  5670. wc_test_ret_t ret = 0;
  5671. int i;
  5672. int times = 4;
  5673. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5674. {
  5675. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5676. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5677. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5678. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5679. };
  5680. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5681. {
  5682. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5683. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5684. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5685. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5686. };
  5687. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5688. {
  5689. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5691. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5692. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5693. };
  5694. /* 128 bit key */
  5695. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5696. {
  5697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5698. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5699. };
  5700. const byte* keys[] = {key1, key2, key3, key4};
  5701. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5702. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5703. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5704. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5705. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5706. #ifndef BENCH_EMBEDDED
  5707. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5708. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5709. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5710. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5711. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5712. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5713. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5714. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5715. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5716. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5717. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5718. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5719. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5720. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5721. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5722. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5723. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5724. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5725. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5726. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5727. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5728. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5729. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5730. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5731. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5732. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5733. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5734. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5735. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5736. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5737. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5738. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5739. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5740. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5741. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5742. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5743. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5744. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5745. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5746. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5747. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5748. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5749. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5750. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5751. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5752. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5753. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5754. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5755. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5756. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5757. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5758. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5759. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5760. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5761. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5762. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5763. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5764. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5765. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5766. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5767. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5768. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5769. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5770. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5771. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5772. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5773. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5774. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5775. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5776. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5777. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5778. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5779. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5780. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5781. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5782. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5783. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5784. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5785. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5786. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5787. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5788. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5789. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5790. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5791. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5792. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5793. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5794. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5795. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5796. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5797. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5798. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5799. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5800. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5801. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5802. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5803. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5804. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5805. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5806. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5807. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5808. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5809. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5810. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5811. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5812. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5813. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5814. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5815. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5816. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5817. };
  5818. #define CHACHA_BIG_TEST_SIZE 1305
  5819. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5820. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5821. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5822. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5823. #else
  5824. byte* cipher_big;
  5825. byte* plain_big;
  5826. byte* input_big;
  5827. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5828. int block_size;
  5829. #endif /* BENCH_EMBEDDED */
  5830. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5831. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5832. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5833. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5834. byte* test_chacha[4];
  5835. test_chacha[0] = a;
  5836. test_chacha[1] = b;
  5837. test_chacha[2] = c;
  5838. test_chacha[3] = d;
  5839. #ifndef BENCH_EMBEDDED
  5840. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5841. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5842. DYNAMIC_TYPE_TMP_BUFFER);
  5843. if (cipher_big == NULL) {
  5844. return MEMORY_E;
  5845. }
  5846. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5847. DYNAMIC_TYPE_TMP_BUFFER);
  5848. if (plain_big == NULL) {
  5849. return MEMORY_E;
  5850. }
  5851. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5852. DYNAMIC_TYPE_TMP_BUFFER);
  5853. if (input_big == NULL) {
  5854. return MEMORY_E;
  5855. }
  5856. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5857. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5858. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5859. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5860. #endif /* BENCH_EMBEDDED */
  5861. for (i = 0; i < times; ++i) {
  5862. if (i < 3) {
  5863. keySz = 32;
  5864. }
  5865. else {
  5866. keySz = 16;
  5867. }
  5868. XMEMCPY(plain, keys[i], keySz);
  5869. XMEMSET(cipher, 0, 32);
  5870. XMEMCPY(cipher + 4, ivs[i], 8);
  5871. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5872. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5873. if (ret != 0)
  5874. return ret;
  5875. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5876. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5877. if (ret != 0)
  5878. return ret;
  5879. XMEMCPY(plain, input, 8);
  5880. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5881. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5882. if (ret != 0)
  5883. return ret;
  5884. if (XMEMCMP(test_chacha[i], cipher, 8))
  5885. return WC_TEST_RET_ENC_I(i);
  5886. if (XMEMCMP(plain, input, 8))
  5887. return WC_TEST_RET_ENC_I(i);
  5888. }
  5889. /* test of starting at a different counter
  5890. encrypts all of the information and decrypts starting at 2nd chunk */
  5891. XMEMSET(plain, 0, sizeof(plain));
  5892. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5893. XMEMSET(cipher, 0, sizeof(cipher));
  5894. XMEMCPY(cipher + 4, ivs[0], 8);
  5895. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5896. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5897. if (ret != 0)
  5898. return ret;
  5899. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5900. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5901. if (ret != 0)
  5902. return ret;
  5903. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5904. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5905. if (ret != 0)
  5906. return ret;
  5907. if (XMEMCMP(plain + 64, sliver, 64))
  5908. return WC_TEST_RET_ENC_NC;
  5909. #ifndef BENCH_EMBEDDED
  5910. /* test of encrypting more data */
  5911. keySz = 32;
  5912. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5913. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5914. if (ret != 0)
  5915. return ret;
  5916. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5917. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5918. if (ret != 0)
  5919. return ret;
  5920. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5921. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5922. CHACHA_BIG_TEST_SIZE);
  5923. if (ret != 0)
  5924. return ret;
  5925. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5926. return WC_TEST_RET_ENC_NC;
  5927. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5928. return WC_TEST_RET_ENC_NC;
  5929. for (i = 0; i < 18; ++i) {
  5930. /* this will test all paths
  5931. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5932. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5933. */
  5934. block_size = (2 << (i%9)) - (i<9?1:0);
  5935. keySz = 32;
  5936. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5937. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5938. if (ret != 0)
  5939. return ret;
  5940. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5941. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5942. if (ret != 0)
  5943. return ret;
  5944. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5945. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5946. if (ret != 0)
  5947. return ret;
  5948. if (XMEMCMP(plain_big, input_big, block_size))
  5949. return WC_TEST_RET_ENC_I(i);
  5950. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5951. return WC_TEST_RET_ENC_I(i);
  5952. }
  5953. /* Streaming test */
  5954. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5955. int j, rem;
  5956. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5957. if (ret != 0)
  5958. return WC_TEST_RET_ENC_EC(ret);
  5959. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5960. if (ret != 0)
  5961. return WC_TEST_RET_ENC_EC(ret);
  5962. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5963. if (ret != 0)
  5964. return WC_TEST_RET_ENC_EC(ret);
  5965. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5966. if (ret != 0)
  5967. return WC_TEST_RET_ENC_EC(ret);
  5968. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5969. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5970. if (ret != 0)
  5971. return WC_TEST_RET_ENC_EC(ret);
  5972. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5973. if (ret != 0)
  5974. return WC_TEST_RET_ENC_EC(ret);
  5975. }
  5976. rem = CHACHA_BIG_TEST_SIZE - j;
  5977. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5978. if (ret != 0)
  5979. return WC_TEST_RET_ENC_EC(ret);
  5980. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5981. if (ret != 0)
  5982. return WC_TEST_RET_ENC_EC(ret);
  5983. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5984. return WC_TEST_RET_ENC_NC;
  5985. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5986. return WC_TEST_RET_ENC_NC;
  5987. }
  5988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5989. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5990. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5991. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5992. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5993. #endif /* BENCH_EMBEDDED */
  5994. return 0;
  5995. }
  5996. #endif /* HAVE_CHACHA */
  5997. #ifdef HAVE_POLY1305
  5998. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  5999. {
  6000. wc_test_ret_t ret = 0;
  6001. int i;
  6002. byte tag[16];
  6003. Poly1305 enc;
  6004. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6005. {
  6006. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6007. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6008. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6009. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6010. 0x75,0x70
  6011. };
  6012. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6013. {
  6014. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6015. 0x6c,0x64,0x21
  6016. };
  6017. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6018. {
  6019. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6020. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6021. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6022. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6023. };
  6024. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6025. {
  6026. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6027. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6028. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6029. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6030. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6031. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6032. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6033. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6034. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6035. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6036. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6037. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6038. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6039. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6040. 0x61,0x16
  6041. };
  6042. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6043. {
  6044. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6045. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6046. };
  6047. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6048. {
  6049. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6050. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6051. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6052. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6053. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6054. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6055. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6056. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6057. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6058. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6059. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6060. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6061. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6062. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6063. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6064. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6065. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6066. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6067. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6068. 0x61,0x16
  6069. };
  6070. byte additional[] =
  6071. {
  6072. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6073. 0xc4,0xc5,0xc6,0xc7
  6074. };
  6075. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6076. {
  6077. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6078. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6079. };
  6080. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6081. {
  6082. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6083. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6084. };
  6085. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  6086. {
  6087. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  6088. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  6089. };
  6090. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  6091. {
  6092. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  6093. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  6094. };
  6095. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  6096. {
  6097. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  6098. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  6099. };
  6100. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  6101. {
  6102. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6104. };
  6105. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  6106. {
  6107. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  6108. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  6109. };
  6110. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  6111. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  6112. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  6113. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6114. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6115. };
  6116. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6117. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  6118. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  6119. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  6120. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  6121. };
  6122. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  6123. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  6124. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  6125. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  6126. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  6127. };
  6128. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  6129. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6130. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6133. };
  6134. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  6135. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  6136. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  6137. const byte* keys[] = {key, key, key2, key2, key5, key};
  6138. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  6139. correct6};
  6140. for (i = 0; i < 6; i++) {
  6141. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  6142. if (ret != 0)
  6143. return WC_TEST_RET_ENC_I(i);
  6144. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  6145. if (ret != 0)
  6146. return WC_TEST_RET_ENC_I(i);
  6147. ret = wc_Poly1305Final(&enc, tag);
  6148. if (ret != 0)
  6149. return WC_TEST_RET_ENC_I(i);
  6150. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  6151. return WC_TEST_RET_ENC_I(i);
  6152. }
  6153. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  6154. XMEMSET(tag, 0, sizeof(tag));
  6155. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  6156. if (ret != 0)
  6157. return WC_TEST_RET_ENC_EC(ret);
  6158. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6159. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6160. if (ret != 0)
  6161. return WC_TEST_RET_ENC_EC(ret);
  6162. if (XMEMCMP(tag, correct4, sizeof(tag)))
  6163. return WC_TEST_RET_ENC_NC;
  6164. /* Check fail of TLS MAC function if altering additional data */
  6165. XMEMSET(tag, 0, sizeof(tag));
  6166. additional[0]++;
  6167. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6168. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6169. if (ret != 0)
  6170. return WC_TEST_RET_ENC_EC(ret);
  6171. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  6172. return WC_TEST_RET_ENC_NC;
  6173. return 0;
  6174. }
  6175. #endif /* HAVE_POLY1305 */
  6176. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  6177. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  6178. {
  6179. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  6180. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6181. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  6182. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  6183. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  6184. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  6185. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  6186. };
  6187. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  6188. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  6189. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  6190. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  6191. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  6192. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  6193. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  6194. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  6195. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  6196. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  6197. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  6198. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  6199. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  6200. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  6201. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  6202. 0x74, 0x2e
  6203. };
  6204. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  6205. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  6206. 0x44, 0x45, 0x46, 0x47
  6207. };
  6208. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  6209. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  6210. 0xc4, 0xc5, 0xc6, 0xc7
  6211. };
  6212. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  6213. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  6214. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  6215. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  6216. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  6217. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  6218. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  6219. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  6220. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  6221. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  6222. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  6223. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  6224. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  6225. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  6226. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  6227. 0x61, 0x16
  6228. };
  6229. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  6230. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  6231. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  6232. };
  6233. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  6234. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6235. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6236. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  6237. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  6238. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  6239. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  6240. };
  6241. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  6242. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  6243. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  6244. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  6245. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6246. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  6247. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  6248. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  6249. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  6250. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  6251. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  6252. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  6253. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  6254. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  6255. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  6256. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  6257. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6258. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  6259. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  6260. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  6261. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  6262. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  6263. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  6264. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  6265. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  6266. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  6267. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  6268. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  6269. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  6270. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  6271. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  6272. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  6273. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  6274. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  6275. 0x9d
  6276. };
  6277. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6278. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  6279. 0x05, 0x06, 0x07, 0x08
  6280. };
  6281. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  6282. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  6283. 0x00, 0x00, 0x4e, 0x91
  6284. };
  6285. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  6286. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  6287. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  6288. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  6289. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  6290. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  6291. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  6292. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  6293. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  6294. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  6295. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  6296. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  6297. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  6298. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  6299. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  6300. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  6301. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  6302. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  6303. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  6304. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  6305. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  6306. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  6307. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  6308. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  6309. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  6310. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  6311. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  6312. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  6313. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  6314. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  6315. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  6316. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  6317. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  6318. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  6319. 0x9b
  6320. };
  6321. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  6322. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  6323. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  6324. };
  6325. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  6326. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  6327. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  6328. wc_test_ret_t err;
  6329. ChaChaPoly_Aead aead;
  6330. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  6331. #define TEST_SMALL_CHACHA_CHUNKS 32
  6332. #else
  6333. #define TEST_SMALL_CHACHA_CHUNKS 64
  6334. #endif
  6335. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6336. word32 testLen;
  6337. #endif
  6338. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6339. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6340. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6341. /* Parameter Validation testing */
  6342. /* Encrypt */
  6343. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  6344. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6345. if (err != BAD_FUNC_ARG)
  6346. return WC_TEST_RET_ENC_EC(err);
  6347. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  6348. plaintext1, sizeof(plaintext1), generatedCiphertext,
  6349. generatedAuthTag);
  6350. if (err != BAD_FUNC_ARG)
  6351. return WC_TEST_RET_ENC_EC(err);
  6352. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6353. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6354. if (err != BAD_FUNC_ARG)
  6355. return WC_TEST_RET_ENC_EC(err);
  6356. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6357. sizeof(plaintext1), NULL, generatedAuthTag);
  6358. if (err != BAD_FUNC_ARG)
  6359. return WC_TEST_RET_ENC_EC(err);
  6360. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6361. sizeof(plaintext1), generatedCiphertext, NULL);
  6362. if (err != BAD_FUNC_ARG)
  6363. return WC_TEST_RET_ENC_EC(err);
  6364. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6365. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6366. if (err != BAD_FUNC_ARG)
  6367. return WC_TEST_RET_ENC_EC(err);
  6368. /* Decrypt */
  6369. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  6370. sizeof(cipher2), authTag2, generatedPlaintext);
  6371. if (err != BAD_FUNC_ARG)
  6372. return WC_TEST_RET_ENC_EC(err);
  6373. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  6374. sizeof(cipher2), authTag2, generatedPlaintext);
  6375. if (err != BAD_FUNC_ARG)
  6376. return WC_TEST_RET_ENC_EC(err);
  6377. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6378. sizeof(cipher2), authTag2, generatedPlaintext);
  6379. if (err != BAD_FUNC_ARG)
  6380. return WC_TEST_RET_ENC_EC(err);
  6381. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6382. sizeof(cipher2), NULL, generatedPlaintext);
  6383. if (err != BAD_FUNC_ARG)
  6384. return WC_TEST_RET_ENC_EC(err);
  6385. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6386. sizeof(cipher2), authTag2, NULL);
  6387. if (err != BAD_FUNC_ARG)
  6388. return WC_TEST_RET_ENC_EC(err);
  6389. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6390. sizeof(cipher2), authTag2, generatedPlaintext);
  6391. if (err != BAD_FUNC_ARG)
  6392. return WC_TEST_RET_ENC_EC(err);
  6393. /* Test #1 */
  6394. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  6395. aad1, sizeof(aad1),
  6396. plaintext1, sizeof(plaintext1),
  6397. generatedCiphertext, generatedAuthTag);
  6398. if (err) {
  6399. return WC_TEST_RET_ENC_EC(err);
  6400. }
  6401. /* -- Check the ciphertext and authtag */
  6402. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6403. return WC_TEST_RET_ENC_NC;
  6404. }
  6405. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  6406. return WC_TEST_RET_ENC_NC;
  6407. }
  6408. /* -- Verify decryption works */
  6409. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  6410. aad1, sizeof(aad1),
  6411. cipher1, sizeof(cipher1),
  6412. authTag1, generatedPlaintext);
  6413. if (err) {
  6414. return err;
  6415. }
  6416. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6417. return WC_TEST_RET_ENC_NC;
  6418. }
  6419. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6420. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6421. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6422. /* Test #2 */
  6423. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  6424. aad2, sizeof(aad2),
  6425. plaintext2, sizeof(plaintext2),
  6426. generatedCiphertext, generatedAuthTag);
  6427. if (err) {
  6428. return err;
  6429. }
  6430. /* -- Check the ciphertext and authtag */
  6431. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6432. return WC_TEST_RET_ENC_NC;
  6433. }
  6434. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  6435. return WC_TEST_RET_ENC_NC;
  6436. }
  6437. /* -- Verify decryption works */
  6438. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  6439. aad2, sizeof(aad2),
  6440. cipher2, sizeof(cipher2),
  6441. authTag2, generatedPlaintext);
  6442. if (err) {
  6443. return err;
  6444. }
  6445. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6446. return WC_TEST_RET_ENC_NC;
  6447. }
  6448. /* AEAD init/update/final - bad argument tests */
  6449. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  6450. CHACHA20_POLY1305_AEAD_DECRYPT);
  6451. if (err != BAD_FUNC_ARG)
  6452. return WC_TEST_RET_ENC_EC(err);
  6453. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  6454. CHACHA20_POLY1305_AEAD_DECRYPT);
  6455. if (err != BAD_FUNC_ARG)
  6456. return WC_TEST_RET_ENC_EC(err);
  6457. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  6458. CHACHA20_POLY1305_AEAD_DECRYPT);
  6459. if (err != BAD_FUNC_ARG)
  6460. return WC_TEST_RET_ENC_EC(err);
  6461. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  6462. if (err != BAD_FUNC_ARG)
  6463. return WC_TEST_RET_ENC_EC(err);
  6464. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  6465. if (err != BAD_FUNC_ARG)
  6466. return WC_TEST_RET_ENC_EC(err);
  6467. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  6468. generatedPlaintext, sizeof(plaintext1));
  6469. if (err != BAD_FUNC_ARG)
  6470. return WC_TEST_RET_ENC_EC(err);
  6471. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  6472. sizeof(plaintext1));
  6473. if (err != BAD_FUNC_ARG)
  6474. return WC_TEST_RET_ENC_EC(err);
  6475. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  6476. sizeof(plaintext1));
  6477. if (err != BAD_FUNC_ARG)
  6478. return WC_TEST_RET_ENC_EC(err);
  6479. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  6480. if (err != BAD_FUNC_ARG)
  6481. return WC_TEST_RET_ENC_EC(err);
  6482. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  6483. if (err != BAD_FUNC_ARG)
  6484. return WC_TEST_RET_ENC_EC(err);
  6485. /* AEAD init/update/final - bad state tests */
  6486. /* clear struct - make valgrind happy to resolve
  6487. "Conditional jump or move depends on uninitialised value(s)".
  6488. The enum is "int" size and aead.state is "byte" */
  6489. /* The wc_ChaCha20Poly1305_Init function does this normally */
  6490. XMEMSET(&aead, 0, sizeof(aead));
  6491. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6492. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6493. if (err != BAD_STATE_E)
  6494. return WC_TEST_RET_ENC_EC(err);
  6495. aead.state = CHACHA20_POLY1305_STATE_DATA;
  6496. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6497. if (err != BAD_STATE_E)
  6498. return WC_TEST_RET_ENC_EC(err);
  6499. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6500. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  6501. generatedPlaintext, sizeof(plaintext1));
  6502. if (err != BAD_STATE_E)
  6503. return WC_TEST_RET_ENC_EC(err);
  6504. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6505. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6506. if (err != BAD_STATE_E)
  6507. return WC_TEST_RET_ENC_EC(err);
  6508. aead.state = CHACHA20_POLY1305_STATE_READY;
  6509. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6510. if (err != BAD_STATE_E)
  6511. return WC_TEST_RET_ENC_EC(err);
  6512. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6513. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6514. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6515. /* Test 1 - Encrypt */
  6516. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6517. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6518. if (err != 0)
  6519. return WC_TEST_RET_ENC_EC(err);
  6520. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6521. if (err != 0)
  6522. return WC_TEST_RET_ENC_EC(err);
  6523. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6524. /* test doing data in smaller chunks */
  6525. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6526. word32 dataLen = sizeof(plaintext1) - testLen;
  6527. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6528. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6529. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  6530. &generatedCiphertext[testLen], dataLen);
  6531. if (err != 0)
  6532. return WC_TEST_RET_ENC_EC(err);
  6533. testLen += dataLen;
  6534. }
  6535. #else
  6536. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  6537. generatedCiphertext, sizeof(plaintext1));
  6538. #endif
  6539. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6540. if (err != 0)
  6541. return WC_TEST_RET_ENC_EC(err);
  6542. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6543. if (err != 0)
  6544. return WC_TEST_RET_ENC_EC(err);
  6545. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6546. return WC_TEST_RET_ENC_NC;
  6547. }
  6548. /* Test 1 - Decrypt */
  6549. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6550. CHACHA20_POLY1305_AEAD_DECRYPT);
  6551. if (err != 0)
  6552. return WC_TEST_RET_ENC_EC(err);
  6553. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6554. if (err != 0)
  6555. return WC_TEST_RET_ENC_EC(err);
  6556. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6557. /* test doing data in smaller chunks */
  6558. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6559. word32 dataLen = sizeof(plaintext1) - testLen;
  6560. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6561. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6562. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6563. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6564. dataLen);
  6565. if (err != 0)
  6566. return WC_TEST_RET_ENC_EC(err);
  6567. testLen += dataLen;
  6568. }
  6569. #else
  6570. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6571. generatedPlaintext, sizeof(cipher1));
  6572. #endif
  6573. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6574. if (err != 0)
  6575. return WC_TEST_RET_ENC_EC(err);
  6576. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6577. if (err != 0)
  6578. return WC_TEST_RET_ENC_EC(err);
  6579. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6580. return WC_TEST_RET_ENC_NC;
  6581. }
  6582. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6583. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6584. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6585. /* Test 2 - Encrypt */
  6586. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6587. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6588. if (err != 0)
  6589. return WC_TEST_RET_ENC_EC(err);
  6590. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6591. if (err != 0)
  6592. return WC_TEST_RET_ENC_EC(err);
  6593. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6594. /* test doing data in smaller chunks */
  6595. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6596. word32 dataLen = sizeof(plaintext2) - testLen;
  6597. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6598. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6599. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  6600. &generatedCiphertext[testLen], dataLen);
  6601. if (err != 0)
  6602. return WC_TEST_RET_ENC_EC(err);
  6603. testLen += dataLen;
  6604. }
  6605. #else
  6606. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  6607. sizeof(plaintext2));
  6608. #endif
  6609. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6610. if (err != 0)
  6611. return WC_TEST_RET_ENC_EC(err);
  6612. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6613. if (err != 0)
  6614. return WC_TEST_RET_ENC_EC(err);
  6615. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6616. return WC_TEST_RET_ENC_NC;
  6617. }
  6618. /* Test 2 - Decrypt */
  6619. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6620. CHACHA20_POLY1305_AEAD_DECRYPT);
  6621. if (err != 0)
  6622. return WC_TEST_RET_ENC_EC(err);
  6623. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6624. if (err != 0)
  6625. return WC_TEST_RET_ENC_EC(err);
  6626. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6627. /* test doing data in smaller chunks */
  6628. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6629. word32 dataLen = sizeof(plaintext2) - testLen;
  6630. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6631. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6632. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6633. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6634. dataLen);
  6635. if (err != 0)
  6636. return WC_TEST_RET_ENC_EC(err);
  6637. testLen += dataLen;
  6638. }
  6639. #else
  6640. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6641. generatedPlaintext, sizeof(cipher2));
  6642. #endif
  6643. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6644. if (err != 0)
  6645. return WC_TEST_RET_ENC_EC(err);
  6646. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6647. if (err != 0)
  6648. return WC_TEST_RET_ENC_EC(err);
  6649. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6650. return WC_TEST_RET_ENC_NC;
  6651. }
  6652. return err;
  6653. }
  6654. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6655. #ifndef NO_DES3
  6656. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  6657. {
  6658. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6659. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6660. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6661. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6662. };
  6663. byte plain[24];
  6664. byte cipher[24];
  6665. Des enc;
  6666. Des dec;
  6667. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6668. {
  6669. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6670. };
  6671. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6672. {
  6673. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6674. };
  6675. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6676. {
  6677. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6678. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6679. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6680. };
  6681. wc_test_ret_t ret;
  6682. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6683. if (ret != 0)
  6684. return WC_TEST_RET_ENC_EC(ret);
  6685. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6686. if (ret != 0)
  6687. return WC_TEST_RET_ENC_EC(ret);
  6688. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6689. if (ret != 0)
  6690. return WC_TEST_RET_ENC_EC(ret);
  6691. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6692. if (ret != 0)
  6693. return WC_TEST_RET_ENC_EC(ret);
  6694. if (XMEMCMP(plain, vector, sizeof(plain)))
  6695. return WC_TEST_RET_ENC_NC;
  6696. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6697. return WC_TEST_RET_ENC_NC;
  6698. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6699. if (ret != 0)
  6700. return WC_TEST_RET_ENC_EC(ret);
  6701. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6702. {
  6703. EncryptedInfo info;
  6704. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6705. XMEMCPY(info.iv, iv, sizeof(iv));
  6706. info.ivSz = sizeof(iv);
  6707. info.keySz = sizeof(key);
  6708. info.cipherType = WC_CIPHER_DES;
  6709. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6710. sizeof(key), WC_HASH_TYPE_SHA);
  6711. if (ret != 0)
  6712. return WC_TEST_RET_ENC_EC(ret);
  6713. /* Test invalid info ptr */
  6714. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6715. sizeof(key), WC_HASH_TYPE_SHA);
  6716. if (ret != BAD_FUNC_ARG)
  6717. return WC_TEST_RET_ENC_EC(ret);
  6718. #ifndef NO_PWDBASED
  6719. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6720. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6721. sizeof(key), WC_HASH_TYPE_NONE);
  6722. if (ret == 0)
  6723. return WC_TEST_RET_ENC_EC(ret);
  6724. #endif /* !NO_PWDBASED */
  6725. }
  6726. #endif
  6727. return 0;
  6728. }
  6729. #endif /* !NO_DES3 */
  6730. #ifndef NO_DES3
  6731. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  6732. {
  6733. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6734. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6735. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6736. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6737. };
  6738. byte plain[24];
  6739. byte cipher[24];
  6740. Des3 enc;
  6741. Des3 dec;
  6742. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6743. {
  6744. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6745. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6746. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6747. };
  6748. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6749. {
  6750. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6751. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6752. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6753. };
  6754. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6755. {
  6756. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6757. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6758. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6759. };
  6760. wc_test_ret_t ret;
  6761. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6762. size_t i;
  6763. #endif
  6764. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  6765. if (ret != 0)
  6766. return WC_TEST_RET_ENC_EC(ret);
  6767. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  6768. if (ret != 0)
  6769. return WC_TEST_RET_ENC_EC(ret);
  6770. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6771. if (ret != 0)
  6772. return WC_TEST_RET_ENC_EC(ret);
  6773. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6774. if (ret != 0)
  6775. return WC_TEST_RET_ENC_EC(ret);
  6776. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6777. #if defined(WOLFSSL_ASYNC_CRYPT)
  6778. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6779. #endif
  6780. if (ret != 0)
  6781. return WC_TEST_RET_ENC_EC(ret);
  6782. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6783. #if defined(WOLFSSL_ASYNC_CRYPT)
  6784. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6785. #endif
  6786. if (ret != 0)
  6787. return WC_TEST_RET_ENC_EC(ret);
  6788. if (XMEMCMP(plain, vector, sizeof(plain)))
  6789. return WC_TEST_RET_ENC_NC;
  6790. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6791. return WC_TEST_RET_ENC_NC;
  6792. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6793. /* test the same vectors with using compatibility layer */
  6794. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  6795. DES_key_schedule ks1;
  6796. DES_key_schedule ks2;
  6797. DES_key_schedule ks3;
  6798. DES_cblock iv4;
  6799. byte tmp[sizeof(vector)];
  6800. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6801. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6802. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6803. XMEMSET(plain, 0, sizeof(plain));
  6804. XMEMSET(cipher, 0, sizeof(cipher));
  6805. /* Test in-place encrypt/decrypt */
  6806. XMEMCPY(tmp, vector, sizeof(vector));
  6807. /* Use i as the splitter */
  6808. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6809. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6810. &iv4, DES_ENCRYPT);
  6811. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  6812. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  6813. XMEMCPY(cipher, tmp, sizeof(cipher));
  6814. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6815. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6816. &iv4, DES_DECRYPT);
  6817. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  6818. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  6819. XMEMCPY(plain, tmp, sizeof(plain));
  6820. if (XMEMCMP(plain, vector, sizeof(plain)))
  6821. return WC_TEST_RET_ENC_NC;
  6822. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6823. return WC_TEST_RET_ENC_NC;
  6824. }
  6825. #endif /* OPENSSL_EXTRA */
  6826. wc_Des3Free(&enc);
  6827. wc_Des3Free(&dec);
  6828. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6829. {
  6830. EncryptedInfo info;
  6831. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6832. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6833. info.ivSz = sizeof(iv3);
  6834. info.keySz = sizeof(key3);
  6835. info.cipherType = WC_CIPHER_DES3;
  6836. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6837. sizeof(key3), WC_HASH_TYPE_SHA);
  6838. if (ret != 0)
  6839. return WC_TEST_RET_ENC_EC(ret);
  6840. }
  6841. #endif
  6842. return 0;
  6843. }
  6844. #endif /* NO_DES3 */
  6845. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  6846. * see print_fiducials() below.
  6847. */
  6848. #ifndef NO_AES
  6849. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6850. defined(WOLFSSL_AES_XTS)
  6851. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6852. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6853. /* pass in the function, key, iv, plain text and expected and this function
  6854. * tests that the encryption and decryption is successful */
  6855. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6856. const byte* iv, const byte* plain, int plainSz,
  6857. const byte* expected, int expectedSz)
  6858. {
  6859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6860. EVP_CIPHER_CTX *ctx = NULL;
  6861. #else
  6862. EVP_CIPHER_CTX ctx[1];
  6863. #endif
  6864. int idx, cipherSz;
  6865. wc_test_ret_t ret = 0;
  6866. byte* cipher;
  6867. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6868. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6869. return MEMORY_E;
  6870. #endif
  6871. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6872. if (cipher == NULL) {
  6873. ret = WC_TEST_RET_ENC_ERRNO;
  6874. goto EVP_TEST_END;
  6875. }
  6876. /* test encrypt */
  6877. EVP_CIPHER_CTX_init(ctx);
  6878. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6879. ret = WC_TEST_RET_ENC_NC;
  6880. goto EVP_TEST_END;
  6881. }
  6882. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6883. ret = WC_TEST_RET_ENC_NC;
  6884. goto EVP_TEST_END;
  6885. }
  6886. cipherSz = idx;
  6887. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6888. ret = WC_TEST_RET_ENC_NC;
  6889. goto EVP_TEST_END;
  6890. }
  6891. cipherSz += idx;
  6892. if (XMEMCMP(cipher, expected, plainSz)) {
  6893. ret = WC_TEST_RET_ENC_NC;
  6894. goto EVP_TEST_END;
  6895. }
  6896. /* test decrypt */
  6897. EVP_CIPHER_CTX_init(ctx);
  6898. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6899. ret = WC_TEST_RET_ENC_NC;
  6900. goto EVP_TEST_END;
  6901. }
  6902. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6903. ret = WC_TEST_RET_ENC_NC;
  6904. goto EVP_TEST_END;
  6905. }
  6906. cipherSz = idx;
  6907. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6908. ret = WC_TEST_RET_ENC_NC;
  6909. goto EVP_TEST_END;
  6910. }
  6911. cipherSz += idx;
  6912. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6913. ret = WC_TEST_RET_ENC_NC;
  6914. goto EVP_TEST_END;
  6915. }
  6916. EVP_TEST_END:
  6917. if (cipher)
  6918. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6919. (void)cipherSz;
  6920. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6921. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6922. #endif
  6923. return ret;
  6924. }
  6925. #endif /* OPENSSL_EXTRA */
  6926. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6927. #ifdef WOLFSSL_AES_OFB
  6928. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6929. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  6930. {
  6931. #ifdef WOLFSSL_AES_256
  6932. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6933. {
  6934. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6935. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6936. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6937. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6938. };
  6939. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6940. {
  6941. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6942. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6943. };
  6944. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6945. {
  6946. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6947. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6948. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6949. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6950. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6951. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6952. };
  6953. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6954. {
  6955. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6956. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6957. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6958. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6959. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6960. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6961. };
  6962. #endif /* WOLFSSL_AES_256 */
  6963. #ifdef WOLFSSL_AES_128
  6964. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6965. {
  6966. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6967. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6968. };
  6969. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6970. {
  6971. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6973. };
  6974. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6975. {
  6976. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6977. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6978. };
  6979. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6980. {
  6981. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6982. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6983. };
  6984. #endif /* WOLFSSL_AES_128 */
  6985. #ifdef WOLFSSL_AES_192
  6986. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6987. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6988. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6989. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6990. };
  6991. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6992. {
  6993. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6994. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6995. };
  6996. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6997. {
  6998. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6999. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7000. };
  7001. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7002. {
  7003. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7004. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7005. };
  7006. #endif /* WOLFSSL_AES_192 */
  7007. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7008. Aes *enc = NULL;
  7009. #else
  7010. Aes enc[1];
  7011. #endif
  7012. byte cipher[AES_BLOCK_SIZE * 4];
  7013. #ifdef HAVE_AES_DECRYPT
  7014. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7015. Aes *dec = NULL;
  7016. #else
  7017. Aes dec[1];
  7018. #endif
  7019. byte plain [AES_BLOCK_SIZE * 4];
  7020. #endif
  7021. wc_test_ret_t ret = 0;
  7022. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7023. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7024. ERROR_OUT(-1, out);
  7025. #ifdef HAVE_AES_DECRYPT
  7026. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7027. ERROR_OUT(-1, out);
  7028. #endif
  7029. #endif
  7030. XMEMSET(enc, 0, sizeof *enc);
  7031. #ifdef HAVE_AES_DECRYPT
  7032. XMEMSET(dec, 0, sizeof *dec);
  7033. #endif
  7034. #ifdef WOLFSSL_AES_128
  7035. /* 128 key size test */
  7036. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7037. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7038. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7039. cipher2, sizeof(cipher2));
  7040. if (ret != 0) {
  7041. goto out;
  7042. }
  7043. #endif
  7044. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7045. if (ret != 0)
  7046. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7047. #ifdef HAVE_AES_DECRYPT
  7048. /* decrypt uses AES_ENCRYPTION */
  7049. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7050. if (ret != 0)
  7051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7052. #endif
  7053. XMEMSET(cipher, 0, sizeof(cipher));
  7054. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  7055. if (ret != 0)
  7056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7057. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  7058. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7059. #ifdef HAVE_AES_DECRYPT
  7060. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  7061. if (ret != 0)
  7062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7063. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  7064. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7065. #endif /* HAVE_AES_DECRYPT */
  7066. #endif /* WOLFSSL_AES_128 */
  7067. #ifdef WOLFSSL_AES_192
  7068. /* 192 key size test */
  7069. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7070. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7071. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  7072. cipher3, sizeof(cipher3));
  7073. if (ret != 0) {
  7074. goto out;
  7075. }
  7076. #endif
  7077. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7078. if (ret != 0)
  7079. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7080. #ifdef HAVE_AES_DECRYPT
  7081. /* decrypt uses AES_ENCRYPTION */
  7082. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7083. if (ret != 0)
  7084. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7085. #endif
  7086. XMEMSET(cipher, 0, sizeof(cipher));
  7087. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  7088. if (ret != 0)
  7089. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7090. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  7091. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7092. #ifdef HAVE_AES_DECRYPT
  7093. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  7094. if (ret != 0)
  7095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7096. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  7097. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7098. #endif /* HAVE_AES_DECRYPT */
  7099. #endif /* WOLFSSL_AES_192 */
  7100. #ifdef WOLFSSL_AES_256
  7101. /* 256 key size test */
  7102. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7103. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7104. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  7105. cipher1, sizeof(cipher1));
  7106. if (ret != 0) {
  7107. goto out;
  7108. }
  7109. #endif
  7110. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7111. if (ret != 0)
  7112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7113. #ifdef HAVE_AES_DECRYPT
  7114. /* decrypt uses AES_ENCRYPTION */
  7115. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7116. if (ret != 0)
  7117. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7118. #endif
  7119. XMEMSET(cipher, 0, sizeof(cipher));
  7120. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  7121. if (ret != 0)
  7122. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7123. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  7124. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7125. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7126. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7127. if (ret != 0)
  7128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7129. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  7130. AES_BLOCK_SIZE))
  7131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7132. #ifdef HAVE_AES_DECRYPT
  7133. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  7134. if (ret != 0)
  7135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7136. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  7137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7138. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  7139. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7140. if (ret != 0)
  7141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7142. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  7143. AES_BLOCK_SIZE))
  7144. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7145. #endif /* HAVE_AES_DECRYPT */
  7146. /* multiple blocks at once */
  7147. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7148. if (ret != 0)
  7149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7150. #ifdef HAVE_AES_DECRYPT
  7151. /* decrypt uses AES_ENCRYPTION */
  7152. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7153. if (ret != 0)
  7154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7155. #endif
  7156. XMEMSET(cipher, 0, sizeof(cipher));
  7157. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  7158. if (ret != 0)
  7159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7160. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  7161. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7162. #ifdef HAVE_AES_DECRYPT
  7163. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  7164. if (ret != 0)
  7165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7166. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  7167. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7168. #endif /* HAVE_AES_DECRYPT */
  7169. /* inline decrypt/encrypt*/
  7170. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7171. if (ret != 0)
  7172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7173. #ifdef HAVE_AES_DECRYPT
  7174. /* decrypt uses AES_ENCRYPTION */
  7175. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7176. if (ret != 0)
  7177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7178. #endif
  7179. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  7180. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  7181. if (ret != 0)
  7182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7183. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7185. #ifdef HAVE_AES_DECRYPT
  7186. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  7187. if (ret != 0)
  7188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7189. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  7190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7191. #endif /* HAVE_AES_DECRYPT */
  7192. /* 256 key size test leftover support */
  7193. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7194. if (ret != 0)
  7195. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7196. #ifdef HAVE_AES_DECRYPT
  7197. /* decrypt uses AES_ENCRYPTION */
  7198. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7199. if (ret != 0)
  7200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7201. #endif
  7202. XMEMSET(cipher, 0, sizeof(cipher));
  7203. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  7204. if (ret != 0)
  7205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7206. if (XMEMCMP(cipher, cipher1, 3))
  7207. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7208. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  7209. if (ret != 0)
  7210. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7211. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  7212. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7213. #ifdef HAVE_AES_DECRYPT
  7214. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  7215. if (ret != 0)
  7216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7217. if (XMEMCMP(plain, plain1, 6))
  7218. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7219. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  7220. if (ret != 0)
  7221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7222. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  7223. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7224. #endif /* HAVE_AES_DECRYPT */
  7225. out:
  7226. wc_AesFree(enc);
  7227. wc_AesFree(dec);
  7228. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7229. if (enc)
  7230. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7231. #ifdef HAVE_AES_DECRYPT
  7232. if (dec)
  7233. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7234. #endif
  7235. #endif
  7236. #endif /* WOLFSSL_AES_256 */
  7237. return ret;
  7238. }
  7239. #endif /* WOLFSSL_AES_OFB */
  7240. #if defined(WOLFSSL_AES_CFB)
  7241. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  7242. static wc_test_ret_t aescfb_test(void)
  7243. {
  7244. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7245. Aes *enc = NULL;
  7246. #else
  7247. Aes enc[1];
  7248. #endif
  7249. int enc_inited = 0;
  7250. byte cipher[AES_BLOCK_SIZE * 4];
  7251. #ifdef HAVE_AES_DECRYPT
  7252. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7253. Aes *dec = NULL;
  7254. #else
  7255. Aes dec[1];
  7256. #endif
  7257. int dec_inited = 0;
  7258. byte plain [AES_BLOCK_SIZE * 4];
  7259. #endif
  7260. wc_test_ret_t ret = 0;
  7261. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7262. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7263. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  7264. };
  7265. #ifdef WOLFSSL_AES_128
  7266. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7267. {
  7268. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7269. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7270. };
  7271. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7272. {
  7273. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  7274. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  7275. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  7276. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  7277. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  7278. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  7279. };
  7280. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7281. {
  7282. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7283. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7284. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7285. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7286. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7287. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  7288. };
  7289. #endif /* WOLFSSL_AES_128 */
  7290. #ifdef WOLFSSL_AES_192
  7291. /* 192 size key test */
  7292. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7293. {
  7294. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7295. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7296. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7297. };
  7298. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7299. {
  7300. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  7301. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  7302. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  7303. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  7304. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  7305. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  7306. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  7307. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  7308. };
  7309. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7310. {
  7311. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7312. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7313. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7314. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7315. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7316. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7317. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7318. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7319. };
  7320. #endif /* WOLFSSL_AES_192 */
  7321. #ifdef WOLFSSL_AES_256
  7322. /* 256 size key simple test */
  7323. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7324. {
  7325. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7326. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7327. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7328. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7329. };
  7330. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7331. {
  7332. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  7333. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  7334. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  7335. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  7336. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  7337. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  7338. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  7339. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  7340. };
  7341. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7342. {
  7343. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7344. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7345. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7346. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7347. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7348. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7349. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7350. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7351. };
  7352. #endif /* WOLFSSL_AES_256 */
  7353. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7354. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7355. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7356. #ifdef HAVE_AES_DECRYPT
  7357. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7358. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7359. #endif
  7360. #endif
  7361. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7362. if (ret != 0)
  7363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7364. else
  7365. enc_inited = 1;
  7366. #ifdef HAVE_AES_DECRYPT
  7367. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7368. if (ret != 0)
  7369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7370. else
  7371. dec_inited = 1;
  7372. #endif
  7373. #ifdef WOLFSSL_AES_128
  7374. /* 128 key tests */
  7375. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7376. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7377. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  7378. cipher1, sizeof(cipher1));
  7379. if (ret != 0) {
  7380. return ret;
  7381. }
  7382. #endif
  7383. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7384. if (ret != 0)
  7385. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7386. #ifdef HAVE_AES_DECRYPT
  7387. /* decrypt uses AES_ENCRYPTION */
  7388. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7389. if (ret != 0)
  7390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7391. #endif
  7392. XMEMSET(cipher, 0, sizeof(cipher));
  7393. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  7394. if (ret != 0)
  7395. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7396. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7397. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7398. /* test restarting encryption process */
  7399. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  7400. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  7401. if (ret != 0)
  7402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7403. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  7404. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  7405. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7406. #ifdef HAVE_AES_DECRYPT
  7407. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  7408. if (ret != 0)
  7409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7410. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  7411. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7412. #endif /* HAVE_AES_DECRYPT */
  7413. #endif /* WOLFSSL_AES_128 */
  7414. #ifdef WOLFSSL_AES_192
  7415. /* 192 key size test */
  7416. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7417. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7418. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  7419. cipher2, sizeof(cipher2));
  7420. if (ret != 0) {
  7421. return ret;
  7422. }
  7423. #endif
  7424. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7425. if (ret != 0)
  7426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7427. #ifdef HAVE_AES_DECRYPT
  7428. /* decrypt uses AES_ENCRYPTION */
  7429. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7430. if (ret != 0)
  7431. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7432. #endif
  7433. XMEMSET(cipher, 0, sizeof(cipher));
  7434. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  7435. if (ret != 0)
  7436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7437. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  7438. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7439. #ifdef HAVE_AES_DECRYPT
  7440. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  7441. if (ret != 0)
  7442. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7443. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  7444. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7445. #endif /* HAVE_AES_DECRYPT */
  7446. #endif /* WOLFSSL_AES_192 */
  7447. #ifdef WOLFSSL_AES_256
  7448. /* 256 key size test */
  7449. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7450. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7451. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  7452. cipher3, sizeof(cipher3));
  7453. if (ret != 0) {
  7454. return ret;
  7455. }
  7456. #endif
  7457. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7458. if (ret != 0)
  7459. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7460. #ifdef HAVE_AES_DECRYPT
  7461. /* decrypt uses AES_ENCRYPTION */
  7462. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7463. if (ret != 0)
  7464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7465. #endif
  7466. /* test with data left overs, magic lengths are checking near edges */
  7467. XMEMSET(cipher, 0, sizeof(cipher));
  7468. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  7469. if (ret != 0)
  7470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7471. if (XMEMCMP(cipher, cipher3, 4))
  7472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7473. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  7474. if (ret != 0)
  7475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7476. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  7477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7478. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  7479. (AES_BLOCK_SIZE * 4) - 31);
  7480. if (ret != 0)
  7481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7482. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  7483. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7484. #ifdef HAVE_AES_DECRYPT
  7485. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  7486. if (ret != 0)
  7487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7488. if (XMEMCMP(plain, msg3, 4))
  7489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7490. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  7491. if (ret != 0)
  7492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7493. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  7494. if (ret != 0)
  7495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7496. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  7497. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7498. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  7499. (AES_BLOCK_SIZE * 4) - 31);
  7500. if (ret != 0)
  7501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7502. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  7503. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7504. #endif /* HAVE_AES_DECRYPT */
  7505. #endif /* WOLFSSL_AES_256 */
  7506. out:
  7507. if (enc_inited)
  7508. wc_AesFree(enc);
  7509. if (dec_inited)
  7510. wc_AesFree(dec);
  7511. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7512. if (enc)
  7513. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7514. #ifdef HAVE_AES_DECRYPT
  7515. if (dec)
  7516. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7517. #endif
  7518. #endif
  7519. return ret;
  7520. }
  7521. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7522. static wc_test_ret_t aescfb1_test(void)
  7523. {
  7524. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7525. Aes *enc = NULL;
  7526. #else
  7527. Aes enc[1];
  7528. #endif
  7529. int enc_inited = 0;
  7530. byte cipher[AES_BLOCK_SIZE];
  7531. #ifdef HAVE_AES_DECRYPT
  7532. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7533. Aes *dec = NULL;
  7534. #else
  7535. Aes dec[1];
  7536. #endif
  7537. int dec_inited = 0;
  7538. byte plain [AES_BLOCK_SIZE];
  7539. #endif
  7540. wc_test_ret_t ret = 0;
  7541. #ifdef WOLFSSL_AES_128
  7542. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7543. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  7544. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  7545. };
  7546. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7547. {
  7548. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  7549. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  7550. };
  7551. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7552. {
  7553. 0x00
  7554. };
  7555. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7556. {
  7557. 0xC0
  7558. };
  7559. #endif /* WOLFSSL_AES_128 */
  7560. #ifdef WOLFSSL_AES_192
  7561. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7562. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  7563. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  7564. };
  7565. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7566. {
  7567. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  7568. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  7569. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  7570. };
  7571. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7572. {
  7573. 0x30
  7574. };
  7575. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7576. {
  7577. 0x80
  7578. };
  7579. #endif /* WOLFSSL_AES_192 */
  7580. #ifdef WOLFSSL_AES_256
  7581. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7582. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  7583. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  7584. };
  7585. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7586. {
  7587. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  7588. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  7589. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  7590. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  7591. };
  7592. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7593. {
  7594. 0xF7, 0x00
  7595. };
  7596. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7597. {
  7598. 0x41, 0xC0
  7599. };
  7600. #endif /* WOLFSSL_AES_256 */
  7601. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7602. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7603. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7604. #ifdef HAVE_AES_DECRYPT
  7605. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7606. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7607. #endif
  7608. #endif
  7609. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7610. if (ret != 0)
  7611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7612. else
  7613. enc_inited = 1;
  7614. #ifdef HAVE_AES_DECRYPT
  7615. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7616. if (ret != 0)
  7617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7618. else
  7619. dec_inited = 1;
  7620. #endif
  7621. #ifdef WOLFSSL_AES_128
  7622. /* 128 key tests */
  7623. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7624. if (ret != 0)
  7625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7626. #ifdef HAVE_AES_DECRYPT
  7627. /* decrypt uses AES_ENCRYPTION */
  7628. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7629. if (ret != 0)
  7630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7631. #endif
  7632. XMEMSET(cipher, 0, sizeof(cipher));
  7633. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  7634. if (ret != 0)
  7635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7636. if (cipher[0] != cipher1[0])
  7637. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7638. #ifdef HAVE_AES_DECRYPT
  7639. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  7640. if (ret != 0)
  7641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7642. if (plain[0] != msg1[0])
  7643. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7644. #endif /* HAVE_AES_DECRYPT */
  7645. #ifdef OPENSSL_EXTRA
  7646. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7647. if (ret != 0)
  7648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7649. XMEMSET(cipher, 0, sizeof(cipher));
  7650. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  7651. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  7652. if (ret != 0)
  7653. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7654. #ifndef WOLFCRYPT_ONLY
  7655. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  7656. cipher, sizeof(msg1));
  7657. if (ret != 0) {
  7658. goto out;
  7659. }
  7660. #endif
  7661. #endif
  7662. #endif /* WOLFSSL_AES_128 */
  7663. #ifdef WOLFSSL_AES_192
  7664. /* 192 key tests */
  7665. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7666. if (ret != 0)
  7667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7668. XMEMSET(cipher, 0, sizeof(cipher));
  7669. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  7670. if (ret != 0)
  7671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7672. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7673. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7674. #ifdef OPENSSL_EXTRA
  7675. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7676. if (ret != 0)
  7677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7678. XMEMSET(cipher, 0, sizeof(cipher));
  7679. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7680. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7681. if (ret != 0)
  7682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7683. #ifndef WOLFCRYPT_ONLY
  7684. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7685. cipher, sizeof(msg2));
  7686. if (ret != 0) {
  7687. goto out;
  7688. }
  7689. #endif
  7690. #endif
  7691. #endif /* WOLFSSL_AES_192 */
  7692. #ifdef WOLFSSL_AES_256
  7693. /* 256 key tests */
  7694. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7695. if (ret != 0)
  7696. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7697. XMEMSET(cipher, 0, sizeof(cipher));
  7698. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7699. if (ret != 0)
  7700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7701. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7702. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7703. #ifdef OPENSSL_EXTRA
  7704. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7705. if (ret != 0)
  7706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7707. XMEMSET(cipher, 0, sizeof(cipher));
  7708. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7709. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7710. if (ret != 0)
  7711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7712. #ifndef WOLFCRYPT_ONLY
  7713. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7714. cipher, sizeof(msg3));
  7715. if (ret != 0) {
  7716. goto out;
  7717. }
  7718. #endif
  7719. #endif
  7720. #endif /* WOLFSSL_AES_256 */
  7721. out:
  7722. if (enc_inited)
  7723. wc_AesFree(enc);
  7724. #ifdef HAVE_AES_DECRYPT
  7725. if (dec_inited)
  7726. wc_AesFree(dec);
  7727. #endif
  7728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7729. if (enc)
  7730. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7731. #ifdef HAVE_AES_DECRYPT
  7732. if (dec)
  7733. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7734. #endif
  7735. #endif
  7736. return ret;
  7737. }
  7738. static wc_test_ret_t aescfb8_test(void)
  7739. {
  7740. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7741. Aes *enc = NULL;
  7742. #else
  7743. Aes enc[1];
  7744. #endif
  7745. int enc_inited = 0;
  7746. byte cipher[AES_BLOCK_SIZE];
  7747. #ifdef HAVE_AES_DECRYPT
  7748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7749. Aes *dec = NULL;
  7750. #else
  7751. Aes dec[1];
  7752. #endif
  7753. int dec_inited = 0;
  7754. byte plain [AES_BLOCK_SIZE];
  7755. #endif
  7756. wc_test_ret_t ret = 0;
  7757. #ifdef WOLFSSL_AES_128
  7758. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7759. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7760. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7761. };
  7762. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7763. {
  7764. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7765. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7766. };
  7767. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7768. {
  7769. 0xd2,0x76,0x91
  7770. };
  7771. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7772. {
  7773. 0xc9,0x06,0x35
  7774. };
  7775. #endif /* WOLFSSL_AES_128 */
  7776. #ifdef WOLFSSL_AES_192
  7777. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7778. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7779. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7780. };
  7781. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7782. {
  7783. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7784. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7785. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7786. };
  7787. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7788. {
  7789. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7790. 0xb5,0xe8
  7791. };
  7792. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7793. {
  7794. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7795. 0x3a,0x60
  7796. };
  7797. #endif
  7798. #ifdef WOLFSSL_AES_256
  7799. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7800. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7801. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7802. };
  7803. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7804. {
  7805. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7806. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7807. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7808. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7809. };
  7810. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7811. {
  7812. 0x1c,0xff,0x95
  7813. };
  7814. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7815. {
  7816. 0xb9,0x74,0xfa
  7817. };
  7818. #endif
  7819. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7820. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7821. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7822. #ifdef HAVE_AES_DECRYPT
  7823. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7824. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7825. #endif
  7826. #endif
  7827. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7828. if (ret != 0)
  7829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7830. else
  7831. enc_inited = 1;
  7832. #ifdef HAVE_AES_DECRYPT
  7833. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7834. if (ret != 0)
  7835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7836. else
  7837. dec_inited = 1;
  7838. #endif
  7839. #ifdef WOLFSSL_AES_128
  7840. /* 128 key tests */
  7841. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7842. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7843. cipher1, sizeof(cipher1));
  7844. if (ret != 0) {
  7845. return ret;
  7846. }
  7847. #endif
  7848. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7849. if (ret != 0)
  7850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7851. #ifdef HAVE_AES_DECRYPT
  7852. /* decrypt uses AES_ENCRYPTION */
  7853. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7854. if (ret != 0)
  7855. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7856. #endif
  7857. XMEMSET(cipher, 0, sizeof(cipher));
  7858. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7859. if (ret != 0)
  7860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7861. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7862. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7863. #ifdef HAVE_AES_DECRYPT
  7864. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7865. if (ret != 0)
  7866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7867. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7868. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7869. #endif /* HAVE_AES_DECRYPT */
  7870. #endif /* WOLFSSL_AES_128 */
  7871. #ifdef WOLFSSL_AES_192
  7872. /* 192 key tests */
  7873. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7874. if (ret != 0)
  7875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7876. XMEMSET(cipher, 0, sizeof(cipher));
  7877. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7878. if (ret != 0)
  7879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7880. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7881. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7882. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7883. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7884. cipher2, sizeof(msg2));
  7885. if (ret != 0) {
  7886. return ret;
  7887. }
  7888. #endif
  7889. #endif /* WOLFSSL_AES_192 */
  7890. #ifdef WOLFSSL_AES_256
  7891. /* 256 key tests */
  7892. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7893. if (ret != 0)
  7894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7895. XMEMSET(cipher, 0, sizeof(cipher));
  7896. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7897. if (ret != 0)
  7898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7899. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7900. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7901. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7902. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7903. cipher3, sizeof(msg3));
  7904. if (ret != 0) {
  7905. goto out;
  7906. }
  7907. #endif
  7908. #endif /* WOLFSSL_AES_256 */
  7909. out:
  7910. if (enc_inited)
  7911. wc_AesFree(enc);
  7912. #ifdef HAVE_AES_DECRYPT
  7913. if (dec_inited)
  7914. wc_AesFree(dec);
  7915. #endif
  7916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7917. if (enc)
  7918. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7919. #ifdef HAVE_AES_DECRYPT
  7920. if (dec)
  7921. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7922. #endif
  7923. #endif
  7924. return ret;
  7925. }
  7926. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7927. #endif /* WOLFSSL_AES_CFB */
  7928. static wc_test_ret_t aes_key_size_test(void)
  7929. {
  7930. wc_test_ret_t ret;
  7931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7932. Aes *aes;
  7933. #else
  7934. Aes aes[1];
  7935. #endif
  7936. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7937. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7938. #ifndef WOLFSSL_CRYPTOCELL
  7939. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7940. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7941. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7942. #endif
  7943. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7944. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7945. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7946. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7947. byte iv[] = "1234567890abcdef";
  7948. #ifndef HAVE_FIPS
  7949. word32 keySize;
  7950. #endif
  7951. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7952. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7953. return WC_TEST_RET_ENC_ERRNO;
  7954. #endif
  7955. #if !defined(HAVE_FIPS) || \
  7956. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7957. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7958. * supported with that FIPS version */
  7959. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7960. if (ret != BAD_FUNC_ARG)
  7961. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7962. #endif
  7963. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7964. /* 0 check OK for FIPSv1 */
  7965. if (ret != 0)
  7966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7967. #ifndef HAVE_FIPS
  7968. /* Parameter Validation testing. */
  7969. ret = wc_AesGetKeySize(NULL, NULL);
  7970. if (ret != BAD_FUNC_ARG)
  7971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7972. ret = wc_AesGetKeySize(aes, NULL);
  7973. if (ret != BAD_FUNC_ARG)
  7974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7975. ret = wc_AesGetKeySize(NULL, &keySize);
  7976. if (ret != BAD_FUNC_ARG)
  7977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7978. /* Crashes in FIPS */
  7979. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7980. if (ret != BAD_FUNC_ARG)
  7981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7982. #endif
  7983. /* NULL IV indicates to use all zeros IV. */
  7984. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7985. #ifdef WOLFSSL_AES_128
  7986. if (ret != 0)
  7987. #else
  7988. if (ret != BAD_FUNC_ARG)
  7989. #endif
  7990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7991. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7992. if (ret != BAD_FUNC_ARG)
  7993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7994. /* CryptoCell handles rounds internally */
  7995. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7996. /* PSA don't use aes->rounds */
  7997. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7998. /* Force invalid rounds */
  7999. aes->rounds = 16;
  8000. ret = wc_AesGetKeySize(aes, &keySize);
  8001. if (ret != BAD_FUNC_ARG)
  8002. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8003. #endif
  8004. #endif
  8005. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8006. #ifdef WOLFSSL_AES_128
  8007. if (ret != 0)
  8008. #else
  8009. if (ret != BAD_FUNC_ARG)
  8010. #endif
  8011. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8012. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8013. ret = wc_AesGetKeySize(aes, &keySize);
  8014. if (ret != 0 || keySize != sizeof(key16))
  8015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8016. #endif
  8017. #ifndef WOLFSSL_CRYPTOCELL
  8018. /* Cryptocell only supports AES-128 key size */
  8019. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8020. #ifdef WOLFSSL_AES_192
  8021. if (ret != 0)
  8022. #else
  8023. if (ret != BAD_FUNC_ARG)
  8024. #endif
  8025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8026. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8027. ret = wc_AesGetKeySize(aes, &keySize);
  8028. if (ret != 0 || keySize != sizeof(key24))
  8029. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8030. #endif
  8031. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8032. #ifdef WOLFSSL_AES_256
  8033. if (ret != 0)
  8034. #else
  8035. if (ret != BAD_FUNC_ARG)
  8036. #endif
  8037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8038. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  8039. ret = wc_AesGetKeySize(aes, &keySize);
  8040. if (ret != 0 || keySize != sizeof(key32))
  8041. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8042. #endif
  8043. #endif /* !WOLFSSL_CRYPTOCELL */
  8044. ret = 0; /* success */
  8045. out:
  8046. wc_AesFree(aes);
  8047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8048. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8049. #endif
  8050. return ret;
  8051. }
  8052. #if defined(WOLFSSL_AES_XTS)
  8053. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  8054. #ifdef WOLFSSL_AES_128
  8055. static wc_test_ret_t aes_xts_128_test(void)
  8056. {
  8057. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8058. XtsAes *aes = NULL;
  8059. #else
  8060. XtsAes aes[1];
  8061. #endif
  8062. int aes_inited = 0;
  8063. wc_test_ret_t ret = 0;
  8064. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  8065. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  8066. /* 128 key tests */
  8067. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8068. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  8069. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  8070. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  8071. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  8072. };
  8073. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8074. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  8075. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8076. };
  8077. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8078. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8079. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  8080. };
  8081. /* plain text test of partial block is not from NIST test vector list */
  8082. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8083. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8084. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8085. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8086. };
  8087. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8088. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  8089. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  8090. };
  8091. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8092. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  8093. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  8094. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  8095. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  8096. };
  8097. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8098. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  8099. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  8100. };
  8101. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8102. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  8103. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  8104. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  8105. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  8106. };
  8107. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8108. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  8109. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  8110. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  8111. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  8112. };
  8113. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8114. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  8115. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8116. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8117. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8118. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8119. };
  8120. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  8121. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8122. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8123. };
  8124. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  8125. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8126. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8127. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8128. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8129. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  8130. };
  8131. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  8132. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  8133. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  8134. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  8135. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  8136. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  8137. };
  8138. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8139. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8140. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8141. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8142. #endif
  8143. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8144. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8145. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8146. if (ret != 0) {
  8147. printf("EVP_aes_128_xts failed!\n");
  8148. goto out;
  8149. }
  8150. #endif
  8151. XMEMSET(buf, 0, sizeof(buf));
  8152. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8153. HEAP_HINT, devId);
  8154. if (ret != 0)
  8155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8156. else
  8157. aes_inited = 1;
  8158. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8159. #if defined(WOLFSSL_ASYNC_CRYPT)
  8160. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8161. #endif
  8162. if (ret != 0)
  8163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8164. if (XMEMCMP(c2, buf, sizeof(c2)))
  8165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8166. XMEMSET(buf, 0, sizeof(buf));
  8167. wc_AesXtsFree(aes);
  8168. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8169. HEAP_HINT, devId);
  8170. if (ret != 0)
  8171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8172. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8173. #if defined(WOLFSSL_ASYNC_CRYPT)
  8174. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8175. #endif
  8176. if (ret != 0)
  8177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8178. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8179. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8180. /* partial block encryption test */
  8181. XMEMSET(cipher, 0, sizeof(cipher));
  8182. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8183. #if defined(WOLFSSL_ASYNC_CRYPT)
  8184. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8185. #endif
  8186. if (ret != 0)
  8187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8188. wc_AesXtsFree(aes);
  8189. /* partial block decrypt test */
  8190. XMEMSET(buf, 0, sizeof(buf));
  8191. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8192. HEAP_HINT, devId);
  8193. if (ret != 0)
  8194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8195. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8196. #if defined(WOLFSSL_ASYNC_CRYPT)
  8197. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8198. #endif
  8199. if (ret != 0)
  8200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8201. if (XMEMCMP(pp, buf, sizeof(pp)))
  8202. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8203. /* NIST decrypt test vector */
  8204. XMEMSET(buf, 0, sizeof(buf));
  8205. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8206. #if defined(WOLFSSL_ASYNC_CRYPT)
  8207. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8208. #endif
  8209. if (ret != 0)
  8210. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8211. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8212. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8213. /* fail case with decrypting using wrong key */
  8214. XMEMSET(buf, 0, sizeof(buf));
  8215. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8216. #if defined(WOLFSSL_ASYNC_CRYPT)
  8217. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8218. #endif
  8219. if (ret != 0)
  8220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8221. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  8222. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8223. wc_AesXtsFree(aes);
  8224. /* set correct key and retest */
  8225. XMEMSET(buf, 0, sizeof(buf));
  8226. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8227. HEAP_HINT, devId);
  8228. if (ret != 0)
  8229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8230. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8231. #if defined(WOLFSSL_ASYNC_CRYPT)
  8232. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8233. #endif
  8234. if (ret != 0)
  8235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8236. if (XMEMCMP(p2, buf, sizeof(p2)))
  8237. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8238. wc_AesXtsFree(aes);
  8239. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8240. /* Test ciphertext stealing in-place. */
  8241. XMEMCPY(buf, p3, sizeof(p3));
  8242. ret = wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_ENCRYPTION,
  8243. HEAP_HINT, devId);
  8244. if (ret != 0)
  8245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8246. else
  8247. aes_inited = 1;
  8248. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  8249. #if defined(WOLFSSL_ASYNC_CRYPT)
  8250. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8251. #endif
  8252. if (ret != 0)
  8253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8254. if (XMEMCMP(c3, buf, sizeof(c3)))
  8255. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8256. wc_AesXtsFree(aes);
  8257. ret = wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_DECRYPTION,
  8258. HEAP_HINT, devId);
  8259. if (ret != 0)
  8260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8261. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  8262. #if defined(WOLFSSL_ASYNC_CRYPT)
  8263. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8264. #endif
  8265. if (ret != 0)
  8266. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8267. if (XMEMCMP(p3, buf, sizeof(p3)))
  8268. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8269. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8270. out:
  8271. if (aes_inited)
  8272. wc_AesXtsFree(aes);
  8273. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8274. if (aes)
  8275. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8276. #endif
  8277. return ret;
  8278. }
  8279. #endif /* WOLFSSL_AES_128 */
  8280. #ifdef WOLFSSL_AES_256
  8281. static wc_test_ret_t aes_xts_256_test(void)
  8282. {
  8283. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8284. XtsAes *aes = NULL;
  8285. #else
  8286. XtsAes aes[1];
  8287. #endif
  8288. int aes_inited = 0;
  8289. wc_test_ret_t ret = 0;
  8290. unsigned char buf[AES_BLOCK_SIZE * 3];
  8291. unsigned char cipher[AES_BLOCK_SIZE * 3];
  8292. /* 256 key tests */
  8293. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8294. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  8295. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  8296. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  8297. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  8298. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  8299. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  8300. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  8301. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  8302. };
  8303. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8304. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  8305. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  8306. };
  8307. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8308. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  8309. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  8310. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  8311. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  8312. };
  8313. /* plain text test of partial block is not from NIST test vector list */
  8314. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8315. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8316. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8317. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8318. };
  8319. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8320. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  8321. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  8322. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  8323. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  8324. };
  8325. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8326. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  8327. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  8328. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  8329. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  8330. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  8331. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  8332. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  8333. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  8334. };
  8335. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8336. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  8337. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  8338. };
  8339. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8340. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  8341. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  8342. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  8343. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  8344. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  8345. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  8346. };
  8347. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8348. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  8349. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  8350. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  8351. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  8352. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  8353. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  8354. };
  8355. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8356. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8357. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8358. #endif
  8359. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8360. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8361. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8362. if (ret != 0) {
  8363. printf("EVP_aes_256_xts failed\n");
  8364. goto out;
  8365. }
  8366. #endif
  8367. XMEMSET(buf, 0, sizeof(buf));
  8368. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8369. HEAP_HINT, devId);
  8370. if (ret != 0)
  8371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8372. else
  8373. aes_inited = 1;
  8374. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8375. #if defined(WOLFSSL_ASYNC_CRYPT)
  8376. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8377. #endif
  8378. if (ret != 0)
  8379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8380. if (XMEMCMP(c2, buf, sizeof(c2)))
  8381. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8382. wc_AesXtsFree(aes);
  8383. XMEMSET(buf, 0, sizeof(buf));
  8384. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8385. HEAP_HINT, devId);
  8386. if (ret != 0)
  8387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8388. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8389. #if defined(WOLFSSL_ASYNC_CRYPT)
  8390. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8391. #endif
  8392. if (ret != 0)
  8393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8394. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8395. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8396. /* partial block encryption test */
  8397. XMEMSET(cipher, 0, sizeof(cipher));
  8398. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8399. #if defined(WOLFSSL_ASYNC_CRYPT)
  8400. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8401. #endif
  8402. if (ret != 0)
  8403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8404. wc_AesXtsFree(aes);
  8405. /* partial block decrypt test */
  8406. XMEMSET(buf, 0, sizeof(buf));
  8407. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8408. HEAP_HINT, devId);
  8409. if (ret != 0)
  8410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8411. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8412. #if defined(WOLFSSL_ASYNC_CRYPT)
  8413. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8414. #endif
  8415. if (ret != 0)
  8416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8417. if (XMEMCMP(pp, buf, sizeof(pp)))
  8418. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8419. /* NIST decrypt test vector */
  8420. XMEMSET(buf, 0, sizeof(buf));
  8421. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8422. #if defined(WOLFSSL_ASYNC_CRYPT)
  8423. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8424. #endif
  8425. if (ret != 0)
  8426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8427. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8428. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8429. wc_AesXtsFree(aes);
  8430. XMEMSET(buf, 0, sizeof(buf));
  8431. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8432. HEAP_HINT, devId);
  8433. if (ret != 0)
  8434. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8435. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8436. #if defined(WOLFSSL_ASYNC_CRYPT)
  8437. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8438. #endif
  8439. if (ret != 0)
  8440. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8441. if (XMEMCMP(p2, buf, sizeof(p2)))
  8442. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8443. out:
  8444. if (aes_inited)
  8445. wc_AesXtsFree(aes);
  8446. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8447. if (aes)
  8448. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8449. #endif
  8450. return ret;
  8451. }
  8452. #endif /* WOLFSSL_AES_256 */
  8453. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8454. /* both 128 and 256 bit key test */
  8455. static wc_test_ret_t aes_xts_sector_test(void)
  8456. {
  8457. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8458. XtsAes *aes = NULL;
  8459. #else
  8460. XtsAes aes[1];
  8461. #endif
  8462. int aes_inited = 0;
  8463. wc_test_ret_t ret = 0;
  8464. unsigned char buf[AES_BLOCK_SIZE * 2];
  8465. /* 128 key tests */
  8466. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8467. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  8468. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  8469. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  8470. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  8471. };
  8472. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8473. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  8474. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  8475. };
  8476. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8477. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  8478. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  8479. };
  8480. word64 s1 = 141;
  8481. /* 256 key tests */
  8482. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8483. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  8484. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  8485. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  8486. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  8487. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  8488. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  8489. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  8490. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  8491. };
  8492. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8493. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  8494. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  8495. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  8496. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  8497. };
  8498. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8499. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  8500. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  8501. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  8502. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  8503. };
  8504. word64 s2 = 187;
  8505. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8506. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8507. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8508. #endif
  8509. XMEMSET(buf, 0, sizeof(buf));
  8510. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8511. HEAP_HINT, devId);
  8512. if (ret != 0)
  8513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8514. else
  8515. aes_inited = 1;
  8516. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  8517. #if defined(WOLFSSL_ASYNC_CRYPT)
  8518. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8519. #endif
  8520. if (ret != 0)
  8521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8522. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8523. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8524. wc_AesXtsFree(aes);
  8525. /* decrypt test */
  8526. XMEMSET(buf, 0, sizeof(buf));
  8527. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8528. HEAP_HINT, devId);
  8529. if (ret != 0)
  8530. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8531. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  8532. #if defined(WOLFSSL_ASYNC_CRYPT)
  8533. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8534. #endif
  8535. if (ret != 0)
  8536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8537. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8538. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8539. wc_AesXtsFree(aes);
  8540. /* 256 bit key tests */
  8541. XMEMSET(buf, 0, sizeof(buf));
  8542. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8543. HEAP_HINT, devId);
  8544. if (ret != 0)
  8545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8546. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  8547. #if defined(WOLFSSL_ASYNC_CRYPT)
  8548. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8549. #endif
  8550. if (ret != 0)
  8551. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8552. if (XMEMCMP(c2, buf, sizeof(c2)))
  8553. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8554. wc_AesXtsFree(aes);
  8555. /* decrypt test */
  8556. XMEMSET(buf, 0, sizeof(buf));
  8557. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8558. HEAP_HINT, devId);
  8559. if (ret != 0)
  8560. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8561. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  8562. #if defined(WOLFSSL_ASYNC_CRYPT)
  8563. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8564. #endif
  8565. if (ret != 0)
  8566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8567. if (XMEMCMP(p2, buf, sizeof(p2)))
  8568. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8569. out:
  8570. if (aes_inited)
  8571. wc_AesXtsFree(aes);
  8572. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8573. if (aes)
  8574. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8575. #endif
  8576. return ret;
  8577. }
  8578. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  8579. #ifdef WOLFSSL_AES_128
  8580. /* testing of bad arguments */
  8581. static wc_test_ret_t aes_xts_args_test(void)
  8582. {
  8583. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8584. XtsAes *aes = NULL;
  8585. #else
  8586. XtsAes aes[1];
  8587. #endif
  8588. int aes_inited = 0;
  8589. wc_test_ret_t ret;
  8590. unsigned char buf[AES_BLOCK_SIZE * 2];
  8591. /* 128 key tests */
  8592. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8593. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  8594. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  8595. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  8596. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  8597. };
  8598. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8599. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  8600. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  8601. };
  8602. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8603. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  8604. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  8605. };
  8606. word64 s1 = 141;
  8607. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8608. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8609. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8610. #endif
  8611. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  8612. HEAP_HINT, devId) == 0)
  8613. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8614. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  8615. HEAP_HINT, devId) == 0)
  8616. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8617. /* encryption operations */
  8618. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8619. HEAP_HINT, devId);
  8620. if (ret != 0)
  8621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8622. else
  8623. aes_inited = 1;
  8624. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  8625. #if defined(WOLFSSL_ASYNC_CRYPT)
  8626. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8627. #endif
  8628. if (ret == 0)
  8629. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8630. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  8631. #if defined(WOLFSSL_ASYNC_CRYPT)
  8632. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8633. #endif
  8634. if (ret == 0)
  8635. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8636. wc_AesXtsFree(aes);
  8637. /* decryption operations */
  8638. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8639. HEAP_HINT, devId);
  8640. if (ret != 0)
  8641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8642. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  8643. #if defined(WOLFSSL_ASYNC_CRYPT)
  8644. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8645. #endif
  8646. if (ret == 0)
  8647. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8648. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  8649. #if defined(WOLFSSL_ASYNC_CRYPT)
  8650. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8651. #endif
  8652. if (ret == 0)
  8653. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8654. ret = 0;
  8655. out:
  8656. if (aes_inited)
  8657. wc_AesXtsFree(aes);
  8658. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8659. if (aes)
  8660. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8661. #endif
  8662. return ret;
  8663. }
  8664. #endif /* WOLFSSL_AES_128 */
  8665. #endif /* WOLFSSL_AES_XTS */
  8666. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8667. static wc_test_ret_t aes_cbc_test(void)
  8668. {
  8669. byte cipher[AES_BLOCK_SIZE];
  8670. byte plain[AES_BLOCK_SIZE];
  8671. wc_test_ret_t ret;
  8672. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8673. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8674. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8675. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8676. };
  8677. byte key[] = "0123456789abcdef "; /* align */
  8678. byte iv[] = "1234567890abcdef "; /* align */
  8679. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8680. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8681. /* Parameter Validation testing. */
  8682. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  8683. if (ret != BAD_FUNC_ARG)
  8684. return WC_TEST_RET_ENC_EC(ret);
  8685. #ifdef HAVE_AES_DECRYPT
  8686. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  8687. if (ret != BAD_FUNC_ARG)
  8688. return WC_TEST_RET_ENC_EC(ret);
  8689. #endif
  8690. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  8691. AES_BLOCK_SIZE, iv);
  8692. if (ret != 0)
  8693. return WC_TEST_RET_ENC_EC(ret);
  8694. #ifdef HAVE_AES_DECRYPT
  8695. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  8696. AES_BLOCK_SIZE, iv);
  8697. if (ret != 0)
  8698. return WC_TEST_RET_ENC_EC(ret);
  8699. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  8700. return WC_TEST_RET_ENC_NC;
  8701. #endif /* HAVE_AES_DECRYPT */
  8702. (void)plain;
  8703. return 0;
  8704. }
  8705. #endif
  8706. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8707. static wc_test_ret_t aesecb_test(void)
  8708. {
  8709. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8710. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8711. #else
  8712. Aes enc[1];
  8713. #endif
  8714. int enc_inited = 0;
  8715. byte cipher[AES_BLOCK_SIZE * 4];
  8716. #ifdef HAVE_AES_DECRYPT
  8717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8718. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8719. #else
  8720. Aes dec[1];
  8721. #endif
  8722. int dec_inited = 0;
  8723. byte plain [AES_BLOCK_SIZE * 4];
  8724. #endif /* HAVE_AES_DECRYPT */
  8725. wc_test_ret_t ret = 0;
  8726. #if defined(WOLFSSL_AES_256)
  8727. {
  8728. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8729. {
  8730. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8731. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8732. };
  8733. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8734. {
  8735. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8736. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8737. };
  8738. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8739. {
  8740. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8741. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8742. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8743. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8744. };
  8745. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8746. if (ret != 0)
  8747. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8748. enc_inited = 1;
  8749. #if defined(HAVE_AES_DECRYPT)
  8750. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8751. if (ret != 0)
  8752. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8753. dec_inited = 1;
  8754. #endif
  8755. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8756. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8757. if (ret != 0)
  8758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8759. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8760. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8761. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8762. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8763. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8764. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8765. if (ret != 0)
  8766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8767. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8768. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8769. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8770. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8771. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8772. }
  8773. out:
  8774. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8775. if (enc) {
  8776. if (enc_inited)
  8777. wc_AesFree(enc);
  8778. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8779. }
  8780. #ifdef HAVE_AES_DECRYPT
  8781. if (dec) {
  8782. if (dec_inited)
  8783. wc_AesFree(dec);
  8784. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8785. }
  8786. #endif
  8787. #else
  8788. if (enc_inited)
  8789. wc_AesFree(enc);
  8790. #ifdef HAVE_AES_DECRYPT
  8791. if (dec_inited)
  8792. wc_AesFree(dec);
  8793. #endif
  8794. #endif
  8795. #endif /* WOLFSSL_AES_256 */
  8796. return ret;
  8797. }
  8798. #endif /* HAVE_AES_ECB */
  8799. #ifdef WOLFSSL_AES_COUNTER
  8800. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8801. {
  8802. wc_test_ret_t ret = 0;
  8803. /* test vectors from "Recommendation for Block Cipher Modes of
  8804. * Operation" NIST Special Publication 800-38A */
  8805. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8806. {
  8807. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8808. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8809. };
  8810. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8811. {
  8812. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8813. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8814. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8815. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8816. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8817. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8818. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8819. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8820. };
  8821. #ifdef WOLFSSL_ARMASM
  8822. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8823. {
  8824. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8825. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8826. };
  8827. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8828. {
  8829. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8830. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8831. };
  8832. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8833. {
  8834. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8835. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8836. };
  8837. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8838. {
  8839. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8840. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8841. };
  8842. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8843. {
  8844. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8845. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8846. };
  8847. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8848. {
  8849. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8850. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8851. };
  8852. #endif
  8853. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8854. {
  8855. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8856. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8857. };
  8858. #ifdef WOLFSSL_ARMASM
  8859. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8860. {
  8861. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8862. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8863. };
  8864. #endif
  8865. #ifdef WOLFSSL_AES_128
  8866. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8867. {
  8868. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8869. 0xc2
  8870. };
  8871. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8872. {
  8873. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8874. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8875. };
  8876. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8877. {
  8878. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8879. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8880. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8881. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8882. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8883. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8884. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8885. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8886. };
  8887. #ifdef WOLFSSL_ARMASM
  8888. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8889. {
  8890. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8891. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8892. 0x7e,0xad,0xea,0x5c,0xd1
  8893. };
  8894. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8895. {
  8896. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8897. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8898. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8899. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8900. };
  8901. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8902. {
  8903. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8904. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8905. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8906. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8907. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8908. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8909. };
  8910. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8911. {
  8912. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8913. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8914. 0xae,0x74,0x94,0x72,0x8e
  8915. };
  8916. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8917. {
  8918. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8919. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8920. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8921. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8922. };
  8923. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8924. {
  8925. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8926. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8927. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8928. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8929. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8930. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8931. };
  8932. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8933. {
  8934. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8935. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8936. 0x9d,0x77,0xc9,0x5f,0x38
  8937. };
  8938. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8939. {
  8940. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8941. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8942. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8943. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8944. };
  8945. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8946. {
  8947. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8948. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8949. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8950. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8951. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8952. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8953. };
  8954. #endif
  8955. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8956. {
  8957. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8958. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8959. 0xd3,0xda,0xe1,0x5b,0x04
  8960. };
  8961. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8962. {
  8963. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8964. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8965. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8966. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8967. };
  8968. #ifdef WOLFSSL_ARMASM
  8969. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8970. {
  8971. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8972. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8973. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8974. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8975. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8976. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8977. };
  8978. #endif
  8979. #endif /* WOLFSSL_AES_128 */
  8980. #ifdef WOLFSSL_AES_192
  8981. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8982. {
  8983. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8984. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8985. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8986. };
  8987. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8988. {
  8989. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8990. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8991. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8992. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8993. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8994. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8995. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8996. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8997. };
  8998. #ifdef WOLFSSL_ARMASM
  8999. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  9000. {
  9001. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9002. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9003. 0x0c,0x33,0x97,0x06,0xc0
  9004. };
  9005. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  9006. {
  9007. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9008. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9009. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  9010. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  9011. };
  9012. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  9013. {
  9014. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  9015. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  9016. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  9017. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  9018. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  9019. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  9020. };
  9021. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  9022. {
  9023. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9024. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9025. 0x3d,0xf7,0xb4,0xfd,0x8c
  9026. };
  9027. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  9028. {
  9029. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9030. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9031. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  9032. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  9033. };
  9034. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  9035. {
  9036. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  9037. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  9038. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  9039. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  9040. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  9041. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  9042. };
  9043. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  9044. {
  9045. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9046. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9047. 0x47,0xb5,0xf3,0x30,0x3b
  9048. };
  9049. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  9050. {
  9051. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9052. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9053. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  9054. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  9055. };
  9056. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  9057. {
  9058. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  9059. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  9060. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  9061. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  9062. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  9063. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  9064. };
  9065. #endif
  9066. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  9067. {
  9068. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9069. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9070. 0x8c,0x68,0xa7,0xd9,0x57
  9071. };
  9072. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  9073. {
  9074. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9075. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9076. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  9077. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  9078. };
  9079. #ifdef WOLFSSL_ARMASM
  9080. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  9081. {
  9082. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  9083. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  9084. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  9085. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  9086. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  9087. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  9088. };
  9089. #endif
  9090. #endif
  9091. #ifdef WOLFSSL_AES_256
  9092. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  9093. {
  9094. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9095. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9096. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9097. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9098. };
  9099. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  9100. {
  9101. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  9102. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  9103. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  9104. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  9105. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  9106. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  9107. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  9108. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  9109. };
  9110. #ifdef WOLFSSL_ARMASM
  9111. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  9112. {
  9113. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9114. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9115. 0xa2,0x13,0x47,0x74,0xda
  9116. };
  9117. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  9118. {
  9119. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9120. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9121. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  9122. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  9123. };
  9124. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  9125. {
  9126. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  9127. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  9128. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  9129. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  9130. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  9131. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  9132. };
  9133. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  9134. {
  9135. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9136. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9137. 0xd4,0x94,0x4c,0xcd,0x4d
  9138. };
  9139. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  9140. {
  9141. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9142. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9143. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  9144. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  9145. };
  9146. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  9147. {
  9148. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  9149. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  9150. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  9151. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  9152. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  9153. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  9154. };
  9155. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  9156. {
  9157. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9158. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9159. 0xb8,0x69,0xa1,0x48,0x45
  9160. };
  9161. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  9162. {
  9163. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9164. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9165. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  9166. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  9167. };
  9168. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  9169. {
  9170. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  9171. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  9172. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  9173. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  9174. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  9175. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  9176. };
  9177. #endif
  9178. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  9179. {
  9180. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9181. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9182. 0x4b,0x45,0x7c,0xd6,0x8a
  9183. };
  9184. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  9185. {
  9186. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9187. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9188. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  9189. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  9190. };
  9191. #ifdef WOLFSSL_ARMASM
  9192. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  9193. {
  9194. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  9195. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  9196. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  9197. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  9198. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  9199. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  9200. };
  9201. #endif
  9202. #endif
  9203. int i;
  9204. struct {
  9205. const byte* key;
  9206. int keySz;
  9207. const byte* iv;
  9208. const byte* plain;
  9209. int len;
  9210. const byte* cipher;
  9211. } testVec[] = {
  9212. #ifdef WOLFSSL_AES_128
  9213. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9214. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  9215. /* let's try with just 9 bytes, non block size test */
  9216. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9217. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  9218. /* and an additional 9 bytes to reuse tmp left buffer */
  9219. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  9220. /* Counter wrapping */
  9221. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9222. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  9223. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9224. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  9225. ctr128Wrap128CipherLong },
  9226. #ifdef WOLFSSL_ARMASM
  9227. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  9228. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  9229. ctr128Wrap128_2CipherLong },
  9230. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9231. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  9232. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9233. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  9234. ctr128Wrap96CipherLong },
  9235. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  9236. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  9237. ctr128Wrap96_2CipherLong },
  9238. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9239. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  9240. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9241. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  9242. ctr128Wrap64CipherLong },
  9243. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  9244. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  9245. ctr128Wrap64_2CipherLong },
  9246. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9247. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  9248. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9249. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  9250. ctr128Wrap32CipherLong },
  9251. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  9252. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  9253. ctr128Wrap32_2CipherLong },
  9254. #endif
  9255. #endif
  9256. #ifdef WOLFSSL_AES_192
  9257. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9258. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  9259. /* let's try with just 9 bytes, non block size test */
  9260. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9261. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  9262. /* Counter wrapping */
  9263. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9264. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  9265. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9266. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  9267. ctr192Wrap128CipherLong },
  9268. #ifdef WOLFSSL_ARMASM
  9269. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  9270. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  9271. ctr192Wrap128_2CipherLong },
  9272. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9273. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  9274. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9275. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  9276. ctr192Wrap96CipherLong },
  9277. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  9278. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  9279. ctr192Wrap96_2CipherLong },
  9280. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9281. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  9282. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9283. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  9284. ctr192Wrap64CipherLong },
  9285. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  9286. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  9287. ctr192Wrap64_2CipherLong },
  9288. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9289. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  9290. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9291. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  9292. ctr192Wrap32CipherLong },
  9293. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  9294. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  9295. ctr192Wrap32_2CipherLong },
  9296. #endif
  9297. #endif
  9298. #ifdef WOLFSSL_AES_256
  9299. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9300. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  9301. /* let's try with just 9 bytes, non block size test */
  9302. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9303. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  9304. /* Counter wrapping */
  9305. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9306. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  9307. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9308. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  9309. ctr256Wrap128CipherLong },
  9310. #ifdef WOLFSSL_ARMASM
  9311. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  9312. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  9313. ctr256Wrap128_2CipherLong },
  9314. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9315. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  9316. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9317. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  9318. ctr256Wrap96CipherLong },
  9319. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  9320. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  9321. ctr256Wrap96_2CipherLong },
  9322. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9323. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  9324. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9325. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  9326. ctr256Wrap64CipherLong },
  9327. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  9328. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  9329. ctr256Wrap64_2CipherLong },
  9330. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9331. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  9332. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9333. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  9334. ctr256Wrap32CipherLong },
  9335. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  9336. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  9337. ctr256Wrap32_2CipherLong },
  9338. #endif
  9339. #endif
  9340. };
  9341. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  9342. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  9343. if (testVec[i].key != NULL) {
  9344. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  9345. testVec[i].iv, AES_ENCRYPTION);
  9346. if (ret != 0) {
  9347. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9348. }
  9349. /* Ctr only uses encrypt, even on key setup */
  9350. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  9351. testVec[i].iv, AES_ENCRYPTION);
  9352. if (ret != 0) {
  9353. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9354. }
  9355. }
  9356. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  9357. if (ret != 0) {
  9358. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9359. }
  9360. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  9361. if (ret != 0) {
  9362. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9363. }
  9364. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  9365. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9366. }
  9367. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  9368. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  9369. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9370. }
  9371. #endif
  9372. }
  9373. out:
  9374. return ret;
  9375. }
  9376. #endif /* WOLFSSL_AES_COUNTER */
  9377. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  9378. {
  9379. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9380. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9381. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9382. #else
  9383. Aes enc[1];
  9384. #endif
  9385. int enc_inited = 0;
  9386. byte cipher[AES_BLOCK_SIZE * 4];
  9387. #ifdef HAVE_AES_DECRYPT
  9388. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9389. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9390. #else
  9391. Aes dec[1];
  9392. #endif
  9393. int dec_inited = 0;
  9394. byte plain [AES_BLOCK_SIZE * 4];
  9395. #endif /* HAVE_AES_DECRYPT */
  9396. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9397. wc_test_ret_t ret = 0;
  9398. #ifdef HAVE_AES_CBC
  9399. #ifdef WOLFSSL_AES_128
  9400. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  9401. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9402. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9403. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9404. };
  9405. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9406. {
  9407. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  9408. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  9409. };
  9410. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  9411. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  9412. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9413. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9414. if (enc == NULL)
  9415. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9416. #endif
  9417. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9418. if (dec == NULL)
  9419. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9420. #endif
  9421. #endif
  9422. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9423. if (ret != 0)
  9424. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9425. enc_inited = 1;
  9426. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9427. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9428. if (ret != 0)
  9429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9430. dec_inited = 1;
  9431. #endif
  9432. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  9433. if (ret != 0)
  9434. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9435. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9436. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  9437. if (ret != 0)
  9438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9439. #endif
  9440. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  9441. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  9442. #if defined(WOLFSSL_ASYNC_CRYPT)
  9443. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9444. #endif
  9445. if (ret != 0)
  9446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9447. #ifdef HAVE_AES_DECRYPT
  9448. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  9449. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  9450. #if defined(WOLFSSL_ASYNC_CRYPT)
  9451. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9452. #endif
  9453. if (ret != 0)
  9454. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9455. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  9456. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9457. #endif /* HAVE_AES_DECRYPT */
  9458. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  9459. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9460. #endif /* WOLFSSL_AES_128 */
  9461. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  9462. {
  9463. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  9464. /* "All work and no play makes Jack a dull boy. " */
  9465. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9466. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9467. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9468. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9469. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9470. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9471. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9472. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9473. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9474. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9475. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9476. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9477. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9478. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9479. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9480. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9481. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9482. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9483. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9484. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9485. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9486. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9487. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9488. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9489. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9490. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9491. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9492. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9493. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9494. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9495. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9496. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9497. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9498. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9499. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9500. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9501. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9502. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9503. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9504. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9505. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9506. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9507. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9508. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9509. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9510. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9511. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9512. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  9513. };
  9514. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  9515. word32 keySz, msgSz;
  9516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9517. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9518. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9519. if ((bigCipher == NULL) ||
  9520. (bigPlain == NULL)) {
  9521. if (bigCipher != NULL)
  9522. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9523. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9524. }
  9525. #else
  9526. byte bigCipher[sizeof(bigMsg)];
  9527. byte bigPlain[sizeof(bigMsg)];
  9528. #endif
  9529. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  9530. * message by AES_BLOCK_SIZE for each size of AES key. */
  9531. for (keySz = 16; keySz <= 32; keySz += 8) {
  9532. for (msgSz = AES_BLOCK_SIZE;
  9533. msgSz <= sizeof(bigMsg);
  9534. msgSz += AES_BLOCK_SIZE) {
  9535. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  9536. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  9537. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  9538. if (ret != 0) {
  9539. ret = WC_TEST_RET_ENC_EC(ret);
  9540. break;
  9541. }
  9542. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  9543. if (ret != 0) {
  9544. ret = WC_TEST_RET_ENC_EC(ret);
  9545. break;
  9546. }
  9547. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  9548. #if defined(WOLFSSL_ASYNC_CRYPT)
  9549. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9550. #endif
  9551. if (ret != 0) {
  9552. ret = WC_TEST_RET_ENC_EC(ret);
  9553. break;
  9554. }
  9555. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  9556. #if defined(WOLFSSL_ASYNC_CRYPT)
  9557. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9558. #endif
  9559. if (ret != 0) {
  9560. ret = WC_TEST_RET_ENC_EC(ret);
  9561. break;
  9562. }
  9563. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  9564. ret = WC_TEST_RET_ENC_NC;
  9565. break;
  9566. }
  9567. }
  9568. if (ret != 0)
  9569. break;
  9570. }
  9571. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9572. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9573. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9574. #endif
  9575. if (ret != 0)
  9576. goto out;
  9577. }
  9578. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  9579. /* Test of AES IV state with encrypt/decrypt */
  9580. #ifdef WOLFSSL_AES_128
  9581. {
  9582. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  9583. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  9584. */
  9585. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  9586. {
  9587. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  9588. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  9589. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  9590. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  9591. };
  9592. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  9593. {
  9594. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  9595. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  9596. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  9597. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  9598. };
  9599. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  9600. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  9601. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  9602. };
  9603. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  9604. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9605. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  9606. };
  9607. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  9608. if (ret != 0)
  9609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9610. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  9611. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  9612. #if defined(WOLFSSL_ASYNC_CRYPT)
  9613. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9614. #endif
  9615. if (ret != 0)
  9616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9617. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  9618. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9619. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  9620. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  9621. #if defined(WOLFSSL_ASYNC_CRYPT)
  9622. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9623. #endif
  9624. if (ret != 0)
  9625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9626. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  9627. AES_BLOCK_SIZE))
  9628. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9629. #if defined(HAVE_AES_DECRYPT)
  9630. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  9631. if (ret != 0)
  9632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9633. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  9634. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  9635. #if defined(WOLFSSL_ASYNC_CRYPT)
  9636. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9637. #endif
  9638. if (ret != 0)
  9639. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9640. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  9641. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9642. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  9643. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  9644. #if defined(WOLFSSL_ASYNC_CRYPT)
  9645. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9646. #endif
  9647. if (ret != 0)
  9648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9649. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  9650. AES_BLOCK_SIZE))
  9651. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9652. #endif /* HAVE_AES_DECRYPT */
  9653. }
  9654. #endif /* WOLFSSL_AES_128 */
  9655. #endif /* HAVE_AES_CBC */
  9656. #ifdef WOLFSSL_AES_COUNTER
  9657. ret = aesctr_test(enc, dec, cipher, plain);
  9658. if (ret != 0)
  9659. return ret;
  9660. #endif
  9661. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  9662. {
  9663. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  9664. {
  9665. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9666. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9667. };
  9668. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  9669. {
  9670. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  9671. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  9672. };
  9673. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  9674. {
  9675. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9676. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9677. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9678. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9679. };
  9680. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9681. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9682. if (ret != 0)
  9683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9684. #if !defined(HAVE_SELFTEST) && \
  9685. (defined(WOLFSSL_LINUXKM) || \
  9686. !defined(HAVE_FIPS) || \
  9687. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  9688. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  9689. if (ret != 0)
  9690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9691. #else
  9692. wc_AesEncryptDirect(enc, cipher, niPlain);
  9693. #endif
  9694. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9695. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9696. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9697. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9698. if (ret != 0)
  9699. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9700. #if !defined(HAVE_SELFTEST) && \
  9701. (defined(WOLFSSL_LINUXKM) || \
  9702. !defined(HAVE_FIPS) || \
  9703. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  9704. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  9705. if (ret != 0)
  9706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9707. #else
  9708. wc_AesDecryptDirect(dec, plain, niCipher);
  9709. #endif
  9710. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9711. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9712. }
  9713. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  9714. ret = aes_key_size_test();
  9715. if (ret != 0)
  9716. goto out;
  9717. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  9718. ret = aes_cbc_test();
  9719. if (ret != 0)
  9720. goto out;
  9721. #endif
  9722. #if defined(WOLFSSL_AES_XTS)
  9723. #ifdef WOLFSSL_AES_128
  9724. ret = aes_xts_128_test();
  9725. if (ret != 0)
  9726. goto out;
  9727. #endif
  9728. #ifdef WOLFSSL_AES_256
  9729. ret = aes_xts_256_test();
  9730. if (ret != 0)
  9731. goto out;
  9732. #endif
  9733. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  9734. ret = aes_xts_sector_test();
  9735. if (ret != 0)
  9736. goto out;
  9737. #endif
  9738. #ifdef WOLFSSL_AES_128
  9739. ret = aes_xts_args_test();
  9740. if (ret != 0)
  9741. goto out;
  9742. #endif
  9743. #endif
  9744. #if defined(WOLFSSL_AES_CFB)
  9745. ret = aescfb_test();
  9746. if (ret != 0)
  9747. goto out;
  9748. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9749. ret = aescfb1_test();
  9750. if (ret != 0)
  9751. goto out;
  9752. ret = aescfb8_test();
  9753. if (ret != 0)
  9754. goto out;
  9755. #endif
  9756. #endif
  9757. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9758. ret = aesecb_test();
  9759. if (ret != 0)
  9760. goto out;
  9761. #endif
  9762. out:
  9763. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9764. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9765. if (enc) {
  9766. if (enc_inited)
  9767. wc_AesFree(enc);
  9768. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9769. }
  9770. #else
  9771. if (enc_inited)
  9772. wc_AesFree(enc);
  9773. #endif
  9774. (void)cipher;
  9775. #ifdef HAVE_AES_DECRYPT
  9776. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9777. if (dec) {
  9778. if (dec_inited)
  9779. wc_AesFree(dec);
  9780. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9781. }
  9782. #else
  9783. if (dec_inited)
  9784. wc_AesFree(dec);
  9785. #endif
  9786. (void)plain;
  9787. #endif /* HAVE_AES_DECRYPT */
  9788. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9789. return ret;
  9790. }
  9791. #ifdef WOLFSSL_AES_192
  9792. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  9793. {
  9794. #ifdef HAVE_AES_CBC
  9795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9796. Aes *enc = NULL;
  9797. #else
  9798. Aes enc[1];
  9799. #endif
  9800. byte cipher[AES_BLOCK_SIZE];
  9801. #ifdef HAVE_AES_DECRYPT
  9802. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9803. Aes *dec = NULL;
  9804. #else
  9805. Aes dec[1];
  9806. #endif
  9807. byte plain[AES_BLOCK_SIZE];
  9808. #endif
  9809. #endif /* HAVE_AES_CBC */
  9810. wc_test_ret_t ret = 0;
  9811. #ifdef HAVE_AES_CBC
  9812. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9813. * Appendix F.2.3 */
  9814. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9815. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9816. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9817. };
  9818. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9819. {
  9820. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9821. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9822. };
  9823. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9824. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9825. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9826. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9827. };
  9828. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9829. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9830. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9831. };
  9832. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9833. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9834. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9835. #ifdef HAVE_AES_DECRYPT
  9836. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9837. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9838. #endif
  9839. #endif
  9840. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9841. if (ret != 0)
  9842. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9843. #ifdef HAVE_AES_DECRYPT
  9844. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9845. if (ret != 0)
  9846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9847. #endif
  9848. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9849. if (ret != 0)
  9850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9851. #ifdef HAVE_AES_DECRYPT
  9852. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9853. if (ret != 0)
  9854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9855. #endif
  9856. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9857. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9858. #if defined(WOLFSSL_ASYNC_CRYPT)
  9859. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9860. #endif
  9861. if (ret != 0)
  9862. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9863. #ifdef HAVE_AES_DECRYPT
  9864. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9865. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9866. #if defined(WOLFSSL_ASYNC_CRYPT)
  9867. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9868. #endif
  9869. if (ret != 0)
  9870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9871. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9872. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9873. }
  9874. #endif
  9875. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9876. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9877. wc_AesFree(enc);
  9878. #ifdef HAVE_AES_DECRYPT
  9879. wc_AesFree(dec);
  9880. #endif
  9881. out:
  9882. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9883. if (enc)
  9884. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9885. #ifdef HAVE_AES_DECRYPT
  9886. if (dec)
  9887. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9888. #endif
  9889. #endif
  9890. #endif /* HAVE_AES_CBC */
  9891. return ret;
  9892. }
  9893. #endif /* WOLFSSL_AES_192 */
  9894. #ifdef WOLFSSL_AES_256
  9895. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  9896. {
  9897. #ifdef HAVE_AES_CBC
  9898. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9899. Aes *enc = NULL;
  9900. #else
  9901. Aes enc[1];
  9902. #endif
  9903. byte cipher[AES_BLOCK_SIZE];
  9904. #ifdef HAVE_AES_DECRYPT
  9905. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9906. Aes *dec = NULL;
  9907. #else
  9908. Aes dec[1];
  9909. #endif
  9910. byte plain[AES_BLOCK_SIZE];
  9911. #endif
  9912. #endif /* HAVE_AES_CBC */
  9913. wc_test_ret_t ret = 0;
  9914. #ifdef HAVE_AES_CBC
  9915. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9916. * Appendix F.2.5 */
  9917. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9918. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9919. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9920. };
  9921. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9922. {
  9923. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9924. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9925. };
  9926. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9927. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9928. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9929. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9930. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9931. };
  9932. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9933. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9934. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9935. };
  9936. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9937. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9938. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9939. #ifdef HAVE_AES_DECRYPT
  9940. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9941. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9942. #endif
  9943. #endif
  9944. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9945. if (ret != 0)
  9946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9947. #ifdef HAVE_AES_DECRYPT
  9948. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9949. if (ret != 0)
  9950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9951. #endif
  9952. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9953. if (ret != 0)
  9954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9955. #ifdef HAVE_AES_DECRYPT
  9956. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9957. if (ret != 0)
  9958. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9959. #endif
  9960. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9961. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9962. #if defined(WOLFSSL_ASYNC_CRYPT)
  9963. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9964. #endif
  9965. if (ret != 0)
  9966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9967. #ifdef HAVE_AES_DECRYPT
  9968. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9969. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9970. #if defined(WOLFSSL_ASYNC_CRYPT)
  9971. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9972. #endif
  9973. if (ret != 0)
  9974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9975. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9976. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9977. }
  9978. #endif
  9979. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9980. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9981. wc_AesFree(enc);
  9982. #ifdef HAVE_AES_DECRYPT
  9983. wc_AesFree(dec);
  9984. #endif
  9985. out:
  9986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9987. if (enc)
  9988. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9989. #ifdef HAVE_AES_DECRYPT
  9990. if (dec)
  9991. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9992. #endif
  9993. #endif
  9994. #endif /* HAVE_AES_CBC */
  9995. return ret;
  9996. }
  9997. #endif /* WOLFSSL_AES_256 */
  9998. #ifdef HAVE_AESGCM
  9999. #ifdef WOLFSSL_AES_128
  10000. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  10001. byte* plain, int plainSz, byte* cipher, int cipherSz,
  10002. byte* aad, int aadSz, byte* tag, int tagSz)
  10003. {
  10004. wc_test_ret_t ret;
  10005. int enc_inited = 0, dec_inited = 0;
  10006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10007. Aes *enc = NULL;
  10008. Aes *dec = NULL;
  10009. #else
  10010. Aes enc[1];
  10011. Aes dec[1];
  10012. #endif
  10013. byte resultT[AES_BLOCK_SIZE];
  10014. byte resultP[AES_BLOCK_SIZE * 3];
  10015. byte resultC[AES_BLOCK_SIZE * 3];
  10016. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10017. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10018. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10019. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10020. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10021. #endif
  10022. XMEMSET(resultT, 0, sizeof(resultT));
  10023. XMEMSET(resultC, 0, sizeof(resultC));
  10024. XMEMSET(resultP, 0, sizeof(resultP));
  10025. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10026. if (ret != 0)
  10027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10028. else
  10029. enc_inited = 1;
  10030. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10031. if (ret != 0)
  10032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10033. else
  10034. dec_inited = 1;
  10035. ret = wc_AesGcmSetKey(enc, key, keySz);
  10036. if (ret != 0)
  10037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10038. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10039. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  10040. resultT, tagSz, aad, aadSz);
  10041. #if defined(WOLFSSL_ASYNC_CRYPT)
  10042. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10043. #endif
  10044. if (ret != 0)
  10045. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10046. if (cipher != NULL) {
  10047. if (XMEMCMP(cipher, resultC, cipherSz))
  10048. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10049. }
  10050. if (XMEMCMP(tag, resultT, tagSz))
  10051. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10052. #ifdef HAVE_AES_DECRYPT
  10053. ret = wc_AesGcmSetKey(dec, key, keySz);
  10054. if (ret != 0)
  10055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10056. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  10057. iv, ivSz, resultT, tagSz, aad, aadSz);
  10058. #if defined(WOLFSSL_ASYNC_CRYPT)
  10059. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10060. #endif
  10061. if (ret != 0)
  10062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10063. if (plain != NULL) {
  10064. if (XMEMCMP(plain, resultP, plainSz))
  10065. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10066. }
  10067. #endif /* HAVE_AES_DECRYPT */
  10068. ret = 0;
  10069. out:
  10070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10071. if (enc) {
  10072. if (enc_inited)
  10073. wc_AesFree(enc);
  10074. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10075. }
  10076. if (dec) {
  10077. if (dec_inited)
  10078. wc_AesFree(dec);
  10079. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10080. }
  10081. #else
  10082. if (enc_inited)
  10083. wc_AesFree(enc);
  10084. if (dec_inited)
  10085. wc_AesFree(dec);
  10086. #endif
  10087. return ret;
  10088. }
  10089. #endif
  10090. /* tests that only use 12 byte IV and 16 or less byte AAD
  10091. * test vectors are from NIST SP 800-38D
  10092. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  10093. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  10094. {
  10095. #ifdef WOLFSSL_AES_128
  10096. byte key1[] = {
  10097. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  10098. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  10099. };
  10100. byte iv1[] = {
  10101. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  10102. 0xe4, 0xed, 0x2f, 0x6d
  10103. };
  10104. ALIGN64 byte plain1[] = {
  10105. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  10106. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  10107. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  10108. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  10109. };
  10110. byte aad1[] = {
  10111. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  10112. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  10113. };
  10114. ALIGN64 byte cipher1[] = {
  10115. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  10116. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  10117. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  10118. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  10119. };
  10120. byte tag1[] = {
  10121. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  10122. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  10123. };
  10124. byte key2[] = {
  10125. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  10126. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  10127. };
  10128. byte iv2[] = {
  10129. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  10130. 0xc9, 0x8a, 0xff, 0xe3
  10131. };
  10132. ALIGN64 byte plain2[] = {
  10133. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  10134. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  10135. };
  10136. ALIGN64 byte cipher2[] = {
  10137. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  10138. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  10139. };
  10140. byte tag2[] = {
  10141. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  10142. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  10143. };
  10144. byte key3[] = {
  10145. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  10146. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  10147. };
  10148. byte iv3[] = {
  10149. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  10150. 0x6a, 0x4b, 0xd5, 0xe1
  10151. };
  10152. byte tag3[] = {
  10153. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  10154. 0x11, 0x64, 0xb2, 0xff
  10155. };
  10156. wc_test_ret_t ret;
  10157. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  10158. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  10159. aad1, sizeof(aad1), tag1, sizeof(tag1));
  10160. if (ret != 0) {
  10161. return ret;
  10162. }
  10163. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  10164. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  10165. NULL, 0, tag2, sizeof(tag2));
  10166. if (ret != 0) {
  10167. return ret;
  10168. }
  10169. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  10170. NULL, 0, NULL, 0,
  10171. NULL, 0, tag3, sizeof(tag3));
  10172. if (ret != 0) {
  10173. return ret;
  10174. }
  10175. #endif
  10176. return 0;
  10177. }
  10178. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  10179. {
  10180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10181. Aes *enc = NULL;
  10182. Aes *dec = NULL;
  10183. #else
  10184. Aes enc[1];
  10185. Aes dec[1];
  10186. #endif
  10187. /*
  10188. * This is Test Case 16 from the document Galois/
  10189. * Counter Mode of Operation (GCM) by McGrew and
  10190. * Viega.
  10191. */
  10192. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10193. {
  10194. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  10195. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  10196. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  10197. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  10198. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  10199. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  10200. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  10201. 0xba, 0x63, 0x7b, 0x39
  10202. };
  10203. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10204. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10205. {
  10206. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10207. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10208. 0xab, 0xad, 0xda, 0xd2
  10209. };
  10210. #endif
  10211. #ifdef WOLFSSL_AES_256
  10212. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10213. {
  10214. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10215. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  10216. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10217. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  10218. };
  10219. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10220. {
  10221. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  10222. 0xde, 0xca, 0xf8, 0x88
  10223. };
  10224. #endif /* WOLFSSL_AES_256 */
  10225. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10226. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10227. {
  10228. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  10229. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  10230. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  10231. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  10232. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  10233. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  10234. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  10235. 0xbc, 0xc9, 0xf6, 0x62
  10236. };
  10237. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  10238. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10239. {
  10240. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  10241. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  10242. };
  10243. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  10244. #if !defined(HAVE_FIPS) && \
  10245. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  10246. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  10247. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  10248. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  10249. !(defined(WOLF_CRYPTO_CB) && \
  10250. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  10251. #define ENABLE_NON_12BYTE_IV_TEST
  10252. #ifdef WOLFSSL_AES_192
  10253. /* Test Case 12, uses same plaintext and AAD data. */
  10254. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10255. {
  10256. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10257. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  10258. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  10259. };
  10260. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10261. {
  10262. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  10263. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  10264. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  10265. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  10266. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  10267. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  10268. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  10269. 0xa6, 0x37, 0xb3, 0x9b
  10270. };
  10271. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10272. {
  10273. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  10274. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  10275. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  10276. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  10277. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  10278. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  10279. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  10280. 0xe9, 0xb7, 0x37, 0x3b
  10281. };
  10282. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10283. {
  10284. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  10285. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  10286. };
  10287. #endif /* WOLFSSL_AES_192 */
  10288. #ifdef WOLFSSL_AES_128
  10289. /* The following is an interesting test case from the example
  10290. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  10291. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  10292. {
  10293. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  10294. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  10295. };
  10296. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10297. {
  10298. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  10299. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  10300. };
  10301. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  10302. {
  10303. 0xca
  10304. };
  10305. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10306. {
  10307. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  10308. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  10309. };
  10310. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  10311. {
  10312. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  10313. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  10316. {
  10317. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  10318. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  10319. };
  10320. #endif /* WOLFSSL_AES_128 */
  10321. #ifdef WOLFSSL_AES_256
  10322. int ivlen;
  10323. #endif
  10324. #endif
  10325. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  10326. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  10327. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  10328. wc_test_ret_t ret = 0;
  10329. #ifdef WOLFSSL_AES_256
  10330. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  10331. int alen;
  10332. #endif
  10333. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10334. int plen;
  10335. #endif
  10336. #endif
  10337. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10338. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  10339. byte bufA[sizeof(a) + 1];
  10340. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10341. #endif
  10342. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  10343. #if !defined(BENCH_AESGCM_LARGE)
  10344. #define BENCH_AESGCM_LARGE 1024
  10345. #endif
  10346. #ifndef WOLFSSL_NO_MALLOC
  10347. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10348. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10349. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10350. if ((! large_input) || (! large_output) || (! large_outdec))
  10351. ERROR_OUT(MEMORY_E, out);
  10352. #else
  10353. byte large_input[BENCH_AESGCM_LARGE];
  10354. byte large_output[BENCH_AESGCM_LARGE];
  10355. byte large_outdec[BENCH_AESGCM_LARGE];
  10356. #endif
  10357. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  10358. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  10359. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  10360. #endif
  10361. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10362. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10363. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10364. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10365. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10366. #endif
  10367. XMEMSET(resultT, 0, sizeof(resultT));
  10368. XMEMSET(resultC, 0, sizeof(resultC));
  10369. XMEMSET(resultP, 0, sizeof(resultP));
  10370. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10371. if (ret != 0)
  10372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10373. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10374. if (ret != 0)
  10375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10376. #ifdef WOLFSSL_AES_256
  10377. ret = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10378. if (ret != 0)
  10379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10380. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10381. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  10382. resultT, sizeof(t1), a, sizeof(a));
  10383. #if defined(WOLFSSL_ASYNC_CRYPT)
  10384. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10385. #endif
  10386. if (ret != 0)
  10387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10388. if (XMEMCMP(c1, resultC, sizeof(c1)))
  10389. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10390. if (XMEMCMP(t1, resultT, sizeof(t1)))
  10391. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10392. #ifdef HAVE_AES_DECRYPT
  10393. ret = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10394. if (ret != 0)
  10395. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10396. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  10397. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10398. #if defined(WOLFSSL_ASYNC_CRYPT)
  10399. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10400. #endif
  10401. if (ret != 0)
  10402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10403. if (XMEMCMP(p, resultP, sizeof(p)))
  10404. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10405. #endif /* HAVE_AES_DECRYPT */
  10406. /* Large buffer test */
  10407. #ifdef BENCH_AESGCM_LARGE
  10408. /* setup test buffer */
  10409. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  10410. large_input[alen] = (byte)alen;
  10411. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10412. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  10413. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  10414. resultT, sizeof(t1), a, sizeof(a));
  10415. #if defined(WOLFSSL_ASYNC_CRYPT)
  10416. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10417. #endif
  10418. if (ret != 0)
  10419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10420. #ifdef HAVE_AES_DECRYPT
  10421. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  10422. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  10423. sizeof(t1), a, sizeof(a));
  10424. #if defined(WOLFSSL_ASYNC_CRYPT)
  10425. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10426. #endif
  10427. if (ret != 0)
  10428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10429. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10430. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10431. #endif /* HAVE_AES_DECRYPT */
  10432. #endif /* BENCH_AESGCM_LARGE */
  10433. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  10434. /* Variable IV length test */
  10435. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  10436. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10437. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  10438. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  10439. #if defined(WOLFSSL_ASYNC_CRYPT)
  10440. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10441. #endif
  10442. if (ret != 0)
  10443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10444. #ifdef HAVE_AES_DECRYPT
  10445. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  10446. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  10447. #if defined(WOLFSSL_ASYNC_CRYPT)
  10448. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10449. #endif
  10450. if (ret != 0)
  10451. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10452. #endif /* HAVE_AES_DECRYPT */
  10453. }
  10454. #endif
  10455. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  10456. /* Variable authenticated data length test */
  10457. for (alen=0; alen<(int)sizeof(p); alen++) {
  10458. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10459. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  10460. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  10461. #if defined(WOLFSSL_ASYNC_CRYPT)
  10462. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10463. #endif
  10464. if (ret != 0)
  10465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10466. #ifdef HAVE_AES_DECRYPT
  10467. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  10468. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  10469. #if defined(WOLFSSL_ASYNC_CRYPT)
  10470. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10471. #endif
  10472. if (ret != 0)
  10473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10474. #endif /* HAVE_AES_DECRYPT */
  10475. }
  10476. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10477. if (! large_aad)
  10478. ERROR_OUT(MEMORY_E, out);
  10479. XMEMSET(large_aad, 0, 1024+16);
  10480. /* Variable authenticated data length test */
  10481. for (alen=0; alen<=1024; alen+=16) {
  10482. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10483. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  10484. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  10485. if (ret != 0)
  10486. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10487. #ifdef HAVE_AES_DECRYPT
  10488. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  10489. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  10490. if (ret != 0)
  10491. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10492. #endif /* HAVE_AES_DECRYPT */
  10493. }
  10494. /* Test unaligned memory of all potential arguments */
  10495. ret = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10496. if (ret != 0)
  10497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10498. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10499. XMEMCPY(&buf[1], p, sizeof(p));
  10500. XMEMCPY(&bufA[1], a, sizeof(a));
  10501. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  10502. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  10503. if (ret != 0)
  10504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10505. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  10506. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10507. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  10508. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10509. #ifdef HAVE_AES_DECRYPT
  10510. ret = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10511. if (ret != 0)
  10512. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10513. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  10514. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  10515. if (ret != 0)
  10516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10517. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  10518. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10519. #endif /* HAVE_AES_DECRYPT */
  10520. #endif /* Xilinx Versal */
  10521. #endif
  10522. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10523. #ifdef BENCH_AESGCM_LARGE
  10524. /* Variable plain text length test */
  10525. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  10526. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10527. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  10528. plen, iv1, sizeof(iv1), resultT,
  10529. sizeof(t1), a, sizeof(a));
  10530. #if defined(WOLFSSL_ASYNC_CRYPT)
  10531. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10532. #endif
  10533. if (ret != 0)
  10534. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10535. #ifdef HAVE_AES_DECRYPT
  10536. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  10537. plen, iv1, sizeof(iv1), resultT,
  10538. sizeof(t1), a, sizeof(a));
  10539. #if defined(WOLFSSL_ASYNC_CRYPT)
  10540. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10541. #endif
  10542. if (ret != 0)
  10543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10544. #endif /* HAVE_AES_DECRYPT */
  10545. }
  10546. #else /* BENCH_AESGCM_LARGE */
  10547. /* Variable plain text length test */
  10548. for (plen=1; plen<(int)sizeof(p); plen++) {
  10549. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10550. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  10551. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10552. #if defined(WOLFSSL_ASYNC_CRYPT)
  10553. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10554. #endif
  10555. if (ret != 0)
  10556. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10557. #ifdef HAVE_AES_DECRYPT
  10558. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  10559. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10560. #if defined(WOLFSSL_ASYNC_CRYPT)
  10561. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10562. #endif
  10563. if (ret != 0)
  10564. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10565. #endif /* HAVE_AES_DECRYPT */
  10566. }
  10567. #endif /* BENCH_AESGCM_LARGE */
  10568. #endif
  10569. #endif /* WOLFSSL_AES_256 */
  10570. /* test with IV != 12 bytes */
  10571. #ifdef ENABLE_NON_12BYTE_IV_TEST
  10572. XMEMSET(resultT, 0, sizeof(resultT));
  10573. XMEMSET(resultC, 0, sizeof(resultC));
  10574. XMEMSET(resultP, 0, sizeof(resultP));
  10575. #ifdef WOLFSSL_AES_192
  10576. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  10577. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10578. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  10579. resultT, sizeof(t1), a, sizeof(a));
  10580. #if defined(WOLFSSL_ASYNC_CRYPT)
  10581. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10582. #endif
  10583. if (ret != 0)
  10584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10585. if (XMEMCMP(c2, resultC, sizeof(c2)))
  10586. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10587. if (XMEMCMP(t2, resultT, sizeof(t1)))
  10588. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10589. #ifdef HAVE_AES_DECRYPT
  10590. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  10591. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  10592. #if defined(WOLFSSL_ASYNC_CRYPT)
  10593. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10594. #endif
  10595. if (ret != 0)
  10596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10597. if (XMEMCMP(p, resultP, sizeof(p)))
  10598. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10599. #endif /* HAVE_AES_DECRYPT */
  10600. XMEMSET(resultT, 0, sizeof(resultT));
  10601. XMEMSET(resultC, 0, sizeof(resultC));
  10602. XMEMSET(resultP, 0, sizeof(resultP));
  10603. #endif /* WOLFSSL_AES_192 */
  10604. #ifdef WOLFSSL_AES_128
  10605. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  10606. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10607. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  10608. resultT, sizeof(t3), a3, sizeof(a3));
  10609. #if defined(WOLFSSL_ASYNC_CRYPT)
  10610. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10611. #endif
  10612. if (ret != 0)
  10613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10614. if (XMEMCMP(c3, resultC, sizeof(c3)))
  10615. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10616. if (XMEMCMP(t3, resultT, sizeof(t3)))
  10617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10618. #ifdef HAVE_AES_DECRYPT
  10619. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  10620. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  10621. #if defined(WOLFSSL_ASYNC_CRYPT)
  10622. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10623. #endif
  10624. if (ret != 0)
  10625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10626. if (XMEMCMP(p3, resultP, sizeof(p3)))
  10627. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10628. #endif /* HAVE_AES_DECRYPT */
  10629. #endif /* WOLFSSL_AES_128 */
  10630. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  10631. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  10632. !defined(WOLFSSL_XILINX_CRYPT) && \
  10633. !(defined(WOLF_CRYPTO_CB) && \
  10634. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  10635. XMEMSET(resultT, 0, sizeof(resultT));
  10636. XMEMSET(resultC, 0, sizeof(resultC));
  10637. XMEMSET(resultP, 0, sizeof(resultP));
  10638. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10639. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10640. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  10641. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  10642. #if defined(WOLFSSL_ASYNC_CRYPT)
  10643. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10644. #endif
  10645. if (ret != 0)
  10646. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10647. if (XMEMCMP(c1, resultC, sizeof(c1)))
  10648. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10649. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  10650. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10651. #ifdef HAVE_AES_DECRYPT
  10652. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  10653. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  10654. #if defined(WOLFSSL_ASYNC_CRYPT)
  10655. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10656. #endif
  10657. if (ret != 0)
  10658. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10659. if (XMEMCMP(p, resultP, sizeof(p)))
  10660. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10661. #endif /* HAVE_AES_DECRYPT */
  10662. #endif /* WOLFSSL_AES_256 */
  10663. #if !defined(HAVE_FIPS) || \
  10664. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  10665. /* Test encrypt with internally generated IV */
  10666. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  10667. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  10668. {
  10669. WC_RNG rng;
  10670. byte randIV[12];
  10671. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10672. if (ret != 0)
  10673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10674. XMEMSET(randIV, 0, sizeof(randIV));
  10675. XMEMSET(resultT, 0, sizeof(resultT));
  10676. XMEMSET(resultC, 0, sizeof(resultC));
  10677. XMEMSET(resultP, 0, sizeof(resultP));
  10678. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10679. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  10680. if (ret != 0)
  10681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10682. ret = wc_AesGcmEncrypt_ex(enc,
  10683. resultC, p, sizeof(p),
  10684. randIV, sizeof(randIV),
  10685. resultT, sizeof(t1),
  10686. a, sizeof(a));
  10687. #if defined(WOLFSSL_ASYNC_CRYPT)
  10688. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10689. #endif
  10690. if (ret != 0)
  10691. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10692. /* Check the IV has been set. */
  10693. {
  10694. word32 i, ivSum = 0;
  10695. for (i = 0; i < sizeof(randIV); i++)
  10696. ivSum += randIV[i];
  10697. if (ivSum == 0)
  10698. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10699. }
  10700. #ifdef HAVE_AES_DECRYPT
  10701. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10702. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  10703. if (ret != 0)
  10704. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10705. ret = wc_AesGcmDecrypt(dec,
  10706. resultP, resultC, sizeof(c1),
  10707. randIV, sizeof(randIV),
  10708. resultT, sizeof(t1),
  10709. a, sizeof(a));
  10710. #if defined(WOLFSSL_ASYNC_CRYPT)
  10711. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10712. #endif
  10713. if (ret != 0)
  10714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10715. if (XMEMCMP(p, resultP, sizeof(p)))
  10716. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10717. #endif /* HAVE_AES_DECRYPT */
  10718. wc_FreeRng(&rng);
  10719. }
  10720. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  10721. #endif /* HAVE_FIPS_VERSION >= 2 */
  10722. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10723. #ifdef WOLFSSL_AES_256
  10724. #ifdef WOLFSSL_AESGCM_STREAM
  10725. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10726. if (ret != 0)
  10727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10728. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  10729. if (ret != 0)
  10730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10731. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10732. if (ret != 0)
  10733. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10734. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10735. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10736. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10737. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10738. #ifdef HAVE_AES_DECRYPT
  10739. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10740. if (ret != 0)
  10741. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10742. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  10743. if (ret != 0)
  10744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10745. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10746. if (ret != 0)
  10747. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10748. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10749. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10750. #endif
  10751. /* alen is the size to pass in with each update. */
  10752. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10753. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10754. if (ret != 0)
  10755. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10756. /* plen is the offset into AAD to update with. */
  10757. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10758. int len = sizeof(a) - plen;
  10759. if (len > alen) len = alen;
  10760. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10761. if (ret != 0)
  10762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10763. }
  10764. /* plen is the offset into plaintext to update with. */
  10765. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  10766. int len = sizeof(p) - plen;
  10767. if (len > alen) len = alen;
  10768. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  10769. NULL, 0);
  10770. if (ret != 0)
  10771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10772. }
  10773. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10774. if (ret != 0)
  10775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10776. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10777. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10778. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10779. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10780. }
  10781. #ifdef HAVE_AES_DECRYPT
  10782. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10783. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10784. if (ret != 0)
  10785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10786. /* plen is the offset into AAD to update with. */
  10787. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10788. int len = sizeof(a) - plen;
  10789. if (len > alen) len = alen;
  10790. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10791. if (ret != 0)
  10792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10793. }
  10794. /* plen is the offset into cipher text to update with. */
  10795. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10796. int len = sizeof(c1) - plen;
  10797. if (len > alen) len = alen;
  10798. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10799. NULL, 0);
  10800. if (ret != 0)
  10801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10802. }
  10803. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10804. if (ret != 0)
  10805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10806. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10807. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10808. }
  10809. #endif /* HAVE_AES_DECRYPT */
  10810. #ifdef BENCH_AESGCM_LARGE
  10811. /* setup test buffer */
  10812. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10813. if (ret != 0)
  10814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10815. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10816. BENCH_AESGCM_LARGE, a, sizeof(a));
  10817. if (ret != 0)
  10818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10819. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10820. if (ret != 0)
  10821. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10822. #ifdef HAVE_AES_DECRYPT
  10823. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10824. if (ret != 0)
  10825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10826. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10827. BENCH_AESGCM_LARGE, a, sizeof(a));
  10828. if (ret != 0)
  10829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10830. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10831. if (ret != 0)
  10832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10833. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10834. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10835. #endif /* HAVE_AES_DECRYPT */
  10836. #endif /* BENCH_AESGCM_LARGE */
  10837. #endif /* WOLFSSL_AESGCM_STREAM */
  10838. #endif /* WOLFSSL_AES_256 */
  10839. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10840. wc_AesFree(enc);
  10841. wc_AesFree(dec);
  10842. ret = 0;
  10843. out:
  10844. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10845. !defined(WOLFSSL_NO_MALLOC)
  10846. if (large_input)
  10847. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10848. if (large_output)
  10849. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10850. if (large_outdec)
  10851. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10852. #endif
  10853. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10854. if (enc)
  10855. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10856. if (dec)
  10857. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10858. #endif
  10859. return ret;
  10860. }
  10861. #ifdef WOLFSSL_AES_128
  10862. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  10863. {
  10864. wc_test_ret_t ret;
  10865. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10866. Gmac *gmac;
  10867. #else
  10868. Gmac gmac[1];
  10869. #endif
  10870. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10871. {
  10872. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10873. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10874. };
  10875. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10876. {
  10877. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10878. 0xe2, 0x8c, 0x8f, 0x16
  10879. };
  10880. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10881. {
  10882. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10883. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10884. };
  10885. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10886. {
  10887. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10888. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10889. };
  10890. #if (!defined(HAVE_FIPS) || \
  10891. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10892. /* FIPS builds only allow 16-byte auth tags. */
  10893. /* This sample uses a 15-byte auth tag. */
  10894. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10895. {
  10896. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10897. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10898. };
  10899. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10900. {
  10901. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10902. 0x1a, 0x60, 0x24, 0xa7
  10903. };
  10904. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10905. {
  10906. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10907. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10908. };
  10909. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10910. {
  10911. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10912. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10913. };
  10914. #endif
  10915. byte tag[16];
  10916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10917. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10918. return WC_TEST_RET_ENC_ERRNO;
  10919. #endif
  10920. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10921. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10922. XMEMSET(tag, 0, sizeof(tag));
  10923. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10924. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10925. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10926. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10927. #if (!defined(HAVE_FIPS) || \
  10928. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10929. XMEMSET(tag, 0, sizeof(tag));
  10930. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10931. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10932. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10933. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10934. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10935. {
  10936. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10937. {
  10938. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10939. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10940. };
  10941. WC_RNG rng;
  10942. byte iv[12];
  10943. #ifndef HAVE_FIPS
  10944. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10945. if (ret != 0)
  10946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10947. #else
  10948. ret = wc_InitRng(&rng);
  10949. if (ret != 0)
  10950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10951. #endif
  10952. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10953. t1, sizeof(t1));
  10954. if (ret != 0)
  10955. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10956. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10957. badT, sizeof(badT));
  10958. if (ret != AES_GCM_AUTH_E)
  10959. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10960. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10961. t2, sizeof(t2));
  10962. if (ret != 0)
  10963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10964. XMEMSET(tag, 0, sizeof(tag));
  10965. XMEMSET(iv, 0, sizeof(iv));
  10966. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10967. tag, sizeof(tag), &rng);
  10968. if (ret != 0)
  10969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10970. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10971. tag, sizeof(tag));
  10972. if (ret != 0)
  10973. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10974. wc_FreeRng(&rng);
  10975. }
  10976. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10977. #endif /* HAVE_FIPS */
  10978. ret = 0;
  10979. out:
  10980. wc_AesFree(&gmac->aes);
  10981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10982. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10983. #endif
  10984. return ret;
  10985. }
  10986. #endif /* WOLFSSL_AES_128 */
  10987. #endif /* HAVE_AESGCM */
  10988. #if defined(HAVE_AESCCM)
  10989. #if defined(WOLFSSL_AES_256)
  10990. static wc_test_ret_t aesccm_256_test(void)
  10991. {
  10992. wc_test_ret_t ret;
  10993. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10994. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10995. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10996. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10997. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10998. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10999. };
  11000. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  11001. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  11002. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  11003. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  11004. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  11005. 0x20, 0x21, 0x22, 0x23};
  11006. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  11007. 0x8A, 0xB1, 0xA8, 0x74};
  11008. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  11009. 0x95, 0xFC, 0x08, 0x20};
  11010. byte output[sizeof(in_plaintext)];
  11011. byte atag[sizeof(exp_tag)];
  11012. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11013. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  11014. if (aes == NULL) {
  11015. return MEMORY_E;
  11016. }
  11017. #else
  11018. Aes aes[1];
  11019. #endif
  11020. ret = wc_AesInit(aes, HEAP_HINT, devId);
  11021. if (ret == 0) {
  11022. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  11023. }
  11024. if (ret == 0) {
  11025. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  11026. in_nonce, sizeof(in_nonce),
  11027. atag, sizeof(atag),
  11028. in_auth, sizeof(in_auth));
  11029. }
  11030. /* Verify we produce the proper ciphertext and tag */
  11031. if (ret == 0 &&
  11032. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  11033. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  11034. ret = WC_TEST_RET_ENC_NC;
  11035. }
  11036. if (ret == 0) {
  11037. /* decrypt inline */
  11038. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  11039. in_nonce, sizeof(in_nonce),
  11040. atag, sizeof(atag),
  11041. in_auth, sizeof(in_auth));
  11042. }
  11043. /* Verify decryption was successful */
  11044. if (ret == 0 &&
  11045. XMEMCMP(output, in_plaintext, sizeof(output))) {
  11046. ret = WC_TEST_RET_ENC_NC;
  11047. }
  11048. wc_AesFree(aes);
  11049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11050. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11051. #endif
  11052. return ret;
  11053. }
  11054. #endif /* WOLFSSL_AES_256 */
  11055. #if defined(WOLFSSL_AES_128)
  11056. static wc_test_ret_t aesccm_128_test(void)
  11057. {
  11058. wc_test_ret_t ret;
  11059. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11060. Aes *enc;
  11061. #else
  11062. Aes enc[1];
  11063. #endif
  11064. /* key */
  11065. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  11066. {
  11067. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  11068. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  11069. };
  11070. /* nonce */
  11071. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  11072. {
  11073. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  11074. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  11075. };
  11076. /* plaintext */
  11077. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  11078. {
  11079. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11080. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11081. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  11082. };
  11083. /* plaintext - long */
  11084. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  11085. {
  11086. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11087. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11088. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  11089. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  11090. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  11091. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  11092. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  11093. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  11094. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  11095. 0x50
  11096. };
  11097. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  11098. {
  11099. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11100. };
  11101. /* ciphertext */
  11102. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  11103. {
  11104. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11105. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11106. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  11107. };
  11108. /* tag - authentication */
  11109. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  11110. {
  11111. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  11112. };
  11113. /* ciphertext - long */
  11114. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  11115. {
  11116. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11117. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11118. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  11119. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  11120. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  11121. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  11122. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  11123. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  11124. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  11125. 0x0b
  11126. };
  11127. /* tag - authentication - long */
  11128. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  11129. {
  11130. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  11131. };
  11132. /* tag - authentication - empty plaintext */
  11133. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  11134. {
  11135. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  11136. };
  11137. byte t2[sizeof(t)];
  11138. byte p2[sizeof(p)];
  11139. byte c2[sizeof(c)];
  11140. byte iv2[sizeof(iv)];
  11141. byte pl2[sizeof(pl)];
  11142. byte cl2[sizeof(cl)];
  11143. byte tl2[sizeof(tl)];
  11144. byte t_empty2[sizeof(t_empty)];
  11145. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11146. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11147. return WC_TEST_RET_ENC_ERRNO;
  11148. #endif
  11149. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  11150. XMEMSET(t2, 0, sizeof(t2));
  11151. XMEMSET(c2, 0, sizeof(c2));
  11152. XMEMSET(p2, 0, sizeof(p2));
  11153. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11154. if (ret != 0)
  11155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11156. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11157. if (ret != 0)
  11158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11159. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11160. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  11161. t2, sizeof(t2), a, sizeof(a));
  11162. if (ret != 0)
  11163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11164. if (XMEMCMP(c, c2, sizeof(c2)))
  11165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11166. if (XMEMCMP(t, t2, sizeof(t2)))
  11167. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11168. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  11169. t2, sizeof(t2), a, sizeof(a));
  11170. if (ret != 0)
  11171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11172. if (XMEMCMP(p, p2, sizeof(p2)))
  11173. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11174. /* Test the authentication failure */
  11175. t2[0]++; /* Corrupt the authentication tag. */
  11176. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  11177. t2, sizeof(t2), a, sizeof(a));
  11178. if (ret == 0)
  11179. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11180. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  11181. * authentication fail. */
  11182. XMEMSET(c2, 0, sizeof(c2));
  11183. if (XMEMCMP(p2, c2, sizeof(p2)))
  11184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11185. wc_AesFree(enc);
  11186. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  11187. XMEMSET(t2, 0, sizeof(t2));
  11188. XMEMSET(c2, 0, sizeof(c2));
  11189. XMEMSET(p2, 0, sizeof(p2));
  11190. XMEMSET(iv2, 0, sizeof(iv2));
  11191. #ifndef HAVE_SELFTEST
  11192. /* selftest build does not have wc_AesCcmSetNonce() or
  11193. * wc_AesCcmEncrypt_ex() */
  11194. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11195. if (ret != 0)
  11196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11197. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  11198. if (ret != 0)
  11199. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11200. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  11201. t2, sizeof(t2), a, sizeof(a));
  11202. if (ret != 0)
  11203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11204. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  11205. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11206. if (XMEMCMP(c, c2, sizeof(c2)))
  11207. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11208. if (XMEMCMP(t, t2, sizeof(t2)))
  11209. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11210. #endif
  11211. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11212. /* test fail on invalid IV sizes */
  11213. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11214. if (ret != 0)
  11215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11216. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11217. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  11218. t2, 1, a, sizeof(a));
  11219. if (ret == 0) {
  11220. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11221. }
  11222. #endif
  11223. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11224. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  11225. tl2, sizeof(tl2), a, sizeof(a));
  11226. if (ret != 0)
  11227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11228. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  11229. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11230. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  11231. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11232. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  11233. tl2, sizeof(tl2), a, sizeof(a));
  11234. if (ret != 0)
  11235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11236. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  11237. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11238. /* test empty message as null input or output with nonzero inSz. */
  11239. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  11240. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11241. a, sizeof(a));
  11242. if (ret != BAD_FUNC_ARG)
  11243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11244. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  11245. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11246. a, sizeof(a));
  11247. if (ret != BAD_FUNC_ARG)
  11248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11249. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  11250. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11251. sizeof(a));
  11252. if (ret != BAD_FUNC_ARG)
  11253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11254. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  11255. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11256. sizeof(a));
  11257. if (ret != BAD_FUNC_ARG)
  11258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11259. /* test empty message as null input and output with zero inSz --
  11260. * must either succeed, or fail early with BAD_FUNC_ARG.
  11261. */
  11262. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  11263. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11264. a, sizeof(a));
  11265. if (ret != BAD_FUNC_ARG) {
  11266. if (ret != 0)
  11267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11268. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  11269. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11270. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  11271. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  11272. sizeof(t_empty2), a, sizeof(a));
  11273. if (ret != 0)
  11274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11275. }
  11276. /* test empty message as zero-length string -- must work. */
  11277. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  11278. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11279. sizeof(a));
  11280. if (ret != 0)
  11281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11282. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  11283. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11284. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  11285. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11286. sizeof(a));
  11287. if (ret != 0)
  11288. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11289. wc_AesFree(enc);
  11290. ret = 0;
  11291. out:
  11292. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11293. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11294. #endif
  11295. return ret;
  11296. }
  11297. #endif /* WOLFSSL_AES_128 */
  11298. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  11299. {
  11300. wc_test_ret_t ret = 0;
  11301. #ifdef WOLFSSL_AES_128
  11302. if (ret == 0)
  11303. ret = aesccm_128_test();
  11304. #endif
  11305. #ifdef WOLFSSL_AES_256
  11306. if (ret == 0)
  11307. ret = aesccm_256_test();
  11308. #endif
  11309. return ret;
  11310. }
  11311. #endif /* HAVE_AESCCM */
  11312. #ifdef HAVE_AES_KEYWRAP
  11313. #define MAX_KEYWRAP_TEST_OUTLEN 40
  11314. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  11315. typedef struct keywrapVector {
  11316. const byte* kek;
  11317. const byte* data;
  11318. const byte* verify;
  11319. word32 kekLen;
  11320. word32 dataLen;
  11321. word32 verifyLen;
  11322. } keywrapVector;
  11323. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  11324. {
  11325. int wrapSz, plainSz, testSz, i;
  11326. /* test vectors from RFC 3394 (kek, data, verify) */
  11327. #ifdef WOLFSSL_AES_128
  11328. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  11329. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11330. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11331. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11332. };
  11333. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  11334. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11335. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11336. };
  11337. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  11338. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  11339. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  11340. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  11341. };
  11342. #endif /* WOLFSSL_AES_128 */
  11343. #ifdef WOLFSSL_AES_192
  11344. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  11345. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  11346. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11347. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11348. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  11349. };
  11350. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  11351. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11352. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11353. };
  11354. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  11355. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  11356. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  11357. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  11358. };
  11359. #endif
  11360. #ifdef WOLFSSL_AES_256
  11361. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  11362. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  11363. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11364. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11365. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11366. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11367. };
  11368. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  11369. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11370. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11371. };
  11372. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  11373. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  11374. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  11375. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  11376. };
  11377. #endif
  11378. #ifdef WOLFSSL_AES_192
  11379. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  11380. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  11381. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11382. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11383. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  11384. };
  11385. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  11386. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11387. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11388. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11389. };
  11390. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  11391. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  11392. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  11393. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  11394. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  11395. };
  11396. #endif
  11397. #ifdef WOLFSSL_AES_256
  11398. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  11399. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  11400. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11401. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11402. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11403. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11404. };
  11405. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  11406. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11407. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11408. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11409. };
  11410. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  11411. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  11412. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  11413. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  11414. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  11415. };
  11416. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  11417. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  11418. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11419. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11420. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11421. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11422. };
  11423. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  11424. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11425. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11426. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11427. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11428. };
  11429. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  11430. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  11431. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  11432. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  11433. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  11434. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  11435. };
  11436. #endif /* WOLFSSL_AES_256 */
  11437. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  11438. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  11439. const keywrapVector test_wrap[] =
  11440. {
  11441. #ifdef WOLFSSL_AES_128
  11442. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  11443. #endif
  11444. #ifdef WOLFSSL_AES_192
  11445. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  11446. #endif
  11447. #ifdef WOLFSSL_AES_256
  11448. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  11449. #endif
  11450. #ifdef WOLFSSL_AES_192
  11451. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  11452. #endif
  11453. #ifdef WOLFSSL_AES_256
  11454. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  11455. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  11456. #endif
  11457. };
  11458. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  11459. XMEMSET(output, 0, sizeof(output));
  11460. XMEMSET(plain, 0, sizeof(plain));
  11461. for (i = 0; i < testSz; i++) {
  11462. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  11463. test_wrap[i].data, test_wrap[i].dataLen,
  11464. output, sizeof(output), NULL);
  11465. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  11466. return WC_TEST_RET_ENC_NC;
  11467. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  11468. return WC_TEST_RET_ENC_NC;
  11469. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  11470. output, wrapSz,
  11471. plain, sizeof(plain), NULL);
  11472. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  11473. return WC_TEST_RET_ENC_NC;
  11474. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  11475. return WC_TEST_RET_ENC_I(i);
  11476. }
  11477. return 0;
  11478. }
  11479. #endif /* HAVE_AES_KEYWRAP */
  11480. #endif /* NO_AES */
  11481. #ifdef HAVE_CAMELLIA
  11482. enum {
  11483. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  11484. };
  11485. typedef struct {
  11486. int type;
  11487. const byte* plaintext;
  11488. const byte* iv;
  11489. const byte* ciphertext;
  11490. const byte* key;
  11491. word32 keySz;
  11492. int errorCode;
  11493. } test_vector_t;
  11494. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  11495. {
  11496. /* Camellia ECB Test Plaintext */
  11497. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  11498. {
  11499. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11500. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  11501. };
  11502. /* Camellia ECB Test Initialization Vector */
  11503. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  11504. /* Test 1: Camellia ECB 128-bit key */
  11505. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  11506. {
  11507. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11508. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  11509. };
  11510. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  11511. {
  11512. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  11513. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  11514. };
  11515. /* Test 2: Camellia ECB 192-bit key */
  11516. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  11517. {
  11518. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11519. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  11520. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  11521. };
  11522. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  11523. {
  11524. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  11525. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  11526. };
  11527. /* Test 3: Camellia ECB 256-bit key */
  11528. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  11529. {
  11530. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11531. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  11532. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11533. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  11534. };
  11535. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  11536. {
  11537. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  11538. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  11539. };
  11540. /* Camellia CBC Test Plaintext */
  11541. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  11542. {
  11543. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  11544. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  11545. };
  11546. /* Camellia CBC Test Initialization Vector */
  11547. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  11548. {
  11549. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11550. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11551. };
  11552. /* Test 4: Camellia-CBC 128-bit key */
  11553. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  11554. {
  11555. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  11556. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  11557. };
  11558. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  11559. {
  11560. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  11561. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  11562. };
  11563. /* Test 5: Camellia-CBC 192-bit key */
  11564. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  11565. {
  11566. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  11567. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  11568. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  11569. };
  11570. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  11571. {
  11572. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  11573. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  11574. };
  11575. /* Test 6: CBC 256-bit key */
  11576. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  11577. {
  11578. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  11579. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  11580. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  11581. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  11582. };
  11583. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  11584. {
  11585. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  11586. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  11587. };
  11588. byte out[CAMELLIA_BLOCK_SIZE];
  11589. Camellia cam;
  11590. int i, testsSz, ret;
  11591. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  11592. {
  11593. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  11594. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  11595. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  11596. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  11597. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  11598. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  11599. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  11600. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  11601. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  11602. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  11603. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  11604. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  11605. };
  11606. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  11607. for (i = 0; i < testsSz; i++) {
  11608. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  11609. testVectors[i].iv) != 0)
  11610. return testVectors[i].errorCode;
  11611. switch (testVectors[i].type) {
  11612. case CAM_ECB_ENC:
  11613. ret = wc_CamelliaEncryptDirect(&cam, out,
  11614. testVectors[i].plaintext);
  11615. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  11616. CAMELLIA_BLOCK_SIZE))
  11617. return testVectors[i].errorCode;
  11618. break;
  11619. case CAM_ECB_DEC:
  11620. ret = wc_CamelliaDecryptDirect(&cam, out,
  11621. testVectors[i].ciphertext);
  11622. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  11623. CAMELLIA_BLOCK_SIZE))
  11624. return testVectors[i].errorCode;
  11625. break;
  11626. case CAM_CBC_ENC:
  11627. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  11628. CAMELLIA_BLOCK_SIZE);
  11629. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  11630. CAMELLIA_BLOCK_SIZE))
  11631. return testVectors[i].errorCode;
  11632. break;
  11633. case CAM_CBC_DEC:
  11634. ret = wc_CamelliaCbcDecrypt(&cam, out,
  11635. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  11636. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  11637. CAMELLIA_BLOCK_SIZE))
  11638. return testVectors[i].errorCode;
  11639. break;
  11640. default:
  11641. break;
  11642. }
  11643. }
  11644. /* Setting the IV and checking it was actually set. */
  11645. ret = wc_CamelliaSetIV(&cam, ivc);
  11646. if (ret != 0)
  11647. return WC_TEST_RET_ENC_EC(ret);
  11648. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  11649. return WC_TEST_RET_ENC_NC;
  11650. /* Setting the IV to NULL should be same as all zeros IV */
  11651. ret = wc_CamelliaSetIV(&cam, NULL);
  11652. if (ret != 0)
  11653. return WC_TEST_RET_ENC_EC(ret);
  11654. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  11655. return WC_TEST_RET_ENC_NC;
  11656. /* First parameter should never be null */
  11657. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  11658. return WC_TEST_RET_ENC_NC;
  11659. /* First parameter should never be null, check it fails */
  11660. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  11661. return WC_TEST_RET_ENC_NC;
  11662. /* Key should have a size of 16, 24, or 32 */
  11663. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  11664. return WC_TEST_RET_ENC_NC;
  11665. return 0;
  11666. }
  11667. #endif /* HAVE_CAMELLIA */
  11668. #ifdef WOLFSSL_SM4
  11669. #ifdef WOLFSSL_SM4_ECB
  11670. static int sm4_ecb_test(void)
  11671. {
  11672. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  11673. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11674. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11675. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11676. };
  11677. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11678. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11679. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11680. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11681. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11682. };
  11683. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  11684. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  11685. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  11686. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  11687. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  11688. };
  11689. wc_Sm4 sm4;
  11690. byte enc[SM4_BLOCK_SIZE * 4];
  11691. byte dec[SM4_BLOCK_SIZE * 4];
  11692. int ret;
  11693. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11694. if (ret != 0)
  11695. return WC_TEST_RET_ENC_EC(ret);
  11696. /* Encrypt and decrypt with ECB. */
  11697. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11698. if (ret != 0)
  11699. return WC_TEST_RET_ENC_EC(ret);
  11700. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  11701. if (ret != 0)
  11702. return WC_TEST_RET_ENC_EC(ret);
  11703. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  11704. return WC_TEST_RET_ENC_NC;
  11705. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  11706. if (ret != 0)
  11707. return WC_TEST_RET_ENC_EC(ret);
  11708. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  11709. return WC_TEST_RET_ENC_NC;
  11710. wc_Sm4Free(&sm4);
  11711. return 0;
  11712. }
  11713. #endif
  11714. #ifdef WOLFSSL_SM4_CBC
  11715. static int sm4_cbc_test(void)
  11716. {
  11717. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  11718. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11719. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11720. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11721. };
  11722. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11723. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11724. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11725. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11726. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11727. };
  11728. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11729. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11730. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11731. };
  11732. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  11733. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  11734. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  11735. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  11736. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  11737. };
  11738. wc_Sm4 sm4;
  11739. byte enc[SM4_BLOCK_SIZE * 4];
  11740. byte dec[SM4_BLOCK_SIZE * 4];
  11741. int ret;
  11742. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11743. if (ret != 0)
  11744. return WC_TEST_RET_ENC_EC(ret);
  11745. /* Encrypt and decrypt with CBC. */
  11746. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11747. if (ret != 0)
  11748. return WC_TEST_RET_ENC_EC(ret);
  11749. ret = wc_Sm4SetIV(&sm4, i1);
  11750. if (ret != 0)
  11751. return WC_TEST_RET_ENC_EC(ret);
  11752. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  11753. if (ret != 0)
  11754. return WC_TEST_RET_ENC_EC(ret);
  11755. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  11756. return WC_TEST_RET_ENC_NC;
  11757. ret = wc_Sm4SetIV(&sm4, i1);
  11758. if (ret != 0)
  11759. return WC_TEST_RET_ENC_EC(ret);
  11760. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  11761. if (ret != 0)
  11762. return WC_TEST_RET_ENC_EC(ret);
  11763. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  11764. return WC_TEST_RET_ENC_NC;
  11765. /* Encrypt and decrypt in-place with CBC. */
  11766. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11767. if (ret != 0)
  11768. return WC_TEST_RET_ENC_EC(ret);
  11769. ret = wc_Sm4SetIV(&sm4, i1);
  11770. if (ret != 0)
  11771. return WC_TEST_RET_ENC_EC(ret);
  11772. XMEMCPY(enc, p1, sizeof(p1));
  11773. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  11774. if (ret != 0)
  11775. return WC_TEST_RET_ENC_EC(ret);
  11776. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  11777. return WC_TEST_RET_ENC_NC;
  11778. ret = wc_Sm4SetIV(&sm4, i1);
  11779. if (ret != 0)
  11780. return WC_TEST_RET_ENC_EC(ret);
  11781. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  11782. if (ret != 0)
  11783. return WC_TEST_RET_ENC_EC(ret);
  11784. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  11785. return WC_TEST_RET_ENC_NC;
  11786. wc_Sm4Free(&sm4);
  11787. return 0;
  11788. }
  11789. #endif
  11790. #ifdef WOLFSSL_SM4_CTR
  11791. static int sm4_ctr_test(void)
  11792. {
  11793. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  11794. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11795. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11796. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11797. };
  11798. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11799. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11800. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11801. };
  11802. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  11803. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11804. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  11805. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  11806. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  11807. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  11808. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  11809. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11810. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  11811. };
  11812. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  11813. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  11814. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  11815. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  11816. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  11817. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  11818. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  11819. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  11820. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  11821. };
  11822. wc_Sm4 sm4;
  11823. byte enc[SM4_BLOCK_SIZE * 4];
  11824. byte dec[SM4_BLOCK_SIZE * 4];
  11825. int chunk;
  11826. int i;
  11827. int ret;
  11828. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11829. if (ret != 0)
  11830. return WC_TEST_RET_ENC_EC(ret);
  11831. /* Encrypt and decrypt using encrypt with CTR. */
  11832. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11833. if (ret != 0)
  11834. return WC_TEST_RET_ENC_EC(ret);
  11835. ret = wc_Sm4SetIV(&sm4, i1);
  11836. if (ret != 0)
  11837. return WC_TEST_RET_ENC_EC(ret);
  11838. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  11839. if (ret != 0)
  11840. return WC_TEST_RET_ENC_EC(ret);
  11841. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  11842. return WC_TEST_RET_ENC_NC;
  11843. ret = wc_Sm4SetIV(&sm4, i1);
  11844. if (ret != 0)
  11845. return WC_TEST_RET_ENC_EC(ret);
  11846. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  11847. if (ret != 0)
  11848. return WC_TEST_RET_ENC_EC(ret);
  11849. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  11850. return WC_TEST_RET_ENC_NC;
  11851. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  11852. ret = wc_Sm4SetIV(&sm4, i1);
  11853. if (ret != 0)
  11854. return WC_TEST_RET_ENC_I(chunk);
  11855. XMEMSET(enc, 0, sizeof(enc));
  11856. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  11857. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  11858. if (ret != 0)
  11859. return WC_TEST_RET_ENC_I(i);
  11860. }
  11861. if (i < (int)sizeof(p2)) {
  11862. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  11863. if (ret != 0)
  11864. return WC_TEST_RET_ENC_I(chunk);
  11865. }
  11866. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  11867. return WC_TEST_RET_ENC_I(chunk);
  11868. }
  11869. wc_Sm4Free(&sm4);
  11870. return 0;
  11871. }
  11872. #endif
  11873. #ifdef WOLFSSL_SM4_GCM
  11874. static int sm4_gcm_test(void)
  11875. {
  11876. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11877. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11878. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11879. };
  11880. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11881. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11882. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11883. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11884. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11885. };
  11886. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11887. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11888. 0x08, 0x09, 0x0A, 0x0B
  11889. };
  11890. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  11891. 0xFF, 0xEE, 0xDD
  11892. };
  11893. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  11894. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  11895. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  11896. };
  11897. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  11898. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  11899. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  11900. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  11901. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  11902. };
  11903. /* RFC8998 A.1. */
  11904. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  11905. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  11906. 0x00, 0x00, 0xAB, 0xCD
  11907. };
  11908. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  11909. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11910. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11911. };
  11912. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  11913. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11914. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  11915. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  11916. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  11917. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  11918. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  11919. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  11920. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  11921. };
  11922. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  11923. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  11924. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  11925. 0xAB, 0xAD, 0xDA, 0xD2
  11926. };
  11927. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  11928. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  11929. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  11930. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  11931. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  11932. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  11933. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  11934. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  11935. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  11936. };
  11937. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  11938. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  11939. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  11940. };
  11941. wc_Sm4 sm4;
  11942. byte enc[SM4_BLOCK_SIZE * 4];
  11943. byte dec[SM4_BLOCK_SIZE * 4];
  11944. byte tag[SM4_BLOCK_SIZE];
  11945. int ret;
  11946. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11947. if (ret != 0)
  11948. return WC_TEST_RET_ENC_EC(ret);
  11949. /* Encrypt and decrypt using encrypt with GCM. */
  11950. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  11951. if (ret != 0)
  11952. return WC_TEST_RET_ENC_EC(ret);
  11953. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  11954. sizeof(tag), a1, sizeof(a1));
  11955. if (ret != 0)
  11956. return WC_TEST_RET_ENC_EC(ret);
  11957. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  11958. return WC_TEST_RET_ENC_NC;
  11959. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  11960. return WC_TEST_RET_ENC_NC;
  11961. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  11962. sizeof(tag), a1, sizeof(a1));
  11963. if (ret != 0)
  11964. return WC_TEST_RET_ENC_EC(ret);
  11965. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  11966. return WC_TEST_RET_ENC_NC;
  11967. /* RFC8998 test vector. */
  11968. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  11969. if (ret != 0)
  11970. return WC_TEST_RET_ENC_EC(ret);
  11971. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  11972. sizeof(tag), a2, sizeof(a2));
  11973. if (ret != 0)
  11974. return WC_TEST_RET_ENC_EC(ret);
  11975. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  11976. return WC_TEST_RET_ENC_NC;
  11977. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  11978. return WC_TEST_RET_ENC_NC;
  11979. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  11980. sizeof(tag), a2, sizeof(a2));
  11981. if (ret != 0)
  11982. return WC_TEST_RET_ENC_EC(ret);
  11983. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  11984. return WC_TEST_RET_ENC_NC;
  11985. wc_Sm4Free(&sm4);
  11986. return 0;
  11987. }
  11988. #endif
  11989. #ifdef WOLFSSL_SM4_CCM
  11990. static int sm4_ccm_test(void)
  11991. {
  11992. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11993. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11994. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11995. };
  11996. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11997. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11998. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11999. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  12000. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  12001. };
  12002. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  12003. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12004. 0x08, 0x09, 0x0A, 0x0B
  12005. };
  12006. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  12007. 0xFF, 0xEE, 0xDD
  12008. };
  12009. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  12010. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  12011. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  12012. };
  12013. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  12014. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  12015. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  12016. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  12017. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  12018. };
  12019. /* RFC8998 A.1. */
  12020. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  12021. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  12022. 0x00, 0x00, 0xAB, 0xCD
  12023. };
  12024. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  12025. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12026. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12027. };
  12028. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  12029. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  12030. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  12031. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  12032. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  12033. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12034. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  12035. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12036. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  12037. };
  12038. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  12039. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12040. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12041. 0xAB, 0xAD, 0xDA, 0xD2
  12042. };
  12043. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  12044. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  12045. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  12046. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  12047. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  12048. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  12049. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  12050. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  12051. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  12052. };
  12053. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  12054. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  12055. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  12056. };
  12057. wc_Sm4 sm4;
  12058. byte enc[SM4_BLOCK_SIZE * 4];
  12059. byte dec[SM4_BLOCK_SIZE * 4];
  12060. byte tag[SM4_BLOCK_SIZE];
  12061. int ret;
  12062. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12063. if (ret != 0)
  12064. return -6720;
  12065. /* Encrypt and decrypt using encrypt with CCM. */
  12066. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12067. if (ret != 0)
  12068. return WC_TEST_RET_ENC_EC(ret);
  12069. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  12070. sizeof(tag), a1, sizeof(a1));
  12071. if (ret != 0)
  12072. return WC_TEST_RET_ENC_EC(ret);
  12073. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  12074. return WC_TEST_RET_ENC_NC;
  12075. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  12076. return WC_TEST_RET_ENC_NC;
  12077. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  12078. sizeof(tag), a1, sizeof(a1));
  12079. if (ret != 0)
  12080. return WC_TEST_RET_ENC_EC(ret);
  12081. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  12082. return WC_TEST_RET_ENC_NC;
  12083. /* RFC8998 test vector. */
  12084. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  12085. if (ret != 0)
  12086. return WC_TEST_RET_ENC_EC(ret);
  12087. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  12088. sizeof(tag), a2, sizeof(a2));
  12089. if (ret != 0)
  12090. return WC_TEST_RET_ENC_EC(ret);
  12091. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  12092. return WC_TEST_RET_ENC_NC;
  12093. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  12094. return WC_TEST_RET_ENC_NC;
  12095. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  12096. sizeof(tag), a2, sizeof(a2));
  12097. if (ret != 0)
  12098. return WC_TEST_RET_ENC_EC(ret);
  12099. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  12100. return WC_TEST_RET_ENC_NC;
  12101. wc_Sm4Free(&sm4);
  12102. return 0;
  12103. }
  12104. #endif
  12105. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  12106. {
  12107. wc_test_ret_t ret;
  12108. #ifdef WOLFSSL_SM4_ECB
  12109. ret = sm4_ecb_test();
  12110. if (ret != 0)
  12111. return ret;
  12112. #endif
  12113. #ifdef WOLFSSL_SM4_CBC
  12114. ret = sm4_cbc_test();
  12115. if (ret != 0)
  12116. return ret;
  12117. #endif
  12118. #ifdef WOLFSSL_SM4_CTR
  12119. ret = sm4_ctr_test();
  12120. if (ret != 0)
  12121. return ret;
  12122. #endif
  12123. #ifdef WOLFSSL_SM4_GCM
  12124. ret = sm4_gcm_test();
  12125. if (ret != 0)
  12126. return ret;
  12127. #endif
  12128. #ifdef WOLFSSL_SM4_CCM
  12129. ret = sm4_ccm_test();
  12130. if (ret != 0)
  12131. return ret;
  12132. #endif
  12133. return 0;
  12134. }
  12135. #endif
  12136. #ifdef HAVE_XCHACHA
  12137. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  12138. wc_test_ret_t ret;
  12139. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  12140. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  12141. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  12142. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  12143. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  12144. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  12145. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  12146. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  12147. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  12148. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  12149. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  12150. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  12151. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  12152. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  12153. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  12154. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  12155. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  12156. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  12157. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  12158. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  12159. };
  12160. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  12161. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  12162. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  12163. };
  12164. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  12165. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  12166. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  12167. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  12168. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  12169. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  12170. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  12171. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  12172. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  12173. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  12174. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  12175. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  12176. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  12177. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  12178. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  12179. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  12180. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  12181. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  12182. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  12183. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  12184. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  12185. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  12186. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  12187. };
  12188. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12189. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  12190. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12191. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12192. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  12193. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12194. #else
  12195. struct ChaCha chacha[1];
  12196. byte buf1[sizeof Plaintext];
  12197. byte buf2[sizeof Plaintext];
  12198. #endif
  12199. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  12200. if (ret < 0)
  12201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12202. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  12203. if (ret < 0)
  12204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12205. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  12206. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12207. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  12208. if (ret < 0)
  12209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12210. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  12211. if (ret < 0)
  12212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12213. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  12214. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12215. out:
  12216. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12217. if (chacha)
  12218. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  12219. if (buf1)
  12220. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12221. if (buf2)
  12222. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12223. #endif
  12224. return ret;
  12225. }
  12226. #endif /* HAVE_XCHACHA */
  12227. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  12228. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  12229. wc_test_ret_t ret;
  12230. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  12231. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  12232. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  12233. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  12234. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  12235. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  12236. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  12237. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  12238. 0x74, 0x2e }; /* t. */
  12239. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  12240. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  12241. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  12242. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  12243. };
  12244. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  12245. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  12246. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  12247. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  12248. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  12249. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  12250. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  12251. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  12252. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  12253. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  12254. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  12255. 0xb5, 0x2e };
  12256. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  12257. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  12258. };
  12259. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12260. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12261. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12262. if ((buf1 == NULL) || (buf2 == NULL))
  12263. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12264. #else
  12265. byte buf1[sizeof Ciphertext + sizeof Tag];
  12266. byte buf2[sizeof Plaintext];
  12267. #endif
  12268. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  12269. Plaintext, sizeof Plaintext,
  12270. AAD, sizeof AAD,
  12271. IV, sizeof IV,
  12272. Key, sizeof Key);
  12273. if (ret < 0)
  12274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12275. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  12276. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12277. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  12278. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12279. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  12280. buf1, sizeof Ciphertext + sizeof Tag,
  12281. AAD, sizeof AAD,
  12282. IV, sizeof IV,
  12283. Key, sizeof Key);
  12284. if (ret < 0)
  12285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12286. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  12287. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12288. out:
  12289. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12290. if (buf1 != NULL)
  12291. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12292. if (buf2 != NULL)
  12293. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12294. #endif
  12295. return ret;
  12296. }
  12297. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  12298. #ifndef WC_NO_RNG
  12299. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  12300. {
  12301. byte block[32];
  12302. wc_test_ret_t ret;
  12303. int i;
  12304. XMEMSET(block, 0, sizeof(block));
  12305. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  12306. if (ret != 0) {
  12307. ret = 1;
  12308. goto exit;
  12309. }
  12310. /* Check for 0's */
  12311. for (i=0; i<(int)sizeof(block); i++) {
  12312. if (block[i] == 0) {
  12313. ret++;
  12314. }
  12315. }
  12316. /* All zeros count check */
  12317. if (ret >= (int)sizeof(block)) {
  12318. ret = 2;
  12319. goto exit;
  12320. }
  12321. ret = wc_RNG_GenerateByte(rng, block);
  12322. if (ret != 0) {
  12323. ret = 3;
  12324. goto exit;
  12325. }
  12326. /* Parameter validation testing. */
  12327. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  12328. if (ret != BAD_FUNC_ARG) {
  12329. ret = 4;
  12330. goto exit;
  12331. }
  12332. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  12333. if (ret != BAD_FUNC_ARG) {
  12334. ret = 5;
  12335. goto exit;
  12336. }
  12337. ret = wc_RNG_GenerateByte(NULL, block);
  12338. if (ret != BAD_FUNC_ARG) {
  12339. ret = 6;
  12340. goto exit;
  12341. }
  12342. ret = wc_RNG_GenerateByte(rng, NULL);
  12343. if (ret != BAD_FUNC_ARG) {
  12344. ret = 7;
  12345. goto exit;
  12346. }
  12347. ret = 0;
  12348. exit:
  12349. if (ret != 0)
  12350. ret = errorOffset - (ret * 1000000);
  12351. return ret;
  12352. }
  12353. static wc_test_ret_t random_rng_test(void)
  12354. {
  12355. WC_RNG localRng;
  12356. WC_RNG* rng;
  12357. wc_test_ret_t ret;
  12358. rng = &localRng;
  12359. /* Test stack based RNG. */
  12360. #ifndef HAVE_FIPS
  12361. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  12362. #else
  12363. ret = wc_InitRng(rng);
  12364. #endif
  12365. if (ret != 0)
  12366. return WC_TEST_RET_ENC_EC(ret);
  12367. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  12368. /* Make sure and free RNG */
  12369. wc_FreeRng(rng);
  12370. if (ret != 0)
  12371. return ret;
  12372. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  12373. {
  12374. byte nonce[8] = { 0 };
  12375. /* Test dynamic RNG. */
  12376. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  12377. if (rng == NULL)
  12378. return WC_TEST_RET_ENC_ERRNO;
  12379. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  12380. wc_rng_free(rng);
  12381. }
  12382. #endif
  12383. return ret;
  12384. }
  12385. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  12386. #ifdef WC_RNG_SEED_CB
  12387. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  12388. {
  12389. word32 i;
  12390. (void)os;
  12391. /* Known answer test. Set the seed to the same value every time. */
  12392. for (i = 0; i < sz; i++)
  12393. output[i] = (byte)i;
  12394. return 0;
  12395. }
  12396. static wc_test_ret_t rng_seed_test(void)
  12397. {
  12398. #ifndef HAVE_FIPS
  12399. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  12400. {
  12401. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  12402. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  12403. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  12404. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  12405. };
  12406. #else
  12407. /* FIPS uses a longer seed, so different check value. */
  12408. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  12409. {
  12410. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  12411. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  12412. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  12413. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  12414. };
  12415. #endif
  12416. byte output[WC_SHA256_DIGEST_SIZE];
  12417. WC_RNG rng;
  12418. wc_test_ret_t ret;
  12419. ret = wc_SetSeed_Cb(seed_cb);
  12420. if (ret != 0) {
  12421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12422. }
  12423. ret = wc_InitRng(&rng);
  12424. if (ret != 0) {
  12425. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12426. }
  12427. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  12428. if (ret != 0) {
  12429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12430. }
  12431. ret = XMEMCMP(output, check, sizeof(output));
  12432. if (ret != 0) {
  12433. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12434. }
  12435. ret = wc_FreeRng(&rng);
  12436. if (ret != 0) {
  12437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12438. }
  12439. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  12440. if (ret != 0) {
  12441. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12442. }
  12443. out:
  12444. return ret;
  12445. }
  12446. #endif
  12447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  12448. {
  12449. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  12450. {
  12451. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  12452. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  12453. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  12454. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  12455. };
  12456. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  12457. {
  12458. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  12459. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  12460. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  12461. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  12462. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  12463. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  12464. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  12465. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  12466. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  12467. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  12468. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  12469. };
  12470. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  12471. {
  12472. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  12473. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  12474. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  12475. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  12476. };
  12477. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  12478. {
  12479. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  12480. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  12481. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  12482. };
  12483. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  12484. {
  12485. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  12486. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  12487. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  12488. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  12489. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  12490. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  12491. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  12492. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  12493. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  12494. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  12495. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  12496. };
  12497. byte output[WC_SHA256_DIGEST_SIZE * 4];
  12498. wc_test_ret_t ret;
  12499. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  12500. output, sizeof(output));
  12501. if (ret != 0)
  12502. return WC_TEST_RET_ENC_EC(ret);
  12503. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  12504. return WC_TEST_RET_ENC_NC;
  12505. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  12506. test2EntropyB, sizeof(test2EntropyB),
  12507. output, sizeof(output));
  12508. if (ret != 0)
  12509. return WC_TEST_RET_ENC_EC(ret);
  12510. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  12511. return WC_TEST_RET_ENC_NC;
  12512. /* Basic RNG generate block test */
  12513. if ((ret = random_rng_test()) != 0)
  12514. return ret;
  12515. /* Test the seed check function. */
  12516. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  12517. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  12518. {
  12519. word32 i, outputSz;
  12520. /* Repeat the same byte over and over. Should fail. */
  12521. outputSz = sizeof(output);
  12522. XMEMSET(output, 1, outputSz);
  12523. ret = wc_RNG_TestSeed(output, outputSz);
  12524. if (ret == 0)
  12525. return WC_TEST_RET_ENC_NC;
  12526. /* Every byte of the entropy scratch is different,
  12527. * entropy is a single byte that shouldn't match. */
  12528. outputSz = (sizeof(output) / 2) + 1;
  12529. for (i = 0; i < outputSz; i++)
  12530. output[i] = (byte)i;
  12531. ret = wc_RNG_TestSeed(output, outputSz);
  12532. if (ret != 0)
  12533. return WC_TEST_RET_ENC_EC(ret);
  12534. outputSz = sizeof(output);
  12535. for (i = 0; i < outputSz; i++)
  12536. output[i] = (byte)i;
  12537. ret = wc_RNG_TestSeed(output, outputSz);
  12538. if (ret != 0)
  12539. return WC_TEST_RET_ENC_EC(ret);
  12540. }
  12541. #endif
  12542. /* Test the seed callback. */
  12543. #ifdef WC_RNG_SEED_CB
  12544. if ((ret = rng_seed_test()) != 0)
  12545. return ret;
  12546. #endif
  12547. return 0;
  12548. }
  12549. #else
  12550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  12551. {
  12552. /* Basic RNG generate block test */
  12553. return random_rng_test();
  12554. }
  12555. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  12556. #endif /* WC_NO_RNG */
  12557. #ifndef MEM_TEST_SZ
  12558. #define MEM_TEST_SZ 1024
  12559. #endif
  12560. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  12561. static int simple_mem_test(int sz)
  12562. {
  12563. int ret = 0;
  12564. byte* b;
  12565. int i;
  12566. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12567. if (b == NULL) {
  12568. return WC_TEST_RET_ENC_NC;
  12569. }
  12570. /* utilize memory */
  12571. for (i = 0; i < sz; i++) {
  12572. b[i] = (byte)i;
  12573. }
  12574. /* read back and verify */
  12575. for (i = 0; i < sz; i++) {
  12576. if (b[i] != (byte)i) {
  12577. ret = WC_TEST_RET_ENC_NC;
  12578. break;
  12579. }
  12580. }
  12581. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12582. return ret;
  12583. }
  12584. #endif
  12585. /* If successful, returns the first letter of the byte array `in`.
  12586. **
  12587. ** This is a deceptively simple test of a read-only embedded Linux file system.
  12588. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  12589. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  12590. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  12591. **
  12592. ** Edit with caution. See PR #6523. */
  12593. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  12594. {
  12595. wc_test_ret_t ret = 0;
  12596. volatile word32 j = -1; /* must be volatile to properly detect error */
  12597. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  12598. (void)ret;
  12599. j = *outJ; /* Found index to use in const array. */
  12600. if (j == 0) {
  12601. #ifdef WOLFSSL_DEBUG
  12602. printf("Testing const byte ptr reference...\n");
  12603. #endif
  12604. /* although j is zero, in[0] does not detect the Illegal instruction */
  12605. ret = in[j]; /* The big test: can we actually access the `in` data? */
  12606. }
  12607. else {
  12608. ret = -1;
  12609. }
  12610. return ret;
  12611. }
  12612. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  12613. {
  12614. wc_test_ret_t ret = 0;
  12615. word32 j = 0; /* used in embedded const pointer test */
  12616. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  12617. int i;
  12618. #endif
  12619. #ifdef WOLFSSL_STATIC_MEMORY
  12620. word32 size[] = { WOLFMEM_BUCKETS };
  12621. word32 dist[] = { WOLFMEM_DIST };
  12622. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  12623. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  12624. /* pad to account for if head of buffer is not at set memory
  12625. * alignment when tests are ran */
  12626. #endif
  12627. #ifdef WOLFSSL_STATIC_MEMORY
  12628. /* check macro settings */
  12629. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  12630. return WC_TEST_RET_ENC_NC;
  12631. }
  12632. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  12633. return WC_TEST_RET_ENC_NC;
  12634. }
  12635. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  12636. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  12637. /* each element in array should be divisible by alignment size */
  12638. return WC_TEST_RET_ENC_NC;
  12639. }
  12640. }
  12641. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  12642. if (size[i - 1] >= size[i]) {
  12643. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  12644. }
  12645. }
  12646. /* check that padding size returned is possible */
  12647. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  12648. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  12649. }
  12650. ret = wolfSSL_MemoryPaddingSz();
  12651. if (ret < 0) {
  12652. return WC_TEST_RET_ENC_EC(ret);
  12653. }
  12654. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  12655. return WC_TEST_RET_ENC_NC; /* not aligned! */
  12656. }
  12657. /* check function to return optimum buffer size (rounded down) */
  12658. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  12659. if (ret < 0) {
  12660. return WC_TEST_RET_ENC_EC(ret);
  12661. }
  12662. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  12663. return WC_TEST_RET_ENC_NC; /* not aligned! */
  12664. }
  12665. if ((unsigned int)ret > sizeof(buffer)) {
  12666. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  12667. }
  12668. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  12669. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  12670. * value
  12671. */
  12672. }
  12673. ret = wolfSSL_MemoryPaddingSz();
  12674. ret += pad; /* add space that is going to be needed if buffer not aligned */
  12675. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  12676. (ret + (int)size[0])) {
  12677. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  12678. * value
  12679. */
  12680. }
  12681. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  12682. if ((ret - pad) < 0) {
  12683. return WC_TEST_RET_ENC_NC;
  12684. }
  12685. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  12686. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  12687. }
  12688. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  12689. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  12690. }
  12691. /* check for passing bad or unknown arguments to functions */
  12692. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  12693. return WC_TEST_RET_ENC_NC;
  12694. }
  12695. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  12696. return WC_TEST_RET_ENC_NC; /* should round to 0
  12697. since struct + bucket will not fit */
  12698. }
  12699. (void)dist; /* avoid static analysis warning of variable not used */
  12700. #endif
  12701. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  12702. /* simple test */
  12703. ret = simple_mem_test(MEM_TEST_SZ);
  12704. if (ret != 0)
  12705. return ret;
  12706. #endif
  12707. #ifdef COMPLEX_MEM_TEST
  12708. /* test various size blocks */
  12709. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  12710. ret = simple_mem_test(i);
  12711. if (ret != 0)
  12712. return ret;
  12713. }
  12714. #endif
  12715. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  12716. /* realloc test */
  12717. {
  12718. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  12719. DYNAMIC_TYPE_TMP_BUFFER);
  12720. #ifndef WOLFSSL_NO_REALLOC
  12721. byte *c = NULL;
  12722. if (b) {
  12723. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  12724. DYNAMIC_TYPE_TMP_BUFFER);
  12725. if (c)
  12726. b = c;
  12727. }
  12728. #endif
  12729. if (b)
  12730. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12731. if ((b == NULL)
  12732. #ifndef WOLFSSL_NO_REALLOC
  12733. || (c == NULL)
  12734. #endif
  12735. ) {
  12736. return WC_TEST_RET_ENC_ERRNO;
  12737. }
  12738. }
  12739. #endif
  12740. if (ret == 0) {
  12741. /* This test is only interesting on embedded R/O Flash systems */
  12742. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  12743. ret = 1;
  12744. }
  12745. }
  12746. return ret;
  12747. }
  12748. #ifndef NO_FILESYSTEM
  12749. /* Cert Paths */
  12750. #ifdef FREESCALE_MQX
  12751. #define CERT_PREFIX "a:\\"
  12752. #define CERT_PATH_SEP "\\"
  12753. #elif defined(WOLFSSL_uTKERNEL2)
  12754. #define CERT_PREFIX "/uda/"
  12755. #define CERT_PATH_SEP "/"
  12756. #elif defined(_WIN32_WCE)
  12757. #define CERT_PREFIX "\\windows\\"
  12758. #define CERT_PATH_SEP "\\"
  12759. #endif
  12760. #ifndef CERT_PREFIX
  12761. #define CERT_PREFIX "./"
  12762. #endif
  12763. #ifndef CERT_PATH_SEP
  12764. #define CERT_PATH_SEP "/"
  12765. #endif
  12766. #ifndef CERT_WRITE_TEMP_DIR
  12767. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  12768. #endif
  12769. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  12770. /* Generated Test Certs */
  12771. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12772. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  12773. #if !defined(NO_RSA) && !defined(NO_ASN)
  12774. static const char* clientKey = CERT_ROOT "client-key.der";
  12775. static const char* clientCert = CERT_ROOT "client-cert.der";
  12776. #ifdef WOLFSSL_CERT_EXT
  12777. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  12778. #endif
  12779. #endif /* !NO_RSA && !NO_ASN */
  12780. #endif
  12781. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12782. #if !defined(NO_RSA) && !defined(NO_ASN)
  12783. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  12784. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  12785. #ifdef WOLFSSL_CERT_GEN
  12786. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  12787. #endif
  12788. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  12789. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  12790. #endif
  12791. #ifdef HAVE_PKCS7
  12792. static const char* rsaServerCertDerFile =
  12793. CERT_ROOT "server-cert.der";
  12794. static const char* rsaServerKeyDerFile =
  12795. CERT_ROOT "server-key.der";
  12796. #endif
  12797. #endif
  12798. #endif /* !NO_RSA && !NO_ASN */
  12799. #endif /* !USE_CERT_BUFFER_* */
  12800. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12801. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  12802. #if !defined(NO_ASN) && !defined(NO_DH)
  12803. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  12804. #endif
  12805. #endif
  12806. #if !defined(NO_ASN) && !defined(NO_DH)
  12807. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  12808. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  12809. #if !defined(USE_CERT_BUFFERS_2048)
  12810. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  12811. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  12812. #endif
  12813. #endif
  12814. #endif
  12815. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12816. #ifndef NO_DSA
  12817. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  12818. #endif
  12819. #endif /* !USE_CERT_BUFFER_* */
  12820. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  12821. #ifdef HAVE_ECC
  12822. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  12823. #ifdef HAVE_ECC_KEY_IMPORT
  12824. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  12825. #endif
  12826. #endif
  12827. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  12828. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  12829. !defined(NO_ECC_SECP)
  12830. #ifndef NO_RSA
  12831. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  12832. #endif
  12833. #ifndef NO_ASN_TIME
  12834. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  12835. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  12836. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  12837. static const char* eccCaKey384File =
  12838. CERT_ROOT "ca-ecc384-key.der";
  12839. static const char* eccCaCert384File =
  12840. CERT_ROOT "ca-ecc384-cert.pem";
  12841. #endif
  12842. #endif
  12843. #endif
  12844. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  12845. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  12846. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  12847. #endif
  12848. #endif /* HAVE_ECC */
  12849. #ifdef HAVE_ED25519
  12850. #ifdef WOLFSSL_TEST_CERT
  12851. static const char* serverEd25519Cert =
  12852. CERT_ROOT "ed25519/server-ed25519.der";
  12853. static const char* caEd25519Cert =
  12854. CERT_ROOT "ed25519/ca-ed25519.der";
  12855. #endif
  12856. #endif
  12857. #ifdef HAVE_ED448
  12858. #ifdef WOLFSSL_TEST_CERT
  12859. static const char* serverEd448Cert =
  12860. CERT_ROOT "ed448/server-ed448.der";
  12861. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  12862. #endif
  12863. #endif
  12864. #endif /* !USE_CERT_BUFFER_* */
  12865. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  12866. !defined(NO_FILESYSTEM)
  12867. static const char* certExtNc =
  12868. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  12869. static const char* certExtIa =
  12870. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  12871. static const char* certExtNct =
  12872. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  12873. #endif
  12874. #ifndef NO_WRITE_TEMP_FILES
  12875. #ifdef HAVE_ECC
  12876. #ifndef NO_ECC_SECP
  12877. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  12878. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  12879. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  12880. #endif
  12881. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  12882. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  12883. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  12884. #endif
  12885. #endif
  12886. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  12887. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  12888. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  12889. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  12890. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  12891. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  12892. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  12893. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  12894. #endif
  12895. #endif /* HAVE_ECC_KEY_EXPORT */
  12896. #endif /* HAVE_ECC */
  12897. #ifndef NO_RSA
  12898. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  12899. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  12900. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  12901. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  12902. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  12903. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  12904. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  12905. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  12906. #endif
  12907. #endif
  12908. #endif /* !NO_RSA */
  12909. #if !defined(NO_RSA) || !defined(NO_DSA)
  12910. #ifdef WOLFSSL_KEY_GEN
  12911. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  12912. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  12913. #endif
  12914. #endif
  12915. #endif /* !NO_WRITE_TEMP_FILES */
  12916. #endif /* !NO_FILESYSTEM */
  12917. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  12918. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  12919. static CertName certDefaultName;
  12920. static void initDefaultName(void)
  12921. {
  12922. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  12923. NameAttrib* n;
  12924. #endif
  12925. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  12926. certDefaultName.countryEnc = CTC_PRINTABLE;
  12927. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  12928. certDefaultName.stateEnc = CTC_UTF8;
  12929. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  12930. certDefaultName.streetEnc = CTC_UTF8;
  12931. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  12932. certDefaultName.localityEnc = CTC_UTF8;
  12933. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  12934. certDefaultName.surEnc = CTC_UTF8;
  12935. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  12936. certDefaultName.orgEnc = CTC_UTF8;
  12937. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  12938. certDefaultName.unitEnc = CTC_UTF8;
  12939. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  12940. certDefaultName.commonNameEnc = CTC_UTF8;
  12941. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  12942. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  12943. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  12944. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  12945. #ifdef WOLFSSL_CERT_EXT
  12946. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  12947. certDefaultName.busCatEnc = CTC_UTF8;
  12948. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  12949. certDefaultName.joiStEnc = CTC_PRINTABLE;
  12950. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  12951. certDefaultName.joiCEnc = CTC_PRINTABLE;
  12952. #endif
  12953. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  12954. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  12955. certDefaultName.userIdEnc = CTC_PRINTABLE;
  12956. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  12957. /* test having additional OUs and setting DC */
  12958. n = &certDefaultName.name[0];
  12959. n->id = ASN_ORGUNIT_NAME;
  12960. n->type = CTC_UTF8;
  12961. n->sz = XSTRLEN("Development-2");
  12962. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  12963. #if CTC_MAX_ATTRIB > 3
  12964. n = &certDefaultName.name[1];
  12965. n->id = ASN_DOMAIN_COMPONENT;
  12966. n->type = CTC_UTF8;
  12967. n->sz = XSTRLEN("com");
  12968. XMEMCPY(n->value, "com", sizeof("com"));
  12969. n = &certDefaultName.name[2];
  12970. n->id = ASN_DOMAIN_COMPONENT;
  12971. n->type = CTC_UTF8;
  12972. n->sz = XSTRLEN("wolfssl");
  12973. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  12974. #endif
  12975. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  12976. #ifdef WOLFSSL_CUSTOM_OID
  12977. /* TODO: Add test case for custom OID's */
  12978. #endif
  12979. }
  12980. #ifdef WOLFSSL_CERT_EXT
  12981. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  12982. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  12983. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  12984. "digitalSignature,nonRepudiation";
  12985. #endif
  12986. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  12987. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  12988. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  12989. #endif
  12990. #endif /* WOLFSSL_CERT_EXT */
  12991. #endif /* WOLFSSL_CERT_GEN */
  12992. #ifndef NO_RSA
  12993. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  12994. !defined(NO_FILESYSTEM)
  12995. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  12996. static byte minName[] = { 0x30, 0x00 };
  12997. static byte nameBad[] = {
  12998. 0x30, 0x08,
  12999. 0x31, 0x06,
  13000. 0x30, 0x04,
  13001. 0x06, 0x02,
  13002. 0x55, 0x04,
  13003. };
  13004. static byte minDates[] = {
  13005. 0x30, 0x1e,
  13006. 0x17, 0x0d,
  13007. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  13008. 0x32, 0x33, 0x31, 0x30, 0x5a,
  13009. 0x17, 0x0d,
  13010. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  13011. 0x32, 0x33, 0x31, 0x30, 0x5a
  13012. };
  13013. static byte minPubKey[] = {
  13014. 0x30, 0x1c,
  13015. 0x30, 0x0d,
  13016. 0x06, 0x09,
  13017. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  13018. 0x01,
  13019. 0x05, 0x00,
  13020. 0x03, 0x0b,
  13021. 0x00, 0x30, 0x08,
  13022. 0x02, 0x01,
  13023. 0x03,
  13024. 0x02, 0x03,
  13025. 0x01, 0x00, 0x01
  13026. };
  13027. static byte minSigAlg[] = {
  13028. 0x30, 0x0d,
  13029. 0x06, 0x09,
  13030. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  13031. 0x0b,
  13032. 0x05, 0x00
  13033. };
  13034. static byte minSig[] = {
  13035. 0x03, 0x01,
  13036. 0x00
  13037. };
  13038. static int add_seq(byte* certData, int offset, byte* data, byte length)
  13039. {
  13040. XMEMMOVE(certData + offset + 2, data, length);
  13041. certData[offset++] = 0x30;
  13042. certData[offset++] = length;
  13043. return offset + length;
  13044. }
  13045. static int add_data(byte* certData, int offset, byte* data, byte length)
  13046. {
  13047. XMEMCPY(certData + offset, data, length);
  13048. return offset + length;
  13049. }
  13050. static wc_test_ret_t cert_asn1_test(void)
  13051. {
  13052. wc_test_ret_t ret;
  13053. int len[3];
  13054. DecodedCert cert;
  13055. byte certData[114];
  13056. byte* badCert = NULL;
  13057. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  13058. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  13059. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13060. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  13061. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13062. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  13063. len[1] = add_seq(certData, 0, certData, len[2]);
  13064. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  13065. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  13066. len[0] = add_seq(certData, 0, certData, len[1]);
  13067. /* Minimal good certificate */
  13068. InitDecodedCert(&cert, certData, len[0], 0);
  13069. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13070. FreeDecodedCert(&cert);
  13071. if (ret != 0)
  13072. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13073. /* Bad issuer name */
  13074. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  13075. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  13076. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  13077. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  13078. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13079. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  13080. len[1] = add_seq(certData, 0, certData, len[2]);
  13081. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  13082. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  13083. len[0] = add_seq(certData, 0, certData, len[1]);
  13084. /* Put data into allocated buffer to allow access error checking. */
  13085. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13086. XMEMCPY(badCert, certData, len[0]);
  13087. InitDecodedCert(&cert, badCert, len[0], 0);
  13088. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13089. FreeDecodedCert(&cert);
  13090. if (ret != ASN_PARSE_E) {
  13091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13092. }
  13093. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13094. badCert = NULL;
  13095. ret = 0;
  13096. done:
  13097. if (badCert != NULL)
  13098. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13099. return ret;
  13100. }
  13101. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  13102. {
  13103. #if !defined(NO_FILESYSTEM)
  13104. DecodedCert cert;
  13105. byte* tmp;
  13106. size_t bytes;
  13107. XFILE file;
  13108. wc_test_ret_t ret;
  13109. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13110. if (tmp == NULL)
  13111. return WC_TEST_RET_ENC_ERRNO;
  13112. /* Certificate with Name Constraints extension. */
  13113. file = XFOPEN(certExtNc, "rb");
  13114. if (!file) {
  13115. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13116. }
  13117. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13118. XFCLOSE(file);
  13119. if (bytes == 0)
  13120. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13121. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13122. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13123. if (ret != 0)
  13124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13125. FreeDecodedCert(&cert);
  13126. /* Certificate with Inhibit Any Policy extension. */
  13127. file = XFOPEN(certExtIa, "rb");
  13128. if (!file) {
  13129. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13130. }
  13131. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13132. XFCLOSE(file);
  13133. if (bytes == 0)
  13134. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13135. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13136. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13137. if (ret != 0)
  13138. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13139. FreeDecodedCert(&cert);
  13140. /* Certificate with Netscape Certificate Type extension. */
  13141. file = XFOPEN(certExtNct, "rb");
  13142. if (!file) {
  13143. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13144. }
  13145. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13146. XFCLOSE(file);
  13147. if (bytes == 0)
  13148. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13149. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13150. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13151. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  13152. if (ret != 0)
  13153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13154. #else
  13155. if (ret != ASN_CRIT_EXT_E) {
  13156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13157. }
  13158. ret = 0;
  13159. #endif
  13160. done:
  13161. FreeDecodedCert(&cert);
  13162. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13163. #endif /* !NO_FILESYSTEM */
  13164. if (ret == 0)
  13165. ret = cert_asn1_test();
  13166. return ret;
  13167. }
  13168. #endif /* WOLFSSL_TEST_CERT */
  13169. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  13170. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  13171. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  13172. {
  13173. DecodedCert cert;
  13174. byte* tmp;
  13175. size_t bytes;
  13176. XFILE file;
  13177. wc_test_ret_t ret;
  13178. /* created from rsa_test : othercert.der */
  13179. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  13180. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  13181. /* created from rsa_test : othercert.der */
  13182. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  13183. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  13184. #ifdef HAVE_ECC
  13185. /* created from ecc_test_cert_gen : certecc.der */
  13186. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  13187. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  13188. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  13189. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  13190. #else
  13191. /* Authority key id from ./certs/ca-ecc-cert.pem */
  13192. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  13193. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  13194. #endif
  13195. #endif /* HAVE_ECC */
  13196. /* created from rsa_test : cert.der */
  13197. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  13198. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  13199. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13200. if (tmp == NULL)
  13201. return WC_TEST_RET_ENC_ERRNO;
  13202. /* load othercert.der (Cert signed by an authority) */
  13203. file = XFOPEN(otherCertDerFile, "rb");
  13204. if (!file) {
  13205. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13206. return WC_TEST_RET_ENC_ERRNO;
  13207. }
  13208. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13209. XFCLOSE(file);
  13210. if (bytes == 0)
  13211. return WC_TEST_RET_ENC_ERRNO;
  13212. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13213. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13214. if (ret != 0)
  13215. return WC_TEST_RET_ENC_EC(ret);
  13216. /* check the SKID from a RSA certificate */
  13217. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  13218. return WC_TEST_RET_ENC_NC;
  13219. /* check the AKID from an RSA certificate */
  13220. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13221. return WC_TEST_RET_ENC_NC;
  13222. /* check the Key Usage from an RSA certificate */
  13223. if (!cert.extKeyUsageSet)
  13224. return WC_TEST_RET_ENC_NC;
  13225. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  13226. return WC_TEST_RET_ENC_NC;
  13227. /* check the CA Basic Constraints from an RSA certificate */
  13228. if (cert.isCA)
  13229. return WC_TEST_RET_ENC_NC;
  13230. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13231. /* check the Certificate Policies Id */
  13232. if (cert.extCertPoliciesNb != 1)
  13233. return WC_TEST_RET_ENC_NC;
  13234. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  13235. return WC_TEST_RET_ENC_NC;
  13236. #endif
  13237. FreeDecodedCert(&cert);
  13238. #ifdef HAVE_ECC
  13239. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  13240. file = XFOPEN(certEccDerFile, "rb");
  13241. if (!file) {
  13242. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13243. return WC_TEST_RET_ENC_ERRNO;
  13244. }
  13245. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13246. XFCLOSE(file);
  13247. if (bytes == 0)
  13248. return WC_TEST_RET_ENC_ERRNO;
  13249. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13250. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13251. if (ret != 0)
  13252. return WC_TEST_RET_ENC_EC(ret);
  13253. /* check the SKID from a ECC certificate - generated dynamically */
  13254. /* check the AKID from an ECC certificate */
  13255. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13256. return WC_TEST_RET_ENC_NC;
  13257. /* check the Key Usage from an ECC certificate */
  13258. if (!cert.extKeyUsageSet)
  13259. return WC_TEST_RET_ENC_NC;
  13260. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  13261. return WC_TEST_RET_ENC_NC;
  13262. /* check the CA Basic Constraints from an ECC certificate */
  13263. if (cert.isCA)
  13264. return WC_TEST_RET_ENC_NC;
  13265. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13266. /* check the Certificate Policies Id */
  13267. if (cert.extCertPoliciesNb != 2)
  13268. return WC_TEST_RET_ENC_NC;
  13269. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  13270. return WC_TEST_RET_ENC_NC;
  13271. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  13272. return WC_TEST_RET_ENC_NC;
  13273. #endif
  13274. FreeDecodedCert(&cert);
  13275. #endif /* HAVE_ECC */
  13276. /* load cert.der (self signed certificate) */
  13277. file = XFOPEN(certDerFile, "rb");
  13278. if (!file) {
  13279. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13280. return WC_TEST_RET_ENC_ERRNO;
  13281. }
  13282. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13283. XFCLOSE(file);
  13284. if (bytes == 0)
  13285. return WC_TEST_RET_ENC_ERRNO;
  13286. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13287. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13288. if (ret != 0)
  13289. return WC_TEST_RET_ENC_EC(ret);
  13290. /* check the SKID from a CA certificate */
  13291. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  13292. return WC_TEST_RET_ENC_NC;
  13293. /* check the AKID from an CA certificate */
  13294. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13295. return WC_TEST_RET_ENC_NC;
  13296. /* check the Key Usage from CA certificate */
  13297. if (!cert.extKeyUsageSet)
  13298. return WC_TEST_RET_ENC_NC;
  13299. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  13300. return WC_TEST_RET_ENC_NC;
  13301. /* check the CA Basic Constraints CA certificate */
  13302. if (!cert.isCA)
  13303. return WC_TEST_RET_ENC_NC;
  13304. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13305. /* check the Certificate Policies Id */
  13306. if (cert.extCertPoliciesNb != 2)
  13307. return WC_TEST_RET_ENC_NC;
  13308. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  13309. return WC_TEST_RET_ENC_NC;
  13310. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  13311. return WC_TEST_RET_ENC_NC;
  13312. #endif
  13313. FreeDecodedCert(&cert);
  13314. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13315. return 0;
  13316. }
  13317. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  13318. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  13319. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  13320. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  13321. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  13322. {
  13323. wc_test_ret_t ret = 0;
  13324. Cert cert;
  13325. FILE* file;
  13326. byte* der;
  13327. word32 derSz;
  13328. derSz = FOURK_BUF;
  13329. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13330. if (der == NULL)
  13331. ret = WC_TEST_RET_ENC_NC;
  13332. if (ret == 0) {
  13333. /* load cert.der */
  13334. file = XFOPEN(certDerFile, "rb");
  13335. if (file != NULL) {
  13336. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  13337. XFCLOSE(file);
  13338. if (derSz == 0)
  13339. ret = WC_TEST_RET_ENC_ERRNO;
  13340. }
  13341. else
  13342. ret = WC_TEST_RET_ENC_ERRNO;
  13343. }
  13344. if (ret == 0) {
  13345. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  13346. if (ret != 0)
  13347. ret = WC_TEST_RET_ENC_EC(ret);
  13348. }
  13349. if (ret == 0) {
  13350. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  13351. if (ret != 0)
  13352. ret = WC_TEST_RET_ENC_EC(ret);
  13353. }
  13354. if (ret == 0) {
  13355. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  13356. if (ret == BAD_FUNC_ARG)
  13357. ret = 0;
  13358. else
  13359. ret = WC_TEST_RET_ENC_EC(ret);
  13360. }
  13361. if (ret == 0) {
  13362. ret = wc_SetSubjectRaw(&cert, der, derSz);
  13363. if (ret != 0)
  13364. ret = WC_TEST_RET_ENC_EC(ret);
  13365. }
  13366. if (ret == 0) {
  13367. ret = wc_SetSubjectRaw(NULL, der, derSz);
  13368. if (ret == BAD_FUNC_ARG)
  13369. ret = 0;
  13370. else
  13371. ret = WC_TEST_RET_ENC_EC(ret);
  13372. }
  13373. if (ret == 0) {
  13374. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  13375. if (ret != 0)
  13376. ret = WC_TEST_RET_ENC_EC(ret);
  13377. }
  13378. if (ret == 0) {
  13379. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  13380. if (ret == BAD_FUNC_ARG)
  13381. ret = 0;
  13382. else
  13383. ret = WC_TEST_RET_ENC_EC(ret);
  13384. }
  13385. if (ret == 0) {
  13386. ret = wc_SetIssuerRaw(&cert, der, derSz);
  13387. if (ret != 0)
  13388. ret = WC_TEST_RET_ENC_EC(ret);
  13389. }
  13390. if (ret == 0) {
  13391. ret = wc_SetIssuerRaw(NULL, der, derSz);
  13392. if (ret == BAD_FUNC_ARG)
  13393. ret = 0;
  13394. else
  13395. ret = WC_TEST_RET_ENC_EC(ret);
  13396. }
  13397. #ifdef WOLFSSL_ALT_NAMES
  13398. if (ret == 0) {
  13399. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  13400. if (ret != 0)
  13401. ret = WC_TEST_RET_ENC_EC(ret);
  13402. }
  13403. if (ret == 0) {
  13404. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  13405. if (ret == BAD_FUNC_ARG)
  13406. ret = 0;
  13407. else
  13408. ret = WC_TEST_RET_ENC_EC(ret);
  13409. }
  13410. if (ret == 0) {
  13411. ret = wc_SetDatesBuffer(&cert, der, derSz);
  13412. if (ret != 0)
  13413. ret = WC_TEST_RET_ENC_EC(ret);
  13414. }
  13415. if (ret == 0) {
  13416. ret = wc_SetDatesBuffer(NULL, der, derSz);
  13417. if (ret == BAD_FUNC_ARG)
  13418. ret = 0;
  13419. else
  13420. ret = WC_TEST_RET_ENC_EC(ret);
  13421. }
  13422. #endif
  13423. if (ret == 0) {
  13424. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  13425. if (ret != 0)
  13426. ret = WC_TEST_RET_ENC_EC(ret);
  13427. }
  13428. if (ret == 0) {
  13429. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  13430. if (ret == BAD_FUNC_ARG)
  13431. ret = 0;
  13432. else
  13433. ret = WC_TEST_RET_ENC_NC;
  13434. }
  13435. wc_SetCert_Free(&cert);
  13436. if (ret == 0) {
  13437. if(cert.decodedCert != NULL)
  13438. ret = WC_TEST_RET_ENC_NC;
  13439. }
  13440. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13441. return ret;
  13442. }
  13443. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  13444. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  13445. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  13446. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13447. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13448. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  13449. {
  13450. wc_test_ret_t ret;
  13451. byte e[RSA_TEST_BYTES];
  13452. byte n[RSA_TEST_BYTES];
  13453. word32 eSz = sizeof(e);
  13454. word32 nSz = sizeof(n);
  13455. /* Parameter Validation testing. */
  13456. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  13457. #ifdef HAVE_USER_RSA
  13458. /* Implementation using IPP Libraries returns:
  13459. * -101 = USER_CRYPTO_ERROR
  13460. */
  13461. if (ret == 0)
  13462. #else
  13463. if (ret != BAD_FUNC_ARG)
  13464. #endif
  13465. return WC_TEST_RET_ENC_EC(ret);
  13466. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  13467. #ifdef HAVE_USER_RSA
  13468. /* Implementation using IPP Libraries returns:
  13469. * -101 = USER_CRYPTO_ERROR
  13470. */
  13471. if (ret == 0)
  13472. #else
  13473. if (ret != BAD_FUNC_ARG)
  13474. #endif
  13475. return WC_TEST_RET_ENC_EC(ret);
  13476. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  13477. #ifdef HAVE_USER_RSA
  13478. /* Implementation using IPP Libraries returns:
  13479. * -101 = USER_CRYPTO_ERROR
  13480. */
  13481. if (ret == 0)
  13482. #else
  13483. if (ret != BAD_FUNC_ARG)
  13484. #endif
  13485. return WC_TEST_RET_ENC_EC(ret);
  13486. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  13487. #ifdef HAVE_USER_RSA
  13488. /* Implementation using IPP Libraries returns:
  13489. * -101 = USER_CRYPTO_ERROR
  13490. */
  13491. if (ret == 0)
  13492. #else
  13493. if (ret != BAD_FUNC_ARG)
  13494. #endif
  13495. return WC_TEST_RET_ENC_EC(ret);
  13496. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  13497. #ifdef HAVE_USER_RSA
  13498. /* Implementation using IPP Libraries returns:
  13499. * -101 = USER_CRYPTO_ERROR
  13500. */
  13501. if (ret == 0)
  13502. #else
  13503. if (ret != BAD_FUNC_ARG)
  13504. #endif
  13505. return WC_TEST_RET_ENC_EC(ret);
  13506. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13507. if (ret != 0)
  13508. return WC_TEST_RET_ENC_EC(ret);
  13509. eSz = 0;
  13510. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13511. #ifdef HAVE_USER_RSA
  13512. /* Implementation using IPP Libraries returns:
  13513. * -101 = USER_CRYPTO_ERROR
  13514. */
  13515. if (ret == 0)
  13516. #elif defined(HAVE_FIPS) && \
  13517. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  13518. if (ret != 0)
  13519. #else
  13520. if (ret != RSA_BUFFER_E)
  13521. #endif
  13522. return WC_TEST_RET_ENC_EC(ret);
  13523. eSz = sizeof(e);
  13524. nSz = 0;
  13525. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13526. #ifdef HAVE_USER_RSA
  13527. /* Implementation using IPP Libraries returns:
  13528. * -101 = USER_CRYPTO_ERROR
  13529. */
  13530. if (ret == 0)
  13531. #else
  13532. if (ret != RSA_BUFFER_E)
  13533. #endif
  13534. return WC_TEST_RET_ENC_EC(ret);
  13535. return 0;
  13536. }
  13537. #endif /* NO_ASN */
  13538. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13539. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13540. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  13541. {
  13542. wc_test_ret_t ret;
  13543. byte e[3];
  13544. word32 eSz = sizeof(e);
  13545. byte n[RSA_TEST_BYTES];
  13546. word32 nSz = sizeof(n);
  13547. byte d[RSA_TEST_BYTES];
  13548. word32 dSz = sizeof(d);
  13549. byte p[RSA_TEST_BYTES/2];
  13550. word32 pSz = sizeof(p);
  13551. byte q[RSA_TEST_BYTES/2];
  13552. word32 qSz = sizeof(q);
  13553. word32 zero = 0;
  13554. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13555. if (ret != BAD_FUNC_ARG)
  13556. return WC_TEST_RET_ENC_EC(ret);
  13557. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13558. if (ret != BAD_FUNC_ARG)
  13559. return WC_TEST_RET_ENC_EC(ret);
  13560. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13561. if (ret != BAD_FUNC_ARG)
  13562. return WC_TEST_RET_ENC_EC(ret);
  13563. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13564. if (ret != BAD_FUNC_ARG)
  13565. return WC_TEST_RET_ENC_EC(ret);
  13566. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  13567. if (ret != BAD_FUNC_ARG)
  13568. return WC_TEST_RET_ENC_EC(ret);
  13569. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  13570. if (ret != BAD_FUNC_ARG)
  13571. return WC_TEST_RET_ENC_EC(ret);
  13572. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  13573. if (ret != BAD_FUNC_ARG)
  13574. return WC_TEST_RET_ENC_EC(ret);
  13575. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  13576. if (ret != BAD_FUNC_ARG)
  13577. return WC_TEST_RET_ENC_EC(ret);
  13578. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  13579. if (ret != BAD_FUNC_ARG)
  13580. return WC_TEST_RET_ENC_EC(ret);
  13581. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  13582. if (ret != BAD_FUNC_ARG)
  13583. return WC_TEST_RET_ENC_EC(ret);
  13584. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  13585. if (ret != BAD_FUNC_ARG)
  13586. return WC_TEST_RET_ENC_EC(ret);
  13587. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13588. if (ret != RSA_BUFFER_E)
  13589. return WC_TEST_RET_ENC_EC(ret);
  13590. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  13591. if (ret != RSA_BUFFER_E)
  13592. return WC_TEST_RET_ENC_EC(ret);
  13593. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13594. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  13595. if (ret != RSA_BUFFER_E)
  13596. return WC_TEST_RET_ENC_EC(ret);
  13597. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  13598. if (ret != RSA_BUFFER_E)
  13599. return WC_TEST_RET_ENC_EC(ret);
  13600. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  13601. if (ret != RSA_BUFFER_E)
  13602. return WC_TEST_RET_ENC_EC(ret);
  13603. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13604. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13605. if (ret != 0)
  13606. return WC_TEST_RET_ENC_EC(ret);
  13607. return 0;
  13608. }
  13609. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  13610. #ifndef NO_SIG_WRAPPER
  13611. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  13612. {
  13613. wc_test_ret_t ret;
  13614. word32 sigSz;
  13615. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  13616. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  13617. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  13618. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  13619. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  13620. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  13621. };
  13622. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  13623. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  13624. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  13625. 0x00, 0x04, 0x20,
  13626. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  13627. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  13628. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  13629. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  13630. };
  13631. word32 inLen = (word32)XSTRLEN((char*)in);
  13632. byte out[RSA_TEST_BYTES];
  13633. /* Parameter Validation testing. */
  13634. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  13635. if (ret != BAD_FUNC_ARG)
  13636. return WC_TEST_RET_ENC_EC(ret);
  13637. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  13638. if (ret != BAD_FUNC_ARG)
  13639. return WC_TEST_RET_ENC_EC(ret);
  13640. sigSz = (word32)modLen;
  13641. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  13642. inLen, out, &sigSz, key, keyLen, rng);
  13643. if (ret != BAD_FUNC_ARG)
  13644. return WC_TEST_RET_ENC_EC(ret);
  13645. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13646. 0, out, &sigSz, key, keyLen, rng);
  13647. if (ret != BAD_FUNC_ARG)
  13648. return WC_TEST_RET_ENC_EC(ret);
  13649. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13650. inLen, NULL, &sigSz, key, keyLen, rng);
  13651. if (ret != BAD_FUNC_ARG)
  13652. return WC_TEST_RET_ENC_EC(ret);
  13653. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13654. inLen, out, NULL, key, keyLen, rng);
  13655. if (ret != BAD_FUNC_ARG)
  13656. return WC_TEST_RET_ENC_EC(ret);
  13657. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13658. inLen, out, &sigSz, NULL, keyLen, rng);
  13659. if (ret != BAD_FUNC_ARG)
  13660. return WC_TEST_RET_ENC_EC(ret);
  13661. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13662. inLen, out, &sigSz, key, 0, rng);
  13663. if (ret != BAD_FUNC_ARG)
  13664. return WC_TEST_RET_ENC_EC(ret);
  13665. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13666. inLen, out, &sigSz, key, keyLen, NULL);
  13667. #ifdef HAVE_USER_RSA
  13668. /* Implementation using IPP Libraries returns:
  13669. * -101 = USER_CRYPTO_ERROR
  13670. */
  13671. if (ret == 0)
  13672. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  13673. /* blinding / rng handled with hardware acceleration */
  13674. if (ret != 0)
  13675. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  13676. /* async may not require RNG */
  13677. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13678. if (ret != NO_VALID_DEVID)
  13679. #else
  13680. if (ret != 0 && ret != MISSING_RNG_E)
  13681. #endif
  13682. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  13683. /* FIPS140 implementation does not do blinding */
  13684. if (ret != 0)
  13685. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  13686. if (ret != SIG_TYPE_E)
  13687. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  13688. /* RNG is handled by hardware */
  13689. if (ret != 0)
  13690. #else
  13691. if (ret != MISSING_RNG_E)
  13692. #endif
  13693. return WC_TEST_RET_ENC_EC(ret);
  13694. sigSz = 0;
  13695. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13696. inLen, out, &sigSz, key, keyLen, rng);
  13697. if (ret != BAD_FUNC_ARG)
  13698. return WC_TEST_RET_ENC_EC(ret);
  13699. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  13700. inLen, out, (word32)modLen, key, keyLen);
  13701. if (ret != BAD_FUNC_ARG)
  13702. return WC_TEST_RET_ENC_EC(ret);
  13703. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13704. 0, out, (word32)modLen, key, keyLen);
  13705. if (ret != BAD_FUNC_ARG)
  13706. return WC_TEST_RET_ENC_EC(ret);
  13707. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13708. inLen, NULL, (word32)modLen, key, keyLen);
  13709. if (ret != BAD_FUNC_ARG)
  13710. return WC_TEST_RET_ENC_EC(ret);
  13711. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13712. inLen, out, 0, key, keyLen);
  13713. if (ret != BAD_FUNC_ARG)
  13714. return WC_TEST_RET_ENC_EC(ret);
  13715. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13716. inLen, out, (word32)modLen, NULL, keyLen);
  13717. if (ret != BAD_FUNC_ARG)
  13718. return WC_TEST_RET_ENC_EC(ret);
  13719. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13720. inLen, out, (word32)modLen, key, 0);
  13721. if (ret != BAD_FUNC_ARG)
  13722. return WC_TEST_RET_ENC_EC(ret);
  13723. #ifndef HAVE_ECC
  13724. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  13725. if (ret != SIG_TYPE_E)
  13726. return WC_TEST_RET_ENC_EC(ret);
  13727. #endif
  13728. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13729. return 0;
  13730. #endif
  13731. /* Use APIs. */
  13732. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  13733. if (ret != modLen)
  13734. return WC_TEST_RET_ENC_EC(ret);
  13735. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  13736. if (ret != modLen)
  13737. return WC_TEST_RET_ENC_EC(ret);
  13738. sigSz = (word32)ret;
  13739. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13740. XMEMSET(out, 0, sizeof(out));
  13741. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13742. inLen, out, &sigSz, key, keyLen, rng);
  13743. if (ret != 0)
  13744. return WC_TEST_RET_ENC_EC(ret);
  13745. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13746. inLen, out, (word32)modLen, key, keyLen);
  13747. if (ret != 0)
  13748. return WC_TEST_RET_ENC_EC(ret);
  13749. sigSz = (word32)sizeof(out);
  13750. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13751. in, inLen, out, &sigSz, key, keyLen, rng);
  13752. if (ret != 0)
  13753. return WC_TEST_RET_ENC_EC(ret);
  13754. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13755. in, inLen, out, (word32)modLen, key, keyLen);
  13756. if (ret != 0)
  13757. return WC_TEST_RET_ENC_EC(ret);
  13758. /* Wrong signature type. */
  13759. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13760. inLen, out, (word32)modLen, key, keyLen);
  13761. if (ret == 0)
  13762. return WC_TEST_RET_ENC_EC(ret);
  13763. /* check hash functions */
  13764. sigSz = (word32)sizeof(out);
  13765. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  13766. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  13767. if (ret != 0)
  13768. return WC_TEST_RET_ENC_EC(ret);
  13769. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  13770. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  13771. if (ret != 0)
  13772. return WC_TEST_RET_ENC_EC(ret);
  13773. sigSz = (word32)sizeof(out);
  13774. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13775. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  13776. if (ret != 0)
  13777. return WC_TEST_RET_ENC_EC(ret);
  13778. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13779. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  13780. if (ret != 0)
  13781. return WC_TEST_RET_ENC_EC(ret);
  13782. #else
  13783. (void)hash;
  13784. (void)hashEnc;
  13785. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  13786. return 0;
  13787. }
  13788. #endif /* !NO_SIG_WRAPPER */
  13789. #ifdef WC_RSA_NONBLOCK
  13790. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  13791. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  13792. {
  13793. wc_test_ret_t ret = 0;
  13794. int count;
  13795. int signSz = 0;
  13796. RsaNb nb;
  13797. byte* inlinePlain = NULL;
  13798. /* Enable non-blocking RSA mode - provide context */
  13799. ret = wc_RsaSetNonBlock(key, &nb);
  13800. if (ret != 0)
  13801. return ret;
  13802. #ifdef WC_RSA_NONBLOCK_TIME
  13803. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  13804. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  13805. if (ret != 0)
  13806. return ret;
  13807. #endif
  13808. count = 0;
  13809. do {
  13810. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  13811. count++; /* track number of would blocks */
  13812. if (ret == FP_WOULDBLOCK) {
  13813. /* do "other" work here */
  13814. }
  13815. } while (ret == FP_WOULDBLOCK);
  13816. if (ret < 0) {
  13817. return ret;
  13818. }
  13819. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13820. printf("RSA non-block sign: %d times\n", count);
  13821. #endif
  13822. signSz = ret;
  13823. /* Test non-blocking verify */
  13824. XMEMSET(plain, 0, plainSz);
  13825. count = 0;
  13826. do {
  13827. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  13828. count++; /* track number of would blocks */
  13829. if (ret == FP_WOULDBLOCK) {
  13830. /* do "other" work here */
  13831. }
  13832. } while (ret == FP_WOULDBLOCK);
  13833. if (ret < 0) {
  13834. return ret;
  13835. }
  13836. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13837. printf("RSA non-block verify: %d times\n", count);
  13838. #endif
  13839. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  13840. return SIG_VERIFY_E;
  13841. }
  13842. /* Test inline non-blocking verify */
  13843. count = 0;
  13844. do {
  13845. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  13846. count++; /* track number of would blocks */
  13847. if (ret == FP_WOULDBLOCK) {
  13848. /* do "other" work here */
  13849. }
  13850. } while (ret == FP_WOULDBLOCK);
  13851. if (ret < 0) {
  13852. return ret;
  13853. }
  13854. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13855. printf("RSA non-block inline verify: %d times\n", count);
  13856. #endif
  13857. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  13858. return SIG_VERIFY_E;
  13859. }
  13860. /* Disabling non-block RSA mode */
  13861. ret = wc_RsaSetNonBlock(key, NULL);
  13862. (void)count;
  13863. return 0;
  13864. }
  13865. #endif
  13866. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  13867. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  13868. {
  13869. wc_test_ret_t ret;
  13870. word32 inSz;
  13871. word32 inOutIdx;
  13872. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  13873. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  13874. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  13875. 0x03 };
  13876. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  13877. 0x30, 0x18, 0x30, 0x16,
  13878. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13879. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13880. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  13881. 0x30, 0x1a, 0x30, 0x18,
  13882. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13883. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  13884. 0x02, 0x1, 0x03 };
  13885. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  13886. 0x30, 0x1b, 0x30, 0x19,
  13887. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13888. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  13889. 0x02, 0x1, 0x03 };
  13890. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  13891. 0x30, 0x18, 0x30, 0x16,
  13892. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13893. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13894. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  13895. 0x30, 0x18, 0x30, 0x16,
  13896. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13897. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13898. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  13899. 0x30, 0x16, 0x30, 0x14,
  13900. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13901. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13902. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  13903. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  13904. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13905. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  13906. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  13907. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  13908. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  13909. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  13910. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13911. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  13912. 0x30, 0x17, 0x30, 0x15,
  13913. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13914. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13915. ret = wc_InitRsaKey(keyPub, NULL);
  13916. if (ret != 0)
  13917. return WC_TEST_RET_ENC_EC(ret);
  13918. /* Parameter Validation testing. */
  13919. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  13920. if (ret != BAD_FUNC_ARG) {
  13921. ret = WC_TEST_RET_ENC_EC(ret);
  13922. goto done;
  13923. }
  13924. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  13925. if (ret != BAD_FUNC_ARG) {
  13926. ret = WC_TEST_RET_ENC_EC(ret);
  13927. goto done;
  13928. }
  13929. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  13930. if (ret != BAD_FUNC_ARG) {
  13931. ret = WC_TEST_RET_ENC_EC(ret);
  13932. goto done;
  13933. }
  13934. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  13935. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  13936. if (ret != 0)
  13937. #else
  13938. if (ret != ASN_GETINT_E)
  13939. #endif
  13940. {
  13941. ret = WC_TEST_RET_ENC_EC(ret);
  13942. goto done;
  13943. }
  13944. wc_FreeRsaKey(keyPub);
  13945. ret = wc_InitRsaKey(keyPub, NULL);
  13946. if (ret != 0)
  13947. return WC_TEST_RET_ENC_EC(ret);
  13948. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  13949. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  13950. if (ret != 0) {
  13951. #else
  13952. if (ret != ASN_GETINT_E) {
  13953. #endif
  13954. ret = WC_TEST_RET_ENC_EC(ret);
  13955. goto done;
  13956. }
  13957. wc_FreeRsaKey(keyPub);
  13958. ret = wc_InitRsaKey(keyPub, NULL);
  13959. if (ret != 0)
  13960. return WC_TEST_RET_ENC_EC(ret);
  13961. /* Use API. */
  13962. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  13963. if (ret != 0) {
  13964. ret = WC_TEST_RET_ENC_EC(ret);
  13965. goto done;
  13966. }
  13967. wc_FreeRsaKey(keyPub);
  13968. ret = wc_InitRsaKey(keyPub, NULL);
  13969. if (ret != 0)
  13970. return WC_TEST_RET_ENC_EC(ret);
  13971. /* Parameter Validation testing. */
  13972. inSz = sizeof(good);
  13973. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  13974. if (ret != BAD_FUNC_ARG) {
  13975. ret = WC_TEST_RET_ENC_EC(ret);
  13976. goto done;
  13977. }
  13978. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  13979. if (ret != BAD_FUNC_ARG) {
  13980. ret = WC_TEST_RET_ENC_EC(ret);
  13981. goto done;
  13982. }
  13983. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  13984. if (ret != BAD_FUNC_ARG) {
  13985. ret = WC_TEST_RET_ENC_EC(ret);
  13986. goto done;
  13987. }
  13988. /* Use good data and offset to bad data. */
  13989. inOutIdx = 2;
  13990. inSz = sizeof(good) - inOutIdx;
  13991. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  13992. if (ret != ASN_PARSE_E) {
  13993. ret = WC_TEST_RET_ENC_EC(ret);
  13994. goto done;
  13995. }
  13996. inOutIdx = 2;
  13997. inSz = sizeof(goodAlgId) - inOutIdx;
  13998. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  13999. if (ret != ASN_PARSE_E) {
  14000. ret = WC_TEST_RET_ENC_EC(ret);
  14001. goto done;
  14002. }
  14003. inOutIdx = 2;
  14004. inSz = sizeof(goodAlgId);
  14005. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  14006. #ifndef WOLFSSL_NO_DECODE_EXTRA
  14007. if (ret != ASN_PARSE_E)
  14008. #else
  14009. if (ret != ASN_RSA_KEY_E)
  14010. #endif
  14011. {
  14012. ret = WC_TEST_RET_ENC_EC(ret);
  14013. goto done;
  14014. }
  14015. /* Try different bad data. */
  14016. inSz = sizeof(badAlgIdNull);
  14017. inOutIdx = 0;
  14018. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  14019. if (ret != ASN_EXPECT_0_E) {
  14020. ret = WC_TEST_RET_ENC_EC(ret);
  14021. goto done;
  14022. }
  14023. inSz = sizeof(badNotBitString);
  14024. inOutIdx = 0;
  14025. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  14026. if (ret != ASN_BITSTR_E) {
  14027. ret = WC_TEST_RET_ENC_EC(ret);
  14028. goto done;
  14029. }
  14030. inSz = sizeof(badBitStringLen);
  14031. inOutIdx = 0;
  14032. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  14033. if (ret != ASN_PARSE_E) {
  14034. ret = WC_TEST_RET_ENC_EC(ret);
  14035. goto done;
  14036. }
  14037. inSz = sizeof(badNoSeq);
  14038. inOutIdx = 0;
  14039. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  14040. if (ret != ASN_PARSE_E) {
  14041. ret = WC_TEST_RET_ENC_EC(ret);
  14042. goto done;
  14043. }
  14044. inSz = sizeof(badNoObj);
  14045. inOutIdx = 0;
  14046. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  14047. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  14048. ret = WC_TEST_RET_ENC_EC(ret);
  14049. goto done;
  14050. }
  14051. inSz = sizeof(badIntN);
  14052. inOutIdx = 0;
  14053. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  14054. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  14055. ret = WC_TEST_RET_ENC_EC(ret);
  14056. goto done;
  14057. }
  14058. inSz = sizeof(badNotIntE);
  14059. inOutIdx = 0;
  14060. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  14061. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  14062. ret = WC_TEST_RET_ENC_EC(ret);
  14063. goto done;
  14064. }
  14065. /* TODO: Shouldn't pass as the sequence length is too small. */
  14066. inSz = sizeof(badLength);
  14067. inOutIdx = 0;
  14068. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  14069. #ifndef WOLFSSL_ASN_TEMPLATE
  14070. if (ret != 0)
  14071. #else
  14072. if (ret != ASN_PARSE_E)
  14073. #endif
  14074. {
  14075. ret = WC_TEST_RET_ENC_EC(ret);
  14076. goto done;
  14077. }
  14078. /* TODO: Shouldn't ignore object id's data. */
  14079. wc_FreeRsaKey(keyPub);
  14080. ret = wc_InitRsaKey(keyPub, NULL);
  14081. if (ret != 0)
  14082. return WC_TEST_RET_ENC_EC(ret);
  14083. inSz = sizeof(badBitStrNoZero);
  14084. inOutIdx = 0;
  14085. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  14086. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  14087. ret = WC_TEST_RET_ENC_EC(ret);
  14088. goto done;
  14089. }
  14090. wc_FreeRsaKey(keyPub);
  14091. ret = wc_InitRsaKey(keyPub, NULL);
  14092. if (ret != 0)
  14093. return WC_TEST_RET_ENC_EC(ret);
  14094. /* Valid data cases. */
  14095. inSz = sizeof(good);
  14096. inOutIdx = 0;
  14097. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  14098. if (ret != 0) {
  14099. ret = WC_TEST_RET_ENC_EC(ret);
  14100. goto done;
  14101. }
  14102. if (inOutIdx != inSz) {
  14103. ret = WC_TEST_RET_ENC_NC;
  14104. goto done;
  14105. }
  14106. wc_FreeRsaKey(keyPub);
  14107. ret = wc_InitRsaKey(keyPub, NULL);
  14108. if (ret != 0)
  14109. return WC_TEST_RET_ENC_EC(ret);
  14110. inSz = sizeof(goodAlgId);
  14111. inOutIdx = 0;
  14112. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  14113. if (ret != 0) {
  14114. ret = WC_TEST_RET_ENC_EC(ret);
  14115. goto done;
  14116. }
  14117. if (inOutIdx != inSz) {
  14118. ret = WC_TEST_RET_ENC_NC;
  14119. goto done;
  14120. }
  14121. wc_FreeRsaKey(keyPub);
  14122. ret = wc_InitRsaKey(keyPub, NULL);
  14123. if (ret != 0)
  14124. return WC_TEST_RET_ENC_EC(ret);
  14125. inSz = sizeof(goodAlgIdNull);
  14126. inOutIdx = 0;
  14127. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  14128. if (ret != 0) {
  14129. ret = WC_TEST_RET_ENC_EC(ret);
  14130. goto done;
  14131. }
  14132. if (inOutIdx != inSz) {
  14133. ret = WC_TEST_RET_ENC_NC;
  14134. goto done;
  14135. }
  14136. done:
  14137. wc_FreeRsaKey(keyPub);
  14138. return ret;
  14139. }
  14140. #endif
  14141. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14142. /* Need to create known good signatures to test with this. */
  14143. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14144. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14145. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  14146. {
  14147. byte digest[WC_MAX_DIGEST_SIZE];
  14148. wc_test_ret_t ret = 0;
  14149. const char inStr[] = TEST_STRING;
  14150. word32 inLen = (word32)TEST_STRING_SZ;
  14151. word32 outSz;
  14152. word32 plainSz;
  14153. word32 digestSz;
  14154. int i, j;
  14155. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  14156. int k, l;
  14157. #endif
  14158. #ifndef WOLFSSL_SE050
  14159. int len;
  14160. #endif
  14161. byte* plain;
  14162. int mgf[] = {
  14163. #ifndef NO_SHA
  14164. WC_MGF1SHA1,
  14165. #endif
  14166. #ifdef WOLFSSL_SHA224
  14167. WC_MGF1SHA224,
  14168. #endif
  14169. WC_MGF1SHA256,
  14170. #ifdef WOLFSSL_SHA384
  14171. WC_MGF1SHA384,
  14172. #endif
  14173. #ifdef WOLFSSL_SHA512
  14174. WC_MGF1SHA512
  14175. #endif
  14176. };
  14177. enum wc_HashType hash[] = {
  14178. #ifndef NO_SHA
  14179. WC_HASH_TYPE_SHA,
  14180. #endif
  14181. #ifdef WOLFSSL_SHA224
  14182. WC_HASH_TYPE_SHA224,
  14183. #endif
  14184. WC_HASH_TYPE_SHA256,
  14185. #ifdef WOLFSSL_SHA384
  14186. WC_HASH_TYPE_SHA384,
  14187. #endif
  14188. #ifdef WOLFSSL_SHA512
  14189. WC_HASH_TYPE_SHA512,
  14190. #endif
  14191. };
  14192. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  14193. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14194. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  14195. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14196. if (in == NULL || out == NULL || sig == NULL)
  14197. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  14198. #endif
  14199. XMEMCPY(in, inStr, inLen);
  14200. /* Test all combinations of hash and MGF. */
  14201. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  14202. /* Calculate hash of message. */
  14203. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  14204. if (ret != 0)
  14205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14206. digestSz = wc_HashGetDigestSize(hash[j]);
  14207. #ifdef WOLFSSL_SE050
  14208. /* SE050 only supports MGF matched to same hash type */
  14209. i = j;
  14210. #else
  14211. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  14212. #endif
  14213. outSz = RSA_TEST_BYTES;
  14214. do {
  14215. #if defined(WOLFSSL_ASYNC_CRYPT)
  14216. ret = wc_AsyncWait(ret, &key->asyncDev,
  14217. WC_ASYNC_FLAG_CALL_AGAIN);
  14218. #endif
  14219. if (ret >= 0) {
  14220. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  14221. hash[j], mgf[i], -1, key, rng);
  14222. }
  14223. } while (ret == WC_PENDING_E);
  14224. if (ret <= 0)
  14225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14226. outSz = (word32)ret;
  14227. XMEMCPY(sig, out, outSz);
  14228. plain = NULL;
  14229. TEST_SLEEP();
  14230. do {
  14231. #if defined(WOLFSSL_ASYNC_CRYPT)
  14232. ret = wc_AsyncWait(ret, &key->asyncDev,
  14233. WC_ASYNC_FLAG_CALL_AGAIN);
  14234. #endif
  14235. if (ret >= 0) {
  14236. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  14237. mgf[i], -1, key);
  14238. }
  14239. } while (ret == WC_PENDING_E);
  14240. if (ret <= 0)
  14241. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14242. plainSz = (word32)ret;
  14243. TEST_SLEEP();
  14244. #if defined(HAVE_SELFTEST) && \
  14245. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14246. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14247. hash[j], -1);
  14248. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14249. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14250. hash[j], -1, 0);
  14251. #else
  14252. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  14253. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  14254. #endif
  14255. if (ret != 0)
  14256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14257. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  14258. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  14259. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  14260. if (i == k && j == l)
  14261. continue;
  14262. XMEMCPY(sig, out, outSz);
  14263. do {
  14264. #if defined(WOLFSSL_ASYNC_CRYPT)
  14265. ret = wc_AsyncWait(ret, &key->asyncDev,
  14266. WC_ASYNC_FLAG_CALL_AGAIN);
  14267. #endif
  14268. if (ret >= 0) {
  14269. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  14270. (byte**)&plain, hash[l], mgf[k], -1, key);
  14271. }
  14272. } while (ret == WC_PENDING_E);
  14273. if (ret >= 0)
  14274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14275. }
  14276. }
  14277. #endif
  14278. #ifndef WOLFSSL_SE050
  14279. } /* end mgf for loop */
  14280. #endif
  14281. }
  14282. /* SE050 generates salts internally only of hash length */
  14283. #ifndef WOLFSSL_SE050
  14284. /* Test that a salt length of zero works. */
  14285. digestSz = wc_HashGetDigestSize(hash[0]);
  14286. outSz = RSA_TEST_BYTES;
  14287. do {
  14288. #if defined(WOLFSSL_ASYNC_CRYPT)
  14289. ret = wc_AsyncWait(ret, &key->asyncDev,
  14290. WC_ASYNC_FLAG_CALL_AGAIN);
  14291. #endif
  14292. if (ret >= 0) {
  14293. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14294. mgf[0], 0, key, rng);
  14295. }
  14296. } while (ret == WC_PENDING_E);
  14297. if (ret <= 0)
  14298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14299. outSz = (word32)ret;
  14300. TEST_SLEEP();
  14301. do {
  14302. #if defined(WOLFSSL_ASYNC_CRYPT)
  14303. ret = wc_AsyncWait(ret, &key->asyncDev,
  14304. WC_ASYNC_FLAG_CALL_AGAIN);
  14305. #endif
  14306. if (ret >= 0) {
  14307. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  14308. 0, key);
  14309. }
  14310. } while (ret == WC_PENDING_E);
  14311. if (ret <= 0)
  14312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14313. plainSz = (word32)ret;
  14314. TEST_SLEEP();
  14315. do {
  14316. #if defined(WOLFSSL_ASYNC_CRYPT)
  14317. ret = wc_AsyncWait(ret, &key->asyncDev,
  14318. WC_ASYNC_FLAG_CALL_AGAIN);
  14319. #endif
  14320. if (ret >= 0) {
  14321. #if defined(HAVE_SELFTEST) && \
  14322. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14323. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  14324. hash[0], 0);
  14325. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14326. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  14327. hash[0], 0, 0);
  14328. #else
  14329. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  14330. hash[0], 0, 0, HEAP_HINT);
  14331. #endif
  14332. }
  14333. } while (ret == WC_PENDING_E);
  14334. if (ret != 0)
  14335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14336. XMEMCPY(sig, out, outSz);
  14337. plain = NULL;
  14338. do {
  14339. #if defined(WOLFSSL_ASYNC_CRYPT)
  14340. ret = wc_AsyncWait(ret, &key->asyncDev,
  14341. WC_ASYNC_FLAG_CALL_AGAIN);
  14342. #endif
  14343. if (ret >= 0) {
  14344. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  14345. 0, key);
  14346. }
  14347. } while (ret == WC_PENDING_E);
  14348. if (ret <= 0)
  14349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14350. plainSz = (word32)ret;
  14351. TEST_SLEEP();
  14352. #if defined(HAVE_SELFTEST) && \
  14353. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14354. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14355. hash[0], 0);
  14356. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14357. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14358. hash[0], 0, 0);
  14359. #else
  14360. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14361. 0, 0, HEAP_HINT);
  14362. #endif
  14363. if (ret != 0)
  14364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14365. /* Test bad salt lengths in various APIs. */
  14366. digestSz = wc_HashGetDigestSize(hash[0]);
  14367. outSz = RSA_TEST_BYTES;
  14368. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  14369. len = -2;
  14370. #else
  14371. len = -3;
  14372. #endif
  14373. do {
  14374. #if defined(WOLFSSL_ASYNC_CRYPT)
  14375. ret = wc_AsyncWait(ret, &key->asyncDev,
  14376. WC_ASYNC_FLAG_CALL_AGAIN);
  14377. #endif
  14378. if (ret >= 0) {
  14379. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14380. mgf[0], len, key, rng);
  14381. }
  14382. } while (ret == WC_PENDING_E);
  14383. if (ret != PSS_SALTLEN_E)
  14384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14385. do {
  14386. #if defined(WOLFSSL_ASYNC_CRYPT)
  14387. ret = wc_AsyncWait(ret, &key->asyncDev,
  14388. WC_ASYNC_FLAG_CALL_AGAIN);
  14389. #endif
  14390. if (ret >= 0) {
  14391. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14392. mgf[0], digestSz + 1, key, rng);
  14393. }
  14394. } while (ret == WC_PENDING_E);
  14395. if (ret != PSS_SALTLEN_E)
  14396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14397. TEST_SLEEP();
  14398. do {
  14399. #if defined(WOLFSSL_ASYNC_CRYPT)
  14400. ret = wc_AsyncWait(ret, &key->asyncDev,
  14401. WC_ASYNC_FLAG_CALL_AGAIN);
  14402. #endif
  14403. if (ret >= 0) {
  14404. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  14405. mgf[0], -2, key);
  14406. }
  14407. } while (ret == WC_PENDING_E);
  14408. if (ret != PSS_SALTLEN_E)
  14409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14410. TEST_SLEEP();
  14411. do {
  14412. #if defined(WOLFSSL_ASYNC_CRYPT)
  14413. ret = wc_AsyncWait(ret, &key->asyncDev,
  14414. WC_ASYNC_FLAG_CALL_AGAIN);
  14415. #endif
  14416. if (ret >= 0) {
  14417. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  14418. digestSz + 1, key);
  14419. }
  14420. } while (ret == WC_PENDING_E);
  14421. if (ret != PSS_SALTLEN_E)
  14422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14423. TEST_SLEEP();
  14424. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  14425. len = -2;
  14426. #else
  14427. len = -3;
  14428. #endif
  14429. #if defined(HAVE_SELFTEST) && \
  14430. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14431. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14432. hash[0], len);
  14433. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14434. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14435. hash[0], len, 0);
  14436. #else
  14437. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14438. len, 0, HEAP_HINT);
  14439. #endif
  14440. if (ret != PSS_SALTLEN_E)
  14441. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14442. #ifndef WOLFSSL_PSS_LONG_SALT
  14443. len = digestSz + 1;
  14444. #else
  14445. len = plainSz - digestSz - 1;
  14446. #endif
  14447. #if defined(HAVE_SELFTEST) && \
  14448. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14449. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14450. hash[0], len);
  14451. if (ret != PSS_SALTLEN_E)
  14452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14453. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14454. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14455. hash[0], len, 0);
  14456. if (ret != BAD_PADDING_E)
  14457. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14458. #else
  14459. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14460. len, 0, HEAP_HINT);
  14461. if (ret != PSS_SALTLEN_E)
  14462. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14463. #endif
  14464. ret = 0;
  14465. #endif /* WOLFSSL_SE050 */
  14466. exit_rsa_pss:
  14467. WC_FREE_VAR(sig, HEAP_HINT);
  14468. WC_FREE_VAR(in, HEAP_HINT);
  14469. WC_FREE_VAR(out, HEAP_HINT);
  14470. return ret;
  14471. }
  14472. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  14473. #endif
  14474. #ifdef WC_RSA_NO_PADDING
  14475. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  14476. {
  14477. WC_RNG rng;
  14478. byte* tmp = NULL;
  14479. size_t bytes;
  14480. wc_test_ret_t ret;
  14481. word32 inLen = 0;
  14482. word32 idx = 0;
  14483. word32 outSz = RSA_TEST_BYTES;
  14484. word32 plainSz = RSA_TEST_BYTES;
  14485. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14486. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14487. !defined(NO_FILESYSTEM)
  14488. XFILE file;
  14489. #endif
  14490. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  14491. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14492. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14493. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14494. if (key == NULL || out == NULL || plain == NULL)
  14495. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  14496. #endif
  14497. /* initialize stack structures */
  14498. XMEMSET(&rng, 0, sizeof(rng));
  14499. XMEMSET(key, 0, sizeof(RsaKey));
  14500. #ifdef USE_CERT_BUFFERS_1024
  14501. bytes = (size_t)sizeof_client_key_der_1024;
  14502. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14503. bytes = (size_t)sizeof_client_cert_der_1024;
  14504. #elif defined(USE_CERT_BUFFERS_2048)
  14505. bytes = (size_t)sizeof_client_key_der_2048;
  14506. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14507. bytes = (size_t)sizeof_client_cert_der_2048;
  14508. #else
  14509. bytes = FOURK_BUF;
  14510. #endif
  14511. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14512. if (tmp == NULL
  14513. #ifdef WOLFSSL_ASYNC_CRYPT
  14514. || out == NULL || plain == NULL
  14515. #endif
  14516. ) {
  14517. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14518. }
  14519. #ifdef USE_CERT_BUFFERS_1024
  14520. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14521. #elif defined(USE_CERT_BUFFERS_2048)
  14522. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14523. #elif defined(USE_CERT_BUFFERS_3072)
  14524. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14525. #elif defined(USE_CERT_BUFFERS_4096)
  14526. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14527. #elif !defined(NO_FILESYSTEM)
  14528. file = XFOPEN(clientKey, "rb");
  14529. if (!file) {
  14530. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  14531. WC_TEST_RET_ENC_ERRNO);
  14532. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  14533. }
  14534. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14535. XFCLOSE(file);
  14536. if (bytes == 0)
  14537. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  14538. #else
  14539. /* No key to use. */
  14540. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14541. #endif /* USE_CERT_BUFFERS */
  14542. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14543. if (ret != 0) {
  14544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14545. }
  14546. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14547. if (ret != 0) {
  14548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14549. }
  14550. /* after loading in key use tmp as the test buffer */
  14551. #ifndef HAVE_FIPS
  14552. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14553. #else
  14554. ret = wc_InitRng(&rng);
  14555. #endif
  14556. if (ret != 0) {
  14557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14558. }
  14559. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14560. inLen = wc_RsaEncryptSize(key);
  14561. outSz = inLen;
  14562. plainSz = inLen;
  14563. XMEMSET(tmp, 7, inLen);
  14564. do {
  14565. #if defined(WOLFSSL_ASYNC_CRYPT)
  14566. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14567. #endif
  14568. if (ret >= 0) {
  14569. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  14570. RSA_PRIVATE_ENCRYPT, &rng);
  14571. }
  14572. } while (ret == WC_PENDING_E);
  14573. if (ret <= 0) {
  14574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14575. }
  14576. /* encrypted result should not be the same as input */
  14577. if (XMEMCMP(out, tmp, inLen) == 0) {
  14578. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14579. }
  14580. TEST_SLEEP();
  14581. /* decrypt with public key and compare result */
  14582. do {
  14583. #if defined(WOLFSSL_ASYNC_CRYPT)
  14584. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14585. #endif
  14586. if (ret >= 0) {
  14587. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  14588. RSA_PUBLIC_DECRYPT, &rng);
  14589. }
  14590. } while (ret == WC_PENDING_E);
  14591. if (ret <= 0) {
  14592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14593. }
  14594. if (XMEMCMP(plain, tmp, inLen) != 0) {
  14595. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14596. }
  14597. TEST_SLEEP();
  14598. #endif
  14599. #ifdef WC_RSA_BLINDING
  14600. ret = wc_RsaSetRNG(NULL, &rng);
  14601. if (ret != BAD_FUNC_ARG) {
  14602. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14603. }
  14604. ret = wc_RsaSetRNG(key, &rng);
  14605. if (ret < 0) {
  14606. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14607. }
  14608. #endif
  14609. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  14610. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14611. do {
  14612. #if defined(WOLFSSL_ASYNC_CRYPT)
  14613. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14614. #endif
  14615. if (ret >= 0) {
  14616. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  14617. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  14618. }
  14619. } while (ret == WC_PENDING_E);
  14620. if (ret < 0) {
  14621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14622. }
  14623. TEST_SLEEP();
  14624. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14625. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14626. do {
  14627. #if defined(WOLFSSL_ASYNC_CRYPT)
  14628. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14629. #endif
  14630. if (ret >= 0) {
  14631. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  14632. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  14633. }
  14634. } while (ret == WC_PENDING_E);
  14635. if (ret < 0) {
  14636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14637. }
  14638. if (XMEMCMP(plain, tmp, inLen) != 0) {
  14639. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14640. }
  14641. TEST_SLEEP();
  14642. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14643. /* test some bad arguments */
  14644. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  14645. &rng);
  14646. if (ret != BAD_FUNC_ARG) {
  14647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14648. }
  14649. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  14650. &rng);
  14651. if (ret != BAD_FUNC_ARG) {
  14652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14653. }
  14654. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  14655. &rng);
  14656. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  14657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14658. }
  14659. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  14660. RSA_PUBLIC_DECRYPT, &rng);
  14661. if (ret != BAD_FUNC_ARG) {
  14662. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14663. }
  14664. /* if making it to this point of code without hitting an ERROR_OUT then
  14665. * all tests have passed */
  14666. ret = 0;
  14667. exit_rsa_nopadding:
  14668. wc_FreeRsaKey(key);
  14669. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14670. WC_FREE_VAR(key, HEAP_HINT);
  14671. WC_FREE_VAR(out, HEAP_HINT);
  14672. WC_FREE_VAR(plain, HEAP_HINT);
  14673. wc_FreeRng(&rng);
  14674. return ret;
  14675. }
  14676. #endif /* WC_RSA_NO_PADDING */
  14677. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14678. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  14679. {
  14680. byte* tmp = NULL;
  14681. size_t bytes;
  14682. wc_test_ret_t ret;
  14683. word32 inLen = 0;
  14684. #ifndef NO_ASN
  14685. word32 idx = 0;
  14686. #endif
  14687. word32 outSz = RSA_TEST_BYTES;
  14688. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14689. word32 plainSz = RSA_TEST_BYTES;
  14690. #endif
  14691. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  14692. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  14693. XFILE file;
  14694. #endif
  14695. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14696. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14697. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14698. #endif
  14699. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14700. if (out == NULL
  14701. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14702. || plain == NULL
  14703. #endif
  14704. ) {
  14705. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  14706. }
  14707. #endif
  14708. #if defined(USE_CERT_BUFFERS_2048)
  14709. bytes = (size_t)sizeof_client_key_der_2048;
  14710. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14711. bytes = (size_t)sizeof_client_cert_der_2048;
  14712. #else
  14713. bytes = FOURK_BUF;
  14714. #endif
  14715. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14716. if (tmp == NULL
  14717. #ifdef WOLFSSL_ASYNC_CRYPT
  14718. || out == NULL || plain == NULL
  14719. #endif
  14720. ) {
  14721. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  14722. }
  14723. #if defined(USE_CERT_BUFFERS_2048)
  14724. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14725. #elif defined(USE_CERT_BUFFERS_3072)
  14726. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14727. #elif defined(USE_CERT_BUFFERS_4096)
  14728. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14729. #elif !defined(NO_FILESYSTEM)
  14730. file = XFOPEN(clientKey, "rb");
  14731. if (!file) {
  14732. err_sys("can't open ./certs/client-key.der, "
  14733. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  14734. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  14735. }
  14736. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14737. XFCLOSE(file);
  14738. if (bytes == 0)
  14739. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  14740. #else
  14741. /* No key to use. */
  14742. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  14743. #endif /* USE_CERT_BUFFERS */
  14744. #ifndef NO_ASN
  14745. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14746. if (ret != 0) {
  14747. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14748. }
  14749. #else
  14750. #ifdef USE_CERT_BUFFERS_2048
  14751. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14752. if (ret != 0) {
  14753. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14754. }
  14755. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14756. if (ret != 0) {
  14757. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14758. }
  14759. #ifndef NO_SIG_WRAPPER
  14760. modLen = 2048;
  14761. #endif
  14762. #else
  14763. #error Not supported yet!
  14764. #endif
  14765. #endif
  14766. key->n.dp[0] &= (mp_digit)-2;
  14767. if (ret != 0) {
  14768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14769. }
  14770. /* after loading in key use tmp as the test buffer */
  14771. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  14772. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  14773. !defined(WOLFSSL_XILINX_CRYPT)
  14774. /* The ARM64_ASM code that was FIPS validated did not return these expected
  14775. * failure codes. These tests cases were added after the assembly was
  14776. * in-lined in the module and validated, these tests will be available in
  14777. * the 140-3 module */
  14778. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  14779. inLen = 32;
  14780. outSz = wc_RsaEncryptSize(key);
  14781. XMEMSET(tmp, 7, plainSz);
  14782. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  14783. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  14784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14785. }
  14786. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  14787. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14789. }
  14790. #endif
  14791. #ifdef WC_RSA_BLINDING
  14792. ret = wc_RsaSetRNG(key, rng);
  14793. if (ret < 0) {
  14794. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14795. }
  14796. #endif
  14797. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  14798. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  14799. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  14800. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14802. }
  14803. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14804. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14805. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  14806. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  14807. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14808. }
  14809. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14810. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  14811. /* if making it to this point of code without hitting an ERROR_OUT then
  14812. * all tests have passed */
  14813. ret = 0;
  14814. exit_rsa_even_mod:
  14815. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14816. WC_FREE_VAR(out, HEAP_HINT);
  14817. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14818. WC_FREE_VAR(plain, HEAP_HINT);
  14819. #endif
  14820. (void)out;
  14821. (void)outSz;
  14822. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14823. (void)plain;
  14824. (void)plainSz;
  14825. #endif
  14826. (void)inLen;
  14827. (void)rng;
  14828. return ret;
  14829. }
  14830. #endif /* WOLFSSL_HAVE_SP_RSA */
  14831. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  14832. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  14833. {
  14834. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14835. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14836. #ifdef WOLFSSL_TEST_CERT
  14837. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14838. #endif
  14839. #else
  14840. RsaKey caKey[1];
  14841. #ifdef WOLFSSL_TEST_CERT
  14842. DecodedCert decode[1];
  14843. #endif
  14844. #endif
  14845. byte* der = NULL;
  14846. wc_test_ret_t ret;
  14847. Cert* myCert = NULL;
  14848. int certSz;
  14849. size_t bytes3;
  14850. word32 idx3 = 0;
  14851. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14852. XFILE file3;
  14853. #endif
  14854. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  14855. struct tm beforeTime;
  14856. struct tm afterTime;
  14857. #endif
  14858. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  14859. (void)keypub;
  14860. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14861. if (caKey == NULL)
  14862. ERROR_OUT(MEMORY_E, exit_rsa);
  14863. #ifdef WOLFSSL_TEST_CERT
  14864. if (decode == NULL)
  14865. ERROR_OUT(MEMORY_E, exit_rsa);
  14866. #endif
  14867. #endif
  14868. XMEMSET(caKey, 0, sizeof *caKey);
  14869. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14870. if (der == NULL) {
  14871. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14872. }
  14873. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14874. if (myCert == NULL) {
  14875. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14876. }
  14877. /* self signed */
  14878. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  14879. if (ret != 0) {
  14880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14881. }
  14882. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  14883. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  14884. myCert->serialSz = (int)sizeof(mySerial);
  14885. myCert->isCA = 1;
  14886. #ifndef NO_SHA256
  14887. myCert->sigType = CTC_SHA256wRSA;
  14888. #else
  14889. myCert->sigType = CTC_SHAwRSA;
  14890. #endif
  14891. #ifdef WOLFSSL_CERT_EXT
  14892. /* add Policies */
  14893. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  14894. CTC_MAX_CERTPOL_SZ);
  14895. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  14896. CTC_MAX_CERTPOL_SZ);
  14897. myCert->certPoliciesNb = 2;
  14898. /* add SKID from the Public Key */
  14899. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  14900. if (ret != 0) {
  14901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14902. }
  14903. /* add AKID from the Public Key */
  14904. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  14905. if (ret != 0) {
  14906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14907. }
  14908. /* add Key Usage */
  14909. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  14910. if (ret != 0) {
  14911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14912. }
  14913. #ifdef WOLFSSL_EKU_OID
  14914. {
  14915. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14916. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  14917. HEAP_HINT);
  14918. if (ret != 0) {
  14919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14920. }
  14921. }
  14922. #endif /* WOLFSSL_EKU_OID */
  14923. #endif /* WOLFSSL_CERT_EXT */
  14924. do {
  14925. #if defined(WOLFSSL_ASYNC_CRYPT)
  14926. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14927. #endif
  14928. if (ret >= 0) {
  14929. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  14930. }
  14931. } while (ret == WC_PENDING_E);
  14932. if (ret < 0) {
  14933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14934. }
  14935. certSz = (word32)ret;
  14936. #ifdef WOLFSSL_TEST_CERT
  14937. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  14938. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  14939. if (ret != 0) {
  14940. FreeDecodedCert(decode);
  14941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14942. }
  14943. FreeDecodedCert(decode);
  14944. #endif
  14945. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  14946. CERT_TYPE);
  14947. if (ret != 0) {
  14948. goto exit_rsa;
  14949. }
  14950. /* Setup Certificate */
  14951. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  14952. if (ret < 0) {
  14953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14954. }
  14955. #ifdef WOLFSSL_ALT_NAMES
  14956. /* Get CA Cert for testing */
  14957. #ifdef USE_CERT_BUFFERS_1024
  14958. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  14959. bytes3 = sizeof_ca_cert_der_1024;
  14960. #elif defined(USE_CERT_BUFFERS_2048)
  14961. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  14962. bytes3 = sizeof_ca_cert_der_2048;
  14963. #else
  14964. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  14965. if (!file3) {
  14966. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14967. }
  14968. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  14969. XFCLOSE(file3);
  14970. if (bytes3 == 0)
  14971. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14972. #endif /* USE_CERT_BUFFERS */
  14973. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  14974. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  14975. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  14976. if (ret != 0)
  14977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14978. #endif
  14979. /* get alt names from der */
  14980. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  14981. if (ret != 0)
  14982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14983. /* get dates from der */
  14984. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  14985. if (ret != 0)
  14986. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14987. #ifndef NO_ASN_TIME
  14988. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  14989. if (ret < 0)
  14990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14991. #endif
  14992. #endif /* WOLFSSL_ALT_NAMES */
  14993. /* Get CA Key */
  14994. #ifdef USE_CERT_BUFFERS_1024
  14995. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  14996. bytes3 = sizeof_ca_key_der_1024;
  14997. #elif defined(USE_CERT_BUFFERS_2048)
  14998. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  14999. bytes3 = sizeof_ca_key_der_2048;
  15000. #else
  15001. file3 = XFOPEN(rsaCaKeyFile, "rb");
  15002. if (!file3) {
  15003. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15004. }
  15005. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15006. XFCLOSE(file3);
  15007. if (bytes3 == 0)
  15008. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15009. #endif /* USE_CERT_BUFFERS */
  15010. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  15011. if (ret != 0)
  15012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15013. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  15014. if (ret != 0)
  15015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15016. #ifndef NO_SHA256
  15017. myCert->sigType = CTC_SHA256wRSA;
  15018. #else
  15019. myCert->sigType = CTC_SHAwRSA;
  15020. #endif
  15021. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  15022. #ifdef WOLFSSL_CERT_EXT
  15023. /* add Policies */
  15024. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  15025. CTC_MAX_CERTPOL_SZ);
  15026. myCert->certPoliciesNb =1;
  15027. /* add SKID from the Public Key */
  15028. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  15029. if (ret != 0)
  15030. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15031. /* add AKID from the CA certificate */
  15032. #if defined(USE_CERT_BUFFERS_2048)
  15033. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  15034. sizeof_ca_cert_der_2048);
  15035. #elif defined(USE_CERT_BUFFERS_1024)
  15036. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  15037. sizeof_ca_cert_der_1024);
  15038. #else
  15039. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  15040. #endif
  15041. if (ret != 0)
  15042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15043. /* add Key Usage */
  15044. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  15045. if (ret != 0)
  15046. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15047. #endif /* WOLFSSL_CERT_EXT */
  15048. #if defined(USE_CERT_BUFFERS_2048)
  15049. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  15050. sizeof_ca_cert_der_2048);
  15051. #elif defined(USE_CERT_BUFFERS_1024)
  15052. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  15053. sizeof_ca_cert_der_1024);
  15054. #else
  15055. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  15056. #endif
  15057. if (ret < 0)
  15058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15059. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  15060. if (certSz < 0) {
  15061. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  15062. }
  15063. ret = 0;
  15064. do {
  15065. #if defined(WOLFSSL_ASYNC_CRYPT)
  15066. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15067. #endif
  15068. if (ret >= 0) {
  15069. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  15070. caKey, NULL, rng);
  15071. }
  15072. } while (ret == WC_PENDING_E);
  15073. if (ret < 0)
  15074. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15075. certSz = (word32)ret;
  15076. #ifdef WOLFSSL_TEST_CERT
  15077. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  15078. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15079. if (ret != 0) {
  15080. FreeDecodedCert(decode);
  15081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15082. }
  15083. FreeDecodedCert(decode);
  15084. #endif
  15085. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  15086. CERT_TYPE);
  15087. if (ret != 0) {
  15088. goto exit_rsa;
  15089. }
  15090. exit_rsa:
  15091. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15092. if (caKey != NULL) {
  15093. wc_FreeRsaKey(caKey);
  15094. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15095. }
  15096. #ifdef WOLFSSL_TEST_CERT
  15097. if (decode != NULL)
  15098. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15099. #endif
  15100. #else
  15101. wc_FreeRsaKey(caKey);
  15102. #endif
  15103. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15104. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15105. return ret;
  15106. }
  15107. #endif
  15108. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  15109. defined(WOLFSSL_CERT_GEN)
  15110. /* Make Cert / Sign example for ECC cert and RSA CA */
  15111. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  15112. {
  15113. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15114. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15115. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15116. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15117. #ifdef WOLFSSL_TEST_CERT
  15118. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15119. #endif
  15120. #else
  15121. RsaKey caKey[1];
  15122. ecc_key caEccKey[1];
  15123. ecc_key caEccKeyPub[1];
  15124. #ifdef WOLFSSL_TEST_CERT
  15125. DecodedCert decode[1];
  15126. #endif
  15127. #endif
  15128. byte* der = NULL;
  15129. Cert* myCert = NULL;
  15130. int certSz;
  15131. size_t bytes3;
  15132. word32 idx3 = 0;
  15133. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  15134. || !defined(USE_CERT_BUFFERS_256)
  15135. XFILE file3;
  15136. #endif
  15137. wc_test_ret_t ret;
  15138. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15139. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  15140. #ifdef WOLFSSL_TEST_CERT
  15141. || (decode == NULL)
  15142. #endif
  15143. )
  15144. ERROR_OUT(MEMORY_E, exit_rsa);
  15145. #endif
  15146. XMEMSET(caKey, 0, sizeof *caKey);
  15147. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  15148. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  15149. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15150. if (der == NULL) {
  15151. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15152. }
  15153. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15154. if (myCert == NULL) {
  15155. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15156. }
  15157. /* Get CA Key */
  15158. #ifdef USE_CERT_BUFFERS_1024
  15159. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  15160. bytes3 = sizeof_ca_key_der_1024;
  15161. #elif defined(USE_CERT_BUFFERS_2048)
  15162. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  15163. bytes3 = sizeof_ca_key_der_2048;
  15164. #else
  15165. file3 = XFOPEN(rsaCaKeyFile, "rb");
  15166. if (!file3) {
  15167. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15168. }
  15169. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15170. XFCLOSE(file3);
  15171. if (bytes3 == 0)
  15172. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15173. #endif /* USE_CERT_BUFFERS */
  15174. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  15175. if (ret != 0)
  15176. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15177. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  15178. if (ret != 0)
  15179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15180. /* Get Cert Key */
  15181. #ifdef USE_CERT_BUFFERS_256
  15182. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  15183. bytes3 = sizeof_ecc_key_pub_der_256;
  15184. #else
  15185. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  15186. if (!file3) {
  15187. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15188. }
  15189. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15190. XFCLOSE(file3);
  15191. if (bytes3 == 0)
  15192. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15193. #endif
  15194. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  15195. if (ret != 0)
  15196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15197. idx3 = 0;
  15198. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  15199. if (ret != 0)
  15200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15201. /* Setup Certificate */
  15202. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  15203. if (ret != 0)
  15204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15205. #ifndef NO_SHA256
  15206. myCert->sigType = CTC_SHA256wRSA;
  15207. #else
  15208. myCert->sigType = CTC_SHAwRSA;
  15209. #endif
  15210. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  15211. #ifdef WOLFSSL_CERT_EXT
  15212. /* add Policies */
  15213. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  15214. CTC_MAX_CERTPOL_SZ);
  15215. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  15216. CTC_MAX_CERTPOL_SZ);
  15217. myCert->certPoliciesNb = 2;
  15218. /* add SKID from the Public Key */
  15219. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  15220. if (ret != 0)
  15221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15222. /* add AKID from the CA certificate */
  15223. #if defined(USE_CERT_BUFFERS_2048)
  15224. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  15225. sizeof_ca_cert_der_2048);
  15226. #elif defined(USE_CERT_BUFFERS_1024)
  15227. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  15228. sizeof_ca_cert_der_1024);
  15229. #else
  15230. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  15231. #endif
  15232. if (ret != 0)
  15233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15234. /* add Key Usage */
  15235. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  15236. if (ret != 0)
  15237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15238. #endif /* WOLFSSL_CERT_EXT */
  15239. #if defined(USE_CERT_BUFFERS_2048)
  15240. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  15241. sizeof_ca_cert_der_2048);
  15242. #elif defined(USE_CERT_BUFFERS_1024)
  15243. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  15244. sizeof_ca_cert_der_1024);
  15245. #else
  15246. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  15247. #endif
  15248. if (ret < 0)
  15249. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15250. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  15251. if (certSz < 0) {
  15252. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  15253. }
  15254. ret = 0;
  15255. do {
  15256. #if defined(WOLFSSL_ASYNC_CRYPT)
  15257. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15258. #endif
  15259. if (ret >= 0) {
  15260. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  15261. FOURK_BUF, caKey, NULL, rng);
  15262. }
  15263. } while (ret == WC_PENDING_E);
  15264. if (ret < 0)
  15265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15266. certSz = (word32)ret;
  15267. #ifdef WOLFSSL_TEST_CERT
  15268. InitDecodedCert(decode, der, certSz, 0);
  15269. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15270. if (ret != 0) {
  15271. FreeDecodedCert(decode);
  15272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15273. }
  15274. FreeDecodedCert(decode);
  15275. #endif
  15276. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  15277. CERT_TYPE);
  15278. if (ret != 0) {
  15279. goto exit_rsa;
  15280. }
  15281. exit_rsa:
  15282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15283. if (caKey != NULL) {
  15284. wc_FreeRsaKey(caKey);
  15285. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15286. }
  15287. if (caEccKey != NULL) {
  15288. wc_ecc_free(caEccKey);
  15289. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15290. }
  15291. if (caEccKeyPub != NULL) {
  15292. wc_ecc_free(caEccKeyPub);
  15293. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15294. }
  15295. #ifdef WOLFSSL_TEST_CERT
  15296. if (decode != NULL)
  15297. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15298. #endif
  15299. #else
  15300. wc_FreeRsaKey(caKey);
  15301. wc_ecc_free(caEccKey);
  15302. wc_ecc_free(caEccKeyPub);
  15303. #endif
  15304. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15305. myCert = NULL;
  15306. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15307. der = NULL;
  15308. if (ret >= 0)
  15309. ret = 0;
  15310. return ret;
  15311. }
  15312. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  15313. #ifdef WOLFSSL_KEY_GEN
  15314. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  15315. {
  15316. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15317. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15318. #else
  15319. RsaKey genKey[1];
  15320. #endif
  15321. wc_test_ret_t ret;
  15322. byte* der = NULL;
  15323. #ifndef WOLFSSL_CRYPTOCELL
  15324. word32 idx = 0;
  15325. #endif
  15326. int derSz = 0;
  15327. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  15328. int keySz = 1024;
  15329. #else
  15330. int keySz = 2048;
  15331. #endif
  15332. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15333. if (! genKey)
  15334. ERROR_OUT(MEMORY_E, exit_rsa);
  15335. #endif
  15336. XMEMSET(genKey, 0, sizeof *genKey);
  15337. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  15338. if (ret != 0)
  15339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15340. #ifdef HAVE_FIPS
  15341. for (;;) {
  15342. #endif
  15343. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  15344. #if defined(WOLFSSL_ASYNC_CRYPT)
  15345. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15346. #endif
  15347. #ifdef HAVE_FIPS
  15348. if (ret == PRIME_GEN_E)
  15349. continue;
  15350. break;
  15351. }
  15352. #endif
  15353. if (ret != 0)
  15354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15355. TEST_SLEEP();
  15356. #ifdef WOLFSSL_RSA_KEY_CHECK
  15357. ret = wc_CheckRsaKey(genKey);
  15358. if (ret != 0)
  15359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15360. #endif
  15361. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15362. if (der == NULL) {
  15363. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15364. }
  15365. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  15366. if (derSz < 0) {
  15367. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  15368. }
  15369. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  15370. PRIVATEKEY_TYPE);
  15371. if (ret != 0) {
  15372. goto exit_rsa;
  15373. }
  15374. wc_FreeRsaKey(genKey);
  15375. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  15376. if (ret != 0)
  15377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15378. #ifndef WOLFSSL_CRYPTOCELL
  15379. idx = 0;
  15380. /* The private key part of the key gen pairs from cryptocell can't be exported */
  15381. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  15382. if (ret != 0)
  15383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15384. #endif /* WOLFSSL_CRYPTOCELL */
  15385. exit_rsa:
  15386. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15387. if (genKey) {
  15388. wc_FreeRsaKey(genKey);
  15389. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15390. }
  15391. #else
  15392. wc_FreeRsaKey(genKey);
  15393. #endif
  15394. if (der != NULL) {
  15395. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15396. der = NULL;
  15397. }
  15398. return ret;
  15399. }
  15400. #endif
  15401. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  15402. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  15403. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  15404. (!defined(HAVE_FIPS) || \
  15405. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  15406. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15407. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  15408. {
  15409. wc_test_ret_t ret = 0;
  15410. word32 idx = 0;
  15411. const char inStr[] = TEST_STRING;
  15412. const word32 inLen = (word32)TEST_STRING_SZ;
  15413. const word32 outSz = RSA_TEST_BYTES;
  15414. const word32 plainSz = RSA_TEST_BYTES;
  15415. byte* res = NULL;
  15416. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  15417. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15418. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15419. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15420. if (in == NULL || out == NULL || plain == NULL)
  15421. ERROR_OUT(MEMORY_E, exit_rsa);
  15422. #endif
  15423. XMEMCPY(in, inStr, inLen);
  15424. #ifndef NO_SHA
  15425. do {
  15426. #if defined(WOLFSSL_ASYNC_CRYPT)
  15427. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15428. #endif
  15429. if (ret >= 0) {
  15430. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15431. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  15432. }
  15433. } while (ret == WC_PENDING_E);
  15434. if (ret < 0)
  15435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15436. TEST_SLEEP();
  15437. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15438. idx = (word32)ret;
  15439. do {
  15440. #if defined(WOLFSSL_ASYNC_CRYPT)
  15441. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15442. #endif
  15443. if (ret >= 0) {
  15444. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15445. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  15446. }
  15447. } while (ret == WC_PENDING_E);
  15448. if (ret < 0)
  15449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15450. if (XMEMCMP(plain, in, inLen)) {
  15451. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15452. }
  15453. TEST_SLEEP();
  15454. #endif /* NO_SHA */
  15455. #endif
  15456. #ifndef NO_SHA256
  15457. XMEMSET(plain, 0, plainSz);
  15458. do {
  15459. #if defined(WOLFSSL_ASYNC_CRYPT)
  15460. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15461. #endif
  15462. if (ret >= 0) {
  15463. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15464. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15465. }
  15466. } while (ret == WC_PENDING_E);
  15467. if (ret < 0)
  15468. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15469. TEST_SLEEP();
  15470. idx = (word32)ret;
  15471. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15472. do {
  15473. #if defined(WOLFSSL_ASYNC_CRYPT)
  15474. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15475. #endif
  15476. if (ret >= 0) {
  15477. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15478. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15479. }
  15480. } while (ret == WC_PENDING_E);
  15481. if (ret < 0)
  15482. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15483. if (XMEMCMP(plain, in, inLen)) {
  15484. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15485. }
  15486. TEST_SLEEP();
  15487. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15488. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15489. do {
  15490. #if defined(WOLFSSL_ASYNC_CRYPT)
  15491. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15492. #endif
  15493. if (ret >= 0) {
  15494. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  15495. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15496. }
  15497. } while (ret == WC_PENDING_E);
  15498. if (ret < 0)
  15499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15500. if (ret != (int)inLen) {
  15501. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15502. }
  15503. if (XMEMCMP(res, in, inLen)) {
  15504. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15505. }
  15506. TEST_SLEEP();
  15507. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15508. /* check fails if not using the same optional label */
  15509. XMEMSET(plain, 0, plainSz);
  15510. do {
  15511. #if defined(WOLFSSL_ASYNC_CRYPT)
  15512. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15513. #endif
  15514. if (ret >= 0) {
  15515. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15516. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15517. }
  15518. } while (ret == WC_PENDING_E);
  15519. if (ret < 0)
  15520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15521. TEST_SLEEP();
  15522. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  15523. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15524. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  15525. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  15526. * due to label */
  15527. idx = (word32)ret;
  15528. do {
  15529. #if defined(WOLFSSL_ASYNC_CRYPT)
  15530. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15531. #endif
  15532. if (ret >= 0) {
  15533. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15534. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15535. }
  15536. } while (ret == WC_PENDING_E);
  15537. if (ret > 0) { /* in this case decrypt should fail */
  15538. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15539. }
  15540. ret = 0;
  15541. TEST_SLEEP();
  15542. #endif /* !HAVE_CAVIUM */
  15543. /* check using optional label with encrypt/decrypt */
  15544. XMEMSET(plain, 0, plainSz);
  15545. do {
  15546. #if defined(WOLFSSL_ASYNC_CRYPT)
  15547. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15548. #endif
  15549. if (ret >= 0) {
  15550. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15551. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15552. }
  15553. } while (ret == WC_PENDING_E);
  15554. if (ret < 0)
  15555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15556. TEST_SLEEP();
  15557. idx = (word32)ret;
  15558. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15559. do {
  15560. #if defined(WOLFSSL_ASYNC_CRYPT)
  15561. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15562. #endif
  15563. if (ret >= 0) {
  15564. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15565. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15566. }
  15567. } while (ret == WC_PENDING_E);
  15568. if (ret < 0)
  15569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15570. if (XMEMCMP(plain, in, inLen)) {
  15571. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15572. }
  15573. TEST_SLEEP();
  15574. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15575. #ifndef NO_SHA
  15576. /* check fail using mismatch hash algorithms */
  15577. XMEMSET(plain, 0, plainSz);
  15578. do {
  15579. #if defined(WOLFSSL_ASYNC_CRYPT)
  15580. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15581. #endif
  15582. if (ret >= 0) {
  15583. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15584. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  15585. }
  15586. } while (ret == WC_PENDING_E);
  15587. if (ret < 0)
  15588. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15589. TEST_SLEEP();
  15590. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  15591. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15592. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  15593. idx = (word32)ret;
  15594. do {
  15595. #if defined(WOLFSSL_ASYNC_CRYPT)
  15596. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15597. #endif
  15598. if (ret >= 0) {
  15599. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15600. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  15601. in, inLen);
  15602. }
  15603. } while (ret == WC_PENDING_E);
  15604. if (ret > 0) { /* should fail */
  15605. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15606. }
  15607. ret = 0;
  15608. TEST_SLEEP();
  15609. #endif /* !HAVE_CAVIUM */
  15610. #endif /* NO_SHA */
  15611. #endif /* NO_SHA256 */
  15612. #ifdef WOLFSSL_SHA512
  15613. /* Check valid RSA key size is used while using hash length of SHA512
  15614. If key size is less than (hash length * 2) + 2 then is invalid use
  15615. and test, since OAEP padding requires this.
  15616. BAD_FUNC_ARG is returned when this case is not met */
  15617. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  15618. XMEMSET(plain, 0, plainSz);
  15619. do {
  15620. #if defined(WOLFSSL_ASYNC_CRYPT)
  15621. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15622. #endif
  15623. if (ret >= 0) {
  15624. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15625. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  15626. }
  15627. } while (ret == WC_PENDING_E);
  15628. if (ret < 0)
  15629. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15630. TEST_SLEEP();
  15631. idx = (word32)ret;
  15632. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15633. do {
  15634. #if defined(WOLFSSL_ASYNC_CRYPT)
  15635. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15636. #endif
  15637. if (ret >= 0) {
  15638. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15639. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  15640. }
  15641. } while (ret == WC_PENDING_E);
  15642. if (ret < 0)
  15643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15644. if (XMEMCMP(plain, in, inLen)) {
  15645. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15646. }
  15647. TEST_SLEEP();
  15648. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15649. }
  15650. #endif /* WOLFSSL_SHA512 */
  15651. /* check using pkcsv15 padding with _ex API */
  15652. XMEMSET(plain, 0, plainSz);
  15653. do {
  15654. #if defined(WOLFSSL_ASYNC_CRYPT)
  15655. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15656. #endif
  15657. if (ret >= 0) {
  15658. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15659. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  15660. }
  15661. } while (ret == WC_PENDING_E);
  15662. if (ret < 0)
  15663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15664. TEST_SLEEP();
  15665. idx = (word32)ret;
  15666. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15667. do {
  15668. #if defined(WOLFSSL_ASYNC_CRYPT)
  15669. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15670. #endif
  15671. if (ret >= 0) {
  15672. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15673. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  15674. }
  15675. } while (ret == WC_PENDING_E);
  15676. if (ret < 0)
  15677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15678. if (XMEMCMP(plain, in, inLen)) {
  15679. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15680. }
  15681. TEST_SLEEP();
  15682. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15683. exit_rsa:
  15684. WC_FREE_VAR(in, HEAP_HINT);
  15685. WC_FREE_VAR(out, HEAP_HINT);
  15686. WC_FREE_VAR(plain, HEAP_HINT);
  15687. (void)idx;
  15688. (void)inStr;
  15689. (void)res;
  15690. if (ret >= 0)
  15691. ret = 0;
  15692. return ret;
  15693. }
  15694. #endif
  15695. #endif
  15696. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  15697. {
  15698. wc_test_ret_t ret;
  15699. size_t bytes;
  15700. WC_RNG rng;
  15701. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15702. byte* tmp = NULL;
  15703. byte* der = NULL;
  15704. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15705. #else
  15706. RsaKey key[1];
  15707. byte tmp[FOURK_BUF];
  15708. #endif
  15709. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15710. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15711. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15712. #else
  15713. RsaKey keypub[1];
  15714. #endif
  15715. #endif
  15716. word32 idx = 0;
  15717. const char inStr[] = TEST_STRING;
  15718. const word32 inLen = (word32)TEST_STRING_SZ;
  15719. const word32 outSz = RSA_TEST_BYTES;
  15720. const word32 plainSz = RSA_TEST_BYTES;
  15721. byte* res = NULL;
  15722. #ifndef NO_SIG_WRAPPER
  15723. int modLen;
  15724. #endif
  15725. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15726. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  15727. !defined(NO_FILESYSTEM)
  15728. XFILE file;
  15729. #ifdef WOLFSSL_TEST_CERT
  15730. XFILE file2;
  15731. #endif
  15732. #endif
  15733. #ifdef WOLFSSL_TEST_CERT
  15734. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15735. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15736. #else
  15737. DecodedCert cert[1];
  15738. #endif
  15739. #ifndef NO_ASN_TIME
  15740. struct tm timearg;
  15741. const byte* date;
  15742. byte dateFormat;
  15743. int dateLength;
  15744. #endif
  15745. #endif
  15746. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  15747. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15748. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15749. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15750. if (in == NULL || out == NULL || plain == NULL)
  15751. ERROR_OUT(MEMORY_E, exit_rsa);
  15752. #endif
  15753. XMEMCPY(in, inStr, inLen);
  15754. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15755. if (key == NULL)
  15756. ERROR_OUT(MEMORY_E, exit_rsa);
  15757. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15758. if (keypub == NULL)
  15759. ERROR_OUT(MEMORY_E, exit_rsa);
  15760. #endif
  15761. #ifdef WOLFSSL_TEST_CERT
  15762. if (cert == NULL)
  15763. ERROR_OUT(MEMORY_E, exit_rsa);
  15764. #endif
  15765. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  15766. /* initialize stack structures */
  15767. XMEMSET(&rng, 0, sizeof(rng));
  15768. XMEMSET(key, 0, sizeof *key);
  15769. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15770. XMEMSET(keypub, 0, sizeof *keypub);
  15771. #endif
  15772. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  15773. ret = rsa_decode_test(key);
  15774. if (ret != 0)
  15775. ERROR_OUT(ret, exit_rsa);
  15776. #endif
  15777. #ifdef USE_CERT_BUFFERS_1024
  15778. bytes = (size_t)sizeof_client_key_der_1024;
  15779. if (bytes < (size_t)sizeof_client_cert_der_1024)
  15780. bytes = (size_t)sizeof_client_cert_der_1024;
  15781. #elif defined(USE_CERT_BUFFERS_2048)
  15782. bytes = (size_t)sizeof_client_key_der_2048;
  15783. if (bytes < (size_t)sizeof_client_cert_der_2048)
  15784. bytes = (size_t)sizeof_client_cert_der_2048;
  15785. #elif defined(USE_CERT_BUFFERS_3072)
  15786. bytes = (size_t)sizeof_client_key_der_3072;
  15787. if (bytes < (size_t)sizeof_client_cert_der_3072)
  15788. bytes = (size_t)sizeof_client_cert_der_3072;
  15789. #elif defined(USE_CERT_BUFFERS_4096)
  15790. bytes = (size_t)sizeof_client_key_der_4096;
  15791. if (bytes < (size_t)sizeof_client_cert_der_4096)
  15792. bytes = (size_t)sizeof_client_cert_der_4096;
  15793. #else
  15794. bytes = FOURK_BUF;
  15795. #endif
  15796. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15797. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15798. if (tmp == NULL)
  15799. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15800. #endif
  15801. #ifdef USE_CERT_BUFFERS_1024
  15802. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  15803. #elif defined(USE_CERT_BUFFERS_2048)
  15804. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  15805. #elif defined(USE_CERT_BUFFERS_3072)
  15806. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  15807. #elif defined(USE_CERT_BUFFERS_4096)
  15808. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  15809. #elif !defined(NO_FILESYSTEM)
  15810. file = XFOPEN(clientKey, "rb");
  15811. if (!file) {
  15812. err_sys("can't open ./certs/client-key.der, "
  15813. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  15814. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15815. }
  15816. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  15817. XFCLOSE(file);
  15818. if (bytes == 0)
  15819. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15820. #else
  15821. /* No key to use. */
  15822. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15823. #endif /* USE_CERT_BUFFERS */
  15824. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  15825. if (ret != 0)
  15826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15827. #ifndef NO_ASN
  15828. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  15829. if (ret != 0)
  15830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15831. #ifndef NO_SIG_WRAPPER
  15832. modLen = wc_RsaEncryptSize(key);
  15833. #endif
  15834. #else
  15835. #ifdef USE_CERT_BUFFERS_2048
  15836. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  15837. if (ret != 0)
  15838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15839. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  15840. if (ret != 0)
  15841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15842. #ifndef NO_SIG_WRAPPER
  15843. modLen = 2048;
  15844. #endif
  15845. #else
  15846. #error Not supported yet!
  15847. #endif
  15848. #endif
  15849. #ifndef WC_NO_RNG
  15850. #ifndef HAVE_FIPS
  15851. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15852. #else
  15853. ret = wc_InitRng(&rng);
  15854. #endif
  15855. if (ret != 0)
  15856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15857. #endif
  15858. #ifndef NO_SIG_WRAPPER
  15859. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  15860. if (ret != 0)
  15861. goto exit_rsa;
  15862. #endif
  15863. #ifdef WC_RSA_NONBLOCK
  15864. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  15865. if (ret != 0)
  15866. goto exit_rsa;
  15867. #endif
  15868. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15869. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15870. do {
  15871. #if defined(WOLFSSL_ASYNC_CRYPT)
  15872. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15873. #endif
  15874. if (ret >= 0) {
  15875. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  15876. }
  15877. } while (ret == WC_PENDING_E);
  15878. if (ret < 0)
  15879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15880. TEST_SLEEP();
  15881. #ifdef WC_RSA_BLINDING
  15882. {
  15883. wc_test_ret_t tmpret = ret;
  15884. ret = wc_RsaSetRNG(key, &rng);
  15885. if (ret < 0)
  15886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15887. ret = tmpret;
  15888. }
  15889. #endif
  15890. idx = (word32)ret; /* save off encrypted length */
  15891. do {
  15892. #if defined(WOLFSSL_ASYNC_CRYPT)
  15893. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15894. #endif
  15895. if (ret >= 0) {
  15896. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  15897. }
  15898. } while (ret == WC_PENDING_E);
  15899. if (ret < 0)
  15900. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15901. if (XMEMCMP(plain, in, inLen)) {
  15902. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15903. }
  15904. TEST_SLEEP();
  15905. do {
  15906. #if defined(WOLFSSL_ASYNC_CRYPT)
  15907. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15908. #endif
  15909. if (ret >= 0) {
  15910. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  15911. }
  15912. } while (ret == WC_PENDING_E);
  15913. if (ret < 0)
  15914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15915. if (ret != (int)inLen) {
  15916. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15917. }
  15918. if (XMEMCMP(res, in, inLen)) {
  15919. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15920. }
  15921. TEST_SLEEP();
  15922. do {
  15923. #if defined(WOLFSSL_ASYNC_CRYPT)
  15924. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15925. #endif
  15926. if (ret >= 0) {
  15927. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  15928. }
  15929. } while (ret == WC_PENDING_E);
  15930. if (ret < 0)
  15931. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15932. TEST_SLEEP();
  15933. #elif defined(WOLFSSL_PUBLIC_MP)
  15934. {
  15935. static byte signature_2048[] = {
  15936. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  15937. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  15938. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  15939. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  15940. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  15941. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  15942. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  15943. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  15944. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  15945. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  15946. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  15947. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  15948. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  15949. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  15950. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  15951. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  15952. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  15953. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  15954. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  15955. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  15956. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  15957. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  15958. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  15959. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  15960. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  15961. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  15962. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  15963. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  15964. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  15965. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  15966. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  15967. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  15968. };
  15969. ret = sizeof(signature_2048);
  15970. XMEMCPY(out, signature_2048, ret);
  15971. }
  15972. #endif
  15973. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  15974. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  15975. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15976. idx = (word32)ret;
  15977. XMEMSET(plain, 0, plainSz);
  15978. do {
  15979. #if defined(WOLFSSL_ASYNC_CRYPT)
  15980. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15981. #endif
  15982. if (ret >= 0) {
  15983. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  15984. #if defined(WOLFSSL_CRYPTOCELL)
  15985. /*
  15986. Cryptocell requires the input data and signature byte array to verify.
  15987. first argument must be the input data
  15988. second argument must be the length of input data
  15989. third argument must be the signature byte array or the output from
  15990. wc_RsaSSL_Sign()
  15991. fourth argument must be the length of the signature byte array
  15992. */
  15993. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  15994. #else
  15995. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  15996. #endif /* WOLFSSL_CRYPTOCELL */
  15997. #else
  15998. byte* dec = NULL;
  15999. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  16000. if (ret > 0) {
  16001. XMEMCPY(plain, dec, ret);
  16002. }
  16003. #endif
  16004. }
  16005. } while (ret == WC_PENDING_E);
  16006. if (ret < 0)
  16007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16008. if (XMEMCMP(plain, in, (size_t)ret)) {
  16009. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16010. }
  16011. TEST_SLEEP();
  16012. #endif
  16013. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16014. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  16015. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  16016. (!defined(HAVE_FIPS) || \
  16017. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  16018. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16019. ret = rsa_oaep_padding_test(key, &rng);
  16020. if (ret != 0)
  16021. return ret;
  16022. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  16023. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  16024. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  16025. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  16026. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16027. ret = rsa_export_key_test(key);
  16028. if (ret != 0)
  16029. return ret;
  16030. #endif
  16031. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16032. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16033. ret = rsa_flatten_test(key);
  16034. if (ret != 0)
  16035. return ret;
  16036. #endif
  16037. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  16038. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  16039. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  16040. (void)clientCert;
  16041. #endif
  16042. #ifdef WOLFSSL_TEST_CERT
  16043. #if defined(WOLFSSL_MDK_ARM)
  16044. #define sizeof(s) XSTRLEN((char *)(s))
  16045. #endif
  16046. #ifdef USE_CERT_BUFFERS_1024
  16047. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  16048. bytes = (size_t)sizeof_client_cert_der_1024;
  16049. #elif defined(USE_CERT_BUFFERS_2048)
  16050. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  16051. bytes = (size_t)sizeof_client_cert_der_2048;
  16052. #elif defined(USE_CERT_BUFFERS_3072)
  16053. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  16054. bytes = (size_t)sizeof_client_cert_der_3072;
  16055. #elif defined(USE_CERT_BUFFERS_4096)
  16056. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  16057. bytes = (size_t)sizeof_client_cert_der_4096;
  16058. #elif !defined(NO_FILESYSTEM)
  16059. file2 = XFOPEN(clientCert, "rb");
  16060. if (!file2) {
  16061. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16062. }
  16063. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  16064. XFCLOSE(file2);
  16065. if (bytes == 0)
  16066. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16067. #else
  16068. /* No certificate to use. */
  16069. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16070. #endif
  16071. #ifdef sizeof
  16072. #undef sizeof
  16073. #endif
  16074. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  16075. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  16076. if (ret != 0) {
  16077. FreeDecodedCert(cert);
  16078. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16079. }
  16080. #ifndef NO_ASN_TIME
  16081. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  16082. &dateFormat, &dateLength);
  16083. if (ret != 0) {
  16084. FreeDecodedCert(cert);
  16085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16086. }
  16087. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  16088. if (ret != 0) {
  16089. FreeDecodedCert(cert);
  16090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16091. }
  16092. #endif
  16093. FreeDecodedCert(cert);
  16094. #endif /* WOLFSSL_TEST_CERT */
  16095. #ifdef WOLFSSL_CERT_EXT
  16096. #ifdef USE_CERT_BUFFERS_1024
  16097. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16098. bytes = sizeof_client_keypub_der_1024;
  16099. #elif defined(USE_CERT_BUFFERS_2048)
  16100. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16101. bytes = sizeof_client_keypub_der_2048;
  16102. #elif defined(USE_CERT_BUFFERS_3072)
  16103. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  16104. bytes = sizeof_client_keypub_der_3072;
  16105. #elif defined(USE_CERT_BUFFERS_4096)
  16106. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  16107. bytes = sizeof_client_keypub_der_4096;
  16108. #else
  16109. file = XFOPEN(clientKeyPub, "rb");
  16110. if (!file) {
  16111. err_sys("can't open ./certs/client-keyPub.der, "
  16112. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  16113. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16114. }
  16115. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16116. XFCLOSE(file);
  16117. if (bytes == 0)
  16118. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16119. #endif /* USE_CERT_BUFFERS */
  16120. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  16121. if (ret != 0)
  16122. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16123. idx = 0;
  16124. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  16125. if (ret != 0)
  16126. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16127. #endif /* WOLFSSL_CERT_EXT */
  16128. #ifdef WOLFSSL_KEY_GEN
  16129. ret = rsa_keygen_test(&rng);
  16130. if (ret != 0)
  16131. goto exit_rsa;
  16132. #endif
  16133. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16134. /* Make Cert / Sign example for RSA cert and RSA CA */
  16135. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  16136. if (ret != 0)
  16137. goto exit_rsa;
  16138. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  16139. ret = rsa_ecc_certgen_test(&rng, tmp);
  16140. if (ret != 0)
  16141. goto exit_rsa;
  16142. #endif
  16143. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16144. {
  16145. Cert *req;
  16146. int derSz;
  16147. #ifndef WOLFSSL_SMALL_STACK
  16148. byte* der = NULL;
  16149. #endif
  16150. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16151. if (! req)
  16152. ERROR_OUT(MEMORY_E, exit_rsa);
  16153. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  16154. if (der == NULL) {
  16155. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16156. }
  16157. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  16158. if (ret != 0)
  16159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16160. req->version = 0;
  16161. req->isCA = 1;
  16162. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  16163. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  16164. #ifndef NO_SHA256
  16165. req->sigType = CTC_SHA256wRSA;
  16166. #else
  16167. req->sigType = CTC_SHAwRSA;
  16168. #endif
  16169. #ifdef WOLFSSL_CERT_EXT
  16170. /* add SKID from the Public Key */
  16171. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  16172. if (ret != 0)
  16173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16174. /* add Key Usage */
  16175. ret = wc_SetKeyUsage(req, certKeyUsage2);
  16176. if (ret != 0)
  16177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16178. /* add Extended Key Usage */
  16179. ret = wc_SetExtKeyUsage(req,
  16180. "serverAuth,clientAuth,codeSigning,"
  16181. "emailProtection,timeStamping,OCSPSigning");
  16182. if (ret != 0)
  16183. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16184. #ifdef WOLFSSL_EKU_OID
  16185. {
  16186. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  16187. "2.16.840.1.111111.100.1.10.1";
  16188. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  16189. HEAP_HINT);
  16190. if (ret != 0)
  16191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16192. }
  16193. #endif /* WOLFSSL_EKU_OID */
  16194. #endif /* WOLFSSL_CERT_EXT */
  16195. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  16196. if (derSz < 0) {
  16197. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16198. }
  16199. #ifdef WOLFSSL_CERT_EXT
  16200. /* Try again with "any" flag set, will override all others */
  16201. ret = wc_SetExtKeyUsage(req, "any");
  16202. if (ret != 0)
  16203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16204. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  16205. if (derSz < 0) {
  16206. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16207. }
  16208. #endif /* WOLFSSL_CERT_EXT */
  16209. ret = 0;
  16210. do {
  16211. #if defined(WOLFSSL_ASYNC_CRYPT)
  16212. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16213. #endif
  16214. if (ret >= 0) {
  16215. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  16216. key, NULL, &rng);
  16217. }
  16218. } while (ret == WC_PENDING_E);
  16219. if (ret < 0)
  16220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16221. derSz = (word32)ret;
  16222. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  16223. CERTREQ_TYPE);
  16224. if (ret != 0) {
  16225. goto exit_rsa;
  16226. }
  16227. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  16228. if (derSz < 0) {
  16229. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16230. }
  16231. /* Test getting the size of the buffer without providing the buffer.
  16232. * derSz is set to the "largest buffer" we are willing to allocate. */
  16233. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  16234. if (derSz < 0) {
  16235. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16236. }
  16237. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16238. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16239. der = NULL;
  16240. }
  16241. #endif /* WOLFSSL_CERT_REQ */
  16242. #endif /* WOLFSSL_CERT_GEN */
  16243. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  16244. /* Need to create known good signatures to test with this. */
  16245. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16246. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16247. ret = rsa_pss_test(&rng, key);
  16248. if (ret != 0)
  16249. goto exit_rsa;
  16250. #endif
  16251. #endif
  16252. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  16253. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16254. /* New key to be loaded in rsa_even_mod_test(). */
  16255. if (key != NULL)
  16256. #endif
  16257. wc_FreeRsaKey(key);
  16258. /* New key to be loaded in rsa_even_mod_test(). */
  16259. ret = rsa_even_mod_test(&rng, key);
  16260. #endif
  16261. exit_rsa:
  16262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16263. if (key != NULL) {
  16264. wc_FreeRsaKey(key);
  16265. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16266. }
  16267. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16268. if (keypub != NULL) {
  16269. wc_FreeRsaKey(keypub);
  16270. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16271. }
  16272. #endif
  16273. #ifdef WOLFSSL_TEST_CERT
  16274. if (cert != NULL)
  16275. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16276. #endif
  16277. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16278. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16279. #else
  16280. wc_FreeRsaKey(key);
  16281. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16282. wc_FreeRsaKey(keypub);
  16283. #endif
  16284. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16285. wc_FreeRng(&rng);
  16286. WC_FREE_VAR(in, HEAP_HINT);
  16287. WC_FREE_VAR(out, HEAP_HINT);
  16288. WC_FREE_VAR(plain, HEAP_HINT);
  16289. (void)res;
  16290. (void)bytes;
  16291. (void)idx;
  16292. (void)in;
  16293. (void)out;
  16294. (void)plain;
  16295. (void)idx;
  16296. (void)inStr;
  16297. (void)inLen;
  16298. (void)outSz;
  16299. (void)plainSz;
  16300. /* ret can be greater then 0 with certgen but all negative values should
  16301. * be returned and treated as an error */
  16302. if (ret >= 0) {
  16303. return 0;
  16304. }
  16305. else {
  16306. return ret;
  16307. }
  16308. }
  16309. #endif /* !NO_RSA */
  16310. #ifndef NO_DH
  16311. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  16312. {
  16313. wc_test_ret_t ret = 0;
  16314. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16315. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16316. #else
  16317. DhKey key[1];
  16318. #endif
  16319. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  16320. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  16321. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  16322. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  16323. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  16324. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  16325. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  16326. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  16327. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  16328. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  16329. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  16330. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  16331. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  16332. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  16333. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  16334. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  16335. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  16336. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  16337. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  16338. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  16339. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  16340. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  16341. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  16342. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  16343. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  16344. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  16345. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  16346. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  16347. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  16348. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  16349. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  16350. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  16351. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  16352. };
  16353. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16354. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  16355. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  16356. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  16357. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  16358. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  16359. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  16360. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  16361. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  16362. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  16363. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  16364. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  16365. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  16366. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  16367. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  16368. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  16369. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  16370. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  16371. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  16372. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  16373. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  16374. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  16375. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  16376. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  16377. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  16378. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  16379. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  16380. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  16381. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  16382. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  16383. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  16384. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  16385. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  16386. };
  16387. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  16388. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  16389. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  16390. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  16391. 0x40, 0x52, 0xed, 0x41
  16392. };
  16393. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  16394. 0x00,
  16395. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  16396. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  16397. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  16398. 0x40, 0x52, 0xed, 0x41
  16399. };
  16400. byte priv[256];
  16401. byte pub[256];
  16402. word32 privSz = sizeof(priv);
  16403. word32 pubSz = sizeof(pub);
  16404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16405. if (key == NULL)
  16406. ERROR_OUT(MEMORY_E, exit_gen_test);
  16407. #endif
  16408. /* Parameter Validation testing. */
  16409. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  16410. if (ret != BAD_FUNC_ARG)
  16411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16412. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  16413. if (ret != BAD_FUNC_ARG)
  16414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16415. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  16416. if (ret != BAD_FUNC_ARG)
  16417. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16418. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  16419. if (ret != BAD_FUNC_ARG)
  16420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16421. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  16422. if (ret != BAD_FUNC_ARG)
  16423. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16424. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  16425. if (ret != BAD_FUNC_ARG)
  16426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16427. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16428. if (ret != 0)
  16429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16430. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  16431. if (ret != 0)
  16432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16433. wc_FreeDhKey(key);
  16434. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16435. if (ret != 0)
  16436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16437. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  16438. if (ret != 0)
  16439. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16440. /* Use API. */
  16441. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16442. #if defined(WOLFSSL_ASYNC_CRYPT)
  16443. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16444. #endif
  16445. if (ret != 0)
  16446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16447. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  16448. if (ret != 0)
  16449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16450. wc_FreeDhKey(key);
  16451. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16452. if (ret != 0)
  16453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16454. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  16455. if (ret != 0)
  16456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16457. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  16458. if (ret != 0)
  16459. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16460. #ifndef HAVE_SELFTEST
  16461. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16462. if (ret != 0)
  16463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16464. /* Taint the public key so the check fails. */
  16465. pub[0]++;
  16466. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16467. if (ret != MP_CMP_E) {
  16468. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16469. }
  16470. #ifdef WOLFSSL_KEY_GEN
  16471. wc_FreeDhKey(key);
  16472. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16473. if (ret != 0)
  16474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16475. ret = wc_DhGenerateParams(rng, 2048, key);
  16476. if (ret != 0)
  16477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16478. privSz = sizeof(priv);
  16479. pubSz = sizeof(pub);
  16480. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16481. #if defined(WOLFSSL_ASYNC_CRYPT)
  16482. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16483. #endif
  16484. if (ret != 0)
  16485. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16486. #endif /* WOLFSSL_KEY_GEN */
  16487. #endif /* HAVE_SELFTEST */
  16488. ret = 0;
  16489. exit_gen_test:
  16490. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16491. if (key) {
  16492. wc_FreeDhKey(key);
  16493. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16494. }
  16495. #else
  16496. wc_FreeDhKey(key);
  16497. #endif
  16498. return ret;
  16499. }
  16500. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  16501. {
  16502. wc_test_ret_t ret = 0;
  16503. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16504. DhKey *smallKey = NULL;
  16505. #else
  16506. DhKey smallKey[1];
  16507. #endif
  16508. byte p[2] = { 1, 7 }; /* 263 in decimal */
  16509. byte g[2] = { 0, 2 };
  16510. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  16511. #ifdef WOLFSSL_DH_CONST
  16512. /* the table for constant DH lookup will round to the lowest byte size 21 */
  16513. byte priv[21];
  16514. byte pub[21];
  16515. #else
  16516. byte priv[2];
  16517. byte pub[2];
  16518. #endif
  16519. word32 privSz = sizeof(priv);
  16520. word32 pubSz = sizeof(pub);
  16521. #endif
  16522. int smallKey_inited = 0;
  16523. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16524. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  16525. return WC_TEST_RET_ENC_ERRNO;
  16526. #endif
  16527. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  16528. if (ret != 0)
  16529. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16530. smallKey_inited = 1;
  16531. /* Parameter Validation testing. */
  16532. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  16533. if (ret != BAD_FUNC_ARG)
  16534. return WC_TEST_RET_ENC_EC(ret);
  16535. wc_FreeDhKey(NULL);
  16536. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  16537. if (ret != BAD_FUNC_ARG) {
  16538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16539. }
  16540. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  16541. if (ret != BAD_FUNC_ARG) {
  16542. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16543. }
  16544. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  16545. if (ret != BAD_FUNC_ARG) {
  16546. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16547. }
  16548. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  16549. if (ret != BAD_FUNC_ARG) {
  16550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16551. }
  16552. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  16553. if (ret != BAD_FUNC_ARG) {
  16554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16555. }
  16556. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  16557. if (ret != 0)
  16558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16559. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  16560. /* Use API. */
  16561. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  16562. #if defined(WOLFSSL_ASYNC_CRYPT)
  16563. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16564. #endif
  16565. if (ret != 0) {
  16566. ret = WC_TEST_RET_ENC_EC(ret);
  16567. }
  16568. #else
  16569. (void)rng;
  16570. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  16571. ret = 0;
  16572. #endif
  16573. #endif
  16574. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  16575. {
  16576. byte priv[260];
  16577. byte pub[260];
  16578. word32 privSz = sizeof(priv);
  16579. word32 pubSz = sizeof(pub);
  16580. /* test odd ball param generation with DH */
  16581. wc_FreeDhKey(smallKey);
  16582. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  16583. if (ret != 0)
  16584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16585. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  16586. if (ret != 0)
  16587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16588. privSz = sizeof(priv);
  16589. pubSz = sizeof(pub);
  16590. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  16591. #if defined(WOLFSSL_ASYNC_CRYPT)
  16592. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16593. #endif
  16594. if (ret != 0)
  16595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16596. }
  16597. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  16598. exit_gen_test:
  16599. if (smallKey_inited)
  16600. wc_FreeDhKey(smallKey);
  16601. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16602. if (smallKey != NULL)
  16603. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16604. #endif
  16605. return ret;
  16606. }
  16607. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16608. typedef struct dh_pubvalue_test {
  16609. const byte* data;
  16610. word32 len;
  16611. } dh_pubvalue_test;
  16612. static wc_test_ret_t dh_test_check_pubvalue(void)
  16613. {
  16614. wc_test_ret_t ret;
  16615. word32 i;
  16616. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  16617. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  16618. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  16619. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  16620. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  16621. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  16622. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  16623. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  16624. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  16625. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  16626. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  16627. const dh_pubvalue_test dh_pubval_fail[] = {
  16628. { prime, sizeof(prime) },
  16629. { pubValZero, sizeof(pubValZero) },
  16630. { pubValZeroLong, sizeof(pubValZeroLong) },
  16631. { pubValOne, sizeof(pubValOne) },
  16632. { pubValOneLong, sizeof(pubValOneLong) },
  16633. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  16634. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  16635. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  16636. { pubValTooBig0, sizeof(pubValTooBig0) },
  16637. { pubValTooBig1, sizeof(pubValTooBig1) },
  16638. { pubValTooLong, sizeof(pubValTooLong) },
  16639. };
  16640. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  16641. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  16642. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  16643. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  16644. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  16645. const dh_pubvalue_test dh_pubval_pass[] = {
  16646. { pubValTwo, sizeof(pubValTwo) },
  16647. { pubValTwoLong, sizeof(pubValTwoLong) },
  16648. { pubValGood, sizeof(pubValGood) },
  16649. { pubValGoodLen, sizeof(pubValGoodLen) },
  16650. { pubValGoodLong, sizeof(pubValGoodLong) },
  16651. };
  16652. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  16653. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  16654. dh_pubval_fail[i].len);
  16655. if (ret != MP_VAL)
  16656. return WC_TEST_RET_ENC_I(i);
  16657. }
  16658. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  16659. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  16660. dh_pubval_pass[i].len);
  16661. if (ret != 0)
  16662. return WC_TEST_RET_ENC_I(i);
  16663. }
  16664. return 0;
  16665. }
  16666. #endif
  16667. #if defined(HAVE_FFDHE)
  16668. #if defined(HAVE_FFDHE_4096)
  16669. #define MAX_DH_PRIV_SZ 39
  16670. #define MAX_DH_KEY_SZ 512
  16671. #elif defined(HAVE_FFDHE_3072)
  16672. #define MAX_DH_PRIV_SZ 34
  16673. #define MAX_DH_KEY_SZ 384
  16674. #else
  16675. #define MAX_DH_PRIV_SZ 29
  16676. #define MAX_DH_KEY_SZ 256
  16677. #endif
  16678. #ifndef WC_NO_RNG
  16679. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  16680. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  16681. #ifdef HAVE_PUBLIC_FFDHE
  16682. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  16683. #else
  16684. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  16685. #endif
  16686. {
  16687. wc_test_ret_t ret;
  16688. word32 privSz, pubSz, privSz2, pubSz2;
  16689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16690. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16691. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16692. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16693. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16694. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16695. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16696. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16697. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16698. #else
  16699. byte priv[MAX_DH_PRIV_SZ];
  16700. byte pub[MAX_DH_KEY_SZ];
  16701. byte priv2[MAX_DH_PRIV_SZ];
  16702. byte pub2[MAX_DH_KEY_SZ];
  16703. byte agree[MAX_DH_KEY_SZ];
  16704. byte agree2[MAX_DH_KEY_SZ];
  16705. DhKey key[1];
  16706. DhKey key2[1];
  16707. #endif
  16708. word32 agreeSz = MAX_DH_KEY_SZ;
  16709. word32 agreeSz2 = MAX_DH_KEY_SZ;
  16710. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16711. if ((priv == NULL) ||
  16712. (pub == NULL) ||
  16713. (priv2 == NULL) ||
  16714. (pub2 == NULL) ||
  16715. (agree == NULL) ||
  16716. (agree2 == NULL) ||
  16717. (key == NULL) ||
  16718. (key2 == NULL))
  16719. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16720. #endif
  16721. pubSz = MAX_DH_KEY_SZ;
  16722. pubSz2 = MAX_DH_KEY_SZ;
  16723. #ifdef HAVE_PUBLIC_FFDHE
  16724. privSz = MAX_DH_PRIV_SZ;
  16725. privSz2 = MAX_DH_PRIV_SZ;
  16726. #else
  16727. privSz = wc_DhGetNamedKeyMinSize(name);
  16728. privSz2 = privSz;
  16729. #endif
  16730. XMEMSET(key, 0, sizeof(*key));
  16731. XMEMSET(key2, 0, sizeof(*key2));
  16732. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16733. if (ret != 0)
  16734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16735. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  16736. if (ret != 0)
  16737. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16738. #ifdef HAVE_PUBLIC_FFDHE
  16739. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  16740. #else
  16741. ret = wc_DhSetNamedKey(key, name);
  16742. #endif
  16743. if (ret != 0)
  16744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16745. #ifdef HAVE_PUBLIC_FFDHE
  16746. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  16747. params->g_len);
  16748. #else
  16749. ret = wc_DhSetNamedKey(key2, name);
  16750. #endif
  16751. if (ret != 0)
  16752. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16753. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16754. #if defined(WOLFSSL_ASYNC_CRYPT)
  16755. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16756. #endif
  16757. if (ret != 0)
  16758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16759. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  16760. #if defined(WOLFSSL_ASYNC_CRYPT)
  16761. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  16762. #endif
  16763. if (ret != 0)
  16764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16765. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  16766. #if defined(WOLFSSL_ASYNC_CRYPT)
  16767. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16768. #endif
  16769. if (ret != 0)
  16770. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16771. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  16772. #if defined(WOLFSSL_ASYNC_CRYPT)
  16773. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  16774. #endif
  16775. if (ret != 0)
  16776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16777. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  16778. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16779. }
  16780. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  16781. /* Make p even */
  16782. key->p.dp[0] &= (mp_digit)-2;
  16783. if (ret != 0)
  16784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16785. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16786. #if defined(WOLFSSL_ASYNC_CRYPT)
  16787. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16788. #endif
  16789. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  16790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16791. }
  16792. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  16793. #if defined(WOLFSSL_ASYNC_CRYPT)
  16794. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16795. #endif
  16796. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  16797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16798. }
  16799. #ifndef HAVE_SELFTEST
  16800. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16801. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  16802. ret != ASYNC_OP_E) {
  16803. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16804. }
  16805. #endif
  16806. /* Getting here means success - set ret to 0. */
  16807. ret = 0;
  16808. #endif
  16809. done:
  16810. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  16811. !defined(WC_NO_RNG)
  16812. if (priv)
  16813. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16814. if (pub)
  16815. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16816. if (priv2)
  16817. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16818. if (pub2)
  16819. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16820. if (agree)
  16821. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16822. if (agree2)
  16823. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16824. if (key) {
  16825. wc_FreeDhKey(key);
  16826. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16827. }
  16828. if (key2) {
  16829. wc_FreeDhKey(key2);
  16830. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16831. }
  16832. #else
  16833. wc_FreeDhKey(key);
  16834. wc_FreeDhKey(key2);
  16835. #endif
  16836. return ret;
  16837. }
  16838. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  16839. #endif /* !WC_NO_RNG */
  16840. #endif /* HAVE_FFDHE */
  16841. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  16842. {
  16843. wc_test_ret_t ret;
  16844. word32 bytes;
  16845. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  16846. #ifndef WC_NO_RNG
  16847. WC_RNG rng;
  16848. int rngInit = 0;
  16849. #endif
  16850. int keyInit = 0, key2Init = 0;
  16851. #define DH_TEST_TMP_SIZE 1024
  16852. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  16853. #define DH_TEST_BUF_SIZE 256
  16854. #else
  16855. #define DH_TEST_BUF_SIZE 512
  16856. #endif
  16857. #ifndef WC_NO_RNG
  16858. word32 agreeSz = DH_TEST_BUF_SIZE;
  16859. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  16860. #endif
  16861. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16862. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16863. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16864. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16865. #else
  16866. DhKey key[1];
  16867. DhKey key2[1];
  16868. byte tmp[DH_TEST_TMP_SIZE];
  16869. #endif
  16870. #ifndef WC_NO_RNG
  16871. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16872. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16873. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16874. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16875. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16876. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16877. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16878. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  16879. agree == NULL || agree2 == NULL) {
  16880. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16881. }
  16882. #else
  16883. byte priv[DH_TEST_BUF_SIZE];
  16884. byte pub[DH_TEST_BUF_SIZE];
  16885. byte priv2[DH_TEST_BUF_SIZE];
  16886. byte pub2[DH_TEST_BUF_SIZE];
  16887. byte agree[DH_TEST_BUF_SIZE];
  16888. byte agree2[DH_TEST_BUF_SIZE];
  16889. #endif
  16890. #endif /* !WC_NO_RNG */
  16891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16892. if (key == NULL || key2 == NULL || tmp == NULL) {
  16893. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16894. }
  16895. #endif
  16896. #ifdef USE_CERT_BUFFERS_1024
  16897. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  16898. bytes = (size_t)sizeof_dh_key_der_1024;
  16899. #elif defined(USE_CERT_BUFFERS_2048)
  16900. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  16901. bytes = (size_t)sizeof_dh_key_der_2048;
  16902. #elif defined(USE_CERT_BUFFERS_3072)
  16903. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  16904. bytes = (size_t)sizeof_dh_key_der_3072;
  16905. #elif defined(USE_CERT_BUFFERS_4096)
  16906. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  16907. bytes = (size_t)sizeof_dh_key_der_4096;
  16908. #elif defined(NO_ASN)
  16909. /* don't use file, no DER parsing */
  16910. #elif !defined(NO_FILESYSTEM)
  16911. {
  16912. XFILE file = XFOPEN(dhParamsFile, "rb");
  16913. if (! file)
  16914. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16915. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  16916. XFCLOSE(file);
  16917. if (bytes == 0)
  16918. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16919. }
  16920. #else
  16921. /* No DH key to use. */
  16922. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16923. #endif /* USE_CERT_BUFFERS */
  16924. (void)idx;
  16925. (void)tmp;
  16926. (void)bytes;
  16927. pubSz = DH_TEST_BUF_SIZE;
  16928. pubSz2 = DH_TEST_BUF_SIZE;
  16929. privSz = DH_TEST_BUF_SIZE;
  16930. privSz2 = DH_TEST_BUF_SIZE;
  16931. #ifndef WC_NO_RNG
  16932. XMEMSET(&rng, 0, sizeof(rng));
  16933. #endif
  16934. /* Use API for coverage. */
  16935. ret = wc_InitDhKey(key);
  16936. if (ret != 0)
  16937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16938. wc_FreeDhKey(key);
  16939. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16940. if (ret != 0)
  16941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16942. keyInit = 1;
  16943. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  16944. if (ret != 0)
  16945. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16946. key2Init = 1;
  16947. #ifdef NO_ASN
  16948. #ifndef WOLFSSL_SP_MATH
  16949. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  16950. if (ret != 0)
  16951. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16952. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  16953. if (ret != 0)
  16954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16955. #else
  16956. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  16957. sizeof(dh2048_g));
  16958. if (ret != 0)
  16959. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16960. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  16961. sizeof(dh2048_g));
  16962. if (ret != 0)
  16963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16964. #endif
  16965. #else
  16966. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  16967. if (ret != 0)
  16968. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16969. idx = 0;
  16970. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  16971. if (ret != 0)
  16972. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16973. #endif
  16974. #ifndef WC_NO_RNG
  16975. #ifndef HAVE_FIPS
  16976. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16977. #else
  16978. ret = wc_InitRng(&rng);
  16979. #endif
  16980. if (ret != 0)
  16981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16982. rngInit = 1;
  16983. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  16984. #if defined(WOLFSSL_ASYNC_CRYPT)
  16985. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16986. #endif
  16987. if (ret != 0)
  16988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16989. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  16990. #if defined(WOLFSSL_ASYNC_CRYPT)
  16991. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  16992. #endif
  16993. if (ret != 0)
  16994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16995. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  16996. #if defined(WOLFSSL_ASYNC_CRYPT)
  16997. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16998. #endif
  16999. if (ret != 0)
  17000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17001. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  17002. #if defined(WOLFSSL_ASYNC_CRYPT)
  17003. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  17004. #endif
  17005. if (ret != 0)
  17006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17007. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  17008. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17009. }
  17010. #endif /* !WC_NO_RNG */
  17011. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17012. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  17013. if (ret != BAD_FUNC_ARG)
  17014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17015. ret = wc_DhCheckPrivKey(key, priv, privSz);
  17016. if (ret != 0)
  17017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17018. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  17019. if (ret != BAD_FUNC_ARG)
  17020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17021. {
  17022. word32 pSz, qSz, gSz;
  17023. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  17024. if (ret != LENGTH_ONLY_E)
  17025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17026. }
  17027. #endif
  17028. /* Test DH key import / export */
  17029. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  17030. (!defined(HAVE_FIPS) || \
  17031. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  17032. wc_FreeDhKey(key);
  17033. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17034. if (ret != 0)
  17035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17036. #ifndef NO_ASN
  17037. {
  17038. /* DH Private - Key Export / Import */
  17039. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17040. byte *tmp2;
  17041. #else
  17042. byte tmp2[DH_TEST_TMP_SIZE];
  17043. #endif
  17044. #if defined(USE_CERT_BUFFERS_2048)
  17045. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  17046. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  17047. #else
  17048. XFILE file = XFOPEN(dhKeyFile, "rb");
  17049. if (!file)
  17050. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17051. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  17052. XFCLOSE(file);
  17053. if (bytes == 0)
  17054. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17055. #endif
  17056. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17057. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17058. if (tmp2 == NULL)
  17059. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17060. #endif
  17061. idx = 0;
  17062. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  17063. /* Import DH Private key as DER */
  17064. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  17065. if (ret == 0) {
  17066. /* Export as DER */
  17067. idx = DH_TEST_TMP_SIZE;
  17068. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  17069. }
  17070. /* Verify export matches original */
  17071. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  17072. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17073. }
  17074. /* DH Public Key - Export / Import */
  17075. #if defined(USE_CERT_BUFFERS_2048)
  17076. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  17077. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  17078. #else
  17079. file = XFOPEN(dhKeyPubFile, "rb");
  17080. if (!file)
  17081. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17082. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  17083. XFCLOSE(file);
  17084. if (bytes == 0)
  17085. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17086. #endif
  17087. /* for HAVE_WOLF_BIGINT prevent leak */
  17088. wc_FreeDhKey(key);
  17089. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17090. idx = 0;
  17091. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  17092. /* Import DH Public key as DER */
  17093. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  17094. if (ret == 0) {
  17095. /* Export as DER */
  17096. idx = DH_TEST_TMP_SIZE;
  17097. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  17098. }
  17099. /* Verify export matches original */
  17100. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  17101. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17102. }
  17103. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17104. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17105. #endif
  17106. }
  17107. #else
  17108. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17109. if (ret != 0)
  17110. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17111. #endif /* !NO_ASN */
  17112. privSz = DH_TEST_BUF_SIZE;
  17113. pubSz = DH_TEST_BUF_SIZE;
  17114. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  17115. if (ret != 0)
  17116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17117. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  17118. if (ret != 0)
  17119. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17120. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  17121. #ifndef WC_NO_RNG
  17122. ret = dh_generate_test(&rng);
  17123. if (ret != 0)
  17124. ERROR_OUT(ret, done);
  17125. ret = dh_fips_generate_test(&rng);
  17126. if (ret != 0)
  17127. ERROR_OUT(ret, done);
  17128. #endif /* !WC_NO_RNG */
  17129. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17130. ret = dh_test_check_pubvalue();
  17131. if (ret != 0)
  17132. ERROR_OUT(ret, done);
  17133. #endif
  17134. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17135. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  17136. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  17137. * this will be available for testing in the 140-3 module */
  17138. #ifndef WC_NO_RNG
  17139. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  17140. #ifdef HAVE_FFDHE_2048
  17141. #ifdef HAVE_PUBLIC_FFDHE
  17142. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  17143. #else
  17144. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  17145. #endif
  17146. if (ret != 0)
  17147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17148. #endif
  17149. #ifdef HAVE_FFDHE_3072
  17150. #ifdef HAVE_PUBLIC_FFDHE
  17151. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  17152. #else
  17153. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  17154. #endif
  17155. if (ret != 0)
  17156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17157. #endif
  17158. #ifdef HAVE_FFDHE_4096
  17159. #ifdef HAVE_PUBLIC_FFDHE
  17160. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  17161. #else
  17162. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  17163. #endif
  17164. if (ret != 0)
  17165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17166. #endif
  17167. #endif /* !WC_NO_RNG */
  17168. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  17169. wc_FreeDhKey(key);
  17170. keyInit = 0;
  17171. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  17172. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  17173. /* Test Check Key */
  17174. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  17175. NULL, 0, 0, &rng);
  17176. if (ret != 0)
  17177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17178. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  17179. #endif
  17180. done:
  17181. #ifndef WC_NO_RNG
  17182. if (rngInit)
  17183. wc_FreeRng(&rng);
  17184. #endif
  17185. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17186. if (key) {
  17187. if (keyInit)
  17188. wc_FreeDhKey(key);
  17189. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17190. }
  17191. if (key2) {
  17192. if (key2Init)
  17193. wc_FreeDhKey(key2);
  17194. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17195. }
  17196. if (tmp)
  17197. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17198. if (priv)
  17199. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17200. if (pub)
  17201. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17202. if (priv2)
  17203. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17204. if (pub2)
  17205. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17206. if (agree)
  17207. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17208. if (agree2)
  17209. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17210. #else
  17211. if (keyInit)
  17212. wc_FreeDhKey(key);
  17213. if (key2Init)
  17214. wc_FreeDhKey(key2);
  17215. #endif
  17216. (void)privSz;
  17217. (void)pubSz;
  17218. (void)pubSz2;
  17219. (void)privSz2;
  17220. return ret;
  17221. #undef DH_TEST_BUF_SIZE
  17222. #undef DH_TEST_TMP_SIZE
  17223. }
  17224. #endif /* NO_DH */
  17225. #ifndef NO_DSA
  17226. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  17227. {
  17228. wc_test_ret_t ret = 0;
  17229. int answer;
  17230. word32 bytes;
  17231. word32 idx = 0;
  17232. WC_RNG rng;
  17233. wc_Sha sha;
  17234. byte hash[WC_SHA_DIGEST_SIZE];
  17235. byte signature[40];
  17236. #ifdef WOLFSSL_KEY_GEN
  17237. byte* der = 0;
  17238. #endif
  17239. #define DSA_TEST_TMP_SIZE 1024
  17240. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17241. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17242. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17243. #ifdef WOLFSSL_KEY_GEN
  17244. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17245. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17246. #endif
  17247. if ((tmp == NULL) ||
  17248. (key == NULL)
  17249. #ifdef WOLFSSL_KEY_GEN
  17250. || (derIn == NULL)
  17251. || (genKey == NULL)
  17252. #endif
  17253. ) {
  17254. ret = WC_TEST_RET_ENC_NC;
  17255. goto out;
  17256. }
  17257. #else
  17258. byte tmp[1024];
  17259. DsaKey key[1];
  17260. #ifdef WOLFSSL_KEY_GEN
  17261. DsaKey derIn[1];
  17262. DsaKey genKey[1];
  17263. #endif
  17264. #endif
  17265. #ifdef USE_CERT_BUFFERS_1024
  17266. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17267. bytes = sizeof_dsa_key_der_1024;
  17268. #elif defined(USE_CERT_BUFFERS_2048)
  17269. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17270. bytes = sizeof_dsa_key_der_2048;
  17271. #else
  17272. {
  17273. XFILE file = XFOPEN(dsaKey, "rb");
  17274. if (!file)
  17275. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  17276. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  17277. XFCLOSE(file);
  17278. if (bytes == 0)
  17279. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  17280. }
  17281. #endif /* USE_CERT_BUFFERS */
  17282. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  17283. if (ret != 0)
  17284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17285. wc_ShaUpdate(&sha, tmp, bytes);
  17286. wc_ShaFinal(&sha, hash);
  17287. wc_ShaFree(&sha);
  17288. ret = wc_InitDsaKey(key);
  17289. if (ret != 0)
  17290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17291. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  17292. if (ret != 0)
  17293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17294. #ifndef HAVE_FIPS
  17295. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17296. #else
  17297. ret = wc_InitRng(&rng);
  17298. #endif
  17299. if (ret != 0)
  17300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17301. ret = wc_DsaSign(hash, signature, key, &rng);
  17302. if (ret != 0)
  17303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17304. ret = wc_DsaVerify(hash, signature, key, &answer);
  17305. if (ret != 0)
  17306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17307. if (answer != 1)
  17308. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  17309. wc_FreeDsaKey(key);
  17310. #ifdef WOLFSSL_KEY_GEN
  17311. {
  17312. int derSz = 0;
  17313. ret = wc_InitDsaKey(genKey);
  17314. if (ret != 0)
  17315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17316. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  17317. if (ret != 0) {
  17318. wc_FreeDsaKey(genKey);
  17319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17320. }
  17321. ret = wc_MakeDsaKey(&rng, genKey);
  17322. if (ret != 0) {
  17323. wc_FreeDsaKey(genKey);
  17324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17325. }
  17326. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17327. if (der == NULL) {
  17328. wc_FreeDsaKey(genKey);
  17329. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  17330. }
  17331. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  17332. if (derSz < 0) {
  17333. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17334. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  17335. }
  17336. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  17337. DSA_PRIVATEKEY_TYPE);
  17338. if (ret != 0) {
  17339. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17340. wc_FreeDsaKey(genKey);
  17341. goto out;
  17342. }
  17343. ret = wc_InitDsaKey(derIn);
  17344. if (ret != 0) {
  17345. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17346. wc_FreeDsaKey(genKey);
  17347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17348. }
  17349. idx = 0;
  17350. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  17351. if (ret != 0) {
  17352. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17353. wc_FreeDsaKey(derIn);
  17354. wc_FreeDsaKey(genKey);
  17355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17356. }
  17357. }
  17358. #endif /* WOLFSSL_KEY_GEN */
  17359. out:
  17360. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17361. if (key) {
  17362. #endif
  17363. ret = wc_InitDsaKey_h(key, NULL);
  17364. if (ret != 0)
  17365. ret = WC_TEST_RET_ENC_EC(ret);
  17366. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17367. }
  17368. #endif
  17369. #ifdef WOLFSSL_KEY_GEN
  17370. if (der)
  17371. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17372. #endif
  17373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17374. if (tmp)
  17375. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17376. if (key)
  17377. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17378. #ifdef WOLFSSL_KEY_GEN
  17379. if (derIn) {
  17380. wc_FreeDsaKey(derIn);
  17381. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17382. }
  17383. if (genKey) {
  17384. wc_FreeDsaKey(genKey);
  17385. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17386. }
  17387. #endif
  17388. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  17389. #ifdef WOLFSSL_KEY_GEN
  17390. wc_FreeDsaKey(derIn);
  17391. wc_FreeDsaKey(genKey);
  17392. #endif
  17393. #endif
  17394. wc_FreeRng(&rng);
  17395. return ret;
  17396. }
  17397. #endif /* NO_DSA */
  17398. #ifdef WOLFCRYPT_HAVE_SRP
  17399. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  17400. {
  17401. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  17402. WC_RNG rng;
  17403. if(NULL == buf || !size)
  17404. return WC_TEST_RET_ENC_NC;
  17405. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  17406. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  17407. wc_FreeRng(&rng);
  17408. }
  17409. return ret;
  17410. }
  17411. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  17412. {
  17413. wc_test_ret_t r;
  17414. byte clientPubKey[192]; /* A */
  17415. byte serverPubKey[192]; /* B */
  17416. word32 clientPubKeySz = 192;
  17417. word32 serverPubKeySz = 192;
  17418. byte username[] = "user";
  17419. word32 usernameSz = 4;
  17420. byte password[] = "password";
  17421. word32 passwordSz = 8;
  17422. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  17423. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  17424. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  17425. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  17426. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  17427. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  17428. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  17429. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  17430. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  17431. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  17432. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  17433. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  17434. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  17435. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  17436. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  17437. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  17438. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  17439. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  17440. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  17441. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  17442. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  17443. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  17444. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  17445. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  17446. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  17447. };
  17448. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  17449. 0x02
  17450. };
  17451. byte salt[10];
  17452. byte verifier[192];
  17453. word32 v_size = sizeof(verifier);
  17454. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  17455. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  17456. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17457. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17458. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17459. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  17460. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  17461. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  17462. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  17463. if ((cli == NULL) ||
  17464. (srv == NULL) ||
  17465. (clientProof == NULL) ||
  17466. (serverProof == NULL)) {
  17467. r = WC_TEST_RET_ENC_NC;
  17468. goto out;
  17469. }
  17470. #else
  17471. Srp cli[1], srv[1];
  17472. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  17473. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  17474. #endif
  17475. /* set as 0's so if second init on srv not called SrpTerm is not on
  17476. * garbage values */
  17477. XMEMSET(srv, 0, sizeof *srv);
  17478. XMEMSET(cli, 0, sizeof *cli);
  17479. /* generating random salt */
  17480. r = generate_random_salt(salt, sizeof(salt));
  17481. /* client knows username and password. */
  17482. /* server knows N, g, salt and verifier. */
  17483. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  17484. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  17485. /* loading N, g and salt in advance to generate the verifier. */
  17486. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  17487. g, sizeof(g),
  17488. salt, sizeof(salt));
  17489. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  17490. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  17491. /* client sends username to server */
  17492. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  17493. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  17494. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  17495. g, sizeof(g),
  17496. salt, sizeof(salt));
  17497. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  17498. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  17499. /* server sends N, g, salt and B to client */
  17500. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  17501. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  17502. serverPubKey, serverPubKeySz);
  17503. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  17504. /* client sends A and M1 to server */
  17505. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  17506. serverPubKey, serverPubKeySz);
  17507. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  17508. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  17509. /* server sends M2 to client */
  17510. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  17511. wc_SrpTerm(cli);
  17512. wc_SrpTerm(srv);
  17513. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17514. out:
  17515. if (cli)
  17516. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17517. if (srv)
  17518. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17519. if (clientProof)
  17520. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17521. if (serverProof)
  17522. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17523. #endif
  17524. return r;
  17525. }
  17526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  17527. {
  17528. wc_test_ret_t ret;
  17529. #ifndef NO_SHA
  17530. ret = srp_test_digest(SRP_TYPE_SHA);
  17531. if (ret != 0)
  17532. return ret;
  17533. #endif
  17534. #ifndef NO_SHA256
  17535. ret = srp_test_digest(SRP_TYPE_SHA256);
  17536. if (ret != 0)
  17537. return ret;
  17538. #endif
  17539. #ifdef WOLFSSL_SHA384
  17540. ret = srp_test_digest(SRP_TYPE_SHA384);
  17541. if (ret != 0)
  17542. return ret;
  17543. #endif
  17544. #ifdef WOLFSSL_SHA512
  17545. ret = srp_test_digest(SRP_TYPE_SHA512);
  17546. if (ret != 0)
  17547. return ret;
  17548. #endif
  17549. return ret;
  17550. }
  17551. #endif /* WOLFCRYPT_HAVE_SRP */
  17552. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  17553. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17554. static wc_test_ret_t openssl_aes_test(void)
  17555. {
  17556. #ifdef HAVE_AES_CBC
  17557. #ifdef WOLFSSL_AES_128
  17558. {
  17559. /* EVP_CipherUpdate test */
  17560. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17561. {
  17562. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17563. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17564. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17565. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17566. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17567. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17568. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17569. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17570. };
  17571. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17572. "0123456789abcdef "; /* align */
  17573. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17574. "1234567890abcdef "; /* align */
  17575. byte cipher[AES_BLOCK_SIZE * 4];
  17576. byte plain [AES_BLOCK_SIZE * 4];
  17577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17578. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17579. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17580. #else
  17581. EVP_CIPHER_CTX en[1];
  17582. EVP_CIPHER_CTX de[1];
  17583. #endif
  17584. int outlen ;
  17585. int total = 0;
  17586. int i;
  17587. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17588. if ((en == NULL) || (de == NULL))
  17589. return MEMORY_E;
  17590. #endif
  17591. EVP_CIPHER_CTX_init(en);
  17592. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17593. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17594. return WC_TEST_RET_ENC_NC;
  17595. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17596. (byte*)cbcPlain, 9) == 0)
  17597. return WC_TEST_RET_ENC_NC;
  17598. if (outlen != 0)
  17599. return WC_TEST_RET_ENC_NC;
  17600. total += outlen;
  17601. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17602. (byte*)&cbcPlain[9] , 9) == 0)
  17603. return WC_TEST_RET_ENC_NC;
  17604. if (outlen != 16)
  17605. return WC_TEST_RET_ENC_NC;
  17606. total += outlen;
  17607. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17608. return WC_TEST_RET_ENC_NC;
  17609. if (outlen != 16)
  17610. return WC_TEST_RET_ENC_NC;
  17611. total += outlen;
  17612. if (total != 32)
  17613. return 3408;
  17614. total = 0;
  17615. EVP_CIPHER_CTX_init(de);
  17616. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17617. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17618. return WC_TEST_RET_ENC_NC;
  17619. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17620. return WC_TEST_RET_ENC_NC;
  17621. if (outlen != 0)
  17622. return WC_TEST_RET_ENC_NC;
  17623. total += outlen;
  17624. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17625. (byte*)&cipher[6], 12) == 0)
  17626. return WC_TEST_RET_ENC_NC;
  17627. if (outlen != 0)
  17628. total += outlen;
  17629. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17630. (byte*)&cipher[6+12], 14) == 0)
  17631. return WC_TEST_RET_ENC_NC;
  17632. if (outlen != 16)
  17633. return WC_TEST_RET_ENC_NC;
  17634. total += outlen;
  17635. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17636. return WC_TEST_RET_ENC_NC;
  17637. if (outlen != 2)
  17638. return WC_TEST_RET_ENC_NC;
  17639. total += outlen;
  17640. if (total != 18)
  17641. return 3427;
  17642. if (XMEMCMP(plain, cbcPlain, 18))
  17643. return WC_TEST_RET_ENC_NC;
  17644. /* test with encrypting/decrypting more than 16 bytes at once */
  17645. total = 0;
  17646. EVP_CIPHER_CTX_init(en);
  17647. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17648. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17649. return WC_TEST_RET_ENC_NC;
  17650. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17651. (byte*)cbcPlain, 17) == 0)
  17652. return WC_TEST_RET_ENC_NC;
  17653. if (outlen != 16)
  17654. return WC_TEST_RET_ENC_NC;
  17655. total += outlen;
  17656. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17657. (byte*)&cbcPlain[17] , 1) == 0)
  17658. return WC_TEST_RET_ENC_NC;
  17659. if (outlen != 0)
  17660. return WC_TEST_RET_ENC_NC;
  17661. total += outlen;
  17662. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17663. return WC_TEST_RET_ENC_NC;
  17664. if (outlen != 16)
  17665. return WC_TEST_RET_ENC_NC;
  17666. total += outlen;
  17667. if (total != 32)
  17668. return WC_TEST_RET_ENC_NC;
  17669. total = 0;
  17670. EVP_CIPHER_CTX_init(de);
  17671. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17672. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17673. return WC_TEST_RET_ENC_NC;
  17674. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  17675. return WC_TEST_RET_ENC_NC;
  17676. if (outlen != 16)
  17677. return WC_TEST_RET_ENC_NC;
  17678. total += outlen;
  17679. /* final call on non block size should fail */
  17680. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  17681. return WC_TEST_RET_ENC_NC;
  17682. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17683. (byte*)&cipher[17], 1) == 0)
  17684. return WC_TEST_RET_ENC_NC;
  17685. if (outlen != 0)
  17686. total += outlen;
  17687. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17688. (byte*)&cipher[17+1], 14) == 0)
  17689. return WC_TEST_RET_ENC_NC;
  17690. if (outlen != 0)
  17691. return WC_TEST_RET_ENC_NC;
  17692. total += outlen;
  17693. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17694. return WC_TEST_RET_ENC_NC;
  17695. if (outlen != 2)
  17696. return WC_TEST_RET_ENC_NC;
  17697. total += outlen;
  17698. if (total != 18)
  17699. return WC_TEST_RET_ENC_NC;
  17700. if (XMEMCMP(plain, cbcPlain, 18))
  17701. return WC_TEST_RET_ENC_NC;
  17702. /* test byte by byte decrypt */
  17703. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  17704. plain[i] = i;
  17705. }
  17706. total = 0;
  17707. EVP_CIPHER_CTX_init(en);
  17708. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17709. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17710. return WC_TEST_RET_ENC_NC;
  17711. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17712. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  17713. return WC_TEST_RET_ENC_NC;
  17714. if (outlen != AES_BLOCK_SIZE * 3)
  17715. return WC_TEST_RET_ENC_NC;
  17716. total += outlen;
  17717. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17718. return WC_TEST_RET_ENC_NC;
  17719. if (outlen != AES_BLOCK_SIZE)
  17720. return WC_TEST_RET_ENC_NC;
  17721. total += outlen;
  17722. if (total != sizeof(plain))
  17723. return WC_TEST_RET_ENC_NC;
  17724. total = 0;
  17725. EVP_CIPHER_CTX_init(de);
  17726. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17727. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17728. return WC_TEST_RET_ENC_NC;
  17729. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  17730. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  17731. (byte*)cipher + i, 1) == 0)
  17732. return WC_TEST_RET_ENC_NC;
  17733. if (outlen > 0) {
  17734. int j;
  17735. total += outlen;
  17736. for (j = 0; j < total; j++) {
  17737. if (plain[j] != j) {
  17738. return WC_TEST_RET_ENC_NC;
  17739. }
  17740. }
  17741. }
  17742. }
  17743. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17744. return WC_TEST_RET_ENC_NC;
  17745. total += outlen;
  17746. if (total != AES_BLOCK_SIZE * 3) {
  17747. return WC_TEST_RET_ENC_NC;
  17748. }
  17749. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  17750. if (plain[i] != i) {
  17751. return WC_TEST_RET_ENC_NC;
  17752. }
  17753. }
  17754. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17755. wolfSSL_EVP_CIPHER_CTX_free(en);
  17756. wolfSSL_EVP_CIPHER_CTX_free(de);
  17757. #endif
  17758. }
  17759. /* set buffers to be exact size to catch potential over read/write */
  17760. {
  17761. /* EVP_CipherUpdate test */
  17762. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17763. {
  17764. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17765. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17766. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17767. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17768. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17769. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17770. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17771. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17772. };
  17773. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17774. "0123456789abcdef "; /* align */
  17775. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17776. "1234567890abcdef "; /* align */
  17777. #define EVP_TEST_BUF_SZ 18
  17778. #define EVP_TEST_BUF_PAD 32
  17779. byte cipher[EVP_TEST_BUF_SZ];
  17780. byte plain [EVP_TEST_BUF_SZ];
  17781. byte padded[EVP_TEST_BUF_PAD];
  17782. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17783. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17784. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17785. #else
  17786. EVP_CIPHER_CTX en[1];
  17787. EVP_CIPHER_CTX de[1];
  17788. #endif
  17789. int outlen ;
  17790. int total = 0;
  17791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17792. if ((en == NULL) || (de == NULL))
  17793. return MEMORY_E;
  17794. #endif
  17795. EVP_CIPHER_CTX_init(en);
  17796. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17797. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17798. return WC_TEST_RET_ENC_NC;
  17799. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  17800. return WC_TEST_RET_ENC_NC;
  17801. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17802. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  17803. return WC_TEST_RET_ENC_NC;
  17804. if (outlen != 16)
  17805. return WC_TEST_RET_ENC_NC;
  17806. total += outlen;
  17807. /* should fail here */
  17808. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  17809. return WC_TEST_RET_ENC_NC;
  17810. /* turn padding back on and do successful encrypt */
  17811. total = 0;
  17812. EVP_CIPHER_CTX_init(en);
  17813. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17814. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17815. return WC_TEST_RET_ENC_NC;
  17816. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  17817. return WC_TEST_RET_ENC_NC;
  17818. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  17819. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  17820. return WC_TEST_RET_ENC_NC;
  17821. if (outlen != 16)
  17822. return WC_TEST_RET_ENC_NC;
  17823. total += outlen;
  17824. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  17825. return WC_TEST_RET_ENC_NC;
  17826. total += outlen;
  17827. if (total != 32)
  17828. return WC_TEST_RET_ENC_NC;
  17829. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  17830. /* test out of bounds read on buffers w/o padding during decryption */
  17831. total = 0;
  17832. EVP_CIPHER_CTX_init(de);
  17833. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17834. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17835. return WC_TEST_RET_ENC_NC;
  17836. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  17837. return WC_TEST_RET_ENC_NC;
  17838. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  17839. EVP_TEST_BUF_SZ) == 0)
  17840. return WC_TEST_RET_ENC_NC;
  17841. if (outlen != 16)
  17842. return WC_TEST_RET_ENC_NC;
  17843. total += outlen;
  17844. /* should fail since not using padding */
  17845. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  17846. return WC_TEST_RET_ENC_NC;
  17847. total = 0;
  17848. EVP_CIPHER_CTX_init(de);
  17849. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17850. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17851. return WC_TEST_RET_ENC_NC;
  17852. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  17853. return WC_TEST_RET_ENC_NC;
  17854. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  17855. EVP_TEST_BUF_PAD) == 0)
  17856. return WC_TEST_RET_ENC_NC;
  17857. if (outlen != 16)
  17858. return WC_TEST_RET_ENC_NC;
  17859. total += outlen;
  17860. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  17861. return WC_TEST_RET_ENC_NC;
  17862. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  17863. return WC_TEST_RET_ENC_NC;
  17864. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17865. wolfSSL_EVP_CIPHER_CTX_free(en);
  17866. wolfSSL_EVP_CIPHER_CTX_free(de);
  17867. #endif
  17868. }
  17869. { /* evp_cipher test: EVP_aes_128_cbc */
  17870. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17871. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17872. #else
  17873. EVP_CIPHER_CTX ctx[1];
  17874. #endif
  17875. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17876. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17877. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17878. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17879. };
  17880. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17881. {
  17882. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17883. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  17884. };
  17885. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17886. "0123456789abcdef "; /* align */
  17887. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17888. "1234567890abcdef "; /* align */
  17889. byte cipher[AES_BLOCK_SIZE * 4];
  17890. byte plain [AES_BLOCK_SIZE * 4];
  17891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17892. if (ctx == NULL)
  17893. return MEMORY_E;
  17894. #endif
  17895. EVP_CIPHER_CTX_init(ctx);
  17896. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  17897. return WC_TEST_RET_ENC_NC;
  17898. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  17899. return WC_TEST_RET_ENC_NC;
  17900. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17901. return WC_TEST_RET_ENC_NC;
  17902. EVP_CIPHER_CTX_init(ctx);
  17903. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  17904. return WC_TEST_RET_ENC_NC;
  17905. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  17906. return WC_TEST_RET_ENC_NC;
  17907. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17908. return WC_TEST_RET_ENC_NC;
  17909. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17910. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17911. #endif
  17912. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17913. #endif /* WOLFSSL_AES_128 */
  17914. #endif /* HAVE_AES_CBC */
  17915. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17916. { /* evp_cipher test: EVP_aes_256_ecb*/
  17917. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17918. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17919. #else
  17920. EVP_CIPHER_CTX ctx[1];
  17921. #endif
  17922. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17923. {
  17924. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17925. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17926. };
  17927. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17928. {
  17929. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17930. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17931. };
  17932. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17933. {
  17934. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17935. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17936. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17937. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17938. };
  17939. byte cipher[AES_BLOCK_SIZE * 4];
  17940. byte plain [AES_BLOCK_SIZE * 4];
  17941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17942. if (ctx == NULL)
  17943. return MEMORY_E;
  17944. #endif
  17945. EVP_CIPHER_CTX_init(ctx);
  17946. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  17947. return WC_TEST_RET_ENC_NC;
  17948. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  17949. return WC_TEST_RET_ENC_NC;
  17950. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17951. return WC_TEST_RET_ENC_NC;
  17952. EVP_CIPHER_CTX_init(ctx);
  17953. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  17954. return WC_TEST_RET_ENC_NC;
  17955. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  17956. return WC_TEST_RET_ENC_NC;
  17957. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17958. return WC_TEST_RET_ENC_NC;
  17959. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17960. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17961. #endif
  17962. } /* end evp_cipher test */
  17963. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  17964. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17965. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17966. {
  17967. /* Test: AES_encrypt/decrypt/set Key */
  17968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17969. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17970. #ifdef HAVE_AES_DECRYPT
  17971. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17972. #endif
  17973. #else
  17974. AES_KEY enc[1];
  17975. #ifdef HAVE_AES_DECRYPT
  17976. AES_KEY dec[1];
  17977. #endif
  17978. #endif
  17979. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17980. {
  17981. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17982. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17983. };
  17984. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17985. {
  17986. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17987. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17988. };
  17989. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17990. {
  17991. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17992. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17993. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17994. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17995. };
  17996. byte plain[sizeof(msg)];
  17997. byte cipher[sizeof(msg)];
  17998. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17999. if (enc == NULL)
  18000. return MEMORY_E;
  18001. #ifdef HAVE_AES_DECRYPT
  18002. if (dec == NULL)
  18003. return MEMORY_E;
  18004. #endif
  18005. #endif
  18006. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  18007. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  18008. AES_encrypt(msg, cipher, enc);
  18009. #ifdef HAVE_AES_DECRYPT
  18010. AES_decrypt(cipher, plain, dec);
  18011. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18012. return WC_TEST_RET_ENC_NC;
  18013. #endif /* HAVE_AES_DECRYPT */
  18014. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18015. return WC_TEST_RET_ENC_NC;
  18016. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18017. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18018. #ifdef HAVE_AES_DECRYPT
  18019. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18020. #endif
  18021. #endif
  18022. }
  18023. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  18024. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  18025. #ifdef WOLFSSL_AES_COUNTER
  18026. {
  18027. byte plainBuff [64];
  18028. byte cipherBuff[64];
  18029. #ifdef WOLFSSL_AES_128
  18030. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  18031. {
  18032. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18033. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18034. };
  18035. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  18036. {
  18037. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18038. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18039. };
  18040. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  18041. {
  18042. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18043. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18044. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18045. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18046. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18047. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18048. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18049. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18050. };
  18051. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  18052. {
  18053. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  18054. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  18055. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  18056. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  18057. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  18058. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  18059. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  18060. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  18061. };
  18062. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  18063. {
  18064. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  18065. 0xc2
  18066. };
  18067. #endif
  18068. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18069. * NIST Special Publication 800-38A */
  18070. #ifdef WOLFSSL_AES_192
  18071. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  18072. {
  18073. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  18074. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  18075. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  18076. };
  18077. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  18078. {
  18079. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18080. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18081. };
  18082. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  18083. {
  18084. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18085. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18086. };
  18087. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  18088. {
  18089. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  18090. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  18091. };
  18092. #endif /* WOLFSSL_AES_192 */
  18093. #ifdef WOLFSSL_AES_256
  18094. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18095. * NIST Special Publication 800-38A */
  18096. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  18097. {
  18098. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18099. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18100. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18101. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18102. };
  18103. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  18104. {
  18105. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18106. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18107. };
  18108. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  18109. {
  18110. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18111. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18112. };
  18113. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  18114. {
  18115. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  18116. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  18117. };
  18118. #endif /* WOLFSSL_AES_256 */
  18119. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18120. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  18121. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  18122. #else
  18123. EVP_CIPHER_CTX en[1];
  18124. EVP_CIPHER_CTX de[1];
  18125. #endif
  18126. #ifdef WOLFSSL_AES_128
  18127. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18128. EVP_CIPHER_CTX *p_en;
  18129. EVP_CIPHER_CTX *p_de;
  18130. #endif
  18131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18132. if ((en == NULL) || (de == NULL))
  18133. return MEMORY_E;
  18134. #endif
  18135. EVP_CIPHER_CTX_init(en);
  18136. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  18137. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18138. return WC_TEST_RET_ENC_NC;
  18139. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  18140. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18141. return WC_TEST_RET_ENC_NC;
  18142. EVP_CIPHER_CTX_init(de);
  18143. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  18144. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18145. return WC_TEST_RET_ENC_NC;
  18146. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18147. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18148. return WC_TEST_RET_ENC_NC;
  18149. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  18150. return WC_TEST_RET_ENC_NC;
  18151. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  18152. return WC_TEST_RET_ENC_NC;
  18153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18154. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  18155. if (p_en == NULL)
  18156. return WC_TEST_RET_ENC_ERRNO;
  18157. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  18158. if (p_de == NULL)
  18159. return WC_TEST_RET_ENC_ERRNO;
  18160. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  18161. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18162. return WC_TEST_RET_ENC_NC;
  18163. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  18164. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18165. return WC_TEST_RET_ENC_NC;
  18166. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  18167. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18168. return WC_TEST_RET_ENC_NC;
  18169. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  18170. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18171. return WC_TEST_RET_ENC_NC;
  18172. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  18173. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  18174. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18175. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  18176. return WC_TEST_RET_ENC_NC;
  18177. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  18178. return WC_TEST_RET_ENC_NC;
  18179. EVP_CIPHER_CTX_init(en);
  18180. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  18181. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18182. return WC_TEST_RET_ENC_NC;
  18183. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  18184. return WC_TEST_RET_ENC_NC;
  18185. EVP_CIPHER_CTX_init(de);
  18186. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  18187. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18188. return WC_TEST_RET_ENC_NC;
  18189. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  18190. return WC_TEST_RET_ENC_NC;
  18191. if (XMEMCMP(plainBuff, ctrPlain, 9))
  18192. return WC_TEST_RET_ENC_NC;
  18193. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  18194. return WC_TEST_RET_ENC_NC;
  18195. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  18196. return WC_TEST_RET_ENC_NC;
  18197. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  18198. return WC_TEST_RET_ENC_NC;
  18199. if (XMEMCMP(plainBuff, ctrPlain, 9))
  18200. return WC_TEST_RET_ENC_NC;
  18201. if (XMEMCMP(cipherBuff, oddCipher, 9))
  18202. return WC_TEST_RET_ENC_NC;
  18203. #endif /* WOLFSSL_AES_128 */
  18204. #ifdef WOLFSSL_AES_192
  18205. EVP_CIPHER_CTX_init(en);
  18206. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  18207. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  18208. return WC_TEST_RET_ENC_NC;
  18209. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  18210. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18211. return WC_TEST_RET_ENC_NC;
  18212. EVP_CIPHER_CTX_init(de);
  18213. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  18214. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  18215. return WC_TEST_RET_ENC_NC;
  18216. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  18217. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18218. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18219. return WC_TEST_RET_ENC_NC;
  18220. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  18221. return WC_TEST_RET_ENC_NC;
  18222. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  18223. return WC_TEST_RET_ENC_NC;
  18224. #endif /* WOLFSSL_AES_192 */
  18225. #ifdef WOLFSSL_AES_256
  18226. EVP_CIPHER_CTX_init(en);
  18227. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  18228. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  18229. return WC_TEST_RET_ENC_NC;
  18230. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  18231. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18232. return WC_TEST_RET_ENC_NC;
  18233. EVP_CIPHER_CTX_init(de);
  18234. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  18235. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  18236. return WC_TEST_RET_ENC_NC;
  18237. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  18238. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18239. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18240. return WC_TEST_RET_ENC_NC;
  18241. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  18242. return WC_TEST_RET_ENC_NC;
  18243. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  18244. return WC_TEST_RET_ENC_NC;
  18245. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18246. wolfSSL_EVP_CIPHER_CTX_free(en);
  18247. wolfSSL_EVP_CIPHER_CTX_free(de);
  18248. #endif
  18249. #endif /* WOLFSSL_AES_256 */
  18250. }
  18251. #endif /* HAVE_AES_COUNTER */
  18252. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  18253. {
  18254. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18255. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18256. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18257. #else
  18258. AES_KEY enc[1];
  18259. AES_KEY dec[1];
  18260. #endif
  18261. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  18262. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  18263. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  18264. };
  18265. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18266. {
  18267. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18268. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18269. };
  18270. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  18271. {
  18272. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  18273. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  18274. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  18275. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  18276. };
  18277. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18278. {
  18279. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18280. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18281. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18282. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  18283. };
  18284. byte cipher[AES_BLOCK_SIZE * 2];
  18285. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  18286. int num = 0;
  18287. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18288. if ((enc == NULL) || (dec == NULL))
  18289. return MEMORY_E;
  18290. #endif
  18291. XMEMCPY(iv, setIv, sizeof(setIv));
  18292. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  18293. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  18294. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  18295. &num, AES_ENCRYPT);
  18296. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  18297. return WC_TEST_RET_ENC_NC;
  18298. if (num != 15) /* should have used 15 of the 16 bytes */
  18299. return WC_TEST_RET_ENC_NC;
  18300. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  18301. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  18302. &num, AES_ENCRYPT);
  18303. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  18304. return WC_TEST_RET_ENC_NC;
  18305. if (num != 0)
  18306. return WC_TEST_RET_ENC_NC;
  18307. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18308. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18309. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18310. #endif
  18311. }
  18312. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  18313. return 0;
  18314. }
  18315. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  18316. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  18317. {
  18318. wc_test_ret_t ret;
  18319. EVP_MD_CTX md_ctx;
  18320. testVector a, b, c, d, e, f;
  18321. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  18322. a.inLen = 0;
  18323. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  18324. (void)a;
  18325. (void)b;
  18326. (void)c;
  18327. (void)d;
  18328. (void)e;
  18329. (void)f;
  18330. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  18331. {
  18332. byte* p;
  18333. p = (byte*)CRYPTO_malloc(10, "", 0);
  18334. if (p == NULL) {
  18335. return WC_TEST_RET_ENC_NC;
  18336. }
  18337. XMEMSET(p, 0, 10);
  18338. CRYPTO_free(p, "", 0);
  18339. }
  18340. #ifndef NO_MD5
  18341. a.input = "1234567890123456789012345678901234567890123456789012345678"
  18342. "9012345678901234567890";
  18343. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  18344. "\x7a";
  18345. a.inLen = XSTRLEN(a.input);
  18346. a.outLen = WC_MD5_DIGEST_SIZE;
  18347. EVP_MD_CTX_init(&md_ctx);
  18348. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  18349. if (ret == WOLFSSL_SUCCESS) {
  18350. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  18351. }
  18352. if (ret == WOLFSSL_SUCCESS) {
  18353. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18354. }
  18355. EVP_MD_CTX_cleanup(&md_ctx);
  18356. if (ret != WOLFSSL_SUCCESS)
  18357. return WC_TEST_RET_ENC_NC;
  18358. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  18359. return WC_TEST_RET_ENC_NC;
  18360. #endif /* NO_MD5 */
  18361. #ifndef NO_SHA
  18362. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  18363. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  18364. "aaaaaaaaaa";
  18365. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  18366. "\x53\x99\x5E\x26\xA0";
  18367. b.inLen = XSTRLEN(b.input);
  18368. b.outLen = WC_SHA_DIGEST_SIZE;
  18369. EVP_MD_CTX_init(&md_ctx);
  18370. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  18371. if (ret == WOLFSSL_SUCCESS) {
  18372. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  18373. if (ret == WOLFSSL_SUCCESS)
  18374. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18375. }
  18376. EVP_MD_CTX_cleanup(&md_ctx);
  18377. if (ret != WOLFSSL_SUCCESS)
  18378. return WC_TEST_RET_ENC_NC;
  18379. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  18380. return WC_TEST_RET_ENC_NC;
  18381. #endif /* NO_SHA */
  18382. #ifdef WOLFSSL_SHA224
  18383. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18384. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18385. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  18386. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  18387. e.inLen = XSTRLEN(e.input);
  18388. e.outLen = WC_SHA224_DIGEST_SIZE;
  18389. EVP_MD_CTX_init(&md_ctx);
  18390. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  18391. if (ret == WOLFSSL_SUCCESS) {
  18392. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18393. if (ret == WOLFSSL_SUCCESS)
  18394. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18395. }
  18396. EVP_MD_CTX_cleanup(&md_ctx);
  18397. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18398. return WC_TEST_RET_ENC_NC;
  18399. }
  18400. #endif /* WOLFSSL_SHA224 */
  18401. #ifndef NO_SHA256
  18402. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  18403. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  18404. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  18405. "\x06\xC1";
  18406. d.inLen = XSTRLEN(d.input);
  18407. d.outLen = WC_SHA256_DIGEST_SIZE;
  18408. EVP_MD_CTX_init(&md_ctx);
  18409. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  18410. if (ret == WOLFSSL_SUCCESS) {
  18411. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  18412. if (ret == WOLFSSL_SUCCESS)
  18413. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18414. }
  18415. EVP_MD_CTX_cleanup(&md_ctx);
  18416. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  18417. return WC_TEST_RET_ENC_NC;
  18418. }
  18419. #endif /* !NO_SHA256 */
  18420. #ifdef WOLFSSL_SHA384
  18421. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18422. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18423. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  18424. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  18425. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  18426. "\x74\x60\x39";
  18427. e.inLen = XSTRLEN(e.input);
  18428. e.outLen = WC_SHA384_DIGEST_SIZE;
  18429. EVP_MD_CTX_init(&md_ctx);
  18430. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  18431. if (ret == WOLFSSL_SUCCESS) {
  18432. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18433. if (ret == WOLFSSL_SUCCESS)
  18434. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18435. }
  18436. EVP_MD_CTX_cleanup(&md_ctx);
  18437. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18438. return WC_TEST_RET_ENC_NC;
  18439. }
  18440. #endif /* WOLFSSL_SHA384 */
  18441. #ifdef WOLFSSL_SHA512
  18442. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18443. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18444. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  18445. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  18446. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  18447. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  18448. "\x87\x4b\xe9\x09";
  18449. f.inLen = XSTRLEN(f.input);
  18450. f.outLen = WC_SHA512_DIGEST_SIZE;
  18451. EVP_MD_CTX_init(&md_ctx);
  18452. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  18453. if (ret == WOLFSSL_SUCCESS) {
  18454. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18455. if (ret == WOLFSSL_SUCCESS)
  18456. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18457. }
  18458. EVP_MD_CTX_cleanup(&md_ctx);
  18459. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  18460. return WC_TEST_RET_ENC_NC;
  18461. }
  18462. #if !defined(WOLFSSL_NOSHA512_224) && \
  18463. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18464. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18465. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18466. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  18467. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  18468. f.inLen = XSTRLEN(f.input);
  18469. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  18470. EVP_MD_CTX_init(&md_ctx);
  18471. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  18472. if (ret == WOLFSSL_SUCCESS) {
  18473. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18474. if (ret == WOLFSSL_SUCCESS)
  18475. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18476. }
  18477. EVP_MD_CTX_cleanup(&md_ctx);
  18478. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  18479. return WC_TEST_RET_ENC_NC;
  18480. }
  18481. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  18482. #if !defined(WOLFSSL_NOSHA512_256) && \
  18483. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18484. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18485. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18486. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  18487. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  18488. "\x56\x3a";
  18489. f.inLen = XSTRLEN(f.input);
  18490. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  18491. EVP_MD_CTX_init(&md_ctx);
  18492. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  18493. if (ret == WOLFSSL_SUCCESS) {
  18494. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18495. if (ret == WOLFSSL_SUCCESS)
  18496. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18497. }
  18498. EVP_MD_CTX_cleanup(&md_ctx);
  18499. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  18500. return WC_TEST_RET_ENC_NC;
  18501. }
  18502. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  18503. #endif /* WOLFSSL_SHA512 */
  18504. #ifdef WOLFSSL_SHA3
  18505. #ifndef WOLFSSL_NOSHA3_224
  18506. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18507. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18508. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  18509. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  18510. e.inLen = XSTRLEN(e.input);
  18511. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  18512. EVP_MD_CTX_init(&md_ctx);
  18513. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  18514. if (ret == WOLFSSL_SUCCESS) {
  18515. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18516. if (ret == WOLFSSL_SUCCESS)
  18517. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18518. }
  18519. EVP_MD_CTX_cleanup(&md_ctx);
  18520. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18521. return WC_TEST_RET_ENC_NC;
  18522. }
  18523. #endif /* WOLFSSL_NOSHA3_224 */
  18524. #ifndef WOLFSSL_NOSHA3_256
  18525. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18526. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18527. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  18528. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  18529. "\x1d\x18";
  18530. d.inLen = XSTRLEN(d.input);
  18531. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  18532. EVP_MD_CTX_init(&md_ctx);
  18533. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  18534. if (ret == WOLFSSL_SUCCESS) {
  18535. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  18536. if (ret == WOLFSSL_SUCCESS)
  18537. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18538. }
  18539. EVP_MD_CTX_cleanup(&md_ctx);
  18540. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  18541. return WC_TEST_RET_ENC_NC;
  18542. }
  18543. #endif /* WOLFSSL_NOSHA3_256 */
  18544. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18545. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18546. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  18547. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  18548. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  18549. "\xaa\x7f\xc7";
  18550. e.inLen = XSTRLEN(e.input);
  18551. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  18552. EVP_MD_CTX_init(&md_ctx);
  18553. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  18554. if (ret == WOLFSSL_SUCCESS) {
  18555. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18556. if (ret == WOLFSSL_SUCCESS)
  18557. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18558. }
  18559. EVP_MD_CTX_cleanup(&md_ctx);
  18560. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18561. return WC_TEST_RET_ENC_NC;
  18562. }
  18563. #ifndef WOLFSSL_NOSHA3_512
  18564. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18565. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18566. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  18567. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  18568. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  18569. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  18570. "\xf2\x1d\xd1\x85";
  18571. f.inLen = XSTRLEN(f.input);
  18572. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  18573. EVP_MD_CTX_init(&md_ctx);
  18574. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  18575. if (ret == WOLFSSL_SUCCESS) {
  18576. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18577. if (ret == WOLFSSL_SUCCESS)
  18578. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18579. }
  18580. EVP_MD_CTX_cleanup(&md_ctx);
  18581. if (ret != WOLFSSL_SUCCESS ||
  18582. XMEMCMP(hash, f.output, f.outLen) != 0) {
  18583. return WC_TEST_RET_ENC_NC;
  18584. }
  18585. #endif /* WOLFSSL_NOSHA3_512 */
  18586. #endif /* WOLFSSL_SHA3 */
  18587. #ifndef WC_NO_RNG
  18588. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  18589. return WC_TEST_RET_ENC_NC;
  18590. #endif
  18591. #ifndef NO_MD5
  18592. c.input = "what do ya want for nothing?";
  18593. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  18594. "\x76";
  18595. c.inLen = XSTRLEN(c.input);
  18596. c.outLen = WC_MD5_DIGEST_SIZE;
  18597. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  18598. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  18599. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  18600. hash, 0) != NULL)
  18601. #else
  18602. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  18603. hash, 0) == NULL ||
  18604. XMEMCMP(hash, c.output, c.outLen) != 0)
  18605. #endif
  18606. {
  18607. return WC_TEST_RET_ENC_NC;
  18608. }
  18609. #endif /* NO_MD5 */
  18610. #ifndef NO_DES3
  18611. { /* des test */
  18612. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  18613. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  18614. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  18615. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  18616. };
  18617. byte plain[24];
  18618. byte cipher[24];
  18619. const_DES_cblock key = {
  18620. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  18621. };
  18622. DES_cblock iv = {
  18623. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  18624. };
  18625. DES_key_schedule sched;
  18626. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18627. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  18628. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  18629. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  18630. };
  18631. DES_key_sched(&key, &sched);
  18632. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  18633. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  18634. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  18635. return WC_TEST_RET_ENC_NC;
  18636. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  18637. return WC_TEST_RET_ENC_NC;
  18638. /* test changing iv */
  18639. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  18640. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  18641. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  18642. return WC_TEST_RET_ENC_NC;
  18643. } /* end des test */
  18644. #endif /* NO_DES3 */
  18645. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  18646. if ((ret = openssl_aes_test()) != 0) {
  18647. return ret;
  18648. }
  18649. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  18650. { /* evp_cipher test: EVP_aes_128_cbc */
  18651. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18652. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18653. #else
  18654. EVP_CIPHER_CTX ctx[1];
  18655. #endif
  18656. int idx, cipherSz, plainSz;
  18657. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  18658. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  18659. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  18660. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  18661. };
  18662. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18663. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  18664. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  18665. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  18666. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  18667. };
  18668. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18669. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  18670. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  18671. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  18672. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  18673. };
  18674. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18675. "0123456789abcdef "; /* align */
  18676. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  18677. "1234567890abcdef "; /* align */
  18678. byte cipher[AES_BLOCK_SIZE * 4];
  18679. byte plain [AES_BLOCK_SIZE * 4];
  18680. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18681. if (ctx == NULL)
  18682. return MEMORY_E;
  18683. #endif
  18684. cipherSz = 0;
  18685. EVP_CIPHER_CTX_init(ctx);
  18686. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  18687. if (ret == WOLFSSL_SUCCESS) {
  18688. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  18689. if (ret == WOLFSSL_SUCCESS)
  18690. cipherSz += idx;
  18691. }
  18692. if (ret == WOLFSSL_SUCCESS) {
  18693. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  18694. if (ret == WOLFSSL_SUCCESS)
  18695. cipherSz += idx;
  18696. }
  18697. EVP_CIPHER_CTX_cleanup(ctx);
  18698. if (ret != WOLFSSL_SUCCESS)
  18699. return WC_TEST_RET_ENC_NC;
  18700. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  18701. return WC_TEST_RET_ENC_NC;
  18702. /* check partial decrypt (not enough padding for full block) */
  18703. plainSz = 0;
  18704. EVP_CIPHER_CTX_init(ctx);
  18705. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  18706. if (ret == WOLFSSL_SUCCESS) {
  18707. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  18708. if (ret == WOLFSSL_SUCCESS)
  18709. plainSz += idx;
  18710. }
  18711. if (ret == WOLFSSL_SUCCESS) {
  18712. /* this test should fail... not enough padding for full block */
  18713. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  18714. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  18715. ret = WOLFSSL_SUCCESS;
  18716. else
  18717. ret = WC_TEST_RET_ENC_NC;
  18718. }
  18719. else
  18720. ret = WC_TEST_RET_ENC_NC;
  18721. EVP_CIPHER_CTX_cleanup(ctx);
  18722. if (ret != WOLFSSL_SUCCESS)
  18723. return ret;
  18724. plainSz = 0;
  18725. EVP_CIPHER_CTX_init(ctx);
  18726. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  18727. if (ret == WOLFSSL_SUCCESS) {
  18728. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  18729. if (ret == WOLFSSL_SUCCESS)
  18730. plainSz += idx;
  18731. }
  18732. if (ret == WOLFSSL_SUCCESS) {
  18733. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  18734. if (ret == WOLFSSL_SUCCESS)
  18735. plainSz += idx;
  18736. }
  18737. EVP_CIPHER_CTX_cleanup(ctx);
  18738. if (ret != WOLFSSL_SUCCESS)
  18739. return WC_TEST_RET_ENC_NC;
  18740. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  18741. return WC_TEST_RET_ENC_NC;
  18742. cipherSz = 0;
  18743. EVP_CIPHER_CTX_init(ctx);
  18744. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  18745. if (ret == WOLFSSL_SUCCESS) {
  18746. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  18747. if (ret == WOLFSSL_SUCCESS)
  18748. cipherSz += idx;
  18749. }
  18750. if (ret == WOLFSSL_SUCCESS) {
  18751. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  18752. if (ret == WOLFSSL_SUCCESS)
  18753. cipherSz += idx;
  18754. }
  18755. EVP_CIPHER_CTX_cleanup(ctx);
  18756. if (ret != WOLFSSL_SUCCESS)
  18757. return WC_TEST_RET_ENC_NC;
  18758. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  18759. return WC_TEST_RET_ENC_NC;
  18760. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18761. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18762. #endif
  18763. } /* end evp_cipher test: EVP_aes_128_cbc*/
  18764. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  18765. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  18766. { /* evp_cipher test: EVP_aes_256_ecb*/
  18767. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18768. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18769. #else
  18770. EVP_CIPHER_CTX ctx[1];
  18771. #endif
  18772. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  18773. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18774. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18775. };
  18776. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18777. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18778. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18779. };
  18780. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  18781. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18782. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18783. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18784. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18785. };
  18786. byte cipher[AES_BLOCK_SIZE * 4];
  18787. byte plain [AES_BLOCK_SIZE * 4];
  18788. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18789. if (ctx == NULL)
  18790. return MEMORY_E;
  18791. #endif
  18792. EVP_CIPHER_CTX_init(ctx);
  18793. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  18794. if (ret == WOLFSSL_SUCCESS)
  18795. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  18796. EVP_CIPHER_CTX_cleanup(ctx);
  18797. if (ret != 16)
  18798. return WC_TEST_RET_ENC_NC;
  18799. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18800. return WC_TEST_RET_ENC_NC;
  18801. EVP_CIPHER_CTX_init(ctx);
  18802. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  18803. if (ret == WOLFSSL_SUCCESS)
  18804. ret = EVP_Cipher(ctx, plain, cipher, 16);
  18805. EVP_CIPHER_CTX_cleanup(ctx);
  18806. if (ret != 16)
  18807. return WC_TEST_RET_ENC_NC;
  18808. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18809. return WC_TEST_RET_ENC_NC;
  18810. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18811. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18812. #endif
  18813. } /* end evp_cipher test */
  18814. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  18815. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  18816. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  18817. {
  18818. /* Test: AES_encrypt/decrypt/set Key */
  18819. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18820. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18821. #ifdef HAVE_AES_DECRYPT
  18822. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18823. #endif
  18824. #else
  18825. AES_KEY enc[1];
  18826. #ifdef HAVE_AES_DECRYPT
  18827. AES_KEY dec[1];
  18828. #endif
  18829. #endif
  18830. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18831. {
  18832. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18833. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18834. };
  18835. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18836. {
  18837. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18838. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18839. };
  18840. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18841. {
  18842. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18843. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18844. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18845. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18846. };
  18847. byte plain[sizeof(msg)];
  18848. byte cipher[sizeof(msg)];
  18849. printf("openSSL extra test\n") ;
  18850. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18851. if (enc == NULL)
  18852. return MEMORY_E;
  18853. #ifdef HAVE_AES_DECRYPT
  18854. if (dec == NULL)
  18855. return MEMORY_E;
  18856. #endif
  18857. #endif
  18858. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  18859. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  18860. AES_encrypt(msg, cipher, enc);
  18861. #ifdef HAVE_AES_DECRYPT
  18862. AES_decrypt(cipher, plain, dec);
  18863. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18864. return WC_TEST_RET_ENC_NC;
  18865. #endif /* HAVE_AES_DECRYPT */
  18866. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18867. return WC_TEST_RET_ENC_NC;
  18868. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18869. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18870. #ifdef HAVE_AES_DECRYPT
  18871. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18872. #endif
  18873. #endif
  18874. }
  18875. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  18876. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  18877. #ifdef WOLFSSL_AES_COUNTER
  18878. {
  18879. byte plainBuff [64];
  18880. byte cipherBuff[64];
  18881. #ifdef WOLFSSL_AES_128
  18882. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  18883. {
  18884. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18885. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18886. };
  18887. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  18888. {
  18889. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18890. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18891. };
  18892. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  18893. {
  18894. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18895. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18896. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18897. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18898. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18899. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18900. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18901. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18902. };
  18903. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  18904. {
  18905. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  18906. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  18907. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  18908. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  18909. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  18910. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  18911. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  18912. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  18913. };
  18914. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  18915. {
  18916. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  18917. 0xc2
  18918. };
  18919. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18920. EVP_CIPHER_CTX *p_en;
  18921. EVP_CIPHER_CTX *p_de;
  18922. #endif
  18923. #endif /* WOLFSSL_AES_128 */
  18924. #ifdef WOLFSSL_AES_192
  18925. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18926. * NIST Special Publication 800-38A */
  18927. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  18928. {
  18929. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  18930. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  18931. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  18932. };
  18933. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  18934. {
  18935. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18936. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18937. };
  18938. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  18939. {
  18940. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18941. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18942. };
  18943. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  18944. {
  18945. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  18946. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  18947. };
  18948. #endif /* WOLFSSL_AES_192 */
  18949. #ifdef WOLFSSL_AES_256
  18950. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18951. * NIST Special Publication 800-38A */
  18952. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  18953. {
  18954. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18955. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18956. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18957. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18958. };
  18959. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  18960. {
  18961. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18962. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18963. };
  18964. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  18965. {
  18966. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18967. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18968. };
  18969. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  18970. {
  18971. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  18972. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  18973. };
  18974. #endif /* WOLFSSL_AES_256 */
  18975. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18976. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  18977. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  18978. if ((en == NULL) || (de == NULL))
  18979. return MEMORY_E;
  18980. #else
  18981. EVP_CIPHER_CTX en[1];
  18982. EVP_CIPHER_CTX de[1];
  18983. #endif
  18984. #ifdef WOLFSSL_AES_128
  18985. EVP_CIPHER_CTX_init(en);
  18986. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  18987. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18988. return WC_TEST_RET_ENC_NC;
  18989. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  18990. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18991. return WC_TEST_RET_ENC_NC;
  18992. EVP_CIPHER_CTX_init(de);
  18993. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  18994. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18995. return WC_TEST_RET_ENC_NC;
  18996. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18997. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18998. return WC_TEST_RET_ENC_NC;
  18999. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19000. return WC_TEST_RET_ENC_NC;
  19001. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19002. return WC_TEST_RET_ENC_NC;
  19003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19004. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  19005. if (p_en == NULL)
  19006. return WC_TEST_RET_ENC_ERRNO;
  19007. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  19008. if (p_de == NULL)
  19009. return WC_TEST_RET_ENC_ERRNO;
  19010. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  19011. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19012. return WC_TEST_RET_ENC_NC;
  19013. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  19014. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19015. return WC_TEST_RET_ENC_NC;
  19016. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  19017. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19018. return WC_TEST_RET_ENC_NC;
  19019. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  19020. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19021. return WC_TEST_RET_ENC_NC;
  19022. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  19023. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  19024. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19025. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19026. return WC_TEST_RET_ENC_NC;
  19027. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19028. return WC_TEST_RET_ENC_NC;
  19029. EVP_CIPHER_CTX_init(en);
  19030. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19031. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19032. return WC_TEST_RET_ENC_NC;
  19033. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19034. return WC_TEST_RET_ENC_NC;
  19035. EVP_CIPHER_CTX_init(de);
  19036. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19037. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19038. return WC_TEST_RET_ENC_NC;
  19039. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19040. return WC_TEST_RET_ENC_NC;
  19041. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19042. return WC_TEST_RET_ENC_NC;
  19043. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  19044. return WC_TEST_RET_ENC_NC;
  19045. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19046. return WC_TEST_RET_ENC_NC;
  19047. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19048. return WC_TEST_RET_ENC_NC;
  19049. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19050. return WC_TEST_RET_ENC_NC;
  19051. if (XMEMCMP(cipherBuff, oddCipher, 9))
  19052. return WC_TEST_RET_ENC_NC;
  19053. #endif /* WOLFSSL_AES_128 */
  19054. #ifdef WOLFSSL_AES_192
  19055. EVP_CIPHER_CTX_init(en);
  19056. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  19057. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19058. return WC_TEST_RET_ENC_NC;
  19059. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  19060. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19061. return WC_TEST_RET_ENC_NC;
  19062. EVP_CIPHER_CTX_init(de);
  19063. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  19064. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19065. return WC_TEST_RET_ENC_NC;
  19066. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19067. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19068. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19069. return WC_TEST_RET_ENC_NC;
  19070. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  19071. return WC_TEST_RET_ENC_NC;
  19072. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  19073. return WC_TEST_RET_ENC_NC;
  19074. #endif /* WOLFSSL_AES_192 */
  19075. #ifdef WOLFSSL_AES_256
  19076. EVP_CIPHER_CTX_init(en);
  19077. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  19078. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19079. return WC_TEST_RET_ENC_NC;
  19080. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  19081. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19082. return WC_TEST_RET_ENC_NC;
  19083. EVP_CIPHER_CTX_init(de);
  19084. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  19085. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19086. return WC_TEST_RET_ENC_NC;
  19087. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19088. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19089. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19090. return WC_TEST_RET_ENC_NC;
  19091. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  19092. return WC_TEST_RET_ENC_NC;
  19093. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  19094. return WC_TEST_RET_ENC_NC;
  19095. #endif /* WOLFSSL_AES_256 */
  19096. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19097. wolfSSL_EVP_CIPHER_CTX_free(en);
  19098. wolfSSL_EVP_CIPHER_CTX_free(de);
  19099. #endif
  19100. }
  19101. #endif /* HAVE_AES_COUNTER */
  19102. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  19103. {
  19104. /* EVP_CipherUpdate test */
  19105. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  19106. {
  19107. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19108. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19109. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19110. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19111. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19112. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19113. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19114. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19115. };
  19116. byte key[] = "0123456789abcdef "; /* align */
  19117. byte iv[] = "1234567890abcdef "; /* align */
  19118. byte cipher[AES_BLOCK_SIZE * 4];
  19119. byte plain [AES_BLOCK_SIZE * 4];
  19120. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19121. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19122. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19123. #else
  19124. EVP_CIPHER_CTX en[1];
  19125. EVP_CIPHER_CTX de[1];
  19126. #endif
  19127. int outlen ;
  19128. int total = 0;
  19129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19130. if ((en == NULL) || (de == NULL))
  19131. return MEMORY_E;
  19132. #endif
  19133. EVP_CIPHER_CTX_init(en);
  19134. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19135. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19136. return WC_TEST_RET_ENC_NC;
  19137. /* openSSL compatibility, if(inlen == 0)return 1; */
  19138. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19139. (byte*)cbcPlain, 0) != 1)
  19140. return WC_TEST_RET_ENC_NC;
  19141. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19142. return WC_TEST_RET_ENC_NC;
  19143. EVP_CIPHER_CTX_init(en);
  19144. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19145. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19146. return WC_TEST_RET_ENC_NC;
  19147. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19148. (byte*)cbcPlain, 9) == 0)
  19149. return WC_TEST_RET_ENC_NC;
  19150. if(outlen != 0)
  19151. return WC_TEST_RET_ENC_NC;
  19152. total += outlen;
  19153. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  19154. (byte*)&cbcPlain[9] , 9) == 0)
  19155. return WC_TEST_RET_ENC_NC;
  19156. if(outlen != 16)
  19157. return WC_TEST_RET_ENC_NC;
  19158. total += outlen;
  19159. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19160. return WC_TEST_RET_ENC_NC;
  19161. if(outlen != 16)
  19162. return WC_TEST_RET_ENC_NC;
  19163. total += outlen;
  19164. if(total != 32)
  19165. return WC_TEST_RET_ENC_NC;
  19166. total = 0;
  19167. EVP_CIPHER_CTX_init(de);
  19168. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19169. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19170. return WC_TEST_RET_ENC_NC;
  19171. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  19172. return WC_TEST_RET_ENC_NC;
  19173. if(outlen != 0)
  19174. return WC_TEST_RET_ENC_NC;
  19175. total += outlen;
  19176. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19177. (byte*)&cipher[6], 12) == 0)
  19178. return WC_TEST_RET_ENC_NC;
  19179. if(outlen != 0)
  19180. total += outlen;
  19181. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19182. (byte*)&cipher[6+12], 14) == 0)
  19183. return WC_TEST_RET_ENC_NC;
  19184. if(outlen != 16)
  19185. return WC_TEST_RET_ENC_NC;
  19186. total += outlen;
  19187. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  19188. return WC_TEST_RET_ENC_NC;
  19189. if(outlen != 2)
  19190. return WC_TEST_RET_ENC_NC;
  19191. total += outlen;
  19192. if(total != 18)
  19193. return WC_TEST_RET_ENC_NC;
  19194. if (XMEMCMP(plain, cbcPlain, 18))
  19195. return WC_TEST_RET_ENC_NC;
  19196. total = 0;
  19197. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19198. return WC_TEST_RET_ENC_NC;
  19199. EVP_CIPHER_CTX_init(en);
  19200. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  19201. (unsigned char*)key, (unsigned char*)iv) == 0)
  19202. return WC_TEST_RET_ENC_NC;
  19203. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  19204. return WC_TEST_RET_ENC_NC;
  19205. if(outlen != 0)
  19206. return WC_TEST_RET_ENC_NC;
  19207. total += outlen;
  19208. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  19209. return WC_TEST_RET_ENC_NC;
  19210. if(outlen != 16)
  19211. return WC_TEST_RET_ENC_NC;
  19212. total += outlen;
  19213. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19214. return WC_TEST_RET_ENC_NC;
  19215. if(outlen != 16)
  19216. return WC_TEST_RET_ENC_NC;
  19217. total += outlen;
  19218. if(total != 32)
  19219. return 3438;
  19220. total = 0;
  19221. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19222. return WC_TEST_RET_ENC_NC;
  19223. EVP_CIPHER_CTX_init(de);
  19224. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  19225. (unsigned char*)key, (unsigned char*)iv) == 0)
  19226. return WC_TEST_RET_ENC_NC;
  19227. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  19228. return WC_TEST_RET_ENC_NC;
  19229. if(outlen != 0)
  19230. return WC_TEST_RET_ENC_NC;
  19231. total += outlen;
  19232. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  19233. return WC_TEST_RET_ENC_NC;
  19234. if(outlen != 0)
  19235. total += outlen;
  19236. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  19237. return WC_TEST_RET_ENC_NC;
  19238. if(outlen != 16)
  19239. return WC_TEST_RET_ENC_NC;
  19240. total += outlen;
  19241. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  19242. return WC_TEST_RET_ENC_NC;
  19243. if(outlen != 2)
  19244. return WC_TEST_RET_ENC_NC;
  19245. total += outlen;
  19246. if(total != 18)
  19247. return 3447;
  19248. if (XMEMCMP(plain, cbcPlain, 18))
  19249. return WC_TEST_RET_ENC_NC;
  19250. if (EVP_CIPHER_key_length(NULL) != 0)
  19251. return WC_TEST_RET_ENC_NC;
  19252. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  19253. return WC_TEST_RET_ENC_NC;
  19254. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  19255. return WC_TEST_RET_ENC_NC;
  19256. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  19257. return WC_TEST_RET_ENC_NC;
  19258. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19259. return WC_TEST_RET_ENC_NC;
  19260. EVP_CIPHER_CTX_init(en);
  19261. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  19262. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19263. return WC_TEST_RET_ENC_NC;
  19264. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19265. return WC_TEST_RET_ENC_NC;
  19266. EVP_CIPHER_CTX_init(en);
  19267. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  19268. (unsigned char*)key, (unsigned char*)iv) == 0)
  19269. return WC_TEST_RET_ENC_NC;
  19270. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19271. return WC_TEST_RET_ENC_NC;
  19272. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19273. return WC_TEST_RET_ENC_NC;
  19274. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19275. return WC_TEST_RET_ENC_NC;
  19276. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19277. return WC_TEST_RET_ENC_NC;
  19278. EVP_CIPHER_CTX_init(de);
  19279. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  19280. (unsigned char*)key, (unsigned char*)iv) == 0)
  19281. return WC_TEST_RET_ENC_NC;
  19282. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19283. return WC_TEST_RET_ENC_NC;
  19284. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19285. return WC_TEST_RET_ENC_NC;
  19286. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  19287. return WC_TEST_RET_ENC_NC;
  19288. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19289. return WC_TEST_RET_ENC_NC;
  19290. EVP_CIPHER_CTX_init(en);
  19291. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  19292. (unsigned char*)key, (unsigned char*)iv);
  19293. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  19294. return WC_TEST_RET_ENC_NC;
  19295. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  19296. return WC_TEST_RET_ENC_NC;
  19297. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  19298. return WC_TEST_RET_ENC_NC;
  19299. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  19300. return WC_TEST_RET_ENC_NC;
  19301. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  19302. return WC_TEST_RET_ENC_NC;
  19303. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  19304. EVP_CIPHER_CTX_set_flags(en, 42);
  19305. if (en->flags != 42)
  19306. return WC_TEST_RET_ENC_NC;
  19307. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  19308. return WC_TEST_RET_ENC_NC;
  19309. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  19310. return WC_TEST_RET_ENC_NC;
  19311. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  19312. return WC_TEST_RET_ENC_NC;
  19313. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19314. return WC_TEST_RET_ENC_NC;
  19315. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19316. return WC_TEST_RET_ENC_NC;
  19317. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19318. wolfSSL_EVP_CIPHER_CTX_free(en);
  19319. wolfSSL_EVP_CIPHER_CTX_free(de);
  19320. #endif
  19321. }
  19322. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  19323. #endif /* ifndef NO_AES */
  19324. return 0;
  19325. }
  19326. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  19327. {
  19328. wc_test_ret_t ret = 0;
  19329. #if !defined(NO_SHA256) && !defined(NO_SHA)
  19330. WOLFSSL_EVP_MD_CTX* ctx;
  19331. WOLFSSL_EVP_MD_CTX* ctx2;
  19332. ctx = EVP_MD_CTX_create();
  19333. ctx2 = EVP_MD_CTX_create();
  19334. ret = EVP_DigestInit(ctx, EVP_sha256());
  19335. if (ret != SSL_SUCCESS) {
  19336. ret = WC_TEST_RET_ENC_NC;
  19337. goto openSSL_evpMD_test_done;
  19338. }
  19339. ret = EVP_MD_CTX_copy(ctx2, ctx);
  19340. if (ret != SSL_SUCCESS) {
  19341. ret = WC_TEST_RET_ENC_NC;
  19342. goto openSSL_evpMD_test_done;
  19343. }
  19344. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  19345. ret = WC_TEST_RET_ENC_NC;
  19346. goto openSSL_evpMD_test_done;
  19347. }
  19348. ret = EVP_DigestInit(ctx, EVP_sha1());
  19349. if (ret != SSL_SUCCESS) {
  19350. ret = WC_TEST_RET_ENC_NC;
  19351. goto openSSL_evpMD_test_done;
  19352. }
  19353. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  19354. ret = WC_TEST_RET_ENC_NC;
  19355. goto openSSL_evpMD_test_done;
  19356. }
  19357. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  19358. if (ret != SSL_SUCCESS) {
  19359. ret = WC_TEST_RET_ENC_NC;
  19360. goto openSSL_evpMD_test_done;
  19361. }
  19362. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  19363. ret = WC_TEST_RET_ENC_NC;
  19364. goto openSSL_evpMD_test_done;
  19365. }
  19366. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  19367. ret = WC_TEST_RET_ENC_NC;
  19368. goto openSSL_evpMD_test_done;
  19369. }
  19370. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  19371. ret = WC_TEST_RET_ENC_NC;
  19372. goto openSSL_evpMD_test_done;
  19373. }
  19374. if (EVP_add_digest(NULL) != 0) {
  19375. ret = WC_TEST_RET_ENC_NC;
  19376. goto openSSL_evpMD_test_done;
  19377. }
  19378. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  19379. ret = WC_TEST_RET_ENC_NC;
  19380. goto openSSL_evpMD_test_done;
  19381. }
  19382. ret = 0; /* got to success state without jumping to end with a fail */
  19383. openSSL_evpMD_test_done:
  19384. EVP_MD_CTX_destroy(ctx);
  19385. EVP_MD_CTX_destroy(ctx2);
  19386. #endif /* NO_SHA256 */
  19387. return ret;
  19388. }
  19389. #ifdef DEBUG_SIGN
  19390. static void show(const char *title, const char *p, unsigned int s) {
  19391. char* i;
  19392. printf("%s: ", title);
  19393. for (i = p;
  19394. i < p + s;
  19395. printf("%c", *i), i++);
  19396. printf("\n");
  19397. }
  19398. #else
  19399. #define show(a,b,c)
  19400. #endif
  19401. #define FOURK_BUFF 4096
  19402. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  19403. {
  19404. wc_test_ret_t ret = 0;
  19405. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  19406. byte* prvTmp;
  19407. byte* pubTmp;
  19408. int prvBytes;
  19409. int pubBytes;
  19410. RSA *prvRsa = NULL;
  19411. RSA *pubRsa = NULL;
  19412. EVP_PKEY *prvPkey = NULL;
  19413. EVP_PKEY *pubPkey = NULL;
  19414. EVP_PKEY_CTX *enc = NULL;
  19415. EVP_PKEY_CTX *dec = NULL;
  19416. byte in[] = TEST_STRING;
  19417. byte out[256];
  19418. size_t outlen;
  19419. size_t keySz;
  19420. byte plain[256];
  19421. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  19422. XFILE keyFile;
  19423. XFILE keypubFile;
  19424. char cliKey[] = "./certs/client-key.der";
  19425. char cliKeypub[] = "./certs/client-keyPub.der";
  19426. #endif
  19427. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19428. if (prvTmp == NULL)
  19429. return WC_TEST_RET_ENC_ERRNO;
  19430. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19431. if (pubTmp == NULL) {
  19432. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19433. return WC_TEST_RET_ENC_NC;
  19434. }
  19435. #ifdef USE_CERT_BUFFERS_1024
  19436. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  19437. prvBytes = sizeof_client_key_der_1024;
  19438. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19439. pubBytes = sizeof_client_keypub_der_1024;
  19440. #elif defined(USE_CERT_BUFFERS_2048)
  19441. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  19442. prvBytes = sizeof_client_key_der_2048;
  19443. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19444. pubBytes = sizeof_client_keypub_der_2048;
  19445. #else
  19446. keyFile = XFOPEN(cliKey, "rb");
  19447. if (!keyFile) {
  19448. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19449. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19450. err_sys("can't open ./certs/client-key.der, "
  19451. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19452. return WC_TEST_RET_ENC_ERRNO;
  19453. }
  19454. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  19455. XFCLOSE(keyFile);
  19456. if (prvBytes == 0) {
  19457. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19458. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19459. return WC_TEST_RET_ENC_ERRNO;
  19460. }
  19461. keypubFile = XFOPEN(cliKeypub, "rb");
  19462. if (!keypubFile) {
  19463. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19464. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19465. err_sys("can't open ./certs/client-cert.der, "
  19466. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19467. return WC_TEST_RET_ENC_ERRNO;
  19468. }
  19469. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  19470. XFCLOSE(keypubFile);
  19471. if (pubBytes == 0) {
  19472. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19473. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19474. return WC_TEST_RET_ENC_ERRNO;
  19475. }
  19476. #endif /* USE_CERT_BUFFERS */
  19477. prvRsa = wolfSSL_RSA_new();
  19478. pubRsa = wolfSSL_RSA_new();
  19479. if((prvRsa == NULL) || (pubRsa == NULL)){
  19480. printf("error with RSA_new\n");
  19481. ret = WC_TEST_RET_ENC_ERRNO;
  19482. goto openssl_pkey0_test_done;
  19483. }
  19484. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  19485. if(ret != SSL_SUCCESS){
  19486. printf("error with RSA_LoadDer_ex\n");
  19487. ret = WC_TEST_RET_ENC_EC(ret);
  19488. goto openssl_pkey0_test_done;
  19489. }
  19490. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  19491. if(ret != SSL_SUCCESS){
  19492. printf("error with RSA_LoadDer_ex\n");
  19493. ret = WC_TEST_RET_ENC_EC(ret);
  19494. goto openssl_pkey0_test_done;
  19495. }
  19496. keySz = (size_t)RSA_size(pubRsa);
  19497. prvPkey = wolfSSL_EVP_PKEY_new();
  19498. pubPkey = wolfSSL_EVP_PKEY_new();
  19499. if((prvPkey == NULL) || (pubPkey == NULL)){
  19500. printf("error with PKEY_new\n");
  19501. ret = WC_TEST_RET_ENC_NC;
  19502. goto openssl_pkey0_test_done;
  19503. }
  19504. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  19505. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  19506. if(ret != 2){
  19507. printf("error with PKEY_set1_RSA\n");
  19508. ret = WC_TEST_RET_ENC_I(ret);
  19509. goto openssl_pkey0_test_done;
  19510. }
  19511. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  19512. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  19513. if((dec == NULL)||(enc==NULL)){
  19514. printf("error with EVP_PKEY_CTX_new\n");
  19515. ret = WC_TEST_RET_ENC_NC;
  19516. goto openssl_pkey0_test_done;
  19517. }
  19518. ret = EVP_PKEY_decrypt_init(dec);
  19519. if (ret != 1) {
  19520. printf("error with decrypt init\n");
  19521. ret = WC_TEST_RET_ENC_NC;
  19522. goto openssl_pkey0_test_done;
  19523. }
  19524. ret = EVP_PKEY_encrypt_init(enc);
  19525. if (ret != 1) {
  19526. printf("error with encrypt init\n");
  19527. ret = WC_TEST_RET_ENC_NC;
  19528. goto openssl_pkey0_test_done;
  19529. }
  19530. XMEMSET(out, 0, sizeof(out));
  19531. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  19532. if (ret != 1) {
  19533. printf("error encrypting msg\n");
  19534. ret = WC_TEST_RET_ENC_NC;
  19535. goto openssl_pkey0_test_done;
  19536. }
  19537. show("encrypted msg", out, outlen);
  19538. XMEMSET(plain, 0, sizeof(plain));
  19539. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  19540. if (ret != 1) {
  19541. printf("error decrypting msg\n");
  19542. ret = WC_TEST_RET_ENC_NC;
  19543. goto openssl_pkey0_test_done;
  19544. }
  19545. show("decrypted msg", plain, outlen);
  19546. /* RSA_PKCS1_OAEP_PADDING test */
  19547. ret = EVP_PKEY_decrypt_init(dec);
  19548. if (ret != 1) {
  19549. printf("error with decrypt init\n");
  19550. ret = WC_TEST_RET_ENC_NC;
  19551. goto openssl_pkey0_test_done;
  19552. }
  19553. ret = EVP_PKEY_encrypt_init(enc);
  19554. if (ret != 1) {
  19555. printf("error with encrypt init\n");
  19556. ret = WC_TEST_RET_ENC_NC;
  19557. goto openssl_pkey0_test_done;
  19558. }
  19559. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  19560. printf("first set rsa padding error\n");
  19561. ret = WC_TEST_RET_ENC_EC(ret);
  19562. goto openssl_pkey0_test_done;
  19563. }
  19564. #ifndef HAVE_FIPS
  19565. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  19566. printf("second set rsa padding error\n");
  19567. ret = WC_TEST_RET_ENC_EC(ret);
  19568. goto openssl_pkey0_test_done;
  19569. }
  19570. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  19571. printf("third set rsa padding error\n");
  19572. ret = WC_TEST_RET_ENC_EC(ret);
  19573. goto openssl_pkey0_test_done;
  19574. }
  19575. #endif
  19576. XMEMSET(out, 0, sizeof(out));
  19577. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  19578. if (ret != 1) {
  19579. printf("error encrypting msg\n");
  19580. ret = WC_TEST_RET_ENC_NC;
  19581. goto openssl_pkey0_test_done;
  19582. }
  19583. show("encrypted msg", out, outlen);
  19584. XMEMSET(plain, 0, sizeof(plain));
  19585. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  19586. if (ret != 1) {
  19587. printf("error decrypting msg\n");
  19588. ret = WC_TEST_RET_ENC_NC;
  19589. goto openssl_pkey0_test_done;
  19590. }
  19591. show("decrypted msg", plain, outlen);
  19592. ret = 0; /* made it to this point without error then set success */
  19593. openssl_pkey0_test_done:
  19594. wolfSSL_RSA_free(prvRsa);
  19595. wolfSSL_RSA_free(pubRsa);
  19596. EVP_PKEY_free(pubPkey);
  19597. EVP_PKEY_free(prvPkey);
  19598. EVP_PKEY_CTX_free(dec);
  19599. EVP_PKEY_CTX_free(enc);
  19600. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19601. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19602. #endif /* NO_RSA */
  19603. return ret;
  19604. }
  19605. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  19606. {
  19607. wc_test_ret_t ret = 0;
  19608. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  19609. !defined(NO_SHA)
  19610. EVP_PKEY_CTX* dec = NULL;
  19611. EVP_PKEY_CTX* enc = NULL;
  19612. EVP_PKEY* pubKey = NULL;
  19613. EVP_PKEY* prvKey = NULL;
  19614. X509* x509 = NULL;
  19615. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  19616. const unsigned char* clikey;
  19617. long cliKeySz;
  19618. size_t outlen;
  19619. int keyLenBits = 2048;
  19620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19621. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19622. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19623. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19624. if ((tmp == NULL) ||
  19625. (cipher == NULL) ||
  19626. (plain == NULL)) {
  19627. ret = WC_TEST_RET_ENC_NC;
  19628. goto openssl_pkey1_test_done;
  19629. }
  19630. #else
  19631. unsigned char tmp[FOURK_BUF];
  19632. unsigned char cipher[RSA_TEST_BYTES];
  19633. unsigned char plain[RSA_TEST_BYTES];
  19634. #endif
  19635. #if defined(USE_CERT_BUFFERS_1024)
  19636. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  19637. cliKeySz = (long)sizeof_client_key_der_1024;
  19638. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  19639. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  19640. keyLenBits = 1024;
  19641. #elif defined(USE_CERT_BUFFERS_2048)
  19642. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  19643. cliKeySz = (long)sizeof_client_key_der_2048;
  19644. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  19645. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  19646. #elif defined(USE_CERT_BUFFERS_3072)
  19647. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  19648. cliKeySz = (long)sizeof_client_key_der_3072;
  19649. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  19650. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  19651. keyLenBits = 3072;
  19652. #elif defined(USE_CERT_BUFFERS_4096)
  19653. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  19654. cliKeySz = (long)sizeof_client_key_der_4096;
  19655. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  19656. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  19657. keyLenBits = 4096;
  19658. #else
  19659. {
  19660. XFILE f;
  19661. f = XFOPEN(clientKey, "rb");
  19662. if (!f) {
  19663. err_sys("can't open ./certs/client-key.der, "
  19664. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19665. ret = WC_TEST_RET_ENC_ERRNO;
  19666. goto openssl_pkey1_test_done;
  19667. }
  19668. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  19669. XFCLOSE(f);
  19670. if (cliKeySz == 0)
  19671. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  19672. }
  19673. /* using existing wolfSSL api to get public and private key */
  19674. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  19675. #endif /* USE_CERT_BUFFERS */
  19676. clikey = tmp;
  19677. if ((prvKey = EVP_PKEY_new()) == NULL) {
  19678. ret = WC_TEST_RET_ENC_ERRNO;
  19679. goto openssl_pkey1_test_done;
  19680. }
  19681. EVP_PKEY_free(prvKey);
  19682. prvKey = NULL;
  19683. if (x509 == NULL) {
  19684. ret = WC_TEST_RET_ENC_NC;
  19685. goto openssl_pkey1_test_done;
  19686. }
  19687. pubKey = X509_get_pubkey(x509);
  19688. if (pubKey == NULL) {
  19689. ret = WC_TEST_RET_ENC_NC;
  19690. goto openssl_pkey1_test_done;
  19691. }
  19692. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  19693. if (prvKey == NULL) {
  19694. ret = WC_TEST_RET_ENC_NC;
  19695. goto openssl_pkey1_test_done;
  19696. }
  19697. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  19698. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  19699. ret = WC_TEST_RET_ENC_NC;
  19700. goto openssl_pkey1_test_done;
  19701. }
  19702. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  19703. ret = WC_TEST_RET_ENC_NC;
  19704. goto openssl_pkey1_test_done;
  19705. }
  19706. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  19707. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  19708. if (dec == NULL || enc == NULL) {
  19709. ret = WC_TEST_RET_ENC_ERRNO;
  19710. goto openssl_pkey1_test_done;
  19711. }
  19712. if (EVP_PKEY_decrypt_init(dec) != 1) {
  19713. ret = WC_TEST_RET_ENC_NC;
  19714. goto openssl_pkey1_test_done;
  19715. }
  19716. if (EVP_PKEY_encrypt_init(enc) != 1) {
  19717. ret = WC_TEST_RET_ENC_NC;
  19718. goto openssl_pkey1_test_done;
  19719. }
  19720. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  19721. ret = WC_TEST_RET_ENC_NC;
  19722. goto openssl_pkey1_test_done;
  19723. }
  19724. #ifndef HAVE_FIPS
  19725. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  19726. ret = WC_TEST_RET_ENC_EC(ret);
  19727. goto openssl_pkey1_test_done;
  19728. }
  19729. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  19730. ret = WC_TEST_RET_ENC_EC(ret);
  19731. goto openssl_pkey1_test_done;
  19732. }
  19733. #endif
  19734. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  19735. outlen = keyLenBits/8;
  19736. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  19737. ret = WC_TEST_RET_ENC_EC(ret);
  19738. goto openssl_pkey1_test_done;
  19739. }
  19740. XMEMSET(plain, 0, RSA_TEST_BYTES);
  19741. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  19742. ret = WC_TEST_RET_ENC_NC;
  19743. goto openssl_pkey1_test_done;
  19744. }
  19745. openssl_pkey1_test_done:
  19746. if (pubKey != NULL) {
  19747. EVP_PKEY_free(pubKey);
  19748. }
  19749. if (prvKey != NULL) {
  19750. EVP_PKEY_free(prvKey);
  19751. }
  19752. if (dec != NULL) {
  19753. EVP_PKEY_CTX_free(dec);
  19754. }
  19755. if (enc != NULL) {
  19756. EVP_PKEY_CTX_free(enc);
  19757. }
  19758. if (x509 != NULL) {
  19759. X509_free(x509);
  19760. }
  19761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19762. if (tmp != NULL)
  19763. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19764. if (cipher != NULL)
  19765. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19766. if (plain != NULL)
  19767. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19768. #endif
  19769. #endif
  19770. return ret;
  19771. }
  19772. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  19773. {
  19774. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  19775. byte* prvTmp;
  19776. byte* pubTmp;
  19777. int prvBytes;
  19778. int pubBytes;
  19779. RSA *prvRsa;
  19780. RSA *pubRsa;
  19781. EVP_PKEY *prvPkey;
  19782. EVP_PKEY *pubPkey;
  19783. EVP_MD_CTX* sign;
  19784. EVP_MD_CTX* verf;
  19785. char msg[] = "see spot run";
  19786. unsigned char sig[256];
  19787. unsigned int sigSz;
  19788. const void* pt;
  19789. unsigned int count;
  19790. wc_test_ret_t ret, ret1, ret2;
  19791. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  19792. XFILE keyFile;
  19793. XFILE keypubFile;
  19794. char cliKey[] = "./certs/client-key.der";
  19795. char cliKeypub[] = "./certs/client-keyPub.der";
  19796. #endif
  19797. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19798. if (prvTmp == NULL)
  19799. return WC_TEST_RET_ENC_ERRNO;
  19800. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19801. if (pubTmp == NULL) {
  19802. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19803. return WC_TEST_RET_ENC_NC;
  19804. }
  19805. #ifdef USE_CERT_BUFFERS_1024
  19806. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  19807. prvBytes = sizeof_client_key_der_1024;
  19808. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19809. pubBytes = sizeof_client_keypub_der_1024;
  19810. #elif defined(USE_CERT_BUFFERS_2048)
  19811. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  19812. prvBytes = sizeof_client_key_der_2048;
  19813. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19814. pubBytes = sizeof_client_keypub_der_2048;
  19815. #else
  19816. keyFile = XFOPEN(cliKey, "rb");
  19817. if (!keyFile) {
  19818. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19819. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19820. err_sys("can't open ./certs/client-key.der, "
  19821. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19822. return WC_TEST_RET_ENC_ERRNO;
  19823. }
  19824. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  19825. XFCLOSE(keyFile);
  19826. if (prvBytes == 0) {
  19827. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19828. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19829. return WC_TEST_RET_ENC_ERRNO;
  19830. }
  19831. keypubFile = XFOPEN(cliKeypub, "rb");
  19832. if (!keypubFile) {
  19833. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19834. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19835. err_sys("can't open ./certs/client-cert.der, "
  19836. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19837. return WC_TEST_RET_ENC_ERRNO;
  19838. }
  19839. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  19840. XFCLOSE(keypubFile);
  19841. if (pubBytes == 0) {
  19842. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19843. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19844. return WC_TEST_RET_ENC_ERRNO;
  19845. }
  19846. #endif /* USE_CERT_BUFFERS */
  19847. prvRsa = wolfSSL_RSA_new();
  19848. pubRsa = wolfSSL_RSA_new();
  19849. if((prvRsa == NULL) || (pubRsa == NULL)){
  19850. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19851. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19852. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  19853. return WC_TEST_RET_ENC_NC;
  19854. }
  19855. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  19856. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  19857. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19858. printf("error with RSA_LoadDer_ex\n");
  19859. return WC_TEST_RET_ENC_NC;
  19860. }
  19861. prvPkey = wolfSSL_EVP_PKEY_new();
  19862. pubPkey = wolfSSL_EVP_PKEY_new();
  19863. if((prvPkey == NULL) || (pubPkey == NULL)){
  19864. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19865. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19866. printf("error with KEY_new\n");
  19867. return WC_TEST_RET_ENC_NC;
  19868. }
  19869. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  19870. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  19871. if((ret1 != 1) || (ret2 != 1)){
  19872. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19873. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19874. printf("error with EVP_PKEY_set1_RSA\n");
  19875. return WC_TEST_RET_ENC_NC;
  19876. }
  19877. /****************** sign and verify *******************/
  19878. sign = EVP_MD_CTX_create();
  19879. verf = EVP_MD_CTX_create();
  19880. if((sign == NULL)||(verf == NULL)){
  19881. printf("error with EVP_MD_CTX_create\n");
  19882. EVP_MD_CTX_destroy(sign);
  19883. EVP_MD_CTX_destroy(verf);
  19884. return WC_TEST_RET_ENC_NC;
  19885. }
  19886. ret = EVP_SignInit(sign, EVP_sha1());
  19887. if (ret != SSL_SUCCESS){
  19888. printf("error with EVP_SignInit\n");
  19889. EVP_MD_CTX_destroy(sign);
  19890. EVP_MD_CTX_destroy(verf);
  19891. return WC_TEST_RET_ENC_NC;
  19892. }
  19893. count = sizeof(msg);
  19894. show("message = ", (char *)msg, count);
  19895. /* sign */
  19896. XMEMSET(sig, 0, sizeof(sig));
  19897. pt = (const void*)msg;
  19898. ret1 = EVP_SignUpdate(sign, pt, count);
  19899. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  19900. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19901. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19902. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19903. EVP_MD_CTX_destroy(sign);
  19904. EVP_MD_CTX_destroy(verf);
  19905. printf("error with EVP_MD_CTX_create\n");
  19906. return WC_TEST_RET_ENC_NC;
  19907. }
  19908. show("signature = ", (char *)sig, sigSz);
  19909. /* verify */
  19910. pt = (const void*)msg;
  19911. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  19912. ret2 = EVP_VerifyUpdate(verf, pt, count);
  19913. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19914. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19915. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19916. EVP_MD_CTX_destroy(sign);
  19917. EVP_MD_CTX_destroy(verf);
  19918. printf("error with EVP_Verify\n");
  19919. return WC_TEST_RET_ENC_NC;
  19920. }
  19921. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  19922. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19923. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19924. EVP_MD_CTX_destroy(sign);
  19925. EVP_MD_CTX_destroy(verf);
  19926. printf("error with EVP_VerifyFinal\n");
  19927. return WC_TEST_RET_ENC_NC;
  19928. }
  19929. /* expect fail without update */
  19930. EVP_VerifyInit(verf, EVP_sha1());
  19931. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  19932. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19933. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19934. EVP_MD_CTX_destroy(sign);
  19935. EVP_MD_CTX_destroy(verf);
  19936. printf("EVP_VerifyInit without update not detected\n");
  19937. return WC_TEST_RET_ENC_NC;
  19938. }
  19939. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19940. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19941. EVP_MD_CTX_destroy(sign);
  19942. EVP_MD_CTX_destroy(verf);
  19943. wolfSSL_RSA_free(prvRsa);
  19944. wolfSSL_RSA_free(pubRsa);
  19945. EVP_PKEY_free(pubPkey);
  19946. EVP_PKEY_free(prvPkey);
  19947. #endif /* NO_RSA */
  19948. return 0;
  19949. }
  19950. #endif /* OPENSSL_EXTRA */
  19951. #ifndef NO_PWDBASED
  19952. #ifdef HAVE_SCRYPT
  19953. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  19954. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  19955. {
  19956. #ifdef HAVE_FIPS
  19957. /* RFC 7914 test vector keys are too short for FIPS. */
  19958. #else
  19959. wc_test_ret_t ret;
  19960. byte derived[64];
  19961. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  19962. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  19963. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  19964. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  19965. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  19966. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  19967. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  19968. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  19969. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  19970. };
  19971. #if !defined(BENCH_EMBEDDED)
  19972. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19973. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  19974. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  19975. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  19976. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  19977. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  19978. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  19979. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  19980. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  19981. };
  19982. #endif
  19983. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  19984. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19985. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  19986. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  19987. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  19988. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  19989. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  19990. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  19991. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  19992. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  19993. };
  19994. #endif
  19995. #ifdef SCRYPT_TEST_ALL
  19996. /* Test case is very slow.
  19997. * Use for confirmation after code change or new platform.
  19998. */
  19999. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  20000. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  20001. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  20002. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  20003. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  20004. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  20005. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  20006. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  20007. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  20008. };
  20009. #endif
  20010. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  20011. if (ret != 0)
  20012. return WC_TEST_RET_ENC_EC(ret);
  20013. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  20014. return WC_TEST_RET_ENC_NC;
  20015. #if !defined(BENCH_EMBEDDED)
  20016. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  20017. sizeof(verify2));
  20018. if (ret != 0)
  20019. return WC_TEST_RET_ENC_EC(ret);
  20020. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  20021. return WC_TEST_RET_ENC_NC;
  20022. #endif
  20023. /* Test case with parallel overflowing */
  20024. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  20025. sizeof(verify1));
  20026. if (ret != BAD_FUNC_ARG)
  20027. return WC_TEST_RET_ENC_EC(ret);
  20028. /* Don't run these test on embedded, since they use large mallocs */
  20029. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  20030. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  20031. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  20032. if (ret != 0)
  20033. return WC_TEST_RET_ENC_EC(ret);
  20034. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  20035. return WC_TEST_RET_ENC_NC;
  20036. #ifdef SCRYPT_TEST_ALL
  20037. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  20038. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  20039. if (ret != 0)
  20040. return WC_TEST_RET_ENC_EC(ret);
  20041. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  20042. return WC_TEST_RET_ENC_NC;
  20043. #endif
  20044. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  20045. #if !defined(BENCH_EMBEDDED)
  20046. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  20047. 8, 16, sizeof(verify2));
  20048. if (ret != 0)
  20049. return WC_TEST_RET_ENC_EC(ret);
  20050. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  20051. return WC_TEST_RET_ENC_NC;
  20052. #endif
  20053. #endif /* !HAVE_FIPS */
  20054. return 0;
  20055. }
  20056. #endif
  20057. #ifdef HAVE_PKCS12
  20058. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  20059. {
  20060. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  20061. 0x00, 0x00 };
  20062. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  20063. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  20064. 0x00, 0x67, 0x00, 0x00 };
  20065. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  20066. byte derived[64];
  20067. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20068. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  20069. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  20070. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  20071. };
  20072. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  20073. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  20074. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  20075. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  20076. };
  20077. int id = 1;
  20078. int kLen = 24;
  20079. int iterations = 1;
  20080. wc_test_ret_t ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  20081. iterations, kLen, WC_SHA256, id);
  20082. if (ret < 0)
  20083. return WC_TEST_RET_ENC_EC(ret);
  20084. if (XMEMCMP(derived, verify, kLen) != 0)
  20085. return WC_TEST_RET_ENC_NC;
  20086. iterations = 1000;
  20087. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  20088. iterations, kLen, WC_SHA256, id);
  20089. if (ret < 0)
  20090. return WC_TEST_RET_ENC_EC(ret);
  20091. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  20092. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  20093. if (ret < 0)
  20094. return WC_TEST_RET_ENC_EC(ret);
  20095. if (XMEMCMP(derived, verify2, 24) != 0)
  20096. return WC_TEST_RET_ENC_NC;
  20097. return 0;
  20098. }
  20099. #endif /* HAVE_PKCS12 */
  20100. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  20101. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  20102. {
  20103. char passwd[] = "passwordpassword";
  20104. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  20105. int iterations = 2048;
  20106. int kLen = 24;
  20107. byte derived[64];
  20108. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20109. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  20110. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  20111. };
  20112. wc_test_ret_t ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  20113. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  20114. if (ret != 0)
  20115. return ret;
  20116. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  20117. return WC_TEST_RET_ENC_NC;
  20118. return 0;
  20119. }
  20120. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  20121. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  20122. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  20123. {
  20124. char passwd[] = "password";
  20125. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  20126. int iterations = 1000;
  20127. int kLen = 16;
  20128. byte derived[16];
  20129. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20130. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  20131. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  20132. };
  20133. wc_test_ret_t ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  20134. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  20135. HEAP_HINT);
  20136. if (ret != 0)
  20137. return ret;
  20138. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  20139. return WC_TEST_RET_ENC_NC;
  20140. return 0;
  20141. }
  20142. #endif /* HAVE_PBKDF2 && !NO_SHA */
  20143. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  20144. {
  20145. wc_test_ret_t ret = 0;
  20146. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  20147. ret = pbkdf1_test();
  20148. if (ret != 0)
  20149. return ret;
  20150. #endif
  20151. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  20152. ret = pbkdf2_test();
  20153. if (ret != 0)
  20154. return ret;
  20155. #endif
  20156. #ifdef HAVE_PKCS12
  20157. ret = pkcs12_test();
  20158. if (ret != 0)
  20159. return ret;
  20160. #endif
  20161. #ifdef HAVE_SCRYPT
  20162. ret = scrypt_test();
  20163. if (ret != 0)
  20164. return ret;
  20165. #endif
  20166. return ret;
  20167. }
  20168. #endif /* NO_PWDBASED */
  20169. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  20170. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  20171. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  20172. defined(WOLFSSL_AFALG_XILINX_RSA)
  20173. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  20174. static wc_test_ret_t hkdf_test(void)
  20175. #else
  20176. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  20177. #endif
  20178. {
  20179. wc_test_ret_t ret = 0;
  20180. #if !defined(NO_SHA) || !defined(NO_SHA256)
  20181. int L;
  20182. byte okm1[42];
  20183. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  20184. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  20185. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  20186. #ifndef HAVE_FIPS
  20187. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  20188. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  20189. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  20190. 0xf8, 0xf9 };
  20191. #endif
  20192. #ifndef NO_SHA
  20193. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  20194. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  20195. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  20196. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  20197. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  20198. 0x49, 0x18 };
  20199. #ifndef HAVE_FIPS
  20200. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  20201. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  20202. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  20203. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  20204. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  20205. 0xf8, 0x96 };
  20206. #endif
  20207. #endif /* !NO_SHA */
  20208. #ifndef NO_SHA256
  20209. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  20210. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  20211. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  20212. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  20213. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  20214. 0x96, 0xc8 };
  20215. #ifndef HAVE_FIPS
  20216. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  20217. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  20218. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  20219. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  20220. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  20221. 0x58, 0x65 };
  20222. #endif
  20223. #endif /* !NO_SHA256 */
  20224. XMEMSET(okm1, 0, sizeof(okm1));
  20225. L = (int)sizeof(okm1);
  20226. #ifndef NO_SHA
  20227. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  20228. okm1, L);
  20229. if (ret != 0)
  20230. return WC_TEST_RET_ENC_EC(ret);
  20231. if (XMEMCMP(okm1, res1, L) != 0)
  20232. return WC_TEST_RET_ENC_NC;
  20233. #ifndef HAVE_FIPS
  20234. /* fips can't have key size under 14 bytes, salt is key too */
  20235. L = (int)sizeof(okm1);
  20236. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  20237. info1, (word32)sizeof(info1), okm1, L);
  20238. if (ret != 0)
  20239. return WC_TEST_RET_ENC_EC(ret);
  20240. if (XMEMCMP(okm1, res2, L) != 0)
  20241. return WC_TEST_RET_ENC_NC;
  20242. #endif /* HAVE_FIPS */
  20243. #endif /* !NO_SHA */
  20244. #ifndef NO_SHA256
  20245. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  20246. okm1, L);
  20247. if (ret != 0)
  20248. return WC_TEST_RET_ENC_EC(ret);
  20249. if (XMEMCMP(okm1, res3, L) != 0)
  20250. return WC_TEST_RET_ENC_NC;
  20251. #ifndef HAVE_FIPS
  20252. /* fips can't have key size under 14 bytes, salt is key too */
  20253. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  20254. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  20255. if (ret != 0)
  20256. return WC_TEST_RET_ENC_EC(ret);
  20257. if (XMEMCMP(okm1, res4, L) != 0)
  20258. return WC_TEST_RET_ENC_NC;
  20259. #endif /* HAVE_FIPS */
  20260. #endif /* !NO_SHA256 */
  20261. #endif /* !NO_SHA || !NO_SHA256 */
  20262. return ret;
  20263. }
  20264. #endif /* HAVE_HKDF */
  20265. #ifdef WOLFSSL_WOLFSSH
  20266. typedef struct {
  20267. byte hashId;
  20268. byte keyId;
  20269. const byte* k;
  20270. word32 kSz;
  20271. const byte* h;
  20272. word32 hSz;
  20273. const byte* sessionId;
  20274. word32 sessionIdSz;
  20275. const byte* expectedKey;
  20276. word32 expectedKeySz;
  20277. } SshKdfTestVector;
  20278. /** Test Vector Set #3: SHA-256 **/
  20279. static const byte sshKdfTvSet3k[] = {
  20280. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  20281. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  20282. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  20283. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  20284. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  20285. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  20286. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  20287. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  20288. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  20289. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  20290. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  20291. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  20292. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  20293. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  20294. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  20295. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  20296. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  20297. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  20298. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  20299. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  20300. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  20301. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  20302. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  20303. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  20304. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  20305. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  20306. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  20307. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  20308. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  20309. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  20310. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  20311. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  20312. };
  20313. static const byte sshKdfTvSet3h[] = {
  20314. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  20315. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  20316. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  20317. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  20318. };
  20319. static const byte sshKdfTvSet3sid[] = {
  20320. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  20321. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  20322. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  20323. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  20324. };
  20325. static const byte sshKdfTvSet3a[] = {
  20326. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  20327. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  20328. };
  20329. static const byte sshKdfTvSet3b[] = {
  20330. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  20331. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  20332. };
  20333. static const byte sshKdfTvSet3c[] = {
  20334. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  20335. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  20336. };
  20337. static const byte sshKdfTvSet3d[] = {
  20338. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  20339. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  20340. };
  20341. static const byte sshKdfTvSet3e[] = {
  20342. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  20343. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  20344. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  20345. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  20346. };
  20347. static const byte sshKdfTvSet3f[] = {
  20348. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  20349. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  20350. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  20351. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  20352. };
  20353. static const SshKdfTestVector sshKdfTestVectors[] = {
  20354. {WC_HASH_TYPE_SHA256, 'A',
  20355. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20356. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20357. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20358. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  20359. {WC_HASH_TYPE_SHA256, 'B',
  20360. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20361. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20362. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20363. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  20364. {WC_HASH_TYPE_SHA256, 'C',
  20365. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20366. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20367. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20368. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  20369. {WC_HASH_TYPE_SHA256, 'D',
  20370. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20371. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20372. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20373. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  20374. {WC_HASH_TYPE_SHA256, 'E',
  20375. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20376. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20377. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20378. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  20379. {WC_HASH_TYPE_SHA256, 'F',
  20380. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20381. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20382. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20383. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  20384. };
  20385. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  20386. {
  20387. wc_test_ret_t result = 0;
  20388. word32 i;
  20389. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  20390. const SshKdfTestVector* tv = NULL;
  20391. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  20392. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  20393. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  20394. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  20395. cKey, tv->expectedKeySz,
  20396. tv->k, tv->kSz, tv->h, tv->hSz,
  20397. tv->sessionId, tv->sessionIdSz);
  20398. if (result != 0) {
  20399. printf("KDF: Could not derive key.\n");
  20400. result = WC_TEST_RET_ENC_EC(result);
  20401. }
  20402. else {
  20403. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  20404. printf("KDF: Calculated Key does not match Expected Key.\n");
  20405. result = WC_TEST_RET_ENC_EC(result);
  20406. }
  20407. }
  20408. if (result != 0) break;
  20409. }
  20410. return result;
  20411. }
  20412. #endif /* WOLFSSL_WOLFSSH */
  20413. #ifdef WOLFSSL_TLS13
  20414. #define TLSV13_PSK_DHE_SZ 40
  20415. typedef struct {
  20416. enum wc_HashType hashAlg;
  20417. word32 pskSz;
  20418. word32 dheSz;
  20419. byte psk[TLSV13_PSK_DHE_SZ];
  20420. byte dhe[TLSV13_PSK_DHE_SZ];
  20421. byte hashHello1[WC_MAX_DIGEST_SIZE];
  20422. byte hashHello2[WC_MAX_DIGEST_SIZE];
  20423. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  20424. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  20425. /* Expected */
  20426. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  20427. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  20428. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  20429. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  20430. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  20431. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  20432. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  20433. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  20434. } Tls13KdfTestVector;
  20435. /* The following tests come from the CAVP test vectors we used for
  20436. * our FIPS validation. The hash values used are the components from
  20437. * the test hashed together. hashHello1 is the hash of the
  20438. * clientHelloRandom value of the test vector. hashHello2 is the hash
  20439. * of the clientHelloRandom and serverHelloRandom values from the test
  20440. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  20441. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  20442. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  20443. * hashed together. */
  20444. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  20445. { /* 1 */
  20446. WC_HASH_TYPE_SHA256, 35, 35,
  20447. { /* PSK */
  20448. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  20449. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  20450. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  20451. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  20452. 0x7f, 0x6d, 0xbe
  20453. },
  20454. { /* DHE */
  20455. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  20456. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  20457. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  20458. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  20459. 0x81, 0x26, 0x40
  20460. },
  20461. { /* Hello 1 */
  20462. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  20463. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  20464. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  20465. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  20466. },
  20467. { /* Hello 2 */
  20468. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  20469. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  20470. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  20471. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  20472. },
  20473. { /* Finished 1 */
  20474. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  20475. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  20476. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  20477. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  20478. },
  20479. { /* Finished 2 */
  20480. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  20481. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  20482. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  20483. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  20484. },
  20485. { /* Client Early Traffic Secret */
  20486. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  20487. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  20488. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  20489. },
  20490. { /* Early Exporter Master Secret */
  20491. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  20492. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  20493. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  20494. },
  20495. { /* Client Handshake Traffic Secret */
  20496. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  20497. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  20498. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  20499. },
  20500. { /* Server Handshake Traffic Secret */
  20501. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  20502. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  20503. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  20504. },
  20505. { /* Client Application Traffic Secret */
  20506. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  20507. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  20508. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  20509. },
  20510. { /* Server Application Traffic Secret */
  20511. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  20512. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  20513. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  20514. },
  20515. { /* Exporter Master Secret */
  20516. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  20517. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  20518. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  20519. },
  20520. { /* Resumption Master Secret */
  20521. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  20522. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  20523. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  20524. }
  20525. },
  20526. { /* 6 */
  20527. WC_HASH_TYPE_SHA256, 0, 33,
  20528. { 0 }, /* PSK */
  20529. { /* DHE */
  20530. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  20531. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  20532. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  20533. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  20534. 0x18
  20535. },
  20536. { /* Hello 1 */
  20537. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  20538. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  20539. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  20540. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  20541. },
  20542. { /* Hello 2 */
  20543. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  20544. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  20545. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  20546. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  20547. },
  20548. { /* Finished 1 */
  20549. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  20550. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  20551. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  20552. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  20553. },
  20554. { /* Finished 2 */
  20555. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  20556. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  20557. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  20558. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  20559. },
  20560. {
  20561. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  20562. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  20563. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  20564. },
  20565. {
  20566. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  20567. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  20568. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  20569. },
  20570. {
  20571. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  20572. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  20573. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  20574. },
  20575. {
  20576. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  20577. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  20578. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  20579. },
  20580. {
  20581. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  20582. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  20583. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  20584. },
  20585. {
  20586. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  20587. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  20588. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  20589. },
  20590. {
  20591. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  20592. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  20593. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  20594. },
  20595. {
  20596. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  20597. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  20598. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  20599. }
  20600. },
  20601. { /* 11 */
  20602. WC_HASH_TYPE_SHA256, 33, 0,
  20603. { /* PSK */
  20604. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  20605. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  20606. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  20607. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  20608. 0x12
  20609. },
  20610. { 0 }, /* DHE */
  20611. { /* Hello 1 */
  20612. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  20613. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  20614. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  20615. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  20616. },
  20617. { /* Hello 2 */
  20618. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  20619. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  20620. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  20621. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  20622. },
  20623. { /* Finished 1 */
  20624. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  20625. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  20626. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  20627. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  20628. },
  20629. { /* Finished 2 */
  20630. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  20631. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  20632. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  20633. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  20634. },
  20635. {
  20636. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  20637. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  20638. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  20639. {
  20640. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  20641. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  20642. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  20643. {
  20644. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  20645. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  20646. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  20647. {
  20648. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  20649. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  20650. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  20651. {
  20652. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  20653. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  20654. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  20655. {
  20656. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  20657. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  20658. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  20659. {
  20660. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  20661. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  20662. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  20663. {
  20664. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  20665. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  20666. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  20667. },
  20668. #ifdef WOLFSSL_SHA384
  20669. { /* 26 */
  20670. WC_HASH_TYPE_SHA384, 35, 35,
  20671. { /* PSK */
  20672. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  20673. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  20674. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  20675. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  20676. 0x68, 0xba, 0xf9
  20677. },
  20678. { /* DHE */
  20679. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  20680. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  20681. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  20682. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  20683. 0xd8, 0x66, 0xac
  20684. },
  20685. { /* Hello 1 */
  20686. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  20687. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  20688. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  20689. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  20690. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  20691. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  20692. },
  20693. { /* Hello 2 */
  20694. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  20695. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  20696. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  20697. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  20698. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  20699. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  20700. },
  20701. { /* Finished 1 */
  20702. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  20703. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  20704. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  20705. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  20706. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  20707. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  20708. },
  20709. { /* Finished 2 */
  20710. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  20711. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  20712. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  20713. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  20714. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  20715. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  20716. },
  20717. {
  20718. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  20719. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  20720. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  20721. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  20722. },
  20723. {
  20724. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  20725. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  20726. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  20727. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  20728. },
  20729. {
  20730. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  20731. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  20732. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  20733. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  20734. },
  20735. {
  20736. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  20737. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  20738. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  20739. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  20740. },
  20741. {
  20742. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  20743. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  20744. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  20745. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  20746. },
  20747. {
  20748. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  20749. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  20750. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  20751. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  20752. },
  20753. {
  20754. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  20755. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  20756. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  20757. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  20758. },
  20759. {
  20760. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  20761. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  20762. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  20763. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  20764. }
  20765. },
  20766. { /* 36 */
  20767. WC_HASH_TYPE_SHA384, 0, 33,
  20768. { 0 }, /* PSK */
  20769. { /* DHE */
  20770. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  20771. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  20772. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  20773. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  20774. 0xfd
  20775. },
  20776. { /* Hello 1 */
  20777. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  20778. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  20779. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  20780. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  20781. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  20782. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  20783. },
  20784. { /* Hello 2 */
  20785. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  20786. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  20787. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  20788. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  20789. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  20790. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  20791. },
  20792. { /* Finished 1 */
  20793. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  20794. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  20795. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  20796. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  20797. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  20798. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  20799. },
  20800. { /* Finished 2 */
  20801. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  20802. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  20803. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  20804. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  20805. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  20806. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  20807. },
  20808. {
  20809. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  20810. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  20811. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  20812. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  20813. },
  20814. {
  20815. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  20816. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  20817. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  20818. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  20819. },
  20820. {
  20821. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  20822. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  20823. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  20824. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  20825. },
  20826. {
  20827. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  20828. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  20829. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  20830. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  20831. },
  20832. {
  20833. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  20834. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  20835. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  20836. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  20837. },
  20838. {
  20839. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  20840. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  20841. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  20842. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  20843. },
  20844. {
  20845. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  20846. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  20847. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  20848. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  20849. },
  20850. {
  20851. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  20852. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  20853. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  20854. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  20855. }
  20856. },
  20857. { /* 41 */
  20858. WC_HASH_TYPE_SHA384, 33, 0,
  20859. { /* PSK */
  20860. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  20861. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  20862. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  20863. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  20864. 0x12
  20865. },
  20866. { 0 }, /* DHE */
  20867. { /* Hello 1 */
  20868. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  20869. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  20870. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  20871. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  20872. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  20873. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  20874. },
  20875. { /* Hello 2 */
  20876. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  20877. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  20878. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  20879. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  20880. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  20881. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  20882. },
  20883. { /* Finished 1 */
  20884. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  20885. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  20886. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  20887. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  20888. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  20889. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  20890. },
  20891. { /* Finished 2 */
  20892. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  20893. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  20894. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  20895. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  20896. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  20897. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  20898. },
  20899. {
  20900. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  20901. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  20902. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  20903. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  20904. },
  20905. {
  20906. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  20907. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  20908. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  20909. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  20910. },
  20911. {
  20912. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  20913. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  20914. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  20915. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  20916. },
  20917. {
  20918. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  20919. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  20920. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  20921. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  20922. },
  20923. {
  20924. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  20925. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  20926. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  20927. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  20928. },
  20929. {
  20930. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  20931. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  20932. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  20933. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  20934. },
  20935. {
  20936. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  20937. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  20938. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  20939. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  20940. },
  20941. {
  20942. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  20943. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  20944. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  20945. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  20946. }
  20947. },
  20948. #endif /* WOLFSSL_SHA384 */
  20949. };
  20950. static const char protocolLabel[] = "tls13 ";
  20951. static const char ceTrafficLabel[] = "c e traffic";
  20952. static const char eExpMasterLabel[] = "e exp master";
  20953. static const char cHsTrafficLabel[] = "c hs traffic";
  20954. static const char sHsTrafficLabel[] = "s hs traffic";
  20955. static const char cAppTrafficLabel[] = "c ap traffic";
  20956. static const char sAppTrafficLabel[] = "s ap traffic";
  20957. static const char expMasterLabel[] = "exp master";
  20958. static const char resMasterLabel[] = "res master";
  20959. static const char derivedLabel[] = "derived";
  20960. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  20961. {
  20962. wc_test_ret_t ret = 0;
  20963. word32 i;
  20964. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  20965. const Tls13KdfTestVector* tv = NULL;
  20966. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  20967. byte output[WC_MAX_DIGEST_SIZE];
  20968. byte secret[WC_MAX_DIGEST_SIZE];
  20969. byte salt[WC_MAX_DIGEST_SIZE];
  20970. byte zeroes[WC_MAX_DIGEST_SIZE];
  20971. byte hashZero[WC_MAX_DIGEST_SIZE];
  20972. int hashAlgSz;
  20973. XMEMSET(zeroes, 0, sizeof zeroes);
  20974. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  20975. if (hashAlgSz == BAD_FUNC_ARG) break;
  20976. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  20977. if (ret != 0) break;
  20978. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  20979. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  20980. tv->pskSz, tv->hashAlg);
  20981. if (ret != 0) break;
  20982. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  20983. secret, hashAlgSz,
  20984. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  20985. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  20986. tv->hashHello1, hashAlgSz, tv->hashAlg);
  20987. if (ret != 0) break;
  20988. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  20989. if (ret != 0) break;
  20990. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  20991. secret, hashAlgSz,
  20992. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  20993. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  20994. tv->hashHello1, hashAlgSz, tv->hashAlg);
  20995. if (ret != 0) break;
  20996. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  20997. if (ret != 0) break;
  20998. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  20999. secret, hashAlgSz,
  21000. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21001. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  21002. hashZero, hashAlgSz, tv->hashAlg);
  21003. if (ret != 0) break;
  21004. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  21005. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  21006. tv->dheSz, tv->hashAlg);
  21007. if (ret != 0) break;
  21008. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21009. secret, hashAlgSz,
  21010. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21011. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  21012. tv->hashHello2, hashAlgSz, tv->hashAlg);
  21013. if (ret != 0) break;
  21014. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  21015. output, hashAlgSz);
  21016. if (ret != 0) break;
  21017. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21018. secret, hashAlgSz,
  21019. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21020. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  21021. tv->hashHello2, hashAlgSz, tv->hashAlg);
  21022. if (ret != 0) break;
  21023. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  21024. if (ret != 0) break;
  21025. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  21026. secret, hashAlgSz,
  21027. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21028. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  21029. hashZero, hashAlgSz, tv->hashAlg);
  21030. if (ret != 0) break;
  21031. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  21032. zeroes, hashAlgSz, tv->hashAlg);
  21033. if (ret != 0) break;
  21034. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21035. secret, hashAlgSz,
  21036. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21037. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  21038. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21039. if (ret != 0) break;
  21040. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  21041. if (ret != 0) break;
  21042. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21043. secret, hashAlgSz,
  21044. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21045. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  21046. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21047. if (ret != 0) break;
  21048. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  21049. if (ret != 0) break;
  21050. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21051. secret, hashAlgSz,
  21052. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21053. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  21054. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21055. if (ret != 0) break;
  21056. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  21057. if (ret != 0) break;
  21058. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21059. secret, hashAlgSz,
  21060. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21061. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  21062. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  21063. if (ret != 0) break;
  21064. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  21065. if (ret != 0) break;
  21066. }
  21067. return ret;
  21068. }
  21069. #endif /* WOLFSSL_TLS13 */
  21070. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  21071. * see print_fiducials() below.
  21072. */
  21073. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  21074. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  21075. {
  21076. wc_test_ret_t ret;
  21077. byte kek[128];
  21078. #ifndef NO_SHA
  21079. /* SHA-1, COUNT = 0
  21080. * shared secret length: 192
  21081. * SharedInfo length: 0
  21082. * key data length: 128
  21083. */
  21084. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  21085. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  21086. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  21087. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  21088. };
  21089. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21090. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  21091. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  21092. };
  21093. #endif
  21094. #ifndef NO_SHA256
  21095. /* SHA-256, COUNT = 3
  21096. * shared secret length: 192
  21097. * SharedInfo length: 0
  21098. * key data length: 128
  21099. */
  21100. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  21101. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  21102. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  21103. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  21104. };
  21105. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21106. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  21107. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  21108. };
  21109. #endif
  21110. #ifdef WOLFSSL_SHA512
  21111. /* SHA-512, COUNT = 0
  21112. * shared secret length: 192
  21113. * SharedInfo length: 0
  21114. * key data length: 128
  21115. */
  21116. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  21117. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  21118. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  21119. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  21120. };
  21121. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  21122. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  21123. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  21124. };
  21125. /* SHA-512, COUNT = 0
  21126. * shared secret length: 521
  21127. * SharedInfo length: 128
  21128. * key data length: 1024
  21129. */
  21130. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  21131. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  21132. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  21133. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  21134. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  21135. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  21136. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  21137. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  21138. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  21139. 0x5a, 0x2d
  21140. };
  21141. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  21142. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  21143. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  21144. };
  21145. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  21146. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  21147. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  21148. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  21149. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  21150. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  21151. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  21152. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  21153. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  21154. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  21155. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  21156. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  21157. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  21158. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  21159. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  21160. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  21161. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  21162. };
  21163. #endif
  21164. #ifndef NO_SHA
  21165. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  21166. kek, sizeof(verify));
  21167. if (ret != 0)
  21168. return WC_TEST_RET_ENC_EC(ret);
  21169. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  21170. return WC_TEST_RET_ENC_NC;
  21171. #endif
  21172. #ifndef NO_SHA256
  21173. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  21174. kek, sizeof(verify2));
  21175. if (ret != 0)
  21176. return WC_TEST_RET_ENC_EC(ret);
  21177. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  21178. return WC_TEST_RET_ENC_NC;
  21179. #endif
  21180. #ifdef WOLFSSL_SHA512
  21181. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  21182. kek, sizeof(verify3));
  21183. if (ret != 0)
  21184. return WC_TEST_RET_ENC_EC(ret);
  21185. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  21186. return WC_TEST_RET_ENC_NC;
  21187. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  21188. sizeof(info4), kek, sizeof(verify4));
  21189. if (ret != 0)
  21190. return WC_TEST_RET_ENC_EC(ret);
  21191. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  21192. return WC_TEST_RET_ENC_NC;
  21193. #endif
  21194. return 0;
  21195. }
  21196. #endif /* HAVE_X963_KDF */
  21197. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  21198. defined(HAVE_AESGCM)
  21199. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  21200. {
  21201. wc_test_ret_t ret = 0;
  21202. int rngRet = 0;
  21203. WC_RNG rng[1];
  21204. const char* start_text = "this is a test";
  21205. const char* info_text = "info";
  21206. const char* aad_text = "aad";
  21207. byte ciphertext[MAX_HPKE_LABEL_SZ];
  21208. byte plaintext[MAX_HPKE_LABEL_SZ];
  21209. void* receiverKey = NULL;
  21210. void* ephemeralKey = NULL;
  21211. #ifdef WOLFSSL_SMALL_STACK
  21212. byte *pubKey = NULL; /* public key */
  21213. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  21214. #else
  21215. byte pubKey[HPKE_Npk_MAX]; /* public key */
  21216. word16 pubKeySz = (word16)sizeof(pubKey);
  21217. #endif
  21218. rngRet = ret = wc_InitRng(rng);
  21219. if (ret != 0)
  21220. return ret;
  21221. #ifdef WOLFSSL_SMALL_STACK
  21222. if (ret == 0) {
  21223. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  21224. DYNAMIC_TYPE_TMP_BUFFER);
  21225. if (pubKey == NULL)
  21226. ret = MEMORY_E;
  21227. }
  21228. #endif
  21229. /* generate the keys */
  21230. if (ret == 0)
  21231. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  21232. if (ret == 0)
  21233. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  21234. /* seal */
  21235. if (ret == 0) {
  21236. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  21237. (byte*)info_text, (word32)XSTRLEN(info_text),
  21238. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  21239. (byte*)start_text, (word32)XSTRLEN(start_text),
  21240. ciphertext);
  21241. }
  21242. /* export ephemeral key */
  21243. if (ret == 0)
  21244. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  21245. /* open with exported ephemeral key */
  21246. if (ret == 0) {
  21247. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  21248. (byte*)info_text, (word32)XSTRLEN(info_text),
  21249. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  21250. ciphertext, (word32)XSTRLEN(start_text),
  21251. plaintext);
  21252. }
  21253. if (ret == 0)
  21254. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  21255. if (ephemeralKey != NULL)
  21256. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  21257. if (receiverKey != NULL)
  21258. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  21259. #ifdef WOLFSSL_SMALL_STACK
  21260. if (pubKey != NULL)
  21261. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21262. #endif
  21263. if (rngRet == 0)
  21264. wc_FreeRng(rng);
  21265. return ret;
  21266. }
  21267. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  21268. {
  21269. wc_test_ret_t ret = 0;
  21270. Hpke hpke[1];
  21271. #if defined(HAVE_ECC)
  21272. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  21273. /* p256 */
  21274. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  21275. HPKE_AES_128_GCM, NULL);
  21276. if (ret != 0)
  21277. return ret;
  21278. ret = hpke_test_single(hpke);
  21279. if (ret != 0)
  21280. return ret;
  21281. #endif
  21282. #if defined(WOLFSSL_SHA384) && \
  21283. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  21284. /* p384 */
  21285. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  21286. HPKE_AES_128_GCM, NULL);
  21287. if (ret != 0)
  21288. return ret;
  21289. ret = hpke_test_single(hpke);
  21290. if (ret != 0)
  21291. return ret;
  21292. #endif
  21293. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  21294. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  21295. /* p521 */
  21296. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  21297. HPKE_AES_128_GCM, NULL);
  21298. if (ret != 0)
  21299. return ret;
  21300. ret = hpke_test_single(hpke);
  21301. if (ret != 0)
  21302. return ret;
  21303. #endif
  21304. #endif
  21305. #if defined(HAVE_CURVE25519)
  21306. /* test with curve25519 and aes256 */
  21307. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  21308. HPKE_AES_256_GCM, NULL);
  21309. if (ret != 0)
  21310. return ret;
  21311. ret = hpke_test_single(hpke);
  21312. if (ret != 0)
  21313. return ret;
  21314. #endif
  21315. return ret;
  21316. /* x448 and chacha20 are unimplemented */
  21317. }
  21318. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  21319. #ifdef HAVE_ECC
  21320. /* size to use for ECC key gen tests */
  21321. #ifndef ECC_KEYGEN_SIZE
  21322. #ifndef NO_ECC256
  21323. #define ECC_KEYGEN_SIZE 32
  21324. #elif defined(HAVE_ECC384)
  21325. #define ECC_KEYGEN_SIZE 48
  21326. #elif defined(HAVE_ECC224)
  21327. #define ECC_KEYGEN_SIZE 28
  21328. #elif defined(HAVE_ECC521)
  21329. #define ECC_KEYGEN_SIZE 66
  21330. #else
  21331. #error No ECC keygen size defined for test
  21332. #endif
  21333. #endif
  21334. #ifdef BENCH_EMBEDDED
  21335. #define ECC_SHARED_SIZE 128
  21336. #else
  21337. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  21338. #endif
  21339. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  21340. #define HAVE_ECC_DETERMINISTIC_K
  21341. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  21342. #else
  21343. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  21344. #endif
  21345. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  21346. #ifdef NO_ECC_SECP
  21347. #define NO_ECC_VECTOR_TEST
  21348. #endif
  21349. #ifndef NO_ECC_VECTOR_TEST
  21350. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  21351. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  21352. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  21353. #define HAVE_ECC_VECTOR_TEST
  21354. #endif
  21355. #endif
  21356. #ifdef HAVE_ECC_VECTOR_TEST
  21357. typedef struct eccVector {
  21358. const char* msg; /* SHA-1 Encoded Message */
  21359. const char* Qx;
  21360. const char* Qy;
  21361. const char* d; /* Private Key */
  21362. const char* R;
  21363. const char* S;
  21364. const char* curveName;
  21365. word32 msgLen;
  21366. word32 keySize;
  21367. #ifndef NO_ASN
  21368. const byte* r;
  21369. word32 rSz;
  21370. const byte* s;
  21371. word32 sSz;
  21372. #endif
  21373. } eccVector;
  21374. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21375. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  21376. {
  21377. wc_test_ret_t ret = 0;
  21378. int verify = 0;
  21379. word32 sigSz;
  21380. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21381. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21382. #else
  21383. ecc_key userA[1];
  21384. #endif
  21385. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21386. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21387. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  21388. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  21389. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  21390. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  21391. #endif
  21392. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21393. if (sig == NULL)
  21394. ERROR_OUT(MEMORY_E, done);
  21395. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21396. if (sigRaw == NULL || r == NULL || s == NULL)
  21397. ERROR_OUT(MEMORY_E, done);
  21398. #endif
  21399. #endif
  21400. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21401. if (userA == NULL)
  21402. ERROR_OUT(MEMORY_E, done);
  21403. #endif
  21404. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21405. if (ret != 0)
  21406. goto done;
  21407. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  21408. vector->d, vector->curveName);
  21409. if (ret != 0)
  21410. goto done;
  21411. XMEMSET(sig, 0, ECC_SIG_SIZE);
  21412. sigSz = ECC_SIG_SIZE;
  21413. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  21414. if (ret != 0)
  21415. goto done;
  21416. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21417. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  21418. sigRawSz = ECC_SIG_SIZE;
  21419. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  21420. sigRaw, &sigRawSz);
  21421. if (ret != 0)
  21422. goto done;
  21423. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  21424. ret = WC_TEST_RET_ENC_NC;
  21425. goto done;
  21426. }
  21427. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  21428. if (ret != 0)
  21429. goto done;
  21430. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  21431. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  21432. ret = WC_TEST_RET_ENC_NC;
  21433. goto done;
  21434. }
  21435. #endif
  21436. #ifdef HAVE_ECC_VERIFY
  21437. do {
  21438. #if defined(WOLFSSL_ASYNC_CRYPT)
  21439. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21440. #endif
  21441. if (ret == 0)
  21442. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  21443. vector->msgLen, &verify, userA);
  21444. } while (ret == WC_PENDING_E);
  21445. if (ret != 0)
  21446. goto done;
  21447. TEST_SLEEP();
  21448. if (verify != 1)
  21449. ret = WC_TEST_RET_ENC_NC;
  21450. #endif
  21451. done:
  21452. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21453. if (userA != NULL) {
  21454. wc_ecc_free(userA);
  21455. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21456. }
  21457. #else
  21458. wc_ecc_free(userA);
  21459. #endif
  21460. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21461. WC_FREE_VAR(sigRaw, HEAP_HINT);
  21462. WC_FREE_VAR(r, HEAP_HINT);
  21463. WC_FREE_VAR(s, HEAP_HINT);
  21464. #endif
  21465. WC_FREE_VAR(sig, HEAP_HINT);
  21466. return ret;
  21467. }
  21468. static wc_test_ret_t ecc_test_vector(int keySize)
  21469. {
  21470. wc_test_ret_t ret;
  21471. eccVector vec;
  21472. XMEMSET(&vec, 0, sizeof(vec));
  21473. vec.keySize = (word32)keySize;
  21474. switch(keySize) {
  21475. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  21476. case 14:
  21477. return 0;
  21478. #endif /* HAVE_ECC112 */
  21479. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  21480. case 16:
  21481. return 0;
  21482. #endif /* HAVE_ECC128 */
  21483. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  21484. case 20:
  21485. return 0;
  21486. #endif /* HAVE_ECC160 */
  21487. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  21488. case 24:
  21489. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21490. #if 1
  21491. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  21492. vec.msgLen = 20;
  21493. #else
  21494. /* This is the raw message prior to SHA-1 */
  21495. vec.msg =
  21496. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  21497. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  21498. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  21499. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  21500. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  21501. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  21502. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  21503. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  21504. vec.msgLen = 128;
  21505. #endif
  21506. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  21507. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  21508. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  21509. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21510. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21511. vec.curveName = "SECP192R1";
  21512. #ifndef NO_ASN
  21513. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  21514. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  21515. vec.rSz = 24;
  21516. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  21517. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  21518. vec.sSz = 24;
  21519. #endif
  21520. break;
  21521. #endif /* HAVE_ECC192 */
  21522. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  21523. case 28:
  21524. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  21525. #if 1
  21526. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  21527. vec.msgLen = 20;
  21528. #else
  21529. /* This is the raw message prior to SHA-1 */
  21530. vec.msg =
  21531. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  21532. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  21533. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  21534. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  21535. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  21536. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  21537. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  21538. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  21539. vec.msgLen = 128;
  21540. #endif
  21541. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  21542. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  21543. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  21544. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  21545. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  21546. vec.curveName = "SECP224R1";
  21547. #ifndef NO_ASN
  21548. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  21549. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  21550. "\xbc\x16\x71\xa7";
  21551. vec.rSz = 28;
  21552. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  21553. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  21554. "\x6a\xf3\xad\x5b";
  21555. vec.sSz = 28;
  21556. #endif
  21557. break;
  21558. #endif /* HAVE_ECC224 */
  21559. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  21560. case 30:
  21561. return 0;
  21562. #endif /* HAVE_ECC239 */
  21563. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  21564. case 32:
  21565. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  21566. #if 1
  21567. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  21568. vec.msgLen = 20;
  21569. #else
  21570. /* This is the raw message prior to SHA-1 */
  21571. vec.msg =
  21572. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  21573. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  21574. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  21575. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  21576. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  21577. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  21578. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  21579. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  21580. vec.msgLen = 128;
  21581. #endif
  21582. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  21583. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  21584. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  21585. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  21586. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  21587. #ifndef NO_ASN
  21588. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  21589. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  21590. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  21591. vec.rSz = 32;
  21592. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  21593. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  21594. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  21595. vec.sSz = 32;
  21596. #endif
  21597. vec.curveName = "SECP256R1";
  21598. break;
  21599. #endif /* !NO_ECC256 */
  21600. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  21601. case 40:
  21602. return 0;
  21603. #endif /* HAVE_ECC320 */
  21604. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  21605. case 48:
  21606. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  21607. #if 1
  21608. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  21609. vec.msgLen = 20;
  21610. #else
  21611. /* This is the raw message prior to SHA-1 */
  21612. vec.msg =
  21613. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  21614. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  21615. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  21616. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  21617. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  21618. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  21619. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  21620. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  21621. vec.msgLen = 128;
  21622. #endif
  21623. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  21624. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  21625. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  21626. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  21627. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  21628. vec.curveName = "SECP384R1";
  21629. #ifndef NO_ASN
  21630. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  21631. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  21632. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  21633. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  21634. vec.rSz = 48;
  21635. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  21636. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  21637. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  21638. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  21639. vec.sSz = 48;
  21640. #endif
  21641. break;
  21642. #endif /* HAVE_ECC384 */
  21643. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  21644. case 64:
  21645. return 0;
  21646. #endif /* HAVE_ECC512 */
  21647. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  21648. case 66:
  21649. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  21650. #if 1
  21651. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  21652. vec.msgLen = 20;
  21653. #else
  21654. /* This is the raw message prior to SHA-1 */
  21655. vec.msg =
  21656. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  21657. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  21658. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  21659. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  21660. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  21661. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  21662. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  21663. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  21664. vec.msgLen = 128;
  21665. #endif
  21666. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  21667. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  21668. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  21669. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  21670. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  21671. vec.curveName = "SECP521R1";
  21672. #ifndef NO_ASN
  21673. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  21674. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  21675. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  21676. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  21677. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  21678. "\xa3\xbb\x15\x21\xbe";
  21679. vec.rSz = 65;
  21680. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  21681. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  21682. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  21683. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  21684. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  21685. "\x3d\x22\xf2\x48\x0c";
  21686. vec.sSz = 65;
  21687. #endif
  21688. break;
  21689. #endif /* HAVE_ECC521 */
  21690. default:
  21691. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  21692. }; /* Switch */
  21693. ret = ecc_test_vector_item(&vec);
  21694. if (ret < 0) {
  21695. return ret;
  21696. }
  21697. return 0;
  21698. }
  21699. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21700. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  21701. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  21702. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  21703. #if defined(HAVE_ECC256)
  21704. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  21705. {
  21706. wc_test_ret_t ret;
  21707. #ifdef WOLFSSL_SMALL_STACK
  21708. ecc_key *key = NULL;
  21709. #else
  21710. ecc_key key[1];
  21711. #endif
  21712. int key_inited = 0;
  21713. byte sig[72];
  21714. word32 sigSz;
  21715. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21716. unsigned char hash[32];
  21717. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21718. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  21719. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  21720. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  21721. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  21722. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  21723. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  21724. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  21725. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  21726. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  21727. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  21728. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  21729. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  21730. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  21731. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  21732. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  21733. };
  21734. #ifdef WOLFSSL_SMALL_STACK
  21735. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21736. if (key == NULL)
  21737. return MEMORY_E;
  21738. #endif
  21739. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  21740. if (ret != 0) {
  21741. goto done;
  21742. }
  21743. key_inited = 1;
  21744. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  21745. if (ret != 0) {
  21746. goto done;
  21747. }
  21748. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  21749. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  21750. if (ret != 0) {
  21751. goto done;
  21752. }
  21753. ret = wc_ecc_set_deterministic(key, 1);
  21754. if (ret != 0) {
  21755. goto done;
  21756. }
  21757. sigSz = sizeof(sig);
  21758. do {
  21759. #if defined(WOLFSSL_ASYNC_CRYPT)
  21760. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21761. #endif
  21762. if (ret == 0)
  21763. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  21764. } while (ret == WC_PENDING_E);
  21765. if (ret != 0) {
  21766. goto done;
  21767. }
  21768. TEST_SLEEP();
  21769. if (sigSz != sizeof(expSig)) {
  21770. ret = WC_TEST_RET_ENC_NC;
  21771. goto done;
  21772. }
  21773. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  21774. ret = WC_TEST_RET_ENC_NC;
  21775. goto done;
  21776. }
  21777. sigSz = sizeof(sig);
  21778. do {
  21779. #if defined(WOLFSSL_ASYNC_CRYPT)
  21780. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21781. #endif
  21782. if (ret == 0)
  21783. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  21784. } while (ret == WC_PENDING_E);
  21785. if (ret != 0) {
  21786. goto done;
  21787. }
  21788. TEST_SLEEP();
  21789. done:
  21790. if (key_inited)
  21791. wc_ecc_free(key);
  21792. #ifdef WOLFSSL_SMALL_STACK
  21793. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21794. #endif
  21795. return ret;
  21796. }
  21797. #endif
  21798. #ifdef WOLFSSL_PUBLIC_MP
  21799. #if defined(HAVE_ECC384)
  21800. /* KAT from RFC6979 */
  21801. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  21802. {
  21803. wc_test_ret_t ret;
  21804. #ifdef WOLFSSL_SMALL_STACK
  21805. ecc_key *key;
  21806. mp_int *r, *s, *expR, *expS;
  21807. #else
  21808. ecc_key key[1];
  21809. mp_int r[1], s[1], expR[1], expS[1];
  21810. #endif
  21811. int key_inited = 0;
  21812. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21813. unsigned char hash[32];
  21814. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21815. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  21816. "96D5724E4C70A825F872C9EA60D2EDF5";
  21817. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  21818. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  21819. "DEF8F0EA9055866064A254515480BC13";
  21820. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  21821. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  21822. "288B231C3AE0D4FE7344FD2533264720";
  21823. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  21824. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  21825. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  21826. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  21827. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  21828. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  21829. #ifdef WOLFSSL_SMALL_STACK
  21830. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21831. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21832. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21833. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21834. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21835. if ((key == NULL) ||
  21836. (r == NULL) ||
  21837. (s == NULL) ||
  21838. (expR == NULL) ||
  21839. (expS == NULL))
  21840. {
  21841. ret = MEMORY_E;
  21842. goto done;
  21843. }
  21844. #endif
  21845. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  21846. if (ret != MP_OKAY) {
  21847. goto done;
  21848. }
  21849. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  21850. if (ret != 0) {
  21851. goto done;
  21852. }
  21853. key_inited = 1;
  21854. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  21855. if (ret != 0) {
  21856. goto done;
  21857. }
  21858. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  21859. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  21860. if (ret != 0) {
  21861. goto done;
  21862. }
  21863. ret = wc_ecc_set_deterministic(key, 1);
  21864. if (ret != 0) {
  21865. goto done;
  21866. }
  21867. do {
  21868. #if defined(WOLFSSL_ASYNC_CRYPT)
  21869. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21870. #endif
  21871. if (ret == 0)
  21872. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  21873. } while (ret == WC_PENDING_E);
  21874. if (ret != 0) {
  21875. goto done;
  21876. }
  21877. TEST_SLEEP();
  21878. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  21879. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  21880. if (mp_cmp(r, expR) != MP_EQ) {
  21881. ret = WC_TEST_RET_ENC_NC;
  21882. }
  21883. done:
  21884. if (key_inited)
  21885. wc_ecc_free(key);
  21886. #ifdef WOLFSSL_SMALL_STACK
  21887. if (key != NULL)
  21888. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21889. if (r != NULL)
  21890. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21891. if (s != NULL)
  21892. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21893. if (expR != NULL)
  21894. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21895. if (expS != NULL)
  21896. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21897. #endif
  21898. return ret;
  21899. }
  21900. #endif /* HAVE_ECC384 */
  21901. #if defined(HAVE_ECC521)
  21902. /* KAT from RFC6979 */
  21903. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  21904. {
  21905. wc_test_ret_t ret;
  21906. #ifdef WOLFSSL_SMALL_STACK
  21907. ecc_key *key;
  21908. mp_int *r, *s, *expR, *expS;
  21909. #else
  21910. ecc_key key[1];
  21911. mp_int r[1], s[1], expR[1], expS[1];
  21912. #endif
  21913. int key_inited = 0;
  21914. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21915. unsigned char hash[32];
  21916. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21917. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  21918. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  21919. "538";
  21920. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  21921. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  21922. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  21923. "3A4";
  21924. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  21925. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  21926. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  21927. "CF5";
  21928. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  21929. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  21930. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  21931. "1A7";
  21932. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  21933. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  21934. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  21935. "CFC";
  21936. #ifdef WOLFSSL_SMALL_STACK
  21937. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21938. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21939. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21940. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21941. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21942. if ((key == NULL) ||
  21943. (r == NULL) ||
  21944. (s == NULL) ||
  21945. (expR == NULL) ||
  21946. (expS == NULL))
  21947. {
  21948. ret = MEMORY_E;
  21949. goto done;
  21950. }
  21951. #endif
  21952. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  21953. if (ret != MP_OKAY) {
  21954. goto done;
  21955. }
  21956. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  21957. if (ret != 0) {
  21958. return ret;
  21959. }
  21960. key_inited = 1;
  21961. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  21962. if (ret != 0) {
  21963. goto done;
  21964. }
  21965. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  21966. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  21967. if (ret != 0) {
  21968. goto done;
  21969. }
  21970. ret = wc_ecc_set_deterministic(key, 1);
  21971. if (ret != 0) {
  21972. goto done;
  21973. }
  21974. do {
  21975. #if defined(WOLFSSL_ASYNC_CRYPT)
  21976. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21977. #endif
  21978. if (ret == 0)
  21979. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  21980. } while (ret == WC_PENDING_E);
  21981. if (ret != 0) {
  21982. goto done;
  21983. }
  21984. TEST_SLEEP();
  21985. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  21986. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  21987. if (mp_cmp(r, expR) != MP_EQ) {
  21988. ret = WC_TEST_RET_ENC_NC;
  21989. }
  21990. done:
  21991. if (key_inited)
  21992. wc_ecc_free(key);
  21993. #ifdef WOLFSSL_SMALL_STACK
  21994. if (key != NULL)
  21995. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21996. if (r != NULL)
  21997. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21998. if (s != NULL)
  21999. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22000. if (expR != NULL)
  22001. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22002. if (expS != NULL)
  22003. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22004. #endif
  22005. return ret;
  22006. }
  22007. #endif /* HAVE_ECC521 */
  22008. #endif /* WOLFSSL_PUBLIC_MP */
  22009. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  22010. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  22011. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  22012. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  22013. !defined(WOLFSSL_KCAPI_ECC)
  22014. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  22015. {
  22016. wc_test_ret_t ret;
  22017. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22018. ecc_key *key = NULL;
  22019. #else
  22020. ecc_key key[1];
  22021. #endif
  22022. int key_inited = 0;
  22023. byte sig[72];
  22024. word32 sigSz;
  22025. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  22026. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  22027. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  22028. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  22029. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  22030. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  22031. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  22032. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  22033. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  22034. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  22035. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  22036. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  22037. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  22038. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  22039. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  22040. };
  22041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22042. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  22043. return MEMORY_E;
  22044. #endif
  22045. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22046. if (ret != 0) {
  22047. goto done;
  22048. }
  22049. key_inited = 1;
  22050. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  22051. if (ret != 0) {
  22052. goto done;
  22053. }
  22054. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  22055. && (HAVE_FIPS_VERSION > 2)))
  22056. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  22057. #endif
  22058. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  22059. if (ret != 0) {
  22060. goto done;
  22061. }
  22062. sigSz = sizeof(sig);
  22063. do {
  22064. #if defined(WOLFSSL_ASYNC_CRYPT)
  22065. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22066. #endif
  22067. if (ret == 0)
  22068. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  22069. } while (ret == WC_PENDING_E);
  22070. if (ret != 0) {
  22071. goto done;
  22072. }
  22073. TEST_SLEEP();
  22074. if (sigSz != sizeof(expSig)) {
  22075. ret = WC_TEST_RET_ENC_NC;
  22076. goto done;
  22077. }
  22078. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  22079. ret = WC_TEST_RET_ENC_NC;
  22080. goto done;
  22081. }
  22082. sigSz = sizeof(sig);
  22083. do {
  22084. #if defined(WOLFSSL_ASYNC_CRYPT)
  22085. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22086. #endif
  22087. if (ret == 0)
  22088. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  22089. } while (ret == WC_PENDING_E);
  22090. if (ret != 0) {
  22091. goto done;
  22092. }
  22093. TEST_SLEEP();
  22094. done:
  22095. if (key_inited)
  22096. wc_ecc_free(key);
  22097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22098. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22099. #endif
  22100. return ret;
  22101. }
  22102. #endif
  22103. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  22104. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  22105. {
  22106. wc_test_ret_t ret;
  22107. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22108. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22109. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22110. #else
  22111. ecc_key pub_key[1], priv_key[1];
  22112. #endif
  22113. byte sharedA[32] = {0}, sharedB[32] = {0};
  22114. word32 x, z;
  22115. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  22116. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  22117. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  22118. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  22119. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  22120. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  22121. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22122. if ((pub_key == NULL) ||
  22123. (priv_key == NULL)) {
  22124. ret = MEMORY_E;
  22125. goto done;
  22126. }
  22127. #endif
  22128. XMEMSET(pub_key, 0, sizeof *pub_key);
  22129. XMEMSET(priv_key, 0, sizeof *priv_key);
  22130. /* setup private and public keys */
  22131. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  22132. if (ret != 0)
  22133. goto done;
  22134. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  22135. if (ret != 0)
  22136. goto done;
  22137. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  22138. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  22139. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  22140. if (ret != 0)
  22141. goto done;
  22142. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  22143. if (ret != 0)
  22144. goto done;
  22145. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22146. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22147. !defined(HAVE_SELFTEST)
  22148. ret = wc_ecc_set_rng(priv_key, rng);
  22149. if (ret != 0)
  22150. goto done;
  22151. #else
  22152. (void)rng;
  22153. #endif
  22154. /* compute ECC Cofactor shared secret */
  22155. x = sizeof(sharedA);
  22156. do {
  22157. #if defined(WOLFSSL_ASYNC_CRYPT)
  22158. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22159. #endif
  22160. if (ret == 0)
  22161. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  22162. } while (ret == WC_PENDING_E);
  22163. if (ret != 0) {
  22164. goto done;
  22165. }
  22166. TEST_SLEEP();
  22167. /* read in expected Z */
  22168. z = sizeof(sharedB);
  22169. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  22170. if (ret != 0)
  22171. goto done;
  22172. /* compare results */
  22173. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  22174. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22175. }
  22176. done:
  22177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22178. if (priv_key) {
  22179. wc_ecc_free(priv_key);
  22180. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22181. }
  22182. if (pub_key) {
  22183. wc_ecc_free(pub_key);
  22184. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22185. }
  22186. #else
  22187. wc_ecc_free(priv_key);
  22188. wc_ecc_free(pub_key);
  22189. #endif
  22190. return ret;
  22191. }
  22192. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  22193. #endif /* HAVE_ECC_VECTOR_TEST */
  22194. #ifdef HAVE_ECC_KEY_IMPORT
  22195. /* returns 0 on success */
  22196. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  22197. {
  22198. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22199. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22200. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22201. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22202. #endif
  22203. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22204. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22205. #else
  22206. ecc_key key[1];
  22207. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  22208. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22209. ecc_key pub[1];
  22210. #endif
  22211. byte exportBuf[ECC_BUFSIZE];
  22212. byte tmp[ECC_BUFSIZE];
  22213. #endif
  22214. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  22215. word32 x;
  22216. word32 tmpSz;
  22217. wc_test_ret_t ret = 0;
  22218. ecc_point* pubPoint = NULL;
  22219. #ifdef HAVE_ECC_VERIFY
  22220. int verify = 0;
  22221. #endif
  22222. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22223. if ((key == NULL) ||
  22224. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22225. (pub == NULL) ||
  22226. #endif
  22227. (exportBuf == NULL) ||
  22228. (tmp == NULL))
  22229. ERROR_OUT(MEMORY_E, done);
  22230. #endif
  22231. (void)msg;
  22232. (void)verify;
  22233. (void)exportBuf;
  22234. (void)rng;
  22235. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22236. #ifndef NO_ECC256
  22237. #ifdef USE_CERT_BUFFERS_256
  22238. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  22239. tmpSz = (size_t)sizeof_ecc_key_der_256;
  22240. #else
  22241. {
  22242. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22243. if (!file) {
  22244. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22245. }
  22246. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  22247. XFCLOSE(file);
  22248. if (tmpSz == 0)
  22249. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22250. }
  22251. #endif /* USE_CERT_BUFFERS_256 */
  22252. /* import private only then test with */
  22253. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  22254. if (ret == 0) {
  22255. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22256. }
  22257. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  22258. if (ret == 0) {
  22259. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22260. }
  22261. x = 0;
  22262. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  22263. if (ret != 0)
  22264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22265. #ifdef HAVE_ECC_KEY_EXPORT
  22266. x = ECC_BUFSIZE;
  22267. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  22268. if (ret != 0)
  22269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22270. /* make private only key */
  22271. wc_ecc_free(key);
  22272. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22273. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  22274. if (ret != 0)
  22275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22276. x = ECC_BUFSIZE;
  22277. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22278. if (ret == 0) {
  22279. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22280. }
  22281. #endif /* HAVE_ECC_KEY_EXPORT */
  22282. ret = wc_ecc_make_pub(NULL, NULL);
  22283. if (ret == 0) {
  22284. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22285. }
  22286. TEST_SLEEP();
  22287. #ifndef WOLFSSL_NO_MALLOC
  22288. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  22289. if (pubPoint == NULL) {
  22290. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22291. }
  22292. #if !defined(WOLFSSL_CRYPTOCELL)
  22293. ret = wc_ecc_make_pub(key, pubPoint);
  22294. #if defined(WOLFSSL_ASYNC_CRYPT)
  22295. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22296. #endif
  22297. if (ret != 0)
  22298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22299. #endif
  22300. TEST_SLEEP();
  22301. #ifdef HAVE_ECC_KEY_EXPORT
  22302. /* export should still fail, is private only key */
  22303. x = ECC_BUFSIZE;
  22304. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22305. if (ret == 0) {
  22306. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22307. }
  22308. #endif /* HAVE_ECC_KEY_EXPORT */
  22309. #endif /* !WOLFSSL_NO_MALLOC */
  22310. #endif /* !NO_ECC256 */
  22311. /* create a new key since above test for loading key is not supported */
  22312. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  22313. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  22314. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  22315. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22316. if (ret != 0)
  22317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22318. #endif
  22319. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  22320. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  22321. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  22322. tmpSz = ECC_BUFSIZE;
  22323. ret = 0;
  22324. do {
  22325. #if defined(WOLFSSL_ASYNC_CRYPT)
  22326. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22327. #endif
  22328. if (ret == 0) {
  22329. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  22330. &tmpSz, rng, key);
  22331. }
  22332. } while (ret == WC_PENDING_E);
  22333. if (ret != 0)
  22334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22335. TEST_SLEEP();
  22336. #ifdef HAVE_ECC_VERIFY
  22337. /* try verify with private only key */
  22338. ret = 0;
  22339. do {
  22340. #if defined(WOLFSSL_ASYNC_CRYPT)
  22341. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22342. #endif
  22343. if (ret == 0) {
  22344. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  22345. (word32)XSTRLEN((const char*)msg), &verify, key);
  22346. }
  22347. } while (ret == WC_PENDING_E);
  22348. if (ret != 0)
  22349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22350. if (verify != 1) {
  22351. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22352. }
  22353. TEST_SLEEP();
  22354. #ifdef HAVE_ECC_KEY_EXPORT
  22355. /* exporting the public part should now work */
  22356. x = ECC_BUFSIZE;
  22357. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22358. if (ret != 0)
  22359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22360. #endif /* HAVE_ECC_KEY_EXPORT */
  22361. #endif /* HAVE_ECC_VERIFY */
  22362. #endif /* HAVE_ECC_SIGN */
  22363. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22364. /* now test private only key with creating a shared secret */
  22365. x = ECC_BUFSIZE;
  22366. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  22367. if (ret != 0)
  22368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22369. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  22370. /* make private only key */
  22371. wc_ecc_free(key);
  22372. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22373. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  22374. if (ret != 0)
  22375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22376. /* check that public export fails with private only key */
  22377. x = ECC_BUFSIZE;
  22378. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22379. if (ret == 0) {
  22380. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22381. }
  22382. #endif /* WOLFSSL_QNX_CAAM */
  22383. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22384. /* make public key for shared secret */
  22385. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  22386. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  22387. #ifdef HAVE_ECC_CDH
  22388. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  22389. #endif
  22390. #if defined(WOLFSSL_ASYNC_CRYPT)
  22391. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  22392. #endif
  22393. if (ret != 0)
  22394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22395. TEST_SLEEP();
  22396. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22397. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22398. !defined(HAVE_SELFTEST)
  22399. ret = wc_ecc_set_rng(key, rng);
  22400. if (ret != 0)
  22401. goto done;
  22402. #endif
  22403. x = ECC_BUFSIZE;
  22404. do {
  22405. #if defined(WOLFSSL_ASYNC_CRYPT)
  22406. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22407. #endif
  22408. if (ret == 0) {
  22409. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  22410. }
  22411. } while (ret == WC_PENDING_E);
  22412. wc_ecc_free(pub);
  22413. if (ret != 0)
  22414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22415. TEST_SLEEP();
  22416. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  22417. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22418. ret = 0;
  22419. done:
  22420. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  22421. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22422. if (key != NULL) {
  22423. wc_ecc_free(key);
  22424. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22425. }
  22426. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22427. if (pub != NULL)
  22428. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22429. #endif
  22430. if (exportBuf != NULL)
  22431. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22432. if (tmp != NULL)
  22433. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22434. #else
  22435. wc_ecc_free(key);
  22436. #endif
  22437. return ret;
  22438. }
  22439. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  22440. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22441. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  22442. {
  22443. wc_test_ret_t ret;
  22444. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22445. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22446. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22447. #else
  22448. ecc_key eccKey[1];
  22449. byte tmpBuf[ECC_BUFSIZE];
  22450. #endif
  22451. word32 tmpSz;
  22452. word32 idx;
  22453. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22454. if ((eccKey == NULL) || (tmpBuf == NULL))
  22455. ERROR_OUT(MEMORY_E, done);
  22456. #endif
  22457. ret = wc_ecc_init(eccKey);
  22458. if (ret != 0) {
  22459. goto done;
  22460. }
  22461. ret = wc_ecc_make_key(rng, keySize, eccKey);
  22462. #if defined(WOLFSSL_ASYNC_CRYPT)
  22463. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22464. #endif
  22465. if (ret != 0) {
  22466. goto done;
  22467. }
  22468. tmpSz = ECC_BUFSIZE;
  22469. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  22470. wc_ecc_free(eccKey);
  22471. if (ret < 0) {
  22472. goto done;
  22473. }
  22474. tmpSz = (word32)ret;
  22475. ret = wc_ecc_init(eccKey);
  22476. if (ret != 0) {
  22477. goto done;
  22478. }
  22479. idx = 0;
  22480. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  22481. if (ret != 0) {
  22482. goto done;
  22483. }
  22484. wc_ecc_free(eccKey);
  22485. ret = wc_ecc_init(eccKey);
  22486. if (ret != 0) {
  22487. goto done;
  22488. }
  22489. idx = 0;
  22490. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  22491. if (ret != 0) {
  22492. goto done;
  22493. }
  22494. ret = 0;
  22495. done:
  22496. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22497. if (eccKey != NULL) {
  22498. wc_ecc_free(eccKey);
  22499. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22500. }
  22501. if (tmpBuf != NULL)
  22502. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22503. #else
  22504. wc_ecc_free(eccKey);
  22505. #endif
  22506. return ret;
  22507. }
  22508. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  22509. #endif /* HAVE_ECC_KEY_IMPORT */
  22510. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  22511. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22512. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  22513. {
  22514. wc_test_ret_t ret = 0;
  22515. int derSz;
  22516. #ifdef HAVE_PKCS8
  22517. word32 pkcs8Sz;
  22518. #endif
  22519. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22520. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22521. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22522. #else
  22523. byte der[ECC_BUFSIZE];
  22524. ecc_key userA[1];
  22525. #endif
  22526. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22527. if ((der == NULL) || (userA == NULL))
  22528. ERROR_OUT(MEMORY_E, done);
  22529. #endif
  22530. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  22531. if (ret != 0)
  22532. goto done;
  22533. ret = wc_ecc_make_key(rng, keySize, userA);
  22534. #if defined(WOLFSSL_ASYNC_CRYPT)
  22535. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  22536. #endif
  22537. if (ret != 0)
  22538. goto done;
  22539. TEST_SLEEP();
  22540. ret = wc_ecc_check_key(userA);
  22541. if (ret != 0)
  22542. goto done;
  22543. TEST_SLEEP();
  22544. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  22545. if (derSz < 0) {
  22546. ERROR_OUT(derSz, done);
  22547. }
  22548. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  22549. ECC_PRIVATEKEY_TYPE);
  22550. if (ret != 0) {
  22551. goto done;
  22552. }
  22553. /* test export of public key */
  22554. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  22555. if (derSz < 0) {
  22556. ERROR_OUT(derSz, done);
  22557. }
  22558. if (derSz == 0) {
  22559. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22560. }
  22561. #ifdef HAVE_COMP_KEY
  22562. /* test export of compressed public key */
  22563. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  22564. if (derSz < 0) {
  22565. ERROR_OUT(derSz, done);
  22566. }
  22567. if (derSz == 0) {
  22568. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22569. }
  22570. #endif
  22571. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  22572. if (ret != 0) {
  22573. goto done;
  22574. }
  22575. #ifdef HAVE_PKCS8
  22576. /* test export of PKCS#8 unencrypted private key */
  22577. pkcs8Sz = FOURK_BUF;
  22578. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  22579. if (derSz < 0) {
  22580. ERROR_OUT(derSz, done);
  22581. }
  22582. if (derSz == 0) {
  22583. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22584. }
  22585. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  22586. if (ret != 0) {
  22587. goto done;
  22588. }
  22589. #endif /* HAVE_PKCS8 */
  22590. done:
  22591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22592. if (der != NULL)
  22593. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22594. if (userA != NULL) {
  22595. wc_ecc_free(userA);
  22596. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22597. }
  22598. #else
  22599. wc_ecc_free(userA);
  22600. #endif
  22601. return ret;
  22602. }
  22603. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  22604. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  22605. int curve_id, const ecc_set_type* dp)
  22606. {
  22607. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22608. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22609. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  22610. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  22611. word32 y;
  22612. #endif
  22613. #ifdef HAVE_ECC_KEY_EXPORT
  22614. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  22615. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  22616. #endif
  22617. word32 x = 0;
  22618. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22619. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  22620. defined(HAVE_ECC_SIGN)
  22621. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  22622. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  22623. int i;
  22624. #ifdef HAVE_ECC_VERIFY
  22625. int verify;
  22626. #endif /* HAVE_ECC_VERIFY */
  22627. #endif /* HAVE_ECC_SIGN */
  22628. wc_test_ret_t ret;
  22629. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22630. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22631. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22632. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22633. #else
  22634. ecc_key userA[1];
  22635. ecc_key userB[1];
  22636. ecc_key pubKey[1];
  22637. #endif
  22638. #ifndef WC_NO_RNG
  22639. int curveSize;
  22640. #endif
  22641. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  22642. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  22643. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22644. if (sharedA == NULL || sharedB == NULL)
  22645. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22646. #endif
  22647. #ifdef HAVE_ECC_KEY_EXPORT
  22648. if (exportBuf == NULL)
  22649. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22650. #endif
  22651. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22652. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  22653. defined(HAVE_ECC_SIGN)
  22654. if (sig == NULL || digest == NULL)
  22655. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22656. #endif
  22657. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22658. (void)testVerifyCount;
  22659. (void)dp;
  22660. (void)x;
  22661. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22662. if ((userA == NULL) ||
  22663. (userB == NULL) ||
  22664. (pubKey == NULL))
  22665. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22666. #endif
  22667. XMEMSET(userA, 0, sizeof *userA);
  22668. XMEMSET(userB, 0, sizeof *userB);
  22669. XMEMSET(pubKey, 0, sizeof *pubKey);
  22670. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  22671. if (ret != 0)
  22672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22673. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  22674. if (ret != 0)
  22675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22676. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  22677. if (ret != 0)
  22678. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22679. #ifdef WOLFSSL_CUSTOM_CURVES
  22680. if (dp != NULL) {
  22681. ret = wc_ecc_set_custom_curve(userA, dp);
  22682. if (ret != 0)
  22683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22684. ret = wc_ecc_set_custom_curve(userB, dp);
  22685. if (ret != 0)
  22686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22687. }
  22688. #endif
  22689. #ifndef WC_NO_RNG
  22690. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  22691. #if defined(WOLFSSL_ASYNC_CRYPT)
  22692. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  22693. #endif
  22694. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  22695. if (ret == NO_VALID_DEVID) {
  22696. ret = 0;
  22697. goto done; /* no software case */
  22698. }
  22699. #endif
  22700. if (ret == ECC_CURVE_OID_E)
  22701. goto done; /* catch case, where curve is not supported */
  22702. if (ret != 0)
  22703. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22704. TEST_SLEEP();
  22705. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  22706. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  22707. if (curveSize != userA->dp->size)
  22708. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22709. }
  22710. ret = wc_ecc_check_key(userA);
  22711. if (ret != 0)
  22712. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22713. TEST_SLEEP();
  22714. /* ATECC508/608 configuration may not support more than one ECDH key */
  22715. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22716. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  22717. #if defined(WOLFSSL_ASYNC_CRYPT)
  22718. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  22719. #endif
  22720. if (ret != 0)
  22721. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22722. TEST_SLEEP();
  22723. /* only perform the below tests if the key size matches */
  22724. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  22725. ERROR_OUT(ECC_CURVE_OID_E, done);
  22726. #ifdef HAVE_ECC_DHE
  22727. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22728. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22729. !defined(HAVE_SELFTEST)
  22730. ret = wc_ecc_set_rng(userA, rng);
  22731. if (ret != 0)
  22732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22733. ret = wc_ecc_set_rng(userB, rng);
  22734. if (ret != 0)
  22735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22736. #endif
  22737. x = ECC_SHARED_SIZE;
  22738. do {
  22739. #if defined(WOLFSSL_ASYNC_CRYPT)
  22740. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22741. #endif
  22742. if (ret == 0)
  22743. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  22744. } while (ret == WC_PENDING_E);
  22745. if (ret != 0)
  22746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22747. TEST_SLEEP();
  22748. y = ECC_SHARED_SIZE;
  22749. do {
  22750. #if defined(WOLFSSL_ASYNC_CRYPT)
  22751. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22752. #endif
  22753. if (ret == 0)
  22754. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  22755. } while (ret == WC_PENDING_E);
  22756. if (ret != 0)
  22757. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22758. if (y != x)
  22759. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22760. if (XMEMCMP(sharedA, sharedB, x))
  22761. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22762. TEST_SLEEP();
  22763. #ifdef HAVE_ECC_CDH
  22764. /* add cofactor flag */
  22765. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  22766. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  22767. x = ECC_SHARED_SIZE;
  22768. do {
  22769. #if defined(WOLFSSL_ASYNC_CRYPT)
  22770. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22771. #endif
  22772. if (ret == 0)
  22773. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  22774. } while (ret == WC_PENDING_E);
  22775. if (ret != 0)
  22776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22777. TEST_SLEEP();
  22778. y = ECC_SHARED_SIZE;
  22779. do {
  22780. #if defined(WOLFSSL_ASYNC_CRYPT)
  22781. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22782. #endif
  22783. if (ret == 0)
  22784. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  22785. } while (ret == WC_PENDING_E);
  22786. if (ret != 0)
  22787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22788. if (y != x)
  22789. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22790. if (XMEMCMP(sharedA, sharedB, x))
  22791. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22792. TEST_SLEEP();
  22793. /* remove cofactor flag */
  22794. wc_ecc_set_flags(userA, 0);
  22795. wc_ecc_set_flags(userB, 0);
  22796. #endif /* HAVE_ECC_CDH */
  22797. #endif /* HAVE_ECC_DHE */
  22798. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  22799. #ifdef HAVE_ECC_KEY_EXPORT
  22800. x = ECC_KEY_EXPORT_BUF_SIZE;
  22801. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  22802. if (ret != 0)
  22803. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22804. #ifdef HAVE_ECC_KEY_IMPORT
  22805. #ifdef WOLFSSL_CUSTOM_CURVES
  22806. if (dp != NULL) {
  22807. ret = wc_ecc_set_custom_curve(pubKey, dp);
  22808. if (ret != 0)
  22809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22810. }
  22811. #endif
  22812. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  22813. if (ret != 0)
  22814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22815. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22816. #ifdef HAVE_ECC_DHE
  22817. y = ECC_SHARED_SIZE;
  22818. do {
  22819. #if defined(WOLFSSL_ASYNC_CRYPT)
  22820. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22821. #endif
  22822. if (ret == 0)
  22823. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  22824. } while (ret == WC_PENDING_E);
  22825. if (ret != 0)
  22826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22827. if (XMEMCMP(sharedA, sharedB, y))
  22828. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22829. TEST_SLEEP();
  22830. #endif /* HAVE_ECC_DHE */
  22831. #ifdef HAVE_COMP_KEY
  22832. /* try compressed export / import too */
  22833. x = ECC_KEY_EXPORT_BUF_SIZE;
  22834. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  22835. if (ret != 0)
  22836. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22837. wc_ecc_free(pubKey);
  22838. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  22839. if (ret != 0)
  22840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22841. #ifdef WOLFSSL_CUSTOM_CURVES
  22842. if (dp != NULL) {
  22843. ret = wc_ecc_set_custom_curve(pubKey, dp);
  22844. if (ret != 0)
  22845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22846. }
  22847. #endif
  22848. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  22849. if (ret != 0)
  22850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22851. #ifdef HAVE_ECC_DHE
  22852. y = ECC_SHARED_SIZE;
  22853. do {
  22854. #if defined(WOLFSSL_ASYNC_CRYPT)
  22855. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22856. #endif
  22857. if (ret == 0)
  22858. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  22859. } while (ret == WC_PENDING_E);
  22860. if (ret != 0)
  22861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22862. if (XMEMCMP(sharedA, sharedB, y))
  22863. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22864. TEST_SLEEP();
  22865. #endif /* HAVE_ECC_DHE */
  22866. #endif /* HAVE_COMP_KEY */
  22867. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  22868. #endif /* !WC_NO_RNG */
  22869. #endif /* HAVE_ECC_KEY_IMPORT */
  22870. #endif /* HAVE_ECC_KEY_EXPORT */
  22871. /* For KCAPI cannot sign using generated ECDH key */
  22872. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22873. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  22874. #ifdef HAVE_ECC_SIGN
  22875. /* ECC w/out Shamir has issue with all 0 digest */
  22876. /* WC_BIGINT doesn't have 0 len well on hardware */
  22877. /* Cryptocell has issues with all 0 digest */
  22878. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  22879. !defined(WOLFSSL_CRYPTOCELL)
  22880. /* test DSA sign hash with zeros */
  22881. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  22882. digest[i] = 0;
  22883. }
  22884. x = ECC_SIG_SIZE;
  22885. do {
  22886. #if defined(WOLFSSL_ASYNC_CRYPT)
  22887. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22888. #endif
  22889. if (ret == 0)
  22890. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  22891. userA);
  22892. } while (ret == WC_PENDING_E);
  22893. if (ret != 0)
  22894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22895. TEST_SLEEP();
  22896. #ifdef HAVE_ECC_VERIFY
  22897. for (i=0; i<testVerifyCount; i++) {
  22898. verify = 0;
  22899. do {
  22900. #if defined(WOLFSSL_ASYNC_CRYPT)
  22901. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22902. #endif
  22903. if (ret == 0)
  22904. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  22905. &verify, userA);
  22906. } while (ret == WC_PENDING_E);
  22907. if (ret != 0)
  22908. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22909. if (verify != 1)
  22910. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22911. TEST_SLEEP();
  22912. }
  22913. #endif /* HAVE_ECC_VERIFY */
  22914. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  22915. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  22916. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  22917. digest[i] = (byte)i;
  22918. }
  22919. x = ECC_SIG_SIZE;
  22920. do {
  22921. #if defined(WOLFSSL_ASYNC_CRYPT)
  22922. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22923. #endif
  22924. if (ret == 0)
  22925. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  22926. } while (ret == WC_PENDING_E);
  22927. if (ret != 0)
  22928. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22929. TEST_SLEEP();
  22930. #ifdef HAVE_ECC_VERIFY
  22931. for (i=0; i<testVerifyCount; i++) {
  22932. verify = 0;
  22933. do {
  22934. #if defined(WOLFSSL_ASYNC_CRYPT)
  22935. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22936. #endif
  22937. if (ret == 0)
  22938. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  22939. } while (ret == WC_PENDING_E);
  22940. if (ret != 0)
  22941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22942. if (verify != 1)
  22943. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22944. TEST_SLEEP();
  22945. }
  22946. #endif /* HAVE_ECC_VERIFY */
  22947. #endif /* HAVE_ECC_SIGN */
  22948. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  22949. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  22950. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  22951. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  22952. !defined(WOLFSSL_KCAPI_ECC)
  22953. x = ECC_KEY_EXPORT_BUF_SIZE;
  22954. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  22955. if (ret != 0)
  22956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22957. #elif defined(HAVE_ECC_KEY_EXPORT)
  22958. (void)exportBuf;
  22959. #endif /* HAVE_ECC_KEY_EXPORT */
  22960. done:
  22961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22962. if (userA != NULL) {
  22963. wc_ecc_free(userA);
  22964. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22965. }
  22966. if (userB != NULL) {
  22967. wc_ecc_free(userB);
  22968. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22969. }
  22970. if (pubKey != NULL) {
  22971. wc_ecc_free(pubKey);
  22972. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22973. }
  22974. #else
  22975. wc_ecc_free(pubKey);
  22976. wc_ecc_free(userB);
  22977. wc_ecc_free(userA);
  22978. #endif
  22979. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  22980. WC_FREE_VAR(sharedA, HEAP_HINT);
  22981. WC_FREE_VAR(sharedB, HEAP_HINT);
  22982. #endif
  22983. #ifdef HAVE_ECC_KEY_EXPORT
  22984. WC_FREE_VAR(exportBuf, HEAP_HINT);
  22985. #endif
  22986. #ifdef HAVE_ECC_SIGN
  22987. WC_FREE_VAR(sig, HEAP_HINT);
  22988. WC_FREE_VAR(digest, HEAP_HINT);
  22989. #endif
  22990. (void)keySize;
  22991. (void)curve_id;
  22992. (void)rng;
  22993. return ret;
  22994. }
  22995. #undef ECC_TEST_VERIFY_COUNT
  22996. #define ECC_TEST_VERIFY_COUNT 2
  22997. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  22998. {
  22999. wc_test_ret_t ret;
  23000. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  23001. NULL);
  23002. if (ret < 0) {
  23003. if (ret == ECC_CURVE_OID_E) {
  23004. /* ignore error for curves not found */
  23005. /* some curve sizes are only available with:
  23006. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  23007. and HAVE_ECC_KOBLITZ */
  23008. }
  23009. else {
  23010. printf("ecc_test_curve_size %d failed!\n", keySize);
  23011. return ret;
  23012. }
  23013. }
  23014. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  23015. #ifdef HAVE_ECC_VECTOR_TEST
  23016. ret = ecc_test_vector(keySize);
  23017. if (ret < 0) {
  23018. printf("ecc_test_vector %d failed!\n", keySize);
  23019. return ret;
  23020. }
  23021. #endif
  23022. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23023. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  23024. ret = ecc_test_key_decode(rng, keySize);
  23025. if (ret < 0) {
  23026. if (ret == ECC_CURVE_OID_E) {
  23027. /* ignore error for curves not found */
  23028. }
  23029. else {
  23030. printf("ecc_test_key_decode %d failed!\n", keySize);
  23031. return ret;
  23032. }
  23033. }
  23034. #endif
  23035. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  23036. ret = ecc_test_key_gen(rng, keySize);
  23037. if (ret < 0) {
  23038. if (ret == ECC_CURVE_OID_E) {
  23039. /* ignore error for curves not found */
  23040. }
  23041. else {
  23042. printf("ecc_test_key_gen %d failed!\n", keySize);
  23043. return ret;
  23044. }
  23045. }
  23046. #endif
  23047. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23048. return 0;
  23049. }
  23050. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23051. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23052. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23053. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23054. static wc_test_ret_t ecc_point_test(void)
  23055. {
  23056. wc_test_ret_t ret;
  23057. ecc_point* point;
  23058. ecc_point* point2;
  23059. #ifdef HAVE_COMP_KEY
  23060. ecc_point* point3;
  23061. ecc_point* point4;
  23062. #endif
  23063. word32 outLen;
  23064. byte out[65];
  23065. byte der[] = { 0x04, /* = Uncompressed */
  23066. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23067. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23068. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23069. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23070. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23071. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23072. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23073. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23074. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  23075. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  23076. byte derComp0[] = { 0x02, /* = Compressed, y even */
  23077. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23078. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23079. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23080. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23081. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  23082. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23083. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23084. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23085. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23086. #endif
  23087. byte altDer[] = { 0x04, /* = Uncompressed */
  23088. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23089. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23090. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23091. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23092. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23093. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23094. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23095. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  23096. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  23097. /* if curve P256 is not enabled then test should not fail */
  23098. if (curve_idx == ECC_CURVE_INVALID)
  23099. return 0;
  23100. outLen = sizeof(out);
  23101. point = wc_ecc_new_point();
  23102. if (point == NULL)
  23103. return WC_TEST_RET_ENC_ERRNO;
  23104. point2 = wc_ecc_new_point();
  23105. if (point2 == NULL) {
  23106. wc_ecc_del_point(point);
  23107. return WC_TEST_RET_ENC_NC;
  23108. }
  23109. #ifdef HAVE_COMP_KEY
  23110. point3 = wc_ecc_new_point();
  23111. if (point3 == NULL) {
  23112. wc_ecc_del_point(point2);
  23113. wc_ecc_del_point(point);
  23114. return WC_TEST_RET_ENC_NC;
  23115. }
  23116. point4 = wc_ecc_new_point();
  23117. if (point4 == NULL) {
  23118. wc_ecc_del_point(point3);
  23119. wc_ecc_del_point(point2);
  23120. wc_ecc_del_point(point);
  23121. return WC_TEST_RET_ENC_NC;
  23122. }
  23123. #endif
  23124. /* Parameter Validation testing. */
  23125. wc_ecc_del_point(NULL);
  23126. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  23127. if (ret != ECC_BAD_ARG_E) {
  23128. ret = WC_TEST_RET_ENC_EC(ret);
  23129. goto done;
  23130. }
  23131. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  23132. if (ret != ECC_BAD_ARG_E) {
  23133. ret = WC_TEST_RET_ENC_EC(ret);
  23134. goto done;
  23135. }
  23136. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  23137. if (ret != ECC_BAD_ARG_E) {
  23138. ret = WC_TEST_RET_ENC_EC(ret);
  23139. goto done;
  23140. }
  23141. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  23142. if (ret != ECC_BAD_ARG_E) {
  23143. ret = WC_TEST_RET_ENC_EC(ret);
  23144. goto done;
  23145. }
  23146. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  23147. if (ret != ECC_BAD_ARG_E) {
  23148. ret = WC_TEST_RET_ENC_EC(ret);
  23149. goto done;
  23150. }
  23151. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  23152. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  23153. ret = WC_TEST_RET_ENC_EC(ret);
  23154. goto done;
  23155. }
  23156. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  23157. if (ret != ECC_BAD_ARG_E) {
  23158. ret = WC_TEST_RET_ENC_EC(ret);
  23159. goto done;
  23160. }
  23161. outLen = 0;
  23162. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  23163. if (ret != BUFFER_E) {
  23164. ret = WC_TEST_RET_ENC_EC(ret);
  23165. goto done;
  23166. }
  23167. ret = wc_ecc_copy_point(NULL, NULL);
  23168. if (ret != ECC_BAD_ARG_E) {
  23169. ret = WC_TEST_RET_ENC_EC(ret);
  23170. goto done;
  23171. }
  23172. ret = wc_ecc_copy_point(NULL, point2);
  23173. if (ret != ECC_BAD_ARG_E) {
  23174. ret = WC_TEST_RET_ENC_EC(ret);
  23175. goto done;
  23176. }
  23177. ret = wc_ecc_copy_point(point, NULL);
  23178. if (ret != ECC_BAD_ARG_E) {
  23179. ret = WC_TEST_RET_ENC_EC(ret);
  23180. goto done;
  23181. }
  23182. ret = wc_ecc_cmp_point(NULL, NULL);
  23183. if (ret != BAD_FUNC_ARG) {
  23184. ret = WC_TEST_RET_ENC_EC(ret);
  23185. goto done;
  23186. }
  23187. ret = wc_ecc_cmp_point(NULL, point2);
  23188. if (ret != BAD_FUNC_ARG) {
  23189. ret = WC_TEST_RET_ENC_EC(ret);
  23190. goto done;
  23191. }
  23192. ret = wc_ecc_cmp_point(point, NULL);
  23193. if (ret != BAD_FUNC_ARG) {
  23194. ret = WC_TEST_RET_ENC_EC(ret);
  23195. goto done;
  23196. }
  23197. /* Use API. */
  23198. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  23199. if (ret != 0) {
  23200. ret = WC_TEST_RET_ENC_EC(ret);
  23201. goto done;
  23202. }
  23203. outLen = sizeof(out);
  23204. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  23205. if (ret != 0) {
  23206. ret = WC_TEST_RET_ENC_EC(ret);
  23207. goto done;
  23208. }
  23209. if (outLen != sizeof(der)) {
  23210. ret = WC_TEST_RET_ENC_NC;
  23211. goto done;
  23212. }
  23213. if (XMEMCMP(out, der, outLen) != 0) {
  23214. ret = WC_TEST_RET_ENC_NC;
  23215. goto done;
  23216. }
  23217. ret = wc_ecc_copy_point(point2, point);
  23218. if (ret != MP_OKAY) {
  23219. ret = WC_TEST_RET_ENC_EC(ret);
  23220. goto done;
  23221. }
  23222. ret = wc_ecc_cmp_point(point2, point);
  23223. if (ret != MP_EQ) {
  23224. ret = WC_TEST_RET_ENC_EC(ret);
  23225. goto done;
  23226. }
  23227. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  23228. if (ret != 0) {
  23229. ret = WC_TEST_RET_ENC_EC(ret);
  23230. goto done;
  23231. }
  23232. ret = wc_ecc_cmp_point(point2, point);
  23233. if (ret != MP_GT) {
  23234. ret = WC_TEST_RET_ENC_EC(ret);
  23235. goto done;
  23236. }
  23237. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  23238. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  23239. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  23240. if (ret != 0) {
  23241. ret = WC_TEST_RET_ENC_EC(ret);
  23242. goto done;
  23243. }
  23244. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  23245. if (ret != 0) {
  23246. ret = WC_TEST_RET_ENC_EC(ret);
  23247. goto done;
  23248. }
  23249. ret = wc_ecc_cmp_point(point3, point4);
  23250. if (ret != MP_EQ) {
  23251. ret = WC_TEST_RET_ENC_EC(ret);
  23252. goto done;
  23253. }
  23254. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  23255. if (ret != 0) {
  23256. ret = WC_TEST_RET_ENC_EC(ret);
  23257. goto done;
  23258. }
  23259. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  23260. if (ret != 0) {
  23261. ret = WC_TEST_RET_ENC_EC(ret);
  23262. goto done;
  23263. }
  23264. ret = wc_ecc_cmp_point(point3, point4);
  23265. if (ret != MP_EQ) {
  23266. ret = WC_TEST_RET_ENC_EC(ret);
  23267. goto done;
  23268. }
  23269. #endif
  23270. done:
  23271. #ifdef HAVE_COMP_KEY
  23272. wc_ecc_del_point(point4);
  23273. wc_ecc_del_point(point3);
  23274. #endif
  23275. wc_ecc_del_point(point2);
  23276. wc_ecc_del_point(point);
  23277. return ret;
  23278. }
  23279. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  23280. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23281. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  23282. {
  23283. wc_test_ret_t ret;
  23284. word32 sigSz;
  23285. int size;
  23286. byte out[ECC_MAX_SIG_SIZE];
  23287. byte in[] = TEST_STRING;
  23288. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  23289. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  23290. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  23291. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  23292. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  23293. };
  23294. word32 inLen = (word32)XSTRLEN((char*)in);
  23295. size = wc_ecc_sig_size(key);
  23296. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  23297. if (ret != size)
  23298. return WC_TEST_RET_ENC_NC;
  23299. sigSz = (word32)ret;
  23300. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  23301. inLen, out, &sigSz, key, sizeof(*key), rng);
  23302. if (ret != 0)
  23303. return WC_TEST_RET_ENC_EC(ret);
  23304. TEST_SLEEP();
  23305. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  23306. inLen, out, sigSz, key, sizeof(*key));
  23307. if (ret != 0)
  23308. return WC_TEST_RET_ENC_EC(ret);
  23309. TEST_SLEEP();
  23310. sigSz = (word32)sizeof(out);
  23311. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  23312. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  23313. if (ret != 0)
  23314. return WC_TEST_RET_ENC_EC(ret);
  23315. TEST_SLEEP();
  23316. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  23317. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  23318. if (ret != 0)
  23319. return WC_TEST_RET_ENC_EC(ret);
  23320. TEST_SLEEP();
  23321. return 0;
  23322. }
  23323. #endif
  23324. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23325. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23326. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  23327. {
  23328. wc_test_ret_t ret;
  23329. int curve_id;
  23330. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23331. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23332. #else
  23333. ecc_key keyImp[1];
  23334. #endif
  23335. byte priv[32];
  23336. word32 privLen;
  23337. byte pub[65*2];
  23338. word32 pubLen, pubLenX, pubLenY;
  23339. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  23340. "ce46cbc947616d0cbaa82323818a793d";
  23341. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  23342. "922f8b30ea6e8811742ac7238fe87308";
  23343. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  23344. "2e7a493ec1153b3a95feb8a4873f8d08";
  23345. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23346. if (keyImp == NULL)
  23347. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23348. #endif
  23349. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23350. privLen = sizeof(priv);
  23351. ret = wc_ecc_export_private_only(key, priv, &privLen);
  23352. if (ret != 0) {
  23353. ret = WC_TEST_RET_ENC_EC(ret);
  23354. goto done;
  23355. }
  23356. pubLen = sizeof(pub);
  23357. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  23358. if (ret != 0) {
  23359. ret = WC_TEST_RET_ENC_EC(ret);
  23360. goto done;
  23361. }
  23362. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  23363. if (ret != 0) {
  23364. ret = WC_TEST_RET_ENC_EC(ret);
  23365. goto done;
  23366. }
  23367. wc_ecc_free(keyImp);
  23368. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23369. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  23370. if (ret != 0) {
  23371. ret = WC_TEST_RET_ENC_EC(ret);
  23372. goto done;
  23373. }
  23374. wc_ecc_free(keyImp);
  23375. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23376. curve_id = wc_ecc_get_curve_id(key->idx);
  23377. if (curve_id < 0) {
  23378. ret = WC_TEST_RET_ENC_EC(curve_id);
  23379. goto done;
  23380. }
  23381. /* test import private only */
  23382. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  23383. curve_id);
  23384. if (ret != 0) {
  23385. ret = WC_TEST_RET_ENC_EC(ret);
  23386. goto done;
  23387. }
  23388. wc_ecc_free(keyImp);
  23389. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23390. /* test export public raw */
  23391. pubLenX = pubLenY = 32;
  23392. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  23393. if (ret != 0) {
  23394. ret = WC_TEST_RET_ENC_EC(ret);
  23395. goto done;
  23396. }
  23397. #ifndef HAVE_SELFTEST
  23398. /* test import of public */
  23399. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  23400. if (ret != 0) {
  23401. ret = WC_TEST_RET_ENC_EC(ret);
  23402. goto done;
  23403. }
  23404. #endif
  23405. wc_ecc_free(keyImp);
  23406. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23407. /* test export private and public raw */
  23408. pubLenX = pubLenY = privLen = 32;
  23409. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  23410. priv, &privLen);
  23411. if (ret != 0) {
  23412. ret = WC_TEST_RET_ENC_EC(ret);
  23413. goto done;
  23414. }
  23415. #ifndef HAVE_SELFTEST
  23416. /* test import of private and public */
  23417. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  23418. if (ret != 0) {
  23419. ret = WC_TEST_RET_ENC_EC(ret);
  23420. goto done;
  23421. }
  23422. #endif
  23423. done:
  23424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23425. if (keyImp != NULL) {
  23426. wc_ecc_free(keyImp);
  23427. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23428. }
  23429. #else
  23430. wc_ecc_free(keyImp);
  23431. #endif
  23432. return ret;
  23433. }
  23434. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  23435. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  23436. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23437. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  23438. {
  23439. wc_test_ret_t ret;
  23440. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23441. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23442. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23443. #else
  23444. ecc_key key2[1];
  23445. ecc_key key3[1];
  23446. #endif
  23447. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23448. if ((key2 == NULL) || (key3 == NULL))
  23449. ERROR_OUT(MEMORY_E, done);
  23450. #endif
  23451. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  23452. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  23453. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  23454. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  23455. * private key in key2.
  23456. */
  23457. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  23458. ECC_SECP256R1);
  23459. if (ret != 0)
  23460. goto done;
  23461. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  23462. * in key3.
  23463. */
  23464. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  23465. key1->dp->prime, ECC_SECP256R1);
  23466. if (ret != 0)
  23467. goto done;
  23468. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  23469. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  23470. 1);
  23471. if (ret != 0) {
  23472. ret = WC_TEST_RET_ENC_EC(ret);
  23473. goto done;
  23474. }
  23475. done:
  23476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23477. if (key2 != NULL) {
  23478. wc_ecc_free(key2);
  23479. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23480. }
  23481. if (key3 != NULL) {
  23482. wc_ecc_free(key3);
  23483. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23484. }
  23485. #else
  23486. wc_ecc_free(key3);
  23487. wc_ecc_free(key2);
  23488. #endif
  23489. return ret;
  23490. }
  23491. #endif
  23492. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  23493. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23494. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  23495. {
  23496. wc_test_ret_t ret;
  23497. byte out[128];
  23498. word32 outLen = sizeof(out);
  23499. /* Parameter Validation testing. */
  23500. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  23501. if (ret != BAD_FUNC_ARG)
  23502. return WC_TEST_RET_ENC_EC(ret);
  23503. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  23504. if (ret != BAD_FUNC_ARG)
  23505. return WC_TEST_RET_ENC_EC(ret);
  23506. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  23507. if (ret != BAD_FUNC_ARG)
  23508. return WC_TEST_RET_ENC_EC(ret);
  23509. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  23510. if (ret != BAD_FUNC_ARG)
  23511. return WC_TEST_RET_ENC_EC(ret);
  23512. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23513. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23514. !defined(HAVE_SELFTEST)
  23515. ret = wc_ecc_set_rng(key, rng);
  23516. if (ret != 0)
  23517. return WC_TEST_RET_ENC_EC(ret);
  23518. #else
  23519. (void)rng;
  23520. #endif
  23521. /* Use API. */
  23522. ret = 0;
  23523. do {
  23524. #if defined(WOLFSSL_ASYNC_CRYPT)
  23525. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23526. #endif
  23527. if (ret == 0)
  23528. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  23529. } while (ret == WC_PENDING_E);
  23530. if (ret != 0)
  23531. return WC_TEST_RET_ENC_EC(ret);
  23532. TEST_SLEEP();
  23533. return 0;
  23534. }
  23535. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  23536. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  23537. {
  23538. wc_test_ret_t ret;
  23539. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23540. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23541. #else
  23542. ecc_key key[1];
  23543. #endif
  23544. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  23545. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23546. word32 idx = 0;
  23547. #endif
  23548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23549. if (key == NULL)
  23550. ERROR_OUT(MEMORY_E, done);
  23551. #endif
  23552. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23553. /* Use API */
  23554. ret = wc_ecc_set_flags(NULL, 0);
  23555. if (ret != BAD_FUNC_ARG) {
  23556. ret = WC_TEST_RET_ENC_EC(ret);
  23557. goto done;
  23558. }
  23559. ret = wc_ecc_set_flags(key, 0);
  23560. if (ret != 0) {
  23561. ret = WC_TEST_RET_ENC_EC(ret);
  23562. goto done;
  23563. }
  23564. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  23565. #ifndef WC_NO_RNG
  23566. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  23567. #if defined(WOLFSSL_ASYNC_CRYPT)
  23568. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  23569. #endif
  23570. if (ret != 0) {
  23571. goto done;
  23572. }
  23573. #ifndef NO_SIG_WRAPPER
  23574. ret = ecc_sig_test(rng, key);
  23575. if (ret < 0)
  23576. goto done;
  23577. #endif
  23578. TEST_SLEEP();
  23579. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  23580. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23581. ret = ecc_ssh_test(key, rng);
  23582. if (ret < 0)
  23583. goto done;
  23584. #endif
  23585. wc_ecc_free(key);
  23586. #else
  23587. (void)rng;
  23588. #endif /* !WC_NO_RNG */
  23589. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  23590. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23591. /* Use test ECC key - ensure real private "d" exists */
  23592. #ifdef USE_CERT_BUFFERS_256
  23593. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  23594. sizeof_ecc_key_der_256);
  23595. #else
  23596. {
  23597. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  23598. byte der[128];
  23599. word32 derSz;
  23600. if (!file) {
  23601. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23602. }
  23603. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  23604. XFCLOSE(file);
  23605. if (derSz == 0)
  23606. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23607. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  23608. }
  23609. #endif
  23610. if (ret != 0) {
  23611. goto done;
  23612. }
  23613. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  23614. ret = ecc_exp_imp_test(key);
  23615. if (ret < 0)
  23616. goto done;
  23617. #endif
  23618. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  23619. !defined(WOLFSSL_CRYPTOCELL)
  23620. ret = ecc_mulmod_test(key);
  23621. if (ret < 0)
  23622. goto done;
  23623. #endif
  23624. #endif
  23625. #else
  23626. (void)rng;
  23627. (void)idx;
  23628. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23629. done:
  23630. wc_ecc_free(key);
  23631. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23632. if (key != NULL) {
  23633. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23634. }
  23635. #endif
  23636. return ret;
  23637. }
  23638. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  23639. #if defined(WOLFSSL_CERT_EXT) && \
  23640. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23641. static wc_test_ret_t ecc_decode_test(void)
  23642. {
  23643. wc_test_ret_t ret;
  23644. word32 inSz;
  23645. word32 inOutIdx;
  23646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23647. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23648. #else
  23649. ecc_key key[1];
  23650. #endif
  23651. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  23652. /* This is ecc_clikeypub_der_256. */
  23653. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  23654. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  23655. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  23656. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  23657. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  23658. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  23659. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  23660. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  23661. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  23662. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  23663. 0xb4 };
  23664. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  23665. 0x00, 0x04, 0x01, 0x01 };
  23666. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  23667. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23668. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  23669. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23670. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  23671. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23672. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  23673. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23674. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23675. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  23676. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23677. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  23678. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  23679. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23680. 0x03, 0x03, 0x04, 0x01, 0x01 };
  23681. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  23682. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23683. 0x03, 0x03, 0x00, 0x04, 0x01 };
  23684. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23685. if (key == NULL)
  23686. ERROR_OUT(MEMORY_E, done);
  23687. #endif
  23688. XMEMSET(key, 0, sizeof *key);
  23689. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23690. inSz = sizeof(good);
  23691. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  23692. if (ret != BAD_FUNC_ARG) {
  23693. ret = WC_TEST_RET_ENC_EC(ret);
  23694. goto done;
  23695. }
  23696. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  23697. if (ret != BAD_FUNC_ARG) {
  23698. ret = WC_TEST_RET_ENC_EC(ret);
  23699. goto done;
  23700. }
  23701. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  23702. if (ret != BAD_FUNC_ARG) {
  23703. ret = WC_TEST_RET_ENC_EC(ret);
  23704. goto done;
  23705. }
  23706. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  23707. if (ret != BAD_FUNC_ARG) {
  23708. ret = WC_TEST_RET_ENC_EC(ret);
  23709. goto done;
  23710. }
  23711. /* Change offset to produce bad input data. */
  23712. inOutIdx = 2;
  23713. inSz = sizeof(good) - inOutIdx;
  23714. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23715. if (ret != ASN_PARSE_E) {
  23716. ret = WC_TEST_RET_ENC_EC(ret);
  23717. goto done;
  23718. }
  23719. inOutIdx = 4;
  23720. inSz = sizeof(good) - inOutIdx;
  23721. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23722. if (ret != ASN_PARSE_E) {
  23723. ret = WC_TEST_RET_ENC_EC(ret);
  23724. goto done;
  23725. }
  23726. /* Bad data. */
  23727. inSz = sizeof(badNoObjId);
  23728. inOutIdx = 0;
  23729. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  23730. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  23731. ret = WC_TEST_RET_ENC_EC(ret);
  23732. goto done;
  23733. }
  23734. inSz = sizeof(badOneObjId);
  23735. inOutIdx = 0;
  23736. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  23737. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  23738. ret = WC_TEST_RET_ENC_EC(ret);
  23739. goto done;
  23740. }
  23741. inSz = sizeof(badObjId1Len);
  23742. inOutIdx = 0;
  23743. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  23744. if (ret != ASN_PARSE_E) {
  23745. ret = WC_TEST_RET_ENC_EC(ret);
  23746. goto done;
  23747. }
  23748. inSz = sizeof(badObj2d1Len);
  23749. inOutIdx = 0;
  23750. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  23751. if (ret != ASN_PARSE_E) {
  23752. ret = WC_TEST_RET_ENC_EC(ret);
  23753. goto done;
  23754. }
  23755. inSz = sizeof(badNotBitStr);
  23756. inOutIdx = 0;
  23757. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  23758. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  23759. ret = WC_TEST_RET_ENC_EC(ret);
  23760. goto done;
  23761. }
  23762. inSz = sizeof(badBitStrLen);
  23763. inOutIdx = 0;
  23764. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  23765. if (ret != ASN_PARSE_E) {
  23766. ret = WC_TEST_RET_ENC_EC(ret);
  23767. goto done;
  23768. }
  23769. inSz = sizeof(badNoBitStrZero);
  23770. inOutIdx = 0;
  23771. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  23772. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  23773. ret = WC_TEST_RET_ENC_EC(ret);
  23774. goto done;
  23775. }
  23776. inSz = sizeof(badPoint);
  23777. inOutIdx = 0;
  23778. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  23779. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  23780. ret = WC_TEST_RET_ENC_EC(ret);
  23781. goto done;
  23782. }
  23783. inSz = sizeof(good);
  23784. inOutIdx = 0;
  23785. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23786. if (ret != 0) {
  23787. ret = WC_TEST_RET_ENC_EC(ret);
  23788. goto done;
  23789. }
  23790. done:
  23791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23792. if (key != NULL) {
  23793. wc_ecc_free(key);
  23794. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23795. }
  23796. #else
  23797. wc_ecc_free(key);
  23798. #endif
  23799. return ret;
  23800. }
  23801. #endif /* WOLFSSL_CERT_EXT */
  23802. #ifdef WOLFSSL_CUSTOM_CURVES
  23803. static const byte eccKeyExplicitCurve[] = {
  23804. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  23805. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  23806. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  23807. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  23808. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  23809. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23810. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23811. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  23812. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  23813. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  23814. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  23815. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  23816. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  23817. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  23818. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  23819. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  23820. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  23821. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  23822. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23823. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23824. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  23825. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  23826. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  23827. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  23828. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  23829. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  23830. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  23831. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  23832. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  23833. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  23834. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  23835. };
  23836. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  23837. {
  23838. wc_test_ret_t ret;
  23839. word32 inOutIdx;
  23840. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23841. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23842. #else
  23843. ecc_key key[1];
  23844. #endif
  23845. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  23846. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  23847. #ifndef WOLFSSL_ECC_CURVE_STATIC
  23848. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  23849. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  23850. };
  23851. #define ecc_oid_brainpoolp256r1_sz \
  23852. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  23853. #else
  23854. #define ecc_oid_brainpoolp256r1 { \
  23855. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  23856. }
  23857. #define ecc_oid_brainpoolp256r1_sz 9
  23858. #endif
  23859. #define ecc_oid_brainpoolp256r1_sum 104
  23860. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  23861. 32, /* size/bytes */
  23862. ECC_CURVE_CUSTOM, /* ID */
  23863. "BRAINPOOLP256R1", /* curve name */
  23864. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  23865. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  23866. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  23867. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  23868. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  23869. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  23870. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  23871. ecc_oid_brainpoolp256r1_sz,
  23872. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  23873. 1, /* cofactor */
  23874. };
  23875. #endif /* HAVE_ECC_BRAINPOOL */
  23876. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23877. if (! key) {
  23878. ret = MEMORY_E;
  23879. goto done;
  23880. }
  23881. #endif
  23882. XMEMSET(key, 0, sizeof *key);
  23883. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  23884. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  23885. &ecc_dp_brainpool256r1);
  23886. if (ret != 0) {
  23887. printf("ECC test for custom curve failed!\n");
  23888. goto done;
  23889. }
  23890. #endif
  23891. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  23892. {
  23893. int curve_id;
  23894. #ifdef HAVE_ECC_BRAINPOOL
  23895. curve_id = ECC_BRAINPOOLP256R1;
  23896. #else
  23897. curve_id = ECC_SECP256K1;
  23898. #endif
  23899. /* Test and demonstrate use of non-SECP curve */
  23900. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  23901. if (ret < 0) {
  23902. printf("ECC test for curve_id %d failed!\n", curve_id);
  23903. goto done;
  23904. }
  23905. }
  23906. #endif
  23907. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23908. if (ret != 0) {
  23909. ret = WC_TEST_RET_ENC_EC(ret);
  23910. goto done;
  23911. }
  23912. inOutIdx = 0;
  23913. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  23914. sizeof(eccKeyExplicitCurve));
  23915. if (ret != 0)
  23916. ret = WC_TEST_RET_ENC_EC(ret);
  23917. done:
  23918. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23919. if (key) {
  23920. wc_ecc_free(key);
  23921. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23922. }
  23923. #else
  23924. wc_ecc_free(key);
  23925. #endif
  23926. (void)rng;
  23927. return ret;
  23928. }
  23929. #endif /* WOLFSSL_CUSTOM_CURVES */
  23930. #ifdef WOLFSSL_SM2
  23931. #ifdef HAVE_ECC_VERIFY
  23932. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  23933. #ifdef WOLFSSL_SM2
  23934. #ifdef HAVE_OID_ENCODING
  23935. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  23936. #define CODED_SM2P256V1_SZ 6
  23937. #else
  23938. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  23939. #define CODED_SM2P256V1_SZ 10
  23940. #endif
  23941. #ifndef WOLFSSL_ECC_CURVE_STATIC
  23942. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  23943. #else
  23944. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  23945. #endif
  23946. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  23947. #endif /* WOLFSSL_SM2 */
  23948. #define ECC_SM2P256V1_TEST 102
  23949. static int test_sm2_verify_caseA2()
  23950. {
  23951. ecc_key key;
  23952. int ret, res;
  23953. mp_int r,s;
  23954. /* test key values */
  23955. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  23956. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  23957. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  23958. const ecc_set_type ecc_sm2_A2 = {
  23959. 32, /* size/bytes */
  23960. ECC_SM2P256V1_TEST, /* ID */
  23961. "SM2P256V1_TEST", /* curve name */
  23962. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  23963. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  23964. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  23965. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  23966. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  23967. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  23968. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  23969. ecc_oid_sm2p256v1, /* oid/oidSz */
  23970. ecc_oid_sm2p256v1_sz,
  23971. ECC_SM2P256V1_OID, /* oid sum */
  23972. 1, /* cofactor */
  23973. };
  23974. /* use canned hash value hash = H(ZA||M) */
  23975. const byte hash[] = {
  23976. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  23977. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  23978. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  23979. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  23980. };
  23981. /* canned r and s */
  23982. const byte rCan[] = {
  23983. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  23984. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  23985. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  23986. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  23987. };
  23988. const byte sCan[] = {
  23989. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  23990. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  23991. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  23992. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  23993. };
  23994. mp_init(&r);
  23995. mp_init(&s);
  23996. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  23997. if (ret != 0)
  23998. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23999. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  24000. if (ret != 0)
  24001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24002. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  24003. if (ret != 0)
  24004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24005. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  24006. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  24007. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  24008. if (ret != 0)
  24009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24010. if (res != 1)
  24011. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24012. done:
  24013. mp_free(&r);
  24014. mp_free(&s);
  24015. wc_ecc_free(&key);
  24016. return ret;
  24017. }
  24018. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  24019. static int test_sm2_verify_case()
  24020. {
  24021. ecc_key key;
  24022. int ret, res;
  24023. /* test key values */
  24024. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  24025. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  24026. /* use canned hash value hash = H(ZA||M) */
  24027. const byte hash[] = {
  24028. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  24029. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  24030. 0x50,0x69,0x5B,0x20
  24031. };
  24032. const byte sig[] = {
  24033. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  24034. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  24035. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  24036. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  24037. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  24038. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  24039. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  24040. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  24041. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  24042. };
  24043. const byte badSig[] = {
  24044. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  24045. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  24046. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  24047. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  24048. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  24049. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  24050. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  24051. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  24052. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  24053. };
  24054. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  24055. if (ret != 0)
  24056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24057. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  24058. if (ret != 0)
  24059. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24060. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  24061. &key);
  24062. if (ret != 0)
  24063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24064. if (res != 1)
  24065. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24066. /* now test a case that should fail */
  24067. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  24068. &res, &key);
  24069. if (ret != 0)
  24070. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24071. if (res == 1)
  24072. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24073. done:
  24074. wc_ecc_free(&key);
  24075. return ret;
  24076. }
  24077. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  24078. {
  24079. const ecc_set_type* dp = wc_ecc_get_curve_params(
  24080. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  24081. int keySize = 32;
  24082. int curve_id = ECC_SM2P256V1;
  24083. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24084. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24085. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24086. #endif
  24087. #ifdef HAVE_ECC_KEY_EXPORT
  24088. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  24089. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  24090. #endif
  24091. word32 x = 0;
  24092. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24093. word32 y;
  24094. #endif
  24095. #ifdef HAVE_ECC_SIGN
  24096. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  24097. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  24098. int i;
  24099. #ifdef HAVE_ECC_VERIFY
  24100. int verify;
  24101. #endif /* HAVE_ECC_VERIFY */
  24102. #endif /* HAVE_ECC_SIGN */
  24103. int ret;
  24104. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24105. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24106. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24107. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24108. #else
  24109. ecc_key userA[1];
  24110. ecc_key userB[1];
  24111. ecc_key pubKey[1];
  24112. #endif
  24113. #ifndef WC_NO_RNG
  24114. int curveSize;
  24115. #endif
  24116. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  24117. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24118. if (sharedA == NULL || sharedB == NULL)
  24119. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24120. #endif
  24121. #ifdef HAVE_ECC_KEY_EXPORT
  24122. if (exportBuf == NULL)
  24123. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24124. #endif
  24125. #ifdef HAVE_ECC_SIGN
  24126. if (sig == NULL || digest == NULL)
  24127. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24128. #endif
  24129. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  24130. (void)testVerifyCount;
  24131. (void)dp;
  24132. (void)x;
  24133. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24134. if ((userA == NULL) ||
  24135. (userB == NULL) ||
  24136. (pubKey == NULL))
  24137. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24138. #endif
  24139. XMEMSET(userA, 0, sizeof *userA);
  24140. XMEMSET(userB, 0, sizeof *userB);
  24141. XMEMSET(pubKey, 0, sizeof *pubKey);
  24142. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24143. if (ret != 0)
  24144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24145. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  24146. if (ret != 0)
  24147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24148. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24149. if (ret != 0)
  24150. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24151. #ifndef WC_NO_RNG
  24152. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  24153. if (ret == ECC_CURVE_OID_E)
  24154. goto done; /* catch case, where curve is not supported */
  24155. if (ret != 0)
  24156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24157. TEST_SLEEP();
  24158. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  24159. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  24160. if (curveSize != userA->dp->size) {
  24161. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24162. }
  24163. }
  24164. ret = wc_ecc_check_key(userA);
  24165. if (ret != 0)
  24166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24167. TEST_SLEEP();
  24168. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  24169. if (ret != 0)
  24170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24171. /* only perform the below tests if the key size matches */
  24172. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  24173. if (ret != 0) {
  24174. ret = ECC_CURVE_OID_E;
  24175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24176. }
  24177. #ifdef HAVE_ECC_DHE
  24178. #if defined(ECC_TIMING_RESISTANT)
  24179. ret = wc_ecc_set_rng(userA, rng);
  24180. if (ret != 0)
  24181. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24182. ret = wc_ecc_set_rng(userB, rng);
  24183. if (ret != 0)
  24184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24185. #endif
  24186. x = ECC_SHARED_SIZE;
  24187. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  24188. if (ret != 0)
  24189. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24190. y = ECC_SHARED_SIZE;
  24191. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  24192. if (ret != 0)
  24193. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24194. if (y != x)
  24195. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24196. if (XMEMCMP(sharedA, sharedB, x))
  24197. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24198. #endif /* HAVE_ECC_DHE */
  24199. #ifdef HAVE_ECC_KEY_EXPORT
  24200. x = ECC_KEY_EXPORT_BUF_SIZE;
  24201. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  24202. if (ret != 0)
  24203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24204. #ifdef HAVE_ECC_KEY_IMPORT
  24205. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24206. if (ret != 0)
  24207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24208. #ifdef HAVE_ECC_DHE
  24209. y = ECC_SHARED_SIZE;
  24210. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  24211. if (ret != 0)
  24212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24213. if (XMEMCMP(sharedA, sharedB, y))
  24214. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24215. #endif /* HAVE_ECC_DHE */
  24216. #ifdef HAVE_COMP_KEY
  24217. /* try compressed export / import too */
  24218. x = ECC_KEY_EXPORT_BUF_SIZE;
  24219. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  24220. if (ret != 0)
  24221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24222. wc_ecc_free(pubKey);
  24223. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24224. if (ret != 0)
  24225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24226. #endif
  24227. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24228. if (ret != 0)
  24229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24230. #ifdef HAVE_ECC_DHE
  24231. y = ECC_SHARED_SIZE;
  24232. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  24233. if (ret != 0)
  24234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24235. if (XMEMCMP(sharedA, sharedB, y))
  24236. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24237. #endif /* HAVE_ECC_DHE */
  24238. #endif /* HAVE_ECC_KEY_IMPORT */
  24239. #endif /* HAVE_ECC_KEY_EXPORT */
  24240. #endif /* !WC_NO_RNG */
  24241. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24242. !defined(WC_NO_RNG))
  24243. #ifdef HAVE_ECC_SIGN
  24244. /* ECC w/out Shamir has issue with all 0 digest */
  24245. /* WC_BIGINT doesn't have 0 len well on hardware */
  24246. /* Cryptocell has issues with all 0 digest */
  24247. #if defined(ECC_SHAMIR)
  24248. /* test DSA sign hash with zeros */
  24249. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24250. digest[i] = 0;
  24251. }
  24252. x = ECC_SIG_SIZE;
  24253. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  24254. if (ret != 0)
  24255. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24256. #ifdef HAVE_ECC_VERIFY
  24257. for (i = 0; i < testVerifyCount; i++) {
  24258. verify = 0;
  24259. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  24260. userA);
  24261. if (ret != 0)
  24262. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24263. if (verify != 1)
  24264. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24265. }
  24266. #endif /* HAVE_ECC_VERIFY */
  24267. #endif /* ECC_SHAMIR */
  24268. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  24269. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24270. digest[i] = (byte)i;
  24271. }
  24272. x = ECC_SIG_SIZE;
  24273. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  24274. if (ret != 0)
  24275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24276. #ifdef HAVE_ECC_VERIFY
  24277. for (i = 0; i < testVerifyCount; i++) {
  24278. verify = 0;
  24279. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  24280. userA);
  24281. if (ret != 0)
  24282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24283. if (verify != 1)
  24284. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24285. }
  24286. #endif /* HAVE_ECC_VERIFY */
  24287. #endif /* HAVE_ECC_SIGN */
  24288. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  24289. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24290. x = ECC_KEY_EXPORT_BUF_SIZE;
  24291. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  24292. if (ret != 0)
  24293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24294. #elif defined(HAVE_ECC_KEY_EXPORT)
  24295. (void)exportBuf;
  24296. #endif /* HAVE_ECC_KEY_EXPORT */
  24297. done:
  24298. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24299. if (userA != NULL) {
  24300. wc_ecc_free(userA);
  24301. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24302. }
  24303. if (userB != NULL) {
  24304. wc_ecc_free(userB);
  24305. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24306. }
  24307. if (pubKey != NULL) {
  24308. wc_ecc_free(pubKey);
  24309. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24310. }
  24311. #else
  24312. wc_ecc_free(pubKey);
  24313. wc_ecc_free(userB);
  24314. wc_ecc_free(userA);
  24315. #endif
  24316. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  24317. WC_FREE_VAR(sharedA, HEAP_HINT);
  24318. WC_FREE_VAR(sharedB, HEAP_HINT);
  24319. #endif
  24320. #ifdef HAVE_ECC_KEY_EXPORT
  24321. WC_FREE_VAR(exportBuf, HEAP_HINT);
  24322. #endif
  24323. #ifdef HAVE_ECC_SIGN
  24324. WC_FREE_VAR(sig, HEAP_HINT);
  24325. WC_FREE_VAR(digest, HEAP_HINT);
  24326. #endif
  24327. (void)keySize;
  24328. (void)curve_id;
  24329. (void)rng;
  24330. return ret;
  24331. }
  24332. #endif /* HAVE_ECC_VERIFY */
  24333. static int test_sm2_create_digest()
  24334. {
  24335. const byte msg[] = "message to sign";
  24336. const byte id[] = "0123456789";
  24337. const byte badId[] = "0123556789";
  24338. byte expected[] = {
  24339. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  24340. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  24341. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  24342. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  24343. };
  24344. ecc_key key;
  24345. int ret;
  24346. /* test key values */
  24347. const char qx[] =
  24348. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  24349. const char qy[] =
  24350. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  24351. const char d[] =
  24352. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  24353. byte digest[WC_SHA256_DIGEST_SIZE];
  24354. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  24355. if (ret != 0)
  24356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24357. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  24358. if (ret != 0)
  24359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24360. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  24361. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  24362. WC_SHA256_DIGEST_SIZE, &key);
  24363. if (ret != 0)
  24364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24365. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  24366. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24367. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  24368. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  24369. WC_SHA256_DIGEST_SIZE, &key);
  24370. if (ret != 0)
  24371. goto done;
  24372. /* should be different than the previous ID used */
  24373. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  24374. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24375. done:
  24376. wc_ecc_free(&key);
  24377. return ret;
  24378. }
  24379. static int test_sm2_verify()
  24380. {
  24381. int ret = 0;
  24382. #ifdef HAVE_ECC_VERIFY
  24383. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  24384. ret = test_sm2_verify_caseA2();
  24385. if (ret != 0)
  24386. return ret;
  24387. #endif
  24388. ret = test_sm2_verify_case();
  24389. if (ret != 0)
  24390. return ret;
  24391. #endif /* HAVE_ECC_VERIFY */
  24392. ret = test_sm2_create_digest();
  24393. return ret;
  24394. }
  24395. #endif /* WOLFSSL_SM2 */
  24396. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  24397. /* Make Cert / Sign example for ECC cert and ECC CA */
  24398. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  24399. {
  24400. wc_test_ret_t ret;
  24401. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24402. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24403. #ifdef WOLFSSL_TEST_CERT
  24404. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24405. #endif
  24406. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24407. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24408. #else
  24409. Cert myCert[1];
  24410. #ifdef WOLFSSL_TEST_CERT
  24411. DecodedCert decode[1];
  24412. #endif
  24413. ecc_key caEccKey[1];
  24414. ecc_key certPubKey[1];
  24415. #endif
  24416. int certSz;
  24417. size_t bytes;
  24418. word32 idx = 0;
  24419. #ifndef USE_CERT_BUFFERS_256
  24420. XFILE file;
  24421. #endif
  24422. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24423. byte* der = NULL;
  24424. #else
  24425. byte der[FOURK_BUF];
  24426. #endif
  24427. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24428. if ((myCert == NULL)
  24429. #ifdef WOLFSSL_TEST_CERT
  24430. || (decode == NULL)
  24431. #endif
  24432. || (caEccKey == NULL) || (certPubKey == NULL))
  24433. ERROR_OUT(MEMORY_E, exit);
  24434. #endif
  24435. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  24436. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  24437. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24438. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24439. if (der == NULL) {
  24440. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24441. }
  24442. #endif
  24443. /* Get cert private key */
  24444. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24445. /* Get Cert Key 384 */
  24446. #ifdef USE_CERT_BUFFERS_256
  24447. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  24448. bytes = sizeof_ca_ecc_key_der_384;
  24449. #else
  24450. file = XFOPEN(eccCaKey384File, "rb");
  24451. if (!file) {
  24452. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24453. }
  24454. bytes = XFREAD(der, 1, FOURK_BUF, file);
  24455. XFCLOSE(file);
  24456. if (bytes == 0)
  24457. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24458. (void)eccCaKeyFile;
  24459. #endif /* USE_CERT_BUFFERS_256 */
  24460. #else
  24461. #ifdef USE_CERT_BUFFERS_256
  24462. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  24463. bytes = sizeof_ca_ecc_key_der_256;
  24464. #else
  24465. file = XFOPEN(eccCaKeyFile, "rb");
  24466. if (!file) {
  24467. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24468. }
  24469. bytes = XFREAD(der, 1, FOURK_BUF, file);
  24470. XFCLOSE(file);
  24471. if (bytes == 0)
  24472. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24473. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24474. (void)eccCaKey384File;
  24475. #endif
  24476. #endif /* USE_CERT_BUFFERS_256 */
  24477. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  24478. /* Get CA Key */
  24479. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  24480. if (ret != 0)
  24481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24482. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  24483. if (ret != 0)
  24484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24485. /* Make a public key */
  24486. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  24487. if (ret != 0)
  24488. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24489. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  24490. #if defined(WOLFSSL_ASYNC_CRYPT)
  24491. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  24492. #endif
  24493. if (ret != 0)
  24494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24495. TEST_SLEEP();
  24496. /* Setup Certificate */
  24497. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  24498. if (ret != 0)
  24499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24500. #ifndef NO_SHA256
  24501. myCert->sigType = CTC_SHA256wECDSA;
  24502. #else
  24503. myCert->sigType = CTC_SHAwECDSA;
  24504. #endif
  24505. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  24506. #ifdef WOLFSSL_CERT_EXT
  24507. /* add Policies */
  24508. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  24509. CTC_MAX_CERTPOL_SZ);
  24510. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  24511. CTC_MAX_CERTPOL_SZ);
  24512. myCert->certPoliciesNb = 2;
  24513. /* add SKID from the Public Key */
  24514. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  24515. if (ret != 0)
  24516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24517. /* add AKID from the Public Key */
  24518. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  24519. if (ret != 0)
  24520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24521. /* add Key Usage */
  24522. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  24523. if (ret != 0)
  24524. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24525. #endif /* WOLFSSL_CERT_EXT */
  24526. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24527. #if defined(USE_CERT_BUFFERS_256)
  24528. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  24529. sizeof_ca_ecc_cert_der_384);
  24530. #else
  24531. ret = wc_SetIssuer(myCert, eccCaCert384File);
  24532. (void)eccCaCertFile;
  24533. #endif
  24534. #else
  24535. #if defined(USE_CERT_BUFFERS_256)
  24536. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  24537. sizeof_ca_ecc_cert_der_256);
  24538. #else
  24539. ret = wc_SetIssuer(myCert, eccCaCertFile);
  24540. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24541. (void)eccCaCert384File;
  24542. #endif
  24543. #endif
  24544. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  24545. if (ret < 0)
  24546. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24547. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  24548. if (certSz < 0) {
  24549. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  24550. }
  24551. ret = 0;
  24552. do {
  24553. #if defined(WOLFSSL_ASYNC_CRYPT)
  24554. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24555. #endif
  24556. if (ret >= 0) {
  24557. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  24558. FOURK_BUF, NULL, caEccKey, rng);
  24559. }
  24560. } while (ret == WC_PENDING_E);
  24561. if (ret < 0)
  24562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24563. certSz = (word32)ret;
  24564. TEST_SLEEP();
  24565. #ifdef WOLFSSL_TEST_CERT
  24566. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  24567. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  24568. if (ret != 0) {
  24569. FreeDecodedCert(decode);
  24570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24571. }
  24572. FreeDecodedCert(decode);
  24573. #endif
  24574. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  24575. CERT_TYPE);
  24576. if (ret != 0) {
  24577. goto exit;
  24578. }
  24579. exit:
  24580. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24581. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24582. #endif
  24583. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24584. if (myCert != NULL)
  24585. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24586. #ifdef WOLFSSL_TEST_CERT
  24587. if (decode != NULL)
  24588. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24589. #endif
  24590. if (caEccKey != NULL) {
  24591. wc_ecc_free(caEccKey);
  24592. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24593. }
  24594. if (certPubKey != NULL) {
  24595. wc_ecc_free(certPubKey);
  24596. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24597. }
  24598. #else
  24599. wc_ecc_free(certPubKey);
  24600. wc_ecc_free(caEccKey);
  24601. #endif
  24602. return ret;
  24603. }
  24604. #endif /* WOLFSSL_CERT_GEN */
  24605. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  24606. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  24607. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  24608. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  24609. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  24610. {
  24611. wc_test_ret_t ret = 0;
  24612. ecc_key* key;
  24613. #ifdef WC_NO_RNG
  24614. word32 idx = 0;
  24615. #endif
  24616. key = wc_ecc_key_new(HEAP_HINT);
  24617. if (key == NULL) {
  24618. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24619. }
  24620. #ifndef WC_NO_RNG
  24621. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  24622. #if defined(WOLFSSL_ASYNC_CRYPT)
  24623. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  24624. #endif
  24625. if (ret != 0)
  24626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24627. #else
  24628. /* use test ECC key */
  24629. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  24630. (word32)sizeof_ecc_key_der_256);
  24631. (void)rng;
  24632. #endif
  24633. exit:
  24634. wc_ecc_key_free(key);
  24635. return ret;
  24636. }
  24637. #endif
  24638. /* ECC Non-blocking tests for Sign and Verify */
  24639. /* Requires SP math and supports P384 or P256 */
  24640. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  24641. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  24642. defined(WOLFSSL_PUBLIC_MP)
  24643. /* ECC Private Key "d" */
  24644. static const byte p256PrivKey[] = {
  24645. /* SECP256R1 */
  24646. /* d */
  24647. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  24648. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  24649. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  24650. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  24651. };
  24652. #ifdef HAVE_ECC384
  24653. static const byte p384PrivKey[] = {
  24654. /* SECP384R1 */
  24655. /* d */
  24656. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  24657. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  24658. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  24659. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  24660. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  24661. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  24662. };
  24663. #endif /* HAVE_ECC384 */
  24664. #ifdef HAVE_ECC521
  24665. static const byte p521PrivKey[] = {
  24666. /* SECP521R1 */
  24667. /* d */
  24668. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  24669. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  24670. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  24671. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  24672. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  24673. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  24674. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  24675. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  24676. 0xa4, 0x36
  24677. };
  24678. #endif /* HAVE_ECC521 */
  24679. /* ECC public key Qx/Qy */
  24680. static const byte p256PubKey[] = {
  24681. /* SECP256R1 */
  24682. /* Qx */
  24683. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  24684. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  24685. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  24686. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  24687. /* Qy */
  24688. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  24689. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  24690. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  24691. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  24692. };
  24693. #ifdef HAVE_ECC384
  24694. static const byte p384PubKey[] = {
  24695. /* SECP384R1 */
  24696. /* Qx */
  24697. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  24698. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  24699. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  24700. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  24701. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  24702. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  24703. /* Qy */
  24704. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  24705. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  24706. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  24707. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  24708. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  24709. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  24710. };
  24711. #endif
  24712. #ifdef HAVE_ECC521
  24713. static const byte p521PubKey[] = {
  24714. /* SECP521R1 */
  24715. /* Qx */
  24716. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  24717. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  24718. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  24719. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  24720. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  24721. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  24722. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  24723. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  24724. 0x8b, 0xbc,
  24725. /* Qy */
  24726. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  24727. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  24728. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  24729. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  24730. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  24731. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  24732. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  24733. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  24734. 0x91, 0xd9
  24735. };
  24736. /* perform verify of signature and hash using public key */
  24737. /* key is public Qx + public Qy */
  24738. /* sig is r + s */
  24739. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  24740. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  24741. uint32_t curveSz, int curveId)
  24742. {
  24743. wc_test_ret_t ret;
  24744. int verify_res = 0, count = 0;
  24745. mp_int r, s;
  24746. ecc_key ecc;
  24747. ecc_nb_ctx_t nb_ctx;
  24748. /* validate arguments */
  24749. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  24750. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  24751. {
  24752. return WC_TEST_RET_ENC_NC;
  24753. }
  24754. /* Setup the ECC key */
  24755. ret = wc_ecc_init(&ecc);
  24756. if (ret < 0) {
  24757. return WC_TEST_RET_ENC_EC(ret);
  24758. }
  24759. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  24760. if (ret != MP_OKAY) {
  24761. wc_ecc_free(&ecc);
  24762. return WC_TEST_RET_ENC_EC(ret);
  24763. }
  24764. /* Setup the signature r/s variables */
  24765. ret = mp_init(&r);
  24766. if (ret != MP_OKAY) {
  24767. wc_ecc_free(&ecc);
  24768. return WC_TEST_RET_ENC_EC(ret);
  24769. }
  24770. ret = mp_init(&s);
  24771. if (ret != MP_OKAY) {
  24772. mp_clear(&r);
  24773. wc_ecc_free(&ecc);
  24774. return WC_TEST_RET_ENC_EC(ret);
  24775. }
  24776. /* Import public key x/y */
  24777. ret = wc_ecc_import_unsigned(
  24778. &ecc,
  24779. (byte*)key, /* Public "x" Coordinate */
  24780. (byte*)(key + curveSz), /* Public "y" Coordinate */
  24781. NULL, /* Private "d" (optional) */
  24782. curveId /* ECC Curve Id */
  24783. );
  24784. /* Make sure it was a public key imported */
  24785. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  24786. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  24787. }
  24788. /* Import signature r/s */
  24789. if (ret == 0) {
  24790. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  24791. if (ret < 0)
  24792. ret = WC_TEST_RET_ENC_EC(ret);
  24793. }
  24794. if (ret == 0) {
  24795. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  24796. if (ret < 0)
  24797. ret = WC_TEST_RET_ENC_EC(ret);
  24798. }
  24799. /* Verify ECC Signature */
  24800. if (ret == 0) {
  24801. do {
  24802. ret = wc_ecc_verify_hash_ex(
  24803. &r, &s, /* r/s as mp_int */
  24804. hash, hashSz, /* computed hash digest */
  24805. &verify_res, /* verification result 1=success */
  24806. &ecc
  24807. );
  24808. count++;
  24809. /* This is where real-time work could be called */
  24810. } while (ret == FP_WOULDBLOCK);
  24811. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24812. printf("ECC non-block verify: %d times\n", count);
  24813. #endif
  24814. if (ret < 0)
  24815. ret = WC_TEST_RET_ENC_EC(ret);
  24816. }
  24817. /* check verify result */
  24818. if (ret == 0 && verify_res == 0) {
  24819. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  24820. }
  24821. mp_clear(&r);
  24822. mp_clear(&s);
  24823. wc_ecc_free(&ecc);
  24824. (void)count;
  24825. return ret;
  24826. }
  24827. /* perform signature operation against hash using private key */
  24828. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  24829. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  24830. uint32_t curveSz, int curveId, WC_RNG* rng)
  24831. {
  24832. wc_test_ret_t ret;
  24833. int count = 0;
  24834. mp_int r, s;
  24835. ecc_key ecc;
  24836. ecc_nb_ctx_t nb_ctx;
  24837. /* validate arguments */
  24838. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  24839. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  24840. {
  24841. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  24842. }
  24843. /* Initialize signature result */
  24844. memset(sig, 0, curveSz*2);
  24845. /* Setup the ECC key */
  24846. ret = wc_ecc_init(&ecc);
  24847. if (ret < 0) {
  24848. return WC_TEST_RET_ENC_EC(ret);
  24849. }
  24850. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  24851. if (ret != MP_OKAY) {
  24852. wc_ecc_free(&ecc);
  24853. return WC_TEST_RET_ENC_EC(ret);
  24854. }
  24855. /* Setup the signature r/s variables */
  24856. ret = mp_init(&r);
  24857. if (ret != MP_OKAY) {
  24858. wc_ecc_free(&ecc);
  24859. return WC_TEST_RET_ENC_EC(ret);
  24860. }
  24861. ret = mp_init(&s);
  24862. if (ret != MP_OKAY) {
  24863. mp_clear(&r);
  24864. wc_ecc_free(&ecc);
  24865. return WC_TEST_RET_ENC_EC(ret);
  24866. }
  24867. /* Import private key "k" */
  24868. ret = wc_ecc_import_private_key_ex(
  24869. key, keySz, /* private key "d" */
  24870. NULL, 0, /* public (optional) */
  24871. &ecc,
  24872. curveId /* ECC Curve Id */
  24873. );
  24874. if (ret < 0)
  24875. ret = WC_TEST_RET_ENC_EC(ret);
  24876. if (ret == 0) {
  24877. do {
  24878. /* Verify ECC Signature */
  24879. ret = wc_ecc_sign_hash_ex(
  24880. hash, hashSz, /* computed hash digest */
  24881. rng, &ecc, /* random and key context */
  24882. &r, &s /* r/s as mp_int */
  24883. );
  24884. count++;
  24885. /* This is where real-time work could be called */
  24886. } while (ret == FP_WOULDBLOCK);
  24887. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24888. printf("ECC non-block sign: %d times\n", count);
  24889. #endif
  24890. if (ret < 0)
  24891. ret = WC_TEST_RET_ENC_EC(ret);
  24892. }
  24893. if (ret == 0) {
  24894. /* export r/s */
  24895. mp_to_unsigned_bin_len(&r, sig, curveSz);
  24896. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  24897. }
  24898. mp_clear(&r);
  24899. mp_clear(&s);
  24900. wc_ecc_free(&ecc);
  24901. (void)count;
  24902. return ret;
  24903. }
  24904. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  24905. /*
  24906. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  24907. * don't have non-blocking versions of the key checking functions, yet.
  24908. */
  24909. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  24910. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  24911. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  24912. {
  24913. wc_test_ret_t ret;
  24914. ecc_key keyA;
  24915. ecc_key keyB;
  24916. ecc_nb_ctx_t nbCtxA;
  24917. ecc_nb_ctx_t nbCtxB;
  24918. byte secretA[ECC_SHARED_SIZE];
  24919. byte secretB[ECC_SHARED_SIZE];
  24920. word32 secretSzA = ECC_SHARED_SIZE;
  24921. word32 secretSzB = ECC_SHARED_SIZE;
  24922. int count = 0;
  24923. ret = wc_ecc_init(&keyA);
  24924. if (ret == 0) {
  24925. ret = wc_ecc_init(&keyB);
  24926. if (ret < 0)
  24927. ret = WC_TEST_RET_ENC_EC(ret);
  24928. }
  24929. if (ret == 0) {
  24930. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  24931. if (ret < 0)
  24932. ret = WC_TEST_RET_ENC_EC(ret);
  24933. }
  24934. if (ret == 0) {
  24935. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  24936. if (ret < 0)
  24937. ret = WC_TEST_RET_ENC_EC(ret);
  24938. }
  24939. if (ret == 0) {
  24940. do {
  24941. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  24942. count++;
  24943. } while (ret == FP_WOULDBLOCK);
  24944. if (ret < 0)
  24945. ret = WC_TEST_RET_ENC_EC(ret);
  24946. }
  24947. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24948. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  24949. #endif
  24950. if (ret == 0) {
  24951. ret = wc_ecc_check_key(&keyA);
  24952. if (ret < 0)
  24953. ret = WC_TEST_RET_ENC_EC(ret);
  24954. }
  24955. if (ret == 0) {
  24956. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  24957. privKey, curveId);
  24958. if (ret < 0)
  24959. ret = WC_TEST_RET_ENC_EC(ret);
  24960. }
  24961. count = 0;
  24962. if (ret == 0) {
  24963. do {
  24964. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  24965. count++;
  24966. } while (ret == FP_WOULDBLOCK);
  24967. if (ret < 0)
  24968. ret = WC_TEST_RET_ENC_EC(ret);
  24969. }
  24970. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24971. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  24972. #endif
  24973. if (ret == 0) {
  24974. do {
  24975. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  24976. } while (ret == FP_WOULDBLOCK);
  24977. if (ret < 0)
  24978. ret = WC_TEST_RET_ENC_EC(ret);
  24979. }
  24980. if (ret == 0) {
  24981. if (secretSzA != secretSzB ||
  24982. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  24983. ret = WC_TEST_RET_ENC_NC;
  24984. }
  24985. }
  24986. wc_ecc_free(&keyA);
  24987. wc_ecc_free(&keyB);
  24988. return ret;
  24989. }
  24990. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  24991. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  24992. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  24993. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  24994. WC_RNG* rng)
  24995. {
  24996. wc_test_ret_t ret = 0;
  24997. byte* sig = NULL;
  24998. word32 sigSz = curveSz * 2;
  24999. static const byte hash[] = {
  25000. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  25001. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  25002. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  25003. };
  25004. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  25005. if (sig == NULL) {
  25006. ret = WC_TEST_RET_ENC_ERRNO;
  25007. }
  25008. if (ret == 0) {
  25009. /* Sign hash using private key */
  25010. /* Note: result of an ECC sign varies for each call even with same
  25011. private key and hash. This is because a new random public key is
  25012. used for each operation. */
  25013. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  25014. &sigSz, curveSz, curveId, rng);
  25015. }
  25016. if (ret == 0) {
  25017. /* Verify generated signature is valid */
  25018. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  25019. sigSz, curveSz, curveId);
  25020. }
  25021. if (sig != NULL) {
  25022. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  25023. }
  25024. return ret;
  25025. }
  25026. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  25027. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  25028. {
  25029. wc_test_ret_t ret = 0;
  25030. word32 i;
  25031. int curveIds[3] = {0, 0, 0};
  25032. word32 curveSzs[3] = {0, 0, 0};
  25033. const byte* privKeys[3] = {NULL, NULL, NULL};
  25034. word32 privKeySzs[3] = {0, 0, 0};
  25035. const byte* pubKeys[3] = {NULL, NULL, NULL};
  25036. word32 pubKeySzs[3] = {0, 0, 0};
  25037. curveIds[0] = ECC_SECP256R1;
  25038. curveSzs[0] = 32;
  25039. privKeys[0] = p256PrivKey;
  25040. privKeySzs[0] = sizeof(p256PrivKey);
  25041. pubKeys[0] = p256PubKey;
  25042. pubKeySzs[0] = sizeof(p256PubKey);
  25043. #ifdef HAVE_ECC384
  25044. curveIds[1] = ECC_SECP384R1;
  25045. curveSzs[1] = 48;
  25046. privKeys[1] = p384PrivKey;
  25047. privKeySzs[1] = sizeof(p384PrivKey);
  25048. pubKeys[1] = p384PubKey;
  25049. pubKeySzs[1] = sizeof(p384PubKey);
  25050. #endif
  25051. #ifdef HAVE_ECC521
  25052. curveIds[2] = ECC_SECP521R1;
  25053. curveSzs[2] = 66;
  25054. privKeys[2] = p521PrivKey;
  25055. privKeySzs[2] = sizeof(p521PrivKey);
  25056. pubKeys[2] = p521PubKey;
  25057. pubKeySzs[2] = sizeof(p521PubKey);
  25058. #endif
  25059. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  25060. if (curveIds[i] == 0) {
  25061. continue;
  25062. }
  25063. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  25064. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  25065. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  25066. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  25067. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  25068. if (ret == 0) {
  25069. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  25070. pubKeys[i], rng);
  25071. }
  25072. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  25073. }
  25074. return ret;
  25075. }
  25076. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  25077. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  25078. {
  25079. wc_test_ret_t ret;
  25080. WC_RNG rng;
  25081. #if defined(WOLFSSL_CERT_EXT) && \
  25082. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25083. ret = ecc_decode_test();
  25084. if (ret < 0)
  25085. return ret;
  25086. #endif
  25087. #ifndef HAVE_FIPS
  25088. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25089. #else
  25090. ret = wc_InitRng(&rng);
  25091. #endif
  25092. #ifndef WC_NO_RNG
  25093. if (ret != 0)
  25094. return WC_TEST_RET_ENC_EC(ret);
  25095. #else
  25096. (void)ret;
  25097. #endif
  25098. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  25099. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  25100. if (ret < 0) {
  25101. printf("keySize=14, Default\n");
  25102. goto done;
  25103. }
  25104. #endif /* HAVE_ECC112 */
  25105. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  25106. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  25107. if (ret < 0) {
  25108. printf("keySize=16, Default\n");
  25109. goto done;
  25110. }
  25111. #endif /* HAVE_ECC128 */
  25112. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  25113. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  25114. if (ret < 0) {
  25115. printf("keySize=20, Default\n");
  25116. goto done;
  25117. }
  25118. #endif /* HAVE_ECC160 */
  25119. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  25120. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  25121. printf("keySize=24, Default\n");
  25122. if (ret < 0) {
  25123. goto done;
  25124. }
  25125. #endif /* HAVE_ECC192 */
  25126. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  25127. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  25128. if (ret < 0) {
  25129. printf("keySize=28, Default\n");
  25130. goto done;
  25131. }
  25132. #endif /* HAVE_ECC224 */
  25133. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  25134. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  25135. if (ret < 0) {
  25136. printf("keySize=30, Default\n");
  25137. goto done;
  25138. }
  25139. #endif /* HAVE_ECC239 */
  25140. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25141. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  25142. if (ret < 0) {
  25143. printf("keySize=32, Default\n");
  25144. goto done;
  25145. }
  25146. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  25147. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  25148. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25149. ret = ecc_point_test();
  25150. if (ret < 0) {
  25151. goto done;
  25152. }
  25153. #endif
  25154. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  25155. ret = ecc_def_curve_test(&rng);
  25156. if (ret < 0) {
  25157. printf("Default\n");
  25158. goto done;
  25159. }
  25160. #endif
  25161. #endif /* !NO_ECC256 */
  25162. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  25163. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  25164. if (ret < 0) {
  25165. printf("keySize=40, Default\n");
  25166. goto done;
  25167. }
  25168. #endif /* HAVE_ECC320 */
  25169. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  25170. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  25171. if (ret < 0) {
  25172. printf("keySize=48, Default\n");
  25173. goto done;
  25174. }
  25175. #endif /* HAVE_ECC384 */
  25176. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  25177. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  25178. if (ret < 0) {
  25179. printf("keySize=64, Default\n");
  25180. goto done;
  25181. }
  25182. #endif /* HAVE_ECC512 */
  25183. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  25184. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  25185. if (ret < 0) {
  25186. printf("keySize=68, Default\n");
  25187. goto done;
  25188. }
  25189. #endif /* HAVE_ECC521 */
  25190. #ifdef WOLFSSL_SM2
  25191. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  25192. if (ret < 0) {
  25193. printf("SM2\n");
  25194. goto done;
  25195. }
  25196. #endif
  25197. #if defined(WOLFSSL_CUSTOM_CURVES)
  25198. ret = ecc_test_custom_curves(&rng);
  25199. if (ret != 0) {
  25200. printf("Custom\n");
  25201. goto done;
  25202. }
  25203. #endif
  25204. #if defined(WOLFSSL_SM2)
  25205. ret = test_sm2_verify();
  25206. if (ret != 0) {
  25207. printf("SM2 Verify\n");
  25208. goto done;
  25209. }
  25210. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  25211. if (ret != 0) {
  25212. printf("SM2 test\n");
  25213. goto done;
  25214. }
  25215. #endif
  25216. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25217. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  25218. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  25219. #ifdef HAVE_ECC256
  25220. ret = ecc_test_deterministic_k(&rng);
  25221. if (ret != 0) {
  25222. printf("ecc_test_deterministic_k failed!\n");
  25223. goto done;
  25224. }
  25225. #endif
  25226. #ifdef WOLFSSL_PUBLIC_MP
  25227. #if defined(HAVE_ECC384)
  25228. ret = ecc384_test_deterministic_k(&rng);
  25229. if (ret != 0) {
  25230. printf("ecc384_test_deterministic_k failed!\n");
  25231. goto done;
  25232. }
  25233. #endif
  25234. #if defined(HAVE_ECC521)
  25235. ret = ecc521_test_deterministic_k(&rng);
  25236. if (ret != 0) {
  25237. printf("ecc512_test_deterministic_k failed!\n");
  25238. goto done;
  25239. }
  25240. #endif
  25241. #endif
  25242. #endif
  25243. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  25244. !defined(WOLFSSL_KCAPI_ECC)
  25245. ret = ecc_test_sign_vectors(&rng);
  25246. if (ret != 0) {
  25247. printf("ecc_test_sign_vectors failed!\n");
  25248. goto done;
  25249. }
  25250. #endif
  25251. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  25252. defined(HAVE_ECC_DHE)
  25253. ret = ecc_test_cdh_vectors(&rng);
  25254. if (ret != 0) {
  25255. printf("ecc_test_cdh_vectors failed!\n");
  25256. goto done;
  25257. }
  25258. #endif
  25259. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  25260. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  25261. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  25262. ret = ecc_test_make_pub(&rng);
  25263. if (ret != 0) {
  25264. printf("ecc_test_make_pub failed!\n");
  25265. goto done;
  25266. }
  25267. #elif defined(HAVE_ECC_KEY_IMPORT)
  25268. (void)ecc_test_make_pub; /* for compiler warning */
  25269. #endif
  25270. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  25271. ret = ecc_test_cert_gen(&rng);
  25272. if (ret != 0) {
  25273. printf("ecc_test_cert_gen failed!\n");
  25274. goto done;
  25275. }
  25276. #endif
  25277. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  25278. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  25279. defined(WOLFSSL_CUSTOM_CURVES))
  25280. ret = ecc_test_allocator(&rng);
  25281. if (ret != 0) {
  25282. printf("ecc_test_allocator failed!\n");
  25283. goto done;
  25284. }
  25285. #endif
  25286. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  25287. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  25288. ret = ecc_test_nonblock(&rng);
  25289. if (ret != 0) {
  25290. printf("ecc_test_nonblock failed!\n");
  25291. goto done;
  25292. }
  25293. #endif
  25294. done:
  25295. wc_FreeRng(&rng);
  25296. return ret;
  25297. }
  25298. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  25299. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  25300. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  25301. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  25302. {
  25303. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25304. byte* plaintext;
  25305. byte* encrypted;
  25306. byte* decrypted;
  25307. #else
  25308. byte plaintext[128];
  25309. byte encrypted[128];
  25310. byte decrypted[128];
  25311. #endif
  25312. ecEncCtx* aCtx = NULL;
  25313. ecEncCtx* bCtx = NULL;
  25314. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  25315. 14, 15};
  25316. wc_test_ret_t ret = 0;
  25317. static const char message[] = "Hello wolfSSL!";
  25318. word32 plaintextLen;
  25319. word32 encryptLen = 128;
  25320. word32 decryptLen = 128;
  25321. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25322. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25323. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25324. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25325. #endif
  25326. wc_ecc_free(a);
  25327. wc_ecc_free(b);
  25328. ret = wc_ecc_init(a);
  25329. if (ret != 0)
  25330. ret = WC_TEST_RET_ENC_EC(ret);
  25331. if (ret == 0) {
  25332. ret = wc_ecc_init(b);
  25333. if (ret != 0)
  25334. ret = WC_TEST_RET_ENC_EC(ret);
  25335. }
  25336. if (ret == 0)
  25337. ret = wc_ecc_make_key(rng, 32, a);
  25338. if (ret == 0)
  25339. ret = wc_ecc_make_key(rng, 32, b);
  25340. /* create context */
  25341. if (ret == 0) {
  25342. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25343. if (aCtx == NULL)
  25344. ret = WC_TEST_RET_ENC_NC;
  25345. }
  25346. if (ret == 0) {
  25347. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25348. if (bCtx == NULL)
  25349. ret = WC_TEST_RET_ENC_NC;
  25350. }
  25351. /* set salt */
  25352. if (ret == 0) {
  25353. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  25354. if (ret != 0)
  25355. ret = 10472;
  25356. }
  25357. if (ret == 0) {
  25358. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  25359. if (ret != 0)
  25360. ret = 10473;
  25361. }
  25362. XMEMSET(plaintext, 0, 128);
  25363. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  25364. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  25365. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  25366. /* encrypt */
  25367. if (ret == 0) {
  25368. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  25369. &encryptLen, aCtx);
  25370. if (ret != 0)
  25371. ret = WC_TEST_RET_ENC_EC(ret);
  25372. }
  25373. /* decrypt */
  25374. if (ret == 0) {
  25375. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  25376. &decryptLen, bCtx);
  25377. if (ret != 0)
  25378. ret = WC_TEST_RET_ENC_EC(ret);
  25379. }
  25380. /* compare */
  25381. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  25382. ret = WC_TEST_RET_ENC_NC;
  25383. wc_ecc_free(a);
  25384. wc_ecc_free(b);
  25385. wc_ecc_ctx_free(aCtx);
  25386. wc_ecc_ctx_free(bCtx);
  25387. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25388. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25389. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25390. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25391. #endif
  25392. return ret;
  25393. }
  25394. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  25395. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  25396. * wolfFIPS 5.3.
  25397. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  25398. */
  25399. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  25400. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  25401. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  25402. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  25403. {
  25404. wc_test_ret_t ret = 0;
  25405. #ifdef WOLFSSL_ECIES_OLD
  25406. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25407. ecc_key* userA = NULL;
  25408. #else
  25409. ecc_key userA[1];
  25410. #endif
  25411. int userAInit = 0;
  25412. #endif
  25413. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25414. ecc_key* userB = NULL;
  25415. #else
  25416. ecc_key userB[1];
  25417. #endif
  25418. int userBInit = 0;
  25419. ecc_key* tmpKey;
  25420. byte plain[48];
  25421. word32 plainSz = sizeof(plain);
  25422. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  25423. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  25424. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  25425. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  25426. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  25427. };
  25428. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  25429. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  25430. 0x04,
  25431. /* X */
  25432. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  25433. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  25434. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  25435. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  25436. /* X */
  25437. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  25438. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  25439. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  25440. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  25441. };
  25442. #endif
  25443. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  25444. #ifdef WOLFSSL_ECIES_OLD
  25445. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  25446. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  25447. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  25448. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  25449. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  25450. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  25451. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  25452. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  25453. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  25454. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  25455. #elif defined(WOLFSSL_ECIES_ISO18033)
  25456. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  25457. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  25458. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  25459. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  25460. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  25461. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  25462. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  25463. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  25464. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  25465. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  25466. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  25467. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  25468. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  25469. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  25470. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  25471. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  25472. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  25473. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  25474. 0x7f
  25475. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25476. /* EC P-256 point */
  25477. 0x04,
  25478. /* X */
  25479. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  25480. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  25481. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  25482. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  25483. /* Y */
  25484. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  25485. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  25486. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  25487. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  25488. /* IV */
  25489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25490. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25491. /* Encrypted Msg */
  25492. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  25493. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  25494. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  25495. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  25496. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  25497. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  25498. /* HMAC */
  25499. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  25500. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  25501. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  25502. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  25503. #else
  25504. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  25505. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  25506. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  25507. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  25508. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  25509. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  25510. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  25511. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  25512. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  25513. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  25514. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  25515. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  25516. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  25517. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  25518. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  25519. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  25520. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  25521. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  25522. 0x55
  25523. #endif
  25524. };
  25525. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  25526. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25527. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  25528. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  25529. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  25530. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  25531. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  25532. };
  25533. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25534. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  25535. DYNAMIC_TYPE_TMP_BUFFER);
  25536. if (userB == NULL) {
  25537. ret = WC_TEST_RET_ENC_ERRNO;
  25538. }
  25539. #ifdef WOLFSSL_ECIES_OLD
  25540. if (ret == 0) {
  25541. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  25542. DYNAMIC_TYPE_TMP_BUFFER);
  25543. if (userA == NULL) {
  25544. ret = WC_TEST_RET_ENC_ERRNO;
  25545. }
  25546. }
  25547. #endif
  25548. #endif
  25549. if (ret == 0) {
  25550. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  25551. if (ret != 0)
  25552. ret = WC_TEST_RET_ENC_EC(ret);
  25553. }
  25554. if (ret == 0) {
  25555. userBInit = 1;
  25556. #ifdef WOLFSSL_ECIES_OLD
  25557. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25558. if (ret != 0)
  25559. ret = WC_TEST_RET_ENC_EC(ret);
  25560. }
  25561. if (ret == 0) {
  25562. userAInit = 1;
  25563. tmpKey = userA;
  25564. #else
  25565. tmpKey = NULL;
  25566. #endif
  25567. }
  25568. if (ret == 0) {
  25569. #ifdef WOLFSSL_QNX_CAAM
  25570. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  25571. sizeof(pubKey), userB, ECC_SECP256R1);
  25572. #else
  25573. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  25574. userB, ECC_SECP256R1);
  25575. #endif
  25576. if (ret != 0)
  25577. ret = WC_TEST_RET_ENC_EC(ret);
  25578. }
  25579. #ifdef WOLFSSL_ECIES_OLD
  25580. if (ret == 0) {
  25581. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  25582. ECC_SECP256R1);
  25583. if (ret != 0)
  25584. ret = WC_TEST_RET_ENC_EC(ret);
  25585. }
  25586. #endif
  25587. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  25588. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  25589. !defined(HAVE_SELFTEST)
  25590. if (ret == 0) {
  25591. ret = wc_ecc_set_rng(userB, rng);
  25592. if (ret != 0) {
  25593. ret = WC_TEST_RET_ENC_EC(ret);
  25594. }
  25595. }
  25596. #else
  25597. (void)rng;
  25598. #endif
  25599. if (ret == 0) {
  25600. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  25601. &plainSz, NULL);
  25602. if (ret != 0)
  25603. ret = WC_TEST_RET_ENC_EC(ret);
  25604. }
  25605. if (ret == 0) {
  25606. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25607. ret = WC_TEST_RET_ENC_NC;
  25608. }
  25609. }
  25610. if (userBInit)
  25611. wc_ecc_free(userB);
  25612. #ifdef WOLFSSL_ECIES_OLD
  25613. if (userAInit)
  25614. wc_ecc_free(userA);
  25615. #endif
  25616. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25617. if (userB != NULL) {
  25618. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25619. }
  25620. #ifdef WOLFSSL_ECIES_OLD
  25621. if (userA != NULL) {
  25622. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25623. }
  25624. #endif
  25625. #endif
  25626. return ret;
  25627. }
  25628. #endif
  25629. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  25630. byte encAlgo, byte kdfAlgo, byte macAlgo)
  25631. {
  25632. wc_test_ret_t ret = 0;
  25633. byte msg[48];
  25634. byte plain[48];
  25635. #ifdef WOLFSSL_ECIES_OLD
  25636. byte out[80];
  25637. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25638. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  25639. #else
  25640. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  25641. #endif
  25642. word32 outSz = sizeof(out);
  25643. word32 plainSz = sizeof(plain);
  25644. int i;
  25645. ecEncCtx* cliCtx = NULL;
  25646. ecEncCtx* srvCtx = NULL;
  25647. byte cliSalt[EXCHANGE_SALT_SZ];
  25648. byte srvSalt[EXCHANGE_SALT_SZ];
  25649. const byte* tmpSalt;
  25650. byte msg2[48];
  25651. byte plain2[48];
  25652. #ifdef WOLFSSL_ECIES_OLD
  25653. byte out2[80];
  25654. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25655. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  25656. #else
  25657. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  25658. #endif
  25659. word32 outSz2 = sizeof(out2);
  25660. word32 plainSz2 = sizeof(plain2);
  25661. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25662. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  25663. DYNAMIC_TYPE_TMP_BUFFER);
  25664. #else
  25665. ecc_key tmpKey[1];
  25666. #endif
  25667. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25668. if (tmpKey == NULL) {
  25669. ERROR_OUT(MEMORY_E, done);
  25670. }
  25671. #endif
  25672. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  25673. if (ret != 0)
  25674. goto done;
  25675. /* set message to incrementing 0,1,2,etc... */
  25676. for (i = 0; i < (int)sizeof(msg); i++)
  25677. msg[i] = i;
  25678. /* encrypt msg to B */
  25679. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  25680. if (ret != 0) {
  25681. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25682. }
  25683. #ifdef WOLFSSL_ECIES_OLD
  25684. tmpKey->dp = userA->dp;
  25685. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  25686. if (ret != 0) {
  25687. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25688. }
  25689. #endif
  25690. /* decrypt msg from A */
  25691. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  25692. if (ret != 0) {
  25693. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25694. }
  25695. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25696. ret = WC_TEST_RET_ENC_NC; goto done;
  25697. }
  25698. #ifndef WOLFSSL_ECIES_OLD
  25699. /* A decrypts msg (response) from B */
  25700. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  25701. if (ret != 0)
  25702. goto done;
  25703. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25704. ret = WC_TEST_RET_ENC_NC; goto done;
  25705. }
  25706. #endif
  25707. /* let's verify message exchange works, A is client, B is server */
  25708. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25709. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25710. if (cliCtx == NULL || srvCtx == NULL) {
  25711. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  25712. }
  25713. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  25714. if (ret != 0)
  25715. goto done;
  25716. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  25717. if (ret != 0)
  25718. goto done;
  25719. /* get salt to send to peer */
  25720. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  25721. if (tmpSalt == NULL) {
  25722. ret = WC_TEST_RET_ENC_NC; goto done;
  25723. }
  25724. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25725. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  25726. if (tmpSalt == NULL) {
  25727. ret = WC_TEST_RET_ENC_NC; goto done;
  25728. }
  25729. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25730. /* in actual use, we'd get the peer's salt over the transport */
  25731. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  25732. if (ret != 0)
  25733. goto done;
  25734. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  25735. if (ret != 0)
  25736. goto done;
  25737. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  25738. if (ret != 0)
  25739. goto done;
  25740. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  25741. if (ret != 0)
  25742. goto done;
  25743. /* get encrypted msg (request) to send to B */
  25744. outSz = sizeof(out);
  25745. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  25746. if (ret != 0)
  25747. goto done;
  25748. #ifndef WOLFSSL_ECIES_OLD
  25749. wc_ecc_free(tmpKey);
  25750. #endif
  25751. /* B decrypts msg (request) from A */
  25752. plainSz = sizeof(plain);
  25753. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  25754. if (ret != 0)
  25755. goto done;
  25756. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25757. ret = WC_TEST_RET_ENC_NC; goto done;
  25758. }
  25759. /* msg2 (response) from B to A */
  25760. for (i = 0; i < (int)sizeof(msg2); i++)
  25761. msg2[i] = i + sizeof(msg2);
  25762. /* get encrypted msg (response) to send to B */
  25763. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  25764. &outSz2, srvCtx);
  25765. if (ret != 0)
  25766. goto done;
  25767. #ifdef WOLFSSL_ECIES_OLD
  25768. tmpKey->dp = userB->dp;
  25769. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  25770. if (ret != 0) {
  25771. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25772. }
  25773. #else
  25774. wc_ecc_free(tmpKey);
  25775. #endif
  25776. /* A decrypts msg (response) from B */
  25777. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  25778. cliCtx);
  25779. if (ret != 0)
  25780. goto done;
  25781. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  25782. ret = WC_TEST_RET_ENC_NC; goto done;
  25783. }
  25784. #if defined(HAVE_COMP_KEY) && \
  25785. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  25786. /* Create new client and server contexts. */
  25787. wc_ecc_ctx_free(srvCtx);
  25788. wc_ecc_ctx_free(cliCtx);
  25789. /* let's verify message exchange works, A is client, B is server */
  25790. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25791. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25792. if (cliCtx == NULL || srvCtx == NULL) {
  25793. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  25794. }
  25795. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  25796. if (ret != 0)
  25797. goto done;
  25798. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  25799. if (ret != 0)
  25800. goto done;
  25801. /* get salt to send to peer */
  25802. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  25803. if (tmpSalt == NULL) {
  25804. ret = WC_TEST_RET_ENC_NC; goto done;
  25805. }
  25806. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25807. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  25808. if (tmpSalt == NULL) {
  25809. ret = WC_TEST_RET_ENC_NC; goto done;
  25810. }
  25811. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25812. /* in actual use, we'd get the peer's salt over the transport */
  25813. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  25814. if (ret != 0)
  25815. goto done;
  25816. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  25817. if (ret != 0)
  25818. goto done;
  25819. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  25820. if (ret != 0)
  25821. goto done;
  25822. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  25823. if (ret != 0)
  25824. goto done;
  25825. /* get encrypted msg (request) to send to B - compressed public key */
  25826. outSz = sizeof(out);
  25827. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  25828. 1);
  25829. if (ret != 0)
  25830. goto done;
  25831. #ifndef WOLFSSL_ECIES_OLD
  25832. wc_ecc_free(tmpKey);
  25833. #endif
  25834. /* B decrypts msg (request) from A - out has a compressed public key */
  25835. plainSz = sizeof(plain);
  25836. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  25837. if (ret != 0)
  25838. goto done;
  25839. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25840. ret = WC_TEST_RET_ENC_NC; goto done;
  25841. }
  25842. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  25843. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  25844. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  25845. ret = ecc_encrypt_kat(rng);
  25846. #endif
  25847. done:
  25848. /* cleanup */
  25849. wc_ecc_ctx_free(srvCtx);
  25850. wc_ecc_ctx_free(cliCtx);
  25851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25852. if (tmpKey != NULL) {
  25853. wc_ecc_free(tmpKey);
  25854. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25855. }
  25856. #else
  25857. wc_ecc_free(tmpKey);
  25858. #endif
  25859. return ret;
  25860. }
  25861. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  25862. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  25863. {
  25864. WC_RNG rng;
  25865. wc_test_ret_t ret;
  25866. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25867. ecc_key *userA;
  25868. ecc_key *userB;
  25869. #else
  25870. ecc_key userA[1];
  25871. ecc_key userB[1];
  25872. #endif
  25873. #ifndef HAVE_FIPS
  25874. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25875. #else
  25876. ret = wc_InitRng(&rng);
  25877. #endif
  25878. if (ret != 0)
  25879. return WC_TEST_RET_ENC_EC(ret);
  25880. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25881. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  25882. DYNAMIC_TYPE_TMP_BUFFER);
  25883. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  25884. DYNAMIC_TYPE_TMP_BUFFER);
  25885. if ((userA == NULL) || (userB == NULL)) {
  25886. ERROR_OUT(MEMORY_E, done);
  25887. }
  25888. #endif
  25889. XMEMSET(userA, 0, sizeof *userA);
  25890. XMEMSET(userB, 0, sizeof *userB);
  25891. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25892. if (ret != 0)
  25893. goto done;
  25894. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  25895. if (ret != 0)
  25896. goto done;
  25897. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  25898. #if defined(WOLFSSL_ASYNC_CRYPT)
  25899. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  25900. #endif
  25901. if (ret != 0){
  25902. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25903. }
  25904. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  25905. #if defined(WOLFSSL_ASYNC_CRYPT)
  25906. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  25907. #endif
  25908. if (ret != 0){
  25909. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25910. }
  25911. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  25912. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  25913. !defined(HAVE_SELFTEST)
  25914. ret = wc_ecc_set_rng(userA, &rng);
  25915. if (ret != 0) {
  25916. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25917. }
  25918. ret = wc_ecc_set_rng(userB, &rng);
  25919. if (ret != 0) {
  25920. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25921. }
  25922. #endif
  25923. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  25924. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25925. #ifdef WOLFSSL_AES_128
  25926. if (ret == 0) {
  25927. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  25928. ecHKDF_SHA256, ecHMAC_SHA256);
  25929. if (ret != 0) {
  25930. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  25931. }
  25932. }
  25933. #endif
  25934. #ifdef WOLFSSL_AES_256
  25935. if (ret == 0) {
  25936. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  25937. ecHKDF_SHA256, ecHMAC_SHA256);
  25938. if (ret != 0) {
  25939. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  25940. }
  25941. }
  25942. #endif
  25943. #endif
  25944. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  25945. #ifdef WOLFSSL_AES_128
  25946. if (ret == 0) {
  25947. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  25948. ecHKDF_SHA256, ecHMAC_SHA256);
  25949. if (ret != 0) {
  25950. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  25951. }
  25952. }
  25953. #endif
  25954. #ifdef WOLFSSL_AES_256
  25955. if (ret == 0) {
  25956. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  25957. ecHKDF_SHA256, ecHMAC_SHA256);
  25958. if (ret != 0) {
  25959. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  25960. }
  25961. }
  25962. #endif
  25963. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  25964. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25965. if (ret == 0) {
  25966. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  25967. }
  25968. #endif
  25969. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  25970. done:
  25971. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25972. if (userA != NULL) {
  25973. wc_ecc_free(userA);
  25974. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25975. }
  25976. if (userB != NULL) {
  25977. wc_ecc_free(userB);
  25978. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25979. }
  25980. #else
  25981. wc_ecc_free(userB);
  25982. wc_ecc_free(userA);
  25983. #endif
  25984. wc_FreeRng(&rng);
  25985. return ret;
  25986. }
  25987. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  25988. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  25989. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  25990. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  25991. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  25992. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  25993. {
  25994. size_t bytes;
  25995. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25996. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25997. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25998. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25999. #else
  26000. ecc_key cliKey[1];
  26001. ecc_key servKey[1];
  26002. ecc_key tmpKey[1];
  26003. #endif
  26004. WC_RNG rng;
  26005. word32 idx = 0;
  26006. wc_test_ret_t ret;
  26007. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  26008. byte in[] = "Everyone gets Friday off. ecc p";
  26009. word32 inLen = (word32)XSTRLEN((char*)in);
  26010. byte out[256];
  26011. byte plain[256];
  26012. int verify = 0;
  26013. word32 x;
  26014. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26015. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  26016. ERROR_OUT(MEMORY_E, done);
  26017. #endif
  26018. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  26019. if (ret != 0)
  26020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26021. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  26022. if (ret != 0)
  26023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26024. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  26025. if (ret != 0)
  26026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26027. bytes = (size_t)sizeof_ecc_clikey_der_256;
  26028. /* place client key into ecc_key struct cliKey */
  26029. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  26030. (word32)bytes);
  26031. if (ret != 0)
  26032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26033. idx = 0;
  26034. bytes = (size_t)sizeof_ecc_key_der_256;
  26035. /* place server key into ecc_key struct servKey */
  26036. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  26037. (word32)bytes);
  26038. if (ret != 0)
  26039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26040. #ifndef WC_NO_RNG
  26041. #ifndef HAVE_FIPS
  26042. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26043. #else
  26044. ret = wc_InitRng(&rng);
  26045. #endif
  26046. if (ret != 0)
  26047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26048. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26049. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26050. !defined(HAVE_SELFTEST)
  26051. ret = wc_ecc_set_rng(cliKey, &rng);
  26052. if (ret != 0)
  26053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26054. ret = wc_ecc_set_rng(servKey, &rng);
  26055. if (ret != 0)
  26056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26057. #endif
  26058. #endif /* !WC_NO_RNG */
  26059. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  26060. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  26061. {
  26062. word32 y;
  26063. /* test encrypt and decrypt if they're available */
  26064. x = sizeof(out);
  26065. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  26066. if (ret < 0)
  26067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26068. #ifdef WOLFSSL_ECIES_OLD
  26069. tmpKey->dp = cliKey->dp;
  26070. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  26071. if (ret != 0) {
  26072. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  26073. }
  26074. #endif
  26075. y = sizeof(plain);
  26076. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  26077. if (ret < 0)
  26078. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26079. if (XMEMCMP(plain, in, inLen))
  26080. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26081. }
  26082. #endif
  26083. x = sizeof(out);
  26084. do {
  26085. #if defined(WOLFSSL_ASYNC_CRYPT)
  26086. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26087. #endif
  26088. if (ret == 0)
  26089. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  26090. } while (ret == WC_PENDING_E);
  26091. if (ret < 0)
  26092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26093. TEST_SLEEP();
  26094. XMEMSET(plain, 0, sizeof(plain));
  26095. do {
  26096. #if defined(WOLFSSL_ASYNC_CRYPT)
  26097. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26098. #endif
  26099. if (ret == 0)
  26100. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  26101. cliKey);
  26102. } while (ret == WC_PENDING_E);
  26103. if (ret < 0)
  26104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26105. if (verify != 1)
  26106. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26107. TEST_SLEEP();
  26108. #ifdef WOLFSSL_CERT_EXT
  26109. idx = 0;
  26110. bytes = sizeof_ecc_clikeypub_der_256;
  26111. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  26112. (word32) bytes);
  26113. if (ret != 0)
  26114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26115. #endif
  26116. ret = 0;
  26117. done:
  26118. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26119. if (cliKey != NULL) {
  26120. wc_ecc_free(cliKey);
  26121. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26122. }
  26123. if (servKey != NULL) {
  26124. wc_ecc_free(servKey);
  26125. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26126. }
  26127. if (tmpKey != NULL) {
  26128. wc_ecc_free(tmpKey);
  26129. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26130. }
  26131. #else
  26132. wc_ecc_free(cliKey);
  26133. wc_ecc_free(servKey);
  26134. wc_ecc_free(tmpKey);
  26135. #endif
  26136. wc_FreeRng(&rng);
  26137. return ret;
  26138. }
  26139. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  26140. #endif /* HAVE_ECC */
  26141. #ifdef HAVE_CURVE25519
  26142. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26143. defined(HAVE_CURVE25519_KEY_IMPORT)
  26144. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26145. #define X25519_TEST_CNT 5
  26146. #else
  26147. #define X25519_TEST_CNT 1
  26148. #endif
  26149. static wc_test_ret_t curve25519_overflow_test(void)
  26150. {
  26151. /* secret key for party a */
  26152. byte sa[X25519_TEST_CNT][32] = {
  26153. {
  26154. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  26155. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  26156. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  26157. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  26158. },
  26159. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26160. {
  26161. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  26162. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  26163. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  26164. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  26165. },
  26166. {
  26167. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26168. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26169. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26170. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26171. },
  26172. {
  26173. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26174. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26175. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26176. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26177. },
  26178. {
  26179. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26180. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26181. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26182. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26183. }
  26184. #endif
  26185. };
  26186. /* public key for party b */
  26187. byte pb[X25519_TEST_CNT][32] = {
  26188. {
  26189. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26190. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26191. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26192. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  26193. },
  26194. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26195. {
  26196. /* 0xff first byte in original - invalid! */
  26197. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26198. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26199. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26200. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  26201. },
  26202. {
  26203. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  26204. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  26205. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  26206. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  26207. },
  26208. {
  26209. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  26210. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  26211. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  26212. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  26213. },
  26214. {
  26215. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  26216. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  26217. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  26218. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  26219. }
  26220. #endif
  26221. };
  26222. /* expected shared key */
  26223. byte ss[X25519_TEST_CNT][32] = {
  26224. {
  26225. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  26226. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  26227. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  26228. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  26229. },
  26230. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26231. {
  26232. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  26233. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  26234. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  26235. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  26236. },
  26237. {
  26238. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26239. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26240. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26241. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  26242. },
  26243. {
  26244. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26245. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26246. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26247. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  26248. },
  26249. {
  26250. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26251. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26252. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26253. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  26254. }
  26255. #endif
  26256. };
  26257. wc_test_ret_t ret = 0;
  26258. int i;
  26259. word32 y;
  26260. byte shared[32];
  26261. curve25519_key userA;
  26262. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  26263. for (i = 0; i < X25519_TEST_CNT; i++) {
  26264. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  26265. sizeof(pb[i]), &userA) != 0) {
  26266. ret = WC_TEST_RET_ENC_I(i); break;
  26267. }
  26268. /* test against known test vector */
  26269. XMEMSET(shared, 0, sizeof(shared));
  26270. y = sizeof(shared);
  26271. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  26272. ret = WC_TEST_RET_ENC_I(i); break;
  26273. }
  26274. if (XMEMCMP(ss[i], shared, y)) {
  26275. ret = WC_TEST_RET_ENC_I(i); break;
  26276. }
  26277. }
  26278. wc_curve25519_free(&userA);
  26279. return ret;
  26280. }
  26281. /* Test the wc_curve25519_check_public API.
  26282. *
  26283. * returns 0 on success and -ve on failure.
  26284. */
  26285. static wc_test_ret_t curve25519_check_public_test(void)
  26286. {
  26287. wc_test_ret_t ret;
  26288. /* Little-endian values that will fail */
  26289. byte fail_le[][CURVE25519_KEYSIZE] = {
  26290. {
  26291. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26292. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26293. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26295. },
  26296. {
  26297. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26298. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26299. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26300. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26301. },
  26302. {
  26303. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26305. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26306. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  26307. },
  26308. };
  26309. /* Big-endian values that will fail */
  26310. byte fail_be[][CURVE25519_KEYSIZE] = {
  26311. {
  26312. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26313. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26316. },
  26317. {
  26318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26322. },
  26323. {
  26324. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26327. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26328. },
  26329. };
  26330. /* Good or valid public value */
  26331. byte good[CURVE25519_KEYSIZE] = {
  26332. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26334. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26336. };
  26337. int i;
  26338. /* Parameter checks */
  26339. /* NULL pointer */
  26340. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  26341. if (ret != BAD_FUNC_ARG) {
  26342. return WC_TEST_RET_ENC_EC(ret);
  26343. }
  26344. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  26345. if (ret != BAD_FUNC_ARG) {
  26346. return WC_TEST_RET_ENC_EC(ret);
  26347. }
  26348. /* Length of 0 treated differently to other invalid lengths for TLS */
  26349. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  26350. if (ret != BUFFER_E)
  26351. return WC_TEST_RET_ENC_EC(ret);
  26352. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  26353. if (ret != BUFFER_E)
  26354. return WC_TEST_RET_ENC_EC(ret);
  26355. /* Length not CURVE25519_KEYSIZE */
  26356. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  26357. if (i == CURVE25519_KEYSIZE)
  26358. continue;
  26359. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  26360. ECC_BAD_ARG_E) {
  26361. return WC_TEST_RET_ENC_I(i);
  26362. }
  26363. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  26364. ECC_BAD_ARG_E) {
  26365. return WC_TEST_RET_ENC_I(i);
  26366. }
  26367. }
  26368. /* Little-endian fail cases */
  26369. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  26370. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  26371. EC25519_LITTLE_ENDIAN) == 0) {
  26372. return WC_TEST_RET_ENC_I(i);
  26373. }
  26374. }
  26375. /* Big-endian fail cases */
  26376. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  26377. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  26378. EC25519_BIG_ENDIAN) == 0) {
  26379. return WC_TEST_RET_ENC_I(i);
  26380. }
  26381. }
  26382. /* Check a valid public value works! */
  26383. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  26384. EC25519_LITTLE_ENDIAN);
  26385. if (ret != 0) {
  26386. return WC_TEST_RET_ENC_EC(ret);
  26387. }
  26388. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  26389. EC25519_BIG_ENDIAN);
  26390. if (ret != 0) {
  26391. return WC_TEST_RET_ENC_EC(ret);
  26392. }
  26393. return 0;
  26394. }
  26395. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  26396. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  26397. defined(HAVE_CURVE25519_KEY_IMPORT)
  26398. static wc_test_ret_t curve255519_der_test(void)
  26399. {
  26400. wc_test_ret_t ret = 0;
  26401. /* certs/statickeys/x25519.der */
  26402. const byte kCurve25519PrivDer[] = {
  26403. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  26404. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  26405. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  26406. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  26407. };
  26408. /* certs/statickeys/x25519-pub.der */
  26409. const byte kCurve25519PubDer[] = {
  26410. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  26411. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  26412. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  26413. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  26414. };
  26415. curve25519_key key;
  26416. byte output[128];
  26417. word32 outputSz = 128;
  26418. word32 idx;
  26419. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  26420. if (ret != 0)
  26421. return WC_TEST_RET_ENC_EC(ret);
  26422. /* Test decode / encode of Curve25519 private key only */
  26423. if (ret == 0) {
  26424. idx = 0;
  26425. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  26426. (word32)sizeof(kCurve25519PrivDer));
  26427. if (ret < 0)
  26428. ret = WC_TEST_RET_ENC_EC(ret);
  26429. }
  26430. if (ret == 0) {
  26431. outputSz = (word32)sizeof(output);
  26432. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  26433. if (ret >= 0) {
  26434. outputSz = (word32)ret;
  26435. ret = 0;
  26436. }
  26437. else {
  26438. ret = WC_TEST_RET_ENC_EC(ret);
  26439. }
  26440. }
  26441. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  26442. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  26443. ret = WC_TEST_RET_ENC_NC;
  26444. }
  26445. /* Test decode / encode of Curve25519 public key only */
  26446. if (ret == 0) {
  26447. idx = 0;
  26448. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  26449. (word32)sizeof(kCurve25519PubDer));
  26450. if (ret < 0)
  26451. ret = WC_TEST_RET_ENC_EC(ret);
  26452. }
  26453. if (ret == 0) {
  26454. outputSz = (word32)sizeof(output);
  26455. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  26456. if (ret >= 0) {
  26457. outputSz = (word32)ret;
  26458. ret = 0;
  26459. }
  26460. else {
  26461. ret = WC_TEST_RET_ENC_EC(ret);
  26462. }
  26463. }
  26464. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  26465. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  26466. ret = WC_TEST_RET_ENC_NC;
  26467. }
  26468. wc_curve25519_free(&key);
  26469. return ret;
  26470. }
  26471. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  26472. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  26473. {
  26474. WC_RNG rng;
  26475. wc_test_ret_t ret;
  26476. #ifdef HAVE_CURVE25519_SHARED_SECRET
  26477. byte sharedA[32];
  26478. byte sharedB[32];
  26479. word32 y;
  26480. #endif
  26481. #ifdef HAVE_CURVE25519_KEY_EXPORT
  26482. byte exportBuf[32];
  26483. #endif
  26484. word32 x = 0;
  26485. curve25519_key userA, userB, pubKey;
  26486. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26487. defined(HAVE_CURVE25519_KEY_IMPORT)
  26488. /* test vectors from
  26489. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  26490. */
  26491. /* secret key for party a */
  26492. byte sa[] = {
  26493. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  26494. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  26495. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  26496. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  26497. };
  26498. /* public key for party a */
  26499. byte pa[] = {
  26500. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  26501. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  26502. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  26503. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  26504. };
  26505. /* secret key for party b */
  26506. byte sb[] = {
  26507. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  26508. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  26509. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  26510. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  26511. };
  26512. /* public key for party b */
  26513. byte pb[] = {
  26514. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  26515. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  26516. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  26517. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  26518. };
  26519. /* expected shared key */
  26520. byte ss[] = {
  26521. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  26522. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  26523. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  26524. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  26525. };
  26526. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  26527. (void)x;
  26528. #ifndef HAVE_FIPS
  26529. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26530. #else
  26531. ret = wc_InitRng(&rng);
  26532. #endif
  26533. if (ret != 0)
  26534. return WC_TEST_RET_ENC_EC(ret);
  26535. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  26536. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  26537. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  26538. /* make curve25519 keys */
  26539. ret = wc_curve25519_make_key(&rng, 32, &userA);
  26540. if (ret != 0)
  26541. return WC_TEST_RET_ENC_EC(ret);
  26542. ret = wc_curve25519_make_key(&rng, 32, &userB);
  26543. if (ret != 0)
  26544. return WC_TEST_RET_ENC_EC(ret);
  26545. #ifdef HAVE_CURVE25519_SHARED_SECRET
  26546. /* find shared secret key */
  26547. x = sizeof(sharedA);
  26548. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  26549. printf("wc_curve25519_shared_secret 1 failed\n");
  26550. return WC_TEST_RET_ENC_EC(ret);
  26551. }
  26552. y = sizeof(sharedB);
  26553. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  26554. printf("wc_curve25519_shared_secret 2 failed\n");
  26555. return WC_TEST_RET_ENC_EC(ret);
  26556. }
  26557. /* compare shared secret keys to test they are the same */
  26558. if (y != x)
  26559. return WC_TEST_RET_ENC_NC;
  26560. if (XMEMCMP(sharedA, sharedB, x))
  26561. return WC_TEST_RET_ENC_NC;
  26562. #endif
  26563. #ifdef HAVE_CURVE25519_KEY_EXPORT
  26564. /* export a public key and import it for another user */
  26565. x = sizeof(exportBuf);
  26566. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  26567. if (ret != 0)
  26568. return WC_TEST_RET_ENC_EC(ret);
  26569. #ifdef HAVE_CURVE25519_KEY_IMPORT
  26570. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  26571. if (ret != 0)
  26572. return WC_TEST_RET_ENC_EC(ret);
  26573. #endif
  26574. #endif
  26575. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26576. defined(HAVE_CURVE25519_KEY_IMPORT)
  26577. /* test shared key after importing a public key */
  26578. XMEMSET(sharedB, 0, sizeof(sharedB));
  26579. y = sizeof(sharedB);
  26580. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  26581. return WC_TEST_RET_ENC_NC;
  26582. }
  26583. if (XMEMCMP(sharedA, sharedB, y))
  26584. return WC_TEST_RET_ENC_NC;
  26585. /* import RFC test vectors and compare shared key */
  26586. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  26587. &userA);
  26588. if (ret != 0)
  26589. return WC_TEST_RET_ENC_EC(ret);
  26590. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  26591. &userB);
  26592. if (ret != 0)
  26593. return WC_TEST_RET_ENC_EC(ret);
  26594. /* test against known test vector */
  26595. XMEMSET(sharedB, 0, sizeof(sharedB));
  26596. y = sizeof(sharedB);
  26597. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  26598. if (ret != 0)
  26599. return WC_TEST_RET_ENC_EC(ret);
  26600. if (XMEMCMP(ss, sharedB, y))
  26601. return WC_TEST_RET_ENC_NC;
  26602. /* test swapping roles of keys and generating same shared key */
  26603. XMEMSET(sharedB, 0, sizeof(sharedB));
  26604. y = sizeof(sharedB);
  26605. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  26606. if (ret != 0)
  26607. return WC_TEST_RET_ENC_EC(ret);
  26608. if (XMEMCMP(ss, sharedB, y))
  26609. return WC_TEST_RET_ENC_NC;
  26610. /* test with 1 generated key and 1 from known test vector */
  26611. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  26612. &userA);
  26613. if (ret != 0)
  26614. return WC_TEST_RET_ENC_EC(ret);
  26615. wc_curve25519_free(&userB);
  26616. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  26617. ret = wc_curve25519_make_key(&rng, 32, &userB);
  26618. if (ret != 0)
  26619. return WC_TEST_RET_ENC_EC(ret);
  26620. x = sizeof(sharedA);
  26621. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  26622. if (ret != 0)
  26623. return WC_TEST_RET_ENC_EC(ret);
  26624. y = sizeof(sharedB);
  26625. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  26626. if (ret != 0)
  26627. return WC_TEST_RET_ENC_EC(ret);
  26628. /* compare shared secret keys to test they are the same */
  26629. if (y != x)
  26630. return WC_TEST_RET_ENC_NC;
  26631. if (XMEMCMP(sharedA, sharedB, x))
  26632. return WC_TEST_RET_ENC_NC;
  26633. ret = curve25519_overflow_test();
  26634. if (ret != 0)
  26635. return ret;
  26636. ret = curve25519_check_public_test();
  26637. if (ret != 0)
  26638. return ret;
  26639. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  26640. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  26641. defined(HAVE_CURVE25519_KEY_IMPORT)
  26642. ret = curve255519_der_test();
  26643. if (ret != 0)
  26644. return ret;
  26645. #endif
  26646. /* clean up keys when done */
  26647. wc_curve25519_free(&pubKey);
  26648. wc_curve25519_free(&userB);
  26649. wc_curve25519_free(&userA);
  26650. wc_FreeRng(&rng);
  26651. return 0;
  26652. }
  26653. #endif /* HAVE_CURVE25519 */
  26654. #ifdef HAVE_ED25519
  26655. #ifdef WOLFSSL_TEST_CERT
  26656. static wc_test_ret_t ed25519_test_cert(void)
  26657. {
  26658. DecodedCert cert[2];
  26659. DecodedCert* serverCert = NULL;
  26660. DecodedCert* caCert = NULL;
  26661. #ifdef HAVE_ED25519_VERIFY
  26662. ed25519_key key;
  26663. ed25519_key* pubKey = NULL;
  26664. int verify;
  26665. #endif /* HAVE_ED25519_VERIFY */
  26666. wc_test_ret_t ret;
  26667. byte* tmp;
  26668. size_t bytes;
  26669. XFILE file;
  26670. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26671. if (tmp == NULL) {
  26672. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26673. }
  26674. #ifdef USE_CERT_BUFFERS_256
  26675. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  26676. bytes = sizeof_ca_ed25519_cert;
  26677. #elif !defined(NO_FILESYSTEM)
  26678. file = XFOPEN(caEd25519Cert, "rb");
  26679. if (file == NULL) {
  26680. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26681. }
  26682. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  26683. XFCLOSE(file);
  26684. if (bytes == 0)
  26685. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26686. #else
  26687. /* No certificate to use. */
  26688. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26689. #endif
  26690. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  26691. caCert = &cert[0];
  26692. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  26693. if (ret != 0)
  26694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26695. #ifdef USE_CERT_BUFFERS_256
  26696. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  26697. bytes = sizeof_server_ed25519_cert;
  26698. #elif !defined(NO_FILESYSTEM)
  26699. file = XFOPEN(serverEd25519Cert, "rb");
  26700. if (file == NULL) {
  26701. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26702. }
  26703. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  26704. XFCLOSE(file);
  26705. if (bytes == 0)
  26706. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26707. #else
  26708. /* No certificate to use. */
  26709. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26710. #endif
  26711. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  26712. serverCert = &cert[1];
  26713. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  26714. if (ret != 0)
  26715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26716. #ifdef HAVE_ED25519_VERIFY
  26717. ret = wc_ed25519_init(&key);
  26718. if (ret < 0)
  26719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26720. pubKey = &key;
  26721. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  26722. pubKey);
  26723. if (ret < 0)
  26724. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26725. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  26726. serverCert->source + serverCert->certBegin,
  26727. serverCert->sigIndex - serverCert->certBegin,
  26728. &verify, pubKey);
  26729. if (ret < 0 || verify != 1)
  26730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26731. #endif /* HAVE_ED25519_VERIFY */
  26732. done:
  26733. if (tmp != NULL)
  26734. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26735. #ifdef HAVE_ED25519_VERIFY
  26736. wc_ed25519_free(pubKey);
  26737. #endif /* HAVE_ED25519_VERIFY */
  26738. if (caCert != NULL)
  26739. FreeDecodedCert(caCert);
  26740. if (serverCert != NULL)
  26741. FreeDecodedCert(serverCert);
  26742. return ret;
  26743. }
  26744. static wc_test_ret_t ed25519_test_make_cert(void)
  26745. {
  26746. WC_RNG rng;
  26747. Cert cert;
  26748. DecodedCert decode;
  26749. ed25519_key key;
  26750. ed25519_key* privKey = NULL;
  26751. wc_test_ret_t ret = 0;
  26752. byte* tmp = NULL;
  26753. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  26754. #ifndef HAVE_FIPS
  26755. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26756. #else
  26757. ret = wc_InitRng(&rng);
  26758. #endif
  26759. if (ret != 0)
  26760. return WC_TEST_RET_ENC_EC(ret);
  26761. wc_ed25519_init(&key);
  26762. privKey = &key;
  26763. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  26764. cert.daysValid = 365 * 2;
  26765. cert.selfSigned = 1;
  26766. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  26767. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  26768. cert.isCA = 0;
  26769. #ifdef WOLFSSL_CERT_EXT
  26770. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  26771. if (ret < 0)
  26772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26773. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  26774. if (ret < 0)
  26775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26776. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  26777. if (ret < 0)
  26778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26779. #endif
  26780. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26781. if (tmp == NULL) {
  26782. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26783. }
  26784. cert.sigType = CTC_ED25519;
  26785. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  26786. if (ret < 0)
  26787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26788. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  26789. ED25519_TYPE, privKey, &rng);
  26790. if (ret < 0)
  26791. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26792. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  26793. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  26794. FreeDecodedCert(&decode);
  26795. if (ret != 0)
  26796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26797. done:
  26798. if (tmp != NULL)
  26799. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26800. wc_ed25519_free(privKey);
  26801. wc_FreeRng(&rng);
  26802. return ret;
  26803. }
  26804. #endif /* WOLFSSL_TEST_CERT */
  26805. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  26806. defined(HAVE_ED25519_KEY_IMPORT)
  26807. static wc_test_ret_t ed25519ctx_test(void)
  26808. {
  26809. wc_test_ret_t ret;
  26810. byte out[ED25519_SIG_SIZE];
  26811. word32 outlen;
  26812. #ifdef HAVE_ED25519_VERIFY
  26813. int verify = 0;
  26814. #endif /* HAVE_ED25519_VERIFY */
  26815. ed25519_key key;
  26816. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  26817. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  26818. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  26819. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  26820. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  26821. };
  26822. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  26823. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  26824. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  26825. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  26826. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  26827. };
  26828. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  26829. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  26830. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  26831. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  26832. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  26833. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  26834. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  26835. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  26836. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  26837. };
  26838. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  26839. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  26840. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  26841. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  26842. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  26843. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  26844. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  26845. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  26846. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  26847. };
  26848. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  26849. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  26850. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  26851. };
  26852. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  26853. 0x66,0x6f,0x6f
  26854. };
  26855. outlen = sizeof(out);
  26856. XMEMSET(out, 0, sizeof(out));
  26857. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  26858. if (ret != 0)
  26859. return 10800;
  26860. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  26861. sizeof(pKeyCtx), &key);
  26862. if (ret == 0)
  26863. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26864. contextCtx, sizeof(contextCtx));
  26865. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  26866. ret = WC_TEST_RET_ENC_NC;
  26867. #if defined(HAVE_ED25519_VERIFY)
  26868. /* test verify on good msg */
  26869. if (ret == 0)
  26870. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  26871. &verify, &key, contextCtx, sizeof(contextCtx));
  26872. if (ret == 0 && verify != 1)
  26873. ret = WC_TEST_RET_ENC_NC;
  26874. #endif
  26875. if (ret == 0)
  26876. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26877. NULL, 0);
  26878. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  26879. ret = WC_TEST_RET_ENC_NC;
  26880. #if defined(HAVE_ED25519_VERIFY)
  26881. /* test verify on good msg */
  26882. if (ret == 0)
  26883. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  26884. &verify, &key, NULL, 0);
  26885. if (ret == 0 && verify != 1)
  26886. ret = WC_TEST_RET_ENC_NC;
  26887. #endif
  26888. wc_ed25519_free(&key);
  26889. return ret;
  26890. }
  26891. static wc_test_ret_t ed25519ph_test(void)
  26892. {
  26893. wc_test_ret_t ret = 0;
  26894. byte out[ED25519_SIG_SIZE];
  26895. word32 outlen;
  26896. #ifdef HAVE_ED25519_VERIFY
  26897. int verify = 0;
  26898. #endif /* HAVE_ED25519_VERIFY */
  26899. ed25519_key key;
  26900. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  26901. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  26902. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  26903. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  26904. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  26905. };
  26906. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  26907. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  26908. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  26909. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  26910. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  26911. };
  26912. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  26913. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  26914. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  26915. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  26916. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  26917. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  26918. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  26919. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  26920. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  26921. };
  26922. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  26923. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  26924. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  26925. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  26926. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  26927. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  26928. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  26929. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  26930. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  26931. };
  26932. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  26933. 0x61,0x62,0x63
  26934. };
  26935. /* SHA-512 hash of msgPh */
  26936. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  26937. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  26938. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  26939. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  26940. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  26941. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  26942. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  26943. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  26944. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  26945. };
  26946. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  26947. 0x66,0x6f,0x6f
  26948. };
  26949. outlen = sizeof(out);
  26950. XMEMSET(out, 0, sizeof(out));
  26951. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  26952. if (ret != 0)
  26953. return WC_TEST_RET_ENC_EC(ret);
  26954. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  26955. sizeof(pKeyPh), &key);
  26956. if (ret == 0)
  26957. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26958. NULL, 0);
  26959. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  26960. ret = WC_TEST_RET_ENC_NC;
  26961. #if defined(HAVE_ED25519_VERIFY)
  26962. /* test verify on good msg */
  26963. if (ret == 0)
  26964. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  26965. &verify, &key, NULL, 0);
  26966. if (ret == 0 && verify != 1)
  26967. ret = WC_TEST_RET_ENC_NC;
  26968. #endif
  26969. if (ret == 0)
  26970. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26971. contextPh2, sizeof(contextPh2));
  26972. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  26973. ret = WC_TEST_RET_ENC_NC;
  26974. #if defined(HAVE_ED25519_VERIFY)
  26975. /* test verify on good msg */
  26976. if (ret == 0)
  26977. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  26978. &key, contextPh2, sizeof(contextPh2));
  26979. if (ret == 0 && verify != 1)
  26980. ret = WC_TEST_RET_ENC_NC;
  26981. #endif
  26982. if (ret == 0)
  26983. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26984. NULL, 0);
  26985. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  26986. ret = WC_TEST_RET_ENC_NC;
  26987. #if defined(HAVE_ED25519_VERIFY)
  26988. if (ret == 0)
  26989. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  26990. &verify, &key, NULL, 0);
  26991. if (ret == 0 && verify != 1)
  26992. ret = WC_TEST_RET_ENC_NC;
  26993. #endif
  26994. if (ret == 0)
  26995. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26996. contextPh2, sizeof(contextPh2));
  26997. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  26998. ret = WC_TEST_RET_ENC_NC;
  26999. #if defined(HAVE_ED25519_VERIFY)
  27000. if (ret == 0)
  27001. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  27002. &key, contextPh2, sizeof(contextPh2));
  27003. if (ret == 0 && verify != 1)
  27004. ret = WC_TEST_RET_ENC_NC;
  27005. #endif
  27006. wc_ed25519_free(&key);
  27007. return ret;
  27008. }
  27009. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27010. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  27011. {
  27012. wc_test_ret_t ret;
  27013. WC_RNG rng;
  27014. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  27015. defined(HAVE_ED25519_KEY_IMPORT)
  27016. byte out[ED25519_SIG_SIZE];
  27017. byte exportPKey[ED25519_KEY_SIZE];
  27018. byte exportSKey[ED25519_KEY_SIZE];
  27019. word32 exportPSz;
  27020. word32 exportSSz;
  27021. int i;
  27022. word32 outlen;
  27023. #ifdef HAVE_ED25519_VERIFY
  27024. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  27025. int j;
  27026. #endif
  27027. int verify;
  27028. #endif /* HAVE_ED25519_VERIFY */
  27029. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27030. word32 keySz, sigSz;
  27031. ed25519_key key;
  27032. ed25519_key key2;
  27033. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  27034. defined(HAVE_ED25519_KEY_IMPORT)
  27035. /* test vectors from
  27036. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  27037. */
  27038. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  27039. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27040. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27041. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27042. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27043. };
  27044. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  27045. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  27046. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  27047. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  27048. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  27049. };
  27050. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  27051. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  27052. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  27053. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  27054. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  27055. };
  27056. /* uncompressed test */
  27057. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  27058. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27059. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27060. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27061. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27062. };
  27063. /* compressed prefix test */
  27064. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  27065. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27066. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27067. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27068. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27069. };
  27070. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  27071. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  27072. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  27073. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  27074. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  27075. };
  27076. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  27077. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  27078. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  27079. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  27080. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  27081. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  27082. };
  27083. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  27084. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  27085. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  27086. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  27087. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  27088. };
  27089. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  27090. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  27091. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  27092. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  27093. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  27094. };
  27095. /* uncompressed test */
  27096. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  27097. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  27098. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  27099. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  27100. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  27101. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  27102. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  27103. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  27104. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  27105. 0xd7
  27106. };
  27107. /* compressed prefix */
  27108. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  27109. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  27110. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  27111. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  27112. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  27113. };
  27114. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  27115. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  27116. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  27117. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  27118. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  27119. };
  27120. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  27121. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  27122. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  27123. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  27124. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27125. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27126. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27127. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27128. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27129. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27130. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27131. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27132. };
  27133. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  27134. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  27135. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  27136. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  27137. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  27138. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  27139. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  27140. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  27141. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  27142. };
  27143. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  27144. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  27145. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  27146. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  27147. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  27148. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  27149. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  27150. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  27151. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  27152. };
  27153. /* uncompressed test */
  27154. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  27155. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27156. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27157. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27158. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27159. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27160. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27161. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27162. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27163. };
  27164. /* compressed prefix */
  27165. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  27166. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27167. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27168. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27169. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27170. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27171. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27172. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27173. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27174. };
  27175. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  27176. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  27177. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  27178. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  27179. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  27180. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  27181. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  27182. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  27183. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  27184. };
  27185. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  27186. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  27187. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  27188. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  27189. /* test of a 1024 byte long message */
  27190. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  27191. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  27192. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  27193. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  27194. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  27195. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  27196. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  27197. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  27198. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  27199. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  27200. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  27201. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  27202. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  27203. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  27204. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  27205. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  27206. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  27207. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  27208. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  27209. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  27210. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  27211. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  27212. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  27213. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  27214. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  27215. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  27216. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  27217. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  27218. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  27219. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  27220. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  27221. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  27222. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  27223. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  27224. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  27225. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  27226. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  27227. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  27228. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  27229. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  27230. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  27231. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  27232. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  27233. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  27234. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  27235. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  27236. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  27237. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  27238. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  27239. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  27240. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  27241. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  27242. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  27243. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  27244. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  27245. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  27246. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  27247. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  27248. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  27249. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  27250. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  27251. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  27252. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  27253. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  27254. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  27255. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  27256. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  27257. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  27258. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  27259. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  27260. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  27261. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  27262. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  27263. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  27264. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  27265. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  27266. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  27267. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  27268. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  27269. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  27270. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  27271. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  27272. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  27273. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  27274. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  27275. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  27276. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  27277. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  27278. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  27279. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  27280. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  27281. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  27282. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  27283. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  27284. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  27285. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  27286. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  27287. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  27288. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  27289. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  27290. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  27291. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  27292. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  27293. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  27294. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  27295. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  27296. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  27297. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  27298. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  27299. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  27300. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  27301. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  27302. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  27303. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  27304. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  27305. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  27306. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  27307. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  27308. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  27309. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  27310. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  27311. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  27312. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  27313. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  27314. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  27315. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  27316. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  27317. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  27318. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  27319. };
  27320. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  27321. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  27322. sizeof(msg2),
  27323. sizeof(msg3),
  27324. 0 /*sizeof(msg1)*/,
  27325. 0 /*sizeof(msg1)*/,
  27326. sizeof(msg4)
  27327. };
  27328. #ifndef NO_ASN
  27329. static byte privateEd25519[] = {
  27330. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  27331. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27332. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27333. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27334. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27335. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27336. };
  27337. static byte badPrivateEd25519[] = {
  27338. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  27339. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27340. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27341. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27342. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27343. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  27344. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  27345. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27346. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27347. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27348. 0xf7,0x07,0x51,0x1a,
  27349. 0x00 /* add additional bytes to make the pubkey bigger */
  27350. };
  27351. static byte publicEd25519[] = {
  27352. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  27353. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  27354. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27355. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27356. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27357. 0xf7,0x07,0x51,0x1a
  27358. };
  27359. /* size has been altered to catch if sanity check is done */
  27360. static byte badPublicEd25519[] = {
  27361. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  27362. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  27363. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27364. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27365. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27366. 0xf7,0x07,0x51,0x1a,
  27367. 0x00 /* add an additional byte to make the pubkey appear bigger */
  27368. };
  27369. static byte privPubEd25519[] = {
  27370. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  27371. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27372. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27373. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27374. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27375. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  27376. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  27377. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  27378. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  27379. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  27380. 0x51,0x1a
  27381. };
  27382. word32 idx;
  27383. #endif /* NO_ASN */
  27384. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27385. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  27386. ed25519_key key3;
  27387. #endif
  27388. /* create ed25519 keys */
  27389. #ifndef HAVE_FIPS
  27390. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27391. #else
  27392. ret = wc_InitRng(&rng);
  27393. #endif
  27394. if (ret != 0)
  27395. return WC_TEST_RET_ENC_EC(ret);
  27396. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  27397. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  27398. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  27399. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  27400. #endif
  27401. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  27402. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  27403. /* helper functions for signature and key size */
  27404. keySz = wc_ed25519_size(&key);
  27405. sigSz = wc_ed25519_sig_size(&key);
  27406. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  27407. defined(HAVE_ED25519_KEY_IMPORT)
  27408. for (i = 0; i < 6; i++) {
  27409. outlen = sizeof(out);
  27410. XMEMSET(out, 0, sizeof(out));
  27411. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  27412. pKeySz[i], &key) != 0)
  27413. return WC_TEST_RET_ENC_I(i);
  27414. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  27415. return WC_TEST_RET_ENC_I(i);
  27416. if (XMEMCMP(out, sigs[i], 64))
  27417. return WC_TEST_RET_ENC_I(i);
  27418. #if defined(HAVE_ED25519_VERIFY)
  27419. /* test verify on good msg */
  27420. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27421. &key) != 0 || verify != 1)
  27422. return WC_TEST_RET_ENC_I(i);
  27423. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  27424. /* test verify on good msg using streaming interface directly */
  27425. if (wc_ed25519_verify_msg_init(out, outlen,
  27426. &key, (byte)Ed25519, NULL, 0) != 0)
  27427. return WC_TEST_RET_ENC_I(i);
  27428. for (j = 0; j < msgSz[i]; j += i) {
  27429. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  27430. return WC_TEST_RET_ENC_I(i);
  27431. }
  27432. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  27433. &key) != 0 || verify != 1)
  27434. return WC_TEST_RET_ENC_I(i);
  27435. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  27436. /* test verify on bad msg */
  27437. out[outlen-1] = out[outlen-1] + 1;
  27438. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27439. &key) == 0 || verify == 1)
  27440. return WC_TEST_RET_ENC_I(i);
  27441. #endif /* HAVE_ED25519_VERIFY */
  27442. /* test api for import/exporting keys */
  27443. exportPSz = sizeof(exportPKey);
  27444. exportSSz = sizeof(exportSKey);
  27445. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  27446. return WC_TEST_RET_ENC_I(i);
  27447. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  27448. return WC_TEST_RET_ENC_I(i);
  27449. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  27450. return WC_TEST_RET_ENC_I(i);
  27451. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  27452. exportPKey, exportPSz, &key2) != 0)
  27453. return WC_TEST_RET_ENC_I(i);
  27454. /* clear "out" buffer and test sign with imported keys */
  27455. outlen = sizeof(out);
  27456. XMEMSET(out, 0, sizeof(out));
  27457. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  27458. return WC_TEST_RET_ENC_I(i);
  27459. #if defined(HAVE_ED25519_VERIFY)
  27460. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27461. &key2) != 0 || verify != 1)
  27462. return WC_TEST_RET_ENC_I(i);
  27463. if (XMEMCMP(out, sigs[i], 64))
  27464. return WC_TEST_RET_ENC_I(i);
  27465. #endif /* HAVE_ED25519_VERIFY */
  27466. }
  27467. ret = ed25519ctx_test();
  27468. if (ret != 0)
  27469. return ret;
  27470. ret = ed25519ph_test();
  27471. if (ret != 0)
  27472. return ret;
  27473. #ifndef NO_ASN
  27474. /* Try ASN.1 encoded private-only key and public key. */
  27475. idx = 0;
  27476. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  27477. sizeof(privateEd25519));
  27478. if (ret != 0)
  27479. return WC_TEST_RET_ENC_EC(ret);
  27480. idx = 0;
  27481. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  27482. sizeof(badPrivateEd25519)) == 0)
  27483. return WC_TEST_RET_ENC_NC;
  27484. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27485. if (ret != BAD_FUNC_ARG)
  27486. return WC_TEST_RET_ENC_EC(ret);
  27487. /* try with a buffer size that is too large */
  27488. idx = 0;
  27489. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  27490. sizeof(badPublicEd25519)) == 0)
  27491. return WC_TEST_RET_ENC_NC;
  27492. idx = 0;
  27493. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  27494. sizeof(publicEd25519));
  27495. if (ret != 0)
  27496. return WC_TEST_RET_ENC_EC(ret);
  27497. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27498. if (ret != 0)
  27499. return WC_TEST_RET_ENC_EC(ret);
  27500. if (XMEMCMP(out, sigs[0], 64))
  27501. return WC_TEST_RET_ENC_NC;
  27502. #if defined(HAVE_ED25519_VERIFY)
  27503. /* test verify on good msg */
  27504. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  27505. if (ret != 0 || verify != 1)
  27506. return WC_TEST_RET_ENC_EC(ret);
  27507. #endif /* HAVE_ED25519_VERIFY */
  27508. wc_ed25519_free(&key3);
  27509. wc_ed25519_init(&key3);
  27510. idx = 0;
  27511. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  27512. sizeof(privPubEd25519));
  27513. if (ret != 0)
  27514. return WC_TEST_RET_ENC_EC(ret);
  27515. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27516. if (ret != 0)
  27517. return WC_TEST_RET_ENC_EC(ret);
  27518. if (XMEMCMP(out, sigs[0], 64))
  27519. return WC_TEST_RET_ENC_NC;
  27520. wc_ed25519_free(&key3);
  27521. #endif /* NO_ASN */
  27522. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27523. /* clean up keys when done */
  27524. wc_ed25519_free(&key);
  27525. wc_ed25519_free(&key2);
  27526. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  27527. wc_FreeRng(&rng);
  27528. #endif
  27529. /* hush warnings of unused keySz and sigSz */
  27530. (void)keySz;
  27531. (void)sigSz;
  27532. #ifdef WOLFSSL_TEST_CERT
  27533. ret = ed25519_test_cert();
  27534. if (ret < 0)
  27535. return ret;
  27536. #ifdef WOLFSSL_CERT_GEN
  27537. ret = ed25519_test_make_cert();
  27538. if (ret < 0)
  27539. return ret;
  27540. #endif /* WOLFSSL_CERT_GEN */
  27541. #endif /* WOLFSSL_TEST_CERT */
  27542. return 0;
  27543. }
  27544. #endif /* HAVE_ED25519 */
  27545. #ifdef HAVE_CURVE448
  27546. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27547. defined(HAVE_CURVE448_KEY_IMPORT)
  27548. /* Test the wc_curve448_check_public API.
  27549. *
  27550. * returns 0 on success and -ve on failure.
  27551. */
  27552. static wc_test_ret_t curve448_check_public_test(void)
  27553. {
  27554. /* Little-endian values that will fail */
  27555. byte fail_le[][CURVE448_KEY_SIZE] = {
  27556. {
  27557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27558. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27559. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27560. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27561. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27564. },
  27565. {
  27566. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27568. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27569. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27570. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27571. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27572. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27573. },
  27574. };
  27575. /* Big-endian values that will fail */
  27576. byte fail_be[][CURVE448_KEY_SIZE] = {
  27577. {
  27578. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27579. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27580. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27581. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27585. },
  27586. {
  27587. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27588. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27589. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27590. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27592. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27594. },
  27595. };
  27596. /* Good or valid public value */
  27597. byte good[CURVE448_KEY_SIZE] = {
  27598. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27599. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27601. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27602. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27603. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27605. };
  27606. int i;
  27607. wc_test_ret_t ret;
  27608. /* Parameter checks */
  27609. /* NULL pointer */
  27610. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  27611. if (ret != BAD_FUNC_ARG)
  27612. return WC_TEST_RET_ENC_EC(ret);
  27613. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  27614. if (ret != BAD_FUNC_ARG)
  27615. return WC_TEST_RET_ENC_EC(ret);
  27616. /* Length of 0 treated differently to other invalid lengths for TLS */
  27617. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  27618. if (ret != BUFFER_E)
  27619. return WC_TEST_RET_ENC_EC(ret);
  27620. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  27621. if (ret != BUFFER_E)
  27622. return WC_TEST_RET_ENC_EC(ret);
  27623. /* Length not CURVE448_KEY_SIZE */
  27624. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  27625. if (i == CURVE448_KEY_SIZE)
  27626. continue;
  27627. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  27628. ECC_BAD_ARG_E) {
  27629. return WC_TEST_RET_ENC_I(i);
  27630. }
  27631. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  27632. ECC_BAD_ARG_E) {
  27633. return WC_TEST_RET_ENC_I(i);
  27634. }
  27635. }
  27636. /* Little-endian fail cases */
  27637. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  27638. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  27639. EC448_LITTLE_ENDIAN) == 0) {
  27640. return WC_TEST_RET_ENC_I(i);
  27641. }
  27642. }
  27643. /* Big-endian fail cases */
  27644. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  27645. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  27646. EC448_BIG_ENDIAN) == 0) {
  27647. return WC_TEST_RET_ENC_I(i);
  27648. }
  27649. }
  27650. /* Check a valid public value works! */
  27651. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  27652. EC448_LITTLE_ENDIAN);
  27653. if (ret != 0)
  27654. return WC_TEST_RET_ENC_EC(ret);
  27655. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  27656. if (ret != 0)
  27657. return WC_TEST_RET_ENC_EC(ret);
  27658. return 0;
  27659. }
  27660. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  27661. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  27662. {
  27663. WC_RNG rng;
  27664. wc_test_ret_t ret;
  27665. #ifdef HAVE_CURVE448_SHARED_SECRET
  27666. byte sharedA[CURVE448_KEY_SIZE];
  27667. byte sharedB[CURVE448_KEY_SIZE];
  27668. word32 y;
  27669. #endif
  27670. #ifdef HAVE_CURVE448_KEY_EXPORT
  27671. byte exportBuf[CURVE448_KEY_SIZE];
  27672. #endif
  27673. word32 x;
  27674. curve448_key userA, userB, pubKey;
  27675. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27676. defined(HAVE_CURVE448_KEY_IMPORT)
  27677. /* test vectors from
  27678. https://www.rfc-editor.org/rfc/rfc7748.html
  27679. */
  27680. /* secret key for party a */
  27681. byte sa[] = {
  27682. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  27683. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  27684. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  27685. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  27686. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  27687. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  27688. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  27689. };
  27690. /* public key for party a */
  27691. byte pa[] = {
  27692. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  27693. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  27694. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  27695. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  27696. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  27697. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  27698. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  27699. };
  27700. /* secret key for party b */
  27701. byte sb[] = {
  27702. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  27703. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  27704. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  27705. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  27706. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  27707. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  27708. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  27709. };
  27710. /* public key for party b */
  27711. byte pb[] = {
  27712. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  27713. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  27714. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  27715. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  27716. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  27717. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  27718. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  27719. };
  27720. /* expected shared key */
  27721. byte ss[] = {
  27722. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  27723. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  27724. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  27725. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  27726. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  27727. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  27728. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  27729. };
  27730. #endif /* HAVE_CURVE448_SHARED_SECRET */
  27731. (void)x;
  27732. #ifndef HAVE_FIPS
  27733. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27734. #else
  27735. ret = wc_InitRng(&rng);
  27736. #endif
  27737. if (ret != 0)
  27738. return WC_TEST_RET_ENC_EC(ret);
  27739. wc_curve448_init(&userA);
  27740. wc_curve448_init(&userB);
  27741. wc_curve448_init(&pubKey);
  27742. /* make curve448 keys */
  27743. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  27744. if (ret != 0)
  27745. return WC_TEST_RET_ENC_EC(ret);
  27746. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  27747. if (ret != 0)
  27748. return WC_TEST_RET_ENC_EC(ret);
  27749. #ifdef HAVE_CURVE448_SHARED_SECRET
  27750. /* find shared secret key */
  27751. x = sizeof(sharedA);
  27752. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  27753. if (ret != 0)
  27754. return WC_TEST_RET_ENC_EC(ret);
  27755. y = sizeof(sharedB);
  27756. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27757. if (ret != 0)
  27758. return WC_TEST_RET_ENC_EC(ret);
  27759. /* compare shared secret keys to test they are the same */
  27760. if (y != x)
  27761. return WC_TEST_RET_ENC_NC;
  27762. if (XMEMCMP(sharedA, sharedB, x))
  27763. return WC_TEST_RET_ENC_NC;
  27764. #endif
  27765. #ifdef HAVE_CURVE448_KEY_EXPORT
  27766. /* export a public key and import it for another user */
  27767. x = sizeof(exportBuf);
  27768. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  27769. if (ret != 0)
  27770. return WC_TEST_RET_ENC_EC(ret);
  27771. #ifdef HAVE_CURVE448_KEY_IMPORT
  27772. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  27773. if (ret != 0)
  27774. return WC_TEST_RET_ENC_EC(ret);
  27775. #endif
  27776. #endif
  27777. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27778. defined(HAVE_CURVE448_KEY_IMPORT)
  27779. /* test shared key after importing a public key */
  27780. XMEMSET(sharedB, 0, sizeof(sharedB));
  27781. y = sizeof(sharedB);
  27782. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  27783. if (ret != 0)
  27784. return WC_TEST_RET_ENC_EC(ret);
  27785. if (XMEMCMP(sharedA, sharedB, y))
  27786. return WC_TEST_RET_ENC_NC;
  27787. /* import RFC test vectors and compare shared key */
  27788. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  27789. if (ret != 0)
  27790. return WC_TEST_RET_ENC_EC(ret);
  27791. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  27792. if (ret != 0)
  27793. return WC_TEST_RET_ENC_EC(ret);
  27794. /* test against known test vector */
  27795. XMEMSET(sharedB, 0, sizeof(sharedB));
  27796. y = sizeof(sharedB);
  27797. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  27798. if (ret != 0)
  27799. return WC_TEST_RET_ENC_EC(ret);
  27800. if (XMEMCMP(ss, sharedB, y))
  27801. return WC_TEST_RET_ENC_NC;
  27802. /* test swapping roles of keys and generating same shared key */
  27803. XMEMSET(sharedB, 0, sizeof(sharedB));
  27804. y = sizeof(sharedB);
  27805. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27806. if (ret != 0)
  27807. return WC_TEST_RET_ENC_EC(ret);
  27808. if (XMEMCMP(ss, sharedB, y))
  27809. return WC_TEST_RET_ENC_NC;
  27810. /* test with 1 generated key and 1 from known test vector */
  27811. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  27812. if (ret != 0)
  27813. return WC_TEST_RET_ENC_EC(ret);
  27814. ret = wc_curve448_make_key(&rng, 56, &userB);
  27815. if (ret != 0)
  27816. return WC_TEST_RET_ENC_EC(ret);
  27817. x = sizeof(sharedA);
  27818. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  27819. if (ret != 0)
  27820. return WC_TEST_RET_ENC_EC(ret);
  27821. y = sizeof(sharedB);
  27822. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27823. if (ret != 0)
  27824. return WC_TEST_RET_ENC_EC(ret);
  27825. /* compare shared secret keys to test they are the same */
  27826. if (y != x)
  27827. return WC_TEST_RET_ENC_NC;
  27828. if (XMEMCMP(sharedA, sharedB, x))
  27829. return WC_TEST_RET_ENC_NC;
  27830. ret = curve448_check_public_test();
  27831. if (ret != 0)
  27832. return ret;
  27833. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  27834. /* clean up keys when done */
  27835. wc_curve448_free(&pubKey);
  27836. wc_curve448_free(&userB);
  27837. wc_curve448_free(&userA);
  27838. wc_FreeRng(&rng);
  27839. return 0;
  27840. }
  27841. #endif /* HAVE_CURVE448 */
  27842. #ifdef HAVE_ED448
  27843. #ifdef WOLFSSL_TEST_CERT
  27844. static wc_test_ret_t ed448_test_cert(void)
  27845. {
  27846. DecodedCert cert[2];
  27847. DecodedCert* serverCert = NULL;
  27848. DecodedCert* caCert = NULL;
  27849. #ifdef HAVE_ED448_VERIFY
  27850. ed448_key key;
  27851. ed448_key* pubKey = NULL;
  27852. int verify;
  27853. #endif /* HAVE_ED448_VERIFY */
  27854. wc_test_ret_t ret;
  27855. byte* tmp;
  27856. size_t bytes;
  27857. XFILE file;
  27858. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27859. if (tmp == NULL) {
  27860. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27861. }
  27862. #ifdef USE_CERT_BUFFERS_256
  27863. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  27864. bytes = sizeof_ca_ed448_cert;
  27865. #elif !defined(NO_FILESYSTEM)
  27866. file = XFOPEN(caEd448Cert, "rb");
  27867. if (file == NULL) {
  27868. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27869. }
  27870. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  27871. XFCLOSE(file);
  27872. if (bytes == 0)
  27873. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27874. #else
  27875. /* No certificate to use. */
  27876. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27877. #endif
  27878. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  27879. caCert = &cert[0];
  27880. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  27881. if (ret != 0)
  27882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27883. #ifdef USE_CERT_BUFFERS_256
  27884. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  27885. bytes = sizeof_server_ed448_cert;
  27886. #elif !defined(NO_FILESYSTEM)
  27887. file = XFOPEN(serverEd448Cert, "rb");
  27888. if (file == NULL) {
  27889. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27890. }
  27891. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  27892. XFCLOSE(file);
  27893. if (bytes == 0)
  27894. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27895. #else
  27896. /* No certificate to use. */
  27897. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27898. #endif
  27899. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  27900. serverCert = &cert[1];
  27901. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  27902. if (ret != 0)
  27903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27904. #ifdef HAVE_ED448_VERIFY
  27905. ret = wc_ed448_init(&key);
  27906. if (ret < 0)
  27907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27908. pubKey = &key;
  27909. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  27910. if (ret < 0)
  27911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27912. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  27913. serverCert->source + serverCert->certBegin,
  27914. serverCert->sigIndex - serverCert->certBegin,
  27915. &verify, pubKey, NULL, 0);
  27916. if (ret < 0 || verify != 1) {
  27917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27918. }
  27919. #endif /* HAVE_ED448_VERIFY */
  27920. done:
  27921. if (tmp != NULL)
  27922. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27923. #ifdef HAVE_ED448_VERIFY
  27924. wc_ed448_free(pubKey);
  27925. #endif /* HAVE_ED448_VERIFY */
  27926. if (caCert != NULL)
  27927. FreeDecodedCert(caCert);
  27928. if (serverCert != NULL)
  27929. FreeDecodedCert(serverCert);
  27930. return ret;
  27931. }
  27932. static wc_test_ret_t ed448_test_make_cert(void)
  27933. {
  27934. WC_RNG rng;
  27935. Cert cert;
  27936. DecodedCert decode;
  27937. ed448_key key;
  27938. ed448_key* privKey = NULL;
  27939. wc_test_ret_t ret = 0;
  27940. byte* tmp = NULL;
  27941. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  27942. #ifndef HAVE_FIPS
  27943. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27944. #else
  27945. ret = wc_InitRng(&rng);
  27946. #endif
  27947. if (ret != 0)
  27948. return WC_TEST_RET_ENC_EC(ret);
  27949. wc_ed448_init(&key);
  27950. privKey = &key;
  27951. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  27952. cert.daysValid = 365 * 2;
  27953. cert.selfSigned = 1;
  27954. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  27955. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  27956. cert.isCA = 0;
  27957. #ifdef WOLFSSL_CERT_EXT
  27958. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  27959. if (ret < 0)
  27960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27961. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  27962. if (ret < 0)
  27963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27964. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  27965. if (ret < 0)
  27966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27967. #endif
  27968. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27969. if (tmp == NULL) {
  27970. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27971. }
  27972. cert.sigType = CTC_ED448;
  27973. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  27974. if (ret < 0)
  27975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27976. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  27977. privKey, &rng);
  27978. if (ret < 0)
  27979. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27980. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  27981. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  27982. FreeDecodedCert(&decode);
  27983. if (ret != 0)
  27984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27985. done:
  27986. if (tmp != NULL)
  27987. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27988. wc_ed448_free(privKey);
  27989. wc_FreeRng(&rng);
  27990. return ret;
  27991. }
  27992. #endif /* WOLFSSL_TEST_CERT */
  27993. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  27994. defined(HAVE_ED448_KEY_IMPORT)
  27995. static wc_test_ret_t ed448_ctx_test(void)
  27996. {
  27997. wc_test_ret_t ret;
  27998. byte out[ED448_SIG_SIZE];
  27999. word32 outlen;
  28000. #ifdef HAVE_ED448_VERIFY
  28001. int verify;
  28002. #endif /* HAVE_ED448_VERIFY */
  28003. ed448_key key;
  28004. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  28005. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  28006. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  28007. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  28008. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  28009. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  28010. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  28011. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  28012. 0x4e
  28013. };
  28014. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  28015. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  28016. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  28017. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  28018. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  28019. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  28020. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  28021. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  28022. 0x80
  28023. };
  28024. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  28025. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  28026. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  28027. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  28028. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  28029. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  28030. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  28031. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  28032. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  28033. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  28034. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  28035. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  28036. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  28037. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  28038. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  28039. 0x3c, 0x00
  28040. };
  28041. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  28042. 0x03
  28043. };
  28044. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  28045. 0x66,0x6f,0x6f
  28046. };
  28047. outlen = sizeof(out);
  28048. XMEMSET(out, 0, sizeof(out));
  28049. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  28050. if (ret != 0)
  28051. return WC_TEST_RET_ENC_EC(ret);
  28052. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  28053. sizeof(pKeyCtx), &key);
  28054. if (ret != 0)
  28055. return WC_TEST_RET_ENC_EC(ret);
  28056. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  28057. contextCtx, sizeof(contextCtx));
  28058. if (ret != 0)
  28059. return WC_TEST_RET_ENC_EC(ret);
  28060. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  28061. return WC_TEST_RET_ENC_NC;
  28062. #if defined(HAVE_ED448_VERIFY)
  28063. /* test verify on good msg */
  28064. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  28065. contextCtx, sizeof(contextCtx));
  28066. if (ret != 0 || verify != 1)
  28067. return WC_TEST_RET_ENC_EC(ret);
  28068. #endif
  28069. wc_ed448_free(&key);
  28070. return 0;
  28071. }
  28072. static wc_test_ret_t ed448ph_test(void)
  28073. {
  28074. wc_test_ret_t ret;
  28075. byte out[ED448_SIG_SIZE];
  28076. word32 outlen;
  28077. #ifdef HAVE_ED448_VERIFY
  28078. int verify;
  28079. #endif /* HAVE_ED448_VERIFY */
  28080. ed448_key key;
  28081. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  28082. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  28083. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  28084. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  28085. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  28086. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  28087. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  28088. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  28089. 0x49
  28090. };
  28091. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  28092. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  28093. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  28094. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  28095. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  28096. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  28097. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  28098. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  28099. 0x80
  28100. };
  28101. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  28102. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  28103. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  28104. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  28105. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  28106. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  28107. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  28108. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  28109. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  28110. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  28111. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  28112. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  28113. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  28114. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  28115. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  28116. 0x0f, 0x00
  28117. };
  28118. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  28119. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  28120. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  28121. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  28122. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  28123. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  28124. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  28125. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  28126. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  28127. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  28128. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  28129. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  28130. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  28131. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  28132. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  28133. 0x21, 0x00
  28134. };
  28135. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  28136. 0x61,0x62,0x63
  28137. };
  28138. /* SHA-512 hash of msgPh */
  28139. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  28140. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  28141. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  28142. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  28143. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  28144. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  28145. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  28146. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  28147. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  28148. };
  28149. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  28150. 0x66,0x6f,0x6f
  28151. };
  28152. outlen = sizeof(out);
  28153. XMEMSET(out, 0, sizeof(out));
  28154. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  28155. if (ret != 0)
  28156. return WC_TEST_RET_ENC_EC(ret);
  28157. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  28158. sizeof(pKeyPh), &key);
  28159. if (ret != 0)
  28160. return WC_TEST_RET_ENC_EC(ret);
  28161. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  28162. 0);
  28163. if (ret != 0)
  28164. return WC_TEST_RET_ENC_EC(ret);
  28165. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  28166. return WC_TEST_RET_ENC_NC;
  28167. #if defined(HAVE_ED448_VERIFY)
  28168. /* test verify on good msg */
  28169. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  28170. NULL, 0);
  28171. if (ret != 0 || verify != 1) {
  28172. return WC_TEST_RET_ENC_EC(ret);
  28173. }
  28174. #endif
  28175. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  28176. contextPh2, sizeof(contextPh2));
  28177. if (ret != 0)
  28178. return WC_TEST_RET_ENC_EC(ret);
  28179. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  28180. return WC_TEST_RET_ENC_NC;
  28181. #if defined(HAVE_ED448_VERIFY)
  28182. /* test verify on good msg */
  28183. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  28184. contextPh2, sizeof(contextPh2));
  28185. if (ret != 0 || verify != 1) {
  28186. return WC_TEST_RET_ENC_EC(ret);
  28187. }
  28188. #endif
  28189. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  28190. 0);
  28191. if (ret != 0)
  28192. return WC_TEST_RET_ENC_EC(ret);
  28193. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  28194. return WC_TEST_RET_ENC_NC;
  28195. #if defined(HAVE_ED448_VERIFY)
  28196. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  28197. &key, NULL, 0);
  28198. if (ret != 0 || verify != 1) {
  28199. return WC_TEST_RET_ENC_EC(ret);
  28200. }
  28201. #endif
  28202. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  28203. contextPh2, sizeof(contextPh2));
  28204. if (ret != 0)
  28205. return WC_TEST_RET_ENC_EC(ret);
  28206. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  28207. return WC_TEST_RET_ENC_NC;
  28208. #if defined(HAVE_ED448_VERIFY)
  28209. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  28210. &key, contextPh2, sizeof(contextPh2));
  28211. if (ret != 0 || verify != 1) {
  28212. return WC_TEST_RET_ENC_EC(ret);
  28213. }
  28214. #endif
  28215. wc_ed448_free(&key);
  28216. return 0;
  28217. }
  28218. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28219. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  28220. {
  28221. wc_test_ret_t ret;
  28222. WC_RNG rng;
  28223. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  28224. defined(HAVE_ED448_KEY_IMPORT)
  28225. byte out[ED448_SIG_SIZE];
  28226. int i;
  28227. word32 outlen;
  28228. #ifdef HAVE_ED448_VERIFY
  28229. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  28230. int j;
  28231. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  28232. int verify;
  28233. #endif /* HAVE_ED448_VERIFY */
  28234. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28235. word32 keySz, sigSz;
  28236. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28237. ed448_key *key = NULL;
  28238. ed448_key *key2 = NULL;
  28239. #else
  28240. ed448_key key[1];
  28241. ed448_key key2[1];
  28242. #endif
  28243. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  28244. defined(HAVE_ED448_KEY_IMPORT)
  28245. /* test vectors from
  28246. https://tools.ietf.org/html/rfc8032
  28247. */
  28248. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  28249. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28250. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28251. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28252. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28253. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28254. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28255. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28256. 0x5b
  28257. };
  28258. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  28259. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  28260. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  28261. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  28262. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  28263. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  28264. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  28265. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  28266. 0x4e
  28267. };
  28268. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  28269. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  28270. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  28271. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  28272. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  28273. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  28274. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  28275. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  28276. 0x1b
  28277. };
  28278. /* uncompressed test */
  28279. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  28280. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28281. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28282. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28283. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28284. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28285. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28286. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28287. 0x5b
  28288. };
  28289. /* compressed prefix test */
  28290. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  28291. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28292. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28293. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28294. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28295. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28296. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28297. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28298. 0x5b
  28299. };
  28300. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  28301. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  28302. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  28303. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  28304. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  28305. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  28306. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  28307. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  28308. 0xe8
  28309. };
  28310. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  28311. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  28312. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28313. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28314. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28315. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28316. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28317. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28318. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28319. 0x80
  28320. };
  28321. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  28322. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  28323. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  28324. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  28325. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  28326. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  28327. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  28328. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  28329. 0x80
  28330. };
  28331. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  28332. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  28333. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  28334. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  28335. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  28336. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  28337. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  28338. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  28339. 0x80
  28340. };
  28341. /* uncompressed test */
  28342. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  28343. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28344. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28345. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28346. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28347. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28348. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28349. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28350. 0x80
  28351. };
  28352. /* compressed prefix */
  28353. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  28354. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28355. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28356. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28357. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28358. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28359. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28360. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28361. 0x80
  28362. };
  28363. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  28364. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  28365. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  28366. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  28367. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  28368. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  28369. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  28370. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  28371. 0x00
  28372. };
  28373. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  28374. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  28375. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  28376. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  28377. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28378. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28379. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28380. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28381. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28382. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28383. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28384. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28385. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28386. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28387. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28388. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28389. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28390. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28391. 0x26, 0x00
  28392. };
  28393. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  28394. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  28395. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  28396. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  28397. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  28398. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  28399. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  28400. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  28401. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  28402. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  28403. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  28404. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  28405. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  28406. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  28407. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  28408. 0x3a, 0x00
  28409. };
  28410. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  28411. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  28412. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  28413. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  28414. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  28415. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  28416. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  28417. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  28418. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  28419. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  28420. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  28421. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  28422. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  28423. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  28424. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  28425. 0x3c, 0x00
  28426. };
  28427. /* uncompressed test */
  28428. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  28429. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28430. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28431. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28432. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28433. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28434. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28435. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28436. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28437. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28438. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28439. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28440. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28441. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28442. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28443. 0x26, 0x00
  28444. };
  28445. /* compressed prefix */
  28446. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  28447. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28448. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28449. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28450. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28451. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28452. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28453. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28454. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28455. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28456. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28457. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28458. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28459. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28460. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28461. 0x26, 0x00
  28462. };
  28463. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  28464. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  28465. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  28466. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  28467. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  28468. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  28469. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  28470. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  28471. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  28472. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  28473. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  28474. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  28475. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  28476. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  28477. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  28478. 0x1a, 0x00
  28479. };
  28480. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  28481. #define SIGSZ sizeof(sig1)
  28482. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  28483. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  28484. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  28485. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  28486. /* test of a 1023 byte long message */
  28487. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  28488. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  28489. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  28490. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  28491. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  28492. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  28493. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  28494. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  28495. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  28496. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  28497. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  28498. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  28499. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  28500. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  28501. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  28502. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  28503. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  28504. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  28505. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  28506. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  28507. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  28508. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  28509. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  28510. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  28511. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  28512. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  28513. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  28514. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  28515. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  28516. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  28517. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  28518. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  28519. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  28520. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  28521. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  28522. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  28523. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  28524. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  28525. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  28526. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  28527. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  28528. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  28529. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  28530. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  28531. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  28532. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  28533. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  28534. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  28535. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  28536. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  28537. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  28538. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  28539. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  28540. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  28541. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  28542. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  28543. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  28544. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  28545. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  28546. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  28547. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  28548. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  28549. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  28550. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  28551. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  28552. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  28553. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  28554. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  28555. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  28556. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  28557. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  28558. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  28559. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  28560. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  28561. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  28562. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  28563. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  28564. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  28565. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  28566. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  28567. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  28568. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  28569. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  28570. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  28571. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  28572. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  28573. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  28574. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  28575. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  28576. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  28577. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  28578. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  28579. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  28580. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  28581. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  28582. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  28583. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  28584. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  28585. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  28586. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  28587. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  28588. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  28589. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  28590. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  28591. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  28592. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  28593. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  28594. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  28595. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  28596. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  28597. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  28598. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  28599. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  28600. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  28601. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  28602. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  28603. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  28604. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  28605. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  28606. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  28607. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  28608. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  28609. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  28610. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  28611. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  28612. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  28613. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  28614. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  28615. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  28616. };
  28617. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  28618. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  28619. sizeof(msg2),
  28620. sizeof(msg3),
  28621. 0 /*sizeof(msg1)*/,
  28622. 0 /*sizeof(msg1)*/,
  28623. sizeof(msg4)
  28624. };
  28625. #ifndef NO_ASN
  28626. static const byte privateEd448[] = {
  28627. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  28628. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  28629. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28630. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28631. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28632. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28633. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28634. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28635. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28636. 0x5b
  28637. };
  28638. static const byte publicEd448[] = {
  28639. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  28640. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  28641. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  28642. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  28643. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  28644. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  28645. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  28646. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  28647. 0xaf, 0xe8, 0x25, 0x61, 0x80
  28648. };
  28649. static const byte privPubEd448[] = {
  28650. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  28651. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  28652. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  28653. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  28654. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  28655. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  28656. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  28657. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  28658. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  28659. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  28660. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  28661. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  28662. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  28663. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  28664. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  28665. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  28666. 0xaf, 0xe8, 0x25, 0x61, 0x80
  28667. };
  28668. word32 idx;
  28669. #endif /* NO_ASN */
  28670. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28671. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28673. ed448_key *key3 = NULL;
  28674. #else
  28675. ed448_key key3[1];
  28676. #endif
  28677. #endif
  28678. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28679. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28680. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28681. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28682. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28683. #endif
  28684. #endif
  28685. /* create ed448 keys */
  28686. #ifndef HAVE_FIPS
  28687. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28688. #else
  28689. ret = wc_InitRng(&rng);
  28690. #endif
  28691. if (ret != 0) {
  28692. XMEMSET(&rng, 0, sizeof(rng));
  28693. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28694. }
  28695. ret = wc_ed448_init(key);
  28696. if (ret < 0)
  28697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28698. ret = wc_ed448_init(key2);
  28699. if (ret < 0)
  28700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28701. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28702. ret = wc_ed448_init(key3);
  28703. if (ret < 0)
  28704. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28705. #endif
  28706. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  28707. if (ret < 0)
  28708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28709. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  28710. if (ret < 0)
  28711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28712. /* helper functions for signature and key size */
  28713. keySz = wc_ed448_size(key);
  28714. sigSz = wc_ed448_sig_size(key);
  28715. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  28716. defined(HAVE_ED448_KEY_IMPORT)
  28717. for (i = 0; i < 6; i++) {
  28718. outlen = sizeof(out);
  28719. XMEMSET(out, 0, sizeof(out));
  28720. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  28721. pKeySz[i], key) != 0)
  28722. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28723. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  28724. 0) != 0)
  28725. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28726. if (XMEMCMP(out, sigs[i], 114))
  28727. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28728. #if defined(HAVE_ED448_VERIFY)
  28729. /* test verify on good msg */
  28730. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  28731. NULL, 0) != 0 || verify != 1)
  28732. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28733. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  28734. /* test verify on good msg using streaming interface directly */
  28735. if (wc_ed448_verify_msg_init(out, outlen,
  28736. key, (byte)Ed448, NULL, 0) != 0)
  28737. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28738. for (j = 0; j < msgSz[i]; j += i) {
  28739. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  28740. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28741. }
  28742. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  28743. key) != 0 || verify != 1)
  28744. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28745. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  28746. /* test verify on bad msg */
  28747. out[outlen-2] = out[outlen-2] + 1;
  28748. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  28749. NULL, 0) == 0 || verify == 1)
  28750. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28751. #endif /* HAVE_ED448_VERIFY */
  28752. /* test api for import/exporting keys */
  28753. {
  28754. byte *exportPKey = NULL;
  28755. byte *exportSKey = NULL;
  28756. word32 exportPSz = ED448_KEY_SIZE;
  28757. word32 exportSSz = ED448_KEY_SIZE;
  28758. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28759. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28760. if ((exportPKey == NULL) || (exportSKey == NULL))
  28761. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28762. ret = 0;
  28763. do {
  28764. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  28765. ret = WC_TEST_RET_ENC_I(i);
  28766. break;
  28767. }
  28768. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  28769. ret = WC_TEST_RET_ENC_I(i);
  28770. break;
  28771. }
  28772. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  28773. ret = WC_TEST_RET_ENC_I(i);
  28774. break;
  28775. }
  28776. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  28777. exportPKey, exportPSz, key2) != 0) {
  28778. ret = WC_TEST_RET_ENC_I(i);
  28779. break;
  28780. }
  28781. /* clear "out" buffer and test sign with imported keys */
  28782. outlen = sizeof(out);
  28783. XMEMSET(out, 0, sizeof(out));
  28784. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  28785. 0) != 0) {
  28786. ret = WC_TEST_RET_ENC_I(i);
  28787. break;
  28788. }
  28789. } while(0);
  28790. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28791. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28792. if (ret != 0)
  28793. goto out;
  28794. }
  28795. #if defined(HAVE_ED448_VERIFY)
  28796. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  28797. NULL, 0) != 0 || verify != 1)
  28798. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28799. if (XMEMCMP(out, sigs[i], SIGSZ))
  28800. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28801. #endif /* HAVE_ED448_VERIFY */
  28802. }
  28803. ret = ed448_ctx_test();
  28804. if (ret != 0)
  28805. goto out;
  28806. ret = ed448ph_test();
  28807. if (ret != 0)
  28808. goto out;
  28809. #ifndef NO_ASN
  28810. /* Try ASN.1 encoded private-only key and public key. */
  28811. idx = 0;
  28812. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  28813. sizeof(privateEd448));
  28814. if (ret != 0)
  28815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28816. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28817. if (ret != BAD_FUNC_ARG)
  28818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28819. idx = 0;
  28820. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  28821. if (ret != 0)
  28822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28823. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28824. if (ret != 0)
  28825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28826. if (XMEMCMP(out, sigs[0], SIGSZ))
  28827. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28828. #if defined(HAVE_ED448_VERIFY)
  28829. /* test verify on good msg */
  28830. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  28831. NULL, 0);
  28832. if (ret != 0 || verify != 1)
  28833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28834. #endif /* HAVE_ED448_VERIFY */
  28835. wc_ed448_free(key3);
  28836. ret = wc_ed448_init(key3);
  28837. if (ret < 0)
  28838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28839. idx = 0;
  28840. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  28841. sizeof(privPubEd448));
  28842. if (ret != 0)
  28843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28844. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28845. if (ret != 0)
  28846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28847. if (XMEMCMP(out, sigs[0], SIGSZ))
  28848. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28849. #endif /* NO_ASN */
  28850. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28851. ret = 0;
  28852. out:
  28853. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28854. if (key) {
  28855. wc_ed448_free(key);
  28856. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28857. }
  28858. if (key2) {
  28859. wc_ed448_free(key2);
  28860. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28861. }
  28862. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28863. if (key3) {
  28864. wc_ed448_free(key3);
  28865. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28866. }
  28867. #endif
  28868. #else
  28869. wc_ed448_free(key);
  28870. wc_ed448_free(key2);
  28871. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28872. wc_ed448_free(key3);
  28873. #endif
  28874. #endif
  28875. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  28876. wc_FreeRng(&rng);
  28877. #endif
  28878. if (ret < 0)
  28879. return ret;
  28880. /* hush warnings of unused keySz and sigSz */
  28881. (void)keySz;
  28882. (void)sigSz;
  28883. #ifdef WOLFSSL_TEST_CERT
  28884. ret = ed448_test_cert();
  28885. if (ret < 0)
  28886. return ret;
  28887. #ifdef WOLFSSL_CERT_GEN
  28888. ret = ed448_test_make_cert();
  28889. if (ret < 0)
  28890. return ret;
  28891. #endif /* WOLFSSL_CERT_GEN */
  28892. #endif /* WOLFSSL_TEST_CERT */
  28893. return 0;
  28894. }
  28895. #endif /* HAVE_ED448 */
  28896. #ifdef WOLFSSL_HAVE_KYBER
  28897. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  28898. #ifdef WOLFSSL_KYBER512
  28899. static wc_test_ret_t kyber512_kat(void)
  28900. {
  28901. KyberKey key;
  28902. wc_test_ret_t ret;
  28903. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  28904. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  28905. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  28906. byte ss[KYBER_SS_SZ];
  28907. byte ss_dec[KYBER_SS_SZ];
  28908. const byte kyber512_rand[] = {
  28909. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28910. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28911. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28912. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28913. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28914. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28915. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28916. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28917. };
  28918. const byte kyber512enc_rand[] = {
  28919. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28920. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28921. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28922. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28923. };
  28924. const byte kyber512_pk[] = {
  28925. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  28926. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  28927. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  28928. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  28929. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  28930. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  28931. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  28932. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  28933. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  28934. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  28935. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  28936. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  28937. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  28938. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  28939. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  28940. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  28941. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  28942. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  28943. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  28944. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  28945. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  28946. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  28947. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  28948. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  28949. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  28950. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  28951. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  28952. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  28953. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  28954. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  28955. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  28956. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  28957. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  28958. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  28959. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  28960. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  28961. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  28962. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  28963. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  28964. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  28965. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  28966. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  28967. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  28968. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  28969. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  28970. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  28971. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  28972. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  28973. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  28974. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  28975. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  28976. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  28977. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  28978. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  28979. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  28980. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  28981. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  28982. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  28983. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  28984. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  28985. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  28986. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  28987. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  28988. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  28989. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  28990. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  28991. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  28992. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  28993. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  28994. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  28995. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  28996. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  28997. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  28998. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  28999. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  29000. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  29001. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  29002. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  29003. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  29004. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  29005. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  29006. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  29007. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  29008. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  29009. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  29010. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  29011. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  29012. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  29013. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  29014. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  29015. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  29016. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  29017. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  29018. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  29019. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  29020. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  29021. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29022. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29023. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29024. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29025. };
  29026. const byte kyber512_sk[] = {
  29027. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  29028. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  29029. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  29030. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  29031. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  29032. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  29033. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  29034. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  29035. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  29036. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  29037. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  29038. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  29039. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  29040. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  29041. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  29042. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  29043. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  29044. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  29045. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  29046. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  29047. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  29048. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  29049. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  29050. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  29051. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  29052. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  29053. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  29054. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  29055. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  29056. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  29057. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  29058. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  29059. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  29060. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  29061. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  29062. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  29063. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  29064. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  29065. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  29066. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  29067. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  29068. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  29069. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  29070. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  29071. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  29072. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  29073. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  29074. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  29075. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  29076. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  29077. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  29078. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  29079. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  29080. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  29081. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  29082. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  29083. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  29084. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  29085. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  29086. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  29087. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  29088. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  29089. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  29090. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  29091. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  29092. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  29093. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  29094. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  29095. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  29096. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  29097. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  29098. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  29099. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  29100. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  29101. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  29102. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  29103. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  29104. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  29105. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  29106. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  29107. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  29108. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  29109. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  29110. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  29111. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  29112. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  29113. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  29114. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  29115. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  29116. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  29117. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  29118. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  29119. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  29120. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  29121. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  29122. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  29123. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  29124. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  29125. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  29126. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  29127. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  29128. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  29129. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  29130. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  29131. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  29132. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  29133. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  29134. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  29135. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  29136. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  29137. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  29138. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  29139. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  29140. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  29141. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  29142. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  29143. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  29144. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  29145. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  29146. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  29147. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  29148. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  29149. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  29150. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  29151. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  29152. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  29153. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  29154. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  29155. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  29156. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  29157. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  29158. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  29159. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  29160. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  29161. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  29162. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  29163. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  29164. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  29165. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  29166. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  29167. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  29168. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  29169. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  29170. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  29171. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  29172. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  29173. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  29174. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  29175. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  29176. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  29177. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  29178. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  29179. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  29180. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  29181. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  29182. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  29183. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  29184. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  29185. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  29186. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  29187. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  29188. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  29189. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  29190. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  29191. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  29192. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  29193. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  29194. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  29195. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  29196. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  29197. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  29198. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  29199. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  29200. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  29201. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  29202. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  29203. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  29204. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  29205. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  29206. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  29207. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  29208. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  29209. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  29210. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  29211. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  29212. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  29213. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  29214. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  29215. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  29216. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  29217. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  29218. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  29219. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29220. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29221. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29222. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29223. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  29224. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  29225. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  29226. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  29227. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29228. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29229. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29230. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29231. };
  29232. const byte kyber512_ct[] = {
  29233. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  29234. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  29235. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  29236. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  29237. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  29238. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  29239. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  29240. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  29241. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  29242. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  29243. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  29244. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  29245. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  29246. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  29247. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  29248. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  29249. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  29250. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  29251. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  29252. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  29253. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  29254. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  29255. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  29256. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  29257. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  29258. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  29259. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  29260. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  29261. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  29262. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  29263. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  29264. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  29265. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  29266. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  29267. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  29268. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  29269. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  29270. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  29271. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  29272. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  29273. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  29274. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  29275. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  29276. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  29277. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  29278. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  29279. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  29280. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  29281. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  29282. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  29283. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  29284. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  29285. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  29286. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  29287. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  29288. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  29289. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  29290. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  29291. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  29292. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  29293. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  29294. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  29295. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  29296. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  29297. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  29298. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  29299. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  29300. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  29301. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  29302. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  29303. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  29304. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  29305. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  29306. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  29307. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  29308. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  29309. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  29310. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  29311. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  29312. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  29313. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  29314. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  29315. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  29316. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  29317. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  29318. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  29319. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  29320. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  29321. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  29322. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  29323. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  29324. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  29325. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  29326. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  29327. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  29328. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  29329. };
  29330. const byte kyber512_ss[] = {
  29331. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  29332. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  29333. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  29334. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  29335. };
  29336. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  29337. if (ret != 0)
  29338. return WC_TEST_RET_ENC_EC(ret);
  29339. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  29340. sizeof(kyber512_rand));
  29341. if (ret != 0)
  29342. return WC_TEST_RET_ENC_EC(ret);
  29343. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  29344. if (ret != 0)
  29345. return WC_TEST_RET_ENC_EC(ret);
  29346. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  29347. if (ret != 0)
  29348. return WC_TEST_RET_ENC_EC(ret);
  29349. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  29350. return WC_TEST_RET_ENC_NC;
  29351. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  29352. return WC_TEST_RET_ENC_NC;
  29353. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  29354. sizeof(kyber512enc_rand));
  29355. if (ret != 0)
  29356. return WC_TEST_RET_ENC_EC(ret);
  29357. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  29358. return WC_TEST_RET_ENC_NC;
  29359. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  29360. return WC_TEST_RET_ENC_NC;
  29361. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  29362. if (ret != 0)
  29363. return WC_TEST_RET_ENC_EC(ret);
  29364. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  29365. return WC_TEST_RET_ENC_NC;
  29366. wc_KyberKey_Free(&key);
  29367. return 0;
  29368. }
  29369. #endif /* WOLFSSL_KYBER512 */
  29370. #ifdef WOLFSSL_KYBER768
  29371. static wc_test_ret_t kyber768_kat(void)
  29372. {
  29373. KyberKey key;
  29374. wc_test_ret_t ret;
  29375. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  29376. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  29377. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  29378. byte ss[KYBER_SS_SZ];
  29379. byte ss_dec[KYBER_SS_SZ];
  29380. const byte kyber768_rand[] = {
  29381. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  29382. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  29383. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  29384. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  29385. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29386. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29387. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29388. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29389. };
  29390. const byte kyber768enc_rand[] = {
  29391. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  29392. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  29393. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  29394. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  29395. };
  29396. const byte kyber768_pk[] = {
  29397. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  29398. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  29399. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  29400. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  29401. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  29402. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  29403. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  29404. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  29405. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  29406. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  29407. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  29408. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  29409. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  29410. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  29411. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  29412. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  29413. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  29414. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  29415. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  29416. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  29417. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  29418. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  29419. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  29420. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  29421. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  29422. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  29423. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  29424. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  29425. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  29426. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  29427. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  29428. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  29429. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  29430. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  29431. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  29432. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  29433. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  29434. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  29435. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  29436. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  29437. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  29438. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  29439. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  29440. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  29441. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  29442. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  29443. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  29444. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  29445. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  29446. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  29447. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  29448. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  29449. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  29450. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  29451. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  29452. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  29453. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  29454. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  29455. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  29456. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  29457. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  29458. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  29459. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  29460. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  29461. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  29462. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  29463. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  29464. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  29465. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  29466. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  29467. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  29468. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  29469. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  29470. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  29471. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  29472. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  29473. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  29474. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  29475. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  29476. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  29477. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  29478. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  29479. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  29480. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  29481. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  29482. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  29483. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  29484. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  29485. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  29486. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  29487. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  29488. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  29489. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  29490. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  29491. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  29492. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  29493. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  29494. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  29495. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  29496. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  29497. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  29498. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  29499. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  29500. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  29501. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  29502. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  29503. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  29504. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  29505. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  29506. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  29507. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  29508. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  29509. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  29510. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  29511. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  29512. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  29513. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  29514. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  29515. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  29516. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  29517. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  29518. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  29519. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  29520. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  29521. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  29522. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  29523. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  29524. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  29525. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  29526. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  29527. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  29528. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  29529. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  29530. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  29531. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  29532. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  29533. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  29534. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  29535. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  29536. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  29537. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  29538. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  29539. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  29540. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  29541. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29542. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29543. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29544. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29545. };
  29546. const byte kyber768_sk[] = {
  29547. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  29548. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  29549. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  29550. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  29551. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  29552. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  29553. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  29554. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  29555. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  29556. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  29557. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  29558. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  29559. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  29560. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  29561. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  29562. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  29563. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  29564. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  29565. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  29566. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  29567. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  29568. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  29569. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  29570. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  29571. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  29572. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  29573. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  29574. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  29575. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  29576. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  29577. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  29578. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  29579. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  29580. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  29581. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  29582. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  29583. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  29584. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  29585. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  29586. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  29587. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  29588. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  29589. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  29590. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  29591. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  29592. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  29593. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  29594. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  29595. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  29596. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  29597. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  29598. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  29599. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  29600. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  29601. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  29602. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  29603. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  29604. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  29605. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  29606. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  29607. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  29608. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  29609. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  29610. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  29611. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  29612. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  29613. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  29614. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  29615. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  29616. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  29617. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  29618. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  29619. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  29620. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  29621. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  29622. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  29623. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  29624. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  29625. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  29626. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  29627. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  29628. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  29629. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  29630. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  29631. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  29632. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  29633. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  29634. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  29635. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  29636. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  29637. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  29638. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  29639. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  29640. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  29641. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  29642. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  29643. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  29644. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  29645. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  29646. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  29647. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  29648. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  29649. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  29650. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  29651. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  29652. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  29653. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  29654. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  29655. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  29656. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  29657. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  29658. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  29659. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  29660. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  29661. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  29662. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  29663. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  29664. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  29665. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  29666. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  29667. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  29668. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  29669. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  29670. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  29671. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  29672. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  29673. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  29674. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  29675. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  29676. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  29677. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  29678. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  29679. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  29680. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  29681. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  29682. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  29683. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  29684. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  29685. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  29686. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  29687. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  29688. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  29689. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  29690. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  29691. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  29692. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  29693. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  29694. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  29695. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  29696. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  29697. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  29698. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  29699. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  29700. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  29701. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  29702. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  29703. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  29704. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  29705. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  29706. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  29707. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  29708. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  29709. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  29710. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  29711. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  29712. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  29713. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  29714. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  29715. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  29716. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  29717. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  29718. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  29719. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  29720. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  29721. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  29722. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  29723. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  29724. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  29725. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  29726. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  29727. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  29728. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  29729. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  29730. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  29731. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  29732. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  29733. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  29734. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  29735. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  29736. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  29737. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  29738. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  29739. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  29740. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  29741. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  29742. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  29743. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  29744. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  29745. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  29746. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  29747. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  29748. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  29749. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  29750. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  29751. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  29752. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  29753. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  29754. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  29755. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  29756. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  29757. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  29758. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  29759. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  29760. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  29761. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  29762. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  29763. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  29764. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  29765. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  29766. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  29767. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  29768. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  29769. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  29770. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  29771. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  29772. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  29773. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  29774. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  29775. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  29776. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  29777. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  29778. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  29779. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  29780. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  29781. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  29782. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  29783. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  29784. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  29785. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  29786. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  29787. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  29788. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  29789. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  29790. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  29791. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  29792. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  29793. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  29794. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  29795. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  29796. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  29797. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  29798. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  29799. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  29800. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  29801. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  29802. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  29803. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  29804. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  29805. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  29806. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  29807. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  29808. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  29809. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  29810. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  29811. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  29812. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  29813. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  29814. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  29815. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  29816. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  29817. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  29818. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  29819. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  29820. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  29821. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  29822. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  29823. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  29824. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  29825. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  29826. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  29827. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  29828. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  29829. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  29830. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  29831. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  29832. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  29833. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  29834. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  29835. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29836. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29837. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29838. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29839. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  29840. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  29841. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  29842. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  29843. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29844. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29845. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29846. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29847. };
  29848. const byte kyber768_ct[] = {
  29849. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  29850. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  29851. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  29852. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  29853. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  29854. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  29855. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  29856. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  29857. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  29858. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  29859. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  29860. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  29861. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  29862. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  29863. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  29864. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  29865. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  29866. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  29867. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  29868. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  29869. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  29870. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  29871. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  29872. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  29873. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  29874. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  29875. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  29876. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  29877. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  29878. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  29879. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  29880. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  29881. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  29882. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  29883. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  29884. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  29885. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  29886. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  29887. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  29888. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  29889. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  29890. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  29891. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  29892. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  29893. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  29894. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  29895. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  29896. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  29897. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  29898. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  29899. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  29900. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  29901. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  29902. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  29903. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  29904. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  29905. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  29906. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  29907. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  29908. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  29909. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  29910. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  29911. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  29912. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  29913. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  29914. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  29915. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  29916. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  29917. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  29918. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  29919. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  29920. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  29921. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  29922. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  29923. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  29924. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  29925. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  29926. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  29927. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  29928. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  29929. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  29930. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  29931. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  29932. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  29933. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  29934. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  29935. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  29936. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  29937. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  29938. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  29939. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  29940. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  29941. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  29942. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  29943. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  29944. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  29945. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  29946. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  29947. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  29948. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  29949. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  29950. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  29951. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  29952. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  29953. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  29954. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  29955. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  29956. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  29957. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  29958. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  29959. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  29960. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  29961. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  29962. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  29963. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  29964. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  29965. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  29966. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  29967. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  29968. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  29969. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  29970. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  29971. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  29972. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  29973. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  29974. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  29975. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  29976. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  29977. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  29978. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  29979. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  29980. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  29981. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  29982. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  29983. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  29984. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  29985. };
  29986. const byte kyber768_ss[] = {
  29987. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  29988. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  29989. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  29990. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  29991. };
  29992. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  29993. if (ret != 0)
  29994. return WC_TEST_RET_ENC_EC(ret);
  29995. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  29996. sizeof(kyber768_rand));
  29997. if (ret != 0)
  29998. return WC_TEST_RET_ENC_EC(ret);
  29999. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30000. if (ret != 0)
  30001. return WC_TEST_RET_ENC_EC(ret);
  30002. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30003. if (ret != 0)
  30004. return WC_TEST_RET_ENC_EC(ret);
  30005. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  30006. return WC_TEST_RET_ENC_NC;
  30007. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  30008. return WC_TEST_RET_ENC_NC;
  30009. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  30010. sizeof(kyber768enc_rand));
  30011. if (ret != 0)
  30012. return WC_TEST_RET_ENC_EC(ret);
  30013. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  30014. return WC_TEST_RET_ENC_NC;
  30015. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  30016. return WC_TEST_RET_ENC_NC;
  30017. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  30018. if (ret != 0)
  30019. return WC_TEST_RET_ENC_EC(ret);
  30020. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  30021. return WC_TEST_RET_ENC_NC;
  30022. wc_KyberKey_Free(&key);
  30023. return 0;
  30024. }
  30025. #endif /* WOLFSSL_KYBER768 */
  30026. #ifdef WOLFSSL_KYBER1024
  30027. static wc_test_ret_t kyber1024_kat(void)
  30028. {
  30029. KyberKey key;
  30030. wc_test_ret_t ret;
  30031. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  30032. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  30033. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  30034. byte ss[KYBER_SS_SZ];
  30035. byte ss_dec[KYBER_SS_SZ];
  30036. const byte kyber1024_rand[] = {
  30037. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  30038. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  30039. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  30040. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  30041. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30042. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30043. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30044. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30045. };
  30046. const byte kyber1024enc_rand[] = {
  30047. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  30048. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  30049. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  30050. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  30051. };
  30052. const byte kyber1024_pk[] = {
  30053. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  30054. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  30055. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  30056. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  30057. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  30058. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  30059. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  30060. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  30061. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  30062. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  30063. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  30064. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  30065. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  30066. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  30067. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  30068. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  30069. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  30070. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  30071. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  30072. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  30073. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  30074. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  30075. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  30076. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  30077. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  30078. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  30079. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  30080. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  30081. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  30082. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  30083. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  30084. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  30085. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  30086. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  30087. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  30088. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  30089. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  30090. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  30091. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  30092. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  30093. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  30094. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  30095. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  30096. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  30097. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  30098. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  30099. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  30100. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  30101. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  30102. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  30103. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  30104. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  30105. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  30106. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  30107. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  30108. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  30109. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  30110. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  30111. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  30112. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  30113. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  30114. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  30115. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  30116. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  30117. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  30118. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  30119. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  30120. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  30121. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  30122. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  30123. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  30124. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  30125. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  30126. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  30127. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  30128. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  30129. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  30130. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  30131. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  30132. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  30133. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  30134. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  30135. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  30136. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  30137. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  30138. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  30139. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  30140. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  30141. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  30142. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  30143. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  30144. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  30145. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  30146. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  30147. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  30148. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  30149. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  30150. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  30151. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  30152. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  30153. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  30154. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  30155. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  30156. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  30157. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  30158. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  30159. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  30160. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  30161. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  30162. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  30163. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  30164. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  30165. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  30166. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  30167. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  30168. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  30169. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  30170. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  30171. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  30172. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  30173. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  30174. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  30175. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  30176. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  30177. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  30178. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  30179. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  30180. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  30181. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  30182. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  30183. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  30184. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  30185. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  30186. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  30187. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  30188. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  30189. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  30190. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  30191. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  30192. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  30193. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  30194. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  30195. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  30196. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  30197. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  30198. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  30199. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  30200. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  30201. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  30202. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  30203. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  30204. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  30205. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  30206. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  30207. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  30208. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  30209. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  30210. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  30211. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  30212. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  30213. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  30214. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  30215. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  30216. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  30217. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  30218. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  30219. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  30220. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  30221. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  30222. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  30223. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  30224. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  30225. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  30226. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  30227. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  30228. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  30229. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  30230. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  30231. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  30232. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  30233. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  30234. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  30235. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  30236. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  30237. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  30238. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  30239. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  30240. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  30241. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  30242. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  30243. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  30244. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  30245. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30246. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30247. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30248. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  30249. };
  30250. const byte kyber1024_sk[] = {
  30251. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  30252. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  30253. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  30254. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  30255. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  30256. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  30257. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  30258. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  30259. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  30260. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  30261. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  30262. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  30263. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  30264. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  30265. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  30266. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  30267. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  30268. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  30269. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  30270. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  30271. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  30272. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  30273. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  30274. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  30275. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  30276. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  30277. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  30278. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  30279. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  30280. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  30281. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  30282. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  30283. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  30284. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  30285. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  30286. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  30287. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  30288. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  30289. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  30290. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  30291. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  30292. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  30293. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  30294. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  30295. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  30296. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  30297. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  30298. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  30299. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  30300. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  30301. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  30302. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  30303. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  30304. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  30305. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  30306. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  30307. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  30308. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  30309. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  30310. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  30311. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  30312. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  30313. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  30314. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  30315. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  30316. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  30317. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  30318. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  30319. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  30320. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  30321. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  30322. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  30323. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  30324. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  30325. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  30326. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  30327. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  30328. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  30329. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  30330. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  30331. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  30332. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  30333. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  30334. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  30335. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  30336. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  30337. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  30338. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  30339. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  30340. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  30341. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  30342. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  30343. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  30344. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  30345. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  30346. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  30347. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  30348. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  30349. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  30350. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  30351. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  30352. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  30353. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  30354. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  30355. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  30356. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  30357. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  30358. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  30359. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  30360. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  30361. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  30362. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  30363. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  30364. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  30365. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  30366. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  30367. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  30368. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  30369. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  30370. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  30371. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  30372. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  30373. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  30374. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  30375. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  30376. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  30377. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  30378. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  30379. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  30380. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  30381. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  30382. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  30383. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  30384. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  30385. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  30386. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  30387. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  30388. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  30389. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  30390. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  30391. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  30392. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  30393. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  30394. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  30395. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  30396. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  30397. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  30398. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  30399. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  30400. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  30401. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  30402. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  30403. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  30404. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  30405. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  30406. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  30407. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  30408. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  30409. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  30410. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  30411. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  30412. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  30413. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  30414. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  30415. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  30416. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  30417. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  30418. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  30419. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  30420. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  30421. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  30422. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  30423. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  30424. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  30425. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  30426. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  30427. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  30428. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  30429. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  30430. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  30431. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  30432. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  30433. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  30434. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  30435. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  30436. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  30437. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  30438. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  30439. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  30440. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  30441. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  30442. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  30443. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  30444. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  30445. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  30446. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  30447. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  30448. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  30449. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  30450. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  30451. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  30452. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  30453. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  30454. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  30455. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  30456. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  30457. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  30458. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  30459. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  30460. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  30461. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  30462. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  30463. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  30464. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  30465. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  30466. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  30467. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  30468. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  30469. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  30470. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  30471. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  30472. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  30473. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  30474. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  30475. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  30476. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  30477. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  30478. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  30479. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  30480. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  30481. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  30482. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  30483. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  30484. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  30485. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  30486. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  30487. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  30488. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  30489. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  30490. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  30491. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  30492. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  30493. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  30494. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  30495. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  30496. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  30497. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  30498. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  30499. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  30500. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  30501. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  30502. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  30503. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  30504. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  30505. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  30506. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  30507. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  30508. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  30509. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  30510. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  30511. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  30512. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  30513. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  30514. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  30515. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  30516. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  30517. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  30518. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  30519. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  30520. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  30521. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  30522. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  30523. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  30524. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  30525. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  30526. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  30527. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  30528. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  30529. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  30530. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  30531. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  30532. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  30533. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  30534. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  30535. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  30536. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  30537. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  30538. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  30539. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  30540. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  30541. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  30542. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  30543. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  30544. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  30545. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  30546. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  30547. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  30548. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  30549. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  30550. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  30551. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  30552. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  30553. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  30554. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  30555. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  30556. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  30557. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  30558. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  30559. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  30560. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  30561. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  30562. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  30563. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  30564. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  30565. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  30566. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  30567. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  30568. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  30569. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  30570. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  30571. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  30572. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  30573. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  30574. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  30575. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  30576. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  30577. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  30578. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  30579. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  30580. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  30581. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  30582. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  30583. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  30584. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  30585. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  30586. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  30587. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  30588. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  30589. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  30590. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  30591. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  30592. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  30593. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  30594. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  30595. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  30596. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  30597. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  30598. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  30599. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  30600. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  30601. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  30602. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  30603. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  30604. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  30605. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  30606. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  30607. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  30608. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  30609. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  30610. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  30611. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  30612. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  30613. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  30614. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  30615. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  30616. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  30617. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  30618. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  30619. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  30620. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  30621. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  30622. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  30623. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  30624. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  30625. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  30626. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  30627. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  30628. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  30629. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  30630. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  30631. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  30632. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  30633. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  30634. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  30635. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30636. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30637. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30638. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  30639. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  30640. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  30641. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  30642. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  30643. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30644. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30645. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30646. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30647. };
  30648. const byte kyber1024_ct[] = {
  30649. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  30650. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  30651. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  30652. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  30653. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  30654. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  30655. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  30656. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  30657. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  30658. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  30659. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  30660. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  30661. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  30662. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  30663. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  30664. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  30665. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  30666. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  30667. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  30668. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  30669. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  30670. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  30671. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  30672. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  30673. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  30674. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  30675. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  30676. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  30677. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  30678. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  30679. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  30680. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  30681. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  30682. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  30683. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  30684. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  30685. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  30686. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  30687. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  30688. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  30689. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  30690. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  30691. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  30692. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  30693. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  30694. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  30695. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  30696. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  30697. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  30698. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  30699. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  30700. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  30701. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  30702. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  30703. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  30704. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  30705. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  30706. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  30707. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  30708. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  30709. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  30710. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  30711. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  30712. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  30713. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  30714. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  30715. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  30716. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  30717. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  30718. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  30719. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  30720. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  30721. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  30722. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  30723. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  30724. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  30725. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  30726. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  30727. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  30728. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  30729. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  30730. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  30731. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  30732. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  30733. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  30734. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  30735. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  30736. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  30737. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  30738. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  30739. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  30740. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  30741. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  30742. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  30743. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  30744. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  30745. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  30746. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  30747. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  30748. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  30749. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  30750. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  30751. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  30752. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  30753. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  30754. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  30755. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  30756. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  30757. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  30758. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  30759. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  30760. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  30761. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  30762. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  30763. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  30764. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  30765. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  30766. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  30767. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  30768. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  30769. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  30770. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  30771. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  30772. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  30773. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  30774. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  30775. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  30776. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  30777. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  30778. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  30779. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  30780. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  30781. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  30782. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  30783. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  30784. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  30785. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  30786. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  30787. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  30788. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  30789. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  30790. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  30791. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  30792. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  30793. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  30794. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  30795. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  30796. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  30797. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  30798. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  30799. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  30800. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  30801. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  30802. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  30803. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  30804. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  30805. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  30806. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  30807. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  30808. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  30809. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  30810. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  30811. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  30812. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  30813. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  30814. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  30815. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  30816. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  30817. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  30818. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  30819. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  30820. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  30821. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  30822. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  30823. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  30824. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  30825. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  30826. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  30827. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  30828. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  30829. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  30830. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  30831. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  30832. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  30833. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  30834. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  30835. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  30836. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  30837. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  30838. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  30839. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  30840. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  30841. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  30842. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  30843. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  30844. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  30845. };
  30846. const byte kyber1024_ss[] = {
  30847. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  30848. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  30849. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  30850. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  30851. };
  30852. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  30853. if (ret != 0)
  30854. return WC_TEST_RET_ENC_EC(ret);
  30855. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  30856. sizeof(kyber1024_rand));
  30857. if (ret != 0)
  30858. return WC_TEST_RET_ENC_EC(ret);
  30859. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30860. if (ret != 0)
  30861. return WC_TEST_RET_ENC_EC(ret);
  30862. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30863. if (ret != 0)
  30864. return WC_TEST_RET_ENC_EC(ret);
  30865. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  30866. return WC_TEST_RET_ENC_NC;
  30867. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  30868. return WC_TEST_RET_ENC_NC;
  30869. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  30870. sizeof(kyber1024enc_rand));
  30871. if (ret != 0)
  30872. return WC_TEST_RET_ENC_EC(ret);
  30873. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  30874. return WC_TEST_RET_ENC_NC;
  30875. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30876. return WC_TEST_RET_ENC_NC;
  30877. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  30878. if (ret != 0)
  30879. return WC_TEST_RET_ENC_EC(ret);
  30880. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30881. return WC_TEST_RET_ENC_NC;
  30882. wc_KyberKey_Free(&key);
  30883. return 0;
  30884. }
  30885. #endif /* WOLFSSL_KYBER1024 */
  30886. #endif /* WOLFSSL_WC_KYBER */
  30887. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  30888. {
  30889. wc_test_ret_t ret;
  30890. KyberKey key;
  30891. WC_RNG rng;
  30892. int i;
  30893. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  30894. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  30895. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  30896. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  30897. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  30898. byte ss[KYBER_SS_SZ];
  30899. byte ss_dec[KYBER_SS_SZ];
  30900. int testData[][4] = {
  30901. #ifdef WOLFSSL_KYBER512
  30902. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  30903. KYBER512_CIPHER_TEXT_SIZE },
  30904. #endif
  30905. #ifdef WOLFSSL_KYBER768
  30906. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  30907. KYBER768_CIPHER_TEXT_SIZE },
  30908. #endif
  30909. #ifdef WOLFSSL_KYBER1024
  30910. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  30911. KYBER1024_CIPHER_TEXT_SIZE },
  30912. #endif
  30913. };
  30914. #ifndef HAVE_FIPS
  30915. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  30916. #else
  30917. ret = wc_InitRng(&rng);
  30918. #endif
  30919. if (ret != 0)
  30920. return WC_TEST_RET_ENC_EC(ret);
  30921. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  30922. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30923. if (ret != 0)
  30924. return WC_TEST_RET_ENC_I(i);
  30925. ret = wc_KyberKey_MakeKey(&key, &rng);
  30926. if (ret != 0)
  30927. return WC_TEST_RET_ENC_I(i);
  30928. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  30929. if (ret != 0)
  30930. return WC_TEST_RET_ENC_I(i);
  30931. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  30932. if (ret != 0)
  30933. return WC_TEST_RET_ENC_I(i);
  30934. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30935. if (ret != 0)
  30936. return WC_TEST_RET_ENC_I(i);
  30937. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  30938. if (ret != 0)
  30939. return WC_TEST_RET_ENC_I(i);
  30940. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  30941. if (ret != 0)
  30942. return WC_TEST_RET_ENC_I(i);
  30943. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  30944. if (ret != 0)
  30945. return WC_TEST_RET_ENC_I(i);
  30946. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  30947. return WC_TEST_RET_ENC_I(i);
  30948. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30949. if (ret != 0)
  30950. return WC_TEST_RET_ENC_I(i);
  30951. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  30952. if (ret != 0)
  30953. return WC_TEST_RET_ENC_I(i);
  30954. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  30955. if (ret != 0)
  30956. return WC_TEST_RET_ENC_I(i);
  30957. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  30958. return WC_TEST_RET_ENC_I(i);
  30959. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  30960. if (ret != 0)
  30961. return WC_TEST_RET_ENC_I(i);
  30962. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  30963. return WC_TEST_RET_ENC_I(i);
  30964. wc_KyberKey_Free(&key);
  30965. }
  30966. wc_FreeRng(&rng);
  30967. #ifdef WOLFSSL_WC_KYBER
  30968. #ifdef WOLFSSL_KYBER512
  30969. ret = kyber512_kat();
  30970. if (ret != 0)
  30971. return ret;
  30972. #endif
  30973. #ifdef WOLFSSL_KYBER768
  30974. ret = kyber768_kat();
  30975. if (ret != 0)
  30976. return ret;
  30977. #endif
  30978. #ifdef WOLFSSL_KYBER1024
  30979. ret = kyber1024_kat();
  30980. if (ret != 0)
  30981. return ret;
  30982. #endif
  30983. #endif /* WOLFSSL_WC_KYBER */
  30984. return 0;
  30985. }
  30986. #endif /* WOLFSSL_HAVE_KYBER */
  30987. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  30988. * see print_fiducials() below.
  30989. */
  30990. #ifdef WOLFCRYPT_HAVE_ECCSI
  30991. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  30992. ecc_point* pvt)
  30993. {
  30994. wc_test_ret_t ret;
  30995. byte id[1] = { 0x00 };
  30996. int valid;
  30997. word32 sz;
  30998. byte data[256];
  30999. byte hash[WC_MAX_DIGEST_SIZE];
  31000. byte hashSz;
  31001. byte sig[257];
  31002. word32 sigSz;
  31003. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  31004. if (ret != BAD_FUNC_ARG)
  31005. return WC_TEST_RET_ENC_EC(ret);
  31006. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  31007. if (ret != BAD_FUNC_ARG)
  31008. return WC_TEST_RET_ENC_EC(ret);
  31009. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  31010. if (ret != BAD_FUNC_ARG)
  31011. return WC_TEST_RET_ENC_EC(ret);
  31012. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  31013. if (ret != BAD_FUNC_ARG)
  31014. return WC_TEST_RET_ENC_EC(ret);
  31015. wc_FreeEccsiKey(NULL);
  31016. /* Create a valid key. */
  31017. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  31018. if (ret != 0)
  31019. return WC_TEST_RET_ENC_EC(ret);
  31020. ret = wc_MakeEccsiKey(NULL, NULL);
  31021. if (ret != BAD_FUNC_ARG)
  31022. return WC_TEST_RET_ENC_EC(ret);
  31023. ret = wc_MakeEccsiKey(key, NULL);
  31024. if (ret != BAD_FUNC_ARG)
  31025. return WC_TEST_RET_ENC_EC(ret);
  31026. ret = wc_MakeEccsiKey(NULL, rng);
  31027. if (ret != BAD_FUNC_ARG)
  31028. return WC_TEST_RET_ENC_EC(ret);
  31029. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  31030. NULL);
  31031. if (ret != BAD_FUNC_ARG)
  31032. return WC_TEST_RET_ENC_EC(ret);
  31033. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  31034. if (ret != BAD_FUNC_ARG)
  31035. return WC_TEST_RET_ENC_EC(ret);
  31036. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  31037. if (ret != BAD_FUNC_ARG)
  31038. return WC_TEST_RET_ENC_EC(ret);
  31039. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  31040. if (ret != BAD_FUNC_ARG)
  31041. return WC_TEST_RET_ENC_EC(ret);
  31042. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31043. if (ret != BAD_FUNC_ARG)
  31044. return WC_TEST_RET_ENC_EC(ret);
  31045. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31046. if (ret != BAD_FUNC_ARG)
  31047. return WC_TEST_RET_ENC_EC(ret);
  31048. /* No key set */
  31049. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31050. if (ret != BAD_STATE_E)
  31051. return WC_TEST_RET_ENC_EC(ret);
  31052. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  31053. NULL);
  31054. if (ret != BAD_FUNC_ARG)
  31055. return WC_TEST_RET_ENC_EC(ret);
  31056. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31057. NULL);
  31058. if (ret != BAD_FUNC_ARG)
  31059. return WC_TEST_RET_ENC_EC(ret);
  31060. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  31061. &valid);
  31062. if (ret != BAD_FUNC_ARG)
  31063. return WC_TEST_RET_ENC_EC(ret);
  31064. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  31065. &valid);
  31066. if (ret != BAD_FUNC_ARG)
  31067. return WC_TEST_RET_ENC_EC(ret);
  31068. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  31069. &valid);
  31070. if (ret != BAD_FUNC_ARG)
  31071. return WC_TEST_RET_ENC_EC(ret);
  31072. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31073. &valid);
  31074. if (ret != BAD_FUNC_ARG)
  31075. return WC_TEST_RET_ENC_EC(ret);
  31076. /* No key set */
  31077. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31078. &valid);
  31079. if (ret != BAD_STATE_E)
  31080. return WC_TEST_RET_ENC_EC(ret);
  31081. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  31082. if (ret != BAD_FUNC_ARG)
  31083. return WC_TEST_RET_ENC_EC(ret);
  31084. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  31085. if (ret != BAD_FUNC_ARG)
  31086. return WC_TEST_RET_ENC_EC(ret);
  31087. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  31088. if (ret != BAD_FUNC_ARG)
  31089. return WC_TEST_RET_ENC_EC(ret);
  31090. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  31091. if (ret != BAD_FUNC_ARG)
  31092. return WC_TEST_RET_ENC_EC(ret);
  31093. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  31094. if (ret != BAD_FUNC_ARG)
  31095. return WC_TEST_RET_ENC_EC(ret);
  31096. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  31097. if (ret != BAD_FUNC_ARG)
  31098. return WC_TEST_RET_ENC_EC(ret);
  31099. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  31100. if (ret != BAD_FUNC_ARG)
  31101. return WC_TEST_RET_ENC_EC(ret);
  31102. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  31103. if (ret != BAD_FUNC_ARG)
  31104. return WC_TEST_RET_ENC_EC(ret);
  31105. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  31106. if (ret != BAD_FUNC_ARG)
  31107. return WC_TEST_RET_ENC_EC(ret);
  31108. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  31109. if (ret != BAD_FUNC_ARG)
  31110. return WC_TEST_RET_ENC_EC(ret);
  31111. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  31112. if (ret != BAD_FUNC_ARG)
  31113. return WC_TEST_RET_ENC_EC(ret);
  31114. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  31115. if (ret != BAD_FUNC_ARG)
  31116. return WC_TEST_RET_ENC_EC(ret);
  31117. /* No key created so no curve information. */
  31118. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  31119. if (ret != LENGTH_ONLY_E)
  31120. return WC_TEST_RET_ENC_EC(ret);
  31121. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  31122. if (ret != BAD_FUNC_ARG)
  31123. return WC_TEST_RET_ENC_EC(ret);
  31124. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  31125. if (ret != BAD_FUNC_ARG)
  31126. return WC_TEST_RET_ENC_EC(ret);
  31127. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  31128. if (ret != BAD_FUNC_ARG)
  31129. return WC_TEST_RET_ENC_EC(ret);
  31130. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  31131. if (ret != BAD_FUNC_ARG)
  31132. return WC_TEST_RET_ENC_EC(ret);
  31133. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  31134. if (ret != BAD_FUNC_ARG)
  31135. return WC_TEST_RET_ENC_EC(ret);
  31136. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  31137. if (ret != BAD_FUNC_ARG)
  31138. return WC_TEST_RET_ENC_EC(ret);
  31139. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  31140. if (ret != BAD_FUNC_ARG)
  31141. return WC_TEST_RET_ENC_EC(ret);
  31142. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  31143. if (ret != BAD_FUNC_ARG)
  31144. return WC_TEST_RET_ENC_EC(ret);
  31145. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  31146. if (ret != BAD_FUNC_ARG)
  31147. return WC_TEST_RET_ENC_EC(ret);
  31148. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  31149. if (ret != BAD_FUNC_ARG)
  31150. return WC_TEST_RET_ENC_EC(ret);
  31151. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  31152. if (ret != BAD_FUNC_ARG)
  31153. return WC_TEST_RET_ENC_EC(ret);
  31154. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  31155. if (ret != BAD_FUNC_ARG)
  31156. return WC_TEST_RET_ENC_EC(ret);
  31157. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  31158. if (ret != BAD_FUNC_ARG)
  31159. return WC_TEST_RET_ENC_EC(ret);
  31160. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  31161. if (ret != BAD_FUNC_ARG)
  31162. return WC_TEST_RET_ENC_EC(ret);
  31163. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  31164. if (ret != BAD_FUNC_ARG)
  31165. return WC_TEST_RET_ENC_EC(ret);
  31166. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  31167. if (ret != BAD_FUNC_ARG)
  31168. return WC_TEST_RET_ENC_EC(ret);
  31169. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  31170. if (ret != BAD_FUNC_ARG)
  31171. return WC_TEST_RET_ENC_EC(ret);
  31172. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  31173. if (ret != BAD_FUNC_ARG)
  31174. return WC_TEST_RET_ENC_EC(ret);
  31175. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  31176. if (ret != BAD_FUNC_ARG)
  31177. return WC_TEST_RET_ENC_EC(ret);
  31178. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  31179. if (ret != BAD_FUNC_ARG)
  31180. return WC_TEST_RET_ENC_EC(ret);
  31181. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  31182. if (ret != BAD_FUNC_ARG)
  31183. return WC_TEST_RET_ENC_EC(ret);
  31184. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  31185. if (ret != BAD_FUNC_ARG)
  31186. return WC_TEST_RET_ENC_EC(ret);
  31187. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  31188. if (ret != BAD_FUNC_ARG)
  31189. return WC_TEST_RET_ENC_EC(ret);
  31190. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  31191. if (ret != BAD_FUNC_ARG)
  31192. return WC_TEST_RET_ENC_EC(ret);
  31193. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  31194. if (ret != BAD_FUNC_ARG)
  31195. return WC_TEST_RET_ENC_EC(ret);
  31196. ret = wc_ExportEccsiKey(NULL, data, NULL);
  31197. if (ret != BAD_FUNC_ARG)
  31198. return WC_TEST_RET_ENC_EC(ret);
  31199. ret = wc_ExportEccsiKey(key, data, NULL);
  31200. if (ret != BAD_FUNC_ARG)
  31201. return WC_TEST_RET_ENC_EC(ret);
  31202. ret = wc_ExportEccsiKey(NULL, data, &sz);
  31203. if (ret != BAD_FUNC_ARG)
  31204. return WC_TEST_RET_ENC_EC(ret);
  31205. /* No key to export */
  31206. ret = wc_ExportEccsiKey(key, NULL, &sz);
  31207. if (ret != BAD_STATE_E)
  31208. return WC_TEST_RET_ENC_EC(ret);
  31209. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  31210. if (ret != BAD_FUNC_ARG)
  31211. return WC_TEST_RET_ENC_EC(ret);
  31212. ret = wc_ImportEccsiKey(key, NULL, 0);
  31213. if (ret != BAD_FUNC_ARG)
  31214. return WC_TEST_RET_ENC_EC(ret);
  31215. ret = wc_ImportEccsiKey(NULL, data, 0);
  31216. if (ret != BAD_FUNC_ARG)
  31217. return WC_TEST_RET_ENC_EC(ret);
  31218. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  31219. if (ret != BAD_FUNC_ARG)
  31220. return WC_TEST_RET_ENC_EC(ret);
  31221. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  31222. if (ret != BAD_FUNC_ARG)
  31223. return WC_TEST_RET_ENC_EC(ret);
  31224. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  31225. if (ret != BAD_FUNC_ARG)
  31226. return WC_TEST_RET_ENC_EC(ret);
  31227. /* No key to export */
  31228. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  31229. if (ret != BAD_STATE_E)
  31230. return WC_TEST_RET_ENC_EC(ret);
  31231. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  31232. if (ret != BAD_FUNC_ARG)
  31233. return WC_TEST_RET_ENC_EC(ret);
  31234. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  31235. if (ret != BAD_FUNC_ARG)
  31236. return WC_TEST_RET_ENC_EC(ret);
  31237. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  31238. if (ret != BAD_FUNC_ARG)
  31239. return WC_TEST_RET_ENC_EC(ret);
  31240. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  31241. if (ret != BAD_FUNC_ARG)
  31242. return WC_TEST_RET_ENC_EC(ret);
  31243. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  31244. if (ret != BAD_FUNC_ARG)
  31245. return WC_TEST_RET_ENC_EC(ret);
  31246. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  31247. if (ret != BAD_FUNC_ARG)
  31248. return WC_TEST_RET_ENC_EC(ret);
  31249. /* No key to export */
  31250. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  31251. if (ret != BAD_STATE_E)
  31252. return WC_TEST_RET_ENC_EC(ret);
  31253. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  31254. if (ret != BAD_FUNC_ARG)
  31255. return WC_TEST_RET_ENC_EC(ret);
  31256. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  31257. if (ret != BAD_FUNC_ARG)
  31258. return WC_TEST_RET_ENC_EC(ret);
  31259. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  31260. if (ret != BAD_FUNC_ARG)
  31261. return WC_TEST_RET_ENC_EC(ret);
  31262. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  31263. if (ret != BAD_FUNC_ARG)
  31264. return WC_TEST_RET_ENC_EC(ret);
  31265. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  31266. if (ret != BAD_FUNC_ARG)
  31267. return WC_TEST_RET_ENC_EC(ret);
  31268. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  31269. if (ret != BAD_FUNC_ARG)
  31270. return WC_TEST_RET_ENC_EC(ret);
  31271. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  31272. if (ret != BAD_FUNC_ARG)
  31273. return WC_TEST_RET_ENC_EC(ret);
  31274. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  31275. &hashSz);
  31276. if (ret != BAD_FUNC_ARG)
  31277. return WC_TEST_RET_ENC_EC(ret);
  31278. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  31279. if (ret != BAD_FUNC_ARG)
  31280. return WC_TEST_RET_ENC_EC(ret);
  31281. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  31282. if (ret != BAD_STATE_E)
  31283. return WC_TEST_RET_ENC_EC(ret);
  31284. ret = wc_SetEccsiHash(NULL, NULL, 1);
  31285. if (ret != BAD_FUNC_ARG)
  31286. return WC_TEST_RET_ENC_EC(ret);
  31287. ret = wc_SetEccsiHash(key, NULL, 1);
  31288. if (ret != BAD_FUNC_ARG)
  31289. return WC_TEST_RET_ENC_EC(ret);
  31290. ret = wc_SetEccsiHash(NULL, hash, 1);
  31291. if (ret != BAD_FUNC_ARG)
  31292. return WC_TEST_RET_ENC_EC(ret);
  31293. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  31294. if (ret != BAD_FUNC_ARG)
  31295. return WC_TEST_RET_ENC_EC(ret);
  31296. ret = wc_SetEccsiPair(key, NULL, NULL);
  31297. if (ret != BAD_FUNC_ARG)
  31298. return WC_TEST_RET_ENC_EC(ret);
  31299. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  31300. if (ret != BAD_FUNC_ARG)
  31301. return WC_TEST_RET_ENC_EC(ret);
  31302. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  31303. if (ret != BAD_FUNC_ARG)
  31304. return WC_TEST_RET_ENC_EC(ret);
  31305. ret = wc_SetEccsiPair(key, ssk, NULL);
  31306. if (ret != BAD_FUNC_ARG)
  31307. return WC_TEST_RET_ENC_EC(ret);
  31308. ret = wc_SetEccsiPair(key, NULL, pvt);
  31309. if (ret != BAD_FUNC_ARG)
  31310. return WC_TEST_RET_ENC_EC(ret);
  31311. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  31312. if (ret != BAD_FUNC_ARG)
  31313. return WC_TEST_RET_ENC_EC(ret);
  31314. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  31315. if (ret != BAD_FUNC_ARG)
  31316. return WC_TEST_RET_ENC_EC(ret);
  31317. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  31318. if (ret != BAD_FUNC_ARG)
  31319. return WC_TEST_RET_ENC_EC(ret);
  31320. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  31321. &sigSz);
  31322. if (ret != BAD_FUNC_ARG)
  31323. return WC_TEST_RET_ENC_EC(ret);
  31324. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  31325. &sigSz);
  31326. if (ret != BAD_FUNC_ARG)
  31327. return WC_TEST_RET_ENC_EC(ret);
  31328. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  31329. &sigSz);
  31330. if (ret != BAD_FUNC_ARG)
  31331. return WC_TEST_RET_ENC_EC(ret);
  31332. /* Key not set. */
  31333. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31334. &sigSz);
  31335. if (ret != BAD_STATE_E)
  31336. return WC_TEST_RET_ENC_EC(ret);
  31337. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  31338. if (ret != BAD_FUNC_ARG)
  31339. return WC_TEST_RET_ENC_EC(ret);
  31340. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  31341. if (ret != BAD_FUNC_ARG)
  31342. return WC_TEST_RET_ENC_EC(ret);
  31343. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  31344. if (ret != BAD_FUNC_ARG)
  31345. return WC_TEST_RET_ENC_EC(ret);
  31346. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  31347. if (ret != BAD_FUNC_ARG)
  31348. return WC_TEST_RET_ENC_EC(ret);
  31349. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  31350. &valid);
  31351. if (ret != BAD_FUNC_ARG)
  31352. return WC_TEST_RET_ENC_EC(ret);
  31353. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  31354. if (ret != BAD_FUNC_ARG)
  31355. return WC_TEST_RET_ENC_EC(ret);
  31356. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  31357. &valid);
  31358. if (ret != BAD_FUNC_ARG)
  31359. return WC_TEST_RET_ENC_EC(ret);
  31360. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  31361. &valid);
  31362. if (ret != BAD_FUNC_ARG)
  31363. return WC_TEST_RET_ENC_EC(ret);
  31364. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31365. &valid);
  31366. if (ret != BAD_FUNC_ARG)
  31367. return WC_TEST_RET_ENC_EC(ret);
  31368. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31369. &valid);
  31370. if (ret != BAD_STATE_E)
  31371. return WC_TEST_RET_ENC_EC(ret);
  31372. ret = wc_SetEccsiPair(key, ssk, pvt);
  31373. if (ret != 0)
  31374. return WC_TEST_RET_ENC_EC(ret);
  31375. /* Identity hash not set. */
  31376. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31377. &sigSz);
  31378. if (ret != BAD_STATE_E)
  31379. return WC_TEST_RET_ENC_EC(ret);
  31380. wc_FreeEccsiKey(key);
  31381. return 0;
  31382. }
  31383. /* RFC 6507: Appendix A */
  31384. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  31385. {
  31386. wc_test_ret_t ret;
  31387. int verified;
  31388. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  31389. word32 msgSz = sizeof(msg);
  31390. byte hash[WC_SHA256_DIGEST_SIZE];
  31391. byte hashSz = WC_SHA256_DIGEST_SIZE;
  31392. static const byte id[] = {
  31393. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  31394. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  31395. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  31396. 0x33, 0x00
  31397. };
  31398. word32 idSz = sizeof(id);
  31399. static const byte sig[] = {
  31400. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  31401. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  31402. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  31403. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  31404. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  31405. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  31406. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  31407. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  31408. 0x04,
  31409. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  31410. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  31411. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  31412. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  31413. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  31414. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  31415. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  31416. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  31417. };
  31418. word32 sigSz = sizeof(sig);
  31419. static const byte pubData[] = {
  31420. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  31421. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  31422. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  31423. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  31424. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  31425. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  31426. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  31427. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  31428. };
  31429. static const byte expHash[] = {
  31430. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  31431. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  31432. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  31433. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  31434. };
  31435. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  31436. if (ret != 0)
  31437. return WC_TEST_RET_ENC_EC(ret);
  31438. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  31439. if (ret != 0)
  31440. return WC_TEST_RET_ENC_EC(ret);
  31441. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  31442. &hashSz);
  31443. if (ret != 0)
  31444. return WC_TEST_RET_ENC_EC(ret);
  31445. if (hashSz != sizeof(expHash))
  31446. return WC_TEST_RET_ENC_NC;
  31447. if (XMEMCMP(hash, expHash, hashSz) != 0)
  31448. return WC_TEST_RET_ENC_NC;
  31449. ret = wc_SetEccsiHash(key, hash, hashSz);
  31450. if (ret != 0)
  31451. return WC_TEST_RET_ENC_EC(ret);
  31452. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31453. &verified);
  31454. if (ret != 0)
  31455. return WC_TEST_RET_ENC_EC(ret);
  31456. if (!verified)
  31457. return WC_TEST_RET_ENC_NC;
  31458. return 0;
  31459. }
  31460. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  31461. {
  31462. wc_test_ret_t ret;
  31463. byte data[32 * 3];
  31464. word32 sz;
  31465. ecc_point* decPvt = NULL;
  31466. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31467. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  31468. DYNAMIC_TYPE_TMP_BUFFER);
  31469. if (decSsk == NULL)
  31470. return WC_TEST_RET_ENC_ERRNO;
  31471. #else
  31472. mp_int decSsk[1];
  31473. #endif
  31474. ret = mp_init(decSsk);
  31475. if (ret != 0)
  31476. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31477. decPvt = wc_ecc_new_point();
  31478. if (decPvt == NULL)
  31479. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  31480. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  31481. if (ret != LENGTH_ONLY_E)
  31482. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31483. if (sz != 32 * 3)
  31484. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31485. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  31486. if (ret != 0)
  31487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31488. if (sz != 32* 3)
  31489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31490. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  31491. if (ret != 0)
  31492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31493. ret = mp_cmp(ssk, decSsk);
  31494. if (ret != MP_EQ)
  31495. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31496. ret = wc_ecc_cmp_point(pvt, decPvt);
  31497. if (ret != MP_EQ)
  31498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31499. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  31500. if (ret != LENGTH_ONLY_E)
  31501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31502. if (sz != 32)
  31503. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31504. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  31505. if (ret != 0)
  31506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31507. if (sz != 32)
  31508. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31509. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  31510. if (ret != 0)
  31511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31512. ret = mp_cmp(ssk, decSsk);
  31513. if (ret != MP_EQ)
  31514. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31515. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  31516. if (ret != LENGTH_ONLY_E)
  31517. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31518. if (sz != 32 * 2)
  31519. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31520. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  31521. if (ret != 0)
  31522. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31523. if (sz != 32 * 2)
  31524. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31525. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31526. if (ret != 0)
  31527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31528. ret = wc_ecc_cmp_point(pvt, decPvt);
  31529. if (ret != MP_EQ)
  31530. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31531. sz = sizeof(data);
  31532. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  31533. if (ret != 0)
  31534. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31535. if (sz != 32 * 2 + 1)
  31536. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31537. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31538. if (ret != 0)
  31539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31540. ret = wc_ecc_cmp_point(pvt, decPvt);
  31541. if (ret != MP_EQ)
  31542. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31543. wc_ecc_del_point(decPvt);
  31544. out:
  31545. mp_free(decSsk);
  31546. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31547. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31548. #endif
  31549. return ret;
  31550. }
  31551. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  31552. {
  31553. wc_test_ret_t ret;
  31554. byte data[32 * 3];
  31555. byte out[32 * 3];
  31556. word32 sz;
  31557. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31558. if (ret != LENGTH_ONLY_E)
  31559. return WC_TEST_RET_ENC_EC(ret);
  31560. if (sz != 32 * 3)
  31561. return WC_TEST_RET_ENC_NC;
  31562. ret = wc_ExportEccsiKey(priv, data, &sz);
  31563. if (ret != 0)
  31564. return WC_TEST_RET_ENC_EC(ret);
  31565. ret = wc_ImportEccsiKey(priv, data, sz);
  31566. if (ret != 0)
  31567. return WC_TEST_RET_ENC_EC(ret);
  31568. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31569. if (ret != LENGTH_ONLY_E)
  31570. return WC_TEST_RET_ENC_EC(ret);
  31571. if (sz != 32 * 3)
  31572. return WC_TEST_RET_ENC_NC;
  31573. ret = wc_ExportEccsiKey(priv, out, &sz);
  31574. if (ret != 0)
  31575. return WC_TEST_RET_ENC_EC(ret);
  31576. if (sz != 32 * 3)
  31577. return WC_TEST_RET_ENC_NC;
  31578. if (XMEMCMP(data, out, sz) != 0)
  31579. return WC_TEST_RET_ENC_NC;
  31580. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31581. if (ret != LENGTH_ONLY_E)
  31582. return WC_TEST_RET_ENC_EC(ret);
  31583. if (sz != 32)
  31584. return WC_TEST_RET_ENC_NC;
  31585. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  31586. if (ret != 0)
  31587. return WC_TEST_RET_ENC_EC(ret);
  31588. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  31589. if (ret != 0)
  31590. return WC_TEST_RET_ENC_EC(ret);
  31591. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31592. if (ret != LENGTH_ONLY_E)
  31593. return WC_TEST_RET_ENC_EC(ret);
  31594. if (sz != 32)
  31595. return WC_TEST_RET_ENC_NC;
  31596. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  31597. if (ret != 0)
  31598. return WC_TEST_RET_ENC_EC(ret);
  31599. if (sz != 32)
  31600. return WC_TEST_RET_ENC_NC;
  31601. if (XMEMCMP(data, out, sz) != 0)
  31602. return WC_TEST_RET_ENC_NC;
  31603. return 0;
  31604. }
  31605. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  31606. {
  31607. wc_test_ret_t ret;
  31608. byte data[32 * 2 + 1];
  31609. byte pubData[32 * 2 + 1];
  31610. word32 sz;
  31611. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  31612. if (ret != LENGTH_ONLY_E)
  31613. return WC_TEST_RET_ENC_EC(ret);
  31614. if (sz != 32 * 2)
  31615. return WC_TEST_RET_ENC_NC;
  31616. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  31617. if (ret != 0)
  31618. return WC_TEST_RET_ENC_EC(ret);
  31619. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  31620. if (ret != 0)
  31621. return WC_TEST_RET_ENC_EC(ret);
  31622. sz = sizeof(pubData);
  31623. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  31624. if (ret != 0)
  31625. return WC_TEST_RET_ENC_EC(ret);
  31626. if (sz != 32 * 2)
  31627. return WC_TEST_RET_ENC_NC;
  31628. if (XMEMCMP(data, pubData, sz) != 0)
  31629. return WC_TEST_RET_ENC_NC;
  31630. sz = sizeof(pubData);
  31631. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  31632. if (ret != 0)
  31633. return WC_TEST_RET_ENC_EC(ret);
  31634. if (sz != 32 * 2 + 1)
  31635. return WC_TEST_RET_ENC_NC;
  31636. if (pubData[0] != 0x04)
  31637. return WC_TEST_RET_ENC_NC;
  31638. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  31639. return WC_TEST_RET_ENC_NC;
  31640. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  31641. if (ret != 0)
  31642. return WC_TEST_RET_ENC_EC(ret);
  31643. return 0;
  31644. }
  31645. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31646. mp_int* ssk, ecc_point* pvt)
  31647. {
  31648. wc_test_ret_t ret;
  31649. char mail[] = "test@wolfssl.com";
  31650. byte* id = (byte*)mail;
  31651. word32 idSz = (word32) XSTRLEN(mail);
  31652. int valid;
  31653. ret = wc_MakeEccsiKey(priv, rng);
  31654. if (ret != 0)
  31655. return WC_TEST_RET_ENC_EC(ret);
  31656. ret = eccsi_imp_exp_key_test(priv);
  31657. if (ret < 0)
  31658. return ret;
  31659. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31660. if (ret < 0)
  31661. return ret;
  31662. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  31663. if (ret != 0)
  31664. return WC_TEST_RET_ENC_EC(ret);
  31665. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31666. &valid);
  31667. if (ret != 0)
  31668. return WC_TEST_RET_ENC_EC(ret);
  31669. if (!valid)
  31670. return WC_TEST_RET_ENC_NC;
  31671. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  31672. if (ret != 0)
  31673. return ret;
  31674. return 0;
  31675. }
  31676. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31677. mp_int* ssk, ecc_point* pvt)
  31678. {
  31679. wc_test_ret_t ret;
  31680. byte hashPriv[WC_MAX_DIGEST_SIZE];
  31681. byte hashPub[WC_MAX_DIGEST_SIZE];
  31682. byte hashSz;
  31683. byte sig[144];
  31684. word32 sigSz;
  31685. int verified, valid;
  31686. char mail[] = "test@wolfssl.com";
  31687. byte* id = (byte*)mail;
  31688. word32 idSz = (word32) XSTRLEN(mail);
  31689. byte msg[] = { 0x00 };
  31690. word32 msgSz = sizeof(msg);
  31691. #ifdef WOLFSSL_SHA384
  31692. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  31693. &hashSz);
  31694. if (ret != BAD_FUNC_ARG)
  31695. return WC_TEST_RET_ENC_EC(ret);
  31696. #endif
  31697. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  31698. &hashSz);
  31699. if (ret != 0)
  31700. return WC_TEST_RET_ENC_EC(ret);
  31701. if (hashSz != 32)
  31702. return WC_TEST_RET_ENC_NC;
  31703. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  31704. &hashSz);
  31705. if (ret != 0)
  31706. return WC_TEST_RET_ENC_EC(ret);
  31707. if (hashSz != 32)
  31708. return WC_TEST_RET_ENC_NC;
  31709. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  31710. return WC_TEST_RET_ENC_NC;
  31711. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  31712. if (ret != 0)
  31713. return WC_TEST_RET_ENC_EC(ret);
  31714. ret = wc_SetEccsiPair(priv, ssk, pvt);
  31715. if (ret != 0)
  31716. return WC_TEST_RET_ENC_EC(ret);
  31717. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  31718. &sigSz);
  31719. if (ret != LENGTH_ONLY_E)
  31720. return WC_TEST_RET_ENC_EC(ret);
  31721. if (sigSz != 129)
  31722. return WC_TEST_RET_ENC_NC;
  31723. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  31724. &sigSz);
  31725. if (ret != 0)
  31726. return WC_TEST_RET_ENC_EC(ret);
  31727. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31728. if (ret != 0)
  31729. return WC_TEST_RET_ENC_EC(ret);
  31730. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31731. &verified);
  31732. if (ret != 0)
  31733. return WC_TEST_RET_ENC_EC(ret);
  31734. if (!verified)
  31735. return WC_TEST_RET_ENC_NC;
  31736. /* Check that changing HS results in verification failure. */
  31737. hashPub[0] ^= 0x80;
  31738. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31739. if (ret != 0)
  31740. return WC_TEST_RET_ENC_EC(ret);
  31741. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31742. &verified);
  31743. if (ret != 0)
  31744. return WC_TEST_RET_ENC_EC(ret);
  31745. if (verified)
  31746. return WC_TEST_RET_ENC_NC;
  31747. hashPub[0] ^= 0x80;
  31748. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31749. if (ret != 0)
  31750. return WC_TEST_RET_ENC_EC(ret);
  31751. /* Check that changing msg results in verification failure. */
  31752. msg[0] ^= 0x80;
  31753. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31754. &verified);
  31755. if (ret != 0)
  31756. return WC_TEST_RET_ENC_EC(ret);
  31757. if (verified)
  31758. return WC_TEST_RET_ENC_NC;
  31759. msg[0] ^= 0x80;
  31760. /* Check that changing signature results in verification failure. */
  31761. sig[0] ^= 0x80;
  31762. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31763. &verified);
  31764. if (ret != 0)
  31765. return WC_TEST_RET_ENC_EC(ret);
  31766. if (verified)
  31767. return WC_TEST_RET_ENC_NC;
  31768. sig[0] ^= 0x80;
  31769. /* Check that key state hasn't been invalidated. */
  31770. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31771. &verified);
  31772. if (ret != 0)
  31773. return WC_TEST_RET_ENC_EC(ret);
  31774. if (!verified)
  31775. return WC_TEST_RET_ENC_NC;
  31776. /* Check that verifying with the private key works. */
  31777. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31778. &verified);
  31779. if (ret != 0)
  31780. return WC_TEST_RET_ENC_EC(ret);
  31781. if (!verified)
  31782. return WC_TEST_RET_ENC_NC;
  31783. /* Check that the KPAK is converted from montgomery form. */
  31784. ret = eccsi_imp_exp_key_test(priv);
  31785. if (ret != 0)
  31786. return ret;
  31787. /* Check that KPAK can converted to Montgomery form again. */
  31788. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31789. &verified);
  31790. if (ret != 0)
  31791. return WC_TEST_RET_ENC_EC(ret);
  31792. if (!verified)
  31793. return WC_TEST_RET_ENC_NC;
  31794. /* Check that the KPAK is converted from montgomery form. */
  31795. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31796. &valid);
  31797. if (ret != 0)
  31798. return WC_TEST_RET_ENC_EC(ret);
  31799. if (!valid)
  31800. return WC_TEST_RET_ENC_NC;
  31801. /* Check that KPAK can converted to Montgomery form again. */
  31802. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31803. &verified);
  31804. if (ret != 0)
  31805. return WC_TEST_RET_ENC_EC(ret);
  31806. if (!verified)
  31807. return WC_TEST_RET_ENC_NC;
  31808. /* Check that the KPAK is converted from montgomery form. */
  31809. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31810. if (ret != 0)
  31811. return ret;
  31812. return 0;
  31813. }
  31814. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  31815. {
  31816. wc_test_ret_t ret = 0;
  31817. WC_RNG rng;
  31818. int rng_inited = 0;
  31819. EccsiKey* priv = NULL;
  31820. EccsiKey* pub = NULL;
  31821. mp_int* ssk = NULL;
  31822. ecc_point* pvt = NULL;
  31823. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31824. DYNAMIC_TYPE_TMP_BUFFER);
  31825. if (priv == NULL)
  31826. ret = WC_TEST_RET_ENC_NC;
  31827. else
  31828. XMEMSET(priv, 0, sizeof(*priv));
  31829. if (ret == 0) {
  31830. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31831. DYNAMIC_TYPE_TMP_BUFFER);
  31832. if (pub == NULL)
  31833. ret = WC_TEST_RET_ENC_NC;
  31834. else
  31835. XMEMSET(pub, 0, sizeof(*pub));
  31836. }
  31837. if (ret == 0) {
  31838. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  31839. DYNAMIC_TYPE_TMP_BUFFER);
  31840. if (ssk == NULL)
  31841. ret = WC_TEST_RET_ENC_NC;
  31842. else
  31843. XMEMSET(ssk, 0, sizeof(*ssk));
  31844. }
  31845. if (ret == 0) {
  31846. #ifndef HAVE_FIPS
  31847. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31848. #else
  31849. ret = wc_InitRng(&rng);
  31850. #endif
  31851. if (ret != 0)
  31852. ret = WC_TEST_RET_ENC_EC(ret);
  31853. else
  31854. rng_inited = 1;
  31855. }
  31856. if (ret == 0) {
  31857. pvt = wc_ecc_new_point();
  31858. if (pvt == NULL)
  31859. ret = WC_TEST_RET_ENC_NC;
  31860. }
  31861. if (ret == 0) {
  31862. ret = mp_init(ssk);
  31863. if (ret != 0)
  31864. ret = WC_TEST_RET_ENC_EC(ret);
  31865. }
  31866. if (ret == 0) {
  31867. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  31868. }
  31869. if (ret == 0) {
  31870. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  31871. if (ret != 0)
  31872. ret = WC_TEST_RET_ENC_EC(ret);
  31873. }
  31874. if (ret == 0) {
  31875. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  31876. if (ret != 0)
  31877. ret = WC_TEST_RET_ENC_EC(ret);
  31878. }
  31879. if (ret == 0) {
  31880. ret = eccsi_kat_verify_test(pub, pvt);
  31881. }
  31882. if (ret == 0) {
  31883. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  31884. }
  31885. if (ret == 0) {
  31886. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  31887. }
  31888. if (pvt != NULL)
  31889. wc_ecc_del_point(pvt);
  31890. if (rng_inited)
  31891. wc_FreeRng(&rng);
  31892. if (ssk != NULL) {
  31893. mp_free(ssk);
  31894. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31895. }
  31896. if (pub != NULL) {
  31897. wc_FreeEccsiKey(pub);
  31898. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31899. }
  31900. if (priv != NULL) {
  31901. wc_FreeEccsiKey(priv);
  31902. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31903. }
  31904. return ret;
  31905. }
  31906. #endif /* WOLFCRYPT_HAVE_ECCSI */
  31907. #ifdef WOLFCRYPT_HAVE_SAKKE
  31908. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  31909. {
  31910. wc_test_ret_t ret;
  31911. byte id[1] = { 0x00 };
  31912. int valid;
  31913. byte data[256];
  31914. word32 sz;
  31915. byte auth[257];
  31916. word16 authSz;
  31917. byte ssv[256];
  31918. word16 ssvSz;
  31919. word32 len;
  31920. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  31921. if (ret != BAD_FUNC_ARG)
  31922. return WC_TEST_RET_ENC_EC(ret);
  31923. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31924. if (ret != BAD_FUNC_ARG)
  31925. return WC_TEST_RET_ENC_EC(ret);
  31926. wc_FreeSakkeKey(NULL);
  31927. XMEMSET(key, 0, sizeof(*key));
  31928. wc_FreeSakkeKey(key);
  31929. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31930. if (ret != 0)
  31931. return WC_TEST_RET_ENC_EC(ret);
  31932. ret = wc_MakeSakkeKey(NULL, NULL);
  31933. if (ret != BAD_FUNC_ARG)
  31934. return WC_TEST_RET_ENC_EC(ret);
  31935. ret = wc_MakeSakkeKey(key, NULL);
  31936. if (ret != BAD_FUNC_ARG)
  31937. return WC_TEST_RET_ENC_EC(ret);
  31938. ret = wc_MakeSakkeKey(NULL, rng);
  31939. if (ret != BAD_FUNC_ARG)
  31940. return WC_TEST_RET_ENC_EC(ret);
  31941. ret = wc_MakeSakkePublicKey(NULL, NULL);
  31942. if (ret != BAD_FUNC_ARG)
  31943. return WC_TEST_RET_ENC_EC(ret);
  31944. ret = wc_MakeSakkePublicKey(key, NULL);
  31945. if (ret != BAD_FUNC_ARG)
  31946. return WC_TEST_RET_ENC_EC(ret);
  31947. ret = wc_MakeSakkePublicKey(NULL, rsk);
  31948. if (ret != BAD_FUNC_ARG)
  31949. return WC_TEST_RET_ENC_EC(ret);
  31950. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  31951. if (ret != BAD_FUNC_ARG)
  31952. return WC_TEST_RET_ENC_EC(ret);
  31953. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  31954. if (ret != BAD_FUNC_ARG)
  31955. return WC_TEST_RET_ENC_EC(ret);
  31956. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  31957. if (ret != BAD_FUNC_ARG)
  31958. return WC_TEST_RET_ENC_EC(ret);
  31959. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  31960. if (ret != BAD_FUNC_ARG)
  31961. return WC_TEST_RET_ENC_EC(ret);
  31962. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  31963. if (ret != BAD_FUNC_ARG)
  31964. return WC_TEST_RET_ENC_EC(ret);
  31965. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  31966. if (ret != BAD_FUNC_ARG)
  31967. return WC_TEST_RET_ENC_EC(ret);
  31968. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  31969. if (ret != BAD_FUNC_ARG)
  31970. return WC_TEST_RET_ENC_EC(ret);
  31971. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  31972. if (ret != BAD_FUNC_ARG)
  31973. return WC_TEST_RET_ENC_EC(ret);
  31974. ret = wc_ExportSakkeKey(key, data, NULL);
  31975. if (ret != BAD_FUNC_ARG)
  31976. return WC_TEST_RET_ENC_EC(ret);
  31977. ret = wc_ExportSakkeKey(NULL, data, &sz);
  31978. if (ret != BAD_FUNC_ARG)
  31979. return WC_TEST_RET_ENC_EC(ret);
  31980. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  31981. if (ret != BAD_FUNC_ARG)
  31982. return WC_TEST_RET_ENC_EC(ret);
  31983. ret = wc_ImportSakkeKey(key, NULL, 1);
  31984. if (ret != BAD_FUNC_ARG)
  31985. return WC_TEST_RET_ENC_EC(ret);
  31986. ret = wc_ImportSakkeKey(NULL, data, 1);
  31987. if (ret != BAD_FUNC_ARG)
  31988. return WC_TEST_RET_ENC_EC(ret);
  31989. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  31990. if (ret != BAD_FUNC_ARG)
  31991. return WC_TEST_RET_ENC_EC(ret);
  31992. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  31993. if (ret != BAD_FUNC_ARG)
  31994. return WC_TEST_RET_ENC_EC(ret);
  31995. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  31996. if (ret != BAD_FUNC_ARG)
  31997. return WC_TEST_RET_ENC_EC(ret);
  31998. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  31999. if (ret != BAD_FUNC_ARG)
  32000. return WC_TEST_RET_ENC_EC(ret);
  32001. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  32002. if (ret != BAD_FUNC_ARG)
  32003. return WC_TEST_RET_ENC_EC(ret);
  32004. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  32005. if (ret != BAD_FUNC_ARG)
  32006. return WC_TEST_RET_ENC_EC(ret);
  32007. sz = sizeof(data);
  32008. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  32009. if (ret != BAD_FUNC_ARG)
  32010. return WC_TEST_RET_ENC_EC(ret);
  32011. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  32012. if (ret != BAD_FUNC_ARG)
  32013. return WC_TEST_RET_ENC_EC(ret);
  32014. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  32015. if (ret != BAD_FUNC_ARG)
  32016. return WC_TEST_RET_ENC_EC(ret);
  32017. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  32018. if (ret != BAD_FUNC_ARG)
  32019. return WC_TEST_RET_ENC_EC(ret);
  32020. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  32021. if (ret != BAD_FUNC_ARG)
  32022. return WC_TEST_RET_ENC_EC(ret);
  32023. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  32024. if (ret != BAD_FUNC_ARG)
  32025. return WC_TEST_RET_ENC_EC(ret);
  32026. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  32027. if (ret != BAD_FUNC_ARG)
  32028. return WC_TEST_RET_ENC_EC(ret);
  32029. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  32030. if (ret != BAD_FUNC_ARG)
  32031. return WC_TEST_RET_ENC_EC(ret);
  32032. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  32033. if (ret != BAD_FUNC_ARG)
  32034. return WC_TEST_RET_ENC_EC(ret);
  32035. ret = wc_ImportSakkeRsk(key, NULL, sz);
  32036. if (ret != BAD_FUNC_ARG)
  32037. return WC_TEST_RET_ENC_EC(ret);
  32038. ret = wc_ImportSakkeRsk(NULL, data, sz);
  32039. if (ret != BAD_FUNC_ARG)
  32040. return WC_TEST_RET_ENC_EC(ret);
  32041. ret = wc_ImportSakkeRsk(key, data, 1);
  32042. if (ret != BUFFER_E)
  32043. return WC_TEST_RET_ENC_EC(ret);
  32044. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  32045. if (ret != BAD_FUNC_ARG)
  32046. return WC_TEST_RET_ENC_EC(ret);
  32047. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  32048. if (ret != BAD_FUNC_ARG)
  32049. return WC_TEST_RET_ENC_EC(ret);
  32050. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  32051. if (ret != BAD_FUNC_ARG)
  32052. return WC_TEST_RET_ENC_EC(ret);
  32053. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  32054. if (ret != BAD_FUNC_ARG)
  32055. return WC_TEST_RET_ENC_EC(ret);
  32056. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  32057. if (ret != BAD_FUNC_ARG)
  32058. return WC_TEST_RET_ENC_EC(ret);
  32059. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  32060. if (ret != BAD_FUNC_ARG)
  32061. return WC_TEST_RET_ENC_EC(ret);
  32062. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  32063. if (ret != BAD_FUNC_ARG)
  32064. return WC_TEST_RET_ENC_EC(ret);
  32065. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32066. if (ret != LENGTH_ONLY_E)
  32067. return WC_TEST_RET_ENC_EC(ret);
  32068. len--;
  32069. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  32070. if (ret != BUFFER_E)
  32071. return WC_TEST_RET_ENC_EC(ret);
  32072. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  32073. if (ret != BAD_FUNC_ARG)
  32074. return WC_TEST_RET_ENC_EC(ret);
  32075. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  32076. if (ret != BAD_FUNC_ARG)
  32077. return WC_TEST_RET_ENC_EC(ret);
  32078. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  32079. if (ret != BAD_FUNC_ARG)
  32080. return WC_TEST_RET_ENC_EC(ret);
  32081. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  32082. if (ret != BAD_FUNC_ARG)
  32083. return WC_TEST_RET_ENC_EC(ret);
  32084. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  32085. if (ret != BAD_FUNC_ARG)
  32086. return WC_TEST_RET_ENC_EC(ret);
  32087. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  32088. if (ret != BAD_FUNC_ARG)
  32089. return WC_TEST_RET_ENC_EC(ret);
  32090. ret = wc_GetSakkeAuthSize(NULL, NULL);
  32091. if (ret != BAD_FUNC_ARG)
  32092. return WC_TEST_RET_ENC_EC(ret);
  32093. ret = wc_GetSakkeAuthSize(key, NULL);
  32094. if (ret != BAD_FUNC_ARG)
  32095. return WC_TEST_RET_ENC_EC(ret);
  32096. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  32097. if (ret != BAD_FUNC_ARG)
  32098. return WC_TEST_RET_ENC_EC(ret);
  32099. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  32100. if (ret != BAD_FUNC_ARG)
  32101. return WC_TEST_RET_ENC_EC(ret);
  32102. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  32103. if (ret != BAD_FUNC_ARG)
  32104. return WC_TEST_RET_ENC_EC(ret);
  32105. ret = wc_MakeSakkePointI(NULL, id, 1);
  32106. if (ret != BAD_FUNC_ARG)
  32107. return WC_TEST_RET_ENC_EC(ret);
  32108. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  32109. if (ret != BAD_FUNC_ARG)
  32110. return WC_TEST_RET_ENC_EC(ret);
  32111. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  32112. if (ret != BAD_FUNC_ARG)
  32113. return WC_TEST_RET_ENC_EC(ret);
  32114. ret = wc_MakeSakkePointI(key, NULL, 1);
  32115. if (ret != BAD_FUNC_ARG)
  32116. return WC_TEST_RET_ENC_EC(ret);
  32117. ret = wc_MakeSakkePointI(NULL, id, 1);
  32118. if (ret != BAD_FUNC_ARG)
  32119. return WC_TEST_RET_ENC_EC(ret);
  32120. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  32121. if (ret != BAD_FUNC_ARG)
  32122. return WC_TEST_RET_ENC_EC(ret);
  32123. ret = wc_GenerateSakkePointITable(key, data, NULL);
  32124. if (ret != BAD_FUNC_ARG)
  32125. return WC_TEST_RET_ENC_EC(ret);
  32126. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  32127. if (ret != BAD_FUNC_ARG)
  32128. return WC_TEST_RET_ENC_EC(ret);
  32129. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  32130. if (ret != LENGTH_ONLY_E)
  32131. return WC_TEST_RET_ENC_EC(ret);
  32132. len--;
  32133. ret = wc_GenerateSakkePointITable(key, data, &len);
  32134. if (ret != BUFFER_E)
  32135. return WC_TEST_RET_ENC_EC(ret);
  32136. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  32137. if (ret != BAD_FUNC_ARG)
  32138. return WC_TEST_RET_ENC_EC(ret);
  32139. ret = wc_SetSakkePointITable(key, NULL, 1);
  32140. if (ret != BAD_FUNC_ARG)
  32141. return WC_TEST_RET_ENC_EC(ret);
  32142. ret = wc_SetSakkePointITable(NULL, data, 1);
  32143. if (ret != BAD_FUNC_ARG)
  32144. return WC_TEST_RET_ENC_EC(ret);
  32145. ret = wc_SetSakkePointITable(key, data, 1);
  32146. if (ret != BUFFER_E)
  32147. return WC_TEST_RET_ENC_EC(ret);
  32148. ret = wc_ClearSakkePointITable(NULL);
  32149. if (ret != BAD_FUNC_ARG)
  32150. return WC_TEST_RET_ENC_EC(ret);
  32151. ret = wc_GetSakkePointI(NULL, data, NULL);
  32152. if (ret != BAD_FUNC_ARG)
  32153. return WC_TEST_RET_ENC_EC(ret);
  32154. ret = wc_GetSakkePointI(key, data, NULL);
  32155. if (ret != BAD_FUNC_ARG)
  32156. return WC_TEST_RET_ENC_EC(ret);
  32157. ret = wc_GetSakkePointI(NULL, data, &sz);
  32158. if (ret != BAD_FUNC_ARG)
  32159. return WC_TEST_RET_ENC_EC(ret);
  32160. sz = 1;
  32161. ret = wc_GetSakkePointI(key, data, &sz);
  32162. if (ret != BUFFER_E)
  32163. return WC_TEST_RET_ENC_EC(ret);
  32164. sz = 256;
  32165. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  32166. if (ret != BAD_FUNC_ARG)
  32167. return WC_TEST_RET_ENC_EC(ret);
  32168. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  32169. if (ret != BAD_FUNC_ARG)
  32170. return WC_TEST_RET_ENC_EC(ret);
  32171. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  32172. if (ret != BAD_FUNC_ARG)
  32173. return WC_TEST_RET_ENC_EC(ret);
  32174. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  32175. if (ret != BAD_FUNC_ARG)
  32176. return WC_TEST_RET_ENC_EC(ret);
  32177. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  32178. if (ret != BAD_FUNC_ARG)
  32179. return WC_TEST_RET_ENC_EC(ret);
  32180. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  32181. if (ret != BAD_FUNC_ARG)
  32182. return WC_TEST_RET_ENC_EC(ret);
  32183. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  32184. if (ret != BAD_FUNC_ARG)
  32185. return WC_TEST_RET_ENC_EC(ret);
  32186. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  32187. if (ret != BUFFER_E)
  32188. return WC_TEST_RET_ENC_EC(ret);
  32189. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  32190. if (ret != BUFFER_E)
  32191. return WC_TEST_RET_ENC_EC(ret);
  32192. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  32193. if (ret != BAD_FUNC_ARG)
  32194. return WC_TEST_RET_ENC_EC(ret);
  32195. ret = wc_SetSakkeIdentity(key, NULL, 1);
  32196. if (ret != BAD_FUNC_ARG)
  32197. return WC_TEST_RET_ENC_EC(ret);
  32198. ret = wc_SetSakkeIdentity(NULL, id, 1);
  32199. if (ret != BAD_FUNC_ARG)
  32200. return WC_TEST_RET_ENC_EC(ret);
  32201. ssvSz = sizeof(ssv);
  32202. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32203. auth, NULL);
  32204. if (ret != BAD_FUNC_ARG)
  32205. return WC_TEST_RET_ENC_EC(ret);
  32206. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32207. auth, NULL);
  32208. if (ret != BAD_FUNC_ARG)
  32209. return WC_TEST_RET_ENC_EC(ret);
  32210. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32211. auth, NULL);
  32212. if (ret != BAD_FUNC_ARG)
  32213. return WC_TEST_RET_ENC_EC(ret);
  32214. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32215. auth, &authSz);
  32216. if (ret != BAD_FUNC_ARG)
  32217. return WC_TEST_RET_ENC_EC(ret);
  32218. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32219. auth, NULL);
  32220. if (ret != BAD_FUNC_ARG)
  32221. return WC_TEST_RET_ENC_EC(ret);
  32222. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32223. auth, &authSz);
  32224. if (ret != BAD_FUNC_ARG)
  32225. return WC_TEST_RET_ENC_EC(ret);
  32226. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32227. auth, &authSz);
  32228. if (ret != BAD_FUNC_ARG)
  32229. return WC_TEST_RET_ENC_EC(ret);
  32230. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32231. auth, &authSz);
  32232. if (ret != BAD_STATE_E)
  32233. return WC_TEST_RET_ENC_EC(ret);
  32234. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  32235. if (ret != BAD_FUNC_ARG)
  32236. return WC_TEST_RET_ENC_EC(ret);
  32237. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  32238. if (ret != BAD_FUNC_ARG)
  32239. return WC_TEST_RET_ENC_EC(ret);
  32240. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  32241. if (ret != BAD_FUNC_ARG)
  32242. return WC_TEST_RET_ENC_EC(ret);
  32243. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  32244. if (ret != BAD_FUNC_ARG)
  32245. return WC_TEST_RET_ENC_EC(ret);
  32246. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  32247. if (ret != BAD_FUNC_ARG)
  32248. return WC_TEST_RET_ENC_EC(ret);
  32249. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  32250. if (ret != BAD_FUNC_ARG)
  32251. return WC_TEST_RET_ENC_EC(ret);
  32252. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  32253. if (ret != BAD_FUNC_ARG)
  32254. return WC_TEST_RET_ENC_EC(ret);
  32255. ssvSz = sizeof(ssv);
  32256. authSz = sizeof(auth);
  32257. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  32258. authSz);
  32259. if (ret != BAD_FUNC_ARG)
  32260. return WC_TEST_RET_ENC_EC(ret);
  32261. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  32262. authSz);
  32263. if (ret != BAD_FUNC_ARG)
  32264. return WC_TEST_RET_ENC_EC(ret);
  32265. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  32266. authSz);
  32267. if (ret != BAD_FUNC_ARG)
  32268. return WC_TEST_RET_ENC_EC(ret);
  32269. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  32270. authSz);
  32271. if (ret != BAD_FUNC_ARG)
  32272. return WC_TEST_RET_ENC_EC(ret);
  32273. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  32274. authSz);
  32275. if (ret != BAD_FUNC_ARG)
  32276. return WC_TEST_RET_ENC_EC(ret);
  32277. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  32278. authSz);
  32279. if (ret != BAD_FUNC_ARG)
  32280. return WC_TEST_RET_ENC_EC(ret);
  32281. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32282. authSz);
  32283. if (ret != BAD_FUNC_ARG)
  32284. return WC_TEST_RET_ENC_EC(ret);
  32285. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32286. authSz);
  32287. if (ret != BAD_STATE_E)
  32288. return WC_TEST_RET_ENC_EC(ret);
  32289. ret = wc_SetSakkeIdentity(key, id, 1);
  32290. if (ret != 0)
  32291. return WC_TEST_RET_ENC_EC(ret);
  32292. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32293. authSz);
  32294. if (ret != BAD_STATE_E)
  32295. return WC_TEST_RET_ENC_EC(ret);
  32296. ret = wc_SetSakkeIdentity(key, id, 0);
  32297. if (ret != 0)
  32298. return WC_TEST_RET_ENC_EC(ret);
  32299. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  32300. if (ret != 0)
  32301. return WC_TEST_RET_ENC_EC(ret);
  32302. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32303. authSz);
  32304. if (ret != BAD_STATE_E)
  32305. return WC_TEST_RET_ENC_EC(ret);
  32306. wc_FreeSakkeKey(key);
  32307. return 0;
  32308. }
  32309. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  32310. {
  32311. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  32312. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32313. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32314. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32315. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32316. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32317. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32318. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32319. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32320. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32321. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32322. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32323. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32324. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32325. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32326. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32327. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32328. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32329. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32330. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32331. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32332. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32333. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32334. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32335. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32336. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32337. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32338. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32339. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32340. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32341. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32342. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32343. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32344. };
  32345. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  32346. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  32347. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  32348. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  32349. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  32350. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  32351. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  32352. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  32353. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  32354. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  32355. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  32356. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  32357. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  32358. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  32359. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  32360. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  32361. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  32362. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  32363. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  32364. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  32365. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  32366. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  32367. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  32368. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  32369. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  32370. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  32371. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  32372. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  32373. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  32374. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  32375. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  32376. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  32377. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  32378. };
  32379. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  32380. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32381. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32382. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32383. 0x33, 0x00
  32384. };
  32385. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  32386. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32387. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32388. };
  32389. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  32390. 0x04,
  32391. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32392. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32393. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32394. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32395. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32396. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32397. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32398. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32399. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32400. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32401. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32402. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32403. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32404. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32405. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32406. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32407. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32408. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32409. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32410. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32411. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32412. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32413. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32414. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32415. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32416. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32417. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32418. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32419. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32420. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32421. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32422. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32423. };
  32424. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  32425. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32426. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32427. };
  32428. wc_test_ret_t ret;
  32429. int valid;
  32430. byte pubKey[sizeof(pubData) + 1];
  32431. word32 sz = sizeof(pubKey);
  32432. byte tmpSsv[sizeof(encSsv)];
  32433. byte* iTable = NULL;
  32434. word32 iTableLen;
  32435. byte* table = NULL;
  32436. word32 len;
  32437. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32438. if (ret != 0)
  32439. return WC_TEST_RET_ENC_EC(ret);
  32440. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  32441. if (ret != 0)
  32442. return WC_TEST_RET_ENC_EC(ret);
  32443. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  32444. if (ret != 0)
  32445. return WC_TEST_RET_ENC_EC(ret);
  32446. if (valid != 1)
  32447. return WC_TEST_RET_ENC_NC;
  32448. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  32449. if (ret != 0)
  32450. return WC_TEST_RET_ENC_EC(ret);
  32451. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  32452. if (ret != 0)
  32453. return WC_TEST_RET_ENC_EC(ret);
  32454. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32455. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32456. auth, sizeof(auth));
  32457. if (ret != 0)
  32458. return WC_TEST_RET_ENC_EC(ret);
  32459. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32460. return WC_TEST_RET_ENC_NC;
  32461. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  32462. if (ret != 0)
  32463. return WC_TEST_RET_ENC_EC(ret);
  32464. iTableLen = 0;
  32465. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  32466. if (ret != LENGTH_ONLY_E)
  32467. return WC_TEST_RET_ENC_EC(ret);
  32468. if (iTableLen != 0) {
  32469. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32470. if (iTable == NULL)
  32471. return WC_TEST_RET_ENC_ERRNO;
  32472. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  32473. if (ret != 0)
  32474. return WC_TEST_RET_ENC_EC(ret);
  32475. }
  32476. len = 0;
  32477. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32478. if (ret != LENGTH_ONLY_E)
  32479. return WC_TEST_RET_ENC_EC(ret);
  32480. if (len > 0) {
  32481. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32482. if (table == NULL)
  32483. return WC_TEST_RET_ENC_ERRNO;
  32484. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  32485. if (ret != 0)
  32486. return WC_TEST_RET_ENC_EC(ret);
  32487. }
  32488. ret = wc_SetSakkeRsk(key, rsk, table, len);
  32489. if (ret != 0)
  32490. return WC_TEST_RET_ENC_EC(ret);
  32491. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32492. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32493. auth, sizeof(auth));
  32494. if (ret != 0)
  32495. return WC_TEST_RET_ENC_EC(ret);
  32496. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32497. return WC_TEST_RET_ENC_NC;
  32498. /* Don't reference table that is about to be freed. */
  32499. ret = wc_ClearSakkePointITable(key);
  32500. if (ret != 0)
  32501. return WC_TEST_RET_ENC_EC(ret);
  32502. /* Dispose of tables */
  32503. if (iTable != NULL)
  32504. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32505. if (table != NULL)
  32506. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32507. /* Make sure the key public key is exportable - convert to Montgomery form
  32508. * in Validation.
  32509. */
  32510. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  32511. if (ret != 0)
  32512. return WC_TEST_RET_ENC_EC(ret);
  32513. if (sz != sizeof(pubData))
  32514. return WC_TEST_RET_ENC_NC;
  32515. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  32516. return WC_TEST_RET_ENC_NC;
  32517. sz = sizeof(pubData) + 1;
  32518. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  32519. if (ret != 0)
  32520. return WC_TEST_RET_ENC_EC(ret);
  32521. if (sz != sizeof(pubData) + 1)
  32522. return WC_TEST_RET_ENC_NC;
  32523. if (pubKey[0] != 0x04)
  32524. return WC_TEST_RET_ENC_NC;
  32525. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  32526. return WC_TEST_RET_ENC_NC;
  32527. return 0;
  32528. }
  32529. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  32530. {
  32531. static const byte pubData[] = {
  32532. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32533. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32534. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32535. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32536. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32537. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32538. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32539. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32540. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32541. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32542. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32543. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32544. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32545. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32546. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32547. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32548. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32549. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32550. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32551. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32552. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32553. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32554. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32555. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32556. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32557. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32558. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32559. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32560. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32561. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32562. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32563. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32564. };
  32565. static const byte id[] = {
  32566. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32567. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32568. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32569. 0x33, 0x00
  32570. };
  32571. static word32 idSz = sizeof(id);
  32572. byte ssv[] = {
  32573. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32574. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32575. };
  32576. static word16 ssvSz = sizeof(ssv);
  32577. static const byte expAuth[] = {
  32578. 0x04,
  32579. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32580. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32581. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32582. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32583. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32584. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32585. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32586. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32587. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32588. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32589. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32590. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32591. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32592. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32593. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32594. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32595. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32596. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32597. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32598. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32599. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32600. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32601. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32602. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32603. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32604. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32605. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32606. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32607. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32608. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32609. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32610. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32611. };
  32612. static const byte encSsv[] = {
  32613. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32614. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32615. };
  32616. wc_test_ret_t ret;
  32617. byte auth[257];
  32618. word16 authSz = sizeof(auth);
  32619. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32620. if (ret != 0)
  32621. return WC_TEST_RET_ENC_EC(ret);
  32622. ret = wc_SetSakkeIdentity(key, id, idSz);
  32623. if (ret != 0)
  32624. return WC_TEST_RET_ENC_EC(ret);
  32625. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32626. auth, &authSz);
  32627. if (ret != 0)
  32628. return WC_TEST_RET_ENC_EC(ret);
  32629. if (authSz != 257)
  32630. return WC_TEST_RET_ENC_NC;
  32631. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  32632. return WC_TEST_RET_ENC_NC;
  32633. if (XMEMCMP(auth, expAuth, authSz) != 0)
  32634. return WC_TEST_RET_ENC_NC;
  32635. return 0;
  32636. }
  32637. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  32638. WC_RNG* rng, ecc_point* rsk)
  32639. {
  32640. wc_test_ret_t ret;
  32641. byte data[440];
  32642. byte pubData[257];
  32643. word32 sz;
  32644. char mail[] = "test@wolfssl.com";
  32645. byte* id = (byte*)mail;
  32646. word32 idSz = (word32)XSTRLEN(mail);
  32647. int valid;
  32648. ecc_point* pubKey = rsk;
  32649. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32650. if (ret != 0)
  32651. return WC_TEST_RET_ENC_EC(ret);
  32652. ret = wc_MakeSakkeKey(priv, rng);
  32653. if (ret != 0)
  32654. return WC_TEST_RET_ENC_EC(ret);
  32655. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  32656. if (ret != LENGTH_ONLY_E)
  32657. return WC_TEST_RET_ENC_EC(ret);
  32658. if (sz != 384)
  32659. return WC_TEST_RET_ENC_NC;
  32660. sz--;
  32661. ret = wc_ExportSakkeKey(priv, data, &sz);
  32662. if (ret == 0)
  32663. return WC_TEST_RET_ENC_NC;
  32664. sz++;
  32665. ret = wc_ExportSakkeKey(priv, data, &sz);
  32666. if (ret != 0)
  32667. return WC_TEST_RET_ENC_EC(ret);
  32668. if (sz != 384)
  32669. return WC_TEST_RET_ENC_NC;
  32670. ret = wc_ImportSakkeKey(key, data, sz - 1);
  32671. if (ret == 0)
  32672. return WC_TEST_RET_ENC_NC;
  32673. ret = wc_ImportSakkeKey(key, data, sz);
  32674. if (ret != 0)
  32675. return WC_TEST_RET_ENC_EC(ret);
  32676. wc_FreeSakkeKey(key);
  32677. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32678. if (ret != 0)
  32679. return WC_TEST_RET_ENC_EC(ret);
  32680. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  32681. if (ret != LENGTH_ONLY_E)
  32682. return WC_TEST_RET_ENC_EC(ret);
  32683. if (sz != 128)
  32684. return WC_TEST_RET_ENC_NC;
  32685. sz--;
  32686. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32687. if (ret == 0)
  32688. return WC_TEST_RET_ENC_NC;
  32689. sz++;
  32690. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32691. if (ret != 0)
  32692. return WC_TEST_RET_ENC_EC(ret);
  32693. if (sz != 128)
  32694. return WC_TEST_RET_ENC_NC;
  32695. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  32696. if (ret == 0)
  32697. return WC_TEST_RET_ENC_NC;
  32698. ret = wc_ImportSakkePrivateKey(key, data, sz);
  32699. if (ret != 0)
  32700. return WC_TEST_RET_ENC_EC(ret);
  32701. ret = wc_MakeSakkePublicKey(key, pubKey);
  32702. if (ret != 0)
  32703. return WC_TEST_RET_ENC_EC(ret);
  32704. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  32705. if (ret != LENGTH_ONLY_E)
  32706. return WC_TEST_RET_ENC_EC(ret);
  32707. if (sz != 256)
  32708. return WC_TEST_RET_ENC_NC;
  32709. sz--;
  32710. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32711. if (ret == 0)
  32712. return WC_TEST_RET_ENC_NC;
  32713. sz++;
  32714. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32715. if (ret != 0)
  32716. return WC_TEST_RET_ENC_EC(ret);
  32717. if (sz != 256)
  32718. return WC_TEST_RET_ENC_NC;
  32719. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  32720. if (ret == 0)
  32721. return WC_TEST_RET_ENC_NC;
  32722. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  32723. if (ret != 0)
  32724. return WC_TEST_RET_ENC_EC(ret);
  32725. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  32726. if (ret != 0)
  32727. return WC_TEST_RET_ENC_EC(ret);
  32728. if (sz != 256)
  32729. return WC_TEST_RET_ENC_NC;
  32730. if (XMEMCMP(data, pubData, sz) != 0)
  32731. return WC_TEST_RET_ENC_NC;
  32732. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  32733. if (ret != 0)
  32734. return WC_TEST_RET_ENC_EC(ret);
  32735. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  32736. if (ret != 0)
  32737. return WC_TEST_RET_ENC_EC(ret);
  32738. if (valid != 1)
  32739. return WC_TEST_RET_ENC_NC;
  32740. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  32741. if (ret != 0)
  32742. return WC_TEST_RET_ENC_EC(ret);
  32743. if (valid != 1)
  32744. return WC_TEST_RET_ENC_NC;
  32745. sz = sizeof(data);
  32746. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  32747. if (ret != 0)
  32748. return WC_TEST_RET_ENC_EC(ret);
  32749. if (sz != 256)
  32750. return WC_TEST_RET_ENC_NC;
  32751. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  32752. if (ret != 0)
  32753. return WC_TEST_RET_ENC_EC(ret);
  32754. sz = sizeof(pubData);
  32755. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  32756. if (ret != 0)
  32757. return WC_TEST_RET_ENC_EC(ret);
  32758. if (sz != sizeof(pubData))
  32759. return WC_TEST_RET_ENC_NC;
  32760. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  32761. if (ret != 0)
  32762. return WC_TEST_RET_ENC_EC(ret);
  32763. wc_FreeSakkeKey(key);
  32764. return 0;
  32765. }
  32766. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  32767. ecc_point* rsk)
  32768. {
  32769. wc_test_ret_t ret;
  32770. byte ssv[16];
  32771. word16 ssvSz;
  32772. byte auth[257];
  32773. word16 authSz;
  32774. char mail[] = "test@wolfssl.com";
  32775. byte* id = (byte*)mail;
  32776. word32 idSz = (word32)XSTRLEN(mail);
  32777. byte pointI[256];
  32778. word32 sz;
  32779. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  32780. if (ret != LENGTH_ONLY_E)
  32781. return WC_TEST_RET_ENC_EC(ret);
  32782. if (ssvSz != 16)
  32783. return WC_TEST_RET_ENC_NC;
  32784. ssvSz += 128;
  32785. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32786. if (ret == 0)
  32787. return WC_TEST_RET_ENC_NC;
  32788. ssvSz -= 128;
  32789. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32790. if (ret != 0)
  32791. return WC_TEST_RET_ENC_EC(ret);
  32792. if (ssvSz != 16)
  32793. return WC_TEST_RET_ENC_NC;
  32794. ret = wc_GetSakkeAuthSize(pub, &authSz);
  32795. if (ret != 0)
  32796. return WC_TEST_RET_ENC_EC(ret);
  32797. ret = wc_SetSakkeIdentity(pub, id, idSz);
  32798. if (ret != 0)
  32799. return WC_TEST_RET_ENC_EC(ret);
  32800. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32801. NULL, &authSz);
  32802. if (ret != LENGTH_ONLY_E)
  32803. return WC_TEST_RET_ENC_EC(ret);
  32804. if (authSz != 257)
  32805. return WC_TEST_RET_ENC_NC;
  32806. authSz--;
  32807. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32808. auth, &authSz);
  32809. if (ret == 0)
  32810. return WC_TEST_RET_ENC_NC;
  32811. authSz++;
  32812. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32813. auth, &authSz);
  32814. if (ret != 0)
  32815. return WC_TEST_RET_ENC_EC(ret);
  32816. if (authSz != 257)
  32817. return WC_TEST_RET_ENC_NC;
  32818. ret = wc_GetSakkePointI(pub, NULL, &sz);
  32819. if (ret != LENGTH_ONLY_E)
  32820. return WC_TEST_RET_ENC_EC(ret);
  32821. if (sz != 256)
  32822. return WC_TEST_RET_ENC_NC;
  32823. ret = wc_GetSakkePointI(pub, pointI, &sz);
  32824. if (ret != 0)
  32825. return WC_TEST_RET_ENC_EC(ret);
  32826. if (sz != 256)
  32827. return WC_TEST_RET_ENC_NC;
  32828. /* Bogus identity - make it check and regenerate I. */
  32829. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  32830. if (ret != 0)
  32831. return WC_TEST_RET_ENC_EC(ret);
  32832. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32833. auth, &authSz);
  32834. if (ret != 0)
  32835. return WC_TEST_RET_ENC_EC(ret);
  32836. if (authSz != 257)
  32837. return WC_TEST_RET_ENC_NC;
  32838. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  32839. if (ret != 0)
  32840. return WC_TEST_RET_ENC_EC(ret);
  32841. ret = wc_SetSakkeIdentity(priv, id, idSz);
  32842. if (ret != 0)
  32843. return WC_TEST_RET_ENC_EC(ret);
  32844. authSz--;
  32845. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32846. authSz);
  32847. if (ret == 0)
  32848. return WC_TEST_RET_ENC_NC;
  32849. authSz++;
  32850. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32851. authSz);
  32852. if (ret != 0)
  32853. return WC_TEST_RET_ENC_EC(ret);
  32854. ssv[0] ^= 0x80;
  32855. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32856. authSz);
  32857. if (ret != SAKKE_VERIFY_FAIL_E)
  32858. return WC_TEST_RET_ENC_EC(ret);
  32859. ssv[0] ^= 0x80;
  32860. /* Bogus identity - make it check and regenerate I. */
  32861. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  32862. if (ret != 0)
  32863. return WC_TEST_RET_ENC_EC(ret);
  32864. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32865. authSz);
  32866. if (ret != 0)
  32867. return WC_TEST_RET_ENC_EC(ret);
  32868. return 0;
  32869. }
  32870. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  32871. {
  32872. wc_test_ret_t ret = 0;
  32873. WC_RNG rng;
  32874. int rng_inited = 0;
  32875. SakkeKey* priv = NULL;
  32876. SakkeKey* pub = NULL;
  32877. SakkeKey* key = NULL;
  32878. ecc_point* rsk = NULL;
  32879. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32880. DYNAMIC_TYPE_TMP_BUFFER);
  32881. if (priv == NULL)
  32882. ret = WC_TEST_RET_ENC_NC;
  32883. else
  32884. XMEMSET(priv, 0, sizeof(*priv));
  32885. if (ret == 0) {
  32886. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32887. DYNAMIC_TYPE_TMP_BUFFER);
  32888. if (pub == NULL)
  32889. ret = WC_TEST_RET_ENC_NC;
  32890. else
  32891. XMEMSET(pub, 0, sizeof(*pub));
  32892. }
  32893. if (ret == 0) {
  32894. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32895. DYNAMIC_TYPE_TMP_BUFFER);
  32896. if (key == NULL)
  32897. ret = WC_TEST_RET_ENC_NC;
  32898. else
  32899. XMEMSET(key, 0, sizeof(*key));
  32900. }
  32901. if (ret == 0) {
  32902. #ifndef HAVE_FIPS
  32903. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32904. #else
  32905. ret = wc_InitRng(&rng);
  32906. #endif
  32907. if (ret == 0)
  32908. rng_inited = 1;
  32909. else
  32910. ret = WC_TEST_RET_ENC_EC(ret);
  32911. }
  32912. if (ret == 0) {
  32913. rsk = wc_ecc_new_point();
  32914. if (rsk == NULL)
  32915. ret = WC_TEST_RET_ENC_NC;
  32916. }
  32917. if (ret == 0) {
  32918. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  32919. if (ret != 0)
  32920. ret = WC_TEST_RET_ENC_EC(ret);
  32921. }
  32922. if (ret == 0) {
  32923. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  32924. if (ret != 0)
  32925. ret = WC_TEST_RET_ENC_EC(ret);
  32926. }
  32927. if (ret == 0) {
  32928. ret = sakke_api_test(&rng, key, rsk);
  32929. }
  32930. if (ret == 0) {
  32931. ret = sakke_kat_derive_test(pub, rsk);
  32932. }
  32933. if (ret == 0) {
  32934. ret = sakke_kat_encapsulate_test(pub);
  32935. }
  32936. if (ret == 0) {
  32937. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  32938. }
  32939. if (ret == 0) {
  32940. ret = sakke_op_test(priv, pub, &rng, rsk);
  32941. }
  32942. if (rsk != NULL) {
  32943. wc_ecc_forcezero_point(rsk);
  32944. wc_ecc_del_point(rsk);
  32945. }
  32946. if (rng_inited)
  32947. wc_FreeRng(&rng);
  32948. if (key != NULL)
  32949. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32950. if (pub != NULL) {
  32951. wc_FreeSakkeKey(pub);
  32952. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32953. }
  32954. if (priv != NULL) {
  32955. wc_FreeSakkeKey(priv);
  32956. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32957. }
  32958. return ret;
  32959. }
  32960. #endif /* WOLFCRYPT_HAVE_SAKKE */
  32961. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  32962. typedef struct CMAC_Test_Case {
  32963. int type;
  32964. int partial;
  32965. const byte* m;
  32966. word32 mSz;
  32967. const byte* k;
  32968. word32 kSz;
  32969. const byte* t;
  32970. word32 tSz;
  32971. } CMAC_Test_Case;
  32972. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  32973. {
  32974. #ifdef WOLFSSL_AES_128
  32975. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  32976. {
  32977. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  32978. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  32979. };
  32980. #define KLEN_128 (sizeof(k128))
  32981. #endif
  32982. #ifdef WOLFSSL_AES_192
  32983. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  32984. {
  32985. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  32986. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  32987. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  32988. };
  32989. #define KLEN_192 (sizeof(k192))
  32990. #endif
  32991. #ifdef WOLFSSL_AES_256
  32992. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  32993. {
  32994. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  32995. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  32996. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  32997. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  32998. };
  32999. #define KLEN_256 (sizeof(k256))
  33000. #endif
  33001. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  33002. {
  33003. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  33004. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  33005. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  33006. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  33007. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  33008. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  33009. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  33010. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  33011. };
  33012. #define MLEN_0 (0)
  33013. #define MLEN_128 (128/8)
  33014. #define MLEN_320 (320/8)
  33015. #define MLEN_319 (MLEN_320 - 1)
  33016. #define MLEN_512 (512/8)
  33017. #ifdef WOLFSSL_AES_128
  33018. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  33019. {
  33020. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  33021. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  33022. };
  33023. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  33024. {
  33025. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  33026. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  33027. };
  33028. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  33029. {
  33030. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  33031. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  33032. };
  33033. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  33034. {
  33035. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  33036. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  33037. };
  33038. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  33039. {
  33040. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  33041. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  33042. };
  33043. #endif
  33044. #ifdef WOLFSSL_AES_192
  33045. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  33046. {
  33047. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  33048. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  33049. };
  33050. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  33051. {
  33052. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  33053. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  33054. };
  33055. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  33056. {
  33057. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  33058. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  33059. };
  33060. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  33061. {
  33062. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  33063. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  33064. };
  33065. #endif
  33066. #ifdef WOLFSSL_AES_256
  33067. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  33068. {
  33069. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  33070. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  33071. };
  33072. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  33073. {
  33074. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  33075. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  33076. };
  33077. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  33078. {
  33079. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  33080. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  33081. };
  33082. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  33083. {
  33084. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  33085. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  33086. };
  33087. #endif
  33088. const CMAC_Test_Case testCases[] =
  33089. {
  33090. #ifdef WOLFSSL_AES_128
  33091. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  33092. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  33093. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  33094. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  33095. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  33096. #endif
  33097. #ifdef WOLFSSL_AES_192
  33098. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  33099. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  33100. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  33101. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  33102. #endif
  33103. #ifdef WOLFSSL_AES_256
  33104. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  33105. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  33106. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  33107. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  33108. #endif
  33109. #ifdef WOLFSSL_AES_128
  33110. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  33111. #endif
  33112. };
  33113. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33114. Cmac *cmac;
  33115. #else
  33116. Cmac cmac[1];
  33117. #endif
  33118. byte tag[AES_BLOCK_SIZE];
  33119. const CMAC_Test_Case* tc;
  33120. word32 i, tagSz;
  33121. wc_test_ret_t ret;
  33122. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33123. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  33124. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  33125. #endif
  33126. for (i = 0, tc = testCases;
  33127. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  33128. i++, tc++) {
  33129. XMEMSET(tag, 0, sizeof(tag));
  33130. tagSz = AES_BLOCK_SIZE;
  33131. #if !defined(HAVE_FIPS) || \
  33132. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  33133. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  33134. #else
  33135. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  33136. #endif
  33137. if (ret != 0)
  33138. {
  33139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33140. }
  33141. if (tc->partial) {
  33142. ret = wc_CmacUpdate(cmac, tc->m,
  33143. tc->mSz/2 - tc->partial);
  33144. if (ret != 0)
  33145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33146. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  33147. tc->mSz/2 + tc->partial);
  33148. if (ret != 0)
  33149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33150. }
  33151. else {
  33152. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  33153. if (ret != 0)
  33154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33155. }
  33156. ret = wc_CmacFinal(cmac, tag, &tagSz);
  33157. if (ret != 0)
  33158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33159. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  33160. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33161. XMEMSET(tag, 0, sizeof(tag));
  33162. tagSz = sizeof(tag);
  33163. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  33164. tc->k, tc->kSz);
  33165. if (ret != 0)
  33166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33167. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  33168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33169. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  33170. tc->k, tc->kSz);
  33171. if (ret != 0)
  33172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33173. }
  33174. ret = 0;
  33175. out:
  33176. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33177. if (cmac)
  33178. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  33179. #endif
  33180. return ret;
  33181. }
  33182. #endif /* NO_AES && WOLFSSL_CMAC */
  33183. #if defined(WOLFSSL_SIPHASH)
  33184. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33185. /* Test vectors from:
  33186. * https://github.com/veorq/SipHash/blob/master/vectors.h
  33187. */
  33188. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  33189. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  33190. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  33191. };
  33192. static const unsigned char siphash_msg[64] = {
  33193. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  33194. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  33195. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  33196. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  33197. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  33198. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  33199. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  33200. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  33201. };
  33202. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  33203. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  33204. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  33205. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  33206. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  33207. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  33208. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  33209. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  33210. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  33211. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  33212. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  33213. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  33214. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  33215. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  33216. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  33217. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  33218. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  33219. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  33220. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  33221. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  33222. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  33223. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  33224. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  33225. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  33226. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  33227. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  33228. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  33229. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  33230. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  33231. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  33232. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  33233. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  33234. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  33235. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  33236. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  33237. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  33238. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  33239. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  33240. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  33241. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  33242. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  33243. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  33244. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  33245. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  33246. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  33247. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  33248. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  33249. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  33250. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  33251. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  33252. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  33253. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  33254. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  33255. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  33256. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  33257. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  33258. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  33259. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  33260. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  33261. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  33262. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  33263. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  33264. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  33265. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  33266. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  33267. };
  33268. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  33269. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  33270. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  33271. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  33272. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  33273. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  33274. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  33275. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  33276. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  33277. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  33278. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  33279. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  33280. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  33281. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  33282. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  33283. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  33284. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  33285. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  33286. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  33287. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  33288. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  33289. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  33290. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  33291. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  33292. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  33293. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  33294. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  33295. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  33296. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  33297. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  33298. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  33299. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  33300. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  33301. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  33302. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  33303. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  33304. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  33305. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  33306. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  33307. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  33308. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  33309. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  33310. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  33311. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  33312. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  33313. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  33314. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  33315. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  33316. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  33317. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  33318. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  33319. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  33320. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  33321. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  33322. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  33323. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  33324. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  33325. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  33326. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  33327. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  33328. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  33329. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  33330. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  33331. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  33332. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  33333. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  33334. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  33335. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  33336. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  33337. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  33338. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  33339. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  33340. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  33341. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  33342. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  33343. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  33344. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  33345. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  33346. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  33347. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  33348. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  33349. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  33350. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  33351. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  33352. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  33353. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  33354. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  33355. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  33356. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  33357. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  33358. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  33359. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  33360. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  33361. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  33362. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  33363. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  33364. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  33365. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  33366. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  33367. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  33368. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  33369. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  33370. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  33371. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  33372. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  33373. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  33374. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  33375. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  33376. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  33377. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  33378. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  33379. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  33380. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  33381. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  33382. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  33383. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  33384. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  33385. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  33386. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  33387. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  33388. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  33389. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  33390. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  33391. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  33392. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  33393. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  33394. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  33395. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  33396. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  33397. };
  33398. #endif
  33399. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  33400. {
  33401. wc_test_ret_t ret = 0;
  33402. int i;
  33403. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33404. unsigned char res[SIPHASH_MAC_SIZE_16];
  33405. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  33406. SipHash siphash;
  33407. for (i = 0; i < 64; i++) {
  33408. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33409. if (ret != 0)
  33410. return WC_TEST_RET_ENC_I(i);
  33411. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33412. if (ret != 0)
  33413. return WC_TEST_RET_ENC_I(i);
  33414. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33415. if (ret != 0)
  33416. return WC_TEST_RET_ENC_I(i);
  33417. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33418. return WC_TEST_RET_ENC_I(i);
  33419. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  33420. if (ret != 0)
  33421. return WC_TEST_RET_ENC_I(i);
  33422. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33423. return WC_TEST_RET_ENC_I(i);
  33424. }
  33425. for (i = 0; i < 64; i++) {
  33426. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  33427. if (ret != 0)
  33428. return WC_TEST_RET_ENC_I(i);
  33429. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33430. if (ret != 0)
  33431. return WC_TEST_RET_ENC_I(i);
  33432. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33433. if (ret != 0)
  33434. return WC_TEST_RET_ENC_I(i);
  33435. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33436. return WC_TEST_RET_ENC_I(i);
  33437. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  33438. if (ret != 0)
  33439. return WC_TEST_RET_ENC_I(i);
  33440. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33441. return WC_TEST_RET_ENC_I(i);
  33442. }
  33443. #endif
  33444. /* Testing bad parameters. */
  33445. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33446. if (ret != BAD_FUNC_ARG)
  33447. return WC_TEST_RET_ENC_EC(ret);
  33448. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  33449. if (ret != BAD_FUNC_ARG)
  33450. return WC_TEST_RET_ENC_EC(ret);
  33451. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33452. if (ret != BAD_FUNC_ARG)
  33453. return WC_TEST_RET_ENC_EC(ret);
  33454. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  33455. if (ret != BAD_FUNC_ARG)
  33456. return WC_TEST_RET_ENC_EC(ret);
  33457. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33458. if (ret != 0)
  33459. return WC_TEST_RET_ENC_EC(ret);
  33460. ret = wc_SipHashUpdate(NULL, NULL, 0);
  33461. if (ret != BAD_FUNC_ARG)
  33462. return WC_TEST_RET_ENC_EC(ret);
  33463. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  33464. if (ret != BAD_FUNC_ARG)
  33465. return WC_TEST_RET_ENC_EC(ret);
  33466. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33467. if (ret != BAD_FUNC_ARG)
  33468. return WC_TEST_RET_ENC_EC(ret);
  33469. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33470. if (ret != BAD_FUNC_ARG)
  33471. return WC_TEST_RET_ENC_EC(ret);
  33472. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  33473. if (ret != BAD_FUNC_ARG)
  33474. return WC_TEST_RET_ENC_EC(ret);
  33475. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33476. if (ret != BAD_FUNC_ARG)
  33477. return WC_TEST_RET_ENC_EC(ret);
  33478. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33479. if (ret != BAD_FUNC_ARG)
  33480. return WC_TEST_RET_ENC_EC(ret);
  33481. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33482. if (ret != BAD_FUNC_ARG)
  33483. return WC_TEST_RET_ENC_EC(ret);
  33484. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  33485. if (ret != BAD_FUNC_ARG)
  33486. return WC_TEST_RET_ENC_EC(ret);
  33487. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  33488. if (ret != BAD_FUNC_ARG)
  33489. return WC_TEST_RET_ENC_EC(ret);
  33490. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  33491. if (ret != BAD_FUNC_ARG)
  33492. return WC_TEST_RET_ENC_EC(ret);
  33493. /* Test cache with multiple non blocksize bytes */
  33494. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33495. if (ret != 0)
  33496. return WC_TEST_RET_ENC_EC(ret);
  33497. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  33498. if (ret != 0)
  33499. return WC_TEST_RET_ENC_EC(ret);
  33500. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  33501. if (ret != 0)
  33502. return WC_TEST_RET_ENC_EC(ret);
  33503. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33504. if (ret != 0)
  33505. return WC_TEST_RET_ENC_EC(ret);
  33506. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33507. if (ret != 0)
  33508. return WC_TEST_RET_ENC_EC(ret);
  33509. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  33510. if (ret != 0)
  33511. return WC_TEST_RET_ENC_EC(ret);
  33512. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  33513. if (ret != 0)
  33514. return WC_TEST_RET_ENC_EC(ret);
  33515. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  33516. return WC_TEST_RET_ENC_NC;
  33517. return 0;
  33518. }
  33519. #endif /* WOLFSSL_SIPHASH */
  33520. #ifdef HAVE_LIBZ
  33521. static const byte sample_text[] =
  33522. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  33523. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  33524. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  33525. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  33526. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  33527. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  33528. "\n"
  33529. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  33530. "four loko you probably haven't heard of them high life. Messenger bag\n"
  33531. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  33532. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  33533. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  33534. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  33535. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  33536. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  33537. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  33538. "four loko whatever street art yr farm-to-table.\n"
  33539. "\n"
  33540. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  33541. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  33542. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  33543. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  33544. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  33545. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  33546. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  33547. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  33548. "locavore.\n"
  33549. "\n"
  33550. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  33551. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  33552. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  33553. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  33554. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  33555. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  33556. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  33557. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  33558. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  33559. "neutra PBR selvage.\n"
  33560. "\n"
  33561. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  33562. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  33563. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  33564. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  33565. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  33566. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  33567. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  33568. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  33569. "mollit magna, sriracha sartorial helvetica.\n"
  33570. "\n"
  33571. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  33572. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  33573. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  33574. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  33575. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  33576. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  33577. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  33578. "\n"
  33579. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  33580. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  33581. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  33582. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  33583. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  33584. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  33585. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  33586. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  33587. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  33588. "\n"
  33589. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  33590. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  33591. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  33592. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  33593. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  33594. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  33595. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  33596. "bag dolor terry richardson sapiente.\n";
  33597. static const byte sample_text_gz[] = {
  33598. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  33599. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  33600. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  33601. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  33602. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  33603. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  33604. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  33605. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  33606. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  33607. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  33608. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  33609. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  33610. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  33611. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  33612. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  33613. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  33614. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  33615. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  33616. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  33617. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  33618. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  33619. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  33620. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  33621. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  33622. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  33623. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  33624. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  33625. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  33626. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  33627. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  33628. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  33629. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  33630. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  33631. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  33632. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  33633. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  33634. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  33635. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  33636. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  33637. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  33638. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  33639. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  33640. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  33641. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  33642. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  33643. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  33644. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  33645. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  33646. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  33647. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  33648. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  33649. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  33650. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  33651. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  33652. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  33653. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  33654. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  33655. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  33656. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  33657. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  33658. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  33659. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  33660. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  33661. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  33662. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  33663. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  33664. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  33665. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  33666. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  33667. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  33668. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  33669. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  33670. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  33671. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  33672. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  33673. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  33674. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  33675. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  33676. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  33677. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  33678. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  33679. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  33680. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  33681. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  33682. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  33683. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  33684. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  33685. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  33686. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  33687. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  33688. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  33689. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  33690. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  33691. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  33692. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  33693. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  33694. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  33695. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  33696. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  33697. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  33698. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  33699. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  33700. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  33701. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  33702. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  33703. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  33704. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  33705. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  33706. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  33707. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  33708. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  33709. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  33710. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  33711. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  33712. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  33713. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  33714. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  33715. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  33716. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  33717. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  33718. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  33719. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  33720. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  33721. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  33722. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  33723. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  33724. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  33725. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  33726. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  33727. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  33728. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  33729. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  33730. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  33731. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  33732. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  33733. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  33734. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  33735. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  33736. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  33737. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  33738. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  33739. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  33740. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  33741. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  33742. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  33743. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  33744. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  33745. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  33746. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  33747. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  33748. };
  33749. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  33750. {
  33751. wc_test_ret_t ret = 0;
  33752. word32 dSz = sizeof(sample_text);
  33753. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  33754. byte *c;
  33755. byte *d;
  33756. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33757. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33758. if (c == NULL || d == NULL) {
  33759. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  33760. }
  33761. /* follow calloc and initialize to 0 */
  33762. XMEMSET(c, 0, cSz);
  33763. XMEMSET(d, 0, dSz);
  33764. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  33765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33766. }
  33767. cSz = (word32)ret;
  33768. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  33769. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33770. }
  33771. dSz = (word32)ret;
  33772. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33773. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33774. }
  33775. /* GZIP tests */
  33776. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  33777. XMEMSET(c, 0, cSz);
  33778. XMEMSET(d, 0, dSz);
  33779. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  33780. if (ret < 0)
  33781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33782. cSz = (word32)ret;
  33783. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  33784. if (ret < 0)
  33785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33786. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33787. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33788. }
  33789. /* Try with gzip generated output */
  33790. XMEMSET(d, 0, dSz);
  33791. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  33792. LIBZ_WINBITS_GZIP);
  33793. if (ret < 0)
  33794. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33795. dSz = (word32)ret;
  33796. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33797. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33798. }
  33799. ret = 0; /* success */
  33800. exit:
  33801. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33802. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33803. return ret;
  33804. }
  33805. #endif /* HAVE_LIBZ */
  33806. #ifdef HAVE_PKCS7
  33807. /* External Debugging/Testing Note:
  33808. *
  33809. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  33810. * additional testing. To dump bundles to files DER encoded files, please
  33811. * define:
  33812. *
  33813. * #define PKCS7_OUTPUT_TEST_BUNDLES
  33814. */
  33815. /* Loads certs and keys for use with PKCS7 tests, from either files
  33816. * or buffers.
  33817. *
  33818. * rsaClientCertBuf - output buffer for RSA client cert
  33819. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  33820. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  33821. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  33822. *
  33823. * rsaServerCertBuf - output buffer for RSA server cert
  33824. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  33825. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  33826. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  33827. *
  33828. * rsaCaCertBuf - output buffer for RSA CA cert
  33829. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  33830. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  33831. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  33832. *
  33833. * eccClientCertBuf - output buffer for ECC cert
  33834. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  33835. * eccClientPrivKeyBuf - output buffer for ECC private key
  33836. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  33837. *
  33838. * Returns 0 on success, negative on error
  33839. */
  33840. static wc_test_ret_t pkcs7_load_certs_keys(
  33841. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  33842. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  33843. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  33844. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  33845. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  33846. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  33847. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  33848. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  33849. {
  33850. #ifndef NO_FILESYSTEM
  33851. XFILE certFile;
  33852. XFILE keyFile;
  33853. (void)certFile;
  33854. (void)keyFile;
  33855. #endif
  33856. #ifndef NO_RSA
  33857. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  33858. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  33859. return BAD_FUNC_ARG;
  33860. #endif
  33861. #ifdef HAVE_ECC
  33862. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  33863. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  33864. return BAD_FUNC_ARG;
  33865. #endif
  33866. /* RSA */
  33867. #ifndef NO_RSA
  33868. #ifdef USE_CERT_BUFFERS_1024
  33869. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  33870. return WC_TEST_RET_ENC_NC;
  33871. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  33872. sizeof_client_cert_der_1024);
  33873. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  33874. if (rsaServerCertBuf != NULL) {
  33875. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  33876. return WC_TEST_RET_ENC_NC;
  33877. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  33878. sizeof_server_cert_der_1024);
  33879. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  33880. }
  33881. if (rsaCaCertBuf != NULL) {
  33882. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  33883. return WC_TEST_RET_ENC_NC;
  33884. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  33885. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  33886. }
  33887. #elif defined(USE_CERT_BUFFERS_2048)
  33888. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  33889. return WC_TEST_RET_ENC_NC;
  33890. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  33891. sizeof_client_cert_der_2048);
  33892. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  33893. if (rsaServerCertBuf != NULL) {
  33894. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  33895. return WC_TEST_RET_ENC_NC;
  33896. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  33897. sizeof_server_cert_der_2048);
  33898. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  33899. }
  33900. if (rsaCaCertBuf != NULL) {
  33901. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  33902. return WC_TEST_RET_ENC_NC;
  33903. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  33904. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  33905. }
  33906. #else
  33907. certFile = XFOPEN(clientCert, "rb");
  33908. if (!certFile)
  33909. return WC_TEST_RET_ENC_ERRNO;
  33910. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  33911. *rsaClientCertBufSz, certFile);
  33912. XFCLOSE(certFile);
  33913. if (*rsaClientCertBufSz == 0)
  33914. return WC_TEST_RET_ENC_ERRNO;
  33915. if (rsaServerCertBuf != NULL) {
  33916. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  33917. if (!certFile)
  33918. return WC_TEST_RET_ENC_ERRNO;
  33919. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  33920. *rsaServerCertBufSz, certFile);
  33921. XFCLOSE(certFile);
  33922. if (*rsaServerCertBufSz == 0)
  33923. return WC_TEST_RET_ENC_ERRNO;
  33924. }
  33925. if (rsaCaCertBuf != NULL) {
  33926. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  33927. if (!certFile)
  33928. return WC_TEST_RET_ENC_ERRNO;
  33929. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  33930. certFile);
  33931. XFCLOSE(certFile);
  33932. if (*rsaCaCertBufSz == 0)
  33933. return WC_TEST_RET_ENC_ERRNO;
  33934. }
  33935. #endif
  33936. #ifdef USE_CERT_BUFFERS_1024
  33937. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  33938. return WC_TEST_RET_ENC_NC;
  33939. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  33940. sizeof_client_key_der_1024);
  33941. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  33942. if (rsaServerPrivKeyBuf != NULL) {
  33943. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  33944. return WC_TEST_RET_ENC_NC;
  33945. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  33946. sizeof_server_key_der_1024);
  33947. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  33948. }
  33949. if (rsaCaPrivKeyBuf != NULL) {
  33950. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  33951. return WC_TEST_RET_ENC_NC;
  33952. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  33953. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  33954. }
  33955. #elif defined(USE_CERT_BUFFERS_2048)
  33956. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  33957. return WC_TEST_RET_ENC_NC;
  33958. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  33959. sizeof_client_key_der_2048);
  33960. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  33961. if (rsaServerPrivKeyBuf != NULL) {
  33962. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  33963. return WC_TEST_RET_ENC_NC;
  33964. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  33965. sizeof_server_key_der_2048);
  33966. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  33967. }
  33968. if (rsaCaPrivKeyBuf != NULL) {
  33969. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  33970. return WC_TEST_RET_ENC_NC;
  33971. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  33972. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  33973. }
  33974. #else
  33975. keyFile = XFOPEN(clientKey, "rb");
  33976. if (!keyFile)
  33977. return WC_TEST_RET_ENC_ERRNO;
  33978. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  33979. *rsaClientPrivKeyBufSz, keyFile);
  33980. XFCLOSE(keyFile);
  33981. if (*rsaClientPrivKeyBufSz == 0)
  33982. return WC_TEST_RET_ENC_ERRNO;
  33983. if (rsaServerPrivKeyBuf != NULL) {
  33984. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  33985. if (!keyFile)
  33986. return WC_TEST_RET_ENC_ERRNO;
  33987. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  33988. *rsaServerPrivKeyBufSz, keyFile);
  33989. XFCLOSE(keyFile);
  33990. if (*rsaServerPrivKeyBufSz == 0)
  33991. return WC_TEST_RET_ENC_ERRNO;
  33992. }
  33993. if (rsaCaPrivKeyBuf != NULL) {
  33994. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  33995. if (!keyFile)
  33996. return WC_TEST_RET_ENC_ERRNO;
  33997. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  33998. *rsaCaPrivKeyBufSz, keyFile);
  33999. XFCLOSE(keyFile);
  34000. if (*rsaCaPrivKeyBufSz == 0)
  34001. return WC_TEST_RET_ENC_ERRNO;
  34002. }
  34003. #endif /* USE_CERT_BUFFERS */
  34004. #endif /* NO_RSA */
  34005. /* ECC */
  34006. #ifdef HAVE_ECC
  34007. #ifdef USE_CERT_BUFFERS_256
  34008. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  34009. return WC_TEST_RET_ENC_NC;
  34010. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  34011. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  34012. #else
  34013. certFile = XFOPEN(eccClientCert, "rb");
  34014. if (!certFile)
  34015. return WC_TEST_RET_ENC_ERRNO;
  34016. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  34017. *eccClientCertBufSz, certFile);
  34018. XFCLOSE(certFile);
  34019. if (*eccClientCertBufSz == 0)
  34020. return WC_TEST_RET_ENC_ERRNO;
  34021. #endif /* USE_CERT_BUFFERS_256 */
  34022. #ifdef USE_CERT_BUFFERS_256
  34023. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  34024. return WC_TEST_RET_ENC_NC;
  34025. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  34026. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  34027. #else
  34028. keyFile = XFOPEN(eccClientKey, "rb");
  34029. if (!keyFile)
  34030. return WC_TEST_RET_ENC_ERRNO;
  34031. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  34032. *eccClientPrivKeyBufSz, keyFile);
  34033. XFCLOSE(keyFile);
  34034. if (*eccClientPrivKeyBufSz == 0)
  34035. return WC_TEST_RET_ENC_ERRNO;
  34036. #endif /* USE_CERT_BUFFERS_256 */
  34037. #endif /* HAVE_ECC */
  34038. #ifdef NO_RSA
  34039. (void)rsaClientCertBuf;
  34040. (void)rsaClientCertBufSz;
  34041. (void)rsaClientPrivKeyBuf;
  34042. (void)rsaClientPrivKeyBufSz;
  34043. (void)rsaServerCertBuf;
  34044. (void)rsaServerCertBufSz;
  34045. (void)rsaServerPrivKeyBuf;
  34046. (void)rsaServerPrivKeyBufSz;
  34047. (void)rsaCaCertBuf;
  34048. (void)rsaCaCertBufSz;
  34049. (void)rsaCaPrivKeyBuf;
  34050. (void)rsaCaPrivKeyBufSz;
  34051. #endif
  34052. #ifndef HAVE_ECC
  34053. (void)eccClientCertBuf;
  34054. (void)eccClientCertBufSz;
  34055. (void)eccClientPrivKeyBuf;
  34056. (void)eccClientPrivKeyBufSz;
  34057. #endif
  34058. #ifndef NO_FILESYSTEM
  34059. (void)certFile;
  34060. (void)keyFile;
  34061. #endif
  34062. return 0;
  34063. }
  34064. typedef struct {
  34065. const byte* content;
  34066. word32 contentSz;
  34067. int contentOID;
  34068. int encryptOID;
  34069. int keyWrapOID;
  34070. int keyAgreeOID;
  34071. byte* cert;
  34072. size_t certSz;
  34073. byte* privateKey;
  34074. word32 privateKeySz;
  34075. byte* optionalUkm;
  34076. word32 optionalUkmSz;
  34077. int ktriOptions; /* KTRI options flags */
  34078. int kariOptions; /* KARI options flags */
  34079. /* KEKRI specific */
  34080. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  34081. word32 secretKeySz; /* size of secretKey, bytes */
  34082. const byte* secretKeyId; /* key identifier */
  34083. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34084. void* timePtr; /* time_t pointer */
  34085. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34086. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34087. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34088. word32 otherAttrSz; /* size of otherAttr, bytes */
  34089. int kekriOptions; /* KEKRI options flags */
  34090. /* PWRI specific */
  34091. const char* password;
  34092. word32 passwordSz;
  34093. const byte* salt;
  34094. word32 saltSz;
  34095. int kdfOID;
  34096. int hashOID;
  34097. int kdfIterations;
  34098. int pwriOptions; /* PWRI options flags */
  34099. /* ORI specific */
  34100. int isOri;
  34101. int oriOptions; /* ORI options flags */
  34102. const char* outFileName;
  34103. } pkcs7EnvelopedVector;
  34104. static const byte asnDataOid[] = {
  34105. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  34106. };
  34107. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  34108. * and giving wolfCrypt the value for oriOID and oriValue to place in
  34109. * OtherRecipientInfo.
  34110. *
  34111. * Returns 0 on success, negative upon error. */
  34112. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  34113. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  34114. void* ctx)
  34115. {
  34116. int i;
  34117. /* make sure buffers are large enough */
  34118. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  34119. return WC_TEST_RET_ENC_NC;
  34120. /* our simple encryption algorithm will be take the bitwise complement */
  34121. oriValue[0] = 0x04; /*ASN OCTET STRING */
  34122. oriValue[1] = (byte)cekSz; /* length */
  34123. for (i = 0; i < (int)cekSz; i++) {
  34124. oriValue[2 + i] = ~cek[i];
  34125. }
  34126. *oriValueSz = 2 + cekSz;
  34127. /* set oriType to ASN.1 encoded data OID */
  34128. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  34129. *oriTypeSz = sizeof(asnDataOid);
  34130. (void)pkcs7;
  34131. (void)ctx;
  34132. return 0;
  34133. }
  34134. /* ORI decrypt callback, responsible for providing a decrypted content
  34135. * encryption key (CEK) placed into decryptedKey and size placed into
  34136. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  34137. * in decrypting the encrypted CEK.
  34138. *
  34139. * Returns 0 on success, negative upon error. */
  34140. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  34141. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  34142. word32* decryptedKeySz, void* ctx)
  34143. {
  34144. int i;
  34145. /* make sure oriType matches what we expect */
  34146. if (oriTypeSz != sizeof(asnDataOid))
  34147. return WC_TEST_RET_ENC_NC;
  34148. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  34149. return WC_TEST_RET_ENC_NC;
  34150. /* make sure decrypted buffer is large enough */
  34151. if (*decryptedKeySz < oriValueSz)
  34152. return WC_TEST_RET_ENC_NC;
  34153. /* decrypt encrypted CEK using simple bitwise complement,
  34154. only for example */
  34155. for (i = 0; i < (int)oriValueSz - 2; i++) {
  34156. decryptedKey[i] = ~oriValue[2 + i];
  34157. }
  34158. *decryptedKeySz = oriValueSz - 2;
  34159. (void)pkcs7;
  34160. (void)ctx;
  34161. return 0;
  34162. }
  34163. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34164. /* returns 0 on success */
  34165. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  34166. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  34167. byte* in, int inSz, byte* out, void* usrCtx)
  34168. {
  34169. wc_test_ret_t ret;
  34170. int keyId = -1, keySz;
  34171. word32 keyIdSz = 8;
  34172. const byte* key;
  34173. byte keyIdRaw[8];
  34174. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34175. Aes *aes;
  34176. #else
  34177. Aes aes[1];
  34178. #endif
  34179. /* looking for KEY ID
  34180. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  34181. */
  34182. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  34183. /* 0x06, 0x0B do not pass in tag and length */
  34184. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34185. 0x01, 0x09, 0x10, 0x02, 0x25
  34186. };
  34187. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  34188. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34189. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34190. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34191. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34192. };
  34193. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  34194. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34195. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34196. };
  34197. /* test user context passed in */
  34198. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  34199. return WC_TEST_RET_ENC_NC;
  34200. }
  34201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34202. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  34203. return WC_TEST_RET_ENC_ERRNO;
  34204. #endif
  34205. /* if needing to find keyIdSz can call with NULL */
  34206. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  34207. &keyIdSz);
  34208. if (ret != LENGTH_ONLY_E) {
  34209. printf("Unexpected error %d when getting keyIdSz\n", ret);
  34210. printf("Possibly no KEY ID attribute set\n");
  34211. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34212. }
  34213. else {
  34214. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  34215. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  34216. &keyIdSz);
  34217. if (ret < 0) {
  34218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34219. }
  34220. if (keyIdSz < 3) {
  34221. printf("keyIdSz is smaller than expected\n");
  34222. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34223. }
  34224. if (keyIdSz > 2 + sizeof(int)) {
  34225. printf("example case was only expecting a keyId of int size\n");
  34226. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34227. }
  34228. /* keyIdRaw[0] OCTET TAG */
  34229. /* keyIdRaw[1] Length */
  34230. #ifdef BIG_ENDIAN_ORDER
  34231. if (keyIdRaw[1] == 0x01) {
  34232. keyId = 1;
  34233. }
  34234. #else
  34235. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  34236. #endif
  34237. }
  34238. /* Use keyID here if found to select key and decrypt in HSM or in this
  34239. * example just select key and do software decryption */
  34240. if (keyId == 1) {
  34241. key = altKey;
  34242. keySz = sizeof(altKey);
  34243. }
  34244. else {
  34245. key = defKey;
  34246. keySz = sizeof(defKey);
  34247. }
  34248. switch (encryptOID) {
  34249. #ifdef WOLFSSL_AES_256
  34250. case AES256CBCb:
  34251. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  34252. ERROR_OUT(BAD_FUNC_ARG, out);
  34253. break;
  34254. #endif
  34255. #ifdef WOLFSSL_AES_128
  34256. case AES128CBCb:
  34257. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  34258. ERROR_OUT(BAD_FUNC_ARG, out);
  34259. break;
  34260. #endif
  34261. default:
  34262. printf("Unsupported content cipher type for example");
  34263. ERROR_OUT(ALGO_ID_E, out);
  34264. };
  34265. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  34266. if (ret == 0) {
  34267. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  34268. if (ret == 0)
  34269. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  34270. wc_AesFree(aes);
  34271. }
  34272. out:
  34273. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34274. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34275. #endif
  34276. (void)aad;
  34277. (void)aadSz;
  34278. (void)authTag;
  34279. (void)authTagSz;
  34280. return (int)ret;
  34281. }
  34282. #endif /* !NO_AES && HAVE_AES_CBC */
  34283. #define PKCS7_BUF_SIZE 2048
  34284. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34285. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34286. byte* eccCert, word32 eccCertSz,
  34287. byte* eccPrivKey, word32 eccPrivKeySz)
  34288. {
  34289. wc_test_ret_t ret = 0;
  34290. int testSz = 0, i;
  34291. int envelopedSz, decodedSz;
  34292. byte *enveloped = NULL;
  34293. byte *decoded = NULL;
  34294. PKCS7* pkcs7 = NULL;
  34295. #ifdef ECC_TIMING_RESISTANT
  34296. WC_RNG rng;
  34297. #endif
  34298. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34299. XFILE pkcs7File;
  34300. #endif
  34301. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34302. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34303. 0x72,0x6c,0x64
  34304. };
  34305. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  34306. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  34307. byte optionalUkm[] = {
  34308. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34309. };
  34310. #endif /* NO_AES */
  34311. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  34312. !defined(NO_SHA)
  34313. /* encryption key for kekri recipient types */
  34314. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34315. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34316. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34317. };
  34318. /* encryption key identifier */
  34319. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34320. 0x02,0x02,0x03,0x04
  34321. };
  34322. #endif
  34323. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  34324. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34325. #ifndef HAVE_FIPS
  34326. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  34327. #else
  34328. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34329. #endif
  34330. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34331. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34332. };
  34333. #endif
  34334. #define MAX_TESTVECTORS_LEN 13
  34335. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  34336. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34337. if (testSz == MAX_TESTVECTORS_LEN) { \
  34338. ret = WC_TEST_RET_ENC_NC; \
  34339. goto out; \
  34340. } \
  34341. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34342. }
  34343. pkcs7EnvelopedVector *testVectors = NULL;
  34344. #ifdef ECC_TIMING_RESISTANT
  34345. XMEMSET(&rng, 0, sizeof(rng));
  34346. #endif
  34347. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34348. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34349. if (testVectors == NULL) {
  34350. ret = WC_TEST_RET_ENC_ERRNO;
  34351. goto out;
  34352. }
  34353. {
  34354. /* key transport key encryption technique */
  34355. #ifndef NO_RSA
  34356. #ifndef NO_DES3
  34357. ADD_PKCS7ENVELOPEDVECTOR(
  34358. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  34359. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34360. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34361. "pkcs7envelopedDataDES3.der");
  34362. #endif
  34363. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34364. #ifdef WOLFSSL_AES_128
  34365. ADD_PKCS7ENVELOPEDVECTOR(
  34366. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  34367. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34368. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34369. "pkcs7envelopedDataAES128CBC.der");
  34370. #endif
  34371. #ifdef WOLFSSL_AES_192
  34372. ADD_PKCS7ENVELOPEDVECTOR(
  34373. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  34374. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34375. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34376. "pkcs7envelopedDataAES192CBC.der");
  34377. #endif
  34378. #ifdef WOLFSSL_AES_256
  34379. ADD_PKCS7ENVELOPEDVECTOR(
  34380. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34381. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34382. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34383. "pkcs7envelopedDataAES256CBC.der");
  34384. /* explicitly using SKID for SubjectKeyIdentifier */
  34385. ADD_PKCS7ENVELOPEDVECTOR(
  34386. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34387. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  34388. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34389. "pkcs7envelopedDataAES256CBC_SKID.der");
  34390. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34391. ADD_PKCS7ENVELOPEDVECTOR(
  34392. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34393. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  34394. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34395. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  34396. #endif
  34397. #endif /* !NO_AES && HAVE_AES_CBC */
  34398. #endif
  34399. /* key agreement key encryption technique*/
  34400. #ifdef HAVE_ECC
  34401. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34402. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34403. ADD_PKCS7ENVELOPEDVECTOR(
  34404. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  34405. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34406. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34407. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34408. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  34409. #endif
  34410. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34411. ADD_PKCS7ENVELOPEDVECTOR(
  34412. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34413. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34414. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34415. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34416. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  34417. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34418. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34419. ADD_PKCS7ENVELOPEDVECTOR(
  34420. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34421. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34422. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34423. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34424. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  34425. /* with optional user keying material (ukm) */
  34426. ADD_PKCS7ENVELOPEDVECTOR(
  34427. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34428. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34429. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  34430. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34431. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  34432. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34433. #endif /* !NO_AES && HAVE_AES_CBC */
  34434. #endif
  34435. /* kekri (KEKRecipientInfo) recipient types */
  34436. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34437. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34438. ADD_PKCS7ENVELOPEDVECTOR(
  34439. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  34440. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  34441. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  34442. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34443. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  34444. #endif
  34445. #endif /* !NO_AES && HAVE_AES_CBC */
  34446. /* pwri (PasswordRecipientInfo) recipient types */
  34447. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  34448. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34449. ADD_PKCS7ENVELOPEDVECTOR(
  34450. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  34451. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34452. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  34453. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34454. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  34455. #endif
  34456. #endif
  34457. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  34458. /* ori (OtherRecipientInfo) recipient types */
  34459. ADD_PKCS7ENVELOPEDVECTOR(
  34460. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  34461. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  34462. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  34463. #endif
  34464. };
  34465. #undef MAX_TESTVECTORS_LEN
  34466. #undef ADD_PKCS7ENVELOPEDVECTOR
  34467. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34468. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34469. if ((! enveloped) || (! decoded)) {
  34470. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34471. }
  34472. #ifdef ECC_TIMING_RESISTANT
  34473. #ifndef HAVE_FIPS
  34474. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34475. #else
  34476. ret = wc_InitRng(&rng);
  34477. #endif
  34478. if (ret != 0)
  34479. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34480. #endif
  34481. for (i = 0; i < testSz; i++) {
  34482. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34483. #ifdef WOLFSSL_ASYNC_CRYPT
  34484. INVALID_DEVID /* async PKCS7 is not supported */
  34485. #else
  34486. devId
  34487. #endif
  34488. );
  34489. if (pkcs7 == NULL) {
  34490. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34491. }
  34492. if (testVectors[i].secretKey != NULL) {
  34493. /* KEKRI recipient type */
  34494. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34495. if (ret != 0)
  34496. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34497. pkcs7->content = (byte*)testVectors[i].content;
  34498. pkcs7->contentSz = testVectors[i].contentSz;
  34499. pkcs7->contentOID = testVectors[i].contentOID;
  34500. pkcs7->encryptOID = testVectors[i].encryptOID;
  34501. pkcs7->ukm = testVectors[i].optionalUkm;
  34502. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34503. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34504. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  34505. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34506. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34507. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34508. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34509. if (ret < 0) {
  34510. wc_PKCS7_Free(pkcs7);
  34511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34512. }
  34513. /* set key, for decryption */
  34514. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  34515. testVectors[i].secretKeySz);
  34516. if (ret != 0) {
  34517. wc_PKCS7_Free(pkcs7);
  34518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34519. }
  34520. } else if (testVectors[i].password != NULL) {
  34521. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34522. /* PWRI recipient type */
  34523. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34524. if (ret != 0)
  34525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34526. pkcs7->content = (byte*)testVectors[i].content;
  34527. pkcs7->contentSz = testVectors[i].contentSz;
  34528. pkcs7->contentOID = testVectors[i].contentOID;
  34529. pkcs7->encryptOID = testVectors[i].encryptOID;
  34530. pkcs7->ukm = testVectors[i].optionalUkm;
  34531. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34532. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34533. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  34534. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  34535. testVectors[i].kdfOID,
  34536. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34537. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  34538. if (ret < 0) {
  34539. wc_PKCS7_Free(pkcs7);
  34540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34541. }
  34542. /* set password, for decryption */
  34543. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34544. testVectors[i].passwordSz);
  34545. if (ret < 0) {
  34546. wc_PKCS7_Free(pkcs7);
  34547. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34548. }
  34549. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34550. } else if (testVectors[i].isOri == 1) {
  34551. /* ORI recipient type */
  34552. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34553. if (ret != 0)
  34554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34555. pkcs7->content = (byte*)testVectors[i].content;
  34556. pkcs7->contentSz = testVectors[i].contentSz;
  34557. pkcs7->contentOID = testVectors[i].contentOID;
  34558. pkcs7->encryptOID = testVectors[i].encryptOID;
  34559. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34560. testVectors[i].oriOptions);
  34561. if (ret < 0) {
  34562. wc_PKCS7_Free(pkcs7);
  34563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34564. }
  34565. /* set decrypt callback for decryption */
  34566. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34567. if (ret < 0) {
  34568. wc_PKCS7_Free(pkcs7);
  34569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34570. }
  34571. } else {
  34572. /* KTRI or KARI recipient types */
  34573. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34574. if (ret != 0)
  34575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34576. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34577. (word32)testVectors[i].certSz);
  34578. if (ret != 0) {
  34579. wc_PKCS7_Free(pkcs7);
  34580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34581. }
  34582. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34583. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34584. pkcs7->privateKey = testVectors[i].privateKey;
  34585. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34586. pkcs7->content = (byte*)testVectors[i].content;
  34587. pkcs7->contentSz = testVectors[i].contentSz;
  34588. pkcs7->contentOID = testVectors[i].contentOID;
  34589. pkcs7->encryptOID = testVectors[i].encryptOID;
  34590. pkcs7->ukm = testVectors[i].optionalUkm;
  34591. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34592. /* set SubjectIdentifier type for KTRI types */
  34593. if (testVectors[i].ktriOptions & CMS_SKID) {
  34594. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34595. if (ret != 0) {
  34596. wc_PKCS7_Free(pkcs7);
  34597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34598. }
  34599. } else if (testVectors[i].ktriOptions &
  34600. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34601. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34602. CMS_ISSUER_AND_SERIAL_NUMBER);
  34603. if (ret != 0) {
  34604. wc_PKCS7_Free(pkcs7);
  34605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34606. }
  34607. }
  34608. }
  34609. #ifdef ECC_TIMING_RESISTANT
  34610. pkcs7->rng = &rng;
  34611. #endif
  34612. /* encode envelopedData */
  34613. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  34614. PKCS7_BUF_SIZE);
  34615. if (envelopedSz <= 0) {
  34616. wc_PKCS7_Free(pkcs7);
  34617. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  34618. }
  34619. /* decode envelopedData */
  34620. pkcs7->contentOID = 0;
  34621. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  34622. decoded, PKCS7_BUF_SIZE);
  34623. if (pkcs7->contentOID != testVectors[i].contentOID ||
  34624. decodedSz <= 0) {
  34625. wc_PKCS7_Free(pkcs7);
  34626. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  34627. }
  34628. /* test decode result */
  34629. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34630. wc_PKCS7_Free(pkcs7);
  34631. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34632. }
  34633. #ifndef NO_PKCS7_STREAM
  34634. { /* test reading byte by byte */
  34635. int z;
  34636. for (z = 0; z < envelopedSz; z++) {
  34637. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  34638. decoded, PKCS7_BUF_SIZE);
  34639. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34640. printf("unexpected error %d\n", decodedSz);
  34641. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  34642. }
  34643. }
  34644. /* test decode result */
  34645. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34646. printf("stream read compare failed\n");
  34647. wc_PKCS7_Free(pkcs7);
  34648. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34649. }
  34650. }
  34651. #endif
  34652. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34653. /* output pkcs7 envelopedData for external testing */
  34654. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34655. if (!pkcs7File) {
  34656. wc_PKCS7_Free(pkcs7);
  34657. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34658. }
  34659. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34660. XFCLOSE(pkcs7File);
  34661. if (ret != envelopedSz) {
  34662. wc_PKCS7_Free(pkcs7);
  34663. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34664. } else {
  34665. /* reset ret to 0 for success */
  34666. ret = 0;
  34667. }
  34668. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34669. wc_PKCS7_Free(pkcs7);
  34670. pkcs7 = NULL;
  34671. }
  34672. #ifdef ECC_TIMING_RESISTANT
  34673. wc_FreeRng(&rng);
  34674. #endif
  34675. (void)eccCert;
  34676. (void)eccCertSz;
  34677. (void)eccPrivKey;
  34678. (void)eccPrivKeySz;
  34679. (void)rsaCert;
  34680. (void)rsaCertSz;
  34681. (void)rsaPrivKey;
  34682. (void)rsaPrivKeySz;
  34683. out:
  34684. if (testVectors)
  34685. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34686. if (enveloped)
  34687. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34688. if (decoded)
  34689. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34690. return ret;
  34691. }
  34692. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  34693. {
  34694. wc_test_ret_t ret = 0;
  34695. byte* rsaCert = NULL;
  34696. byte* rsaPrivKey = NULL;
  34697. word32 rsaCertSz = 0;
  34698. word32 rsaPrivKeySz = 0;
  34699. byte* eccCert = NULL;
  34700. byte* eccPrivKey = NULL;
  34701. word32 eccCertSz = 0;
  34702. word32 eccPrivKeySz = 0;
  34703. #ifndef NO_RSA
  34704. /* read client RSA cert and key in DER format */
  34705. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34706. if (rsaCert == NULL)
  34707. return WC_TEST_RET_ENC_ERRNO;
  34708. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34709. if (rsaPrivKey == NULL) {
  34710. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34711. return WC_TEST_RET_ENC_NC;
  34712. }
  34713. rsaCertSz = FOURK_BUF;
  34714. rsaPrivKeySz = FOURK_BUF;
  34715. #endif /* NO_RSA */
  34716. #ifdef HAVE_ECC
  34717. /* read client ECC cert and key in DER format */
  34718. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34719. if (eccCert == NULL) {
  34720. #ifndef NO_RSA
  34721. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34722. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34723. #endif
  34724. return WC_TEST_RET_ENC_NC;
  34725. }
  34726. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34727. if (eccPrivKey == NULL) {
  34728. #ifndef NO_RSA
  34729. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34730. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34731. #endif
  34732. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34733. return WC_TEST_RET_ENC_NC;
  34734. }
  34735. eccCertSz = FOURK_BUF;
  34736. eccPrivKeySz = FOURK_BUF;
  34737. #endif /* HAVE_ECC */
  34738. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34739. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34740. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34741. eccPrivKey, &eccPrivKeySz);
  34742. if (ret < 0) {
  34743. #ifndef NO_RSA
  34744. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34745. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34746. #endif
  34747. #ifdef HAVE_ECC
  34748. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34749. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34750. #endif
  34751. return WC_TEST_RET_ENC_EC(ret);
  34752. }
  34753. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34754. rsaPrivKey, (word32)rsaPrivKeySz,
  34755. eccCert, (word32)eccCertSz,
  34756. eccPrivKey, (word32)eccPrivKeySz);
  34757. #ifndef NO_RSA
  34758. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34759. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34760. #endif
  34761. #ifdef HAVE_ECC
  34762. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34763. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34764. #endif
  34765. return ret;
  34766. }
  34767. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  34768. typedef struct {
  34769. const byte* content;
  34770. word32 contentSz;
  34771. int contentOID;
  34772. int encryptOID;
  34773. int keyWrapOID;
  34774. int keyAgreeOID;
  34775. byte* cert;
  34776. size_t certSz;
  34777. byte* privateKey;
  34778. word32 privateKeySz;
  34779. PKCS7Attrib* authAttribs;
  34780. word32 authAttribsSz;
  34781. PKCS7Attrib* unauthAttribs;
  34782. word32 unauthAttribsSz;
  34783. /* KARI / KTRI specific */
  34784. byte* optionalUkm;
  34785. word32 optionalUkmSz;
  34786. int ktriOptions; /* KTRI options flags */
  34787. int kariOptions; /* KARI options flags */
  34788. /* KEKRI specific */
  34789. byte* secretKey; /* key, only for kekri RecipientInfo types */
  34790. word32 secretKeySz; /* size of secretKey, bytes */
  34791. byte* secretKeyId; /* key identifier */
  34792. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34793. void* timePtr; /* time_t pointer */
  34794. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34795. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34796. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34797. word32 otherAttrSz; /* size of otherAttr, bytes */
  34798. int kekriOptions; /* KEKRI options flags */
  34799. /* PWRI specific */
  34800. char* password; /* password */
  34801. word32 passwordSz; /* password size, bytes */
  34802. byte* salt; /* KDF salt */
  34803. word32 saltSz; /* KDF salt size, bytes */
  34804. int kdfOID; /* KDF OID */
  34805. int hashOID; /* KDF hash algorithm OID */
  34806. int kdfIterations; /* KDF iterations */
  34807. int kekEncryptOID; /* KEK encryption algorithm OID */
  34808. int pwriOptions; /* PWRI options flags */
  34809. /* ORI specific */
  34810. int isOri;
  34811. int oriOptions; /* ORI options flags */
  34812. const char* outFileName;
  34813. } pkcs7AuthEnvelopedVector;
  34814. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34815. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34816. byte* eccCert, word32 eccCertSz,
  34817. byte* eccPrivKey, word32 eccPrivKeySz)
  34818. {
  34819. wc_test_ret_t ret = 0;
  34820. int testSz = 0, i;
  34821. int envelopedSz, decodedSz;
  34822. byte *enveloped = NULL;
  34823. byte *decoded = NULL;
  34824. WC_RNG rng;
  34825. PKCS7* pkcs7;
  34826. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34827. XFILE pkcs7File;
  34828. #endif
  34829. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34830. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34831. 0x72,0x6c,0x64
  34832. };
  34833. byte senderNonce[PKCS7_NONCE_SZ + 2];
  34834. #ifdef HAVE_ECC
  34835. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34836. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34837. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  34838. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34839. 0x09, 0x05 };
  34840. PKCS7Attrib attribs[] =
  34841. {
  34842. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34843. sizeof(senderNonce) }
  34844. };
  34845. #endif
  34846. #endif
  34847. #endif
  34848. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  34849. defined(WOLFSSL_SHA512)
  34850. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  34851. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34852. };
  34853. #endif /* NO_AES */
  34854. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34855. /* encryption key for kekri recipient types */
  34856. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34857. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34858. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34859. };
  34860. /* encryption key identifier */
  34861. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34862. 0x02,0x02,0x03,0x04
  34863. };
  34864. #endif
  34865. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  34866. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34867. #ifndef HAVE_FIPS
  34868. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  34869. #else
  34870. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34871. #endif
  34872. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34873. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34874. };
  34875. #endif
  34876. #define MAX_TESTVECTORS_LEN 20
  34877. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  34878. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34879. if (testSz == MAX_TESTVECTORS_LEN) { \
  34880. ret = WC_TEST_RET_ENC_NC; \
  34881. goto out; \
  34882. } \
  34883. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  34884. sizeof _this_vector); \
  34885. }
  34886. pkcs7AuthEnvelopedVector *testVectors = NULL;
  34887. XMEMSET(&rng, 0, sizeof(rng));
  34888. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34889. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34890. if (testVectors == NULL) {
  34891. ret = WC_TEST_RET_ENC_ERRNO;
  34892. goto out;
  34893. }
  34894. {
  34895. /* key transport key encryption technique */
  34896. #ifndef NO_RSA
  34897. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34898. #ifdef WOLFSSL_AES_128
  34899. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34900. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  34901. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34902. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34903. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  34904. #endif
  34905. #ifdef WOLFSSL_AES_192
  34906. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34907. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  34908. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34909. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34910. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  34911. #endif
  34912. #ifdef WOLFSSL_AES_256
  34913. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34914. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34915. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34916. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34917. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  34918. /* test with contentType set to FirmwarePkgData */
  34919. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34920. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  34921. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  34922. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  34923. 0, 0, 0, 0, 0, 0, 0, 0,
  34924. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  34925. /* explicitly using SKID for SubjectKeyIdentifier */
  34926. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34927. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34928. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  34929. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34930. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  34931. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34932. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34933. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34934. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  34935. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34936. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34937. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  34938. #endif
  34939. #endif /* NO_AES */
  34940. #endif
  34941. /* key agreement key encryption technique*/
  34942. #ifdef HAVE_ECC
  34943. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34944. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34945. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34946. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  34947. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34948. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34949. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34950. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  34951. #endif
  34952. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34953. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34954. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34955. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34956. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34957. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34958. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  34959. /* with authenticated attributes */
  34960. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34961. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34962. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34963. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34964. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34965. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34966. 0, 0, 0,
  34967. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  34968. /* with unauthenticated attributes */
  34969. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34970. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34971. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34972. eccPrivKeySz, NULL, 0, attribs,
  34973. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  34974. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34975. 0, 0, 0,
  34976. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  34977. /* with authenticated AND unauthenticated attributes */
  34978. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34979. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34980. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34981. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34982. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34983. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34984. 0, 0, 0, 0, 0, 0,
  34985. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  34986. /* with authenticated AND unauthenticated attributes AND
  34987. * contentType of FirmwarePkgData */
  34988. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34989. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  34990. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34991. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34992. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34993. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34994. 0, 0, 0, 0, 0, 0,
  34995. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  34996. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34997. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34998. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34999. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35000. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35001. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  35002. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35003. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  35004. /* with optional user keying material (ukm) */
  35005. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35006. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35007. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35008. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  35009. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  35010. 0, 0, 0, 0, 0, 0,
  35011. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  35012. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  35013. #endif /* NO_AES */
  35014. #endif
  35015. /* kekri (KEKRecipientInfo) recipient types */
  35016. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  35017. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  35018. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35019. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  35020. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  35021. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  35022. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35023. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  35024. #endif
  35025. #endif
  35026. /* pwri (PasswordRecipientInfo) recipient types */
  35027. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  35028. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  35029. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35030. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  35031. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  35032. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  35033. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  35034. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  35035. #endif
  35036. #endif
  35037. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  35038. #ifdef WOLFSSL_AES_128
  35039. /* ori (OtherRecipientInfo) recipient types */
  35040. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35041. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  35042. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  35043. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  35044. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  35045. #endif
  35046. #endif
  35047. }
  35048. #undef MAX_TESTVECTORS_LEN
  35049. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  35050. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35051. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35052. if ((! enveloped) || (! decoded)) {
  35053. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35054. }
  35055. /* generate senderNonce */
  35056. {
  35057. #ifndef HAVE_FIPS
  35058. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35059. #else
  35060. ret = wc_InitRng(&rng);
  35061. #endif
  35062. if (ret != 0)
  35063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35064. senderNonce[0] = 0x04;
  35065. senderNonce[1] = PKCS7_NONCE_SZ;
  35066. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  35067. if (ret != 0) {
  35068. wc_FreeRng(&rng);
  35069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35070. }
  35071. }
  35072. for (i = 0; i < testSz; i++) {
  35073. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  35074. #ifdef WOLFSSL_ASYNC_CRYPT
  35075. INVALID_DEVID /* async PKCS7 is not supported */
  35076. #else
  35077. devId
  35078. #endif
  35079. );
  35080. if (pkcs7 == NULL) {
  35081. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35082. }
  35083. if (testVectors[i].secretKey != NULL) {
  35084. /* KEKRI recipient type */
  35085. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35086. if (ret != 0)
  35087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35088. pkcs7->content = (byte*)testVectors[i].content;
  35089. pkcs7->contentSz = testVectors[i].contentSz;
  35090. pkcs7->contentOID = testVectors[i].contentOID;
  35091. pkcs7->encryptOID = testVectors[i].encryptOID;
  35092. pkcs7->ukm = testVectors[i].optionalUkm;
  35093. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35094. pkcs7->authAttribs = testVectors[i].authAttribs;
  35095. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35096. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35097. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35098. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  35099. testVectors[i].secretKey, testVectors[i].secretKeySz,
  35100. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  35101. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  35102. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  35103. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  35104. if (ret < 0) {
  35105. wc_PKCS7_Free(pkcs7);
  35106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35107. }
  35108. /* set key, for decryption */
  35109. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  35110. testVectors[i].secretKeySz);
  35111. if (ret != 0) {
  35112. wc_PKCS7_Free(pkcs7);
  35113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35114. }
  35115. } else if (testVectors[i].password != NULL) {
  35116. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  35117. /* PWRI recipient type */
  35118. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35119. if (ret != 0)
  35120. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35121. pkcs7->content = (byte*)testVectors[i].content;
  35122. pkcs7->contentSz = testVectors[i].contentSz;
  35123. pkcs7->contentOID = testVectors[i].contentOID;
  35124. pkcs7->encryptOID = testVectors[i].encryptOID;
  35125. pkcs7->ukm = testVectors[i].optionalUkm;
  35126. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35127. pkcs7->authAttribs = testVectors[i].authAttribs;
  35128. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35129. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35130. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35131. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  35132. (byte*)testVectors[i].password,
  35133. testVectors[i].passwordSz, testVectors[i].salt,
  35134. testVectors[i].saltSz, testVectors[i].kdfOID,
  35135. testVectors[i].hashOID, testVectors[i].kdfIterations,
  35136. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  35137. if (ret < 0) {
  35138. wc_PKCS7_Free(pkcs7);
  35139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35140. }
  35141. /* set password, for decryption */
  35142. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  35143. testVectors[i].passwordSz);
  35144. if (ret < 0) {
  35145. wc_PKCS7_Free(pkcs7);
  35146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35147. }
  35148. #endif /* ! NO_PWDBASED && ! NO_SHA */
  35149. } else if (testVectors[i].isOri == 1) {
  35150. /* ORI recipient type */
  35151. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35152. if (ret != 0)
  35153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35154. pkcs7->content = (byte*)testVectors[i].content;
  35155. pkcs7->contentSz = testVectors[i].contentSz;
  35156. pkcs7->contentOID = testVectors[i].contentOID;
  35157. pkcs7->encryptOID = testVectors[i].encryptOID;
  35158. pkcs7->authAttribs = testVectors[i].authAttribs;
  35159. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35160. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35161. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35162. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  35163. testVectors[i].oriOptions);
  35164. if (ret < 0) {
  35165. wc_PKCS7_Free(pkcs7);
  35166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35167. }
  35168. /* set decrypt callback for decryption */
  35169. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  35170. if (ret < 0) {
  35171. wc_PKCS7_Free(pkcs7);
  35172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35173. }
  35174. } else {
  35175. /* KTRI or KARI recipient types */
  35176. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  35177. (word32)testVectors[i].certSz);
  35178. if (ret != 0) {
  35179. wc_PKCS7_Free(pkcs7);
  35180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35181. }
  35182. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  35183. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  35184. pkcs7->privateKey = testVectors[i].privateKey;
  35185. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  35186. pkcs7->content = (byte*)testVectors[i].content;
  35187. pkcs7->contentSz = testVectors[i].contentSz;
  35188. pkcs7->contentOID = testVectors[i].contentOID;
  35189. pkcs7->encryptOID = testVectors[i].encryptOID;
  35190. pkcs7->ukm = testVectors[i].optionalUkm;
  35191. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35192. pkcs7->authAttribs = testVectors[i].authAttribs;
  35193. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35194. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35195. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35196. /* set SubjectIdentifier type for KTRI types */
  35197. if (testVectors[i].ktriOptions & CMS_SKID) {
  35198. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35199. if (ret != 0) {
  35200. wc_PKCS7_Free(pkcs7);
  35201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35202. }
  35203. } else if (testVectors[i].ktriOptions &
  35204. CMS_ISSUER_AND_SERIAL_NUMBER) {
  35205. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  35206. CMS_ISSUER_AND_SERIAL_NUMBER);
  35207. if (ret != 0) {
  35208. wc_PKCS7_Free(pkcs7);
  35209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35210. }
  35211. }
  35212. }
  35213. #ifdef ECC_TIMING_RESISTANT
  35214. pkcs7->rng = &rng;
  35215. #endif
  35216. /* encode envelopedData */
  35217. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  35218. PKCS7_BUF_SIZE);
  35219. if (envelopedSz <= 0) {
  35220. wc_PKCS7_Free(pkcs7);
  35221. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  35222. }
  35223. #ifndef NO_PKCS7_STREAM
  35224. { /* test reading byte by byte */
  35225. int z;
  35226. for (z = 0; z < envelopedSz; z++) {
  35227. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  35228. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  35229. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35230. printf("unexpected error %d\n", decodedSz);
  35231. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35232. }
  35233. }
  35234. /* test decode result */
  35235. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35236. printf("stream read compare failed\n");
  35237. wc_PKCS7_Free(pkcs7);
  35238. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35239. }
  35240. }
  35241. #endif
  35242. /* decode envelopedData */
  35243. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  35244. envelopedSz, decoded,
  35245. PKCS7_BUF_SIZE);
  35246. if (decodedSz <= 0) {
  35247. wc_PKCS7_Free(pkcs7);
  35248. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35249. }
  35250. /* test decode result */
  35251. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  35252. wc_PKCS7_Free(pkcs7);
  35253. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35254. }
  35255. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35256. /* output pkcs7 envelopedData for external testing */
  35257. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35258. if (!pkcs7File) {
  35259. wc_PKCS7_Free(pkcs7);
  35260. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35261. }
  35262. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  35263. XFCLOSE(pkcs7File);
  35264. if (ret != envelopedSz) {
  35265. wc_PKCS7_Free(pkcs7);
  35266. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35267. } else {
  35268. /* reset ret to 0 for success */
  35269. ret = 0;
  35270. }
  35271. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  35272. wc_PKCS7_Free(pkcs7);
  35273. pkcs7 = NULL;
  35274. }
  35275. wc_FreeRng(&rng);
  35276. (void)eccCert;
  35277. (void)eccCertSz;
  35278. (void)eccPrivKey;
  35279. (void)eccPrivKeySz;
  35280. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  35281. (void)secretKey;
  35282. (void)secretKeyId;
  35283. #endif
  35284. #ifdef NO_RSA
  35285. (void)rsaCert;
  35286. (void)rsaCertSz;
  35287. (void)rsaPrivKey;
  35288. (void)rsaPrivKeySz;
  35289. #endif
  35290. out:
  35291. if (testVectors)
  35292. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35293. if (enveloped)
  35294. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35295. if (decoded)
  35296. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35297. return ret;
  35298. }
  35299. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  35300. {
  35301. wc_test_ret_t ret = 0;
  35302. byte* rsaCert = NULL;
  35303. byte* rsaPrivKey = NULL;
  35304. word32 rsaCertSz = 0;
  35305. word32 rsaPrivKeySz = 0;
  35306. byte* eccCert = NULL;
  35307. byte* eccPrivKey = NULL;
  35308. word32 eccCertSz = 0;
  35309. word32 eccPrivKeySz = 0;
  35310. #ifndef NO_RSA
  35311. /* read client RSA cert and key in DER format */
  35312. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35313. if (rsaCert == NULL)
  35314. return WC_TEST_RET_ENC_ERRNO;
  35315. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35316. if (rsaPrivKey == NULL) {
  35317. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35318. return WC_TEST_RET_ENC_NC;
  35319. }
  35320. rsaCertSz = FOURK_BUF;
  35321. rsaPrivKeySz = FOURK_BUF;
  35322. #endif /* NO_RSA */
  35323. #ifdef HAVE_ECC
  35324. /* read client ECC cert and key in DER format */
  35325. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35326. if (eccCert == NULL) {
  35327. #ifndef NO_RSA
  35328. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35329. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35330. #endif
  35331. return WC_TEST_RET_ENC_NC;
  35332. }
  35333. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35334. if (eccPrivKey == NULL) {
  35335. #ifndef NO_RSA
  35336. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35337. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35338. #endif
  35339. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35340. return WC_TEST_RET_ENC_NC;
  35341. }
  35342. eccCertSz = FOURK_BUF;
  35343. eccPrivKeySz = FOURK_BUF;
  35344. #endif /* HAVE_ECC */
  35345. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  35346. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  35347. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  35348. eccPrivKey, &eccPrivKeySz);
  35349. if (ret < 0) {
  35350. #ifndef NO_RSA
  35351. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35352. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35353. #endif
  35354. #ifdef HAVE_ECC
  35355. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35356. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35357. #endif
  35358. return WC_TEST_RET_ENC_EC(ret);
  35359. }
  35360. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  35361. rsaPrivKey, (word32)rsaPrivKeySz,
  35362. eccCert, (word32)eccCertSz,
  35363. eccPrivKey, (word32)eccPrivKeySz);
  35364. #ifndef NO_RSA
  35365. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35366. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35367. #endif
  35368. #ifdef HAVE_ECC
  35369. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35370. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35371. #endif
  35372. return ret;
  35373. }
  35374. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  35375. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35376. static const byte p7DefKey[] = {
  35377. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35378. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35379. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35380. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35381. };
  35382. static const byte p7AltKey[] = {
  35383. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35384. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35385. };
  35386. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  35387. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  35388. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  35389. {
  35390. wc_test_ret_t ret;
  35391. if (cek == NULL || out == NULL)
  35392. return BAD_FUNC_ARG;
  35393. /* test case sanity checks */
  35394. if (keyIdSz != 1) {
  35395. return WC_TEST_RET_ENC_NC;
  35396. }
  35397. if (keyId[0] != 0x00) {
  35398. return WC_TEST_RET_ENC_NC;
  35399. }
  35400. if (type != (int)PKCS7_KEKRI) {
  35401. return WC_TEST_RET_ENC_NC;
  35402. }
  35403. switch (keyWrapAlgo) {
  35404. case AES256_WRAP:
  35405. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  35406. out, outSz, NULL);
  35407. if (ret <= 0)
  35408. return (int)ret;
  35409. break;
  35410. default:
  35411. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  35412. return BAD_KEYWRAP_ALG_E;
  35413. };
  35414. (void)pkcs7;
  35415. (void)direction;
  35416. (void)orginKey; /* used with KAKRI */
  35417. (void)orginKeySz;
  35418. return (int)ret;
  35419. }
  35420. /* returns key size on success */
  35421. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  35422. {
  35423. wc_test_ret_t ret;
  35424. word32 atrSz;
  35425. byte atr[256];
  35426. /* Additionally can look for fwWrappedFirmwareKey
  35427. * 1.2.840.113529.1.9.16.1.16 */
  35428. const unsigned char fwWrappedFirmwareKey[] = {
  35429. /* 0x06, 0x0B */
  35430. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35431. 0x01, 0x09, 0x10, 0x02, 0x27
  35432. };
  35433. /* find keyID in fwWrappedFirmwareKey */
  35434. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35435. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  35436. if (ret == LENGTH_ONLY_E) {
  35437. XMEMSET(atr, 0, sizeof(atr));
  35438. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35439. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  35440. /* keyIdRaw[0] OCTET TAG */
  35441. /* keyIdRaw[1] Length */
  35442. if (ret > 0) {
  35443. PKCS7* envPkcs7;
  35444. envPkcs7 = wc_PKCS7_New(NULL, 0);
  35445. if (envPkcs7 == NULL) {
  35446. return MEMORY_E;
  35447. }
  35448. wc_PKCS7_Init(envPkcs7, NULL, 0);
  35449. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  35450. if (ret == 0) {
  35451. /* expecting FIRMWARE_PKG_DATA content */
  35452. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  35453. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  35454. key, keySz);
  35455. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  35456. /* the contentOID should have been set to the inner
  35457. * FIRMWARE_PKG_DATA content */
  35458. ret = BAD_STATE_E;
  35459. }
  35460. }
  35461. wc_PKCS7_Free(envPkcs7);
  35462. }
  35463. }
  35464. return ret;
  35465. }
  35466. /* create a KEKRI enveloped data
  35467. * return size on success */
  35468. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  35469. word32 outSz)
  35470. {
  35471. wc_test_ret_t ret;
  35472. PKCS7* pkcs7;
  35473. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  35474. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35475. if (pkcs7 == NULL)
  35476. return WC_TEST_RET_ENC_ERRNO;
  35477. pkcs7->content = in;
  35478. pkcs7->contentSz = inSz;
  35479. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  35480. pkcs7->encryptOID = AES256CBCb;
  35481. pkcs7->ukm = NULL;
  35482. pkcs7->ukmSz = 0;
  35483. /* add recipient (KEKRI type) */
  35484. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  35485. sizeof(p7DefKey), (byte*)keyId,
  35486. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  35487. if (ret < 0) {
  35488. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  35489. wc_PKCS7_Free(pkcs7);
  35490. return WC_TEST_RET_ENC_EC(ret);
  35491. }
  35492. /* encode envelopedData, returns size */
  35493. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  35494. if (ret <= 0) {
  35495. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  35496. wc_PKCS7_Free(pkcs7);
  35497. return WC_TEST_RET_ENC_EC(ret);
  35498. }
  35499. wc_PKCS7_Free(pkcs7);
  35500. return ret;
  35501. }
  35502. /*
  35503. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  35504. * returns size of buffer output on success
  35505. */
  35506. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  35507. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  35508. byte* key, word32 keySz)
  35509. {
  35510. wc_test_ret_t ret;
  35511. int attribNum = 1;
  35512. PKCS7* pkcs7;
  35513. /* KEY ID
  35514. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  35515. */
  35516. const unsigned char fwDecryptKeyID[] = {
  35517. 0x06, 0x0B,
  35518. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35519. 0x01, 0x09, 0x10, 0x02, 0x25
  35520. };
  35521. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  35522. const unsigned char fwWrappedFirmwareKey[] = {
  35523. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35524. 0x01, 0x09, 0x10, 0x02, 0x27
  35525. };
  35526. byte keyID[] = { 0x04, 0x01, 0x00 };
  35527. byte env[256];
  35528. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  35529. PKCS7Attrib attribs[] =
  35530. {
  35531. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  35532. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  35533. };
  35534. keyID[2] = keyHint;
  35535. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  35536. if (keyHint == 0) {
  35537. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  35538. sizeof(env));
  35539. if (ret <= 0) {
  35540. return ret;
  35541. }
  35542. attribs[1].valueSz = (int)ret;
  35543. attribNum++;
  35544. }
  35545. /* init PKCS7 */
  35546. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35547. if (pkcs7 == NULL)
  35548. return WC_TEST_RET_ENC_ERRNO;
  35549. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  35550. if (ret != 0) {
  35551. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  35552. wc_PKCS7_Free(pkcs7);
  35553. return WC_TEST_RET_ENC_EC(ret);
  35554. }
  35555. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35556. if (ret != 0) {
  35557. wc_PKCS7_Free(pkcs7);
  35558. return WC_TEST_RET_ENC_EC(ret);
  35559. }
  35560. /* encode Signed Encrypted FirmwarePkgData */
  35561. if (encryptKeySz == 16) {
  35562. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35563. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  35564. (byte*)data, sizeof(data), NULL, 0,
  35565. attribs, attribNum, out, *outSz);
  35566. }
  35567. else {
  35568. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35569. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  35570. (byte*)data, sizeof(data), NULL, 0,
  35571. attribs, attribNum, out, *outSz);
  35572. }
  35573. if (ret <= 0) {
  35574. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  35575. "ret = %d\n", ret);
  35576. wc_PKCS7_Free(pkcs7);
  35577. return WC_TEST_RET_ENC_EC(ret);
  35578. } else {
  35579. *outSz = (int)ret;
  35580. }
  35581. wc_PKCS7_Free(pkcs7);
  35582. return ret;
  35583. }
  35584. /* test verification and decryption of PKCS7 bundle
  35585. * return 0 on success
  35586. */
  35587. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  35588. {
  35589. wc_test_ret_t ret = 0;
  35590. int usrCtx = 1; /* test value to pass as user context to callback */
  35591. PKCS7* pkcs7 = NULL;
  35592. byte* sid = NULL;
  35593. word32 sidSz;
  35594. byte key[256];
  35595. word32 keySz = sizeof(key);
  35596. byte *decoded = NULL;
  35597. int decodedSz = FOURK_BUF/2;
  35598. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  35599. #ifdef NO_SHA
  35600. #ifdef USE_CERT_BUFFERS_1024
  35601. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  35602. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  35603. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  35604. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  35605. #else
  35606. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  35607. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  35608. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  35609. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  35610. #endif
  35611. #else /* !NO_SHA */
  35612. #ifdef USE_CERT_BUFFERS_1024
  35613. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  35614. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  35615. 0x10, 0x69, 0x59, 0xec,
  35616. #else
  35617. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  35618. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  35619. 0xD7, 0x85, 0x65, 0xC0
  35620. #endif
  35621. #endif /* !NO_SHA */
  35622. };
  35623. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35624. if (decoded == NULL) {
  35625. ret = MEMORY_E;
  35626. goto out;
  35627. }
  35628. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  35629. if (pkcs7 == NULL) {
  35630. ret = MEMORY_E;
  35631. goto out;
  35632. }
  35633. /* Test verify */
  35634. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  35635. if (ret != 0)
  35636. goto out;
  35637. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35638. if (ret != 0)
  35639. goto out;
  35640. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  35641. if (ret != 0)
  35642. goto out;
  35643. /* Get size of SID and print it out */
  35644. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  35645. if (ret != LENGTH_ONLY_E)
  35646. goto out;
  35647. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35648. if (sid == NULL) {
  35649. ret = MEMORY_E;
  35650. goto out;
  35651. }
  35652. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  35653. if (ret != 0)
  35654. goto out;
  35655. ret = XMEMCMP(sid, expectedSid, sidSz);
  35656. if (ret != 0) {
  35657. ret = PKCS7_NO_SIGNER_E; /* close enough */
  35658. goto out;
  35659. }
  35660. /* get expected fwWrappedFirmwareKey */
  35661. if (keyHint == 0) {
  35662. ret = getFirmwareKey(pkcs7, key, keySz);
  35663. if (ret < 0)
  35664. goto out;
  35665. pkcs7->encryptionKey = key;
  35666. pkcs7->encryptionKeySz = (int)ret;
  35667. }
  35668. else {
  35669. decodedSz = PKCS7_BUF_SIZE;
  35670. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  35671. if (ret != 0)
  35672. goto out;
  35673. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  35674. if (ret != 0)
  35675. goto out;
  35676. }
  35677. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  35678. pkcs7->contentSz, decoded, decodedSz);
  35679. if (decodedSz < 0) {
  35680. ret = decodedSz;
  35681. goto out;
  35682. }
  35683. ret = 0;
  35684. out:
  35685. if (decoded)
  35686. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35687. if (pkcs7)
  35688. wc_PKCS7_Free(pkcs7);
  35689. if (sid)
  35690. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35691. return ret;
  35692. }
  35693. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  35694. {
  35695. wc_test_ret_t ret = 0;
  35696. word32 derSz;
  35697. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35698. if (! derBuf)
  35699. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35700. /* Doing default generation and verify */
  35701. derSz = FOURK_BUF;
  35702. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  35703. certSz, key, keySz);
  35704. if (ret <= 0) {
  35705. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35706. }
  35707. ret = verifyBundle(derBuf, derSz, 0);
  35708. if (ret != 0)
  35709. ERROR_OUT(ret, out);
  35710. /* test choosing other key with keyID */
  35711. derSz = FOURK_BUF;
  35712. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  35713. cert, certSz, key, keySz);
  35714. if (ret <= 0) {
  35715. ERROR_OUT(ret, out);
  35716. }
  35717. ret = verifyBundle(derBuf, derSz, 1);
  35718. if (ret != 0)
  35719. ERROR_OUT(ret, out);
  35720. /* test fail case with wrong keyID */
  35721. derSz = FOURK_BUF;
  35722. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  35723. cert, certSz, key, keySz);
  35724. if (ret <= 0) {
  35725. ERROR_OUT(ret, out);
  35726. }
  35727. ret = verifyBundle(derBuf, derSz, 1);
  35728. if (ret == 0) {
  35729. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35730. }
  35731. ret = 0;
  35732. out:
  35733. if (derBuf)
  35734. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35735. return ret;
  35736. }
  35737. #endif /* !NO_AES && HAVE_AES_CBC */
  35738. #ifndef NO_PKCS7_ENCRYPTED_DATA
  35739. typedef struct {
  35740. const byte* content;
  35741. word32 contentSz;
  35742. int contentOID;
  35743. int encryptOID;
  35744. byte* encryptionKey;
  35745. word32 encryptionKeySz;
  35746. PKCS7Attrib* attribs;
  35747. word32 attribsSz;
  35748. const char* outFileName;
  35749. } pkcs7EncryptedVector;
  35750. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  35751. {
  35752. wc_test_ret_t ret = 0;
  35753. int i, testSz;
  35754. int encryptedSz, decodedSz, attribIdx;
  35755. PKCS7* pkcs7;
  35756. byte *encrypted;
  35757. byte *decoded;
  35758. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35759. XFILE pkcs7File;
  35760. #endif
  35761. PKCS7Attrib* expectedAttrib;
  35762. PKCS7DecodedAttrib* decodedAttrib;
  35763. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35764. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35765. 0x72,0x6c,0x64
  35766. };
  35767. #ifndef NO_DES3
  35768. byte desKey[] = {
  35769. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  35770. };
  35771. byte des3Key[] = {
  35772. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  35773. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  35774. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  35775. };
  35776. #endif
  35777. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35778. #ifdef WOLFSSL_AES_128
  35779. byte aes128Key[] = {
  35780. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35781. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35782. };
  35783. #endif
  35784. #ifdef WOLFSSL_AES_192
  35785. byte aes192Key[] = {
  35786. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35788. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35789. };
  35790. #endif
  35791. #ifdef WOLFSSL_AES_256
  35792. byte aes256Key[] = {
  35793. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35794. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35795. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35796. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35797. };
  35798. #endif
  35799. #ifdef WOLFSSL_AES_256
  35800. /* Attribute example from RFC 4134, Section 7.2
  35801. * OID = 1.2.5555
  35802. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  35803. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  35804. static byte genAttr[] = { 0x04, 47,
  35805. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35806. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35807. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35808. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35809. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35810. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  35811. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  35812. static byte genAttr2[] = { 0x04, 47,
  35813. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35814. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35815. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35816. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35817. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35818. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  35819. PKCS7Attrib attribs[] =
  35820. {
  35821. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  35822. };
  35823. PKCS7Attrib multiAttribs[] =
  35824. {
  35825. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  35826. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  35827. };
  35828. #endif
  35829. #endif /* NO_AES */
  35830. const pkcs7EncryptedVector testVectors[] =
  35831. {
  35832. #ifndef NO_DES3
  35833. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  35834. NULL, 0, "pkcs7encryptedDataDES3.der"},
  35835. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  35836. NULL, 0, "pkcs7encryptedDataDES.der"},
  35837. #endif /* NO_DES3 */
  35838. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35839. #ifdef WOLFSSL_AES_128
  35840. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  35841. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  35842. #endif
  35843. #ifdef WOLFSSL_AES_192
  35844. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  35845. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  35846. #endif
  35847. #ifdef WOLFSSL_AES_256
  35848. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35849. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  35850. /* test with optional unprotected attributes */
  35851. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35852. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35853. "pkcs7encryptedDataAES256CBC_attribs.der"},
  35854. /* test with multiple optional unprotected attributes */
  35855. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35856. sizeof(aes256Key), multiAttribs,
  35857. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  35858. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  35859. /* test with contentType set to FirmwarePkgData */
  35860. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  35861. sizeof(aes256Key), NULL, 0,
  35862. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  35863. #endif
  35864. #endif /* !NO_AES && HAVE_AES_CBC */
  35865. };
  35866. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35867. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35868. if ((! encrypted) || (! decoded)) {
  35869. ERROR_OUT(MEMORY_E, out);
  35870. }
  35871. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  35872. for (i = 0; i < testSz; i++) {
  35873. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35874. if (pkcs7 == NULL) {
  35875. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35876. }
  35877. pkcs7->content = (byte*)testVectors[i].content;
  35878. pkcs7->contentSz = testVectors[i].contentSz;
  35879. pkcs7->contentOID = testVectors[i].contentOID;
  35880. pkcs7->encryptOID = testVectors[i].encryptOID;
  35881. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  35882. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  35883. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  35884. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  35885. /* encode encryptedData */
  35886. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  35887. PKCS7_BUF_SIZE);
  35888. if (encryptedSz <= 0) {
  35889. wc_PKCS7_Free(pkcs7);
  35890. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  35891. }
  35892. /* decode encryptedData */
  35893. #ifndef NO_PKCS7_STREAM
  35894. { /* test reading byte by byte */
  35895. int z;
  35896. for (z = 0; z < encryptedSz; z++) {
  35897. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  35898. decoded, PKCS7_BUF_SIZE);
  35899. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35900. printf("unexpected error %d\n", decodedSz);
  35901. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35902. }
  35903. }
  35904. /* test decode result */
  35905. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35906. printf("stream read failed\n");
  35907. wc_PKCS7_Free(pkcs7);
  35908. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35909. }
  35910. }
  35911. #endif
  35912. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  35913. decoded, PKCS7_BUF_SIZE);
  35914. if (decodedSz <= 0){
  35915. wc_PKCS7_Free(pkcs7);
  35916. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35917. }
  35918. /* test decode result */
  35919. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35920. wc_PKCS7_Free(pkcs7);
  35921. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35922. }
  35923. /* verify decoded unprotected attributes */
  35924. if (pkcs7->decodedAttrib != NULL) {
  35925. decodedAttrib = pkcs7->decodedAttrib;
  35926. attribIdx = 1;
  35927. while (decodedAttrib != NULL) {
  35928. /* expected attribute, stored list is reversed */
  35929. expectedAttrib = &(pkcs7->unprotectedAttribs
  35930. [pkcs7->unprotectedAttribsSz - attribIdx]);
  35931. /* verify oid */
  35932. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  35933. decodedAttrib->oidSz) != 0) {
  35934. wc_PKCS7_Free(pkcs7);
  35935. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35936. }
  35937. /* verify value */
  35938. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  35939. decodedAttrib->valueSz) != 0) {
  35940. wc_PKCS7_Free(pkcs7);
  35941. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35942. }
  35943. decodedAttrib = decodedAttrib->next;
  35944. attribIdx++;
  35945. }
  35946. }
  35947. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35948. /* output pkcs7 envelopedData for external testing */
  35949. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35950. if (!pkcs7File) {
  35951. wc_PKCS7_Free(pkcs7);
  35952. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35953. }
  35954. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  35955. if (ret < 0)
  35956. ret = WC_TEST_RET_ENC_ERRNO;
  35957. else
  35958. ret = 0;
  35959. XFCLOSE(pkcs7File);
  35960. #endif
  35961. wc_PKCS7_Free(pkcs7);
  35962. }
  35963. out:
  35964. if (encrypted)
  35965. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35966. if (decoded)
  35967. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35968. return ret;
  35969. }
  35970. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  35971. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  35972. typedef struct {
  35973. const byte* content;
  35974. word32 contentSz;
  35975. int contentOID;
  35976. const char* outFileName;
  35977. } pkcs7CompressedVector;
  35978. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  35979. {
  35980. wc_test_ret_t ret = 0;
  35981. int i, testSz;
  35982. int compressedSz, decodedSz;
  35983. PKCS7* pkcs7;
  35984. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35985. byte *compressed;
  35986. byte *decoded;
  35987. #else
  35988. byte compressed[PKCS7_BUF_SIZE];
  35989. byte decoded[PKCS7_BUF_SIZE];
  35990. #endif
  35991. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35992. XFILE pkcs7File;
  35993. #endif
  35994. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35995. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35996. 0x72,0x6c,0x64
  35997. };
  35998. const pkcs7CompressedVector testVectors[] =
  35999. {
  36000. {data, (word32)sizeof(data), DATA,
  36001. "pkcs7compressedData_data_zlib.der"},
  36002. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  36003. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  36004. };
  36005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36006. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36007. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36008. if ((! compressed) || (! decoded)) {
  36009. ERROR_OUT(MEMORY_E, out);
  36010. }
  36011. #endif
  36012. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  36013. for (i = 0; i < testSz; i++) {
  36014. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36015. if (pkcs7 == NULL) {
  36016. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36017. }
  36018. pkcs7->content = (byte*)testVectors[i].content;
  36019. pkcs7->contentSz = testVectors[i].contentSz;
  36020. pkcs7->contentOID = testVectors[i].contentOID;
  36021. /* encode compressedData */
  36022. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  36023. PKCS7_BUF_SIZE);
  36024. if (compressedSz <= 0) {
  36025. wc_PKCS7_Free(pkcs7);
  36026. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  36027. }
  36028. /* decode compressedData */
  36029. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  36030. compressedSz, decoded,
  36031. PKCS7_BUF_SIZE);
  36032. if (decodedSz <= 0){
  36033. wc_PKCS7_Free(pkcs7);
  36034. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  36035. }
  36036. /* test decode result */
  36037. if (XMEMCMP(decoded, testVectors[i].content,
  36038. testVectors[i].contentSz) != 0) {
  36039. wc_PKCS7_Free(pkcs7);
  36040. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36041. }
  36042. /* make sure content type is the same */
  36043. if (testVectors[i].contentOID != pkcs7->contentOID) {
  36044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36045. }
  36046. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36047. /* output pkcs7 compressedData for external testing */
  36048. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  36049. if (!pkcs7File) {
  36050. wc_PKCS7_Free(pkcs7);
  36051. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36052. }
  36053. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  36054. if (ret < 0)
  36055. ret = WC_TEST_RET_ENC_ERRNO;
  36056. else
  36057. ret = 0;
  36058. XFCLOSE(pkcs7File);
  36059. #endif
  36060. wc_PKCS7_Free(pkcs7);
  36061. }
  36062. out:
  36063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36064. if (compressed)
  36065. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36066. if (decoded)
  36067. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36068. #endif
  36069. return ret;
  36070. } /* pkcs7compressed_test() */
  36071. #undef PKCS7_BUF_SIZE
  36072. #endif /* HAVE_LIBZ */
  36073. typedef struct {
  36074. const byte* content;
  36075. word32 contentSz;
  36076. int hashOID;
  36077. int signOID;
  36078. byte* privateKey;
  36079. word32 privateKeySz;
  36080. byte* cert;
  36081. size_t certSz;
  36082. byte* caCert;
  36083. size_t caCertSz;
  36084. PKCS7Attrib* signedAttribs;
  36085. word32 signedAttribsSz;
  36086. const char* outFileName;
  36087. int contentOID;
  36088. byte* contentType;
  36089. word32 contentTypeSz;
  36090. int sidType;
  36091. int encryptOID; /* for single-shot encrypt alg OID */
  36092. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  36093. byte* encryptKey; /* for single-shot, encryptedData */
  36094. word32 encryptKeySz; /* for single-shot, encryptedData */
  36095. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  36096. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  36097. word16 detachedSignature; /* generate detached signature (0:1) */
  36098. } pkcs7SignedVector;
  36099. static wc_test_ret_t pkcs7signed_run_vectors(
  36100. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36101. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36102. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36103. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36104. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36105. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36106. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36107. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36108. {
  36109. wc_test_ret_t ret;
  36110. int testSz = 0, i;
  36111. int encodedSz;
  36112. byte* out = NULL;
  36113. word32 outSz;
  36114. WC_RNG rng;
  36115. PKCS7* pkcs7 = NULL;
  36116. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36117. XFILE file;
  36118. #endif
  36119. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36120. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36121. 0x72,0x6c,0x64
  36122. };
  36123. static byte transIdOid[] =
  36124. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36125. 0x09, 0x07 };
  36126. static byte messageTypeOid[] =
  36127. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36128. 0x09, 0x02 };
  36129. static byte senderNonceOid[] =
  36130. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36131. 0x09, 0x05 };
  36132. #ifndef NO_SHA
  36133. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36134. #else
  36135. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36136. #endif
  36137. static byte messageType[] = { 0x13, 2, '1', '9' };
  36138. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  36139. static PKCS7Attrib attribs[] =
  36140. {
  36141. { transIdOid, sizeof(transIdOid), transId,
  36142. sizeof(transId) - 1 }, /* take off the null */
  36143. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36144. sizeof(messageType) },
  36145. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  36146. sizeof(senderNonce) }
  36147. };
  36148. /* for testing custom contentType, FirmwarePkgData */
  36149. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  36150. 0x48, 0x86, 0xF7, 0x0D,
  36151. 0x01, 0x09, 0x10, 0x01, 0x10 };
  36152. #define MAX_TESTVECTORS_LEN 20
  36153. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36154. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36155. if (testSz == MAX_TESTVECTORS_LEN) { \
  36156. ret = WC_TEST_RET_ENC_NC; \
  36157. goto out; \
  36158. } \
  36159. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  36160. sizeof _this_vector); \
  36161. }
  36162. pkcs7SignedVector *testVectors = NULL;
  36163. XMEMSET(&rng, 0, sizeof(rng));
  36164. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36165. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36166. if (testVectors == NULL) {
  36167. ret = WC_TEST_RET_ENC_ERRNO;
  36168. goto out;
  36169. }
  36170. {
  36171. #ifndef NO_RSA
  36172. #ifndef NO_SHA
  36173. /* RSA with SHA */
  36174. ADD_PKCS7SIGNEDVECTOR(
  36175. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  36176. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36177. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36178. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  36179. 0, 0);
  36180. /* RSA with SHA, no signed attributes */
  36181. ADD_PKCS7SIGNEDVECTOR(
  36182. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  36183. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  36184. NULL, 0, NULL, 0,
  36185. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36186. NULL, 0, 0);
  36187. #endif
  36188. #ifdef WOLFSSL_SHA224
  36189. /* RSA with SHA224 */
  36190. ADD_PKCS7SIGNEDVECTOR(
  36191. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  36192. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36193. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36194. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36195. NULL, 0, 0);
  36196. #endif
  36197. #ifndef NO_SHA256
  36198. /* RSA with SHA256 */
  36199. ADD_PKCS7SIGNEDVECTOR(
  36200. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36201. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36202. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36203. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36204. NULL, 0, 0);
  36205. /* RSA with SHA256, detached signature */
  36206. ADD_PKCS7SIGNEDVECTOR(
  36207. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36208. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36209. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36210. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  36211. NULL, 0, NULL, 0, 1);
  36212. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  36213. ADD_PKCS7SIGNEDVECTOR(
  36214. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36215. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36216. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36217. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  36218. NULL, 0, NULL, 0, 0);
  36219. /* RSA with SHA256 and custom contentType */
  36220. ADD_PKCS7SIGNEDVECTOR(
  36221. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36222. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36223. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36224. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  36225. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  36226. NULL, 0, 0);
  36227. /* RSA with SHA256 and FirmwarePkgData contentType */
  36228. ADD_PKCS7SIGNEDVECTOR(
  36229. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36230. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36231. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36232. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  36233. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36234. /* RSA with SHA256 using server cert and ca cert */
  36235. ADD_PKCS7SIGNEDVECTOR(
  36236. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36237. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36238. rsaCaCertBuf, rsaCaCertBufSz,
  36239. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36240. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  36241. NULL, 0, NULL, 0, 0);
  36242. #endif
  36243. #if defined(WOLFSSL_SHA384)
  36244. /* RSA with SHA384 */
  36245. ADD_PKCS7SIGNEDVECTOR(
  36246. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  36247. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36248. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36249. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36250. NULL, 0, 0);
  36251. #endif
  36252. #if defined(WOLFSSL_SHA512)
  36253. /* RSA with SHA512 */
  36254. ADD_PKCS7SIGNEDVECTOR(
  36255. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  36256. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36257. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36258. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36259. NULL, 0, 0);
  36260. #endif
  36261. #endif /* NO_RSA */
  36262. #ifdef HAVE_ECC
  36263. #ifndef NO_SHA
  36264. /* ECDSA with SHA */
  36265. ADD_PKCS7SIGNEDVECTOR(
  36266. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  36267. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36268. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36269. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36270. NULL, 0, 0);
  36271. /* ECDSA with SHA, no signed attributes */
  36272. ADD_PKCS7SIGNEDVECTOR(
  36273. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  36274. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  36275. NULL, 0, NULL, 0,
  36276. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36277. NULL, 0, 0);
  36278. #endif
  36279. #ifdef WOLFSSL_SHA224
  36280. /* ECDSA with SHA224 */
  36281. ADD_PKCS7SIGNEDVECTOR(
  36282. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  36283. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36284. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36285. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36286. NULL, 0, 0);
  36287. #endif
  36288. #ifndef NO_SHA256
  36289. /* ECDSA with SHA256 */
  36290. ADD_PKCS7SIGNEDVECTOR(
  36291. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36292. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36293. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36294. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36295. NULL, 0, 0);
  36296. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  36297. ADD_PKCS7SIGNEDVECTOR(
  36298. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36299. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36300. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36301. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  36302. NULL, 0, NULL, 0, 0);
  36303. /* ECDSA with SHA256 and custom contentType */
  36304. ADD_PKCS7SIGNEDVECTOR(
  36305. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36306. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36307. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36308. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  36309. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  36310. NULL, 0, 0);
  36311. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  36312. ADD_PKCS7SIGNEDVECTOR(
  36313. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36314. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36315. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36316. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  36317. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36318. #endif
  36319. #ifdef WOLFSSL_SHA384
  36320. /* ECDSA with SHA384 */
  36321. ADD_PKCS7SIGNEDVECTOR(
  36322. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  36323. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36324. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36325. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36326. NULL, 0, 0);
  36327. #endif
  36328. #ifdef WOLFSSL_SHA512
  36329. /* ECDSA with SHA512 */
  36330. ADD_PKCS7SIGNEDVECTOR(
  36331. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  36332. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36333. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36334. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36335. NULL, 0, 0);
  36336. #endif
  36337. #endif /* HAVE_ECC */
  36338. };
  36339. #undef MAX_TESTVECTORS_LEN
  36340. #undef ADD_PKCS7SIGNEDVECTOR
  36341. outSz = FOURK_BUF;
  36342. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36343. if (out == NULL)
  36344. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36345. XMEMSET(out, 0, outSz);
  36346. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36347. if (ret < 0)
  36348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36349. #ifndef HAVE_FIPS
  36350. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36351. #else
  36352. ret = wc_InitRng(&rng);
  36353. #endif
  36354. if (ret != 0)
  36355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36356. for (i = 0; i < testSz; i++) {
  36357. if (pkcs7)
  36358. wc_PKCS7_Free(pkcs7);
  36359. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36360. if (pkcs7 == NULL)
  36361. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36362. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36363. (word32)testVectors[i].certSz);
  36364. if (ret != 0)
  36365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36366. /* load CA certificate, if present */
  36367. if (testVectors[i].caCert != NULL) {
  36368. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36369. (word32)testVectors[i].caCertSz);
  36370. if (ret != 0)
  36371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36372. }
  36373. pkcs7->rng = &rng;
  36374. pkcs7->content = (byte*)testVectors[i].content;
  36375. pkcs7->contentSz = testVectors[i].contentSz;
  36376. pkcs7->contentOID = testVectors[i].contentOID;
  36377. pkcs7->hashOID = testVectors[i].hashOID;
  36378. pkcs7->encryptOID = testVectors[i].signOID;
  36379. pkcs7->privateKey = testVectors[i].privateKey;
  36380. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  36381. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  36382. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  36383. /* optional custom contentType, default is DATA,
  36384. overrides contentOID if set */
  36385. if (testVectors[i].contentType != NULL) {
  36386. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  36387. testVectors[i].contentTypeSz);
  36388. if (ret != 0)
  36389. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36390. }
  36391. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36392. default is IssuerAndSerialNumber */
  36393. if (testVectors[i].sidType == CMS_SKID) {
  36394. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36395. if (ret != 0)
  36396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36397. }
  36398. /* generate senderNonce */
  36399. {
  36400. senderNonce[0] = 0x04;
  36401. senderNonce[1] = PKCS7_NONCE_SZ;
  36402. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  36403. if (ret != 0)
  36404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36405. }
  36406. /* generate transactionID (used with SCEP) */
  36407. {
  36408. #ifndef NO_SHA
  36409. wc_Sha sha;
  36410. byte digest[WC_SHA_DIGEST_SIZE];
  36411. #else
  36412. wc_Sha256 sha;
  36413. byte digest[WC_SHA256_DIGEST_SIZE];
  36414. #endif
  36415. int j,k;
  36416. transId[0] = 0x13;
  36417. transId[1] = sizeof(digest) * 2;
  36418. #ifndef NO_SHA
  36419. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  36420. if (ret != 0)
  36421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36422. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36423. wc_ShaFinal(&sha, digest);
  36424. wc_ShaFree(&sha);
  36425. #else
  36426. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  36427. if (ret != 0)
  36428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36429. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36430. wc_Sha256Final(&sha, digest);
  36431. wc_Sha256Free(&sha);
  36432. #endif
  36433. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  36434. #if defined(WOLF_C89)
  36435. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  36436. #else
  36437. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  36438. #endif
  36439. }
  36440. }
  36441. /* enable detached signature generation, if set */
  36442. if (testVectors[i].detachedSignature == 1) {
  36443. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  36444. if (ret != 0)
  36445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36446. }
  36447. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  36448. if (encodedSz < 0)
  36449. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36450. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36451. /* write PKCS#7 to output file for more testing */
  36452. file = XFOPEN(testVectors[i].outFileName, "wb");
  36453. if (!file) {
  36454. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36455. }
  36456. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36457. XFCLOSE(file);
  36458. if (ret != (int)encodedSz)
  36459. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36460. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36461. wc_PKCS7_Free(pkcs7);
  36462. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36463. if (pkcs7 == NULL)
  36464. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36465. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36466. if (testVectors[i].detachedSignature == 1) {
  36467. /* set content for verifying detached signatures */
  36468. pkcs7->content = (byte*)testVectors[i].content;
  36469. pkcs7->contentSz = testVectors[i].contentSz;
  36470. }
  36471. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36472. if (ret < 0)
  36473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36474. /* verify contentType extracted successfully for custom content types */
  36475. if (testVectors[i].contentTypeSz > 0) {
  36476. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  36477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36478. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  36479. pkcs7->contentTypeSz) != 0) {
  36480. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36481. }
  36482. }
  36483. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36484. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36485. {
  36486. /* check getting signed attributes */
  36487. #ifndef NO_SHA
  36488. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36489. #else
  36490. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36491. #endif
  36492. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  36493. int oidSz = (int)sizeof(transIdOid) - 2;
  36494. int bufSz = 0;
  36495. if (testVectors[i].signedAttribs != NULL) {
  36496. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36497. NULL, (word32*)&bufSz);
  36498. if (ret != LENGTH_ONLY_E)
  36499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36500. ret = 0;
  36501. }
  36502. if (bufSz > (int)sizeof(buf))
  36503. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36504. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36505. buf, (word32*)&bufSz);
  36506. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  36507. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  36508. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36509. }
  36510. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36511. file = XFOPEN("./pkcs7cert.der", "wb");
  36512. if (!file)
  36513. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36514. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  36515. if (ret < 0)
  36516. ret = WC_TEST_RET_ENC_ERRNO;
  36517. else
  36518. ret = 0;
  36519. XFCLOSE(file);
  36520. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36521. }
  36522. out:
  36523. if (pkcs7 != NULL)
  36524. wc_PKCS7_Free(pkcs7);
  36525. if (out != NULL)
  36526. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36527. if (testVectors != NULL)
  36528. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36529. wc_FreeRng(&rng);
  36530. if (ret > 0)
  36531. return 0;
  36532. (void)rsaClientCertBuf;
  36533. (void)rsaClientCertBufSz;
  36534. (void)rsaClientPrivKeyBuf;
  36535. (void)rsaClientPrivKeyBufSz;
  36536. (void)rsaServerCertBuf;
  36537. (void)rsaServerCertBufSz;
  36538. (void)rsaServerPrivKeyBuf;
  36539. (void)rsaServerPrivKeyBufSz;
  36540. (void)rsaCaCertBuf;
  36541. (void)rsaCaCertBufSz;
  36542. (void)rsaCaPrivKeyBuf;
  36543. (void)rsaCaPrivKeyBufSz;
  36544. (void)eccClientCertBuf;
  36545. (void)eccClientCertBufSz;
  36546. (void)eccClientPrivKeyBuf;
  36547. (void)eccClientPrivKeyBufSz;
  36548. return ret;
  36549. }
  36550. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  36551. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36552. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36553. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36554. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36555. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36556. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36557. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36558. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36559. {
  36560. wc_test_ret_t ret;
  36561. int testSz = 0, i;
  36562. int encodedSz;
  36563. byte* out = NULL;
  36564. word32 outSz;
  36565. WC_RNG rng;
  36566. PKCS7* pkcs7 = NULL;
  36567. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36568. XFILE file;
  36569. #endif
  36570. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36571. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36572. byte* encryptedTmp = NULL;
  36573. int encryptedTmpSz;
  36574. #endif
  36575. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36576. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36577. 0x72,0x6c,0x64
  36578. };
  36579. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36580. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36581. static byte aes256Key[] = {
  36582. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36583. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36584. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36585. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36586. };
  36587. #endif
  36588. static byte messageTypeOid[] =
  36589. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36590. 0x09, 0x02 };
  36591. static byte messageType[] = { 0x13, 2, '1', '9' };
  36592. PKCS7Attrib attribs[] =
  36593. {
  36594. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36595. sizeof(messageType) },
  36596. };
  36597. #define MAX_TESTVECTORS_LEN 19
  36598. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36599. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36600. if (testSz == MAX_TESTVECTORS_LEN) { \
  36601. ret = WC_TEST_RET_ENC_NC; \
  36602. goto out; \
  36603. } \
  36604. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  36605. sizeof _this_vector); \
  36606. }
  36607. pkcs7SignedVector *testVectors = NULL;
  36608. XMEMSET(&rng, 0, sizeof(rng));
  36609. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36610. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36611. if (testVectors == NULL) {
  36612. ret = WC_TEST_RET_ENC_ERRNO;
  36613. goto out;
  36614. }
  36615. {
  36616. #ifndef NO_RSA
  36617. #ifndef NO_SHA256
  36618. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  36619. ADD_PKCS7SIGNEDVECTOR(
  36620. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36621. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36622. NULL, 0,
  36623. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  36624. 0, 0, NULL, 0, NULL, 0, 0);
  36625. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  36626. ADD_PKCS7SIGNEDVECTOR(
  36627. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36628. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36629. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36630. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  36631. NULL, 0, NULL, 0, 0);
  36632. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  36633. ADD_PKCS7SIGNEDVECTOR(
  36634. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36635. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36636. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36637. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  36638. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36639. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  36640. ADD_PKCS7SIGNEDVECTOR(
  36641. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36642. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36643. rsaCaCertBuf, rsaCaCertBufSz,
  36644. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36645. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  36646. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36647. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36648. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36649. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  36650. ADD_PKCS7SIGNEDVECTOR(
  36651. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36652. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36653. NULL, 0,
  36654. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36655. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36656. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  36657. ADD_PKCS7SIGNEDVECTOR(
  36658. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36659. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36660. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36661. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  36662. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36663. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36664. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36665. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36666. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  36667. ADD_PKCS7SIGNEDVECTOR(
  36668. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36669. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36670. NULL, 0,
  36671. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36672. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36673. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  36674. ADD_PKCS7SIGNEDVECTOR(
  36675. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36676. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36677. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36678. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  36679. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36680. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36681. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36682. no attribs */
  36683. ADD_PKCS7SIGNEDVECTOR(
  36684. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36685. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36686. NULL, 0,
  36687. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  36688. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36689. 0, 0);
  36690. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36691. attribs */
  36692. ADD_PKCS7SIGNEDVECTOR(
  36693. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36694. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36695. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36696. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  36697. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36698. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36699. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36700. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36701. #endif /* NO_SHA256 */
  36702. #endif /* NO_RSA */
  36703. #ifdef HAVE_ECC
  36704. #ifndef NO_SHA256
  36705. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36706. ADD_PKCS7SIGNEDVECTOR(
  36707. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36708. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36709. NULL, 0,
  36710. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36711. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36712. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  36713. ADD_PKCS7SIGNEDVECTOR(
  36714. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36715. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36716. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36717. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36718. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36719. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  36720. ADD_PKCS7SIGNEDVECTOR(
  36721. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36722. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36723. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36724. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  36725. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36726. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36727. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36728. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  36729. ADD_PKCS7SIGNEDVECTOR(
  36730. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36731. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36732. NULL, 0,
  36733. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36734. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36735. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  36736. ADD_PKCS7SIGNEDVECTOR(
  36737. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36738. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36739. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36740. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36741. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36742. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36743. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36744. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36745. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36746. ADD_PKCS7SIGNEDVECTOR(
  36747. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36748. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36749. NULL, 0,
  36750. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36751. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36752. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  36753. ADD_PKCS7SIGNEDVECTOR(
  36754. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36755. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36756. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36757. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36758. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36759. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36760. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36761. no attribs */
  36762. ADD_PKCS7SIGNEDVECTOR(
  36763. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36764. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36765. NULL, 0,
  36766. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  36767. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36768. 0, 0);
  36769. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36770. attribs */
  36771. ADD_PKCS7SIGNEDVECTOR(
  36772. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36773. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36774. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36775. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  36776. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36777. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36778. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36779. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36780. #endif /* NO_SHA256 */
  36781. #endif /* HAVE_ECC */
  36782. };
  36783. #undef MAX_TESTVECTORS_LEN
  36784. #undef ADD_PKCS7SIGNEDVECTOR
  36785. outSz = FOURK_BUF;
  36786. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36787. if (out == NULL)
  36788. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36789. XMEMSET(out, 0, outSz);
  36790. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36791. if (ret < 0)
  36792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36793. #ifndef HAVE_FIPS
  36794. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36795. #else
  36796. ret = wc_InitRng(&rng);
  36797. #endif
  36798. if (ret != 0)
  36799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36800. for (i = 0; i < testSz; i++) {
  36801. if (pkcs7)
  36802. wc_PKCS7_Free(pkcs7);
  36803. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36804. if (pkcs7 == NULL)
  36805. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36806. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36807. (word32)testVectors[i].certSz);
  36808. if (ret != 0)
  36809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36810. /* load CA certificate, if present */
  36811. if (testVectors[i].caCert != NULL) {
  36812. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36813. (word32)testVectors[i].caCertSz);
  36814. if (ret != 0)
  36815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36816. }
  36817. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36818. default is IssuerAndSerialNumber */
  36819. if (testVectors[i].sidType == CMS_SKID) {
  36820. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36821. if (ret != 0)
  36822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36823. }
  36824. if (testVectors[i].encCompFlag == 0) {
  36825. /* encode Signed FirmwarePkgData */
  36826. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  36827. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36828. testVectors[i].signOID, testVectors[i].hashOID,
  36829. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36830. testVectors[i].signedAttribs,
  36831. testVectors[i].signedAttribsSz, out, outSz);
  36832. if (encodedSz < 0)
  36833. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36834. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36835. } else if (testVectors[i].encCompFlag == 1) {
  36836. /* encode Signed Encrypted FirmwarePkgData */
  36837. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  36838. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36839. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36840. testVectors[i].encryptOID, testVectors[i].signOID,
  36841. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36842. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36843. testVectors[i].unprotectedAttribsSz,
  36844. testVectors[i].signedAttribs,
  36845. testVectors[i].signedAttribsSz, out, outSz);
  36846. if (encodedSz <= 0)
  36847. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36848. #endif
  36849. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36850. } else if (testVectors[i].encCompFlag == 2) {
  36851. /* encode Signed Compressed FirmwarePkgData */
  36852. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  36853. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36854. testVectors[i].signOID, testVectors[i].hashOID,
  36855. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36856. testVectors[i].signedAttribs,
  36857. testVectors[i].signedAttribsSz, out, outSz);
  36858. if (encodedSz <= 0)
  36859. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36860. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36861. } else if (testVectors[i].encCompFlag == 3) {
  36862. /* encode Signed Encrypted Compressed FirmwarePkgData */
  36863. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  36864. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36865. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36866. testVectors[i].encryptOID, testVectors[i].signOID,
  36867. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36868. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36869. testVectors[i].unprotectedAttribsSz,
  36870. testVectors[i].signedAttribs,
  36871. testVectors[i].signedAttribsSz, out, outSz);
  36872. if (encodedSz <= 0)
  36873. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36874. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36875. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36876. } else {
  36877. /* unsupported SignedData single-shot combination */
  36878. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36879. }
  36880. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36881. /* write PKCS#7 to output file for more testing */
  36882. file = XFOPEN(testVectors[i].outFileName, "wb");
  36883. if (!file)
  36884. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36885. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36886. XFCLOSE(file);
  36887. file = NULL;
  36888. if (ret != (int)encodedSz)
  36889. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36890. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36891. wc_PKCS7_Free(pkcs7);
  36892. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36893. if (pkcs7 == NULL)
  36894. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36895. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36896. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36897. if (ret < 0)
  36898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36899. #ifndef NO_PKCS7_STREAM
  36900. {
  36901. word32 z;
  36902. for (z = 0; z < outSz && ret != 0; z++) {
  36903. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  36904. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  36905. printf("unexpected error %d\n", ret);
  36906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36907. }
  36908. }
  36909. }
  36910. #endif
  36911. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36912. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36913. if (testVectors[i].encCompFlag == 0) {
  36914. /* verify decoded content matches expected */
  36915. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  36916. XMEMCMP(pkcs7->content, testVectors[i].content,
  36917. pkcs7->contentSz)) {
  36918. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36919. }
  36920. }
  36921. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36922. else if (testVectors[i].encCompFlag == 1) {
  36923. /* decrypt inner encryptedData */
  36924. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36925. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36926. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36927. pkcs7->contentSz, out, outSz);
  36928. if (ret < 0)
  36929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36930. /* compare decrypted to expected */
  36931. if (((word32)ret != testVectors[i].contentSz) ||
  36932. XMEMCMP(out, testVectors[i].content, ret))
  36933. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36934. }
  36935. #endif
  36936. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36937. else if (testVectors[i].encCompFlag == 2) {
  36938. /* decompress inner compressedData */
  36939. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  36940. pkcs7->contentSz, out, outSz);
  36941. if (ret < 0)
  36942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36943. /* compare decompressed to expected */
  36944. if (((word32)ret != testVectors[i].contentSz) ||
  36945. XMEMCMP(out, testVectors[i].content, ret))
  36946. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36947. }
  36948. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36949. else if (testVectors[i].encCompFlag == 3) {
  36950. encryptedTmpSz = FOURK_BUF;
  36951. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  36952. DYNAMIC_TYPE_TMP_BUFFER);
  36953. if (encryptedTmp == NULL)
  36954. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36955. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  36956. /* decrypt inner encryptedData */
  36957. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36958. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36959. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36960. pkcs7->contentSz, encryptedTmp,
  36961. encryptedTmpSz);
  36962. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  36963. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36964. /* decompress inner compressedData */
  36965. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  36966. encryptedTmpSz, out, outSz);
  36967. if (ret < 0)
  36968. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36969. /* compare decompressed to expected */
  36970. if (((word32)ret != testVectors[i].contentSz) ||
  36971. XMEMCMP(out, testVectors[i].content, ret))
  36972. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36973. }
  36974. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36975. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36976. }
  36977. out:
  36978. if (pkcs7 != NULL)
  36979. wc_PKCS7_Free(pkcs7);
  36980. if (out != NULL)
  36981. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36982. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36983. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36984. if (encryptedTmp != NULL)
  36985. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36986. #endif
  36987. if (testVectors != NULL)
  36988. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36989. wc_FreeRng(&rng);
  36990. if (ret > 0)
  36991. return 0;
  36992. (void)eccClientCertBuf;
  36993. (void)eccClientCertBufSz;
  36994. (void)eccClientPrivKeyBuf;
  36995. (void)eccClientPrivKeyBufSz;
  36996. (void)rsaClientCertBuf;
  36997. (void)rsaClientCertBufSz;
  36998. (void)rsaClientPrivKeyBuf;
  36999. (void)rsaClientPrivKeyBufSz;
  37000. (void)rsaServerCertBuf;
  37001. (void)rsaServerCertBufSz;
  37002. (void)rsaServerPrivKeyBuf;
  37003. (void)rsaServerPrivKeyBufSz;
  37004. (void)rsaCaCertBuf;
  37005. (void)rsaCaCertBufSz;
  37006. (void)rsaCaPrivKeyBuf;
  37007. (void)rsaCaPrivKeyBufSz;
  37008. return ret;
  37009. }
  37010. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  37011. {
  37012. wc_test_ret_t ret = 0;
  37013. byte* rsaClientCertBuf = NULL;
  37014. byte* rsaServerCertBuf = NULL;
  37015. byte* rsaCaCertBuf = NULL;
  37016. byte* eccClientCertBuf = NULL;
  37017. byte* rsaClientPrivKeyBuf = NULL;
  37018. byte* rsaServerPrivKeyBuf = NULL;
  37019. byte* rsaCaPrivKeyBuf = NULL;
  37020. byte* eccClientPrivKeyBuf = NULL;
  37021. word32 rsaClientCertBufSz = 0;
  37022. word32 rsaServerCertBufSz = 0;
  37023. word32 rsaCaCertBufSz = 0;
  37024. word32 eccClientCertBufSz = 0;
  37025. word32 rsaClientPrivKeyBufSz = 0;
  37026. word32 rsaServerPrivKeyBufSz = 0;
  37027. word32 rsaCaPrivKeyBufSz = 0;
  37028. word32 eccClientPrivKeyBufSz = 0;
  37029. #ifndef NO_RSA
  37030. /* read client RSA cert and key in DER format */
  37031. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37032. DYNAMIC_TYPE_TMP_BUFFER);
  37033. if (rsaClientCertBuf == NULL)
  37034. ret = WC_TEST_RET_ENC_NC;
  37035. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37036. DYNAMIC_TYPE_TMP_BUFFER);
  37037. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  37038. ret = WC_TEST_RET_ENC_ERRNO;
  37039. }
  37040. rsaClientCertBufSz = FOURK_BUF;
  37041. rsaClientPrivKeyBufSz = FOURK_BUF;
  37042. /* read server RSA cert and key in DER format */
  37043. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37044. DYNAMIC_TYPE_TMP_BUFFER);
  37045. if (ret == 0 && rsaServerCertBuf == NULL)
  37046. ret = WC_TEST_RET_ENC_NC;
  37047. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37048. DYNAMIC_TYPE_TMP_BUFFER);
  37049. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  37050. ret = WC_TEST_RET_ENC_ERRNO;
  37051. }
  37052. rsaServerCertBufSz = FOURK_BUF;
  37053. rsaServerPrivKeyBufSz = FOURK_BUF;
  37054. /* read CA RSA cert and key in DER format, for use with server cert */
  37055. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37056. DYNAMIC_TYPE_TMP_BUFFER);
  37057. if (ret == 0 && rsaCaCertBuf == NULL)
  37058. ret = WC_TEST_RET_ENC_NC;
  37059. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37060. DYNAMIC_TYPE_TMP_BUFFER);
  37061. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  37062. ret = WC_TEST_RET_ENC_ERRNO;
  37063. }
  37064. rsaCaCertBufSz = FOURK_BUF;
  37065. rsaCaPrivKeyBufSz = FOURK_BUF;
  37066. #endif /* NO_RSA */
  37067. #ifdef HAVE_ECC
  37068. /* read client ECC cert and key in DER format */
  37069. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37070. DYNAMIC_TYPE_TMP_BUFFER);
  37071. if (ret == 0 && eccClientCertBuf == NULL) {
  37072. ret = WC_TEST_RET_ENC_ERRNO;
  37073. }
  37074. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37075. DYNAMIC_TYPE_TMP_BUFFER);
  37076. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  37077. ret = WC_TEST_RET_ENC_ERRNO;
  37078. }
  37079. eccClientCertBufSz = FOURK_BUF;
  37080. eccClientPrivKeyBufSz = FOURK_BUF;
  37081. #endif /* HAVE_ECC */
  37082. if (ret >= 0)
  37083. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  37084. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  37085. rsaServerCertBuf, &rsaServerCertBufSz,
  37086. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  37087. rsaCaCertBuf, &rsaCaCertBufSz,
  37088. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  37089. eccClientCertBuf, &eccClientCertBufSz,
  37090. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  37091. if (ret < 0) {
  37092. ret = WC_TEST_RET_ENC_EC(ret);
  37093. }
  37094. if (ret >= 0)
  37095. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37096. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  37097. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  37098. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  37099. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  37100. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  37101. eccClientCertBuf, (word32)eccClientCertBufSz,
  37102. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  37103. if (ret >= 0)
  37104. ret = pkcs7signed_run_SingleShotVectors(
  37105. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37106. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  37107. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  37108. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  37109. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  37110. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  37111. eccClientCertBuf, (word32)eccClientCertBufSz,
  37112. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  37113. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  37114. if (ret >= 0)
  37115. ret = pkcs7callback_test(
  37116. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37117. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  37118. #endif
  37119. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37120. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37121. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37122. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37123. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37124. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37125. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37126. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37127. return ret;
  37128. }
  37129. #endif /* HAVE_PKCS7 */
  37130. #if defined(WOLFSSL_PUBLIC_MP) && \
  37131. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37132. defined(USE_FAST_MATH))
  37133. /* Maximum number of bytes in a number to test. */
  37134. #define MP_MAX_TEST_BYTE_LEN 32
  37135. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  37136. {
  37137. byte d[MP_MAX_TEST_BYTE_LEN];
  37138. wc_test_ret_t ret;
  37139. (void)heap;
  37140. do {
  37141. ret = wc_RNG_GenerateBlock(rng, d, len);
  37142. if (ret != 0)
  37143. return ret;
  37144. ret = mp_read_unsigned_bin(n, d, len);
  37145. if (ret != 0)
  37146. return ret;
  37147. } while (mp_iszero(n));
  37148. return 0;
  37149. }
  37150. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  37151. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  37152. {
  37153. int i, j;
  37154. mp_digit rem;
  37155. mp_digit rem2;
  37156. wc_test_ret_t ret;
  37157. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37158. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37159. for (i = 0; i < 10; i++) {
  37160. for (j = 1; j < 10; j++) {
  37161. ret = randNum(a, j, rng, NULL);
  37162. if (ret != 0)
  37163. return WC_TEST_RET_ENC_EC(ret);
  37164. ret = mp_div_3(a, r, &rem);
  37165. if (ret != 0)
  37166. return WC_TEST_RET_ENC_EC(ret);
  37167. ret = mp_mul_d(r, 3, r);
  37168. if (ret != 0)
  37169. return WC_TEST_RET_ENC_EC(ret);
  37170. ret = mp_add_d(r, rem, r);
  37171. if (ret != 0)
  37172. return WC_TEST_RET_ENC_EC(ret);
  37173. ret = mp_cmp(r, a);
  37174. if (ret != MP_EQ)
  37175. return WC_TEST_RET_ENC_NC;
  37176. }
  37177. }
  37178. ret = mp_div_3(a, r, &rem);
  37179. if (ret != 0)
  37180. return WC_TEST_RET_ENC_NC;
  37181. ret = mp_div_3(a, a, NULL);
  37182. if (ret != 0)
  37183. return WC_TEST_RET_ENC_NC;
  37184. ret = mp_cmp(r, a);
  37185. if (ret != MP_EQ)
  37186. return WC_TEST_RET_ENC_NC;
  37187. #endif
  37188. #if defined(WOLFSSL_SP_MATH_ALL)
  37189. ret = mp_div_d(a, 10, r, &rem);
  37190. if (ret != 0)
  37191. return WC_TEST_RET_ENC_EC(ret);
  37192. ret = mp_div_d(a, 10, a, NULL);
  37193. if (ret != 0)
  37194. return WC_TEST_RET_ENC_EC(ret);
  37195. ret = mp_cmp(r, a);
  37196. if (ret != MP_EQ)
  37197. return WC_TEST_RET_ENC_NC;
  37198. ret = mp_div_d(a, 12, r, &rem);
  37199. if (ret != 0)
  37200. return WC_TEST_RET_ENC_EC(ret);
  37201. ret = mp_div_d(a, 12, a, NULL);
  37202. if (ret != 0)
  37203. return WC_TEST_RET_ENC_EC(ret);
  37204. ret = mp_cmp(r, a);
  37205. if (ret != MP_EQ)
  37206. return WC_TEST_RET_ENC_NC;
  37207. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  37208. if (ret != 0)
  37209. return WC_TEST_RET_ENC_EC(ret);
  37210. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  37211. if (ret != 0)
  37212. return WC_TEST_RET_ENC_EC(ret);
  37213. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  37214. if (ret != 0)
  37215. return WC_TEST_RET_ENC_EC(ret);
  37216. ret = mp_cmp(r, a);
  37217. if (ret != MP_EQ)
  37218. return WC_TEST_RET_ENC_NC;
  37219. if (rem != rem2)
  37220. return WC_TEST_RET_ENC_NC;
  37221. #endif
  37222. (void)a;
  37223. (void)r;
  37224. (void)rng;
  37225. (void)i;
  37226. (void)j;
  37227. (void)rem;
  37228. (void)rem2;
  37229. (void)ret;
  37230. return 0;
  37231. }
  37232. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  37233. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37234. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37235. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  37236. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  37237. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  37238. {
  37239. wc_test_ret_t ret;
  37240. int i, j;
  37241. int size;
  37242. char str[30];
  37243. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  37244. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  37245. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  37246. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  37247. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  37248. for (i = 0; i < 10; i++) {
  37249. for (j = 2; j < 12; j++) {
  37250. ret = randNum(a, j, rng, NULL);
  37251. if (ret != 0)
  37252. return WC_TEST_RET_ENC_EC(ret);
  37253. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  37254. if (ret != MP_OKAY)
  37255. return WC_TEST_RET_ENC_EC(ret);
  37256. ret = mp_toradix(a, str, MP_RADIX_DEC);
  37257. if (ret != MP_OKAY)
  37258. return WC_TEST_RET_ENC_EC(ret);
  37259. if ((int)XSTRLEN(str) != size - 1)
  37260. return WC_TEST_RET_ENC_NC;
  37261. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  37262. if (ret != MP_OKAY)
  37263. return WC_TEST_RET_ENC_EC(ret);
  37264. ret = mp_cmp(a, r);
  37265. if (ret != MP_EQ)
  37266. return WC_TEST_RET_ENC_NC;
  37267. }
  37268. }
  37269. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  37270. if (ret != MP_VAL)
  37271. return WC_TEST_RET_ENC_EC(ret);
  37272. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  37273. if (ret != MP_VAL)
  37274. return WC_TEST_RET_ENC_EC(ret);
  37275. ret = mp_read_radix(r, badStr3, MP_RADIX_DEC);
  37276. if (ret != MP_VAL)
  37277. return WC_TEST_RET_ENC_EC(ret);
  37278. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  37279. if (ret != MP_OKAY)
  37280. return WC_TEST_RET_ENC_EC(ret);
  37281. if (!mp_iszero(r))
  37282. return WC_TEST_RET_ENC_NC;
  37283. mp_set(r, 1);
  37284. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  37285. if (ret != MP_OKAY)
  37286. return WC_TEST_RET_ENC_EC(ret);
  37287. if (!mp_iszero(r))
  37288. return WC_TEST_RET_ENC_NC;
  37289. mp_zero(a);
  37290. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  37291. if (ret != 0)
  37292. return WC_TEST_RET_ENC_EC(ret);
  37293. if (size != 2)
  37294. return WC_TEST_RET_ENC_NC;
  37295. ret = mp_toradix(a, str, MP_RADIX_DEC);
  37296. if (ret != 0)
  37297. return WC_TEST_RET_ENC_EC(ret);
  37298. if ((int)XSTRLEN(str) != size - 1)
  37299. return WC_TEST_RET_ENC_NC;
  37300. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  37301. if (ret != 0)
  37302. return WC_TEST_RET_ENC_EC(ret);
  37303. if (!mp_iszero(r))
  37304. return WC_TEST_RET_ENC_NC;
  37305. return 0;
  37306. }
  37307. #endif
  37308. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  37309. defined(HAVE_ECC))
  37310. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  37311. {
  37312. wc_test_ret_t ret;
  37313. int i, j;
  37314. int size;
  37315. char str[30];
  37316. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  37317. static char longStr[2 * sizeof(a->dp) + 2];
  37318. #endif
  37319. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  37320. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  37321. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  37322. for (i = 0; i < 10; i++) {
  37323. for (j = 2; j < 12; j++) {
  37324. ret = randNum(a, j, rng, NULL);
  37325. if (ret != 0)
  37326. return WC_TEST_RET_ENC_EC(ret);
  37327. mp_radix_size(a, MP_RADIX_HEX, &size);
  37328. mp_toradix(a, str, MP_RADIX_HEX);
  37329. if ((int)XSTRLEN(str) != size - 1)
  37330. return WC_TEST_RET_ENC_NC;
  37331. mp_read_radix(r, str, MP_RADIX_HEX);
  37332. ret = mp_cmp(a, r);
  37333. if (ret != MP_EQ)
  37334. return WC_TEST_RET_ENC_NC;
  37335. }
  37336. }
  37337. ret = mp_read_radix(r, badStr1, MP_RADIX_HEX);
  37338. if (ret != MP_VAL)
  37339. return WC_TEST_RET_ENC_EC(ret);
  37340. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  37341. if (ret != MP_VAL)
  37342. return WC_TEST_RET_ENC_EC(ret);
  37343. mp_set(r, 1);
  37344. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  37345. if (ret != MP_OKAY)
  37346. return WC_TEST_RET_ENC_EC(ret);
  37347. if (!mp_iszero(r))
  37348. return WC_TEST_RET_ENC_NC;
  37349. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  37350. /* Fixed MP data size - string can be too long. */
  37351. longStr[0] = '8';
  37352. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  37353. longStr[sizeof(longStr)-1] = '\0';
  37354. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  37355. if (ret != MP_VAL)
  37356. return WC_TEST_RET_ENC_EC(ret);
  37357. #endif
  37358. mp_zero(a);
  37359. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  37360. if (ret != 0)
  37361. return WC_TEST_RET_ENC_EC(ret);
  37362. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37363. if (size != 3)
  37364. #else
  37365. if (size != 2)
  37366. #endif
  37367. return WC_TEST_RET_ENC_NC;
  37368. ret = mp_toradix(a, str, MP_RADIX_HEX);
  37369. if (ret != 0)
  37370. return WC_TEST_RET_ENC_EC(ret);
  37371. if ((int)XSTRLEN(str) != size - 1)
  37372. return WC_TEST_RET_ENC_NC;
  37373. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  37374. if (ret != 0)
  37375. return WC_TEST_RET_ENC_EC(ret);
  37376. if (!mp_iszero(r))
  37377. return WC_TEST_RET_ENC_NC;
  37378. #ifdef WOLFSSL_SP_MATH
  37379. ret = mp_toradix(a, str, 8);
  37380. if (ret != MP_VAL)
  37381. return WC_TEST_RET_ENC_EC(ret);
  37382. ret = mp_radix_size(a, 8, &size);
  37383. if (ret != MP_VAL)
  37384. return WC_TEST_RET_ENC_EC(ret);
  37385. #endif
  37386. return 0;
  37387. }
  37388. #endif
  37389. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  37390. {
  37391. int i;
  37392. wc_test_ret_t ret;
  37393. ret = randNum(a, 4, rng, NULL);
  37394. if (ret != 0)
  37395. return WC_TEST_RET_ENC_EC(ret);
  37396. for (i = 0; i < 4; i++) {
  37397. mp_copy(r1, a);
  37398. ret = mp_lshd(r1, i);
  37399. if (ret != MP_OKAY)
  37400. return WC_TEST_RET_ENC_EC(ret);
  37401. #ifndef WOLFSSL_SP_MATH
  37402. mp_rshd(r1, i);
  37403. #else
  37404. mp_rshb(r1, i * SP_WORD_SIZE);
  37405. #endif
  37406. ret = mp_cmp(a, r1);
  37407. if (ret != MP_EQ)
  37408. return WC_TEST_RET_ENC_NC;
  37409. }
  37410. #ifndef WOLFSSL_SP_MATH
  37411. for (i = 0; i < DIGIT_BIT+1; i++) {
  37412. ret = mp_mul_2d(a, i, r1);
  37413. if (ret != MP_OKAY)
  37414. return WC_TEST_RET_ENC_EC(ret);
  37415. mp_rshb(r1, i);
  37416. ret = mp_cmp(a, r1);
  37417. if (ret != MP_EQ)
  37418. return WC_TEST_RET_ENC_NC;
  37419. }
  37420. #endif
  37421. return 0;
  37422. }
  37423. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  37424. {
  37425. int i, j;
  37426. wc_test_ret_t ret;
  37427. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37428. mp_zero(a);
  37429. mp_set_bit(a, i);
  37430. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  37431. return WC_TEST_RET_ENC_NC;
  37432. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  37433. mp_zero(r1);
  37434. mp_set_bit(r1, i);
  37435. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  37436. if (ret != MP_OKAY)
  37437. return WC_TEST_RET_ENC_EC(ret);
  37438. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  37439. if (ret != MP_OKAY)
  37440. return WC_TEST_RET_ENC_EC(ret);
  37441. ret = mp_cmp(a, r1);
  37442. if (ret != MP_EQ)
  37443. return WC_TEST_RET_ENC_NC;
  37444. }
  37445. }
  37446. mp_zero(r1);
  37447. ret = mp_add_d(r1, 1, r1);
  37448. if (ret != MP_OKAY)
  37449. return WC_TEST_RET_ENC_EC(ret);
  37450. if (r1->used != 1)
  37451. return WC_TEST_RET_ENC_NC;
  37452. ret = mp_sub_d(r1, 1, r1);
  37453. if (ret != MP_OKAY)
  37454. return WC_TEST_RET_ENC_EC(ret);
  37455. if (r1->used != 0)
  37456. return WC_TEST_RET_ENC_NC;
  37457. return 0;
  37458. }
  37459. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  37460. {
  37461. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  37462. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  37463. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  37464. };
  37465. byte out[24];
  37466. int i, j, k;
  37467. const byte* p;
  37468. wc_test_ret_t ret;
  37469. for (i = 0; i < (int)sizeof(in); i++) {
  37470. p = in + sizeof(in) - i;
  37471. ret = mp_read_unsigned_bin(a, p, i);
  37472. if (ret != 0)
  37473. return WC_TEST_RET_ENC_EC(ret);
  37474. for (j = i; j < (int)sizeof(out); j++) {
  37475. XMEMSET(out, 0xff, sizeof(out));
  37476. ret = mp_to_unsigned_bin_len(a, out, j);
  37477. if (ret != 0)
  37478. return WC_TEST_RET_ENC_EC(ret);
  37479. for (k = 0; k < j - i; k++) {
  37480. if (out[k] != 0)
  37481. return WC_TEST_RET_ENC_NC;
  37482. }
  37483. for (; k < j; k++) {
  37484. if (out[k] != p[k - (j - i)])
  37485. return WC_TEST_RET_ENC_NC;
  37486. }
  37487. }
  37488. }
  37489. /* Length too small. */
  37490. ret = mp_to_unsigned_bin_len(a, out, 1);
  37491. if (ret != MP_VAL)
  37492. return WC_TEST_RET_ENC_EC(ret);
  37493. ret = mp_read_unsigned_bin(a, NULL, 0);
  37494. if (ret != 0)
  37495. return WC_TEST_RET_ENC_EC(ret);
  37496. if (!mp_iszero(a))
  37497. return WC_TEST_RET_ENC_NC;
  37498. return 0;
  37499. }
  37500. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37501. static wc_test_ret_t mp_test_set_int(mp_int* a)
  37502. {
  37503. #if SP_ULONG_BITS == 64
  37504. unsigned long n = 0xfedcba9876543210UL;
  37505. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  37506. byte out[8] = { 0 };
  37507. #elif SP_ULONG_BITS == 32
  37508. unsigned long n = 0xfedcba98UL;
  37509. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  37510. byte out[4] = { 0 };
  37511. #elif SP_ULONG_BITS == 16
  37512. unsigned long n = 0xfedc;
  37513. byte exp[2] = { 0xfe, 0xdc };
  37514. byte out[2] = { 0 };
  37515. #elif SP_ULONG_BITS == 8
  37516. unsigned long n = 0xfe;
  37517. byte exp[1] = { 0xfe };
  37518. byte out[1] = { 0 };
  37519. #endif
  37520. wc_test_ret_t ret;
  37521. ret = mp_set_int(a, n);
  37522. if (ret != 0)
  37523. return WC_TEST_RET_ENC_EC(ret);
  37524. ret = mp_unsigned_bin_size(a);
  37525. if (ret != sizeof(exp))
  37526. return WC_TEST_RET_ENC_NC;
  37527. ret = mp_to_unsigned_bin(a, out);
  37528. if (ret != 0)
  37529. return WC_TEST_RET_ENC_EC(ret);
  37530. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  37531. return WC_TEST_RET_ENC_NC;
  37532. return 0;
  37533. }
  37534. #endif
  37535. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37536. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  37537. {
  37538. byte buffer[16];
  37539. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37540. char hexStr[] = "abcdef0123456789";
  37541. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37542. char negStr[] = "-1234";
  37543. #endif
  37544. #endif
  37545. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  37546. defined(HAVE_COMP_KEY)
  37547. char decStr[] = "0987654321";
  37548. #endif
  37549. wc_test_ret_t ret;
  37550. #ifdef WOLFSSL_SP_MATH_ALL
  37551. mp_digit rho;
  37552. int size;
  37553. #endif
  37554. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37555. int result;
  37556. #endif
  37557. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37558. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37559. mp_digit rd;
  37560. #endif
  37561. (void)rng;
  37562. (void)r;
  37563. ret = mp_init(NULL);
  37564. if (ret != MP_VAL)
  37565. return WC_TEST_RET_ENC_EC(ret);
  37566. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37567. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  37568. if (ret != MP_OKAY)
  37569. return WC_TEST_RET_ENC_EC(ret);
  37570. #endif
  37571. mp_free(NULL);
  37572. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  37573. ret = mp_grow(NULL, 1);
  37574. if (ret != MP_VAL)
  37575. return WC_TEST_RET_ENC_EC(ret);
  37576. #ifdef WOLFSSL_SP_MATH
  37577. ret = mp_grow(a, SP_INT_DIGITS + 1);
  37578. if (ret != MP_MEM)
  37579. return WC_TEST_RET_ENC_EC(ret);
  37580. #endif
  37581. #endif
  37582. mp_clear(NULL);
  37583. ret = mp_abs(NULL, NULL);
  37584. if (ret != MP_VAL)
  37585. return WC_TEST_RET_ENC_EC(ret);
  37586. ret = mp_abs(a, NULL);
  37587. if (ret != MP_VAL)
  37588. return WC_TEST_RET_ENC_EC(ret);
  37589. ret = mp_abs(NULL, b);
  37590. if (ret != MP_VAL)
  37591. return WC_TEST_RET_ENC_EC(ret);
  37592. ret = mp_unsigned_bin_size(NULL);
  37593. if (ret != 0)
  37594. return WC_TEST_RET_ENC_EC(ret);
  37595. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  37596. if (ret != MP_VAL)
  37597. return WC_TEST_RET_ENC_EC(ret);
  37598. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  37599. if (ret != MP_VAL)
  37600. return WC_TEST_RET_ENC_EC(ret);
  37601. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  37602. if (ret != MP_VAL)
  37603. return WC_TEST_RET_ENC_EC(ret);
  37604. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  37605. if (ret != MP_VAL)
  37606. return WC_TEST_RET_ENC_EC(ret);
  37607. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37608. ret = mp_read_radix(NULL, NULL, 16);
  37609. if (ret != MP_VAL)
  37610. return WC_TEST_RET_ENC_EC(ret);
  37611. ret = mp_read_radix(a, NULL, 16);
  37612. if (ret != MP_VAL)
  37613. return WC_TEST_RET_ENC_EC(ret);
  37614. ret = mp_read_radix(NULL, hexStr, 16);
  37615. if (ret != MP_VAL)
  37616. return WC_TEST_RET_ENC_EC(ret);
  37617. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37618. ret = mp_read_radix(a, negStr, 16);
  37619. if (ret != MP_VAL)
  37620. return WC_TEST_RET_ENC_EC(ret);
  37621. #ifdef WOLFSSL_SP_MATH_ALL
  37622. ret = mp_read_radix(a, negStr, 10);
  37623. if (ret != MP_VAL)
  37624. return WC_TEST_RET_ENC_EC(ret);
  37625. #endif /* WOLFSSL_SP_MATH_ALL */
  37626. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  37627. #endif
  37628. #ifndef WOLFSSL_SP_MATH_ALL
  37629. /* Radix 10 only supported with ALL. */
  37630. ret = mp_read_radix(a, decStr, 10);
  37631. if (ret != MP_VAL)
  37632. return WC_TEST_RET_ENC_EC(ret);
  37633. #endif
  37634. /* Radix 8 not supported SP_INT. */
  37635. ret = mp_read_radix(a, "0123", 8);
  37636. if (ret != MP_VAL)
  37637. return WC_TEST_RET_ENC_EC(ret);
  37638. ret = mp_count_bits(NULL);
  37639. if (ret != 0)
  37640. return WC_TEST_RET_ENC_EC(ret);
  37641. ret = mp_is_bit_set(NULL, 0);
  37642. if (ret != 0)
  37643. return WC_TEST_RET_ENC_EC(ret);
  37644. ret = mp_leading_bit(NULL);
  37645. if (ret != 0)
  37646. return WC_TEST_RET_ENC_EC(ret);
  37647. mp_zero(a);
  37648. ret = mp_leading_bit(a);
  37649. if (ret != 0)
  37650. return WC_TEST_RET_ENC_EC(ret);
  37651. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37652. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  37653. !defined(NO_RSA)
  37654. ret = mp_set_bit(NULL, 1);
  37655. if (ret != MP_VAL)
  37656. return WC_TEST_RET_ENC_EC(ret);
  37657. #endif
  37658. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37659. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37660. ret = mp_to_unsigned_bin(NULL, NULL);
  37661. if (ret != MP_VAL)
  37662. return WC_TEST_RET_ENC_EC(ret);
  37663. ret = mp_to_unsigned_bin(a, NULL);
  37664. if (ret != MP_VAL)
  37665. return WC_TEST_RET_ENC_EC(ret);
  37666. ret = mp_to_unsigned_bin(NULL, buffer);
  37667. if (ret != MP_VAL)
  37668. return WC_TEST_RET_ENC_EC(ret);
  37669. #endif
  37670. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  37671. if (ret != MP_VAL)
  37672. return WC_TEST_RET_ENC_EC(ret);
  37673. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  37674. if (ret != MP_VAL)
  37675. return WC_TEST_RET_ENC_EC(ret);
  37676. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  37677. if (ret != MP_VAL)
  37678. return WC_TEST_RET_ENC_EC(ret);
  37679. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37680. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37681. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  37682. if (ret != MP_VAL)
  37683. return WC_TEST_RET_ENC_EC(ret);
  37684. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  37685. if (ret != MP_VAL)
  37686. return WC_TEST_RET_ENC_EC(ret);
  37687. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  37688. if (ret != MP_VAL)
  37689. return WC_TEST_RET_ENC_EC(ret);
  37690. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  37691. if (ret != MP_OKAY)
  37692. return WC_TEST_RET_ENC_EC(ret);
  37693. #endif
  37694. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37695. ret = mp_copy(NULL, NULL);
  37696. if (ret != MP_VAL)
  37697. return WC_TEST_RET_ENC_EC(ret);
  37698. ret = mp_copy(a, NULL);
  37699. if (ret != MP_VAL)
  37700. return WC_TEST_RET_ENC_EC(ret);
  37701. ret = mp_copy(NULL, b);
  37702. if (ret != MP_VAL)
  37703. return WC_TEST_RET_ENC_EC(ret);
  37704. #endif
  37705. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  37706. ret = sp_2expt(NULL, 1);
  37707. if (ret != MP_VAL)
  37708. return WC_TEST_RET_ENC_EC(ret);
  37709. #endif
  37710. ret = mp_set(NULL, 0);
  37711. if (ret != MP_VAL)
  37712. return WC_TEST_RET_ENC_EC(ret);
  37713. ret = mp_cmp_d(NULL, 0);
  37714. if (ret != MP_LT)
  37715. return WC_TEST_RET_ENC_EC(ret);
  37716. ret = mp_cmp(NULL, NULL);
  37717. if (ret != MP_EQ)
  37718. return WC_TEST_RET_ENC_NC;
  37719. ret = mp_cmp(a, NULL);
  37720. if (ret != MP_GT)
  37721. return WC_TEST_RET_ENC_NC;
  37722. ret = mp_cmp(NULL, b);
  37723. if (ret != MP_LT)
  37724. return WC_TEST_RET_ENC_NC;
  37725. #ifdef WOLFSSL_SP_MATH_ALL
  37726. mp_rshd(NULL, 1);
  37727. #endif
  37728. mp_zero(NULL);
  37729. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37730. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37731. ret = mp_lshd(NULL, 0);
  37732. if (ret != MP_VAL)
  37733. return WC_TEST_RET_ENC_EC(ret);
  37734. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  37735. if (ret != MP_VAL)
  37736. return WC_TEST_RET_ENC_EC(ret);
  37737. #endif
  37738. #if defined(WOLFSSL_SP_MATH_ALL)
  37739. ret = mp_div(NULL, NULL, a, b);
  37740. if (ret != MP_VAL)
  37741. return WC_TEST_RET_ENC_EC(ret);
  37742. ret = mp_div(a, NULL, a, b);
  37743. if (ret != MP_VAL)
  37744. return WC_TEST_RET_ENC_EC(ret);
  37745. ret = mp_div(NULL, b, a, b);
  37746. if (ret != MP_VAL)
  37747. return WC_TEST_RET_ENC_EC(ret);
  37748. ret = mp_div(a, b, NULL, NULL);
  37749. if (ret != MP_VAL)
  37750. return WC_TEST_RET_ENC_EC(ret);
  37751. #endif
  37752. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37753. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37754. ret = mp_mod(NULL, NULL, NULL);
  37755. if (ret != MP_VAL)
  37756. return WC_TEST_RET_ENC_EC(ret);
  37757. ret = mp_mod(a, NULL, NULL);
  37758. if (ret != MP_VAL)
  37759. return WC_TEST_RET_ENC_EC(ret);
  37760. ret = mp_mod(NULL, b, NULL);
  37761. if (ret != MP_VAL)
  37762. return WC_TEST_RET_ENC_EC(ret);
  37763. ret = mp_mod(NULL, NULL, r);
  37764. if (ret != MP_VAL)
  37765. return WC_TEST_RET_ENC_EC(ret);
  37766. ret = mp_mod(a, b, NULL);
  37767. if (ret != MP_VAL)
  37768. return WC_TEST_RET_ENC_EC(ret);
  37769. ret = mp_mod(a, NULL, r);
  37770. if (ret != MP_VAL)
  37771. return WC_TEST_RET_ENC_EC(ret);
  37772. ret = mp_mod(NULL, b, r);
  37773. if (ret != MP_VAL)
  37774. return WC_TEST_RET_ENC_EC(ret);
  37775. #endif
  37776. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  37777. ret = mp_set_int(NULL, 0);
  37778. if (ret != MP_VAL)
  37779. return WC_TEST_RET_ENC_EC(ret);
  37780. #endif
  37781. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37782. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37783. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37784. if (ret != MP_VAL)
  37785. return WC_TEST_RET_ENC_EC(ret);
  37786. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37787. if (ret != MP_VAL)
  37788. return WC_TEST_RET_ENC_EC(ret);
  37789. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  37790. if (ret != MP_VAL)
  37791. return WC_TEST_RET_ENC_EC(ret);
  37792. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  37793. if (ret != MP_VAL)
  37794. return WC_TEST_RET_ENC_EC(ret);
  37795. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37796. if (ret != MP_VAL)
  37797. return WC_TEST_RET_ENC_EC(ret);
  37798. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  37799. if (ret != MP_VAL)
  37800. return WC_TEST_RET_ENC_EC(ret);
  37801. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  37802. if (ret != MP_VAL)
  37803. return WC_TEST_RET_ENC_EC(ret);
  37804. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  37805. if (ret != MP_VAL)
  37806. return WC_TEST_RET_ENC_EC(ret);
  37807. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  37808. if (ret != MP_VAL)
  37809. return WC_TEST_RET_ENC_EC(ret);
  37810. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37811. if (ret != MP_VAL)
  37812. return WC_TEST_RET_ENC_EC(ret);
  37813. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37814. if (ret != MP_VAL)
  37815. return WC_TEST_RET_ENC_EC(ret);
  37816. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  37817. if (ret != MP_VAL)
  37818. return WC_TEST_RET_ENC_EC(ret);
  37819. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  37820. if (ret != MP_VAL)
  37821. return WC_TEST_RET_ENC_EC(ret);
  37822. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37823. if (ret != MP_VAL)
  37824. return WC_TEST_RET_ENC_EC(ret);
  37825. ret = mp_exptmod_nct(a, a, a, NULL);
  37826. if (ret != MP_VAL)
  37827. return WC_TEST_RET_ENC_EC(ret);
  37828. ret = mp_exptmod_nct(a, a, NULL, a);
  37829. if (ret != MP_VAL)
  37830. return WC_TEST_RET_ENC_EC(ret);
  37831. ret = mp_exptmod_nct(a, NULL, a, a);
  37832. if (ret != MP_VAL)
  37833. return WC_TEST_RET_ENC_EC(ret);
  37834. ret = mp_exptmod_nct(NULL, a, a, a);
  37835. if (ret != MP_VAL)
  37836. return WC_TEST_RET_ENC_EC(ret);
  37837. #endif
  37838. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37839. !defined(WC_NO_RNG)
  37840. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  37841. if (ret != MP_VAL)
  37842. return WC_TEST_RET_ENC_EC(ret);
  37843. ret = mp_rand_prime(a, 32, NULL, NULL);
  37844. if (ret != MP_VAL)
  37845. return WC_TEST_RET_ENC_EC(ret);
  37846. ret = mp_rand_prime(NULL, 32, rng, NULL);
  37847. if (ret != MP_VAL)
  37848. return WC_TEST_RET_ENC_EC(ret);
  37849. ret = mp_rand_prime(a, 0, rng, NULL);
  37850. if (ret != MP_VAL)
  37851. return WC_TEST_RET_ENC_EC(ret);
  37852. #endif
  37853. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37854. ret = mp_mul(NULL, NULL, NULL);
  37855. if (ret != MP_VAL)
  37856. return WC_TEST_RET_ENC_EC(ret);
  37857. ret = mp_mul(a, NULL, NULL);
  37858. if (ret != MP_VAL)
  37859. return WC_TEST_RET_ENC_EC(ret);
  37860. ret = mp_mul(NULL, b, NULL);
  37861. if (ret != MP_VAL)
  37862. return WC_TEST_RET_ENC_EC(ret);
  37863. ret = mp_mul(NULL, NULL, r);
  37864. if (ret != MP_VAL)
  37865. return WC_TEST_RET_ENC_EC(ret);
  37866. ret = mp_mul(a, b, NULL);
  37867. if (ret != MP_VAL)
  37868. return WC_TEST_RET_ENC_EC(ret);
  37869. ret = mp_mul(a, NULL, r);
  37870. if (ret != MP_VAL)
  37871. return WC_TEST_RET_ENC_EC(ret);
  37872. ret = mp_mul(NULL, b, r);
  37873. if (ret != MP_VAL)
  37874. return WC_TEST_RET_ENC_EC(ret);
  37875. #endif
  37876. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37877. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37878. ret = mp_sqr(NULL, NULL);
  37879. if (ret != MP_VAL)
  37880. return WC_TEST_RET_ENC_EC(ret);
  37881. ret = mp_sqr(a, NULL);
  37882. if (ret != MP_VAL)
  37883. return WC_TEST_RET_ENC_EC(ret);
  37884. ret = mp_sqr(NULL, r);
  37885. if (ret != MP_VAL)
  37886. return WC_TEST_RET_ENC_EC(ret);
  37887. #endif
  37888. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37889. ret = mp_sqrmod(NULL, NULL, NULL);
  37890. if (ret != MP_VAL)
  37891. return WC_TEST_RET_ENC_EC(ret);
  37892. ret = mp_sqrmod(a, NULL, NULL);
  37893. if (ret != MP_VAL)
  37894. return WC_TEST_RET_ENC_EC(ret);
  37895. ret = mp_sqrmod(NULL, a, NULL);
  37896. if (ret != MP_VAL)
  37897. return WC_TEST_RET_ENC_EC(ret);
  37898. ret = mp_sqrmod(NULL, NULL, a);
  37899. if (ret != MP_VAL)
  37900. return WC_TEST_RET_ENC_EC(ret);
  37901. ret = mp_sqrmod(a, b, NULL);
  37902. if (ret != MP_VAL)
  37903. return WC_TEST_RET_ENC_EC(ret);
  37904. ret = mp_sqrmod(a, NULL, b);
  37905. if (ret != MP_VAL)
  37906. return WC_TEST_RET_ENC_EC(ret);
  37907. ret = mp_sqrmod(NULL, a, b);
  37908. if (ret != MP_VAL)
  37909. return WC_TEST_RET_ENC_EC(ret);
  37910. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  37911. if (ret != MP_VAL)
  37912. return WC_TEST_RET_ENC_EC(ret);
  37913. ret = mp_mulmod(a, NULL, NULL, NULL);
  37914. if (ret != MP_VAL)
  37915. return WC_TEST_RET_ENC_EC(ret);
  37916. ret = mp_mulmod(NULL, a, NULL, NULL);
  37917. if (ret != MP_VAL)
  37918. return WC_TEST_RET_ENC_EC(ret);
  37919. ret = mp_mulmod(NULL, NULL, a, NULL);
  37920. if (ret != MP_VAL)
  37921. return WC_TEST_RET_ENC_EC(ret);
  37922. ret = mp_mulmod(NULL, NULL, NULL, a);
  37923. if (ret != MP_VAL)
  37924. return WC_TEST_RET_ENC_EC(ret);
  37925. ret = mp_mulmod(a, b, b, NULL);
  37926. if (ret != MP_VAL)
  37927. return WC_TEST_RET_ENC_EC(ret);
  37928. ret = mp_mulmod(a, b, NULL, a);
  37929. if (ret != MP_VAL)
  37930. return WC_TEST_RET_ENC_EC(ret);
  37931. ret = mp_mulmod(a, NULL, b, a);
  37932. if (ret != MP_VAL)
  37933. return WC_TEST_RET_ENC_EC(ret);
  37934. ret = mp_mulmod(NULL, b, b, a);
  37935. if (ret != MP_VAL)
  37936. return WC_TEST_RET_ENC_EC(ret);
  37937. #endif
  37938. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  37939. !defined(NO_RSA) || !defined(NO_DSA)
  37940. ret = mp_add_d(NULL, 1, NULL);
  37941. if (ret != MP_VAL)
  37942. return WC_TEST_RET_ENC_EC(ret);
  37943. ret = mp_add_d(a, 1, NULL);
  37944. if (ret != MP_VAL)
  37945. return WC_TEST_RET_ENC_EC(ret);
  37946. ret = mp_add_d(NULL, 1, b);
  37947. if (ret != MP_VAL)
  37948. return WC_TEST_RET_ENC_EC(ret);
  37949. #endif
  37950. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37951. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  37952. ret = mp_sub_d(NULL, 1, NULL);
  37953. if (ret != MP_VAL)
  37954. return WC_TEST_RET_ENC_EC(ret);
  37955. ret = mp_sub_d(a, 1, NULL);
  37956. if (ret != MP_VAL)
  37957. return WC_TEST_RET_ENC_EC(ret);
  37958. ret = mp_sub_d(NULL, 1, b);
  37959. if (ret != MP_VAL)
  37960. return WC_TEST_RET_ENC_EC(ret);
  37961. #endif
  37962. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37963. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37964. ret = mp_div_d(NULL, 0, NULL, NULL);
  37965. if (ret != MP_VAL)
  37966. return WC_TEST_RET_ENC_EC(ret);
  37967. ret = mp_div_d(a, 0, NULL, NULL);
  37968. if (ret != MP_VAL)
  37969. return WC_TEST_RET_ENC_EC(ret);
  37970. ret = mp_div_d(NULL, 1, NULL, NULL);
  37971. if (ret != MP_VAL)
  37972. return WC_TEST_RET_ENC_EC(ret);
  37973. #endif
  37974. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37975. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37976. ret = mp_mod_d(NULL, 0, NULL);
  37977. if (ret != MP_VAL)
  37978. return WC_TEST_RET_ENC_EC(ret);
  37979. ret = mp_mod_d(a, 0, NULL);
  37980. if (ret != MP_VAL)
  37981. return WC_TEST_RET_ENC_EC(ret);
  37982. ret = mp_mod_d(NULL, 0, &rd);
  37983. if (ret != MP_VAL)
  37984. return WC_TEST_RET_ENC_EC(ret);
  37985. #endif
  37986. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  37987. ret = mp_gcd(NULL, NULL, NULL);
  37988. if (ret != MP_VAL)
  37989. return WC_TEST_RET_ENC_EC(ret);
  37990. ret = mp_gcd(a, NULL, NULL);
  37991. if (ret != MP_VAL)
  37992. return WC_TEST_RET_ENC_EC(ret);
  37993. ret = mp_gcd(NULL, a, NULL);
  37994. if (ret != MP_VAL)
  37995. return WC_TEST_RET_ENC_EC(ret);
  37996. ret = mp_gcd(NULL, NULL, a);
  37997. if (ret != MP_VAL)
  37998. return WC_TEST_RET_ENC_EC(ret);
  37999. ret = mp_gcd(a, b, NULL);
  38000. if (ret != MP_VAL)
  38001. return WC_TEST_RET_ENC_EC(ret);
  38002. ret = mp_gcd(a, NULL, b);
  38003. if (ret != MP_VAL)
  38004. return WC_TEST_RET_ENC_EC(ret);
  38005. ret = mp_gcd(NULL, a, b);
  38006. if (ret != MP_VAL)
  38007. return WC_TEST_RET_ENC_EC(ret);
  38008. #endif
  38009. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  38010. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  38011. if (ret != MP_VAL)
  38012. return WC_TEST_RET_ENC_EC(ret);
  38013. ret = mp_div_2_mod_ct(a, NULL, NULL);
  38014. if (ret != MP_VAL)
  38015. return WC_TEST_RET_ENC_EC(ret);
  38016. ret = mp_div_2_mod_ct(NULL, b, NULL);
  38017. if (ret != MP_VAL)
  38018. return WC_TEST_RET_ENC_EC(ret);
  38019. ret = mp_div_2_mod_ct(NULL, NULL, a);
  38020. if (ret != MP_VAL)
  38021. return WC_TEST_RET_ENC_EC(ret);
  38022. ret = mp_div_2_mod_ct(a, b, NULL);
  38023. if (ret != MP_VAL)
  38024. return WC_TEST_RET_ENC_EC(ret);
  38025. ret = mp_div_2_mod_ct(a, b, NULL);
  38026. if (ret != MP_VAL)
  38027. return WC_TEST_RET_ENC_EC(ret);
  38028. ret = mp_div_2_mod_ct(NULL, b, a);
  38029. if (ret != MP_VAL)
  38030. return WC_TEST_RET_ENC_EC(ret);
  38031. ret = mp_div_2(NULL, NULL);
  38032. if (ret != MP_VAL)
  38033. return WC_TEST_RET_ENC_EC(ret);
  38034. ret = mp_div_2(a, NULL);
  38035. if (ret != MP_VAL)
  38036. return WC_TEST_RET_ENC_EC(ret);
  38037. ret = mp_div_2(NULL, a);
  38038. if (ret != MP_VAL)
  38039. return WC_TEST_RET_ENC_EC(ret);
  38040. #endif
  38041. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  38042. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  38043. ret = mp_invmod(NULL, NULL, NULL);
  38044. if (ret != MP_VAL)
  38045. return WC_TEST_RET_ENC_EC(ret);
  38046. ret = mp_invmod(a, NULL, NULL);
  38047. if (ret != MP_VAL)
  38048. return WC_TEST_RET_ENC_EC(ret);
  38049. ret = mp_invmod(NULL, b, NULL);
  38050. if (ret != MP_VAL)
  38051. return WC_TEST_RET_ENC_EC(ret);
  38052. ret = mp_invmod(NULL, NULL, a);
  38053. if (ret != MP_VAL)
  38054. return WC_TEST_RET_ENC_EC(ret);
  38055. ret = mp_invmod(a, b, NULL);
  38056. if (ret != MP_VAL)
  38057. return WC_TEST_RET_ENC_EC(ret);
  38058. ret = mp_invmod(a, NULL, a);
  38059. if (ret != MP_VAL)
  38060. return WC_TEST_RET_ENC_EC(ret);
  38061. ret = mp_invmod(NULL, b, a);
  38062. if (ret != MP_VAL)
  38063. return WC_TEST_RET_ENC_EC(ret);
  38064. #endif
  38065. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  38066. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  38067. if (ret != MP_VAL)
  38068. return WC_TEST_RET_ENC_EC(ret);
  38069. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  38070. if (ret != MP_VAL)
  38071. return WC_TEST_RET_ENC_EC(ret);
  38072. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  38073. if (ret != MP_VAL)
  38074. return WC_TEST_RET_ENC_EC(ret);
  38075. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  38076. if (ret != MP_VAL)
  38077. return WC_TEST_RET_ENC_EC(ret);
  38078. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  38079. if (ret != MP_VAL)
  38080. return WC_TEST_RET_ENC_EC(ret);
  38081. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  38082. if (ret != MP_VAL)
  38083. return WC_TEST_RET_ENC_EC(ret);
  38084. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  38085. if (ret != MP_VAL)
  38086. return WC_TEST_RET_ENC_EC(ret);
  38087. #endif
  38088. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38089. ret = mp_lcm(NULL, NULL, NULL);
  38090. if (ret != MP_VAL)
  38091. return WC_TEST_RET_ENC_EC(ret);
  38092. ret = mp_lcm(a, NULL, NULL);
  38093. if (ret != MP_VAL)
  38094. return WC_TEST_RET_ENC_EC(ret);
  38095. ret = mp_lcm(NULL, b, NULL);
  38096. if (ret != MP_VAL)
  38097. return WC_TEST_RET_ENC_EC(ret);
  38098. ret = mp_lcm(NULL, NULL, a);
  38099. if (ret != MP_VAL)
  38100. return WC_TEST_RET_ENC_EC(ret);
  38101. ret = mp_lcm(a, b, NULL);
  38102. if (ret != MP_VAL)
  38103. return WC_TEST_RET_ENC_EC(ret);
  38104. ret = mp_lcm(a, NULL, a);
  38105. if (ret != MP_VAL)
  38106. return WC_TEST_RET_ENC_EC(ret);
  38107. ret = mp_lcm(NULL, b, a);
  38108. if (ret != MP_VAL)
  38109. return WC_TEST_RET_ENC_EC(ret);
  38110. #endif
  38111. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  38112. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  38113. if (ret != MP_VAL)
  38114. return WC_TEST_RET_ENC_EC(ret);
  38115. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  38116. if (ret != MP_VAL)
  38117. return WC_TEST_RET_ENC_EC(ret);
  38118. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  38119. if (ret != MP_VAL)
  38120. return WC_TEST_RET_ENC_EC(ret);
  38121. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  38122. if (ret != MP_VAL)
  38123. return WC_TEST_RET_ENC_EC(ret);
  38124. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  38125. if (ret != MP_VAL)
  38126. return WC_TEST_RET_ENC_EC(ret);
  38127. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  38128. if (ret != MP_VAL)
  38129. return WC_TEST_RET_ENC_EC(ret);
  38130. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  38131. if (ret != MP_VAL)
  38132. return WC_TEST_RET_ENC_EC(ret);
  38133. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  38134. if (ret != MP_VAL)
  38135. return WC_TEST_RET_ENC_EC(ret);
  38136. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  38137. if (ret != MP_VAL)
  38138. return WC_TEST_RET_ENC_EC(ret);
  38139. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  38140. if (ret != MP_VAL)
  38141. return WC_TEST_RET_ENC_EC(ret);
  38142. ret = mp_exptmod(a, NULL, NULL, NULL);
  38143. if (ret != MP_VAL)
  38144. return WC_TEST_RET_ENC_EC(ret);
  38145. ret = mp_exptmod(NULL, b, NULL, NULL);
  38146. if (ret != MP_VAL)
  38147. return WC_TEST_RET_ENC_EC(ret);
  38148. ret = mp_exptmod(NULL, NULL, b, NULL);
  38149. if (ret != MP_VAL)
  38150. return WC_TEST_RET_ENC_EC(ret);
  38151. ret = mp_exptmod(NULL, NULL, NULL, a);
  38152. if (ret != MP_VAL)
  38153. return WC_TEST_RET_ENC_EC(ret);
  38154. ret = mp_exptmod(a, b, b, NULL);
  38155. if (ret != MP_VAL)
  38156. return WC_TEST_RET_ENC_EC(ret);
  38157. ret = mp_exptmod(a, b, NULL, a);
  38158. if (ret != MP_VAL)
  38159. return WC_TEST_RET_ENC_EC(ret);
  38160. ret = mp_exptmod(a, NULL, b, a);
  38161. if (ret != MP_VAL)
  38162. return WC_TEST_RET_ENC_EC(ret);
  38163. ret = mp_exptmod(NULL, b, b, a);
  38164. if (ret != MP_VAL)
  38165. return WC_TEST_RET_ENC_EC(ret);
  38166. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  38167. if (ret != MP_VAL)
  38168. return WC_TEST_RET_ENC_EC(ret);
  38169. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  38170. if (ret != MP_VAL)
  38171. return WC_TEST_RET_ENC_EC(ret);
  38172. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  38173. if (ret != MP_VAL)
  38174. return WC_TEST_RET_ENC_EC(ret);
  38175. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  38176. if (ret != MP_VAL)
  38177. return WC_TEST_RET_ENC_EC(ret);
  38178. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  38179. if (ret != MP_VAL)
  38180. return WC_TEST_RET_ENC_EC(ret);
  38181. ret = mp_exptmod_nct(a, b, b, NULL);
  38182. if (ret != MP_VAL)
  38183. return WC_TEST_RET_ENC_EC(ret);
  38184. ret = mp_exptmod_nct(a, b, NULL, a);
  38185. if (ret != MP_VAL)
  38186. return WC_TEST_RET_ENC_EC(ret);
  38187. ret = mp_exptmod_nct(a, NULL, b, a);
  38188. if (ret != MP_VAL)
  38189. return WC_TEST_RET_ENC_EC(ret);
  38190. ret = mp_exptmod_nct(NULL, b, b, a);
  38191. if (ret != MP_VAL)
  38192. return WC_TEST_RET_ENC_EC(ret);
  38193. #endif
  38194. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  38195. ret = mp_cnt_lsb(NULL);
  38196. if (ret != 0)
  38197. return WC_TEST_RET_ENC_EC(ret);
  38198. #endif
  38199. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  38200. ret = mp_prime_is_prime(NULL, 1, NULL);
  38201. if (ret != MP_VAL)
  38202. return WC_TEST_RET_ENC_EC(ret);
  38203. ret = mp_prime_is_prime(a, 1, NULL);
  38204. if (ret != MP_VAL)
  38205. return WC_TEST_RET_ENC_EC(ret);
  38206. ret = mp_prime_is_prime(NULL, 1, &result);
  38207. if (ret != MP_VAL)
  38208. return WC_TEST_RET_ENC_EC(ret);
  38209. ret = mp_prime_is_prime(a, 0, &result);
  38210. if (ret != MP_VAL)
  38211. return WC_TEST_RET_ENC_EC(ret);
  38212. ret = mp_prime_is_prime(a, 1024, &result);
  38213. if (ret != MP_VAL)
  38214. return WC_TEST_RET_ENC_EC(ret);
  38215. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  38216. if (ret != MP_VAL)
  38217. return WC_TEST_RET_ENC_EC(ret);
  38218. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  38219. if (ret != MP_VAL)
  38220. return WC_TEST_RET_ENC_EC(ret);
  38221. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  38222. if (ret != MP_VAL)
  38223. return WC_TEST_RET_ENC_EC(ret);
  38224. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  38225. if (ret != MP_VAL)
  38226. return WC_TEST_RET_ENC_EC(ret);
  38227. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  38228. if (ret != MP_VAL)
  38229. return WC_TEST_RET_ENC_EC(ret);
  38230. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  38231. if (ret != MP_VAL)
  38232. return WC_TEST_RET_ENC_EC(ret);
  38233. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  38234. if (ret != MP_VAL)
  38235. return WC_TEST_RET_ENC_EC(ret);
  38236. #endif
  38237. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  38238. ret = mp_exch(NULL, NULL);
  38239. if (ret != MP_VAL)
  38240. return WC_TEST_RET_ENC_EC(ret);
  38241. ret = mp_exch(a, NULL);
  38242. if (ret != MP_VAL)
  38243. return WC_TEST_RET_ENC_EC(ret);
  38244. ret = mp_exch(NULL, b);
  38245. if (ret != MP_VAL)
  38246. return WC_TEST_RET_ENC_EC(ret);
  38247. #endif
  38248. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  38249. defined(WOLFSSL_SP_MATH_ALL)
  38250. ret = mp_mul_d(NULL, 1, NULL);
  38251. if (ret != MP_VAL)
  38252. return WC_TEST_RET_ENC_EC(ret);
  38253. ret = mp_mul_d(a, 1, NULL);
  38254. if (ret != MP_VAL)
  38255. return WC_TEST_RET_ENC_EC(ret);
  38256. ret = mp_mul_d(NULL, 1, b);
  38257. if (ret != MP_VAL)
  38258. return WC_TEST_RET_ENC_EC(ret);
  38259. #endif
  38260. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38261. ret = mp_add(NULL, NULL, NULL);
  38262. if (ret != MP_VAL)
  38263. return WC_TEST_RET_ENC_EC(ret);
  38264. ret = mp_add(a, NULL, NULL);
  38265. if (ret != MP_VAL)
  38266. return WC_TEST_RET_ENC_EC(ret);
  38267. ret = mp_add(NULL, b, NULL);
  38268. if (ret != MP_VAL)
  38269. return WC_TEST_RET_ENC_EC(ret);
  38270. ret = mp_add(NULL, NULL, r);
  38271. if (ret != MP_VAL)
  38272. return WC_TEST_RET_ENC_EC(ret);
  38273. ret = mp_add(a, b, NULL);
  38274. if (ret != MP_VAL)
  38275. return WC_TEST_RET_ENC_EC(ret);
  38276. ret = mp_add(a, NULL, r);
  38277. if (ret != MP_VAL)
  38278. return WC_TEST_RET_ENC_EC(ret);
  38279. ret = mp_add(NULL, b, r);
  38280. if (ret != MP_VAL)
  38281. return WC_TEST_RET_ENC_EC(ret);
  38282. #endif
  38283. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  38284. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38285. ret = mp_sub(NULL, NULL, NULL);
  38286. if (ret != MP_VAL)
  38287. return WC_TEST_RET_ENC_EC(ret);
  38288. ret = mp_sub(a, NULL, NULL);
  38289. if (ret != MP_VAL)
  38290. return WC_TEST_RET_ENC_EC(ret);
  38291. ret = mp_sub(NULL, b, NULL);
  38292. if (ret != MP_VAL)
  38293. return WC_TEST_RET_ENC_EC(ret);
  38294. ret = mp_sub(NULL, NULL, r);
  38295. if (ret != MP_VAL)
  38296. return WC_TEST_RET_ENC_EC(ret);
  38297. ret = mp_sub(a, b, NULL);
  38298. if (ret != MP_VAL)
  38299. return WC_TEST_RET_ENC_EC(ret);
  38300. ret = mp_sub(a, NULL, r);
  38301. if (ret != MP_VAL)
  38302. return WC_TEST_RET_ENC_EC(ret);
  38303. ret = mp_sub(NULL, b, r);
  38304. if (ret != MP_VAL)
  38305. return WC_TEST_RET_ENC_EC(ret);
  38306. #endif
  38307. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  38308. defined(WOLFSSL_CUSTOM_CURVES))
  38309. ret = mp_addmod(NULL, NULL, NULL, NULL);
  38310. if (ret != MP_VAL)
  38311. return WC_TEST_RET_ENC_EC(ret);
  38312. ret = mp_addmod(a, NULL, NULL, NULL);
  38313. if (ret != MP_VAL)
  38314. return WC_TEST_RET_ENC_EC(ret);
  38315. ret = mp_addmod(NULL, b, NULL, NULL);
  38316. if (ret != MP_VAL)
  38317. return WC_TEST_RET_ENC_EC(ret);
  38318. ret = mp_addmod(NULL, NULL, b, NULL);
  38319. if (ret != MP_VAL)
  38320. return WC_TEST_RET_ENC_EC(ret);
  38321. ret = mp_addmod(NULL, NULL, NULL, a);
  38322. if (ret != MP_VAL)
  38323. return WC_TEST_RET_ENC_EC(ret);
  38324. ret = mp_addmod(a, b, b, NULL);
  38325. if (ret != MP_VAL)
  38326. return WC_TEST_RET_ENC_EC(ret);
  38327. ret = mp_addmod(a, b, NULL, a);
  38328. if (ret != MP_VAL)
  38329. return WC_TEST_RET_ENC_EC(ret);
  38330. ret = mp_addmod(a, NULL, b, a);
  38331. if (ret != MP_VAL)
  38332. return WC_TEST_RET_ENC_EC(ret);
  38333. ret = mp_addmod(NULL, b, b, a);
  38334. if (ret != MP_VAL)
  38335. return WC_TEST_RET_ENC_EC(ret);
  38336. #endif
  38337. #ifdef WOLFSSL_SP_MATH_ALL
  38338. ret = mp_submod(NULL, NULL, NULL, NULL);
  38339. if (ret != MP_VAL)
  38340. return WC_TEST_RET_ENC_EC(ret);
  38341. ret = mp_submod(a, NULL, NULL, NULL);
  38342. if (ret != MP_VAL)
  38343. return WC_TEST_RET_ENC_EC(ret);
  38344. ret = mp_submod(NULL, b, NULL, NULL);
  38345. if (ret != MP_VAL)
  38346. return WC_TEST_RET_ENC_EC(ret);
  38347. ret = mp_submod(NULL, NULL, b, NULL);
  38348. if (ret != MP_VAL)
  38349. return WC_TEST_RET_ENC_EC(ret);
  38350. ret = mp_submod(NULL, NULL, NULL, a);
  38351. if (ret != MP_VAL)
  38352. return WC_TEST_RET_ENC_EC(ret);
  38353. ret = mp_submod(a, b, b, NULL);
  38354. if (ret != MP_VAL)
  38355. return WC_TEST_RET_ENC_EC(ret);
  38356. ret = mp_submod(a, b, NULL, a);
  38357. if (ret != MP_VAL)
  38358. return WC_TEST_RET_ENC_EC(ret);
  38359. ret = mp_submod(a, NULL, b, a);
  38360. if (ret != MP_VAL)
  38361. return WC_TEST_RET_ENC_EC(ret);
  38362. ret = mp_submod(NULL, b, b, a);
  38363. if (ret != MP_VAL)
  38364. return WC_TEST_RET_ENC_EC(ret);
  38365. #endif
  38366. #ifdef WOLFSSL_SP_MATH_ALL
  38367. ret = mp_div_2d(NULL, 1, a, b);
  38368. if (ret != MP_VAL)
  38369. return WC_TEST_RET_ENC_EC(ret);
  38370. ret = mp_mod_2d(NULL, 1, NULL);
  38371. if (ret != MP_VAL)
  38372. return WC_TEST_RET_ENC_EC(ret);
  38373. ret = mp_mod_2d(a, 1, NULL);
  38374. if (ret != MP_VAL)
  38375. return WC_TEST_RET_ENC_EC(ret);
  38376. ret = mp_mod_2d(NULL, 1, b);
  38377. if (ret != MP_VAL)
  38378. return WC_TEST_RET_ENC_EC(ret);
  38379. ret = mp_mul_2d(NULL, 1, NULL);
  38380. if (ret != MP_VAL)
  38381. return WC_TEST_RET_ENC_EC(ret);
  38382. ret = mp_mul_2d(a, 1, NULL);
  38383. if (ret != MP_VAL)
  38384. return WC_TEST_RET_ENC_EC(ret);
  38385. ret = mp_mul_2d(NULL, 1, b);
  38386. if (ret != MP_VAL)
  38387. return WC_TEST_RET_ENC_EC(ret);
  38388. #endif
  38389. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38390. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38391. ret = mp_montgomery_reduce(NULL, NULL, 1);
  38392. if (ret != MP_VAL)
  38393. return WC_TEST_RET_ENC_EC(ret);
  38394. ret = mp_montgomery_reduce(a, NULL, 1);
  38395. if (ret != MP_VAL)
  38396. return WC_TEST_RET_ENC_EC(ret);
  38397. ret = mp_montgomery_reduce(NULL, b, 1);
  38398. if (ret != MP_VAL)
  38399. return WC_TEST_RET_ENC_EC(ret);
  38400. mp_zero(b);
  38401. ret = mp_montgomery_reduce(a, b, 1);
  38402. if (ret != MP_VAL)
  38403. return WC_TEST_RET_ENC_EC(ret);
  38404. #endif
  38405. #ifdef WOLFSSL_SP_MATH_ALL
  38406. ret = mp_montgomery_setup(NULL, NULL);
  38407. if (ret != MP_VAL)
  38408. return WC_TEST_RET_ENC_EC(ret);
  38409. ret = mp_montgomery_setup(a, NULL);
  38410. if (ret != MP_VAL)
  38411. return WC_TEST_RET_ENC_EC(ret);
  38412. ret = mp_montgomery_setup(NULL, &rho);
  38413. if (ret != MP_VAL)
  38414. return WC_TEST_RET_ENC_EC(ret);
  38415. ret = mp_montgomery_calc_normalization(NULL, NULL);
  38416. if (ret != MP_VAL)
  38417. return WC_TEST_RET_ENC_EC(ret);
  38418. ret = mp_montgomery_calc_normalization(a, NULL);
  38419. if (ret != MP_VAL)
  38420. return WC_TEST_RET_ENC_EC(ret);
  38421. ret = mp_montgomery_calc_normalization(NULL, b);
  38422. if (ret != MP_VAL)
  38423. return WC_TEST_RET_ENC_EC(ret);
  38424. #endif
  38425. ret = mp_unsigned_bin_size(NULL);
  38426. if (ret != 0)
  38427. return WC_TEST_RET_ENC_EC(ret);
  38428. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  38429. ret = mp_tohex(NULL, NULL);
  38430. if (ret != MP_VAL)
  38431. return WC_TEST_RET_ENC_EC(ret);
  38432. ret = mp_tohex(a, NULL);
  38433. if (ret != MP_VAL)
  38434. return WC_TEST_RET_ENC_EC(ret);
  38435. ret = mp_tohex(NULL, hexStr);
  38436. if (ret != MP_VAL)
  38437. return WC_TEST_RET_ENC_EC(ret);
  38438. #endif
  38439. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  38440. ret = mp_todecimal(NULL, NULL);
  38441. if (ret != MP_VAL)
  38442. return WC_TEST_RET_ENC_EC(ret);
  38443. ret = mp_todecimal(a, NULL);
  38444. if (ret != MP_VAL)
  38445. return WC_TEST_RET_ENC_EC(ret);
  38446. ret = mp_todecimal(NULL, decStr);
  38447. if (ret != MP_VAL)
  38448. return WC_TEST_RET_ENC_EC(ret);
  38449. #endif
  38450. #ifdef WOLFSSL_SP_MATH_ALL
  38451. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  38452. if (ret != MP_VAL)
  38453. return WC_TEST_RET_ENC_EC(ret);
  38454. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  38455. if (ret != MP_VAL)
  38456. return WC_TEST_RET_ENC_EC(ret);
  38457. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  38458. if (ret != MP_VAL)
  38459. return WC_TEST_RET_ENC_EC(ret);
  38460. ret = mp_toradix(a, hexStr, 3);
  38461. if (ret != MP_VAL)
  38462. return WC_TEST_RET_ENC_EC(ret);
  38463. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  38464. if (ret != MP_VAL)
  38465. return WC_TEST_RET_ENC_EC(ret);
  38466. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  38467. if (ret != MP_VAL)
  38468. return WC_TEST_RET_ENC_EC(ret);
  38469. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  38470. if (ret != MP_VAL)
  38471. return WC_TEST_RET_ENC_EC(ret);
  38472. ret = mp_radix_size(a, 3, &size);
  38473. if (ret != MP_VAL)
  38474. return WC_TEST_RET_ENC_EC(ret);
  38475. #endif
  38476. return 0;
  38477. }
  38478. #endif
  38479. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38480. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  38481. {
  38482. int i, j;
  38483. wc_test_ret_t ret;
  38484. mp_zero(a);
  38485. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38486. if (mp_is_bit_set(a, i))
  38487. return WC_TEST_RET_ENC_NC;
  38488. for (j = 0; j < i; j++) {
  38489. if (!mp_is_bit_set(a, j))
  38490. return WC_TEST_RET_ENC_NC;
  38491. }
  38492. if (mp_set_bit(a, i) != 0)
  38493. return WC_TEST_RET_ENC_NC;
  38494. if (!mp_is_bit_set(a, i))
  38495. return WC_TEST_RET_ENC_NC;
  38496. }
  38497. mp_zero(a);
  38498. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38499. if (mp_is_bit_set(a, i))
  38500. return WC_TEST_RET_ENC_NC;
  38501. }
  38502. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38503. mp_zero(a);
  38504. if (mp_set_bit(a, i) != 0)
  38505. return WC_TEST_RET_ENC_NC;
  38506. for (j = 0; j < i; j++) {
  38507. if (mp_is_bit_set(a, j))
  38508. return WC_TEST_RET_ENC_NC;
  38509. }
  38510. if (!mp_is_bit_set(a, i))
  38511. return WC_TEST_RET_ENC_NC;
  38512. }
  38513. #ifdef WOLFSSL_KEY_GEN
  38514. for (i = 0; i < DIGIT_BIT * 2; i++) {
  38515. mp_set(a, 1);
  38516. ret = mp_2expt(a, i);
  38517. if (ret != 0)
  38518. return WC_TEST_RET_ENC_EC(ret);
  38519. for (j = 0; j < i; j++) {
  38520. if (mp_is_bit_set(a, j))
  38521. return WC_TEST_RET_ENC_NC;
  38522. }
  38523. if (!mp_is_bit_set(a, i))
  38524. return WC_TEST_RET_ENC_NC;
  38525. }
  38526. #endif
  38527. #ifdef WOLFSSL_SP_MATH
  38528. mp_zero(a);
  38529. for (j = 1; j <= 3; j++) {
  38530. i = SP_INT_MAX_BITS - j;
  38531. if (mp_is_bit_set(a, i))
  38532. return WC_TEST_RET_ENC_NC;
  38533. if (mp_set_bit(a, i) != 0)
  38534. return WC_TEST_RET_ENC_NC;
  38535. if (!mp_is_bit_set(a, i))
  38536. return WC_TEST_RET_ENC_NC;
  38537. #ifdef WOLFSSL_KEY_GEN
  38538. ret = mp_2expt(a, i);
  38539. if (ret != 0)
  38540. return WC_TEST_RET_ENC_EC(ret);
  38541. if (!mp_is_bit_set(a, i))
  38542. return WC_TEST_RET_ENC_NC;
  38543. #endif
  38544. }
  38545. mp_zero(a);
  38546. for (j = 0; j <= 3; j++) {
  38547. i = SP_INT_MAX_BITS + j;
  38548. if (mp_is_bit_set(a, i))
  38549. return WC_TEST_RET_ENC_NC;
  38550. if (mp_set_bit(a, i) != MP_VAL)
  38551. return WC_TEST_RET_ENC_NC;
  38552. #ifdef WOLFSSL_KEY_GEN
  38553. ret = mp_2expt(a, i);
  38554. if (ret != MP_VAL)
  38555. return WC_TEST_RET_ENC_EC(ret);
  38556. #endif
  38557. }
  38558. #endif
  38559. (void)ret;
  38560. return 0;
  38561. }
  38562. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  38563. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  38564. {
  38565. wc_test_ret_t ret;
  38566. mp_zero(a);
  38567. mp_zero(b);
  38568. ret = mp_cmp_d(a, 0);
  38569. if (ret != MP_EQ)
  38570. return WC_TEST_RET_ENC_EC(ret);
  38571. ret = mp_cmp_d(a, 1);
  38572. if (ret != MP_LT)
  38573. return WC_TEST_RET_ENC_EC(ret);
  38574. ret = mp_cmp(a, b);
  38575. if (ret != MP_EQ)
  38576. return WC_TEST_RET_ENC_NC;
  38577. mp_set(a, 1);
  38578. ret = mp_cmp_d(a, 0);
  38579. if (ret != MP_GT)
  38580. return WC_TEST_RET_ENC_EC(ret);
  38581. ret = mp_cmp_d(a, 1);
  38582. if (ret != MP_EQ)
  38583. return WC_TEST_RET_ENC_EC(ret);
  38584. ret = mp_cmp_d(a, 2);
  38585. if (ret != MP_LT)
  38586. return WC_TEST_RET_ENC_EC(ret);
  38587. ret = mp_cmp(a, b);
  38588. if (ret != MP_GT)
  38589. return WC_TEST_RET_ENC_NC;
  38590. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  38591. ret = mp_cmp_d(b, -1);
  38592. if (ret != MP_GT)
  38593. return WC_TEST_RET_ENC_EC(ret);
  38594. ret = mp_cmp(a, b);
  38595. if (ret != MP_LT)
  38596. return WC_TEST_RET_ENC_NC;
  38597. ret = mp_cmp(b, a);
  38598. if (ret != MP_GT)
  38599. return WC_TEST_RET_ENC_NC;
  38600. ret = mp_cmp(b, b);
  38601. if (ret != MP_EQ)
  38602. return WC_TEST_RET_ENC_NC;
  38603. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  38604. defined(WOLFSSL_SP_INT_NEGATIVE)
  38605. mp_read_radix(a, "-1", MP_RADIX_HEX);
  38606. mp_read_radix(a, "1", MP_RADIX_HEX);
  38607. ret = mp_cmp(a, b);
  38608. if (ret != MP_LT)
  38609. return WC_TEST_RET_ENC_NC;
  38610. ret = mp_cmp(b, a);
  38611. if (ret != MP_GT)
  38612. return WC_TEST_RET_ENC_NC;
  38613. mp_read_radix(b, "-2", MP_RADIX_HEX);
  38614. ret = mp_cmp(a, b);
  38615. if (ret != MP_GT)
  38616. return WC_TEST_RET_ENC_NC;
  38617. ret = mp_cmp(b, a);
  38618. if (ret != MP_LT)
  38619. return WC_TEST_RET_ENC_NC;
  38620. mp_read_radix(a, "-2", MP_RADIX_HEX);
  38621. ret = mp_cmp(a, b);
  38622. if (ret != MP_EQ)
  38623. return WC_TEST_RET_ENC_NC;
  38624. #endif
  38625. return 0;
  38626. }
  38627. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38628. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  38629. {
  38630. wc_test_ret_t ret;
  38631. int i, j, k;
  38632. #ifndef WOLFSSL_SP_MATH
  38633. for (i = 0; i < 10; i++) {
  38634. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38635. ret = randNum(a, j, rng, NULL);
  38636. if (ret != MP_OKAY)
  38637. return WC_TEST_RET_ENC_EC(ret);
  38638. mp_copy(a, b);
  38639. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  38640. ret = mp_mul_2d(a, k, a);
  38641. if (ret != MP_OKAY)
  38642. return WC_TEST_RET_ENC_EC(ret);
  38643. mp_rshb(a, k);
  38644. ret = mp_cmp(a, b);
  38645. if (ret != MP_EQ)
  38646. return WC_TEST_RET_ENC_NC;
  38647. }
  38648. }
  38649. }
  38650. #endif
  38651. for (i = 0; i < 10; i++) {
  38652. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38653. ret = randNum(a, j, rng, NULL);
  38654. if (ret != MP_OKAY)
  38655. return WC_TEST_RET_ENC_EC(ret);
  38656. mp_copy(a, b);
  38657. for (k = 0; k < 10; k++) {
  38658. ret = mp_lshd(a, k);
  38659. if (ret != MP_OKAY)
  38660. return WC_TEST_RET_ENC_EC(ret);
  38661. #ifndef WOLFSSL_SP_MATH
  38662. mp_rshd(a, k);
  38663. #else
  38664. mp_rshb(a, k * SP_WORD_SIZE);
  38665. #endif
  38666. ret = mp_cmp(a, b);
  38667. if (ret != MP_EQ)
  38668. return WC_TEST_RET_ENC_NC;
  38669. }
  38670. }
  38671. }
  38672. #ifndef WOLFSSL_SP_MATH
  38673. mp_zero(a);
  38674. mp_rshd(a, 1);
  38675. if (!mp_iszero(a))
  38676. return WC_TEST_RET_ENC_NC;
  38677. mp_set(a, 1);
  38678. mp_rshd(a, 1);
  38679. if (!mp_iszero(a))
  38680. return WC_TEST_RET_ENC_NC;
  38681. mp_set(a, 1);
  38682. mp_rshd(a, 2);
  38683. if (!mp_iszero(a))
  38684. return WC_TEST_RET_ENC_NC;
  38685. #endif
  38686. return 0;
  38687. }
  38688. #endif
  38689. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  38690. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  38691. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  38692. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  38693. WC_RNG* rng)
  38694. {
  38695. wc_test_ret_t ret;
  38696. int i, j, k;
  38697. mp_zero(a);
  38698. mp_zero(d);
  38699. ret = mp_div(a, d, r, rem);
  38700. if (ret != MP_VAL)
  38701. return WC_TEST_RET_ENC_EC(ret);
  38702. mp_set(d, 1);
  38703. ret = mp_div(a, d, r, rem);
  38704. if (ret != MP_OKAY)
  38705. return WC_TEST_RET_ENC_EC(ret);
  38706. if (!mp_iszero(r))
  38707. return WC_TEST_RET_ENC_EC(ret);
  38708. if (!mp_iszero(rem))
  38709. return WC_TEST_RET_ENC_EC(ret);
  38710. mp_set(a, 1);
  38711. ret = mp_div(a, d, r, rem);
  38712. if (ret != MP_OKAY)
  38713. return WC_TEST_RET_ENC_EC(ret);
  38714. if (!mp_isone(r))
  38715. return WC_TEST_RET_ENC_EC(ret);
  38716. if (!mp_iszero(rem))
  38717. return WC_TEST_RET_ENC_EC(ret);
  38718. for (i = 0; i < 100; i++) {
  38719. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  38720. ret = randNum(d, j, rng, NULL);
  38721. if (ret != MP_OKAY)
  38722. return WC_TEST_RET_ENC_EC(ret);
  38723. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  38724. ret = randNum(a, k, rng, NULL);
  38725. if (ret != MP_OKAY)
  38726. return WC_TEST_RET_ENC_EC(ret);
  38727. ret = mp_div(a, d, NULL, rem);
  38728. if (ret != MP_OKAY)
  38729. return WC_TEST_RET_ENC_EC(ret);
  38730. ret = mp_div(a, d, r, NULL);
  38731. if (ret != MP_OKAY)
  38732. return WC_TEST_RET_ENC_EC(ret);
  38733. ret = mp_div(a, d, r, rem);
  38734. if (ret != MP_OKAY)
  38735. return WC_TEST_RET_ENC_EC(ret);
  38736. mp_mul(r, d, r);
  38737. mp_add(r, rem, r);
  38738. if (mp_cmp(r, a) != MP_EQ)
  38739. return WC_TEST_RET_ENC_NC;
  38740. }
  38741. }
  38742. }
  38743. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  38744. if (ret != MP_OKAY)
  38745. return WC_TEST_RET_ENC_EC(ret);
  38746. mp_add(d, d, a);
  38747. mp_set(rem, 1);
  38748. mp_div(a, d, NULL, rem);
  38749. if (ret != MP_OKAY)
  38750. return WC_TEST_RET_ENC_EC(ret);
  38751. if (!mp_iszero(rem))
  38752. return WC_TEST_RET_ENC_EC(ret);
  38753. mp_set(r, 1);
  38754. mp_div(a, d, r, NULL);
  38755. if (ret != MP_OKAY)
  38756. return WC_TEST_RET_ENC_EC(ret);
  38757. if (mp_cmp_d(r, 2) != MP_EQ)
  38758. return WC_TEST_RET_ENC_EC(ret);
  38759. mp_set(r, 1);
  38760. mp_set(rem, 1);
  38761. mp_div(a, d, r, rem);
  38762. if (ret != MP_OKAY)
  38763. return WC_TEST_RET_ENC_EC(ret);
  38764. if (mp_cmp_d(r, 2) != MP_EQ)
  38765. return WC_TEST_RET_ENC_EC(ret);
  38766. if (!mp_iszero(rem))
  38767. return WC_TEST_RET_ENC_EC(ret);
  38768. mp_set(a, 0xfe);
  38769. mp_lshd(a, 3);
  38770. mp_add_d(a, 0xff, a);
  38771. mp_set(d, 0xfe);
  38772. mp_lshd(d, 2);
  38773. ret = mp_div(a, d, r, rem);
  38774. if (ret != MP_OKAY)
  38775. return WC_TEST_RET_ENC_EC(ret);
  38776. mp_mul(r, d, d);
  38777. mp_add(rem, d, d);
  38778. if (mp_cmp(a, d) != MP_EQ)
  38779. return WC_TEST_RET_ENC_NC;
  38780. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  38781. * a digit. So mp_div must detect and handle.
  38782. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  38783. */
  38784. mp_zero(a);
  38785. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  38786. mp_zero(d);
  38787. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  38788. mp_add_d(d, 1, d);
  38789. ret = mp_div(a, d, r, rem);
  38790. if (ret != MP_OKAY)
  38791. return WC_TEST_RET_ENC_EC(ret);
  38792. /* Make sure [d | d] / d is handled. */
  38793. mp_zero(a);
  38794. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  38795. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  38796. mp_zero(d);
  38797. mp_set_bit(d, DIGIT_BIT - 1);
  38798. ret = mp_div(a, d, r, rem);
  38799. if (ret != MP_OKAY)
  38800. return WC_TEST_RET_ENC_EC(ret);
  38801. mp_zero(a);
  38802. mp_set_bit(a, DIGIT_BIT);
  38803. mp_set_bit(a, 0);
  38804. mp_zero(d);
  38805. if (mp_cmp(r, a) != MP_EQ)
  38806. return WC_TEST_RET_ENC_NC;
  38807. if (mp_cmp(rem, d) != MP_EQ)
  38808. return WC_TEST_RET_ENC_NC;
  38809. return 0;
  38810. }
  38811. #endif
  38812. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38813. !defined(WC_NO_RNG)
  38814. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  38815. {
  38816. wc_test_ret_t ret;
  38817. int res;
  38818. ret = mp_rand_prime(a, 1, rng, NULL);
  38819. #if defined(WOLFSSL_SP_MATH_ALL)
  38820. if (ret != 0)
  38821. return WC_TEST_RET_ENC_EC(ret);
  38822. #else
  38823. if (ret != MP_VAL)
  38824. return WC_TEST_RET_ENC_NC;
  38825. #endif
  38826. #ifndef WOLFSSL_SP_MATH
  38827. ret = mp_rand_prime(a, -5, rng, NULL);
  38828. if (ret != 0 || (a->dp[0] & 3) != 3)
  38829. return WC_TEST_RET_ENC_NC;
  38830. #endif
  38831. ret = mp_prime_is_prime(a, 1, &res);
  38832. if (ret != MP_OKAY)
  38833. return WC_TEST_RET_ENC_EC(ret);
  38834. #ifndef WOLFSSL_SP_MATH
  38835. if (res != MP_YES)
  38836. return WC_TEST_RET_ENC_EC(res);
  38837. #else
  38838. if (res != MP_NO)
  38839. return WC_TEST_RET_ENC_EC(res);
  38840. #endif
  38841. ret = mp_prime_is_prime(a, 0, &res);
  38842. if (ret != MP_VAL)
  38843. return WC_TEST_RET_ENC_EC(ret);
  38844. ret = mp_prime_is_prime(a, -1, &res);
  38845. if (ret != MP_VAL)
  38846. return WC_TEST_RET_ENC_EC(ret);
  38847. ret = mp_prime_is_prime(a, 257, &res);
  38848. if (ret != MP_VAL)
  38849. return WC_TEST_RET_ENC_EC(ret);
  38850. mp_set(a, 1);
  38851. ret = mp_prime_is_prime(a, 1, &res);
  38852. if (ret != MP_OKAY)
  38853. return WC_TEST_RET_ENC_EC(ret);
  38854. if (res != MP_NO)
  38855. return WC_TEST_RET_ENC_EC(res);
  38856. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38857. if (ret != MP_OKAY)
  38858. return WC_TEST_RET_ENC_EC(ret);
  38859. if (res != MP_NO)
  38860. return WC_TEST_RET_ENC_EC(res);
  38861. mp_set(a, 2);
  38862. ret = mp_prime_is_prime(a, 1, &res);
  38863. if (ret != MP_OKAY)
  38864. return WC_TEST_RET_ENC_EC(ret);
  38865. if (res != MP_YES)
  38866. return WC_TEST_RET_ENC_EC(res);
  38867. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38868. if (ret != MP_OKAY)
  38869. return WC_TEST_RET_ENC_EC(ret);
  38870. if (res != MP_YES)
  38871. return WC_TEST_RET_ENC_EC(res);
  38872. mp_set(a, 0xfb);
  38873. ret = mp_prime_is_prime(a, 1, &res);
  38874. if (ret != MP_OKAY)
  38875. return WC_TEST_RET_ENC_EC(ret);
  38876. if (res != MP_YES)
  38877. return WC_TEST_RET_ENC_EC(res);
  38878. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38879. if (ret != MP_OKAY)
  38880. return WC_TEST_RET_ENC_EC(ret);
  38881. if (res != MP_YES)
  38882. return WC_TEST_RET_ENC_EC(res);
  38883. mp_set(a, 0x6);
  38884. ret = mp_prime_is_prime(a, 1, &res);
  38885. if (ret != MP_OKAY)
  38886. return WC_TEST_RET_ENC_EC(ret);
  38887. if (res != MP_NO)
  38888. return WC_TEST_RET_ENC_EC(res);
  38889. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38890. if (ret != MP_OKAY)
  38891. return WC_TEST_RET_ENC_EC(ret);
  38892. if (res != MP_NO)
  38893. return WC_TEST_RET_ENC_EC(res);
  38894. mp_set_int(a, 0x655 * 0x65b);
  38895. ret = mp_prime_is_prime(a, 10, &res);
  38896. if (ret != MP_OKAY)
  38897. return WC_TEST_RET_ENC_EC(ret);
  38898. if (res != MP_NO)
  38899. return WC_TEST_RET_ENC_EC(res);
  38900. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  38901. if (ret != MP_OKAY)
  38902. return WC_TEST_RET_ENC_EC(ret);
  38903. if (res != MP_NO)
  38904. return WC_TEST_RET_ENC_EC(res);
  38905. return 0;
  38906. }
  38907. #endif
  38908. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38909. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  38910. WC_RNG* rng)
  38911. {
  38912. wc_test_ret_t ret;
  38913. int i;
  38914. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  38915. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  38916. { 12, 56, 168 }, { 56, 12, 168 }
  38917. };
  38918. (void)exp;
  38919. mp_set(a, 0);
  38920. mp_set(b, 1);
  38921. ret = mp_lcm(a, a, r);
  38922. if (ret != MP_VAL)
  38923. return WC_TEST_RET_ENC_EC(ret);
  38924. ret = mp_lcm(a, b, r);
  38925. if (ret != MP_VAL)
  38926. return WC_TEST_RET_ENC_EC(ret);
  38927. ret = mp_lcm(b, a, r);
  38928. if (ret != MP_VAL)
  38929. return WC_TEST_RET_ENC_EC(ret);
  38930. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  38931. mp_set(a, kat[i][0]);
  38932. mp_set(b, kat[i][1]);
  38933. ret = mp_lcm(a, b, r);
  38934. if (ret != MP_OKAY)
  38935. return WC_TEST_RET_ENC_EC(ret);
  38936. mp_set(exp, kat[i][2]);
  38937. ret = mp_cmp(r, exp);
  38938. if (ret != MP_EQ)
  38939. return WC_TEST_RET_ENC_NC;
  38940. }
  38941. (void)rng;
  38942. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38943. !defined(WC_NO_RNG)
  38944. ret = mp_rand_prime(a, 20, rng, NULL);
  38945. if (ret != MP_OKAY)
  38946. return WC_TEST_RET_ENC_EC(ret);
  38947. ret = mp_rand_prime(b, 20, rng, NULL);
  38948. if (ret != MP_OKAY)
  38949. return WC_TEST_RET_ENC_EC(ret);
  38950. ret = mp_mul(a, b, exp);
  38951. if (ret != MP_OKAY)
  38952. return WC_TEST_RET_ENC_EC(ret);
  38953. ret = mp_lcm(a, b, r);
  38954. if (ret != MP_OKAY)
  38955. return WC_TEST_RET_ENC_EC(ret);
  38956. ret = mp_cmp(r, exp);
  38957. if (ret != MP_EQ)
  38958. return WC_TEST_RET_ENC_NC;
  38959. ret = mp_lcm(b, a, r);
  38960. if (ret != MP_OKAY)
  38961. return WC_TEST_RET_ENC_EC(ret);
  38962. ret = mp_cmp(r, exp);
  38963. if (ret != MP_EQ)
  38964. return WC_TEST_RET_ENC_NC;
  38965. #endif
  38966. mp_set(a, 11);
  38967. mp_zero(b);
  38968. ret = mp_gcd(a, b, r);
  38969. if (ret != MP_OKAY)
  38970. return WC_TEST_RET_ENC_EC(ret);
  38971. ret = mp_cmp_d(r, 11);
  38972. if (ret != MP_EQ)
  38973. return WC_TEST_RET_ENC_EC(ret);
  38974. ret = mp_gcd(b, a, r);
  38975. if (ret != MP_OKAY)
  38976. return WC_TEST_RET_ENC_EC(ret);
  38977. ret = mp_cmp_d(r, 11);
  38978. if (ret != MP_EQ)
  38979. return WC_TEST_RET_ENC_EC(ret);
  38980. ret = mp_gcd(b, b, r);
  38981. if (ret != MP_VAL)
  38982. return WC_TEST_RET_ENC_EC(ret);
  38983. return 0;
  38984. }
  38985. #endif
  38986. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  38987. defined(WOLFSSL_SP_MATH_ALL)
  38988. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  38989. {
  38990. wc_test_ret_t ret;
  38991. int i;
  38992. int j;
  38993. mp_set(a, 10);
  38994. ret = mp_mod_2d(a, 0, r);
  38995. if (ret != MP_OKAY)
  38996. return WC_TEST_RET_ENC_EC(ret);
  38997. if (!mp_iszero(r))
  38998. return WC_TEST_RET_ENC_NC;
  38999. ret = mp_mod_2d(a, 1, r);
  39000. if (ret != MP_OKAY)
  39001. return WC_TEST_RET_ENC_EC(ret);
  39002. if (!mp_iszero(r))
  39003. return WC_TEST_RET_ENC_NC;
  39004. ret = mp_mod_2d(a, 2, r);
  39005. if (ret != MP_OKAY)
  39006. return WC_TEST_RET_ENC_EC(ret);
  39007. ret = mp_cmp_d(r, 2);
  39008. if (ret != 0)
  39009. return WC_TEST_RET_ENC_EC(ret);
  39010. for (i = 2; i < 20; i++) {
  39011. ret = randNum(a, i, rng, NULL);
  39012. if (ret != 0)
  39013. return WC_TEST_RET_ENC_EC(ret);
  39014. for (j = 1; j <= mp_count_bits(a); j++) {
  39015. /* Get top part */
  39016. ret = mp_div_2d(a, j, t, NULL);
  39017. if (ret != 0)
  39018. return WC_TEST_RET_ENC_EC(ret);
  39019. ret = mp_mul_2d(t, j, t);
  39020. if (ret != 0)
  39021. return WC_TEST_RET_ENC_EC(ret);
  39022. /* Get bottom part */
  39023. ret = mp_mod_2d(a, j, r);
  39024. if (ret != 0)
  39025. return WC_TEST_RET_ENC_EC(ret);
  39026. /* Reassemble */
  39027. ret = mp_add(t, r, r);
  39028. if (ret != 0)
  39029. return WC_TEST_RET_ENC_EC(ret);
  39030. ret = mp_cmp(a, r);
  39031. if (ret != MP_EQ)
  39032. return WC_TEST_RET_ENC_NC;
  39033. }
  39034. }
  39035. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  39036. /* Test negative value being moded. */
  39037. for (j = 0; j < 20; j++) {
  39038. ret = randNum(a, 2, rng, NULL);
  39039. if (ret != 0)
  39040. return WC_TEST_RET_ENC_EC(ret);
  39041. a->sign = MP_NEG;
  39042. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  39043. ret = mp_mod_2d(a, i, r);
  39044. if (ret != 0)
  39045. return WC_TEST_RET_ENC_EC(ret);
  39046. mp_zero(t);
  39047. ret = mp_set_bit(t, i);
  39048. if (ret != 0)
  39049. return WC_TEST_RET_ENC_EC(ret);
  39050. ret = mp_mod(a, t, t);
  39051. if (ret != 0)
  39052. return WC_TEST_RET_ENC_EC(ret);
  39053. ret = mp_cmp(r, t);
  39054. if (ret != MP_EQ)
  39055. return WC_TEST_RET_ENC_NC;
  39056. }
  39057. }
  39058. #endif
  39059. return 0;
  39060. }
  39061. #endif
  39062. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  39063. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  39064. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  39065. {
  39066. wc_test_ret_t ret;
  39067. mp_digit r;
  39068. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  39069. mp_digit rem;
  39070. int i;
  39071. #endif
  39072. (void)rng;
  39073. ret = mp_set(a, 1);
  39074. if (ret != MP_OKAY)
  39075. return WC_TEST_RET_ENC_EC(ret);
  39076. ret = mp_mod_d(a, 0, &r);
  39077. if (ret != MP_VAL)
  39078. return WC_TEST_RET_ENC_EC(ret);
  39079. mp_zero(a);
  39080. ret = mp_mod_d(a, 1, &r);
  39081. if (ret != MP_OKAY)
  39082. return WC_TEST_RET_ENC_EC(ret);
  39083. ret = mp_mod_d(a, 3, &r);
  39084. if (ret != MP_OKAY)
  39085. return WC_TEST_RET_ENC_EC(ret);
  39086. ret = mp_mod_d(a, 5, &r);
  39087. if (ret != MP_OKAY)
  39088. return WC_TEST_RET_ENC_EC(ret);
  39089. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  39090. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  39091. ret = randNum(a, i, rng, NULL);
  39092. if (ret != MP_OKAY)
  39093. return WC_TEST_RET_ENC_EC(ret);
  39094. ret = mp_mod_d(a, 3, &r);
  39095. if (ret != MP_OKAY)
  39096. return WC_TEST_RET_ENC_EC(ret);
  39097. ret = mp_div_d(a, 3, a, &rem);
  39098. if (ret != MP_OKAY)
  39099. return WC_TEST_RET_ENC_EC(ret);
  39100. if (r != rem)
  39101. return WC_TEST_RET_ENC_NC;
  39102. }
  39103. #endif
  39104. return 0;
  39105. }
  39106. #endif
  39107. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  39108. WC_RNG* rng)
  39109. {
  39110. wc_test_ret_t ret;
  39111. int i;
  39112. for (i = 1; i < 16; i++) {
  39113. ret = randNum(a, i, rng, NULL);
  39114. if (ret != 0)
  39115. return WC_TEST_RET_ENC_EC(ret);
  39116. ret = mp_mul(a, a, r1);
  39117. if (ret != 0)
  39118. return WC_TEST_RET_ENC_EC(ret);
  39119. ret = mp_sqr(a, r2);
  39120. if (ret != 0)
  39121. return WC_TEST_RET_ENC_EC(ret);
  39122. ret = mp_cmp(r1, r2);
  39123. if (ret != MP_EQ)
  39124. return WC_TEST_RET_ENC_NC;
  39125. }
  39126. ret = mp_set(b, 0);
  39127. if (ret != MP_OKAY)
  39128. return WC_TEST_RET_ENC_EC(ret);
  39129. ret = mp_mul(a, b, r1);
  39130. if (ret != MP_OKAY)
  39131. return WC_TEST_RET_ENC_EC(ret);
  39132. if (!mp_iszero(r1))
  39133. return WC_TEST_RET_ENC_EC(ret);
  39134. ret = mp_sqr(b, r1);
  39135. if (ret != MP_OKAY)
  39136. return WC_TEST_RET_ENC_EC(ret);
  39137. if (!mp_iszero(r1))
  39138. return WC_TEST_RET_ENC_NC;
  39139. #ifdef WOLFSSL_SP_MATH_ALL
  39140. ret = mp_set(a, 1);
  39141. if (ret != MP_OKAY)
  39142. return WC_TEST_RET_ENC_EC(ret);
  39143. i = (SP_INT_DIGITS / 2) + 1;
  39144. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  39145. if (ret != MP_OKAY)
  39146. return WC_TEST_RET_ENC_EC(ret);
  39147. ret = mp_set(b, 1);
  39148. if (ret != MP_OKAY)
  39149. return WC_TEST_RET_ENC_EC(ret);
  39150. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  39151. if (ret != MP_OKAY)
  39152. return WC_TEST_RET_ENC_EC(ret);
  39153. ret = mp_mul(a, b, r1);
  39154. if (ret != MP_OKAY)
  39155. return WC_TEST_RET_ENC_EC(ret);
  39156. ret = mp_mul(a, a, r1);
  39157. if (ret == MP_OKAY)
  39158. return WC_TEST_RET_ENC_NC;
  39159. ret = mp_sqr(a, r1);
  39160. if (ret == MP_OKAY)
  39161. return WC_TEST_RET_ENC_NC;
  39162. ret = mp_sqr(b, r1);
  39163. if (ret != MP_OKAY)
  39164. return WC_TEST_RET_ENC_EC(ret);
  39165. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39166. (defined(HAVE_ECC) && defined(FP_ECC))
  39167. ret = mp_mulmod(a, b, b, r1);
  39168. if (ret != MP_OKAY)
  39169. return WC_TEST_RET_ENC_EC(ret);
  39170. ret = mp_mulmod(a, a, b, r1);
  39171. if (ret == MP_OKAY)
  39172. return WC_TEST_RET_ENC_NC;
  39173. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  39174. ret = mp_sqrmod(a, b, r1);
  39175. if (ret == MP_OKAY)
  39176. return WC_TEST_RET_ENC_NC;
  39177. ret = mp_sqrmod(b, a, r1);
  39178. if (ret != MP_OKAY)
  39179. return WC_TEST_RET_ENC_EC(ret);
  39180. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  39181. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  39182. #endif /* WOLFSSL_SP_MATH_ALL */
  39183. return 0;
  39184. }
  39185. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39186. defined(OPENSSL_EXTRA)
  39187. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  39188. {
  39189. wc_test_ret_t ret;
  39190. mp_set(a, 0);
  39191. mp_set(m, 1);
  39192. ret = mp_invmod(a, m, r);
  39193. if (ret != MP_VAL)
  39194. return WC_TEST_RET_ENC_EC(ret);
  39195. ret = mp_invmod(m, a, r);
  39196. if (ret != MP_VAL)
  39197. return WC_TEST_RET_ENC_EC(ret);
  39198. mp_set(a, 2);
  39199. mp_set(m, 4);
  39200. ret = mp_invmod(a, m, r);
  39201. if (ret != MP_VAL)
  39202. return WC_TEST_RET_ENC_EC(ret);
  39203. mp_set(a, 3);
  39204. mp_set(m, 6);
  39205. ret = mp_invmod(a, m, r);
  39206. if (ret != MP_VAL)
  39207. return WC_TEST_RET_ENC_EC(ret);
  39208. mp_set(a, 5*9);
  39209. mp_set(m, 6*9);
  39210. ret = mp_invmod(a, m, r);
  39211. if (ret != MP_VAL)
  39212. return WC_TEST_RET_ENC_EC(ret);
  39213. mp_set(a, 1);
  39214. mp_set(m, 4);
  39215. ret = mp_invmod(a, m, r);
  39216. if (ret != MP_OKAY)
  39217. return WC_TEST_RET_ENC_EC(ret);
  39218. if (!mp_isone(r))
  39219. return WC_TEST_RET_ENC_NC;
  39220. mp_set(a, 3);
  39221. mp_set(m, 4);
  39222. ret = mp_invmod(a, m, r);
  39223. if (ret != MP_OKAY)
  39224. return WC_TEST_RET_ENC_EC(ret);
  39225. ret = mp_cmp_d(r, 3);
  39226. if (ret != 0)
  39227. return WC_TEST_RET_ENC_EC(ret);
  39228. mp_set(a, 3);
  39229. mp_set(m, 5);
  39230. ret = mp_invmod(a, m, r);
  39231. if (ret != MP_OKAY)
  39232. return WC_TEST_RET_ENC_EC(ret);
  39233. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39234. /* Maximum 'a' */
  39235. mp_set(a, 0);
  39236. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  39237. mp_sub_d(a, 1, a);
  39238. /* Modulus too big. */
  39239. mp_set(m, 0);
  39240. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  39241. ret = mp_invmod(a, m, r);
  39242. if (ret != MP_VAL)
  39243. return WC_TEST_RET_ENC_EC(ret);
  39244. /* Maximum modulus - even. */
  39245. mp_set(m, 0);
  39246. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  39247. ret = mp_invmod(a, m, r);
  39248. if (ret != MP_OKAY)
  39249. return WC_TEST_RET_ENC_EC(ret);
  39250. #endif
  39251. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  39252. mp_read_radix(a, "-3", 16);
  39253. ret = mp_invmod(a, m, r);
  39254. if (ret != MP_OKAY)
  39255. return WC_TEST_RET_ENC_EC(ret);
  39256. #endif
  39257. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  39258. mp_set(a, 0);
  39259. mp_set(m, 3);
  39260. ret = mp_invmod_mont_ct(a, m, r, 1);
  39261. if (ret != MP_VAL)
  39262. return WC_TEST_RET_ENC_EC(ret);
  39263. mp_set(a, 1);
  39264. mp_set(m, 0);
  39265. ret = mp_invmod_mont_ct(a, m, r, 1);
  39266. if (ret != MP_VAL)
  39267. return WC_TEST_RET_ENC_EC(ret);
  39268. mp_set(a, 1);
  39269. mp_set(m, 1);
  39270. ret = mp_invmod_mont_ct(a, m, r, 1);
  39271. if (ret != MP_VAL)
  39272. return WC_TEST_RET_ENC_EC(ret);
  39273. mp_set(a, 1);
  39274. mp_set(m, 2);
  39275. ret = mp_invmod_mont_ct(a, m, r, 1);
  39276. if (ret != MP_VAL)
  39277. return WC_TEST_RET_ENC_EC(ret);
  39278. mp_set(a, 1);
  39279. mp_set(m, 3);
  39280. ret = mp_invmod_mont_ct(a, m, r, 1);
  39281. if (ret != MP_OKAY)
  39282. return WC_TEST_RET_ENC_EC(ret);
  39283. #endif
  39284. return 0;
  39285. }
  39286. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  39287. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39288. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39289. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  39290. {
  39291. wc_test_ret_t ret;
  39292. mp_set(b, 0x2);
  39293. mp_set(e, 0x3);
  39294. mp_set(m, 0x0);
  39295. ret = mp_exptmod_ex(b, e, 1, m, r);
  39296. if (ret != MP_VAL)
  39297. return WC_TEST_RET_ENC_EC(ret);
  39298. ret = mp_exptmod_nct(b, e, m, r);
  39299. if (ret != MP_VAL)
  39300. return WC_TEST_RET_ENC_EC(ret);
  39301. mp_set(b, 0x2);
  39302. mp_set(e, 0x3);
  39303. mp_set(m, 0x1);
  39304. ret = mp_exptmod_ex(b, e, 1, m, r);
  39305. if (ret != MP_OKAY)
  39306. return WC_TEST_RET_ENC_EC(ret);
  39307. if (!mp_iszero(r))
  39308. return WC_TEST_RET_ENC_NC;
  39309. ret = mp_exptmod_nct(b, e, m, r);
  39310. if (ret != MP_OKAY)
  39311. return WC_TEST_RET_ENC_EC(ret);
  39312. if (!mp_iszero(r))
  39313. return WC_TEST_RET_ENC_NC;
  39314. mp_set(b, 0x2);
  39315. mp_set(e, 0x0);
  39316. mp_set(m, 0x7);
  39317. ret = mp_exptmod_ex(b, e, 1, m, r);
  39318. if (ret != MP_OKAY)
  39319. return WC_TEST_RET_ENC_EC(ret);
  39320. if (!mp_isone(r))
  39321. return WC_TEST_RET_ENC_NC;
  39322. ret = mp_exptmod_nct(b, e, m, r);
  39323. if (ret != MP_OKAY)
  39324. return WC_TEST_RET_ENC_EC(ret);
  39325. if (!mp_isone(r))
  39326. return WC_TEST_RET_ENC_NC;
  39327. mp_set(b, 0x0);
  39328. mp_set(e, 0x3);
  39329. mp_set(m, 0x7);
  39330. ret = mp_exptmod_ex(b, e, 1, m, r);
  39331. if (ret != MP_OKAY)
  39332. return WC_TEST_RET_ENC_EC(ret);
  39333. if (!mp_iszero(r))
  39334. return WC_TEST_RET_ENC_NC;
  39335. ret = mp_exptmod_nct(b, e, m, r);
  39336. if (ret != MP_OKAY)
  39337. return WC_TEST_RET_ENC_EC(ret);
  39338. if (!mp_iszero(r))
  39339. return WC_TEST_RET_ENC_NC;
  39340. mp_set(b, 0x10);
  39341. mp_set(e, 0x3);
  39342. mp_set(m, 0x7);
  39343. ret = mp_exptmod_ex(b, e, 1, m, r);
  39344. if (ret != MP_OKAY)
  39345. return WC_TEST_RET_ENC_EC(ret);
  39346. ret = mp_exptmod_nct(b, e, m, r);
  39347. if (ret != MP_OKAY)
  39348. return WC_TEST_RET_ENC_EC(ret);
  39349. mp_set(b, 0x7);
  39350. mp_set(e, 0x3);
  39351. mp_set(m, 0x7);
  39352. ret = mp_exptmod_ex(b, e, 1, m, r);
  39353. if (ret != MP_OKAY)
  39354. return WC_TEST_RET_ENC_EC(ret);
  39355. if (!mp_iszero(r))
  39356. return WC_TEST_RET_ENC_NC;
  39357. ret = mp_exptmod_nct(b, e, m, r);
  39358. if (ret != MP_OKAY)
  39359. return WC_TEST_RET_ENC_EC(ret);
  39360. if (!mp_iszero(r))
  39361. return WC_TEST_RET_ENC_NC;
  39362. #ifndef WOLFSSL_SP_MATH
  39363. mp_set(b, 0x01);
  39364. mp_mul_2d(b, DIGIT_BIT, b);
  39365. mp_add_d(b, 1, b);
  39366. mp_set(e, 0x3);
  39367. mp_copy(b, m);
  39368. ret = mp_exptmod_ex(b, e, 1, m, r);
  39369. if (ret != MP_OKAY)
  39370. return WC_TEST_RET_ENC_EC(ret);
  39371. if (!mp_iszero(r))
  39372. return WC_TEST_RET_ENC_NC;
  39373. ret = mp_exptmod_nct(b, e, m, r);
  39374. if (ret != MP_OKAY)
  39375. return WC_TEST_RET_ENC_EC(ret);
  39376. if (!mp_iszero(r))
  39377. return WC_TEST_RET_ENC_NC;
  39378. #endif
  39379. mp_set(b, 0x2);
  39380. mp_set(e, 0x3);
  39381. mp_set(m, 0x7);
  39382. ret = mp_exptmod_ex(b, e, 1, m, r);
  39383. if (ret != MP_OKAY)
  39384. return WC_TEST_RET_ENC_EC(ret);
  39385. ret = mp_exptmod_nct(b, e, m, r);
  39386. if (ret != MP_OKAY)
  39387. return WC_TEST_RET_ENC_EC(ret);
  39388. #ifdef WOLFSSL_SP_MATH_ALL
  39389. mp_set(b, 0x2);
  39390. mp_set(e, 0x3);
  39391. mp_set(m, 0x01);
  39392. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  39393. mp_add_d(m, 0x01, m);
  39394. ret = mp_exptmod_ex(b, e, 1, m, r);
  39395. if (ret != MP_VAL)
  39396. return WC_TEST_RET_ENC_EC(ret);
  39397. ret = mp_exptmod_nct(b, e, m, r);
  39398. if (ret != MP_VAL)
  39399. return WC_TEST_RET_ENC_EC(ret);
  39400. #endif
  39401. return 0;
  39402. }
  39403. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  39404. * OPENSSL_EXTRA */
  39405. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39406. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39407. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  39408. {
  39409. wc_test_ret_t ret;
  39410. mp_digit mp;
  39411. static int exp[] = { 7, 8, 16, 27, 32, 64,
  39412. 127, 128, 255, 256,
  39413. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  39414. 383, 384, 2033, 2048
  39415. #endif
  39416. };
  39417. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  39418. 0x01, 0x9f, 0x13, 0xbd,
  39419. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  39420. 0x1f, 0x13d, 0x45, 0x615
  39421. #endif
  39422. };
  39423. int bits[] = { 256, 384,
  39424. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  39425. 2048,
  39426. #endif
  39427. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  39428. 3072
  39429. #endif
  39430. };
  39431. int i;
  39432. int j;
  39433. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  39434. if (exp[i] >= DIGIT_BIT)
  39435. continue;
  39436. mp_zero(m);
  39437. ret = mp_set_bit(m, exp[i]);
  39438. if (ret != MP_OKAY)
  39439. return WC_TEST_RET_ENC_EC(ret);
  39440. ret = mp_sub_d(m, sub[i], m);
  39441. if (ret != MP_OKAY)
  39442. return WC_TEST_RET_ENC_EC(ret);
  39443. ret = mp_montgomery_setup(m, &mp);
  39444. if (ret != MP_OKAY)
  39445. return WC_TEST_RET_ENC_EC(ret);
  39446. ret = mp_montgomery_calc_normalization(n, m);
  39447. if (ret != MP_OKAY)
  39448. return WC_TEST_RET_ENC_EC(ret);
  39449. for (j = 0; j < 10; j++) {
  39450. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  39451. if (ret != 0)
  39452. return WC_TEST_RET_ENC_EC(ret);
  39453. ret = mp_mod(a, m, a);
  39454. if (ret != 0)
  39455. return WC_TEST_RET_ENC_EC(ret);
  39456. /* r = a * a */
  39457. ret = mp_sqrmod(a, m, r);
  39458. if (ret != MP_OKAY)
  39459. return WC_TEST_RET_ENC_EC(ret);
  39460. /* Convert to Montgomery form = a*n */
  39461. ret = mp_mulmod(a, n, m, a);
  39462. if (ret != MP_OKAY)
  39463. return WC_TEST_RET_ENC_EC(ret);
  39464. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  39465. ret = mp_sqr(a, a);
  39466. if (ret != MP_OKAY)
  39467. return WC_TEST_RET_ENC_EC(ret);
  39468. ret = mp_montgomery_reduce(a, m, mp);
  39469. if (ret != MP_OKAY)
  39470. return WC_TEST_RET_ENC_EC(ret);
  39471. ret = mp_montgomery_reduce(a, m, mp);
  39472. if (ret != MP_OKAY)
  39473. return WC_TEST_RET_ENC_EC(ret);
  39474. if (mp_cmp(a, r) != MP_EQ)
  39475. return WC_TEST_RET_ENC_NC;
  39476. }
  39477. }
  39478. /* Force carries. */
  39479. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  39480. /* a = 2^(bits*2) - 1 */
  39481. mp_zero(a);
  39482. mp_set_bit(a, bits[i] * 2);
  39483. mp_sub_d(a, 1, a);
  39484. /* m = 2^(bits) - 1 */
  39485. mp_zero(m);
  39486. mp_set_bit(m, bits[i]);
  39487. mp_sub_d(m, 1, m);
  39488. mp = 1;
  39489. /* result = r = 2^(bits) - 1 */
  39490. mp_zero(r);
  39491. mp_set_bit(r, bits[i]);
  39492. mp_sub_d(r, 1, r);
  39493. ret = mp_montgomery_reduce(a, m, mp);
  39494. if (ret != MP_OKAY)
  39495. return WC_TEST_RET_ENC_EC(ret);
  39496. /* Result is m or 0 if reduced to range of modulus. */
  39497. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  39498. return WC_TEST_RET_ENC_NC;
  39499. }
  39500. return 0;
  39501. }
  39502. #endif
  39503. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  39504. {
  39505. WC_RNG rng;
  39506. int rng_inited = 0;
  39507. wc_test_ret_t ret;
  39508. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  39509. int i, j;
  39510. #ifndef WOLFSSL_SP_MATH
  39511. int k;
  39512. #endif
  39513. mp_digit d = 0;
  39514. #endif
  39515. #ifdef WOLFSSL_SMALL_STACK
  39516. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39517. DYNAMIC_TYPE_TMP_BUFFER),
  39518. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39519. DYNAMIC_TYPE_TMP_BUFFER),
  39520. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39521. DYNAMIC_TYPE_TMP_BUFFER),
  39522. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39523. DYNAMIC_TYPE_TMP_BUFFER),
  39524. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39525. DYNAMIC_TYPE_TMP_BUFFER);
  39526. if ((a == NULL) ||
  39527. (b == NULL) ||
  39528. (r1 == NULL) ||
  39529. (r2 == NULL) ||
  39530. (p == NULL))
  39531. {
  39532. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  39533. }
  39534. #else
  39535. mp_int a[1], b[1], r1[1], r2[1], p[1];
  39536. #endif
  39537. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  39538. if (ret != 0)
  39539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39540. #ifdef WOLFSSL_SP_MATH_ALL
  39541. mp_init_copy(p, a);
  39542. #else
  39543. ret = mp_init(p);
  39544. if (ret != 0)
  39545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39546. #endif
  39547. #ifndef HAVE_FIPS
  39548. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  39549. #else
  39550. ret = wc_InitRng(&rng);
  39551. #endif
  39552. if (ret != 0)
  39553. goto done;
  39554. rng_inited = 1;
  39555. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  39556. mp_set_int(a, 0);
  39557. if (a->used != 0 || a->dp[0] != 0)
  39558. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39559. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  39560. for (i = 0; i < 4 * j; i++) {
  39561. /* New values to use. */
  39562. ret = randNum(p, j, &rng, NULL);
  39563. if (ret != 0)
  39564. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39565. ret = randNum(a, j, &rng, NULL);
  39566. if (ret != 0)
  39567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39568. ret = randNum(b, j, &rng, NULL);
  39569. if (ret != 0)
  39570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39571. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  39572. if (ret != 0)
  39573. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39574. d &= MP_MASK;
  39575. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  39576. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  39577. /* Ensure sqrmod produce same result as mulmod. */
  39578. ret = mp_sqrmod(a, p, r1);
  39579. if (ret != 0)
  39580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39581. ret = mp_mulmod(a, a, p, r2);
  39582. if (ret != 0)
  39583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39584. if (mp_cmp(r1, r2) != 0)
  39585. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39586. #endif
  39587. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39588. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  39589. !defined(WOLFSSL_SP_INT_NEGATIVE))
  39590. ret = mp_addmod(a, b, p, r1);
  39591. if (ret != 0)
  39592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39593. ret = mp_submod(r1, b, p, r2);
  39594. if (ret != 0)
  39595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39596. ret = mp_mod(a, p, r1);
  39597. if (ret != 0)
  39598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39599. if (mp_cmp(r1, r2) != MP_EQ)
  39600. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39601. #else
  39602. /* Ensure add with mod produce same result as sub with mod. */
  39603. ret = mp_addmod(a, b, p, r1);
  39604. if (ret != 0)
  39605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39606. b->sign ^= 1;
  39607. ret = mp_submod(a, b, p, r2);
  39608. if (ret != 0)
  39609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39610. if (mp_cmp(r1, r2) != 0)
  39611. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39612. #endif
  39613. #endif
  39614. /* Ensure add digit produce same result as sub digit. */
  39615. ret = mp_add_d(a, d, r1);
  39616. if (ret != 0)
  39617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39618. ret = mp_sub_d(r1, d, r2);
  39619. if (ret != 0)
  39620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39621. if (mp_cmp(a, r2) != 0)
  39622. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39623. /* Invert - if p is even it will use the slow impl.
  39624. * - if p and a are even it will fail.
  39625. */
  39626. ret = mp_invmod(a, p, r1);
  39627. if (ret != 0 && ret != MP_VAL)
  39628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39629. #ifndef WOLFSSL_SP_MATH
  39630. /* Shift up and down number all bits in a digit. */
  39631. for (k = 0; k < DIGIT_BIT; k++) {
  39632. mp_mul_2d(a, k, r1);
  39633. mp_div_2d(r1, k, r2, p);
  39634. if (mp_cmp(a, r2) != 0)
  39635. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39636. if (!mp_iszero(p))
  39637. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39638. mp_rshb(r1, k);
  39639. if (mp_cmp(a, r1) != 0)
  39640. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39641. }
  39642. #endif
  39643. }
  39644. }
  39645. /* Test adding and subtracting zero from zero. */
  39646. mp_zero(a);
  39647. ret = mp_add_d(a, 0, r1);
  39648. if (ret != 0)
  39649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39650. if (!mp_iszero(r1)) {
  39651. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39652. }
  39653. ret = mp_sub_d(a, 0, r2);
  39654. if (ret != 0)
  39655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39656. if (!mp_iszero(r2)) {
  39657. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39658. }
  39659. #if DIGIT_BIT >= 32
  39660. /* Check that setting a 32-bit digit works. */
  39661. d &= 0xffffffffU;
  39662. mp_set_int(a, d);
  39663. if (a->used != 1 || a->dp[0] != d)
  39664. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39665. #endif
  39666. /* Check setting a bit and testing a bit works. */
  39667. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  39668. mp_zero(a);
  39669. mp_set_bit(a, i);
  39670. if (!mp_is_bit_set(a, i))
  39671. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39672. }
  39673. #endif
  39674. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  39675. mp_zero(a);
  39676. i = mp_cnt_lsb(a);
  39677. if (i != 0)
  39678. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39679. mp_set(a, 1);
  39680. i = mp_cnt_lsb(a);
  39681. if (i != 0)
  39682. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39683. mp_set(a, 32);
  39684. i = mp_cnt_lsb(a);
  39685. if (i != 5)
  39686. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39687. mp_zero(a);
  39688. mp_set_bit(a, 129);
  39689. i = mp_cnt_lsb(a);
  39690. if (i != 129)
  39691. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39692. #endif
  39693. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39694. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  39695. goto done;
  39696. #endif
  39697. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  39698. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  39699. goto done;
  39700. #endif
  39701. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  39702. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39703. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  39704. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  39705. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  39706. goto done;
  39707. #endif
  39708. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  39709. defined(HAVE_ECC))
  39710. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  39711. goto done;
  39712. #endif
  39713. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  39714. goto done;
  39715. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  39716. goto done;
  39717. if ((ret = mp_test_read_to_bin(a)) != 0)
  39718. goto done;
  39719. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39720. if ((ret = mp_test_set_int(a)) != 0)
  39721. goto done;
  39722. #endif
  39723. if ((ret = mp_test_cmp(a, r1)) != 0)
  39724. goto done;
  39725. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39726. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  39727. goto done;
  39728. #endif
  39729. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39730. if ((ret = mp_test_set_is_bit(a)) != 0)
  39731. goto done;
  39732. #endif
  39733. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39734. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  39735. goto done;
  39736. #endif
  39737. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39738. !defined(WC_NO_RNG)
  39739. if ((ret = mp_test_prime(a, &rng)) != 0)
  39740. goto done;
  39741. #endif
  39742. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  39743. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  39744. goto done;
  39745. #endif
  39746. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39747. defined(WOLFSSL_SP_MATH_ALL)
  39748. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  39749. goto done;
  39750. #endif
  39751. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  39752. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  39753. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  39754. goto done;
  39755. #endif
  39756. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  39757. goto done;
  39758. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39759. defined(OPENSSL_EXTRA)
  39760. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  39761. goto done;
  39762. #endif
  39763. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39764. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39765. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  39766. goto done;
  39767. #endif
  39768. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39769. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39770. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  39771. goto done;
  39772. #endif
  39773. done:
  39774. #ifdef WOLFSSL_SMALL_STACK
  39775. if (p) {
  39776. mp_clear(p);
  39777. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39778. }
  39779. if (r2) {
  39780. mp_clear(r2);
  39781. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39782. }
  39783. if (r1) {
  39784. mp_clear(r1);
  39785. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39786. }
  39787. if (b) {
  39788. mp_clear(b);
  39789. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39790. }
  39791. if (a) {
  39792. mp_clear(a);
  39793. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39794. }
  39795. #else
  39796. mp_clear(p);
  39797. mp_clear(r2);
  39798. mp_clear(r1);
  39799. mp_clear(b);
  39800. mp_clear(a);
  39801. #endif
  39802. if (rng_inited)
  39803. wc_FreeRng(&rng);
  39804. return ret;
  39805. }
  39806. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  39807. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  39808. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  39809. typedef struct pairs_t {
  39810. const unsigned char* coeff;
  39811. int coeffSz;
  39812. int exp;
  39813. } pairs_t;
  39814. /*
  39815. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  39816. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  39817. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  39818. */
  39819. static const unsigned char c192a[] =
  39820. {
  39821. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  39822. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  39823. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  39824. };
  39825. static const unsigned char c0a[] =
  39826. {
  39827. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  39828. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  39829. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  39830. };
  39831. static const pairs_t ecPairsA[] =
  39832. {
  39833. {c192a, sizeof(c192a), 192},
  39834. {c0a, sizeof(c0a), 0}
  39835. };
  39836. static const int kA[] = {173, 293};
  39837. static const unsigned char controlPrime[] = {
  39838. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  39839. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  39840. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  39841. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  39842. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  39843. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  39844. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  39845. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  39846. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  39847. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  39848. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  39849. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  39850. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  39851. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  39852. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  39853. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  39854. };
  39855. static const unsigned char testOne[] = { 1 };
  39856. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  39857. {
  39858. wc_test_ret_t ret;
  39859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39860. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39861. if (ki == NULL)
  39862. return MEMORY_E;
  39863. #else
  39864. mp_int ki[1];
  39865. #endif
  39866. ret = mp_init(ki);
  39867. if (ret != 0)
  39868. ret = WC_TEST_RET_ENC_EC(ret);
  39869. if (ret == 0) {
  39870. ret = mp_set(ki, k);
  39871. if (ret != 0)
  39872. ret = WC_TEST_RET_ENC_EC(ret);
  39873. }
  39874. if (ret == 0) {
  39875. ret = mp_sub_d(p1, 1, p2);
  39876. if (ret != 0)
  39877. ret = WC_TEST_RET_ENC_EC(ret);
  39878. }
  39879. if (ret == 0) {
  39880. ret = mp_mul(p2, ki, p2);
  39881. if (ret != 0)
  39882. ret = WC_TEST_RET_ENC_EC(ret);
  39883. }
  39884. if (ret == 0) {
  39885. ret = mp_add_d(p2, 1, p2);
  39886. if (ret != 0)
  39887. ret = WC_TEST_RET_ENC_EC(ret);
  39888. }
  39889. mp_clear(ki);
  39890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39891. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39892. #endif
  39893. return ret;
  39894. }
  39895. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  39896. const pairs_t* ecPairs, int ecPairsSz,
  39897. const int* k)
  39898. {
  39899. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39900. mp_int *x = NULL, *y = NULL;
  39901. #else
  39902. mp_int x[1], y[1];
  39903. #endif
  39904. wc_test_ret_t ret;
  39905. int i;
  39906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39907. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  39908. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  39909. ret = MEMORY_E;
  39910. goto out;
  39911. }
  39912. #endif
  39913. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  39914. if (ret != 0) {
  39915. ret = WC_TEST_RET_ENC_EC(ret);
  39916. goto out;
  39917. }
  39918. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  39919. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  39920. if (ret != 0) {
  39921. ret = WC_TEST_RET_ENC_EC(ret);
  39922. break;
  39923. }
  39924. /* p1 = 2^exp */
  39925. ret = mp_2expt(y, ecPairs[i].exp);
  39926. if (ret != 0) {
  39927. ret = WC_TEST_RET_ENC_EC(ret);
  39928. break;
  39929. }
  39930. /* p1 = p1 * m */
  39931. ret = mp_mul(x, y, x);
  39932. if (ret != 0) {
  39933. ret = WC_TEST_RET_ENC_EC(ret);
  39934. break;
  39935. }
  39936. /* p1 += */
  39937. ret = mp_add(p1, x, p1);
  39938. if (ret != 0) {
  39939. ret = WC_TEST_RET_ENC_EC(ret);
  39940. break;
  39941. }
  39942. mp_zero(x);
  39943. mp_zero(y);
  39944. }
  39945. if (ret == 0)
  39946. ret = GenerateNextP(p1, p2, k[0]);
  39947. if (ret == 0)
  39948. ret = GenerateNextP(p1, p3, k[1]);
  39949. out:
  39950. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39951. if (x != NULL) {
  39952. mp_clear(x);
  39953. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39954. }
  39955. if (y != NULL) {
  39956. mp_clear(y);
  39957. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39958. }
  39959. #else
  39960. mp_clear(x);
  39961. mp_clear(y);
  39962. #endif
  39963. return ret;
  39964. }
  39965. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  39966. {
  39967. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39968. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39969. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39970. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39971. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39972. #else
  39973. mp_int n[1],
  39974. p1[1],
  39975. p2[1],
  39976. p3[1];
  39977. #endif
  39978. wc_test_ret_t ret;
  39979. int isPrime = 0;
  39980. WC_RNG rng;
  39981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39982. if ((n == NULL) ||
  39983. (p1 == NULL) ||
  39984. (p2 == NULL) ||
  39985. (p3 == NULL))
  39986. ERROR_OUT(MEMORY_E, out);
  39987. #endif
  39988. ret = wc_InitRng(&rng);
  39989. if (ret != 0)
  39990. ret = WC_TEST_RET_ENC_EC(ret);
  39991. if (ret == 0) {
  39992. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  39993. if (ret != 0)
  39994. ret = WC_TEST_RET_ENC_EC(ret);
  39995. }
  39996. if (ret == 0)
  39997. ret = GenerateP(p1, p2, p3,
  39998. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  39999. if (ret == 0) {
  40000. ret = mp_mul(p1, p2, n);
  40001. if (ret != 0)
  40002. ret = WC_TEST_RET_ENC_EC(ret);
  40003. }
  40004. if (ret == 0) {
  40005. ret = mp_mul(n, p3, n);
  40006. if (ret != 0)
  40007. ret = WC_TEST_RET_ENC_EC(ret);
  40008. }
  40009. if (ret != 0)
  40010. ERROR_OUT(ret, out);
  40011. /* Check the old prime test using the number that false positives.
  40012. * This test result should indicate as not prime. */
  40013. ret = mp_prime_is_prime(n, 40, &isPrime);
  40014. if (ret != 0)
  40015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40016. if (isPrime)
  40017. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40018. /* This test result should fail. It should indicate the value as prime. */
  40019. ret = mp_prime_is_prime(n, 8, &isPrime);
  40020. if (ret != 0)
  40021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40022. if (!isPrime)
  40023. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40024. /* This test result should indicate the value as not prime. */
  40025. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40026. if (ret != 0)
  40027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40028. if (isPrime)
  40029. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40030. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  40031. if (ret != 0)
  40032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40033. /* This test result should indicate the value as prime. */
  40034. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40035. if (ret != 0)
  40036. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40037. if (!isPrime)
  40038. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40039. /* This test result should indicate the value as prime. */
  40040. isPrime = -1;
  40041. ret = mp_prime_is_prime(n, 8, &isPrime);
  40042. if (ret != 0)
  40043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40044. if (!isPrime)
  40045. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40046. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  40047. if (ret != 0)
  40048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40049. /* This test result should indicate the value as not prime. */
  40050. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40051. if (ret != 0)
  40052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40053. if (isPrime)
  40054. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40055. ret = mp_prime_is_prime(n, 8, &isPrime);
  40056. if (ret != 0)
  40057. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40058. if (isPrime)
  40059. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40060. ret = 0;
  40061. out:
  40062. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40063. if (n != NULL) {
  40064. mp_clear(n);
  40065. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40066. }
  40067. if (p1 != NULL) {
  40068. mp_clear(p1);
  40069. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40070. }
  40071. if (p2 != NULL) {
  40072. mp_clear(p2);
  40073. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40074. }
  40075. if (p3 != NULL) {
  40076. mp_clear(p3);
  40077. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40078. }
  40079. #else
  40080. mp_clear(p3);
  40081. mp_clear(p2);
  40082. mp_clear(p1);
  40083. mp_clear(n);
  40084. #endif
  40085. wc_FreeRng(&rng);
  40086. return ret;
  40087. }
  40088. #endif /* WOLFSSL_PUBLIC_MP */
  40089. #if defined(ASN_BER_TO_DER) && \
  40090. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  40091. defined(OPENSSL_EXTRA_X509_SMALL))
  40092. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  40093. typedef struct berDerTestData {
  40094. const byte *in;
  40095. word32 inSz;
  40096. const byte *out;
  40097. word32 outSz;
  40098. } berDerTestData;
  40099. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  40100. {
  40101. wc_test_ret_t ret;
  40102. int i;
  40103. word32 len = 0, l;
  40104. byte out[32];
  40105. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  40106. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  40107. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  40108. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  40109. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  40110. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  40111. };
  40112. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  40113. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  40114. 0x30, 0x80,
  40115. 0x02, 0x01, 0x01,
  40116. 0x30, 0x80,
  40117. 0x24, 0x80,
  40118. 0x04, 0x01, 0x01,
  40119. 0x04, 0x02, 0x02, 0x03,
  40120. 0x00, 0x00,
  40121. 0x06, 0x01, 0x01,
  40122. 0x00, 0x00,
  40123. 0x31, 0x80,
  40124. 0x06, 0x01, 0x01,
  40125. 0x00, 0x00,
  40126. 0x00, 0x00,
  40127. };
  40128. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  40129. 0x30, 0x12,
  40130. 0x02, 0x01, 0x01,
  40131. 0x30, 0x08,
  40132. 0x04, 0x03, 0x01, 0x02, 0x03,
  40133. 0x06, 0x01, 0x01,
  40134. 0x31, 0x03,
  40135. 0x06, 0x01, 0x01
  40136. };
  40137. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  40138. berDerTestData testData[] = {
  40139. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  40140. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  40141. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  40142. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  40143. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  40144. };
  40145. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  40146. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  40147. if (ret != LENGTH_ONLY_E)
  40148. return WC_TEST_RET_ENC_I(i);
  40149. if (len != testData[i].outSz)
  40150. return WC_TEST_RET_ENC_I(i);
  40151. len = testData[i].outSz;
  40152. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  40153. if (ret != 0)
  40154. return WC_TEST_RET_ENC_I(i);
  40155. if (XMEMCMP(out, testData[i].out, len) != 0)
  40156. return WC_TEST_RET_ENC_I(i);
  40157. for (l = 1; l < testData[i].inSz; l++) {
  40158. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  40159. if (ret != ASN_PARSE_E)
  40160. return WC_TEST_RET_ENC_EC(ret);
  40161. len = testData[i].outSz;
  40162. ret = wc_BerToDer(testData[i].in, l, out, &len);
  40163. if (ret != ASN_PARSE_E)
  40164. return WC_TEST_RET_ENC_EC(ret);
  40165. }
  40166. for (l = 0; l < testData[i].outSz-1; l++) {
  40167. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  40168. if (ret != BUFFER_E)
  40169. return WC_TEST_RET_ENC_EC(ret);
  40170. }
  40171. }
  40172. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  40173. if (ret != BAD_FUNC_ARG)
  40174. return WC_TEST_RET_ENC_EC(ret);
  40175. ret = wc_BerToDer(out, 4, NULL, NULL);
  40176. if (ret != BAD_FUNC_ARG)
  40177. return WC_TEST_RET_ENC_EC(ret);
  40178. ret = wc_BerToDer(NULL, 4, NULL, &len);
  40179. if (ret != BAD_FUNC_ARG)
  40180. return WC_TEST_RET_ENC_EC(ret);
  40181. ret = wc_BerToDer(NULL, 4, out, NULL);
  40182. if (ret != BAD_FUNC_ARG)
  40183. return WC_TEST_RET_ENC_EC(ret);
  40184. ret = wc_BerToDer(out, 4, out, NULL);
  40185. if (ret != BAD_FUNC_ARG)
  40186. return WC_TEST_RET_ENC_EC(ret);
  40187. ret = wc_BerToDer(NULL, 4, out, &len);
  40188. if (ret != BAD_FUNC_ARG)
  40189. return WC_TEST_RET_ENC_EC(ret);
  40190. for (l = 1; l < sizeof(good4_out); l++) {
  40191. len = l;
  40192. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  40193. if (ret != BUFFER_E)
  40194. return WC_TEST_RET_ENC_EC(ret);
  40195. }
  40196. return 0;
  40197. }
  40198. #endif
  40199. #ifdef DEBUG_WOLFSSL
  40200. static THREAD_LS_T int log_cnt = 0;
  40201. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  40202. {
  40203. (void)logLevel;
  40204. (void)logMessage;
  40205. log_cnt++;
  40206. }
  40207. #endif /* DEBUG_WOLFSSL */
  40208. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  40209. {
  40210. wc_test_ret_t ret;
  40211. #ifdef DEBUG_WOLFSSL
  40212. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  40213. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  40214. byte b[256];
  40215. int i;
  40216. for (i = 0; i < (int)sizeof(b); i++)
  40217. b[i] = i;
  40218. ret = wolfSSL_Debugging_ON();
  40219. if (ret != 0)
  40220. return WC_TEST_RET_ENC_EC(ret);
  40221. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  40222. if (ret != 0)
  40223. return WC_TEST_RET_ENC_EC(ret);
  40224. WOLFSSL_MSG(msg);
  40225. WOLFSSL_BUFFER(a, sizeof(a));
  40226. WOLFSSL_BUFFER(b, sizeof(b));
  40227. WOLFSSL_BUFFER(NULL, 0);
  40228. WOLFSSL_ERROR(MEMORY_E);
  40229. WOLFSSL_ERROR_MSG(msg);
  40230. /* turn off logs */
  40231. wolfSSL_Debugging_OFF();
  40232. /* capture log count */
  40233. i = log_cnt;
  40234. /* validate no logs are output when disabled */
  40235. WOLFSSL_MSG(msg);
  40236. WOLFSSL_BUFFER(a, sizeof(a));
  40237. WOLFSSL_BUFFER(b, sizeof(b));
  40238. WOLFSSL_BUFFER(NULL, 0);
  40239. WOLFSSL_ERROR(MEMORY_E);
  40240. WOLFSSL_ERROR_MSG(msg);
  40241. /* check the logs were disabled */
  40242. if (i != log_cnt)
  40243. return WC_TEST_RET_ENC_NC;
  40244. /* restore callback and leave logging enabled */
  40245. wolfSSL_SetLoggingCb(NULL);
  40246. wolfSSL_Debugging_ON();
  40247. /* suppress unused args */
  40248. (void)a;
  40249. (void)b;
  40250. #else
  40251. ret = wolfSSL_Debugging_ON();
  40252. if (ret != NOT_COMPILED_IN)
  40253. return WC_TEST_RET_ENC_EC(ret);
  40254. wolfSSL_Debugging_OFF();
  40255. ret = wolfSSL_SetLoggingCb(NULL);
  40256. if (ret != NOT_COMPILED_IN)
  40257. return WC_TEST_RET_ENC_EC(ret);
  40258. #endif /* DEBUG_WOLFSSL */
  40259. return 0;
  40260. }
  40261. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  40262. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  40263. #else
  40264. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  40265. #endif
  40266. {
  40267. #ifdef WOLFSSL_PTHREADS
  40268. wolfSSL_Mutex m;
  40269. #endif
  40270. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  40271. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  40272. wc_test_ret_t ret;
  40273. #endif
  40274. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  40275. #ifndef WOLFSSL_STATIC_MEMORY
  40276. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  40277. #else
  40278. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  40279. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40280. if (mm != NULL) {
  40281. ret = wc_InitMutex(mm);
  40282. if (ret != 0) {
  40283. WOLFSSL_MSG("Init Mutex failed");
  40284. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40285. return WC_TEST_RET_ENC_EC(ret);
  40286. }
  40287. }
  40288. #endif
  40289. if (mm == NULL)
  40290. return WC_TEST_RET_ENC_ERRNO;
  40291. wc_FreeMutex(mm);
  40292. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40293. #endif
  40294. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  40295. #ifdef WOLFSSL_PTHREADS
  40296. ret = wc_InitMutex(&m);
  40297. if (ret != 0)
  40298. return WC_TEST_RET_ENC_EC(ret);
  40299. ret = wc_LockMutex(&m);
  40300. if (ret != 0)
  40301. return WC_TEST_RET_ENC_EC(ret);
  40302. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  40303. /* trying to free a locked mutex is not portable behavior with pthread */
  40304. /* Attempting to destroy a locked mutex results in undefined behavior */
  40305. ret = wc_FreeMutex(&m);
  40306. if (ret != BAD_MUTEX_E)
  40307. return WC_TEST_RET_ENC_EC(ret);
  40308. #endif
  40309. ret = wc_UnLockMutex(&m);
  40310. if (ret != 0)
  40311. return WC_TEST_RET_ENC_EC(ret);
  40312. ret = wc_FreeMutex(&m);
  40313. if (ret != 0)
  40314. return WC_TEST_RET_ENC_EC(ret);
  40315. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  40316. /* Trying to use a pthread after free'ing is not portable behavior */
  40317. ret = wc_LockMutex(&m);
  40318. if (ret != BAD_MUTEX_E)
  40319. return WC_TEST_RET_ENC_EC(ret);
  40320. ret = wc_UnLockMutex(&m);
  40321. if (ret != BAD_MUTEX_E)
  40322. return WC_TEST_RET_ENC_EC(ret);
  40323. #endif
  40324. #endif
  40325. return 0;
  40326. }
  40327. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  40328. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40329. !defined(WOLFSSL_STATIC_MEMORY)
  40330. static wc_test_ret_t malloc_cnt = 0;
  40331. static wc_test_ret_t realloc_cnt = 0;
  40332. static wc_test_ret_t free_cnt = 0;
  40333. #ifdef WOLFSSL_DEBUG_MEMORY
  40334. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  40335. {
  40336. (void) func;
  40337. (void) line;
  40338. #else
  40339. static void *my_Malloc_cb(size_t size)
  40340. {
  40341. #endif
  40342. malloc_cnt++;
  40343. #ifndef WOLFSSL_NO_MALLOC
  40344. return malloc(size);
  40345. #else
  40346. WOLFSSL_MSG("No malloc available");
  40347. (void)size;
  40348. return NULL;
  40349. #endif
  40350. }
  40351. #ifdef WOLFSSL_DEBUG_MEMORY
  40352. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  40353. {
  40354. (void) func;
  40355. (void) line;
  40356. #else
  40357. static void my_Free_cb(void *ptr)
  40358. {
  40359. #endif
  40360. free_cnt++;
  40361. #ifndef WOLFSSL_NO_MALLOC
  40362. free(ptr);
  40363. #else
  40364. WOLFSSL_MSG("No free available");
  40365. (void)ptr;
  40366. #endif
  40367. }
  40368. #ifdef WOLFSSL_DEBUG_MEMORY
  40369. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  40370. {
  40371. (void) func;
  40372. (void) line;
  40373. #else
  40374. static void *my_Realloc_cb(void *ptr, size_t size)
  40375. {
  40376. #endif
  40377. realloc_cnt++;
  40378. #ifndef WOLFSSL_NO_MALLOC
  40379. return realloc(ptr, size);
  40380. #else
  40381. WOLFSSL_MSG("No realloc available");
  40382. (void)ptr;
  40383. (void)size;
  40384. return NULL;
  40385. #endif
  40386. }
  40387. #endif /* !WOLFSSL_NO_MALLOC */
  40388. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  40389. {
  40390. wc_test_ret_t ret = 0;
  40391. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40392. !defined(WOLFSSL_STATIC_MEMORY)
  40393. byte* b = NULL;
  40394. #endif
  40395. wolfSSL_Malloc_cb mc;
  40396. wolfSSL_Free_cb fc;
  40397. wolfSSL_Realloc_cb rc;
  40398. /* Save existing memory callbacks */
  40399. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  40400. if (ret != 0)
  40401. return WC_TEST_RET_ENC_EC(ret);
  40402. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40403. !defined(WOLFSSL_STATIC_MEMORY)
  40404. /* test realloc */
  40405. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40406. if (b == NULL) {
  40407. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  40408. }
  40409. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40410. b = NULL;
  40411. /* Use API. */
  40412. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  40413. (wolfSSL_Free_cb)my_Free_cb,
  40414. (wolfSSL_Realloc_cb)my_Realloc_cb);
  40415. if (ret != 0) {
  40416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  40417. }
  40418. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40419. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40420. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40421. #ifndef WOLFSSL_STATIC_MEMORY
  40422. #ifndef WOLFSSL_CHECK_MEM_ZERO
  40423. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  40424. #else
  40425. /* Checking zeroized memory means realloc is a malloc and free. */
  40426. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  40427. #endif
  40428. #else
  40429. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  40430. #endif
  40431. ret = WC_TEST_RET_ENC_NC;
  40432. #endif /* !WOLFSSL_NO_MALLOC */
  40433. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40434. !defined(WOLFSSL_STATIC_MEMORY)
  40435. exit_memcb:
  40436. /* reset malloc/free/realloc counts */
  40437. malloc_cnt = 0;
  40438. free_cnt = 0;
  40439. realloc_cnt = 0;
  40440. #endif
  40441. /* restore memory callbacks */
  40442. wolfSSL_SetAllocators(mc, fc, rc);
  40443. return ret;
  40444. }
  40445. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  40446. #if defined(WOLFSSL_CAAM_BLOB)
  40447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  40448. {
  40449. wc_test_ret_t ret = 0;
  40450. byte out[112];
  40451. byte blob[112];
  40452. word32 outSz;
  40453. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  40454. {
  40455. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  40456. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  40457. };
  40458. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  40459. {
  40460. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40461. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  40462. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  40463. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  40464. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  40465. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  40466. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  40467. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  40468. };
  40469. XMEMSET(blob, 0, sizeof(blob));
  40470. XMEMSET(out, 0, sizeof(out));
  40471. outSz = sizeof(blob);
  40472. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  40473. if (ret != 0)
  40474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40475. blob[outSz - 2] += 1;
  40476. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40477. if (ret == 0) { /* should fail with altered blob */
  40478. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40479. }
  40480. XMEMSET(blob, 0, sizeof(blob));
  40481. outSz = sizeof(blob);
  40482. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  40483. if (ret != 0)
  40484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40485. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40486. if (ret != 0)
  40487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40488. if (XMEMCMP(out, iv, sizeof(iv))) {
  40489. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40490. }
  40491. XMEMSET(blob, 0, sizeof(blob));
  40492. outSz = sizeof(blob);
  40493. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  40494. if (ret != 0)
  40495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40496. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40497. if (ret != 0)
  40498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40499. if (XMEMCMP(out, text, sizeof(text))) {
  40500. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40501. }
  40502. exit_blob:
  40503. return ret;
  40504. }
  40505. #endif /* WOLFSSL_CAAM_BLOB */
  40506. #ifdef WOLF_CRYPTO_CB
  40507. /* Example custom context for crypto callback */
  40508. typedef struct {
  40509. int exampleVar; /* flag for testing if only crypt is enabled. */
  40510. } myCryptoDevCtx;
  40511. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  40512. /* Testing rsa cb when CB_ONLY_RSA is enabled
  40513. * When CB_ONLY_RSA is enabled, software imple. is not available.
  40514. *
  40515. * ctx callback ctx
  40516. * returen 0 on success, otherwise return negative
  40517. */
  40518. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  40519. {
  40520. wc_test_ret_t ret = 0;
  40521. #if !defined(NO_RSA)
  40522. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40523. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  40524. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40525. byte* tmp = NULL;
  40526. #else
  40527. RsaKey key[1];
  40528. byte tmp[FOURK_BUF];
  40529. #endif
  40530. size_t bytes;
  40531. const word32 inLen = (word32)TEST_STRING_SZ;
  40532. word32 idx = 0;
  40533. word32 sigSz;
  40534. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  40535. byte out[RSA_TEST_BYTES];
  40536. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  40537. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  40538. !defined(NO_FILESYSTEM)
  40539. XFILE file;
  40540. #endif
  40541. #ifdef WOLFSSL_KEY_GEN
  40542. WC_RNG rng;
  40543. #endif
  40544. #ifdef USE_CERT_BUFFERS_1024
  40545. bytes = (size_t)sizeof_client_key_der_1024;
  40546. if (bytes < (size_t)sizeof_client_cert_der_1024)
  40547. bytes = (size_t)sizeof_client_cert_der_1024;
  40548. #elif defined(USE_CERT_BUFFERS_2048)
  40549. bytes = (size_t)sizeof_client_key_der_2048;
  40550. if (bytes < (size_t)sizeof_client_cert_der_2048)
  40551. bytes = (size_t)sizeof_client_cert_der_2048;
  40552. #elif defined(USE_CERT_BUFFERS_3072)
  40553. bytes = (size_t)sizeof_client_key_der_3072;
  40554. if (bytes < (size_t)sizeof_client_cert_der_3072)
  40555. bytes = (size_t)sizeof_client_cert_der_3072;
  40556. #elif defined(USE_CERT_BUFFERS_4096)
  40557. bytes = (size_t)sizeof_client_key_der_4096;
  40558. if (bytes < (size_t)sizeof_client_cert_der_4096)
  40559. bytes = (size_t)sizeof_client_cert_der_4096;
  40560. #else
  40561. bytes = FOURK_BUF;
  40562. #endif
  40563. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40564. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40565. if (tmp == NULL)
  40566. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40567. #endif
  40568. #ifdef USE_CERT_BUFFERS_1024
  40569. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  40570. #elif defined(USE_CERT_BUFFERS_2048)
  40571. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  40572. #elif defined(USE_CERT_BUFFERS_3072)
  40573. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  40574. #elif defined(USE_CERT_BUFFERS_4096)
  40575. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  40576. #elif !defined(NO_FILESYSTEM)
  40577. file = XFOPEN(clientKey, "rb");
  40578. if (!file) {
  40579. ret = WC_TEST_RET_ENC_ERRNO;
  40580. err_sys("can't open ./certs/client-key.der, "
  40581. "Please run from wolfSSL home dir", ret);
  40582. ERROR_OUT(ret, exit_onlycb);
  40583. }
  40584. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  40585. XFCLOSE(file);
  40586. if (bytes == 0)
  40587. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40588. #endif
  40589. #ifdef WOLFSSL_KEY_GEN
  40590. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  40591. * wc_MakeRsaKey() -> rsa cb ->
  40592. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  40593. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  40594. */
  40595. ctx->exampleVar = 99;
  40596. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  40597. if (ret != 0)
  40598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40599. /* wc_MakeRsaKey() -> rsa cb ->
  40600. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  40601. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  40602. */
  40603. ctx->exampleVar = 1;
  40604. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  40605. if (ret != NO_VALID_DEVID) {
  40606. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40607. } else
  40608. /* reset return code */
  40609. ret = 0;
  40610. #endif
  40611. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  40612. if (ret != 0)
  40613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40614. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  40615. if (ret != 0)
  40616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40617. sigSz = (word32)wc_RsaEncryptSize(key);
  40618. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  40619. if (ret == 0) {
  40620. /* wc_SignatureGenerate() -> rsa cb ->
  40621. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  40622. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  40623. */
  40624. ctx->exampleVar = 99;
  40625. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  40626. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  40627. if (ret != 0)
  40628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40629. }
  40630. if (ret == 0) {
  40631. /* wc_SignatureGenerate() -> rsa cb ->
  40632. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  40633. * wc_SignatureGenerate(INVALID_DEVID) expects to
  40634. * return NO_VALID_DEVID(failure)
  40635. */
  40636. ctx->exampleVar = 1;
  40637. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  40638. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  40639. if (ret != NO_VALID_DEVID) {
  40640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40641. } else
  40642. /* reset return code */
  40643. ret = 0;
  40644. }
  40645. exit_onlycb:
  40646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40647. if (key != NULL) {
  40648. wc_FreeRsaKey(key);
  40649. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40650. }
  40651. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40652. #else
  40653. wc_FreeRsaKey(key);
  40654. #endif
  40655. #endif
  40656. return ret;
  40657. }
  40658. #endif
  40659. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  40660. /* Testing rsa cb when CB_ONLY_ECC is enabled
  40661. * When CB_ONLY_ECC is enabled, software imple. is not available.
  40662. *
  40663. * ctx callback ctx
  40664. * returen 0 on success, otherwise return negative
  40665. */
  40666. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  40667. {
  40668. wc_test_ret_t ret = 0;
  40669. #if defined(HAVE_ECC)
  40670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40671. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  40672. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40673. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  40674. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40675. byte* out = (byte*)XMALLOC(sizeof(byte),
  40676. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40677. #ifdef OPENSSL_EXTRA
  40678. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  40679. DYNAMIC_TYPE_TMP_BUFFER);
  40680. #endif
  40681. #else
  40682. ecc_key key[1];
  40683. ecc_key pub[1];
  40684. byte out[256];
  40685. #ifdef OPENSSL_EXTRA
  40686. unsigned char check[256];
  40687. #endif
  40688. #endif
  40689. #ifdef OPENSSL_EXTRA
  40690. EVP_PKEY* privKey = NULL;
  40691. EVP_PKEY* pubKey = NULL;
  40692. #ifdef USE_CERT_BUFFERS_256
  40693. ecc_key* pkey;
  40694. const unsigned char* cp;
  40695. #endif
  40696. EVP_MD_CTX mdCtx;
  40697. const char testData[] = "Hi There";
  40698. size_t checkSz = -1;
  40699. const unsigned char* p;
  40700. const unsigned char check_v[256] = {
  40701. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  40702. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  40703. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  40704. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  40705. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  40706. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  40707. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  40708. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  40709. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  40710. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  40711. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  40712. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  40713. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  40714. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  40715. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  40716. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  40717. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  40718. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  40719. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  40720. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  40721. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  40722. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  40723. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  40724. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  40725. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  40726. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  40727. };
  40728. #endif
  40729. WC_RNG rng;
  40730. EncryptedInfo encInfo;
  40731. int keyFormat = 0;
  40732. word32 keyIdx = 0;
  40733. byte in[] = "Everyone gets Friday off. ecc p";
  40734. word32 inLen = (word32)XSTRLEN((char*)in);
  40735. word32 outLen;
  40736. int verify;
  40737. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40738. if (key == NULL || pub == NULL) {
  40739. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40740. }
  40741. #endif
  40742. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  40743. if (ret != 0)
  40744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40745. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  40746. ctx->exampleVar = 99;
  40747. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40748. if (ret != 0)
  40749. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40750. ctx->exampleVar = 1;
  40751. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40752. if (ret != NO_VALID_DEVID) {
  40753. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40754. } else
  40755. /* reset return code */
  40756. ret = 0;
  40757. #ifdef USE_CERT_BUFFERS_256
  40758. if (ret == 0) {
  40759. /* load ECC private key and perform private transform */
  40760. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  40761. key, sizeof_ecc_key_der_256);
  40762. }
  40763. if (ret != 0)
  40764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40765. /* wc_CryptoCb_EccSign cb test, no actual testing */
  40766. ctx->exampleVar = 99;
  40767. if (ret == 0) {
  40768. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40769. }
  40770. if (ret != 0)
  40771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40772. ctx->exampleVar = 1;
  40773. if (ret == 0) {
  40774. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40775. }
  40776. if (ret != NO_VALID_DEVID) {
  40777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40778. }
  40779. else
  40780. ret = 0;
  40781. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  40782. ctx->exampleVar = 99;
  40783. if (ret == 0) {
  40784. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40785. }
  40786. if (ret != 0)
  40787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40788. ctx->exampleVar = 1;
  40789. if (ret == 0) {
  40790. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40791. }
  40792. if (ret != NO_VALID_DEVID) {
  40793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40794. }
  40795. else
  40796. ret = 0;
  40797. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  40798. /* make public key for shared secret */
  40799. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  40800. ctx->exampleVar = 99;
  40801. if (ret == 0) {
  40802. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40803. }
  40804. if (ret != 0)
  40805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40806. ctx->exampleVar = 1;
  40807. if (ret == 0) {
  40808. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40809. }
  40810. if (ret != NO_VALID_DEVID) {
  40811. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40812. }
  40813. else
  40814. ret = 0;
  40815. #ifdef OPENSSL_EXTRA
  40816. (void)pkey;
  40817. cp = ecc_clikey_der_256;
  40818. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  40819. sizeof_ecc_clikey_der_256);
  40820. if (privKey == NULL) {
  40821. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40822. }
  40823. pkey = (ecc_key*)privKey->ecc->internal;
  40824. pkey->devId = devId;
  40825. p = ecc_clikeypub_der_256;
  40826. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  40827. if (pubKey == NULL) {
  40828. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40829. }
  40830. pkey = (ecc_key*)pubKey->ecc->internal;
  40831. pkey->devId = devId;
  40832. /* sign */
  40833. EVP_MD_CTX_init(&mdCtx);
  40834. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  40835. if (ret != WOLFSSL_SUCCESS) {
  40836. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40837. }
  40838. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  40839. (unsigned int)XSTRLEN(testData));
  40840. if (ret != WOLFSSL_SUCCESS) {
  40841. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40842. }
  40843. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  40844. if (ret != WOLFSSL_SUCCESS) {
  40845. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40846. }
  40847. ctx->exampleVar = 99;
  40848. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40849. /* just called crypt callback as dummy
  40850. * EVP_DigestSignFinal returns 0 internally.
  40851. */
  40852. if (ret != 0)
  40853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40854. ctx->exampleVar = 1;
  40855. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40856. /* just called crypt callback as dummy
  40857. * EVP_DigestSignFinal returns 0 internally.
  40858. */
  40859. if (ret != 0)
  40860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40861. /* restore checkSz for verify */
  40862. checkSz = 71;
  40863. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40864. if (ret != SSL_SUCCESS) {
  40865. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40866. }
  40867. /* verify */
  40868. EVP_MD_CTX_init(&mdCtx);
  40869. if (ret == SSL_SUCCESS) {
  40870. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  40871. }
  40872. if (ret != WOLFSSL_SUCCESS) {
  40873. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40874. }
  40875. if (ret == WOLFSSL_SUCCESS) {
  40876. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  40877. (unsigned int)XSTRLEN(testData));
  40878. }
  40879. if (ret != WOLFSSL_SUCCESS) {
  40880. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40881. }
  40882. ctx->exampleVar = 99;
  40883. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40884. /* just called crypt callback as dummy
  40885. * EVP_DigestSignFinal returns 0 internally.
  40886. */
  40887. if (ret != 0)
  40888. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40889. ctx->exampleVar = 1;
  40890. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40891. /* just called crypt callback as dummy
  40892. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  40893. */
  40894. if (ret != -1) {
  40895. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40896. }
  40897. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40898. if (ret != SSL_SUCCESS) {
  40899. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40900. } else
  40901. ret = 0;
  40902. #endif
  40903. #else
  40904. (void)verify;
  40905. (void)outLen;
  40906. (void)inLen;
  40907. (void)out;
  40908. (void)pub;
  40909. #ifdef OPENSSL_EXTRA
  40910. (void)privKey;
  40911. (void)pubKey;
  40912. (void)mdCtx;
  40913. (void)check;
  40914. (void)checkSz;
  40915. (void)p;
  40916. #endif
  40917. #endif
  40918. (void)keyFormat;
  40919. (void)encInfo;
  40920. exit_onlycb:
  40921. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40922. if (key != NULL) {
  40923. wc_ecc_free(key);
  40924. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40925. }
  40926. if (pub != NULL) {
  40927. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40928. }
  40929. if (out != NULL) {
  40930. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40931. }
  40932. #ifdef OPENSSL_EXTRA
  40933. if (check) {
  40934. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40935. }
  40936. #endif
  40937. #else
  40938. wc_ecc_free(key);
  40939. #ifdef OPENSSL_EXTRA
  40940. if (privKey)
  40941. EVP_PKEY_free(privKey);
  40942. if (pubKey)
  40943. EVP_PKEY_free(pubKey);
  40944. #endif
  40945. #endif
  40946. #endif /* HAVE_ECC */
  40947. return ret;
  40948. }
  40949. #endif
  40950. /* Example crypto dev callback function that calls software version */
  40951. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  40952. {
  40953. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  40954. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  40955. if (info == NULL)
  40956. return BAD_FUNC_ARG;
  40957. #ifdef DEBUG_WOLFSSL
  40958. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  40959. #endif
  40960. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  40961. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  40962. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  40963. /* if RNG only supports crypto callback, just use seed */
  40964. ret = wc_GenerateSeed(&info->rng.rng->seed,
  40965. info->rng.out, info->rng.sz);
  40966. #elif !defined(WC_NO_RNG)
  40967. /* set devId to invalid, so software is used */
  40968. info->rng.rng->devId = INVALID_DEVID;
  40969. ret = wc_RNG_GenerateBlock(info->rng.rng,
  40970. info->rng.out, info->rng.sz);
  40971. /* reset devId */
  40972. info->rng.rng->devId = devIdArg;
  40973. #endif
  40974. }
  40975. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  40976. #ifndef WC_NO_RNG
  40977. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  40978. word32* seedWord32 = (word32*)seed;
  40979. word32 len;
  40980. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  40981. while (info->seed.sz > 0) {
  40982. len = (word32)sizeof(seed);
  40983. if (info->seed.sz < len)
  40984. len = info->seed.sz;
  40985. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  40986. info->seed.seed += len;
  40987. info->seed.sz -= len;
  40988. (*seedWord32)++;
  40989. }
  40990. ret = 0;
  40991. #endif
  40992. }
  40993. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  40994. #ifdef DEBUG_WOLFSSL
  40995. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  40996. #endif
  40997. #ifndef NO_RSA
  40998. if (info->pk.type == WC_PK_TYPE_RSA) {
  40999. /* set devId to invalid, so software is used */
  41000. info->pk.rsa.key->devId = INVALID_DEVID;
  41001. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41002. #ifdef DEBUG_WOLFSSL
  41003. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41004. #endif
  41005. if (myCtx->exampleVar == 99) {
  41006. info->pk.rsa.key->devId = devIdArg;
  41007. return 0;
  41008. }
  41009. #endif
  41010. switch (info->pk.rsa.type) {
  41011. case RSA_PUBLIC_ENCRYPT:
  41012. case RSA_PUBLIC_DECRYPT:
  41013. /* perform software based RSA public op */
  41014. ret = wc_RsaFunction(
  41015. info->pk.rsa.in, info->pk.rsa.inLen,
  41016. info->pk.rsa.out, info->pk.rsa.outLen,
  41017. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  41018. break;
  41019. case RSA_PRIVATE_ENCRYPT:
  41020. case RSA_PRIVATE_DECRYPT:
  41021. /* perform software based RSA private op */
  41022. ret = wc_RsaFunction(
  41023. info->pk.rsa.in, info->pk.rsa.inLen,
  41024. info->pk.rsa.out, info->pk.rsa.outLen,
  41025. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  41026. break;
  41027. }
  41028. /* reset devId */
  41029. info->pk.rsa.key->devId = devIdArg;
  41030. }
  41031. #ifdef WOLFSSL_KEY_GEN
  41032. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  41033. info->pk.rsakg.key->devId = INVALID_DEVID;
  41034. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41035. #ifdef DEBUG_WOLFSSL
  41036. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41037. #endif
  41038. if (myCtx->exampleVar == 99) {
  41039. info->pk.rsakg.key->devId = devIdArg;
  41040. return 0;
  41041. }
  41042. #endif
  41043. #ifdef HAVE_FIPS
  41044. for (;;) {
  41045. #endif
  41046. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  41047. info->pk.rsakg.e, info->pk.rsakg.rng);
  41048. #ifdef HAVE_FIPS
  41049. if (ret == PRIME_GEN_E)
  41050. continue;
  41051. break;
  41052. }
  41053. #endif
  41054. /* reset devId */
  41055. info->pk.rsakg.key->devId = devIdArg;
  41056. }
  41057. #endif
  41058. #endif /* !NO_RSA */
  41059. #ifdef HAVE_ECC
  41060. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  41061. /* set devId to invalid, so software is used */
  41062. info->pk.eckg.key->devId = INVALID_DEVID;
  41063. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41064. #ifdef DEBUG_WOLFSSL
  41065. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41066. #endif
  41067. if (myCtx->exampleVar == 99) {
  41068. info->pk.eckg.key->devId = devIdArg;
  41069. return 0;
  41070. }
  41071. #endif
  41072. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  41073. info->pk.eckg.key, info->pk.eckg.curveId);
  41074. /* reset devId */
  41075. info->pk.eckg.key->devId = devIdArg;
  41076. }
  41077. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  41078. /* set devId to invalid, so software is used */
  41079. info->pk.eccsign.key->devId = INVALID_DEVID;
  41080. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41081. #ifdef DEBUG_WOLFSSL
  41082. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41083. #endif
  41084. if (myCtx->exampleVar == 99) {
  41085. info->pk.eccsign.key->devId = devIdArg;
  41086. return 0;
  41087. }
  41088. #endif
  41089. ret = wc_ecc_sign_hash(
  41090. info->pk.eccsign.in, info->pk.eccsign.inlen,
  41091. info->pk.eccsign.out, info->pk.eccsign.outlen,
  41092. info->pk.eccsign.rng, info->pk.eccsign.key);
  41093. /* reset devId */
  41094. info->pk.eccsign.key->devId = devIdArg;
  41095. }
  41096. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  41097. /* set devId to invalid, so software is used */
  41098. info->pk.eccverify.key->devId = INVALID_DEVID;
  41099. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41100. #ifdef DEBUG_WOLFSSL
  41101. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41102. #endif
  41103. if (myCtx->exampleVar == 99) {
  41104. info->pk.eccverify.key->devId = devIdArg;
  41105. return 0;
  41106. }
  41107. #endif
  41108. ret = wc_ecc_verify_hash(
  41109. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  41110. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  41111. info->pk.eccverify.res, info->pk.eccverify.key);
  41112. /* reset devId */
  41113. info->pk.eccverify.key->devId = devIdArg;
  41114. }
  41115. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  41116. /* set devId to invalid, so software is used */
  41117. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  41118. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41119. #ifdef DEBUG_WOLFSSL
  41120. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41121. #endif
  41122. if (myCtx->exampleVar == 99) {
  41123. info->pk.ecdh.private_key->devId = devIdArg;
  41124. return 0;
  41125. }
  41126. #endif
  41127. ret = wc_ecc_shared_secret(
  41128. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  41129. info->pk.ecdh.out, info->pk.ecdh.outlen);
  41130. /* reset devId */
  41131. info->pk.ecdh.private_key->devId = devIdArg;
  41132. }
  41133. #endif /* HAVE_ECC */
  41134. #ifdef HAVE_CURVE25519
  41135. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  41136. /* set devId to invalid, so software is used */
  41137. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  41138. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  41139. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  41140. /* reset devId */
  41141. info->pk.curve25519kg.key->devId = devIdArg;
  41142. }
  41143. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  41144. /* set devId to invalid, so software is used */
  41145. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  41146. ret = wc_curve25519_shared_secret_ex(
  41147. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  41148. info->pk.curve25519.out, info->pk.curve25519.outlen,
  41149. info->pk.curve25519.endian);
  41150. /* reset devId */
  41151. info->pk.curve25519.private_key->devId = devIdArg;
  41152. }
  41153. #endif /* HAVE_CURVE25519 */
  41154. #ifdef HAVE_ED25519
  41155. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  41156. /* set devId to invalid, so software is used */
  41157. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  41158. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  41159. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  41160. /* reset devId */
  41161. info->pk.ed25519kg.key->devId = devIdArg;
  41162. }
  41163. #ifdef HAVE_ED25519_SIGN
  41164. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  41165. /* set devId to invalid, so software is used */
  41166. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  41167. ret = wc_ed25519_sign_msg_ex(
  41168. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  41169. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  41170. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  41171. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  41172. /* reset devId */
  41173. info->pk.ed25519sign.key->devId = devIdArg;
  41174. }
  41175. #endif
  41176. #ifdef HAVE_ED25519_VERIFY
  41177. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  41178. /* set devId to invalid, so software is used */
  41179. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  41180. ret = wc_ed25519_verify_msg_ex(
  41181. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  41182. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  41183. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  41184. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  41185. info->pk.ed25519verify.contextLen);
  41186. /* reset devId */
  41187. info->pk.ed25519verify.key->devId = devIdArg;
  41188. }
  41189. #endif
  41190. #endif /* HAVE_ED25519 */
  41191. }
  41192. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  41193. #if !defined(NO_AES) || !defined(NO_DES3)
  41194. #ifdef HAVE_AESGCM
  41195. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  41196. if (info->cipher.enc) {
  41197. /* set devId to invalid, so software is used */
  41198. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  41199. ret = wc_AesGcmEncrypt(
  41200. info->cipher.aesgcm_enc.aes,
  41201. info->cipher.aesgcm_enc.out,
  41202. info->cipher.aesgcm_enc.in,
  41203. info->cipher.aesgcm_enc.sz,
  41204. info->cipher.aesgcm_enc.iv,
  41205. info->cipher.aesgcm_enc.ivSz,
  41206. info->cipher.aesgcm_enc.authTag,
  41207. info->cipher.aesgcm_enc.authTagSz,
  41208. info->cipher.aesgcm_enc.authIn,
  41209. info->cipher.aesgcm_enc.authInSz);
  41210. /* reset devId */
  41211. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  41212. }
  41213. else {
  41214. /* set devId to invalid, so software is used */
  41215. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  41216. ret = wc_AesGcmDecrypt(
  41217. info->cipher.aesgcm_dec.aes,
  41218. info->cipher.aesgcm_dec.out,
  41219. info->cipher.aesgcm_dec.in,
  41220. info->cipher.aesgcm_dec.sz,
  41221. info->cipher.aesgcm_dec.iv,
  41222. info->cipher.aesgcm_dec.ivSz,
  41223. info->cipher.aesgcm_dec.authTag,
  41224. info->cipher.aesgcm_dec.authTagSz,
  41225. info->cipher.aesgcm_dec.authIn,
  41226. info->cipher.aesgcm_dec.authInSz);
  41227. /* reset devId */
  41228. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  41229. }
  41230. }
  41231. #endif /* HAVE_AESGCM */
  41232. #ifdef HAVE_AES_CBC
  41233. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  41234. if (info->cipher.enc) {
  41235. /* set devId to invalid, so software is used */
  41236. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  41237. ret = wc_AesCbcEncrypt(
  41238. info->cipher.aescbc.aes,
  41239. info->cipher.aescbc.out,
  41240. info->cipher.aescbc.in,
  41241. info->cipher.aescbc.sz);
  41242. /* reset devId */
  41243. info->cipher.aescbc.aes->devId = devIdArg;
  41244. }
  41245. else {
  41246. /* set devId to invalid, so software is used */
  41247. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  41248. ret = wc_AesCbcDecrypt(
  41249. info->cipher.aescbc.aes,
  41250. info->cipher.aescbc.out,
  41251. info->cipher.aescbc.in,
  41252. info->cipher.aescbc.sz);
  41253. /* reset devId */
  41254. info->cipher.aescbc.aes->devId = devIdArg;
  41255. }
  41256. }
  41257. #endif /* HAVE_AES_CBC */
  41258. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  41259. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  41260. if (info->cipher.enc) {
  41261. /* set devId to invalid, so software is used */
  41262. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  41263. ret = wc_AesEcbEncrypt(
  41264. info->cipher.aesecb.aes,
  41265. info->cipher.aesecb.out,
  41266. info->cipher.aesecb.in,
  41267. info->cipher.aesecb.sz);
  41268. /* reset devId */
  41269. info->cipher.aesecb.aes->devId = devIdArg;
  41270. }
  41271. else {
  41272. /* set devId to invalid, so software is used */
  41273. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  41274. ret = wc_AesEcbDecrypt(
  41275. info->cipher.aesecb.aes,
  41276. info->cipher.aesecb.out,
  41277. info->cipher.aesecb.in,
  41278. info->cipher.aesecb.sz);
  41279. /* reset devId */
  41280. info->cipher.aesecb.aes->devId = devIdArg;
  41281. }
  41282. }
  41283. #endif /* HAVE_AES_ECB */
  41284. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  41285. !defined(HAVE_SELFTEST)
  41286. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  41287. /* set devId to invalid, so software is used */
  41288. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  41289. ret = wc_AesCtrEncrypt(
  41290. info->cipher.aesctr.aes,
  41291. info->cipher.aesctr.out,
  41292. info->cipher.aesctr.in,
  41293. info->cipher.aesctr.sz);
  41294. /* reset devId */
  41295. info->cipher.aesctr.aes->devId = devIdArg;
  41296. }
  41297. #endif /* WOLFSSL_AES_COUNTER */
  41298. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  41299. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  41300. if (info->cipher.enc) {
  41301. /* set devId to invalid, so software is used */
  41302. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  41303. ret = wc_AesCcmEncrypt(
  41304. info->cipher.aesccm_enc.aes,
  41305. info->cipher.aesccm_enc.out,
  41306. info->cipher.aesccm_enc.in,
  41307. info->cipher.aesccm_enc.sz,
  41308. info->cipher.aesccm_enc.nonce,
  41309. info->cipher.aesccm_enc.nonceSz,
  41310. info->cipher.aesccm_enc.authTag,
  41311. info->cipher.aesccm_enc.authTagSz,
  41312. info->cipher.aesccm_enc.authIn,
  41313. info->cipher.aesccm_enc.authInSz);
  41314. /* reset devId */
  41315. info->cipher.aesccm_enc.aes->devId = devIdArg;
  41316. }
  41317. else {
  41318. /* set devId to invalid, so software is used */
  41319. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  41320. ret = wc_AesCcmDecrypt(
  41321. info->cipher.aesccm_dec.aes,
  41322. info->cipher.aesccm_dec.out,
  41323. info->cipher.aesccm_dec.in,
  41324. info->cipher.aesccm_dec.sz,
  41325. info->cipher.aesccm_dec.nonce,
  41326. info->cipher.aesccm_dec.nonceSz,
  41327. info->cipher.aesccm_dec.authTag,
  41328. info->cipher.aesccm_dec.authTagSz,
  41329. info->cipher.aesccm_dec.authIn,
  41330. info->cipher.aesccm_dec.authInSz);
  41331. /* reset devId */
  41332. info->cipher.aesccm_dec.aes->devId = devIdArg;
  41333. }
  41334. }
  41335. #endif
  41336. #ifndef NO_DES3
  41337. if (info->cipher.type == WC_CIPHER_DES3) {
  41338. if (info->cipher.enc) {
  41339. /* set devId to invalid, so software is used */
  41340. info->cipher.des3.des->devId = INVALID_DEVID;
  41341. ret = wc_Des3_CbcEncrypt(
  41342. info->cipher.des3.des,
  41343. info->cipher.des3.out,
  41344. info->cipher.des3.in,
  41345. info->cipher.des3.sz);
  41346. /* reset devId */
  41347. info->cipher.des3.des->devId = devIdArg;
  41348. }
  41349. else {
  41350. /* set devId to invalid, so software is used */
  41351. info->cipher.des3.des->devId = INVALID_DEVID;
  41352. ret = wc_Des3_CbcDecrypt(
  41353. info->cipher.des3.des,
  41354. info->cipher.des3.out,
  41355. info->cipher.des3.in,
  41356. info->cipher.des3.sz);
  41357. /* reset devId */
  41358. info->cipher.des3.des->devId = devIdArg;
  41359. }
  41360. }
  41361. #endif /* !NO_DES3 */
  41362. #endif /* !NO_AES || !NO_DES3 */
  41363. }
  41364. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  41365. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  41366. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  41367. #if !defined(NO_SHA)
  41368. if (info->hash.type == WC_HASH_TYPE_SHA) {
  41369. if (info->hash.sha1 == NULL)
  41370. return NOT_COMPILED_IN;
  41371. /* set devId to invalid, so software is used */
  41372. info->hash.sha1->devId = INVALID_DEVID;
  41373. if (info->hash.in != NULL) {
  41374. ret = wc_ShaUpdate(
  41375. info->hash.sha1,
  41376. info->hash.in,
  41377. info->hash.inSz);
  41378. }
  41379. if (info->hash.digest != NULL) {
  41380. ret = wc_ShaFinal(
  41381. info->hash.sha1,
  41382. info->hash.digest);
  41383. }
  41384. /* reset devId */
  41385. info->hash.sha1->devId = devIdArg;
  41386. }
  41387. else
  41388. #endif
  41389. #if !defined(NO_SHA256)
  41390. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  41391. if (info->hash.sha256 == NULL)
  41392. return NOT_COMPILED_IN;
  41393. /* set devId to invalid, so software is used */
  41394. info->hash.sha256->devId = INVALID_DEVID;
  41395. if (info->hash.in != NULL) {
  41396. ret = wc_Sha256Update(
  41397. info->hash.sha256,
  41398. info->hash.in,
  41399. info->hash.inSz);
  41400. }
  41401. if (info->hash.digest != NULL) {
  41402. ret = wc_Sha256Final(
  41403. info->hash.sha256,
  41404. info->hash.digest);
  41405. }
  41406. /* reset devId */
  41407. info->hash.sha256->devId = devIdArg;
  41408. }
  41409. else
  41410. #endif
  41411. #ifdef WOLFSSL_SHA384
  41412. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  41413. if (info->hash.sha384 == NULL)
  41414. return NOT_COMPILED_IN;
  41415. #ifndef NO_SHA2_CRYPTO_CB
  41416. /* set devId to invalid, so software is used */
  41417. info->hash.sha384->devId = INVALID_DEVID;
  41418. #endif
  41419. if (info->hash.in != NULL) {
  41420. ret = wc_Sha384Update(
  41421. info->hash.sha384,
  41422. info->hash.in,
  41423. info->hash.inSz);
  41424. }
  41425. if (info->hash.digest != NULL) {
  41426. ret = wc_Sha384Final(
  41427. info->hash.sha384,
  41428. info->hash.digest);
  41429. }
  41430. #ifndef NO_SHA2_CRYPTO_CB
  41431. /* reset devId */
  41432. info->hash.sha384->devId = devIdArg;
  41433. #endif
  41434. }
  41435. else
  41436. #endif
  41437. #ifdef WOLFSSL_SHA512
  41438. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  41439. if (info->hash.sha512 == NULL)
  41440. return NOT_COMPILED_IN;
  41441. #ifndef NO_SHA2_CRYPTO_CB
  41442. /* set devId to invalid, so software is used */
  41443. info->hash.sha512->devId = INVALID_DEVID;
  41444. #endif
  41445. if (info->hash.in != NULL) {
  41446. ret = wc_Sha512Update(
  41447. info->hash.sha512,
  41448. info->hash.in,
  41449. info->hash.inSz);
  41450. }
  41451. if (info->hash.digest != NULL) {
  41452. ret = wc_Sha512Final(
  41453. info->hash.sha512,
  41454. info->hash.digest);
  41455. }
  41456. #ifndef NO_SHA2_CRYPTO_CB
  41457. /* reset devId */
  41458. info->hash.sha512->devId = devIdArg;
  41459. #endif
  41460. }
  41461. else
  41462. #endif
  41463. {
  41464. }
  41465. }
  41466. #endif /* !NO_SHA || !NO_SHA256 */
  41467. #ifndef NO_HMAC
  41468. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  41469. if (info->hmac.hmac == NULL)
  41470. return NOT_COMPILED_IN;
  41471. /* set devId to invalid, so software is used */
  41472. info->hmac.hmac->devId = INVALID_DEVID;
  41473. if (info->hash.in != NULL) {
  41474. ret = wc_HmacUpdate(
  41475. info->hmac.hmac,
  41476. info->hmac.in,
  41477. info->hmac.inSz);
  41478. }
  41479. else if (info->hash.digest != NULL) {
  41480. ret = wc_HmacFinal(
  41481. info->hmac.hmac,
  41482. info->hmac.digest);
  41483. }
  41484. /* reset devId */
  41485. info->hmac.hmac->devId = devIdArg;
  41486. }
  41487. #endif
  41488. (void)devIdArg;
  41489. (void)myCtx;
  41490. return ret;
  41491. }
  41492. #ifdef WOLF_CRYPTO_CB_FIND
  41493. static int myCryptoCbFind(int currentId, int algoType)
  41494. {
  41495. /* can have algo specific overrides here
  41496. switch (algoType) {
  41497. i.e.
  41498. WC_ALGO_TYPE_CMAC
  41499. WC_ALGO_TYPE_SEED
  41500. WC_ALGO_TYPE_HMAC
  41501. WC_ALGO_TYPE_HASH
  41502. WC_ALGO_TYPE_CIPHER
  41503. WC_ALGO_TYPE_PK
  41504. }
  41505. */
  41506. (void)algoType;
  41507. if (currentId == INVALID_DEVID) {
  41508. /* can override invalid devid found with 1 */
  41509. }
  41510. return currentId;
  41511. }
  41512. #endif /* WOLF_CRYPTO_CB_FIND */
  41513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  41514. {
  41515. wc_test_ret_t ret = 0;
  41516. int origDevId = devId;
  41517. myCryptoDevCtx myCtx;
  41518. /* example data for callback */
  41519. myCtx.exampleVar = 1;
  41520. /* set devId to something other than INVALID_DEVID */
  41521. devId = 1;
  41522. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  41523. #ifdef WOLF_CRYPTO_CB_FIND
  41524. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  41525. #endif /* WOLF_CRYPTO_CB_FIND */
  41526. #ifndef WC_NO_RNG
  41527. if (ret == 0)
  41528. ret = random_test();
  41529. #endif /* WC_NO_RNG */
  41530. #if !defined(NO_RSA)
  41531. PRIVATE_KEY_UNLOCK();
  41532. if (ret == 0)
  41533. ret = rsa_test();
  41534. PRIVATE_KEY_LOCK();
  41535. #endif
  41536. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41537. PRIVATE_KEY_UNLOCK();
  41538. if (ret == 0)
  41539. ret = rsa_onlycb_test(&myCtx);
  41540. PRIVATE_KEY_LOCK();
  41541. #endif
  41542. #if defined(HAVE_ECC)
  41543. PRIVATE_KEY_UNLOCK();
  41544. if (ret == 0)
  41545. ret = ecc_test();
  41546. PRIVATE_KEY_LOCK();
  41547. #endif
  41548. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41549. PRIVATE_KEY_UNLOCK();
  41550. if (ret == 0)
  41551. ret = ecc_onlycb_test(&myCtx);
  41552. PRIVATE_KEY_LOCK();
  41553. #endif
  41554. #ifdef HAVE_ED25519
  41555. if (ret == 0)
  41556. ret = ed25519_test();
  41557. #endif
  41558. #ifdef HAVE_CURVE25519
  41559. if (ret == 0)
  41560. ret = curve25519_test();
  41561. #endif
  41562. #ifndef NO_AES
  41563. #ifdef HAVE_AESGCM
  41564. if (ret == 0)
  41565. ret = aesgcm_test();
  41566. #endif
  41567. #ifdef HAVE_AES_CBC
  41568. if (ret == 0)
  41569. ret = aes_test();
  41570. #endif
  41571. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  41572. if (ret == 0)
  41573. ret = aesccm_test();
  41574. #endif
  41575. #endif /* !NO_AES */
  41576. #ifndef NO_DES3
  41577. if (ret == 0)
  41578. ret = des3_test();
  41579. #endif /* !NO_DES3 */
  41580. #ifndef NO_SHA
  41581. if (ret == 0)
  41582. ret = sha_test();
  41583. #endif
  41584. #ifndef NO_SHA256
  41585. if (ret == 0)
  41586. ret = sha256_test();
  41587. #endif
  41588. #ifdef WOLFSSL_SHA384
  41589. if (ret == 0)
  41590. ret = sha384_test();
  41591. #endif
  41592. #ifdef WOLFSSL_SHA512
  41593. if (ret == 0)
  41594. ret = sha512_test();
  41595. #endif
  41596. #ifndef NO_HMAC
  41597. #ifndef NO_SHA
  41598. if (ret == 0)
  41599. ret = hmac_sha_test();
  41600. #endif
  41601. #ifndef NO_SHA256
  41602. if (ret == 0)
  41603. ret = hmac_sha256_test();
  41604. #endif
  41605. #endif
  41606. #ifndef NO_PWDBASED
  41607. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  41608. if (ret == 0)
  41609. ret = pbkdf2_test();
  41610. #endif
  41611. #endif
  41612. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  41613. if (ret == 0)
  41614. ret = cmac_test();
  41615. #endif
  41616. /* restore devId */
  41617. devId = origDevId;
  41618. return ret;
  41619. }
  41620. #endif /* WOLF_CRYPTO_CB */
  41621. #ifdef WOLFSSL_CERT_PIV
  41622. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  41623. {
  41624. wc_test_ret_t ret;
  41625. wc_CertPIV piv;
  41626. /* Template for Identiv PIV cert, nonce and signature */
  41627. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  41628. 0x0A, 0x0B,
  41629. 0x53, 0x09, /* NIST PIV Cert */
  41630. 0x70, 0x02, /* Certificate */
  41631. 0x30, 0x00,
  41632. 0x71, 0x01, 0x05, /* Cert Info */
  41633. 0xFE, 0x00, /* Error Detection */
  41634. 0x0B, 0x01, 0x00, /* Nonce */
  41635. 0x0C, 0x01, 0x00, /* Signed Nonce */
  41636. };
  41637. /* PIV certificate data including certificate, info and error detection. */
  41638. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  41639. 0x53, 0x09, /* NIST PIV Cert */
  41640. 0x70, 0x02, /* Certificate */
  41641. 0x30, 0x00,
  41642. 0x71, 0x01, 0x04, /* Cert Info */
  41643. 0xFE, 0x00, /* Error Detection */
  41644. };
  41645. XMEMSET(&piv, 0, sizeof(piv));
  41646. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  41647. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  41648. if (ret != 0) {
  41649. return WC_TEST_RET_ENC_EC(ret);
  41650. }
  41651. if (!piv.isIdentiv) {
  41652. return WC_TEST_RET_ENC_NC;
  41653. }
  41654. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  41655. return WC_TEST_RET_ENC_NC;
  41656. }
  41657. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  41658. return WC_TEST_RET_ENC_NC;
  41659. }
  41660. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  41661. return WC_TEST_RET_ENC_NC;
  41662. }
  41663. if (!piv.isX509) {
  41664. return WC_TEST_RET_ENC_NC;
  41665. }
  41666. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  41667. return WC_TEST_RET_ENC_NC;
  41668. }
  41669. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  41670. return WC_TEST_RET_ENC_NC;
  41671. }
  41672. XMEMSET(&piv, 0, sizeof(piv));
  41673. /* Test with NIST PIV format */
  41674. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  41675. if (ret != 0) {
  41676. return WC_TEST_RET_ENC_EC(ret);
  41677. }
  41678. if (piv.isIdentiv) {
  41679. return WC_TEST_RET_ENC_NC;
  41680. }
  41681. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  41682. return WC_TEST_RET_ENC_NC;
  41683. }
  41684. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  41685. return WC_TEST_RET_ENC_NC;
  41686. }
  41687. if ((piv.compression != 0)) {
  41688. return WC_TEST_RET_ENC_NC;
  41689. }
  41690. if (!piv.isX509) {
  41691. return WC_TEST_RET_ENC_NC;
  41692. }
  41693. return ret;
  41694. }
  41695. #endif /* WOLFSSL_CERT_PIV */
  41696. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  41697. static time_t time_cb(time_t* t)
  41698. {
  41699. if (t != NULL) {
  41700. *t = 99;
  41701. }
  41702. return 99;
  41703. }
  41704. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  41705. {
  41706. time_t t;
  41707. wc_test_ret_t ret;
  41708. ret = wc_SetTimeCb(time_cb);
  41709. if (ret != 0)
  41710. return WC_TEST_RET_ENC_EC(ret);
  41711. t = wc_Time(NULL);
  41712. if (t != 99)
  41713. return WC_TEST_RET_ENC_NC;
  41714. ret = wc_GetTime(&t, sizeof(time_t));
  41715. if (ret != 0)
  41716. return WC_TEST_RET_ENC_EC(ret);
  41717. if (t != 99)
  41718. return WC_TEST_RET_ENC_NC;
  41719. ret = wc_SetTimeCb(NULL);
  41720. if (ret != 0)
  41721. return WC_TEST_RET_ENC_EC(ret);
  41722. return 0;
  41723. }
  41724. #endif
  41725. #ifdef WOLFSSL_AES_SIV
  41726. typedef struct {
  41727. const byte key[33];
  41728. word32 keySz;
  41729. const byte nonce[49];
  41730. word32 nonceSz;
  41731. const byte assoc[81];
  41732. word32 assocSz;
  41733. const byte plaintext[83];
  41734. word32 plaintextSz;
  41735. const byte siv[AES_BLOCK_SIZE+1];
  41736. const byte ciphertext[82];
  41737. word32 ciphertextSz;
  41738. } AesSivTestVector;
  41739. #define AES_SIV_TEST_VECTORS 7
  41740. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  41741. {
  41742. /* These test vectors come from chrony 4.1's SIV unit tests. */
  41743. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  41744. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41745. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41746. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41747. "", 0,
  41748. "", 0,
  41749. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  41750. "", 0
  41751. },
  41752. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41753. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41754. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41755. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41756. "", 0,
  41757. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  41758. "", 0
  41759. },
  41760. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41761. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41762. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41763. "", 0,
  41764. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41765. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  41766. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  41767. },
  41768. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41769. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41770. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  41771. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  41772. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  41773. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  41774. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  41775. },
  41776. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41777. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41778. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41779. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41780. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  41781. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  41782. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  41783. },
  41784. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41785. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41786. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  41787. "\xd5", 17,
  41788. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  41789. "\xa0", 17,
  41790. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  41791. "\x08", 17,
  41792. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  41793. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  41794. "\xbd", 17
  41795. },
  41796. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41797. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41798. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  41799. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  41800. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  41801. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  41802. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  41803. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  41804. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  41805. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  41806. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  41807. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  41808. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  41809. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  41810. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  41811. "\x31", 81,
  41812. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  41813. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  41814. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  41815. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  41816. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  41817. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  41818. "\xa1", 81
  41819. }};
  41820. int i;
  41821. byte computedCiphertext[82];
  41822. byte computedPlaintext[82];
  41823. byte siv[AES_BLOCK_SIZE];
  41824. wc_test_ret_t ret = 0;
  41825. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  41826. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  41827. testVectors[i].assoc, testVectors[i].assocSz,
  41828. testVectors[i].nonce, testVectors[i].nonceSz,
  41829. testVectors[i].plaintext,
  41830. testVectors[i].plaintextSz, siv,
  41831. computedCiphertext);
  41832. if (ret != 0) {
  41833. return WC_TEST_RET_ENC_EC(ret);
  41834. }
  41835. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  41836. if (ret != 0) {
  41837. return WC_TEST_RET_ENC_NC;
  41838. }
  41839. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  41840. testVectors[i].ciphertextSz);
  41841. if (ret != 0) {
  41842. return WC_TEST_RET_ENC_NC;
  41843. }
  41844. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  41845. testVectors[i].assoc, testVectors[i].assocSz,
  41846. testVectors[i].nonce, testVectors[i].nonceSz,
  41847. computedCiphertext, testVectors[i].plaintextSz,
  41848. siv, computedPlaintext);
  41849. if (ret != 0) {
  41850. return WC_TEST_RET_ENC_EC(ret);
  41851. }
  41852. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  41853. testVectors[i].plaintextSz);
  41854. if (ret != 0) {
  41855. return WC_TEST_RET_ENC_NC;
  41856. }
  41857. }
  41858. return 0;
  41859. }
  41860. #endif
  41861. #undef ERROR_OUT
  41862. static const int fiducial4 = WC_TEST_RET_LN;
  41863. /* print the fiducial line numbers assigned above, allowing confirmation of
  41864. * source code version match when in doubt.
  41865. */
  41866. static void print_fiducials(void) {
  41867. printf(" [fiducial line numbers: %d %d %d %d]\n",
  41868. fiducial1, fiducial2, fiducial3, fiducial4);
  41869. }
  41870. #else
  41871. #ifndef NO_MAIN_DRIVER
  41872. int main(void) { return 0; }
  41873. #endif
  41874. #endif /* NO_CRYPT_TEST */